From ec78dd722c29d3517d9be2526fbd076a1c8a49df 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: Mon, 2 Nov 2026 17:40:18 +0800 Subject: [PATCH 01/59] fix: close owned PCB startup failures before ICDB recovery --- .../2026-11-02-issue-2-startup-recovery.md | 80 +++++++++ src/cbb_refresh_verify/failed_session.py | 110 ++++++++++++ src/cbb_refresh_verify/mentor_adapter.py | 8 + src/cbb_refresh_verify/verify_runner.py | 5 +- tests/test_failed_startup.py | 158 ++++++++++++++++++ 5 files changed, 360 insertions(+), 1 deletion(-) create mode 100644 docs/releases/2026-11-02-issue-2-startup-recovery.md create mode 100644 tests/test_failed_startup.py diff --git a/docs/releases/2026-11-02-issue-2-startup-recovery.md b/docs/releases/2026-11-02-issue-2-startup-recovery.md new file mode 100644 index 0000000..38f43d6 --- /dev/null +++ b/docs/releases/2026-11-02-issue-2-startup-recovery.md @@ -0,0 +1,80 @@ +# Issue #2: release the PCB launched by a failed database open + +Branch: `codex/temp-issue-2-icdb-recovery`. + +## Field failure + +The issue attachment records an iCDB lost-server-session error while opening +`1GPHY_8211`. The supervisor declined the database log viewer and cancelled the +worker. Because document identity had not been established, the existing +document-close helper was never called. PCB and its launcher remained alive; +the subsequent Repair attempt returned `REPAIR_DATABASE_IN_USE` and the other +380 queued entries were skipped. There is no evidence of native Repair actually +running on that field database. + +## Change + +The worker now has a separate startup-failure close path. It requires a current +iCDB diagnosis or a database-load-failure dialog belonging to this PCB during +startup. It checks the private desktop, excludes pre-existing process IDs, and +matches the complete PCB path and ReusableCircuit argument. Only an enabled +empty PCB main window, without modal dialogs, MDI documents or database address +records, receives a cooperative `WM_CLOSE`. A requested close is not treated as +success until process exit is observed. Ambiguous or busy sessions are retained. + +The supervisor allows up to 40 seconds for fatal-error cleanup so that the +worker's 20-second startup-close window can complete before recovery begins. +The existing repair preconditions, backups and success checks remain in force. +Each attempt writes `startup-close.json` and close-request/result events. + +## Verification + +Regression tests cover the missing pre-identity cleanup route, real observed +empty-window titles, exact path matching, pre-existing processes, another +desktop, live address records, modal and MDI windows, disabled windows, unrelated +failure records and a close request that does not result in exit. +Delayed dialog/address release and delayed process exit are also covered. +Validation: `python -m pytest -q` — 491 passed, one existing pywinauto COM-mode +warning; `python -m compileall -q src tests` and `git diff --check` passed. + +A new isolated local library was created with the existing +`tools/validate_icdb_open_repair_reopen.py` tool, output +`diagnostics/issue2-startup-20261102`. No production library was changed. +The test damages only the new copy's database header; it does not reproduce the +field server-session-loss condition. + +The real Mentor opening run produced `MENTOR_DATABASE_LOAD_FAILED`, then: + +- `startup_close_requested` for the launched PCB PID 6532; +- `startup_close_result: CLOSED`; +- worker and supervisor both recorded `process_exit_confirmed`, with no + remaining PCB processes. + +The subsequent separate native Repair run was able to start and completed as +`RESTORED_REVIEW_REQUIRED`, using the fixture's historical backup. This proves +the failed-open session no longer blocks the next repair operation in this +fixture. It does not prove automatic recovery of the field database or establish +that an older backup contains the desired design revision. + +The separate `open-after` run on the same restored fixture completed native +integration, Save/IsSaved/Close/Quit, confirmed process release and returned +`PASS`, `safe_to_continue=True`, `READY (IsVerifiedReuseBlock, raw=1)`. +The `compare` phase passed all seven checks, including absence of the original +opening error and unchanged source-fixture database SHA-256. Its +`server_session_loss_reproduced` field is explicitly `false`. + +Reproduction commands (set `PYTHONPATH=src`; setup requires a new output name): + +```powershell +python tools/validate_icdb_open_repair_reopen.py setup --output issue2-startup-20261102 +python tools/validate_icdb_open_repair_reopen.py open-before --output issue2-startup-20261102 +python tools/validate_icdb_open_repair_reopen.py repair --output issue2-startup-20261102 +python tools/validate_icdb_open_repair_reopen.py open-after --output issue2-startup-20261102 +python tools/validate_icdb_open_repair_reopen.py compare --output issue2-startup-20261102 +``` + +The stages are deliberately separate: the test fixture has a format-corruption +error and historical-backup recovery, not the field lost-session auto-repair +trigger. The test does not change production handling of old-backup recovery. + +READY filtering, hash-scope changes and CBB CRUD are outside this focused fix. diff --git a/src/cbb_refresh_verify/failed_session.py b/src/cbb_refresh_verify/failed_session.py index 76ba18e..b654b49 100644 --- a/src/cbb_refresh_verify/failed_session.py +++ b/src/cbb_refresh_verify/failed_session.py @@ -5,6 +5,116 @@ from pathlib import Path import time +def _command_args(command): + import ctypes + from ctypes import wintypes + shell = ctypes.WinDLL('shell32', use_last_error=True) + kernel = ctypes.WinDLL('kernel32', use_last_error=True) + shell.CommandLineToArgvW.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(ctypes.c_int)] + shell.CommandLineToArgvW.restype = ctypes.POINTER(wintypes.LPWSTR) + kernel.LocalFree.argtypes = [wintypes.HLOCAL] + kernel.LocalFree.restype = wintypes.HLOCAL + count = ctypes.c_int() + args = shell.CommandLineToArgvW(command, ctypes.byref(count)) + if not args: + raise ctypes.WinError(ctypes.get_last_error()) + try: + return list(args[:count.value]) + finally: + kernel.LocalFree(ctypes.cast(args, wintypes.HLOCAL)) + + +def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: None, + timeout_seconds=20): + """Normally close our empty PCB after a diagnosed database startup failure. + + No COM attach: there is no document, and the global ROT can name another + session. Unknown windows, live database addresses and other desktops are + retained. WM_CLOSE is cooperative; no save prompt is accepted or killed. + """ + import win32con, win32gui, win32process + from .mentor_adapter import _process_details + from .native_repair_ui import _current_desktop_name + from .icdb_ownership import unreleased_addresses + root = Path(job['root']) + result = {'status': 'RETAINED', 'pcb_pid': pcb_pid} + try: + diagnosed = False + diagnosis_path = root/'diagnostics/icdb-recovery.json' + if diagnosis_path.is_file(): + diagnosis = json.loads(diagnosis_path.read_text('utf-8')) + diagnosed = diagnosis.get('error_code') == 'ICDB_REPAIR_REQUIRED' + dialogs_path = root/'dialogs.jsonl' + if not diagnosed and dialogs_path.is_file(): + for line in dialogs_path.read_text('utf-8').splitlines(): + try: + dialog = json.loads(line) + except ValueError: + continue + if (dialog.get('pid') == pcb_pid and dialog.get('phase') == 'PCB_STARTING' + and dialog.get('error_code') == 'MENTOR_DATABASE_LOAD_FAILED'): + diagnosed = True + if not diagnosed: + raise ValueError('STARTUP_FAILURE_UNCONFIRMED') + desktop = job.get('desktop', '') + if (job.get('interactive_input') or not desktop.startswith('CbbVerify_') + or _current_desktop_name() != desktop): + raise ValueError('STARTUP_DESKTOP_UNCONFIRMED') + if not pcb_pid or pcb_pid in baseline_pids: + raise ValueError('STARTUP_PROCESS_UNCONFIRMED') + expected = os.path.normcase(os.path.abspath(job['pcb'])) + deadline = time.monotonic() + max(0, timeout_seconds) + sent = False + while True: + records = [r for r in _process_details('ExpeditionPCB.exe') if r[0] == pcb_pid] + if not records: + result['status'] = 'CLOSED' + break + _, executable, command, _ = records[0] + args = _command_args(command) + if (Path(executable).name.casefold() != 'expeditionpcb.exe' + or '-reusablecircuit' not in [a.casefold() for a in args] + or expected not in [os.path.normcase(os.path.abspath(a)) for a in args[1:]]): + raise ValueError('STARTUP_PROCESS_UNCONFIRMED') + if not sent: + windows = [] + def observe(hwnd, _): + if (win32process.GetWindowThreadProcessId(hwnd)[1] == pcb_pid + and win32gui.IsWindowVisible(hwnd)): + windows.append(hwnd) + win32gui.EnumWindows(observe, None) + main = [w for w in windows if win32gui.GetClassName(w).startswith('Afx:') + and win32gui.GetWindowText(w).strip() in ( + 'Expedition PCB', 'Expedition PCB - Reusable Block') + and win32gui.IsWindowEnabled(w)] + dialogs = any(win32gui.GetClassName(w) == '#32770' for w in windows) + documents = [] + def child(hwnd, _): + if (win32gui.GetClassName(hwnd) == 'MDIClient' + and win32gui.GetWindow(hwnd, win32con.GW_CHILD)): + documents.append(hwnd) + if len(main) == 1: + win32gui.EnumChildWindows(main[0], child, None) + addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb']) + if len(main) == 1 and not dialogs and not documents and not addresses: + win32gui.PostMessage(main[0], win32con.WM_CLOSE, 0, 0) + sent = True + emit(stage='startup_close_requested', pcb_pid=pcb_pid, hwnd=main[0]) + # Observe exit even when the caller requested no extra wait. + continue + result['detail'] = 'STARTUP_IDLE_UNCONFIRMED: 等待空闲窗口和数据库释放' + if time.monotonic() >= deadline: + if sent: + result['detail'] = 'STARTUP_EXIT_UNCONFIRMED: 已请求正常关闭,但 PCB 尚未退出' + break + time.sleep(.2) + except Exception as error: + result['detail'] = str(error) + (root/'startup-close.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8') + emit(stage='startup_close_result', **result) + return result + + def close_failed_document(job, *, emit=lambda **event: None, timeout_seconds=30): from .desktop_worker import launch_save_helper root = Path(job['root']) diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index d61f524..7925223 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -863,6 +863,14 @@ def execute_job(job, emit): lm_session.close() except Exception as error: emit(stage='failure_close_unconfirmed', detail=str(error)) + elif not completed and worker_pid is not None: + try: + from .failed_session import close_failed_startup + failed_close = close_failed_startup(job, worker_pid, baseline_pids=pcb_before, emit=emit) + if failed_close.get('status') == 'CLOSED' and lm_session is not None: + lm_session.close() + except Exception as error: + emit(stage='startup_close_unconfirmed', detail=str(error)) cleanup_verify_processes( lm_pid, worker_pid, diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 84e95fe..3a0e4a8 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -866,7 +866,10 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run if dialog_result.error_code: _write_failure_windows(root, latest_snapshots) _write_process_tree(root, process, target_pid) - _stop_worker(process) + # A failed database load has no document to close. Give the + # worker time to normally close its owned empty PCB before + # recovery checks for remaining native database clients. + _stop_worker(process, timeout=40) return finish(VerifyResult( 'FAIL', stderr=f'{dialog_result.error_code}: {dialog_result.detail}', diff --git a/tests/test_failed_startup.py b/tests/test_failed_startup.py new file mode 100644 index 0000000..3fc0b40 --- /dev/null +++ b/tests/test_failed_startup.py @@ -0,0 +1,158 @@ +import json +from pathlib import Path + +import pytest + +from cbb_refresh_verify import failed_session, mentor_adapter + + +def startup_fixture(tmp_path, monkeypatch): + import win32gui, win32process + from cbb_refresh_verify import native_repair_ui + job = {'root': str(tmp_path), 'pcb': str(tmp_path/'PCB/CBB.pcb'), + 'project': str(tmp_path/'CBB.prj'), 'lmc_file': str(tmp_path/'lib.lmc'), + 'desktop': 'CbbVerify_test'} + diagnostics = tmp_path/'diagnostics' + diagnostics.mkdir() + (diagnostics/'icdb-recovery.json').write_text(json.dumps({ + 'error_code': 'ICDB_REPAIR_REQUIRED', 'scope': 'project', + 'database_path': str(tmp_path/'database'), 'session': 'lost-session'})) + records = [(12, r'C:\Mentor\wg\ExpeditionPCB.exe', + f'ExpeditionPCB.exe "{job["pcb"]}" -ReusableCircuit', 11)] + monkeypatch.setattr(mentor_adapter, '_process_details', lambda _: list(records)) + monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'CbbVerify_test') + monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: fn(100, arg)) + monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda *args: None) + monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda _: (1, 12)) + monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda _: True) + monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda _: True) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda _: 'Expedition PCB') + monkeypatch.setattr(win32gui, 'GetClassName', lambda _: 'Afx:main') + messages = [] + def post(hwnd, message, wparam, lparam): + messages.append((hwnd, message)) + records.clear() + monkeypatch.setattr(win32gui, 'PostMessage', post) + return job, records, messages + + +@pytest.mark.parametrize('title', ['Expedition PCB', 'Expedition PCB - Reusable Block']) +def test_failed_startup_normally_closes_owned_empty_pcb(tmp_path, monkeypatch, title): + import win32con + import win32gui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda _: title) + result = failed_session.close_failed_startup(job, 12, baseline_pids=(), timeout_seconds=0) + assert result['status'] == 'CLOSED' + assert not records + assert messages == [(100, win32con.WM_CLOSE)] + assert json.loads((tmp_path/'startup-close.json').read_text())['status'] == 'CLOSED' + + +@pytest.mark.parametrize('obstacle', ['baseline', 'wrong_path', 'path_prefix', 'desktop', 'address', + 'dialog', 'document_title', 'mdi_document', 'disabled', 'no_diagnosis']) +def test_failed_startup_preserves_unconfirmed_or_busy_session(tmp_path, monkeypatch, obstacle): + import win32gui + from cbb_refresh_verify import native_repair_ui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + baseline = () + if obstacle == 'baseline': baseline = (12,) + if obstacle == 'wrong_path': records[0] = (12, records[0][1], 'other.pcb -ReusableCircuit', 11) + if obstacle == 'path_prefix': + records[0] = (12, records[0][1], f'pcb.exe "{job["pcb"]}.other" -ReusableCircuit', 11) + if obstacle == 'desktop': monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'Default') + if obstacle == 'address': + address = tmp_path/'database/cdbsvr/sAddress.adr' + address.parent.mkdir(parents=True) + address.write_text('owner') + if obstacle == 'dialog': monkeypatch.setattr(win32gui, 'GetClassName', lambda _: '#32770') + if obstacle == 'document_title': monkeypatch.setattr(win32gui, 'GetWindowText', lambda _: 'CBB.pcb - Expedition PCB') + if obstacle == 'mdi_document': + monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, fn, arg: fn(101, arg)) + monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'MDIClient' if hwnd == 101 else 'Afx:main') + monkeypatch.setattr(win32gui, 'GetWindow', lambda *args: 102) + if obstacle == 'disabled': monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda _: False) + if obstacle == 'no_diagnosis': (tmp_path/'diagnostics/icdb-recovery.json').unlink() + result = failed_session.close_failed_startup(job, 12, baseline_pids=baseline, timeout_seconds=0) + assert result['status'] == 'RETAINED' + assert records + assert not messages + + +def test_requested_close_without_exit_is_not_reported_closed(tmp_path, monkeypatch): + import win32gui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + monkeypatch.setattr(win32gui, 'PostMessage', lambda hwnd, message, *_: messages.append((hwnd, message))) + result = failed_session.close_failed_startup(job, 12, timeout_seconds=0) + assert result['status'] == 'RETAINED' + assert 'STARTUP_EXIT_UNCONFIRMED' in result['detail'] + assert len(messages) == 1 + assert records + + +@pytest.mark.parametrize('obstacle', ['address', 'dialog', 'process']) +def test_startup_close_waits_for_native_state_transitions(tmp_path, monkeypatch, obstacle): + from types import SimpleNamespace + import win32gui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + ticks = [0] + address = tmp_path/'database/cdbsvr/sAddress.adr' + if obstacle == 'address': + address.parent.mkdir(parents=True) + address.write_text('owner') + if obstacle == 'dialog': + monkeypatch.setattr(win32gui, 'GetClassName', + lambda _: '#32770' if ticks[0] == 0 else 'Afx:main') + if obstacle == 'process': + monkeypatch.setattr(win32gui, 'PostMessage', + lambda hwnd, message, *_: messages.append((hwnd, message))) + def advance(seconds): + ticks[0] += seconds + if obstacle == 'address' and address.exists(): address.unlink() + if obstacle == 'process' and messages: records.clear() + monkeypatch.setattr(failed_session, 'time', SimpleNamespace( + monotonic=lambda: ticks[0], sleep=advance)) + result = failed_session.close_failed_startup(job, 12, timeout_seconds=1) + assert ticks[0] > 0 + assert result['status'] == 'CLOSED' + assert len(messages) == 1 + assert not records + + +def test_worker_closes_startup_failure_before_document_identity(tmp_path, monkeypatch): + from test_verify_worker_stability import exercise_worker + job, events, execute = exercise_worker(tmp_path, monkeypatch) + job['desktop'] = 'CbbVerify_test' + def fail_open(*args, **kwargs): + raise RuntimeError('VERIFY_CANCELLED: database open failed') + monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', fail_open) + # Native process/window behavior is covered above; here verify the worker + # actually reaches startup cleanup instead of requiring a loaded document. + def close(job, pid, **kwargs): + kwargs['emit'](stage='startup_close_result', status='CLOSED', pcb_pid=pid) + return {'status': 'CLOSED'} + monkeypatch.setattr(failed_session, 'close_failed_startup', close, raising=False) + with pytest.raises(RuntimeError, match='VERIFY_CANCELLED'): + execute() + assert any(e['stage'] == 'startup_close_result' and e['pcb_pid'] == 2 for e in events) + assert not any(e['stage'] == 'save_start' for e in events) + + +def test_failed_startup_accepts_current_owned_database_failure_dialog(tmp_path, monkeypatch): + job, records, messages = startup_fixture(tmp_path, monkeypatch) + (tmp_path/'diagnostics/icdb-recovery.json').unlink() + (tmp_path/'dialogs.jsonl').write_text(json.dumps({ + 'pid': 12, 'phase': 'PCB_STARTING', 'error_code': 'MENTOR_DATABASE_LOAD_FAILED'})+'\n') + result = failed_session.close_failed_startup(job, 12, timeout_seconds=0) + assert result['status'] == 'CLOSED' + assert not records + + +@pytest.mark.parametrize('pid,phase', [(99, 'PCB_STARTING'), (12, 'SAVING')]) +def test_failed_startup_rejects_unrelated_failure_dialog(tmp_path, monkeypatch, pid, phase): + job, records, messages = startup_fixture(tmp_path, monkeypatch) + (tmp_path/'diagnostics/icdb-recovery.json').unlink() + (tmp_path/'dialogs.jsonl').write_text(json.dumps({ + 'pid': pid, 'phase': phase, 'error_code': 'MENTOR_DATABASE_LOAD_FAILED'})+'\n') + assert failed_session.close_failed_startup(job, 12, timeout_seconds=0)['status'] == 'RETAINED' + assert not messages -- 2.39.5 From 44b808b8e212d209703321c4c692c6d400dacf7d 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: Mon, 2 Nov 2026 17:52:50 +0800 Subject: [PATCH 02/59] docs: record approved issue 2 implementation plan --- .../plans/2026-11-02-issue-2-management.md | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 docs/superpowers/plans/2026-11-02-issue-2-management.md diff --git a/docs/superpowers/plans/2026-11-02-issue-2-management.md b/docs/superpowers/plans/2026-11-02-issue-2-management.md new file mode 100644 index 0000000..503cb04 --- /dev/null +++ b/docs/superpowers/plans/2026-11-02-issue-2-management.md @@ -0,0 +1,104 @@ +# Issue #2 implementation plan + +Approved by user on 2026-11-02. Continue branch codex/temp-issue-2-icdb-recovery, +baseline ec78dd7 (491 tests). Do not merge, publish or close the issue. Preserve +untracked review.diff. Python 3.12, PySide6, Mentor EE7.9.5, private desktop by +default. No force termination of native database owners, deleting locks, +guessing native write interfaces, or editing LMC binary contents. + +Each task uses regression tests, focused validation and a separate commit. +Real field lost-session reproduction and Windows 10 validation must be reported +as unverified when unavailable; header corruption is not a substitute. + +## Task 1: ICDB failure lifecycle and repair outcomes + +Extend the existing owned-empty-PCB close path to wait for its launcher as well. +Cover COM connected without a document, delayed dialogs/addresses/process exit. +Native Repair must report missing healthy backup, native repair failure, database +busy, unknown dialogs and timeout/unreleased distinctly using this run's native +logs and Output. Safely close an idle, conclusively failed native repair tool; +never act on unrecognized prompts or active native operations. +Preserve original error, original evidence and all repair evidence. Record release +state. Repair success in summaries means original native repair succeeded AND +same-project reopening, verification and authoritative GREEN succeeded. Historical +backup restoration remains RESTORED_REVIEW_REQUIRED. A conclusively failed repair +may continue independent CBB items only if all relevant processes and addresses +are released and scope is project-local; shared-library/unknown ownership stops. +Add sanitized replay of issue2 lost-session/open-failure events with asserted target, +stage and invocation order; one automatic repair maximum, no repeated PCB loop. +Maintain VerifyRequest/VerifyResult compatibility. Files primarily failed_session, +pcb_bridge, mentor_adapter, native_repair[_ui], verify_runner, icdb_maintenance and +their tests. Update release evidence. No native live operations without coordinator +agreement (only one native operation at a time). Coordinator builds candidate and +runs frozen tests after review. Field fault sample is not in the attachment (logs +only); do not claim field recovery. + +## Task 2: Search, filtering, selection and non-READY maintenance + +Implement local name/partition keyword search and filters: all, READY, non-READY, +PENDING, unknown/read errors, missing project. Non-READY includes unconfirmed states +without calling them corrupt. Show visible/READY/error counts; no Mentor rereads on +filtering. Management selection permits READY; verify/maintenance/default backup +recovery exclude READY. Operations target current visible checked rows; hiding a row +clears its checkbox. Provide select visible/clear selection. Maintenance entry, +confirmation, progress and report agree on non-READY target set. Existing verify-all +means visible eligible rows, maintenance defaults visible eligible rows with checked +subset if any. Refresh affected authoritative statuses after operations, retain search +and filter. No premature integration of CRUD; expose selected records for Task4. +Maintain CbbRecord compatibility and explicit status separate from action result. +Tests include mixed READY/PENDING/errors/missing, filtering after state transitions, +selection scope and 381/3000 row responsiveness. Files UI main_window, small focused +filter helper if useful, backup dialog and maintenance report + tests. Avoid unrelated +UI overhaul. Commit and report focused tests plus full suite once. + +## Task 3: Scoped primary-file hashes and verified backups + +Copy and retain complete backup trees. Restrict SHA256 and semantic before/after +comparison to primary project/design/database/constraint files and required library +dependencies: PRJ/PCB/DXP, icdb.dat, Layout, schematic/logic/symbol assets, CES/config, +target LMC and shared database dependencies. Explicitly exclude runtime logs/temp/ +locks/address records, historical backups, PDF/generated output from content hashes; +address files still participate in idle checks. Treat unclassified engineering data +conservatively as primary rather than accidentally excluding it. For central-library +manifest, only target CBB Work/Cert plus active library configuration/shared library +dependency trees; do not hash unrelated CBB projects. +During copy validate tree membership and file size/mtime before/after; primary files +also source/destination SHA256, including locked database stream bytes. Logs changing +after successful snapshot must not invalidate subsequent semantic comparison, while +primary changes/additions/deletions must block install. Manifest is versioned and +contains relative paths, size, hash and scope; progress gives hashed files/bytes/stage. +Keep existing validate-copy -> target conflict check -> retain Work -> install -> +reopen verification. Preserve old backups and evidence on failure. Add tests for logs +only, all primary categories, changed/deleted sources, target concurrency, copied byte +corruption and preserved recovery copies. Files backup_recovery and new focused +manifest helper + tests. Reuse helper in management Task4 without changing old public +Verify contracts. Document explicit rules/compatibility, commit after tests. + +## Task 4: Native CBB management and delivery + +Integrate add, same-name update, remove in current application. Input complete PRJ/PCB +projects, no Packager/PDF/part replacement/rename/partition move. Add conflicts never +overwrite. Update matches existing name/partition and backs up complete old project +and library recovery snapshot. Remove backs up then uses verified Mentor native +operation; reread confirms absence. Never fake removal by deleting directories. +Add/update reread identity, default auto-verify optional per batch, distinguish upload +failed, uploaded-but-verification-failed, uploaded-and-verified. Preserve source files. +Use isolated native adapter: capability detection by installed version/interface plus +isolated validation. Prefer proven COM/command; verified private desktop controls only +fallback, never foreground input. Unsupported operations disabled with exact reason. +Use cbb-upload-795 as research only, no external runtime dependency. Native API research +comes from coordinator. No fabricated operation parameters. Snapshot/readback result +must correspond to actual library entry and source engineering identity. +Requests/results contain operation, library, CBB, partition, source, auto_verify, +stage/status, safe_to_continue and evidence path. Serialize all writes with existing +verification mutex and a library-level lock compatible with original uploader; apply +coordination to verify/recovery too. Stop when native release uncertain. Persist item +checkpoints and show unfinished tasks on restart, never replay automatically. +Provide safe preview/confirmation for destructive operations; backups outside target. +Test isolated add/conflict/update/remove/reimport, cancellation, timeout/network errors, +partial successes, no-op/lying native response, busy library, restart checkpoints. +Implement capabilities conservatively: if actual native operation cannot be proven, +its UI remains disabled with actionable explanation and validation gap is explicit. +Full Python/compile/diff checks, candidate frozen smoke, Windows11 native scenario; +Windows10 and real field sample explicitly unverified if inaccessible. Coordinator +provides final review and source/frozen deliverables without merging/publishing. -- 2.39.5 From aa760c28391024760ecbb7e977c38bf6f1874ae8 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: Mon, 2 Nov 2026 18:14:38 +0800 Subject: [PATCH 03/59] fix: classify ICDB repair outcomes and gate released recovery --- .../2026-11-02-issue-2-startup-recovery.md | 61 +++ src/cbb_refresh_verify/failed_session.py | 22 +- src/cbb_refresh_verify/icdb_maintenance.py | 7 +- src/cbb_refresh_verify/mentor_adapter.py | 3 +- src/cbb_refresh_verify/native_repair.py | 51 ++- src/cbb_refresh_verify/native_repair_ui.py | 118 +++++- src/cbb_refresh_verify/pcb_bridge.py | 5 + src/cbb_refresh_verify/verify_runner.py | 50 ++- .../issue2-lost-session-sanitized.json | 30 ++ tests/test_failed_startup.py | 30 ++ tests/test_native_repair.py | 389 +++++++++++++++++- tests/test_pcb_bridge.py | 12 + 12 files changed, 751 insertions(+), 27 deletions(-) create mode 100644 tests/fixtures/issue2-lost-session-sanitized.json diff --git a/docs/releases/2026-11-02-issue-2-startup-recovery.md b/docs/releases/2026-11-02-issue-2-startup-recovery.md index 38f43d6..cbe4620 100644 --- a/docs/releases/2026-11-02-issue-2-startup-recovery.md +++ b/docs/releases/2026-11-02-issue-2-startup-recovery.md @@ -78,3 +78,64 @@ error and historical-backup recovery, not the field lost-session auto-repair trigger. The test does not change production handling of old-backup recovery. READY filtering, hash-scope changes and CBB CRUD are outside this focused fix. + +## Approved Task 1 follow-up: lifecycle and repair outcomes + +The empty-PCB close path now receives the launcher process handle and waits for +its normal exit after PCB exits. It checks database addresses again during that +wait, so a late address record prevents a false `CLOSED` result. The startup +record includes PCB, launcher and address release state. A connected COM server +with no active document emits `pcb_document_wait`; it remains in startup and +continues reading the current native failure rather than implying identity. + +Native Repair reads bounded Output text and records only the text appended after +this run's Repair invocation as operation evidence. Database logs are restricted +to the current native PID and start time. Output, native log copies, the complete +backup, native result and attempt record remain available under `native-repair`. +Detailed native evidence is preserved separately from its directory path. + +The result distinguishes `MISSING_HEALTHY_BACKUP`, `NATIVE_REPAIR_FAILED`, +`DATABASE_BUSY`, `UNKNOWN_DIALOG`, and `TIMEOUT_UNRELEASED`. A failed tool receives +cooperative close only after a terminal native error, an enabled exact-project +main window, no other windows/prompts and no target database address. The worker +then waits for normal process exit. Unknown/custom prompts and incomplete native +operations receive no close action. A failure classification can coexist with +unreleased state; that state always stops the queue. A busy-project message in +this fresh native process's initial loading Output prevents Repair invocation +entirely; it is not hidden by the later invocation baseline. + +Before reopening or continuing, the parent independently checks relevant PCB, +Library Manager, Repair, importer and close-helper processes, all known project/ +PCB/shared-library addresses, and exclusive read access to every existing known +database. Process-observation errors and exclusive-access failures block reuse. +Only a conclusively failed, released, independently revalidated project-local +target may allow the next CBB. Shared or unknown scope, busy owners, timeouts, +unknown prompts and historical restores stop for review. + +Native `REPAIRED` is retained as `native_status`; summaries count a repair only +after the same request reopens, completes Verify and returns authoritative +`PASS/GREEN` with confirmed release. A failed reopen becomes +`REPAIR_VALIDATION_FAILED`; an unconfirmed native release prevents reopening. +Historical restoration remains `RESTORED_REVIEW_REQUIRED`. The original error, +stage and evidence references, a `pre-repair-result.json` snapshot, native +evidence and validation evidence remain attached even if the retry raises. + +`tests/fixtures/issue2-lost-session-sanitized.json` contains selected fields from +the actual issue attachment, with paths, machine and process identities +sanitized. Replay asserts `PCB_STARTING`, the selected PRJ/database, normal PCB +and launcher release before native Repair, and exactly one automatic repair and +one reopen even if the opening error repeats. Separate replays cover the actual +2026-10-28 missing-backup Output, delayed exits/addresses, unknown custom prompts, +successful native repair and historical restoration. These are nonnative tests. + +The attachment contains logs only. No matching `1GPHY_8211` faulty project was +found in the sibling `Skyworth_CBB_Lib` collection. This host runs Windows 11; +Windows 10 behavior and recovery of the real field database remain unverified. +The coordinator's prepared `diagnostics/issue2-frozen-20261102` fixture damages a +copied database header; it does not reproduce field lost-server-session loss. +Candidate packaging and frozen/native tests remain coordinator work after review. + +Task 1 checks: full suite once, 532 passed with the existing pywinauto COM warning; +after the final initial-busy gate regression, the related final suite passed all +234 tests. Compileall and `git diff --check` passed. The coordinator will run +candidate/frozen validation against the final implementation. diff --git a/src/cbb_refresh_verify/failed_session.py b/src/cbb_refresh_verify/failed_session.py index b654b49..4ecdf13 100644 --- a/src/cbb_refresh_verify/failed_session.py +++ b/src/cbb_refresh_verify/failed_session.py @@ -25,7 +25,7 @@ def _command_args(command): def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: None, - timeout_seconds=20): + timeout_seconds=20, launcher_process=None): """Normally close our empty PCB after a diagnosed database startup failure. No COM attach: there is no document, and the global ROT can name another @@ -38,6 +38,10 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: from .icdb_ownership import unreleased_addresses root = Path(job['root']) result = {'status': 'RETAINED', 'pcb_pid': pcb_pid} + result['release'] = {'pcb_exited': False, 'launcher_exited': launcher_process is None, + 'safe_to_continue': False} + if launcher_process is not None: + result['launcher_pid'] = launcher_process.pid try: diagnosed = False diagnosis_path = root/'diagnostics/icdb-recovery.json' @@ -68,8 +72,20 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: while True: records = [r for r in _process_details('ExpeditionPCB.exe') if r[0] == pcb_pid] if not records: - result['status'] = 'CLOSED' - break + addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb']) + launcher_exited = launcher_process is None or launcher_process.poll() is not None + result['release'] = {'pcb_exited': True, 'launcher_exited': launcher_exited, + 'addresses': addresses, + 'safe_to_continue': launcher_exited and not addresses} + if result['release']['safe_to_continue']: + result['status'] = 'CLOSED' + result.pop('detail', None) + break + result['detail'] = 'STARTUP_RELEASE_UNCONFIRMED: 等待启动器退出和数据库地址释放' + if time.monotonic() >= deadline: + break + time.sleep(.2) + continue _, executable, command, _ = records[0] args = _command_args(command) if (Path(executable).name.casefold() != 'expeditionpcb.exe' diff --git a/src/cbb_refresh_verify/icdb_maintenance.py b/src/cbb_refresh_verify/icdb_maintenance.py index 8644f5d..cd50ba3 100644 --- a/src/cbb_refresh_verify/icdb_maintenance.py +++ b/src/cbb_refresh_verify/icdb_maintenance.py @@ -5,7 +5,12 @@ import json def maintenance_counts(results): attempts = [attempt for result in results for attempt in result.recovery_attempts] - repaired = sum(attempt.get('status') == 'REPAIRED' for attempt in attempts) + repaired = sum(attempt.get('status') == 'REPAIRED' + and attempt.get('native_status') == 'REPAIRED' + and attempt.get('validation_passed') is True + and result.status == 'PASS' and result.library_flag == 'GREEN' + and result.safe_to_continue + for result in results for attempt in result.recovery_attempts) review = sum(attempt.get('status') == 'RESTORED_REVIEW_REQUIRED' for attempt in attempts) return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results), 'skipped': sum(r.status == 'SKIPPED' for r in results), diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index 7925223..b877b7e 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -866,7 +866,8 @@ def execute_job(job, emit): elif not completed and worker_pid is not None: try: from .failed_session import close_failed_startup - failed_close = close_failed_startup(job, worker_pid, baseline_pids=pcb_before, emit=emit) + failed_close = close_failed_startup(job, worker_pid, baseline_pids=pcb_before, emit=emit, + launcher_process=launcher_process) if failed_close.get('status') == 'CLOSED' and lm_session is not None: lm_session.close() except Exception as error: diff --git a/src/cbb_refresh_verify/native_repair.py b/src/cbb_refresh_verify/native_repair.py index 69a1834..ee5b90e 100644 --- a/src/cbb_refresh_verify/native_repair.py +++ b/src/cbb_refresh_verify/native_repair.py @@ -23,6 +23,8 @@ def select_repair_target(request, diagnosis): raise ValueError('REPAIR_TARGET_UNCONFIRMED: 缺少原生失联会话信息') if diagnosis.get('scope') == 'central_library': raise ValueError('SHARED_DATABASE_REPAIR_UNSUPPORTED: 当前原生 Repair 入口要求 PRJ,不能改修无关的 Work 工程') + if diagnosis.get('scope') != 'project': + raise ValueError('REPAIR_TARGET_UNCONFIRMED: 数据库所有权范围未知') project, pcb = preflight(request) try: reported = Path(diagnosis.get('database_path', '')).resolve(strict=True) @@ -115,7 +117,8 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None): marker = root/'attempt.json' if marker.exists(): return {'status': 'BLOCKED', 'detail': 'REPAIR_ALREADY_ATTEMPTED', 'evidence': str(root)} - record = {'status': 'BLOCKED', 'evidence': str(root)} + record = {'status': 'BLOCKED', 'evidence_path': str(root), + 'release': {'safe_to_continue': False}} try: process_record = Path(evidence_root)/'content-check-process.json' if process_record.is_file(): @@ -137,12 +140,51 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None): callback = (lambda event: progress({'block': request.block_name, **event})) if progress else None record = run_native_repair(target, root, mentor_bin=request.mentor_bin, timeout_seconds=max(60, request.timeout_seconds), progress=callback) + record['release'] = repair_release_state(request, target, record) + record['evidence_path'] = str(root) except Exception as error: record['detail'] = str(error) marker.write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8') return record +def repair_release_state(request, target, native_result): + """Require normal exit, owner/address absence and exclusive database access.""" + from .mentor_adapter import pcb_processes, _process_details + from .icdb_ownership import database_directories, unreleased_addresses + from .verify_runner import preflight + result = {**native_result.get('release', {}), 'safe_to_continue': False, + 'database_exclusive': False} + try: + project, pcb = preflight(request) + owners = [int(r[0]) for r in pcb_processes()] + for name in ('LibraryManager.exe', 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe'): + owners.extend(int(r[0]) for r in _process_details(name)) + for pid, _exe, command, _parent in _process_details('cscript.exe'): + if (not command or str(project).casefold() in command.casefold() + or str(pcb).casefold() in command.casefold()): + owners.append(int(pid)) + result.update(remaining_pids=sorted(set(owners)), + addresses=unreleased_addresses(request.lmc_file, project, pcb)) + if (native_result.get('release', {}).get('safe_to_continue') is not True + or owners or result['addresses']): + return result + import win32con, win32file + files = [target.database/'icdb.dat'] + files.extend(folder/'icdb.dat' for folder in database_directories(request.lmc_file, project, pcb) + if (folder/'icdb.dat').is_file()) + result['exclusive_databases'] = [] + for database_file in dict.fromkeys(files): + handle = win32file.CreateFile(str(database_file), win32con.GENERIC_READ, + 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + handle.Close() + result['exclusive_databases'].append(str(database_file)) + result.update(database_exclusive=True, safe_to_continue=True) + except Exception as error: + result['error'] = str(error) + return result + + def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None): """Supervise the GUI on its own desktop; a repair timeout never kills it.""" from .desktop_worker import DesktopWorker @@ -176,11 +218,12 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr outcome = root/'repair-result.json' if worker.poll() is None or not outcome.is_file(): worker.request_stop() - return {'status': 'RETAINED', 'worker_pid': worker.pid, 'desktop': desktop, - 'detail': '原生修复尚未确认结束,保留操作并停止批次', 'evidence': str(root)} + return {'status': 'TIMEOUT_UNRELEASED', 'worker_pid': worker.pid, 'desktop': desktop, + 'release': {'safe_to_continue': False}, + 'detail': '原生修复尚未确认结束,保留操作并停止批次', 'evidence_path': str(root)} result = json.loads(outcome.read_text('utf-8')) if worker.returncode != 0 and result.get('status') == 'REPAIRED': result['status'] = 'UNCONFIRMED' - return {**result, 'evidence': str(root)} + return {**result, 'evidence_path': str(root)} finally: worker.close() diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index 1edebee..8f14966 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -5,6 +5,7 @@ import json import ntpath from pathlib import Path import re +import shutil import subprocess import time import zipfile @@ -30,6 +31,23 @@ def repair_completed(diagnostic_text, database_text, *, native_backup): and 'iCDBProjectBackupRepair' in database_text and 'IC: Closing project' in database_text) +def classify_repair_evidence(diagnostic, database, restore, output): + """Classify this invocation only; an error alone does not prove native idle.""" + combined = '\n'.join((diagnostic, database, restore, output)) + terminal_error = bool(re.search(r'Repair project error\s*\[[^\r\n]+\]', output, re.I)) + closed_error = ('Diagnostic: FINISH' in diagnostic and 'IC: Closing project' in database) + if re.search(r'project is in use|database.*(?:in use|busy)|other clients are connected', combined, re.I): + outcome = 'DATABASE_BUSY' + elif re.search(r'there is no backup of (?:missing )?iCDB|no (?:healthy|valid) backup', combined, re.I): + outcome = 'MISSING_HEALTHY_BACKUP' + elif re.search(r'\bERROR\s*:|not repaired|repair.*failed|Repair project error', combined, re.I): + outcome = 'NATIVE_REPAIR_FAILED' + else: + outcome = 'UNCONFIRMED' + return {'outcome': outcome, 'failure_complete': bool( + outcome != 'UNCONFIRMED' and (terminal_error or closed_error))} + + def _current_desktop_name(): kernel32 = ctypes.WinDLL('kernel32', use_last_error=True) user32 = ctypes.WinDLL('user32', use_last_error=True) @@ -97,12 +115,36 @@ def _windows(pid): return windows -def _native_evidence(project, database, pid, started_ns, baseline): +def _native_output(windows): + """Read Output without UI activation; a hung control has a bounded deadline.""" + user32 = ctypes.WinDLL('user32', use_last_error=True) + user32.SendMessageTimeoutW.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM, + wintypes.LPARAM, wintypes.UINT, wintypes.UINT, ctypes.POINTER(ctypes.c_size_t)] + user32.SendMessageTimeoutW.restype = wintypes.LPARAM + texts = [] + for window in windows: + for child in window['children']: + if not child['class'].casefold().startswith('richedit'): + continue + buffer = ctypes.create_unicode_buffer(131072) + copied = ctypes.c_size_t() + if not user32.SendMessageTimeoutW(child['hwnd'], 0x000D, len(buffer), + ctypes.addressof(buffer), 0x0002, 200, ctypes.byref(copied)): + return None + if copied.value >= len(buffer)-1: + return None # A truncated prefix cannot establish the current operation. + texts.append(buffer.value) + return '\n'.join(texts) if texts else None + + +def _native_evidence(project, database, pid, started_ns, baseline, *, output_text=''): """Use only this native process's new diagnostic/close and backup evidence.""" + log_paths = [] def read_logs(folder): text = [] for path in (database/'cdbsvr'/folder).glob(f'*-{pid}/*.txt'): if path.stat().st_mtime_ns >= started_ns: + log_paths.append(str(path)) text.append(path.read_bytes()[-256*1024:].decode('utf-8', errors='replace')) return '\n'.join(text) from .icdb_recovery import read_log_failure @@ -122,9 +164,13 @@ def _native_evidence(project, database, pid, started_ns, baseline): diagnostic, db_log = read_logs('dbcheck'), read_logs('log') restore_log = read_logs('dbrestore') restored = re.search(r'iCDB database restored from backup:\s*([^\r\n]+)', restore_log) - valid = (not restored and str(database).casefold() in db_log.casefold() + classification = classify_repair_evidence(diagnostic, db_log, restore_log, output_text) + valid = (classification['outcome'] == 'UNCONFIRMED' and not restored + and str(database).casefold() in db_log.casefold() and repair_completed(diagnostic, db_log, native_backup=native_backup)) - return {'confirmed': bool(valid), 'native_backup': native_backup, + return {**classification, 'confirmed': bool(valid), 'native_backup': native_backup, + 'output_tail': output_text[-8192:], 'log_paths': log_paths, + 'backup_log_tail': text[-8192:], 'diagnostic_tail': diagnostic[-8192:], 'database_tail': db_log[-8192:], 'restored_from_backup': restored.group(1).strip() if restored else '', 'restore_complete': bool(restored and '[0] DONE' in restore_log @@ -140,7 +186,8 @@ def repair_worker_main(job_path): job = json.loads(Path(job_path).read_text('utf-8')) root = Path(job['root']) project, database = Path(job['project']), Path(job['database']) - result = {'status': 'BLOCKED', 'project': str(project), 'database': str(database)} + result = {'status': 'BLOCKED', 'project': str(project), 'database': str(database), + 'release': {'safe_to_continue': False}} process = None def emit(**event): with (root/'repair-events.jsonl').open('a', encoding='utf-8') as stream: @@ -175,19 +222,38 @@ def repair_worker_main(job_path): deadline = time.monotonic()+max(1, float(job['timeout'])) invoked = confirmed = closing = False evidence = None + output_baseline = None + output = '' while process.poll() is None and time.monotonic()/ReusableBlocksLP/1GPHY_8211/Work/database] session:[2026-10-20 14.03.54-18212])", + "observed_events": [ + {"stage": "pcb_launch", "strategy": "reusable_circuit_direct"}, + {"stage": "pcb_launcher_started", "launcher_pid": 11}, + {"stage": "pcb_worker_started", "pcb_pid": 12, "binding": "commandline"}, + {"stage": "native_failure_logs"}, + {"stage": "process_retained", "safe_to_continue": false}, + {"stage": "failed", "error": "VERIFY_CANCELLED: stop and normally close target document"} + ], + "dialog": { + "pid": 12, + "phase": "PCB_STARTING", + "action": "acknowledge_fatal", + "error_code": "MENTOR_DATABASE_LOAD_FAILED", + "text": "Problem encountered while opening one of the database files. See PCBLogFile.txt for details. Loading of the job has failed. Do you wish to view the log file now?" + }, + "original_result": {"status": "FAIL", "error_category": "ICDB_REPAIR_REQUIRED", "error_stage": "PCB_STARTING"}, + "original_repair": {"status": "BLOCKED", "detail": "REPAIR_DATABASE_IN_USE"} +} diff --git a/tests/test_failed_startup.py b/tests/test_failed_startup.py index 3fc0b40..28b652f 100644 --- a/tests/test_failed_startup.py +++ b/tests/test_failed_startup.py @@ -156,3 +156,33 @@ def test_failed_startup_rejects_unrelated_failure_dialog(tmp_path, monkeypatch, 'pid': pid, 'phase': phase, 'error_code': 'MENTOR_DATABASE_LOAD_FAILED'})+'\n') assert failed_session.close_failed_startup(job, 12, timeout_seconds=0)['status'] == 'RETAINED' assert not messages + + +@pytest.mark.parametrize('exits', [True, False]) +def test_startup_close_waits_for_launcher_after_empty_pcb_exit(tmp_path, monkeypatch, exits): + from types import SimpleNamespace + job, records, messages = startup_fixture(tmp_path, monkeypatch) + ticks = [0.0] + launcher = SimpleNamespace(pid=11, poll=lambda: 0 if exits and ticks[0] >= .4 else None) + monkeypatch.setattr(failed_session, 'time', SimpleNamespace( + monotonic=lambda: ticks[0], sleep=lambda seconds: ticks.__setitem__(0, ticks[0]+seconds))) + result = failed_session.close_failed_startup(job, 12, launcher_process=launcher, timeout_seconds=1) + assert ticks[0] >= .4 + assert result['status'] == ('CLOSED' if exits else 'RETAINED') + assert result['release']['launcher_exited'] is exits + assert result['release']['pcb_exited'] is True + assert len(messages) == 1 + + +def test_startup_exit_does_not_hide_late_database_address(tmp_path, monkeypatch): + import win32gui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + def close(*args): + records.clear() + address = tmp_path/'database/cdbsvr/sAddress.adr' + address.parent.mkdir(parents=True) + address.write_text('still owned') + monkeypatch.setattr(win32gui, 'PostMessage', close) + result = failed_session.close_failed_startup(job, 12, timeout_seconds=0) + assert result['status'] == 'RETAINED' + assert result['release']['addresses'] diff --git a/tests/test_native_repair.py b/tests/test_native_repair.py index 2497de0..bfad7bd 100644 --- a/tests/test_native_repair.py +++ b/tests/test_native_repair.py @@ -22,7 +22,7 @@ def test_repair_target_matches_actual_database_not_guessed_cbb_name(tmp_path): assert target.database == target.project.parent/'database' -@pytest.mark.parametrize('change', ['foreign', 'shared', 'missing_session']) +@pytest.mark.parametrize('change', ['foreign', 'shared', 'missing_session', 'unknown_scope']) def test_repair_target_rejects_unresolved_or_shared_database(tmp_path, change): from cbb_refresh_verify.native_repair import select_repair_target request, record, target = target_fixture(tmp_path) @@ -30,6 +30,7 @@ def test_repair_target_rejects_unresolved_or_shared_database(tmp_path, change): if change == 'shared': record.update(scope='central_library', database_path=str(tmp_path/'SymbolLibs/__ReusableBlocksSymb')) if change == 'missing_session': record['session'] = '' + if change == 'unknown_scope': record['scope'] = 'unknown' with pytest.raises(ValueError): select_repair_target(request, record) @@ -134,12 +135,41 @@ def test_automatic_repair_retries_only_once_and_preserves_original_failure(tmp_p calls.append(req) return original monkeypatch.setattr(runner, '_run_verify_serialized', verify) - monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: {'status':'REPAIRED'}) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: + {'status':'REPAIRED', 'release': {'safe_to_continue': True}}) outcome = runner.run_verify(request) assert len(calls) == 2 assert outcome.status == 'FAIL' assert outcome.stderr == 'native lost session' assert outcome.recovery_attempts[0]['original_error'] == 'native lost session' + assert outcome.recovery_attempts[0]['status'] == 'REPAIR_VALIDATION_FAILED' + assert outcome.recovery_attempts[0]['native_status'] == 'REPAIRED' + + +@pytest.mark.parametrize('text,expected,terminal', [ + ('There is no backup of iCDB.\nRepair project error [Cannot repair the project - there is no backup of missing iCDB].', + 'MISSING_HEALTHY_BACKUP', True), + ('Repair project error [Database validation failed].', 'NATIVE_REPAIR_FAILED', True), + ('Project is in use. Full functionality is unavailable.', 'DATABASE_BUSY', False), + ('Repairing the project. Diagnostic: START', 'UNCONFIRMED', False), +]) +def test_current_native_output_classifies_failure_and_terminal_evidence(text, expected, terminal): + from cbb_refresh_verify.native_repair_ui import classify_repair_evidence + result = classify_repair_evidence('', '', '', text) + assert result['outcome'] == expected + assert result['failure_complete'] is terminal + + +def test_historical_no_backup_output_replay_is_conclusive_failure(): + from cbb_refresh_verify.native_repair_ui import classify_repair_evidence + # Sanitized actual native Output from the 2026-10-28 no-history fixture. + text = ('Repairing the project [/CBB.prj].\n' + 'Repair backup [repair.zip] created successfully.\n' + 'The iCDB database is corrupted.\nThere is no backup of iCDB.\n' + 'Repair project error [Cannot repair the project - there is no backup of missing iCDB].\n' + 'The project is probably in inconsistent state. Please restore the project from one of previous backups.') + result = classify_repair_evidence('', '', '', text) + assert result == {'outcome': 'MISSING_HEALTHY_BACKUP', 'failure_complete': True} @pytest.mark.parametrize('repair_status', ['BLOCKED', 'RESTORED_REVIEW_REQUIRED']) @@ -264,3 +294,358 @@ def test_backup_detects_a_file_added_during_the_copy(tmp_path, monkeypatch): monkeypatch.setattr(native_repair.shutil, 'copy2', changed) with pytest.raises(OSError, match='REPAIR_BACKUP_CHANGED'): native_repair.backup_project(target, tmp_path/'backup') + + +def repair_worker_fixture(tmp_path, monkeypatch, scenario): + """Replay native window/log transitions without launching a native client.""" + from types import SimpleNamespace + import win32con, win32gui + from cbb_refresh_verify import native_repair_ui as ui, native_repair, mentor_adapter + request, diagnosis, target = target_fixture(tmp_path) + root = tmp_path/'worker' + root.mkdir() + job = root/'request.json' + job.write_text(json.dumps({'project': str(target.project), 'database': str(target.database), + 'root': str(root), 'cancel_signal': str(root/'cancel.signal'), 'timeout': 1, + 'supervisor_pid': 123, 'desktop': 'CbbRepair_test'})) + ticks = [0.0] + state = {'requested': False, 'confirmed': False, 'closed': False} + actions = [] + monkeypatch.setattr(ui, 'time', SimpleNamespace(monotonic=lambda: ticks[0], time_ns=lambda: 0, + sleep=lambda seconds: ticks.__setitem__(0, ticks[0]+seconds))) + monkeypatch.setattr(ui, '_current_desktop_name', lambda: 'CbbRepair_test') + monkeypatch.setattr(ui, '_repair_command', lambda *_: 32924) + monkeypatch.setattr(ui, 'discover_sdd_home', lambda *_: tmp_path, raising=False) + monkeypatch.setattr('cbb_refresh_verify.mentor_paths.discover_sdd_home', lambda *_: tmp_path) + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.start_supervisor_watchdog', lambda *a, **kw: None) + monkeypatch.setattr(native_repair, 'backup_project', lambda *a: {}) + monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: []) + monkeypatch.setattr(mentor_adapter, '_process_details', lambda *_: []) + monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: SimpleNamespace( + pid=777, poll=lambda: 0 if state['closed'] and ticks[0] >= .6 else None)) + terminal = 'Repair project error [Cannot repair the project - there is no backup of missing iCDB].' + def output(_): + if scenario == 'busy_before': return 'Project is in use. Full functionality is unavailable.' + if scenario == 'stale': return terminal + if not state['confirmed']: return '' + if scenario == 'busy': return 'Repair project error [Project is in use].' + if scenario == 'active': return 'Repairing the project. Please wait.' + if scenario in {'success', 'restore'}: return 'Repair project finished.' + return terminal + monkeypatch.setattr(ui, '_native_output', output, raising=False) + def windows(_): + main = {'hwnd': 100, 'class': 'Afx:main', 'title': 'iCDB Project Backup - '+str(target.project), + 'enabled': True, 'children': []} + if state['confirmed'] and scenario in {'unknown', 'custom_prompt'}: + return [main, {'hwnd': 300, 'class': 'CustomDialog' if scenario == 'custom_prompt' else '#32770', 'title': 'Unexpected', + 'enabled': True, 'children': [{'text': 'Overwrite design?', 'class': 'Button', + 'id': 1, 'hwnd': 301, 'enabled': True}]}] + if state['requested'] and not state['confirmed']: + return [main, {'hwnd': 200, 'class': '#32770', 'title': '', 'enabled': True, + 'children': [{'hwnd': 201, 'class': 'Button', 'id': 1, 'enabled': True, + 'text': 'The project content will be validated and repaired if necessary.\n' + 'A project backup will be created before any action take place.\n' + 'You will be able to revert all changes using that backup.'}]}] + return [main] + monkeypatch.setattr(ui, '_windows', windows) + def post(hwnd, message, *args): + actions.append((hwnd, message)) + if message == win32con.WM_COMMAND: state['requested'] = True + if message == win32con.BM_CLICK: + state['confirmed'] = True + if scenario in {'success', 'restore'}: + import zipfile + log = target.project.parent/'Log Files/ProjectBackupLog.txt' + log.parent.mkdir(parents=True) + log.write_text('Successfully created [Project Repair] backup [repair.zip]') + archive = target.project.parent/'ProjectBackup/backups/repair.zip' + archive.parent.mkdir(parents=True) + with zipfile.ZipFile(archive, 'w') as zipped: zipped.writestr('database/icdb.dat', b'backup') + logs = {'dbcheck': 'Diagnostic: START\nDiagnostic: FINISH', + 'log': f'{target.database}\niCDBProjectBackupRepair\nIC: Closing project'} + if scenario == 'restore': + logs['dbrestore'] = 'iCDB database restored from backup: old.zip\n[0] DONE' + for folder, text in logs.items(): + path = target.database/'cdbsvr'/folder/'new-777/log.txt' + path.parent.mkdir(parents=True) + path.write_text(text) + if scenario == 'address': + address = target.database/'cdbsvr/sAddress.adr' + address.parent.mkdir(parents=True) + address.write_text('owner') + if message == win32con.WM_CLOSE: state['closed'] = True + monkeypatch.setattr(win32gui, 'PostMessage', post) + return ui, root, job, actions + + +@pytest.mark.parametrize('scenario,expected,closed', [ + ('failed', 'MISSING_HEALTHY_BACKUP', True), ('busy', 'DATABASE_BUSY', True), + ('unknown', 'UNKNOWN_DIALOG', False), ('custom_prompt', 'UNKNOWN_DIALOG', False), + ('active', 'TIMEOUT_UNRELEASED', False), + ('stale', 'TIMEOUT_UNRELEASED', False), ('address', 'MISSING_HEALTHY_BACKUP', False), +]) +def test_native_worker_closes_only_conclusively_failed_idle_tool(tmp_path, monkeypatch, scenario, expected, closed): + import win32con + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, scenario) + assert ui.repair_worker_main(job) == 1 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['status'] == expected + assert result['release']['safe_to_continue'] is closed + assert ((100, win32con.WM_CLOSE) in actions) is closed + assert actions[:2] == [(100, win32con.WM_COMMAND), (201, win32con.BM_CLICK)] + assert bool(result['evidence']['output_tail']) is (scenario != 'stale') + if scenario == 'unknown': assert result['unknown_dialogs'][0]['title'] == 'Unexpected' + + +@pytest.mark.parametrize('scenario,expected', [('success', 'REPAIRED'), ('restore', 'RESTORED_REVIEW_REQUIRED')]) +def test_native_worker_preserves_success_and_historical_restore_distinction(tmp_path, monkeypatch, scenario, expected): + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, scenario) + assert ui.repair_worker_main(job) == (0 if scenario == 'success' else 1) + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['status'] == expected + assert result['release']['safe_to_continue'] is True + assert result['evidence']['native_backup'] is True + assert result['evidence']['confirmed'] is (scenario == 'success') + assert (root/'native-logs/dbcheck/new-777/log.txt').exists() + + +def test_native_busy_project_on_load_never_invokes_repair(tmp_path, monkeypatch): + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'busy_before') + assert ui.repair_worker_main(job) == 1 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['status'] == 'DATABASE_BUSY' + assert actions == [] + assert not result['release']['safe_to_continue'] + + +@pytest.mark.parametrize('status,released,scope,expected', [ + ('MISSING_HEALTHY_BACKUP', True, 'project', True), + ('NATIVE_REPAIR_FAILED', True, 'project', True), + ('MISSING_HEALTHY_BACKUP', False, 'project', False), + ('MISSING_HEALTHY_BACKUP', True, 'central_library', False), + ('MISSING_HEALTHY_BACKUP', True, 'unknown', False), + ('UNKNOWN_DIALOG', True, 'project', False), ('TIMEOUT_UNRELEASED', True, 'project', False), +]) +def test_failed_repair_continues_only_released_project_local_items( + tmp_path, monkeypatch, status, released, scope, expected): + from cbb_refresh_verify import verify_runner as runner, native_repair + request, diagnosis, target = target_fixture(tmp_path) + diagnosis['scope'] = scope + evidence = tmp_path/'first/result.json' + evidence.parent.mkdir() + (evidence.parent/'diagnostics').mkdir() + (evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(diagnosis)) + original = runner.VerifyResult('FAIL', stderr='original session lost', safe_to_continue=False, + error_category='ICDB_REPAIR_REQUIRED', error_stage='PCB_STARTING', evidence_path=evidence) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **kw: original) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: { + 'status': status, 'release': {'safe_to_continue': released}, + 'evidence': {'failure_complete': True}, 'project': str(target.project), + 'database': str(target.database)}) + result = runner.run_verify(request) + assert result.safe_to_continue is expected + assert result.status == 'FAIL' + assert result.error_stage == 'PCB_STARTING' + assert result.stderr.startswith('original session lost') + assert status in result.stderr + assert result.recovery_attempts[0]['original_stage'] == 'PCB_STARTING' + + +@pytest.mark.parametrize('validation_status,flag,expected', [ + ('PASS', 'GREEN', True), ('PASS', 'UNKNOWN', False), ('FAIL', 'GREEN', False), +]) +def test_maintenance_counts_require_native_success_then_verify_green( + tmp_path, monkeypatch, validation_status, flag, expected): + from cbb_refresh_verify import verify_runner as runner, native_repair + from cbb_refresh_verify.icdb_maintenance import maintenance_counts + request, diagnosis, target = target_fixture(tmp_path) + evidence = tmp_path/'first/result.json' + evidence.parent.mkdir() + (evidence.parent/'diagnostics').mkdir() + (evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(diagnosis)) + original = runner.VerifyResult('FAIL', stderr='original lost session', safe_to_continue=False, + error_category='ICDB_REPAIR_REQUIRED', evidence_path=evidence) + evidence.write_text(json.dumps({'stderr': original.stderr})) + validated = runner.VerifyResult(validation_status, library_flag=flag, + evidence_path=tmp_path/'second.json', safe_to_continue=True) + calls = [] + def verify(req, **kwargs): + calls.append(req) + return original if len(calls) == 1 else validated + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: + {'status': 'REPAIRED', 'release': {'safe_to_continue': True}}) + result = runner.run_verify(request) + assert len(calls) == 2 and calls[1].auto_repair is False + assert result.recovery_attempts[0]['validation_passed'] is expected + assert maintenance_counts([result])['repaired'] == int(expected) + assert original.evidence_path.exists(), 'Original result evidence must remain available' + assert json.loads(original.evidence_path.read_text('utf-8'))['stderr'] == 'original lost session' + + +def test_native_success_without_release_does_not_reopen(tmp_path, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, native_repair + request, diagnosis, target = target_fixture(tmp_path) + evidence = tmp_path/'first/result.json' + evidence.parent.mkdir() + (evidence.parent/'diagnostics').mkdir() + (evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(diagnosis)) + calls = [] + def verify(*args, **kwargs): + calls.append('open') + return runner.VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', + safe_to_continue=False, evidence_path=evidence) + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: {'status': 'REPAIRED'}) + result = runner.run_verify(request) + assert calls == ['open'] + assert not result.safe_to_continue + assert result.recovery_attempts[0]['status'] == 'REPAIR_RELEASE_UNCONFIRMED' + + +@pytest.mark.parametrize('obstacle', ['none', 'launcher', 'repair_tool', 'importer', 'address', 'handle', 'shared_handle', 'probe']) +def test_repair_release_requires_process_address_and_database_handle_release(tmp_path, monkeypatch, obstacle): + import win32con, win32file + from cbb_refresh_verify import native_repair, mentor_adapter + request, diagnosis, target = target_fixture(tmp_path) + monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: + [(123, 'ExpeditionPCB.exe', 'launcher')] if obstacle == 'launcher' else []) + def processes(name): + if obstacle == 'probe': raise OSError('process observation unavailable') + if ((obstacle == 'repair_tool' and name == 'iCDBProjectBackup.exe') + or (obstacle == 'importer' and name == 'ReuseBlockImporter.exe')): + return [(456, name, 'unconfirmed owner', 0)] + return [] + monkeypatch.setattr(mentor_adapter, '_process_details', processes) + if obstacle == 'address': + address = target.database/'cdbsvr/sAddress.adr' + address.parent.mkdir(parents=True) + address.touch() + handle = None + try: + if obstacle in {'handle', 'shared_handle'}: + database_file = target.database/'icdb.dat' + if obstacle == 'shared_handle': + database_file = request.lmc_file.parent/'SymbolLibs/__ReusableBlocksSymb/icdb.dat' + database_file.parent.mkdir(parents=True) + database_file.touch() + handle = win32file.CreateFile(str(database_file), win32con.GENERIC_READ, + 0, None, win32con.OPEN_EXISTING, 0, None) + result = native_repair.repair_release_state(request, target, + {'release': {'safe_to_continue': True}}) + assert result['safe_to_continue'] is (obstacle == 'none') + finally: + if handle: handle.Close() + + +def test_attempt_persists_distinct_native_failure_and_release_result(tmp_path, monkeypatch): + from cbb_refresh_verify import native_repair, mentor_adapter + request, diagnosis, target = target_fixture(tmp_path) + evidence = tmp_path/'evidence' + evidence.mkdir() + monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: []) + monkeypatch.setattr(mentor_adapter, '_process_details', lambda *_: []) + monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **kw: { + 'status': 'MISSING_HEALTHY_BACKUP', 'project': str(target.project), + 'database': str(target.database), 'release': {'safe_to_continue': True}, + 'evidence': {'failure_complete': True, 'output_tail': 'no backup'}}) + result = native_repair.attempt_recovery(request, diagnosis, evidence) + assert result['status'] == 'MISSING_HEALTHY_BACKUP' + assert result['release']['database_exclusive'] is True + assert result['release']['safe_to_continue'] is True + assert isinstance(result['evidence'], dict) + assert result['evidence_path'] == str(evidence/'native-repair') + assert json.loads((evidence/'native-repair/attempt.json').read_text('utf-8')) == result + + +def test_retry_exception_keeps_original_error_and_native_repair_evidence(tmp_path, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, native_repair + request, diagnosis, target = target_fixture(tmp_path) + evidence = tmp_path/'first/result.json' + evidence.parent.mkdir() + (evidence.parent/'diagnostics').mkdir() + (evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(diagnosis)) + calls = [] + def verify(*a, **kw): + calls.append('open') + if len(calls) > 1: raise OSError('retry infrastructure failure') + return runner.VerifyResult('FAIL', stderr='original native error', safe_to_continue=False, + error_category='ICDB_REPAIR_REQUIRED', evidence_path=evidence) + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: { + 'status': 'REPAIRED', 'release': {'safe_to_continue': True}, + 'evidence': {'confirmed': True, 'diagnostic_tail': 'original repair evidence'}}) + result = runner.run_verify(request) + assert not result.safe_to_continue + assert 'original native error' in result.stderr + assert 'retry infrastructure failure' in result.stderr + assert result.recovery_attempts[0]['evidence']['diagnostic_tail'] == 'original repair evidence' + assert result.recovery_attempts[0]['status'] == 'REPAIR_VALIDATION_FAILED' + assert json.loads(evidence.read_text('utf-8'))['recovery_attempts'] + + +def test_sanitized_field_open_failure_replay_closes_launcher_then_repairs_once(tmp_path, monkeypatch): + from types import SimpleNamespace + from dataclasses import replace + from test_failed_startup import startup_fixture + from cbb_refresh_verify import verify_runner as runner, native_repair, failed_session, mentor_adapter + from cbb_refresh_verify.pcb_bridge import wait_for_target_document, PcbBridgeError + replay = json.loads((Path(__file__).parent/'fixtures/issue2-lost-session-sanitized.json').read_text('utf-8')) + request, diagnosis, target = target_fixture(tmp_path) + # Retain a different PRJ name: the field CBB name never authorizes guessing it. + block_root = target.project.parent.parent + block_root.rename(block_root.with_name(replay['block'])) + request = replace(request, block_name=replay['block']) + project, pcb = runner.preflight(request) + diagnosis['database_path'] = str(project.parent/'database') + target = native_repair.select_repair_target(request, diagnosis) + job, records, messages = startup_fixture(tmp_path, monkeypatch) + job.update(project=str(project), pcb=str(pcb), lmc_file=str(request.lmc_file)) + records[0] = (12, records[0][1], f'ExpeditionPCB.exe "{pcb}" -ReusableCircuit', 11) + diagnosis.update(session=replay['diagnosis']['session'], machine='FIELD-PC') + (tmp_path/'diagnostics/icdb-recovery.json').write_text(json.dumps(diagnosis)) + (tmp_path/'dialogs.jsonl').write_text(json.dumps(replay['dialog'])+'\n') + ticks = [0.0] + monkeypatch.setattr(failed_session, 'time', SimpleNamespace(monotonic=lambda: ticks[0], + sleep=lambda seconds: ticks.__setitem__(0, ticks[0]+seconds))) + launcher = SimpleNamespace(pid=11, poll=lambda: 0 if ticks[0] >= .4 else None) + monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: []) + trace = [] + evidence = tmp_path/'result.json' + original = runner.VerifyResult('FAIL', stderr=replay['native_error'], safe_to_continue=False, + error_category=replay['original_result']['error_category'], + error_stage=replay['original_result']['error_stage'], evidence_path=evidence) + assert runner.classify_failure_category(original.stderr) == original.error_category + assert runner.phase_for_event(replay['observed_events'][2]).value == original.error_stage + opens = [] + def verify(req, **kwargs): + opens.append(req) + trace.append('open') + if len(opens) == 1: + errors = iter(('', replay['native_error'])) + with pytest.raises(PcbBridgeError, match='Repair project'): + wait_for_target_document(job, lambda: SimpleNamespace(ActiveDocument=None), + process_alive=lambda: True, error_reader=lambda: next(errors), + emit=lambda **e: trace.append(e['stage']), sleep=lambda _: None) + closed = failed_session.close_failed_startup(job, 12, launcher_process=launcher, + emit=lambda **e: trace.append(e['stage']), timeout_seconds=1) + assert closed['status'] == 'CLOSED' + trace.append('launcher_exited') + return original # Repeated field error after one repair must not loop. + def repair(actual, root, **kwargs): + assert actual == target + assert trace[-1] == 'launcher_exited' + assert not records and launcher.poll() == 0 + trace.append('native_repair') + return {'status': 'REPAIRED', 'project': str(actual.project), 'database': str(actual.database), + 'release': {'safe_to_continue': True}, 'evidence': {'confirmed': True}} + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + monkeypatch.setattr(native_repair, 'run_native_repair', repair) + result = runner.run_verify(request) + assert trace == ['open', 'pcb_document_wait', 'startup_close_requested', 'startup_close_result', + 'launcher_exited', 'native_repair', 'open'] + assert len(opens) == 2 and opens[1].auto_repair is False + assert result.error_stage == 'PCB_STARTING' + assert result.recovery_attempts[0]['status'] == 'REPAIR_VALIDATION_FAILED' + assert result.recovery_attempts[0]['original_error'] == replay['native_error'] + assert json.loads((tmp_path/'pre-repair-result.json').read_text('utf-8'))['stderr'] == replay['native_error'] diff --git a/tests/test_pcb_bridge.py b/tests/test_pcb_bridge.py index 00e1a16..523f840 100644 --- a/tests/test_pcb_bridge.py +++ b/tests/test_pcb_bridge.py @@ -89,3 +89,15 @@ def test_success_requires_save_and_pcb_exit(): assert execution_outcome(save_complete=True, pcb_exited=True)['status'] == 'PASS' assert execution_outcome(save_complete=False, pcb_exited=True)['status'] == 'FAIL' assert execution_outcome(save_complete=True, pcb_exited=False)['status'] == 'FAIL' + + +def test_connected_com_without_document_remains_startup_and_checks_native_failure(tmp_path): + errors = iter(('', 'ICDB_REPAIR_REQUIRED: original lost session')) + events = [] + with pytest.raises(PcbBridgeError) as raised: + wait_for_target_document(job(tmp_path), lambda: Application(None), process_alive=lambda: True, + error_reader=lambda: next(errors), emit=lambda **e: events.append(e), sleep=lambda _: None) + assert raised.value.error_code == 'PCB_STARTUP_FAILED' + assert 'original lost session' in str(raised.value) + assert [e['stage'] for e in events] == ['pcb_document_wait'] + assert events[0]['error_code'] == 'PCB_DOCUMENT_NOT_READY' -- 2.39.5 From c823a4b92d9028ff0751efd56c6bebc410c3e25b 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: Mon, 2 Nov 2026 18:27:19 +0800 Subject: [PATCH 04/59] fix: fail closed on busy or unconfirmed native repair evidence --- .../2026-11-02-issue-2-startup-recovery.md | 20 +- src/cbb_refresh_verify/native_repair.py | 56 ++++- src/cbb_refresh_verify/native_repair_ui.py | 6 +- src/cbb_refresh_verify/verify_runner.py | 17 +- tests/test_native_repair.py | 192 ++++++++++++++++-- 5 files changed, 267 insertions(+), 24 deletions(-) diff --git a/docs/releases/2026-11-02-issue-2-startup-recovery.md b/docs/releases/2026-11-02-issue-2-startup-recovery.md index cbe4620..67b5ffd 100644 --- a/docs/releases/2026-11-02-issue-2-startup-recovery.md +++ b/docs/releases/2026-11-02-issue-2-startup-recovery.md @@ -100,7 +100,11 @@ cooperative close only after a terminal native error, an enabled exact-project main window, no other windows/prompts and no target database address. The worker then waits for normal process exit. Unknown/custom prompts and incomplete native operations receive no close action. A failure classification can coexist with -unreleased state; that state always stops the queue. A busy-project message in +unreleased state; that state always stops the queue. Terminal `DATABASE_BUSY` +errors also retain the native tool without close, reopen or queue continuation. +Unreadable, absent, hung or truncated initial Output waits within the deadline +and retains the tool without requesting Repair; a readable empty string is a +valid invocation baseline. A busy-project message in this fresh native process's initial loading Output prevents Repair invocation entirely; it is not hidden by the later invocation baseline. @@ -112,10 +116,17 @@ Only a conclusively failed, released, independently revalidated project-local target may allow the next CBB. Shared or unknown scope, busy owners, timeouts, unknown prompts and historical restores stop for review. +Before accepting native success, the parent validates native process identity +and true exit, exact project/database, empty address state, confirmed backup/ +repair evidence and absence of restore/failure/unknown-prompt contradictions. +Missing or wrong-typed fields fail closed. Independent process, address and +database-handle probes remain additional requirements. + Native `REPAIRED` is retained as `native_status`; summaries count a repair only after the same request reopens, completes Verify and returns authoritative `PASS/GREEN` with confirmed release. A failed reopen becomes -`REPAIR_VALIDATION_FAILED`; an unconfirmed native release prevents reopening. +`REPAIR_VALIDATION_FAILED`; incomplete or contradictory native success/release +becomes `UNCONFIRMED` and prevents reopening. Historical restoration remains `RESTORED_REVIEW_REQUIRED`. The original error, stage and evidence references, a `pre-repair-result.json` snapshot, native evidence and validation evidence remain attached even if the retry raises. @@ -139,3 +150,8 @@ Task 1 checks: full suite once, 532 passed with the existing pywinauto COM warni after the final initial-busy gate regression, the related final suite passed all 234 tests. Compileall and `git diff --check` passed. The coordinator will run candidate/frozen validation against the final implementation. + +Task 1 review fix round: 259 related tests passed (17.86s); compileall and +`git diff --check` passed. These checks include busy retention, real readback +failure branches, 30 malformed-success variants at both release and reopen/ +counting boundaries, and a complete actual worker-result replay control. diff --git a/src/cbb_refresh_verify/native_repair.py b/src/cbb_refresh_verify/native_repair.py index ee5b90e..9390c8d 100644 --- a/src/cbb_refresh_verify/native_repair.py +++ b/src/cbb_refresh_verify/native_repair.py @@ -140,7 +140,11 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None): callback = (lambda event: progress({'block': request.block_name, **event})) if progress else None record = run_native_repair(target, root, mentor_bin=request.mentor_bin, timeout_seconds=max(60, request.timeout_seconds), progress=callback) + native_status = record.get('status') record['release'] = repair_release_state(request, target, record) + if native_status == 'REPAIRED' and record['release']['safe_to_continue'] is not True: + record.update(status='UNCONFIRMED', native_status=native_status, + detail='NATIVE_REPAIR_EVIDENCE_UNCONFIRMED: 原生成功或释放证据不完整') record['evidence_path'] = str(root) except Exception as error: record['detail'] = str(error) @@ -148,14 +152,64 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None): return record +def _native_identity_and_exit_confirmed(target, record): + if not isinstance(record, dict): + return False + release = record.get('release') + if (not isinstance(release, dict) or release.get('safe_to_continue') is not True + or release.get('native_exited') is not True + or not isinstance(release.get('addresses'), list) or release['addresses'] + or type(record.get('native_pid')) is not int or record['native_pid'] <= 0 + or not isinstance(record.get('unknown_dialogs', []), list) + or record.get('unknown_dialogs')): + return False + try: + for name, expected in (('project', target.project), ('database', target.database)): + path = record.get(name) + if (not isinstance(path, str) or not Path(path).is_absolute() + or Path(path).resolve(strict=True) != expected.resolve(strict=True)): + return False + except (OSError, ValueError): + return False + return True + + +def native_success_confirmed(target, record): + """Validate the worker's complete, consistent success before any reopening.""" + if not _native_identity_and_exit_confirmed(target, record) or record.get('status') != 'REPAIRED': + return False + evidence = record.get('evidence') + if (not isinstance(evidence, dict) or evidence.get('confirmed') is not True + or evidence.get('native_backup') is not True + or evidence.get('outcome') != 'UNCONFIRMED' + or evidence.get('failure_complete') is not False + or evidence.get('restore_complete') is not False + or evidence.get('restored_from_backup') != ''): + return False + fields = ('diagnostic_tail', 'database_tail', 'restore_tail', 'output_tail') + if any(not isinstance(evidence.get(field), str) for field in fields): + return False + if not evidence['diagnostic_tail'] or not evidence['database_tail']: + return False + from .native_repair_ui import classify_repair_evidence + return (classify_repair_evidence(*(evidence[field] for field in fields))['outcome'] == 'UNCONFIRMED' + and 'iCDB database restored from backup:' not in evidence['restore_tail']) + + def repair_release_state(request, target, native_result): """Require normal exit, owner/address absence and exclusive database access.""" from .mentor_adapter import pcb_processes, _process_details from .icdb_ownership import database_directories, unreleased_addresses from .verify_runner import preflight - result = {**native_result.get('release', {}), 'safe_to_continue': False, + release = native_result.get('release') if isinstance(native_result, dict) else None + result = {**(release if isinstance(release, dict) else {}), 'safe_to_continue': False, 'database_exclusive': False} try: + if (not _native_identity_and_exit_confirmed(target, native_result) + or (native_result.get('status') == 'REPAIRED' + and not native_success_confirmed(target, native_result))): + result['error'] = 'NATIVE_REPAIR_EVIDENCE_UNCONFIRMED' + return result project, pcb = preflight(request) owners = [int(r[0]) for r in pcb_processes()] for name in ('LibraryManager.exe', 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe'): diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index 8f14966..1b4339d 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -237,7 +237,8 @@ def repair_worker_main(job_path): if busy_before_start: output = current_output result.update(status='DATABASE_BUSY', detail='原生工具加载工程时报告占用;未请求 Repair') - if not invoked and not busy_before_start and main and not dialogs and not unknown and main['enabled']: + if (not invoked and isinstance(current_output, str) and not busy_before_start + and main and not dialogs and not unknown and main['enabled']): if Path(job['cancel_signal']).exists(): raise ValueError('REPAIR_CANCELLED_BEFORE_START') output_baseline = current_output @@ -275,7 +276,8 @@ def repair_worker_main(job_path): if result['status'] == 'UNKNOWN_DIALOG' or busy_before_start: break if confirmed and not closing and main and not dialogs and main['enabled']: - if (not (database/'cdbsvr/sAddress.adr').exists() and ( + if (evidence['outcome'] != 'DATABASE_BUSY' + and not (database/'cdbsvr/sAddress.adr').exists() and ( evidence['failure_complete'] or evidence['confirmed'] or (evidence.get('restore_complete') and evidence['native_backup'] and evidence['outcome'] == 'UNCONFIRMED'))): diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 74a838e..6b370bc 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -533,7 +533,19 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, 'original_evidence': str(original.evidence_path), 'original_result_snapshot': str(snapshot), 'original_stage': original.error_stage, - 'native_status': recovery.get('status'), 'validation_passed': False} + 'native_status': recovery.get('native_status', recovery.get('status')), + 'validation_passed': False} + if recovery.get('status') == 'REPAIRED': + from .native_repair import native_success_confirmed, select_repair_target + try: + confirmed = native_success_confirmed(select_repair_target(request, diagnosis), recovery) + except (ValueError, OSError): + confirmed = False + if not confirmed: + release = recovery.get('release') + recovery.update(status='UNCONFIRMED', + release={**(release if isinstance(release, dict) else {}), 'safe_to_continue': False}, + detail='NATIVE_REPAIR_EVIDENCE_UNCONFIRMED: 原生成功记录不完整或矛盾,未重试校验') if (recovery.get('status') == 'REPAIRED' and recovery.get('release', {}).get('safe_to_continue') is True): result = replace(original, recovery_attempts=(recovery,)) @@ -552,9 +564,6 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, stderr=(result.stderr+'\nREPAIR_VALIDATION_UNCONFIRMED: 未取得校验 GREEN').strip()) else: recovery['status'] = 'REPAIRED' - elif recovery.get('status') == 'REPAIRED': - recovery['status'] = 'REPAIR_RELEASE_UNCONFIRMED' - result = replace(result, safe_to_continue=False) else: # A failed local database must not block unrelated CBBs once # every owner is released. Re-check target ownership here so diff --git a/tests/test_native_repair.py b/tests/test_native_repair.py index bfad7bd..5485477 100644 --- a/tests/test_native_repair.py +++ b/tests/test_native_repair.py @@ -16,6 +16,19 @@ def target_fixture(tmp_path): return request(tmp_path), diagnosis, select_repair_target(request(tmp_path), diagnosis) +def complete_native_success(target): + """A complete successful worker result, with no contradictory native state.""" + return {'status': 'REPAIRED', 'project': str(target.project), 'database': str(target.database), + 'native_pid': 777, + 'release': {'native_exited': True, 'safe_to_continue': True, 'addresses': []}, + 'evidence': {'confirmed': True, 'native_backup': True, 'outcome': 'UNCONFIRMED', + 'failure_complete': False, 'restore_complete': False, 'restored_from_backup': '', + 'diagnostic_tail': 'Diagnostic: START\nDiagnostic: FINISH', + 'database_tail': f'{target.database}\niCDBProjectBackupRepair\nIC: Closing project', + 'restore_tail': '', 'output_tail': '', 'backup_log_tail': '', + 'log_paths': []}} + + def test_repair_target_matches_actual_database_not_guessed_cbb_name(tmp_path): request, record, target = target_fixture(tmp_path) assert target.project.name == 'CBB1.prj' @@ -135,8 +148,7 @@ def test_automatic_repair_retries_only_once_and_preserves_original_failure(tmp_p calls.append(req) return original monkeypatch.setattr(runner, '_run_verify_serialized', verify) - monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: - {'status':'REPAIRED', 'release': {'safe_to_continue': True}}) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: complete_native_success(target)) outcome = runner.run_verify(request) assert len(calls) == 2 assert outcome.status == 'FAIL' @@ -379,7 +391,7 @@ def repair_worker_fixture(tmp_path, monkeypatch, scenario): @pytest.mark.parametrize('scenario,expected,closed', [ - ('failed', 'MISSING_HEALTHY_BACKUP', True), ('busy', 'DATABASE_BUSY', True), + ('failed', 'MISSING_HEALTHY_BACKUP', True), ('busy', 'DATABASE_BUSY', False), ('unknown', 'UNKNOWN_DIALOG', False), ('custom_prompt', 'UNKNOWN_DIALOG', False), ('active', 'TIMEOUT_UNRELEASED', False), ('stale', 'TIMEOUT_UNRELEASED', False), ('address', 'MISSING_HEALTHY_BACKUP', False), @@ -399,6 +411,7 @@ def test_native_worker_closes_only_conclusively_failed_idle_tool(tmp_path, monke @pytest.mark.parametrize('scenario,expected', [('success', 'REPAIRED'), ('restore', 'RESTORED_REVIEW_REQUIRED')]) def test_native_worker_preserves_success_and_historical_restore_distinction(tmp_path, monkeypatch, scenario, expected): + from cbb_refresh_verify.native_repair import RepairTarget, native_success_confirmed ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, scenario) assert ui.repair_worker_main(job) == (0 if scenario == 'success' else 1) result = json.loads((root/'repair-result.json').read_text('utf-8')) @@ -407,6 +420,9 @@ def test_native_worker_preserves_success_and_historical_restore_distinction(tmp_ assert result['evidence']['native_backup'] is True assert result['evidence']['confirmed'] is (scenario == 'success') assert (root/'native-logs/dbcheck/new-777/log.txt').exists() + request = json.loads(job.read_text('utf-8')) + target = RepairTarget(Path(request['project']), Path(request['database'])) + assert native_success_confirmed(target, result) is (scenario == 'success') def test_native_busy_project_on_load_never_invokes_repair(tmp_path, monkeypatch): @@ -418,6 +434,80 @@ def test_native_busy_project_on_load_never_invokes_repair(tmp_path, monkeypatch) assert not result['release']['safe_to_continue'] +@pytest.mark.parametrize('readback', ['missing', 'timeout', 'truncated']) +def test_unavailable_initial_output_never_requests_or_confirms_repair(tmp_path, monkeypatch, readback): + from types import SimpleNamespace + from cbb_refresh_verify import native_repair_ui as module + actual_read = module._native_output + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + windows = ui._windows + def observe(pid): + result = windows(pid) + if readback != 'missing': + result[0]['children'].append({'hwnd': 400, 'class': 'RichEdit20A'}) + return result + monkeypatch.setattr(ui, '_windows', observe) + class Readback: + def __call__(self, hwnd, message, length, buffer, flags, timeout, copied): + if readback == 'timeout': return 0 + copied._obj.value = length-1 + return 1 + monkeypatch.setattr(ui.ctypes, 'WinDLL', lambda *a, **kw: SimpleNamespace(SendMessageTimeoutW=Readback())) + monkeypatch.setattr(ui, '_native_output', actual_read) + assert ui.repair_worker_main(job) == 1 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert actions == [] + assert result['status'] == 'TIMEOUT_UNRELEASED' + assert result['release']['safe_to_continue'] is False + assert json.loads((root/'native-output.json').read_text('utf-8'))['baseline'] is None + + +def test_initial_output_waits_until_readable_and_accepts_empty_baseline(tmp_path, monkeypatch): + import win32con + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + reads = [] + def output(_): + reads.append(len(reads)) + if len(reads) <= 2: + assert not actions + return None + return '' + monkeypatch.setattr(ui, '_native_output', output) + assert ui.repair_worker_main(job) == 1 + assert actions == [(100, win32con.WM_COMMAND), (201, win32con.BM_CLICK)] + assert json.loads((root/'native-output.json').read_text('utf-8'))['baseline'] == '' + + +def test_invoked_busy_native_result_never_reopens_or_continues_queue(tmp_path, monkeypatch): + import win32con + from test_verify_runner import request + from cbb_refresh_verify import verify_runner as runner, native_repair + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'busy') + assert ui.repair_worker_main(job) == 1 + busy = json.loads((root/'repair-result.json').read_text('utf-8')) + assert (100, win32con.WM_CLOSE) not in actions + assert busy['status'] == 'DATABASE_BUSY' and busy['release']['native_exited'] is False + req = request(tmp_path) + target = json.loads(job.read_text('utf-8')) + evidence = tmp_path/'first/result.json' + evidence.parent.mkdir() + (evidence.parent/'diagnostics').mkdir() + (evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps({ + 'error_code': 'ICDB_REPAIR_REQUIRED', 'scope': 'project', + 'session': 'original-session', 'database_path': target['database']})) + opens = [] + def verify(*a, **kw): + opens.append('open') + return runner.VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', + safe_to_continue=False, evidence_path=evidence) + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: busy) + results = runner.run_batch([req, req], verifier=runner.run_verify) + assert opens == ['open'] + assert results[0].recovery_attempts[0]['status'] == 'DATABASE_BUSY' + assert results[0].safe_to_continue is False and results[1].status == 'SKIPPED' + + @pytest.mark.parametrize('status,released,scope,expected', [ ('MISSING_HEALTHY_BACKUP', True, 'project', True), ('NATIVE_REPAIR_FAILED', True, 'project', True), @@ -425,6 +515,7 @@ def test_native_busy_project_on_load_never_invokes_repair(tmp_path, monkeypatch) ('MISSING_HEALTHY_BACKUP', True, 'central_library', False), ('MISSING_HEALTHY_BACKUP', True, 'unknown', False), ('UNKNOWN_DIALOG', True, 'project', False), ('TIMEOUT_UNRELEASED', True, 'project', False), + ('DATABASE_BUSY', True, 'project', False), ]) def test_failed_repair_continues_only_released_project_local_items( tmp_path, monkeypatch, status, released, scope, expected): @@ -473,8 +564,7 @@ def test_maintenance_counts_require_native_success_then_verify_green( calls.append(req) return original if len(calls) == 1 else validated monkeypatch.setattr(runner, '_run_verify_serialized', verify) - monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: - {'status': 'REPAIRED', 'release': {'safe_to_continue': True}}) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: complete_native_success(target)) result = runner.run_verify(request) assert len(calls) == 2 and calls[1].auto_repair is False assert result.recovery_attempts[0]['validation_passed'] is expected @@ -500,7 +590,7 @@ def test_native_success_without_release_does_not_reopen(tmp_path, monkeypatch): result = runner.run_verify(request) assert calls == ['open'] assert not result.safe_to_continue - assert result.recovery_attempts[0]['status'] == 'REPAIR_RELEASE_UNCONFIRMED' + assert result.recovery_attempts[0]['status'] == 'UNCONFIRMED' @pytest.mark.parametrize('obstacle', ['none', 'launcher', 'repair_tool', 'importer', 'address', 'handle', 'shared_handle', 'probe']) @@ -531,8 +621,7 @@ def test_repair_release_requires_process_address_and_database_handle_release(tmp database_file.touch() handle = win32file.CreateFile(str(database_file), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None) - result = native_repair.repair_release_state(request, target, - {'release': {'safe_to_continue': True}}) + result = native_repair.repair_release_state(request, target, complete_native_success(target)) assert result['safe_to_continue'] is (obstacle == 'none') finally: if handle: handle.Close() @@ -547,7 +636,8 @@ def test_attempt_persists_distinct_native_failure_and_release_result(tmp_path, m monkeypatch.setattr(mentor_adapter, '_process_details', lambda *_: []) monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **kw: { 'status': 'MISSING_HEALTHY_BACKUP', 'project': str(target.project), - 'database': str(target.database), 'release': {'safe_to_continue': True}, + 'database': str(target.database), 'native_pid': 777, + 'release': {'safe_to_continue': True, 'native_exited': True, 'addresses': []}, 'evidence': {'failure_complete': True, 'output_tail': 'no backup'}}) result = native_repair.attempt_recovery(request, diagnosis, evidence) assert result['status'] == 'MISSING_HEALTHY_BACKUP' @@ -572,14 +662,14 @@ def test_retry_exception_keeps_original_error_and_native_repair_evidence(tmp_pat return runner.VerifyResult('FAIL', stderr='original native error', safe_to_continue=False, error_category='ICDB_REPAIR_REQUIRED', evidence_path=evidence) monkeypatch.setattr(runner, '_run_verify_serialized', verify) - monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: { - 'status': 'REPAIRED', 'release': {'safe_to_continue': True}, - 'evidence': {'confirmed': True, 'diagnostic_tail': 'original repair evidence'}}) + native_result = complete_native_success(target) + native_result['evidence']['diagnostic_tail'] += '\noriginal repair evidence' + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: native_result) result = runner.run_verify(request) assert not result.safe_to_continue assert 'original native error' in result.stderr assert 'retry infrastructure failure' in result.stderr - assert result.recovery_attempts[0]['evidence']['diagnostic_tail'] == 'original repair evidence' + assert 'original repair evidence' in result.recovery_attempts[0]['evidence']['diagnostic_tail'] assert result.recovery_attempts[0]['status'] == 'REPAIR_VALIDATION_FAILED' assert json.loads(evidence.read_text('utf-8'))['recovery_attempts'] @@ -637,8 +727,7 @@ def test_sanitized_field_open_failure_replay_closes_launcher_then_repairs_once(t assert trace[-1] == 'launcher_exited' assert not records and launcher.poll() == 0 trace.append('native_repair') - return {'status': 'REPAIRED', 'project': str(actual.project), 'database': str(actual.database), - 'release': {'safe_to_continue': True}, 'evidence': {'confirmed': True}} + return complete_native_success(actual) monkeypatch.setattr(runner, '_run_verify_serialized', verify) monkeypatch.setattr(native_repair, 'run_native_repair', repair) result = runner.run_verify(request) @@ -649,3 +738,76 @@ def test_sanitized_field_open_failure_replay_closes_launcher_then_repairs_once(t assert result.recovery_attempts[0]['status'] == 'REPAIR_VALIDATION_FAILED' assert result.recovery_attempts[0]['original_error'] == replay['native_error'] assert json.loads((tmp_path/'pre-repair-result.json').read_text('utf-8'))['stderr'] == replay['native_error'] + + +MALFORMED_SUCCESS = [ + ('minimal', None), + ('native_pid', None), ('native_pid', True), + ('project', None), ('project', '/CBB.prj'), ('project', []), + ('database', None), ('database', '/database'), + ('release', []), ('release', 'released'), + ('release.native_exited', None), ('release.native_exited', False), + ('release.native_exited', 1), ('release.native_exited', 'true'), + ('release.safe_to_continue', 1), ('release.addresses', ['owner']), ('release.addresses', ''), + ('evidence', None), ('evidence', []), ('evidence', 'confirmed'), + ('evidence.confirmed', None), ('evidence.confirmed', False), + ('evidence.confirmed', 1), ('evidence.confirmed', 'true'), + ('evidence.native_backup', False), ('evidence.failure_complete', True), + ('evidence.outcome', 'DATABASE_BUSY'), ('evidence.restore_complete', True), + ('evidence.restored_from_backup', 'old.zip'), ('unknown_dialogs', [{'title': 'Unknown'}]), +] + + +def malformed_native_success(target, key, value): + result = complete_native_success(target) + if key == 'minimal': return {'status': 'REPAIRED', 'release': {'safe_to_continue': True}} + if key in {'project', 'database'} and isinstance(value, str) and value.startswith(''): + other = target.project.parent.parent.parent/'other-target' + other.mkdir(exist_ok=True) + if key == 'project': + other = other/'other.prj' + other.touch() + value = str(other.resolve()) + if '.' in key: + parent, field = key.split('.') + result[parent][field] = value + else: + result[key] = value + return result + + +@pytest.mark.parametrize('key,value', MALFORMED_SUCCESS) +def test_malformed_native_success_never_authorizes_release(tmp_path, monkeypatch, key, value): + from cbb_refresh_verify import native_repair, mentor_adapter + request, diagnosis, target = target_fixture(tmp_path) + monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: []) + monkeypatch.setattr(mentor_adapter, '_process_details', lambda *_: []) + result = native_repair.repair_release_state(request, target, malformed_native_success(target, key, value)) + assert result['safe_to_continue'] is False + + +@pytest.mark.parametrize('key,value', MALFORMED_SUCCESS) +def test_malformed_native_success_never_reopens_or_counts_repaired(tmp_path, monkeypatch, key, value): + from cbb_refresh_verify import verify_runner as runner, native_repair + from cbb_refresh_verify.icdb_maintenance import maintenance_counts + request, diagnosis, target = target_fixture(tmp_path) + evidence = tmp_path/'first/result.json' + evidence.parent.mkdir() + (evidence.parent/'diagnostics').mkdir() + (evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(diagnosis)) + calls = [] + def verify(*a, **kw): + calls.append('open') + if len(calls) == 1: + return runner.VerifyResult('FAIL', stderr='original error', error_category='ICDB_REPAIR_REQUIRED', + safe_to_continue=False, evidence_path=evidence) + return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=evidence) + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: + malformed_native_success(target, key, value)) + result = runner.run_verify(request) + assert calls == ['open'] + assert result.status == 'FAIL' and result.safe_to_continue is False + assert result.recovery_attempts[0]['status'] == 'UNCONFIRMED' + assert result.recovery_attempts[0]['release']['safe_to_continue'] is False + assert maintenance_counts([result])['repaired'] == 0 -- 2.39.5 From 747ce1add2fd2ff7fad2b4f07e7dd28c378e0094 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: Mon, 2 Nov 2026 18:47:25 +0800 Subject: [PATCH 05/59] feat: scope CBB search selection and maintenance to visible non-READY rows --- src/cbb_refresh_verify/icdb_maintenance.py | 7 +- src/cbb_refresh_verify/ui/main_window.py | 322 ++++++++++++++++----- tests/test_backup_recovery_dialog.py | 15 +- tests/test_ui.py | 44 +-- tests/test_ui_filters.py | 290 +++++++++++++++++++ 5 files changed, 572 insertions(+), 106 deletions(-) create mode 100644 tests/test_ui_filters.py diff --git a/src/cbb_refresh_verify/icdb_maintenance.py b/src/cbb_refresh_verify/icdb_maintenance.py index cd50ba3..4edac2f 100644 --- a/src/cbb_refresh_verify/icdb_maintenance.py +++ b/src/cbb_refresh_verify/icdb_maintenance.py @@ -1,4 +1,4 @@ -"""Reports for the explicit full-library maintenance action.""" +"""Reports for the explicit visible non-READY maintenance action.""" from dataclasses import asdict import json @@ -20,8 +20,9 @@ def maintenance_counts(results): def write_maintenance_report(requests, results, batch_report, skipped_names): path = batch_report.with_name(batch_report.name.replace('batch-', 'icdb-', 1)).with_suffix('.json') - record = {'operation': 'full_library_icdb_check_and_repair', - 'library': str(requests[0].lmc_file), 'includes_ready': True, + record = {'operation': 'visible_non_ready_icdb_check_and_repair', + 'library': str(requests[0].lmc_file), 'includes_ready': False, + 'target_names': [request.block_name for request in requests], 'shared_database_repair_supported': False, 'preflight_skipped': sorted(skipped_names), 'counts': maintenance_counts(results), 'results': [{'block': req.block_name, **asdict(result)} diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 3aaad5d..f8a51d5 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -3,12 +3,13 @@ from threading import Event import os import time from collections import Counter +from dataclasses import replace from PySide6.QtCore import QObject, QThreadPool, Signal, Qt from PySide6.QtGui import QColor, QIcon from PySide6.QtWidgets import (QWidget, QLineEdit, QPushButton, QFormLayout, QTextEdit, QFileDialog, QHBoxLayout, QLabel, QSpinBox, QMessageBox, QToolButton, - QTableWidget, QTableWidgetItem, QHeaderView, QCheckBox, QGroupBox, QVBoxLayout, QProgressBar) + QTableWidget, QTableWidgetItem, QHeaderView, QCheckBox, QGroupBox, QVBoxLayout, QProgressBar, QComboBox) from PySide6.QtWidgets import QStyle try: from dx_productivity.trial_task_center.ui.workers import ServiceWorker @@ -45,7 +46,7 @@ def application_icon_path(): class MainWindow(QWidget): def __init__(self, reader=None, verifier=None, logging_context=None, data_root=None, - authoritative_status=True, path_lease_factory=None): + authoritative_status=True, path_lease_factory=None, status_refresher=None): super().__init__() self.setWindowTitle('CBB 刷新校验') icon_path = application_icon_path() @@ -78,6 +79,9 @@ class MainWindow(QWidget): self.pool = QThreadPool(self); self.pool.setMaxThreadCount(1) self.busy = False; self.loaded_path = None; self.stop = Event(); self.ready_names = set() self._row_by_name = {} + self._records_by_name = {} + self.status_refresher = status_refresher or (lambda names, path: authoritative_status_map( + names, path, self.data_root / 'status-refresh.jsonl')) self.skipped_names = set(); self.skip_reasons = {} self._global_icdb_run = False self._icdb_completed = 0 @@ -109,13 +113,26 @@ class MainWindow(QWidget): self.cbb_table.setSelectionBehavior(QTableWidget.SelectRows) self.cbb_table.setMinimumHeight(150) self.cbb_table.itemChanged.connect(lambda _item: self._update()) + self.search_edit = QLineEdit() + self.search_edit.setPlaceholderText('按 CBB 名称或分区搜索') + self.status_filter = QComboBox() + for label, key in (('全部', 'all'), ('READY', 'ready'), ('非 READY(含未确认)', 'non_ready'), + ('PENDING', 'pending'), ('未知 / 读取异常', 'errors'), ('缺少工程', 'missing')): + self.status_filter.addItem(label, key) + self.counts_label = QLabel() + self.select_visible_button = QPushButton('选择可见项') + self.clear_selection_button = QPushButton('清空选择') + self.select_visible_button.clicked.connect(lambda: self._check_visible(Qt.Checked)) + self.clear_selection_button.clicked.connect(lambda: self._check_visible(Qt.Unchecked)) + self.search_edit.textChanged.connect(self._apply_filters) + self.status_filter.currentIndexChanged.connect(self._apply_filters) self.timeout_spin = QSpinBox(); self.timeout_spin.setRange(30, 1800); self.timeout_spin.setValue(DEFAULT_TIMEOUT_SECONDS); self.timeout_spin.setSuffix(' 秒 / 无进展') self.load_button = QPushButton('读取 CBB'); self.load_button.clicked.connect(self.load_library) self.browse_button = QPushButton('选择'); self.browse_button.clicked.connect(self.choose_library) self.selected_batch_button = QPushButton('批量校验已选'); self.selected_batch_button.clicked.connect(self.verify_selected) - self.batch_button = QPushButton('批量校验全部 CBB'); self.batch_button.clicked.connect(self.verify_all) - self.icdb_repair_button = QPushButton('全库 ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) - self.icdb_repair_button.setToolTip('重新检查所有可用 CBB,包含 Ready 项;确认工程数据库失联后备份并尝试原生修复。') + self.batch_button = QPushButton('校验可见非 READY'); self.batch_button.clicked.connect(self.verify_all) + self.icdb_repair_button = QPushButton('非 READY ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) + self.icdb_repair_button.setToolTip('检查可见非 READY 项;有勾选时仅处理其中可用项。未确认状态不等于损坏。') self.backup_restore_button = QPushButton('从备份恢复 CBB…') self.backup_restore_button.clicked.connect(self.restore_backup) self.stop_button = QPushButton('当前项完成后停止'); self.stop_button.clicked.connect(self.request_stop) @@ -144,6 +161,11 @@ class MainWindow(QWidget): layout = QFormLayout(self) row = QHBoxLayout(); row.addWidget(self.lmc_edit); row.addWidget(self.browse_button); row.addWidget(self.load_button) layout.addRow('中心库 .lmc', row); layout.addRow(self.advanced_toggle); layout.addRow(self.advanced_panel) + filters = QHBoxLayout() + for widget in (self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button): + filters.addWidget(widget) + layout.addRow('搜索 / 筛选', filters) + layout.addRow(self.counts_label) layout.addRow('CBB 清单(勾选后可批量)', self.cbb_table); layout.addRow('超时', self.timeout_spin) layout.addRow(QLabel('逐个刷新:命令行启动 ReusableCircuit PCB → Project Integration → 保存并关闭,全程不切换桌面、不移动鼠标。' '填写 LibSpec 与 DMS 登录配置后,会先执行一次 ReuseBlockImporter 内容检查。')) @@ -154,7 +176,7 @@ class MainWindow(QWidget): maintenance_layout = QVBoxLayout(maintenance) maintenance_row = QHBoxLayout() maintenance_row.addWidget(self.icdb_repair_button) - description = QLabel('包含 Ready 项,逐个重新校验。确认工程数据库失联后,先备份再修复。\n共享符号库需另行处理;无法确认会话安全时停止。') + description = QLabel('处理可见非 READY 项;有勾选时仅处理勾选中的可用项。未确认不等于损坏。\n确认工程数据库失联后先备份再修复;无法确认会话安全时停止。') description.setWordWrap(True) maintenance_row.addWidget(description, 1) maintenance_layout.addLayout(maintenance_row) @@ -179,6 +201,7 @@ class MainWindow(QWidget): if self.loaded_path is not None and self.lmc_edit.text() != self.loaded_path: self._release_library_lease() self.loaded_path = None; self.mentor_lmc_path = None; self.cbb_table.setRowCount(0); self._row_by_name = {} + self._records_by_name = {} self.ready_names = set(); self.skipped_names = set(); self.skip_reasons = {}; self._update() self._global_icdb_run = False self.icdb_progress.hide(); self.icdb_phase.hide() @@ -197,19 +220,75 @@ class MainWindow(QWidget): # The refresh flow needs only the library and a CBB; LibSpec/DMS config # are optional and only enable the extra contents check. ready = not self.busy and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 - self.batch_button.setEnabled(ready and any(self.cbb_table.item(row, 0).text() not in self.ready_names - and self.cbb_table.item(row, 0).text() not in self.skipped_names - for row in range(self.cbb_table.rowCount()))) - self.selected_batch_button.setEnabled(ready and bool(self._selected_names())) - self.icdb_repair_button.setEnabled(ready and any( - self.cbb_table.item(row, 0).text() not in self.skipped_names - for row in range(self.cbb_table.rowCount()))) - self.backup_restore_button.setEnabled(ready) + self.batch_button.setEnabled(ready and bool(self._eligible_names())) + self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True))) + self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names())) + self.backup_restore_button.setEnabled(ready and bool(self._maintenance_names())) for widget in (self.lmc_edit, self.browse_button, self.load_button, self.cbb_table, - self.timeout_spin, self.advanced_toggle, self.advanced_panel): + self.timeout_spin, self.advanced_toggle, self.advanced_panel, + self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button): widget.setEnabled(not self.busy) + counts = self.visible_counts() + self.counts_label.setText(f'可见 {counts["visible"]} / {len(self._records_by_name)} · ' + f'READY {counts["ready"]} · 异常/待确认 {counts["error"]}') self.stop_button.setEnabled(self.busy and self.loaded_path is not None and not self.stop.is_set()) + def visible_records(self): + """Cached records in table order, restricted to the current local view.""" + return tuple(record for name, record in self._records_by_name.items() + if not self.cbb_table.isRowHidden(self._row_by_name[name])) + + def selected_records(self): + """Visible checked management records, including READY/missing projects.""" + return tuple(record for record in self.visible_records() + if self.cbb_table.item(self._row_by_name[record.name], 0).checkState() == Qt.Checked) + + def visible_counts(self): + records = self.visible_records() + return {'visible': len(records), 'ready': sum(r.status == 'READY' for r in records), + 'error': sum(r.status not in {'READY', 'PENDING'} or r.name in self.skipped_names + for r in records)} + + def _eligible_names(self, *, checked_only=False): + records = self.selected_records() if checked_only else self.visible_records() + return [r.name for r in records if r.status != 'READY' and r.name not in self.skipped_names] + + def _maintenance_names(self): + return self._eligible_names(checked_only=bool(self.selected_records())) + + def _check_visible(self, state): + self.cbb_table.blockSignals(True) + try: + for record in self.visible_records(): + self.cbb_table.item(self._row_by_name[record.name], 0).setCheckState(state) + finally: + self.cbb_table.blockSignals(False) + self._update() + + def _apply_filters(self, *_): + keyword = self.search_edit.text().strip().casefold() + mode = self.status_filter.currentData() + self.cbb_table.blockSignals(True) + self.cbb_table.setUpdatesEnabled(False) + try: + for name, record in self._records_by_name.items(): + matches = (not keyword or keyword in record.name.casefold() + or keyword in record.partition_name.casefold()) + matches = matches and (mode == 'all' + or mode == 'ready' and record.status == 'READY' + or mode == 'non_ready' and record.status != 'READY' + or mode == 'pending' and record.status == 'PENDING' + or mode == 'errors' and record.status not in {'READY', 'PENDING'} + or mode == 'missing' and name in self.skipped_names) + row = self._row_by_name[name] + if not matches: + self.cbb_table.item(row, 0).setCheckState(Qt.Unchecked) + self.cbb_table.setRowHidden(row, not matches) + finally: + self.cbb_table.setUpdatesEnabled(True) + self.cbb_table.blockSignals(False) + self._update() + def choose_library(self): path = QFileDialog.getOpenFileName(self, '选择中心库', '', 'LMC (*.lmc)')[0] if path: self.lmc_edit.setText(path); self.load_library() @@ -228,6 +307,7 @@ class MainWindow(QWidget): self.icdb_progress.hide(); self.icdb_phase.hide() self._release_library_lease() self.loaded_path = None; self.cbb_table.setRowCount(0); self._row_by_name = {}; self.ready_names = set() + self._records_by_name = {} self.skipped_names = set(); self.skip_reasons = {} path = self.lmc_edit.text() self.status_label.setText('正在读取中心库…') @@ -281,6 +361,7 @@ class MainWindow(QWidget): self.ready_names = {r.name for r in result.records if r.status == 'READY' and r.name not in skip_reasons} self.skip_reasons = dict(skip_reasons) self.skipped_names = set(skip_reasons) + self._records_by_name = {record.name: record for record in result.records} # Populating a large library emits itemChanged for every cell. # Block those signals and refresh controls once after the batch; # otherwise _update() repeatedly scans the growing table (O(n^2)). @@ -291,17 +372,15 @@ class MainWindow(QWidget): for row, record in enumerate(result.records): self._row_by_name[record.name] = row item = QTableWidgetItem(record.name) - if record.status == 'READY' or record.name in self.skipped_names: - # Already green: nothing left to verify, so it cannot be queued. - item.setFlags(item.flags() & ~Qt.ItemIsUserCheckable) - else: - item.setFlags(item.flags() | Qt.ItemIsUserCheckable) + # Management selection includes READY and missing projects; + # operation queues independently enforce eligibility. + item.setFlags(item.flags() | Qt.ItemIsUserCheckable) item.setCheckState(Qt.Unchecked) self.cbb_table.setItem(row, 0, item) self.cbb_table.setItem(row, 1, QTableWidgetItem(record.partition_name)) status_text = STATUS_LABELS.get(record.status, record.status) if record.name in self.skipped_names: - status_text = '跳过:缺少工程/PCB' + status_text += ' · 缺少工程/PCB' self.cbb_table.setItem(row, 2, QTableWidgetItem(status_text)) # Preflight touches each CBB's project and was previously run # synchronously in this GUI callback, making large libraries @@ -332,6 +411,7 @@ class MainWindow(QWidget): self.status_label.setText( f'已读取 {len(result.records)} 个 CBB,其中 Ready {ready_count} 个,' f'缺文件跳过 {len(self.skipped_names)} 个。{suffix}') + self._apply_filters() self._start(read, loaded) def _request(self, name): @@ -345,56 +425,58 @@ class MainWindow(QWidget): native_lm_verify=self.native_lm_verify_check.isChecked()) def _selected_names(self): - names = [] - for row in range(self.cbb_table.rowCount()): - item = self.cbb_table.item(row, 0) - if item and item.checkState() == Qt.Checked and (item.flags() & Qt.ItemIsUserCheckable): - names.append(item.text()) - return names + return [record.name for record in self.selected_records()] def verify_selected(self): - self._run(self._selected_names()) + self._run(self._eligible_names(checked_only=True)) def verify_all(self): - names = [self.cbb_table.item(row, 0).text() for row in range(self.cbb_table.rowCount())] - self._run([name for name in names if name not in self.ready_names and name not in self.skipped_names]) + self._run(self._eligible_names()) def repair_icdb_all(self): if self.busy or not self.loaded_path: return - names = [self.cbb_table.item(row, 0).text() for row in range(self.cbb_table.rowCount()) - if self.cbb_table.item(row, 0).text() not in self.skipped_names] + names = self._maintenance_names() if not names: return - answer = QMessageBox.question(self, '全库 ICDB 检查与修复', + answer = QMessageBox.question(self, '非 READY ICDB 检查与修复', f'当前中心库:{self.loaded_path}\n\n' - f'将检查 {len(names)} 个 CBB,其中 Ready {len(self.ready_names)} 个;' - f'缺少工程文件跳过 {len(self.skipped_names)} 个。\n' + f'将检查 {len(names)} 个可见非 READY CBB' + f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' '操作会重新校验并保存工程。确认失联问题后,完整备份目标工程并尝试一次原生修复。\n' '共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;' '会话安全无法确认时停止队列。\n\n是否开始检查与修复?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: - self._run(names, include_ready=True) + self._run(names, maintenance=True) def restore_backup(self): if self.busy or not self.loaded_path: return from .backup_recovery_dialog import BackupRecoveryDialog - names = [self.cbb_table.item(row, 0).text() for row in range(self.cbb_table.rowCount())] + names = self._maintenance_names() if not names: return - self.backup_dialog = BackupRecoveryDialog(names, self._request, self) + self.backup_dialog = BackupRecoveryDialog(names, self._backup_request, self) row = self.cbb_table.currentRow() - if row >= 0: + if row >= 0 and self.cbb_table.item(row, 0).text() in names: self.backup_dialog.block_combo.setCurrentText(self.cbb_table.item(row, 0).text()) self.backup_dialog.started.connect(self._backup_started) self.backup_dialog.completed.connect(self._backup_completed) - self.backup_dialog.finished.connect(lambda _code: self._finished()) + self.backup_dialog.finished.connect(lambda _code: self._backup_dialog_closed()) self.backup_dialog.setModal(True) self.backup_dialog.show() + def _backup_request(self, name): + # A still-open dialog must recheck a target after an earlier recovery + # or an authoritative refresh changed its eligibility. + if self.busy or name not in self._maintenance_names(): + raise ValueError('目标需为当前可见、可用的非 READY CBB;请重新选择。') + return self._request(name) + def _backup_started(self, name): self.busy = True self._global_icdb_run = False self.ready_names.discard(name) + if name in self._records_by_name: + self._records_by_name[name] = replace(self._records_by_name[name], status='UNKNOWN') row = self._row_by_name.get(name) if row is not None: self.cbb_table.item(row, 2).setText('恢复中 / 需确认') @@ -413,27 +495,37 @@ class MainWindow(QWidget): self.cbb_table.item(row, 2).setText('未知 / 需确认') self._progress({'block': name, 'stage': result.status, 'detail': result.stderr, 'evidence': str(result.evidence_path or '')}) + if result.safe_to_continue: + self.busy = False + self.refresh_affected_records([name]) + else: + self._defer_status_refresh([name]) - def _run(self, names, *, include_ready=False): + def _backup_dialog_closed(self): + if not getattr(self, '_refreshing_records', False): + self._finished() + + def _run(self, names, *, maintenance=False): if self.busy or not self.loaded_path: return - names = [name for name in names if (include_ready or name not in self.ready_names) and name not in self.skipped_names] + eligible = set(self._eligible_names()) + names = [name for name in names if name in eligible] if not names: - self.status_label.setText('没有需要校验的 CBB(全部已经是绿色 / Ready)。') + self.status_label.setText('当前可见选择中没有可校验的非 READY CBB。') self._update() return requests = [self._request(name) for name in names] - self._global_icdb_run = include_ready + self._global_icdb_run = maintenance self._icdb_completed = 0 - self.icdb_progress.setVisible(include_ready) - self.icdb_phase.setVisible(include_ready) - if include_ready: + self.icdb_progress.setVisible(maintenance) + self.icdb_phase.setVisible(maintenance) + if maintenance: self.icdb_progress.setRange(0, len(requests)) self.icdb_progress.setValue(0) self.icdb_progress.setFormat('已处理 %v / %m 个 CBB') self.icdb_phase.setText('准备检查…') self.stop.clear(); self.recovery_path = None; self.recovery_button.setEnabled(False) self.output.clear(); self.status_label.setText( - f'正在全库 ICDB 检查与修复:共 {len(names)} 个 CBB' if include_ready + f'正在非 READY ICDB 检查与修复:共 {len(names)} 个 CBB' if maintenance else f'正在校验:共 {len(names)} 个 CBB') def action(): from cbb_refresh_verify.verify_runner import read_library_flag_if_available, run_verify @@ -441,18 +533,99 @@ class MainWindow(QWidget): req, progress=lambda e: self.signals.event.emit(e), flag_reader=read_library_flag_if_available)) def verifier(req): - if include_ready: + if maintenance: self.signals.event.emit({'block': req.block_name, 'stage': 'icdb_check_start'}) return verify(req) results = run_batch(requests, verifier=verifier, stop=self.stop, progress=lambda name, result: self.signals.event.emit({'block': name, 'stage': result.status, 'detail': result.stderr, 'evidence': str(result.evidence_path or '')})) report = write_batch_report(requests, results, self.data_root/'reports') - if include_ready: + if maintenance: from cbb_refresh_verify.icdb_maintenance import write_maintenance_report - write_maintenance_report(requests, results, report, self.skipped_names) - return results, report + write_maintenance_report(requests, results, report, ()) + affected = [req.block_name for req, result in zip(requests, results) if result.status != 'SKIPPED'] + safe = all(result.safe_to_continue for result in results) + refresh = self._read_affected_records(affected) if safe else None + return results, report, affected, refresh self._start(action, self._batch_done) + def _read_affected_records(self, names): + """Worker-only read; never derive library state from action outcomes.""" + import pythoncom + pythoncom.CoInitialize() + try: + path = Path(self.mentor_lmc_path or self.loaded_path) + try: + statuses = self.status_refresher(tuple(names), path) + reasons = {name: reason for name in names + if (reason := preflight_skip_reason(path, name))} + return tuple(names), statuses, reasons, '' + except Exception as error: + return tuple(names), {}, None, str(error) + finally: + pythoncom.CoUninitialize() + + def _apply_refreshed_records(self, payload): + names, statuses, reasons, error = payload + self.cbb_table.blockSignals(True) + try: + for name in names: + if name not in self._records_by_name: + continue + status = statuses.get(name, 'UNKNOWN') + if status not in STATUS_LABELS: + status = 'UNKNOWN' + self._records_by_name[name] = replace(self._records_by_name[name], status=status) + self.ready_names.discard(name) + if reasons is not None: + self.skip_reasons.pop(name, None) + if name in reasons: + self.skip_reasons[name] = reasons[name] + self.skipped_names = set(self.skip_reasons) + row = self._row_by_name[name] + text = STATUS_LABELS[status] + if name in self.skipped_names: + text += ' · 缺少工程/PCB' + self.cbb_table.item(row, 2).setText(text) + self.cbb_table.item(row, 2).setToolTip(error or self.skip_reasons.get(name, '')) + if status == 'READY' and name not in self.skipped_names: + self.ready_names.add(name) + self._mark_row_ready(row) + else: + for column in range(self.cbb_table.columnCount()): + self.cbb_table.item(row, column).setBackground(QColor('#FFF8E1')) + finally: + self.cbb_table.blockSignals(False) + if error: + self.output.append(f'状态复核未完成 / 需确认:{error}') + self._apply_filters() + return tuple(self._records_by_name[name] for name in names if name in self._records_by_name) + + def _defer_status_refresh(self, names): + self._apply_refreshed_records((tuple(names), {}, None, + '会话或数据库安全未确认;暂缓原生状态复核,请确认释放后重新读取。')) + + def refresh_affected_records(self, names, callback=None): + """Start an affected-only authoritative refresh once native owners release. + + Return True if scheduled, False if busy/unloaded/empty. Callback receives + tuple[CbbRecord, ...] on the GUI thread after applying refreshed statuses + (UNKNOWN on read failure). It runs before the worker's finished signal. + Search, filter, original logical_type and action-result cells are retained. + """ + if self.busy or not self.loaded_path: + return False + targets = tuple(dict.fromkeys(name for name in names if name in self._records_by_name)) + if not targets: + return False + self._refreshing_records = True + def refreshed(payload): + records = self._apply_refreshed_records(payload) + self._refreshing_records = False + if callback: + callback(records) + self._start(lambda: self._read_affected_records(targets), refreshed) + return True + def _progress(self, event): line = f"{event.get('block', '')} | {event.get('stage', '')}" for key in ('detail', 'error', 'action', 'statuses', 'evidence'): @@ -474,40 +647,31 @@ class MainWindow(QWidget): if block and stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED'}: row = self._row_by_name.get(block) if row is not None: - if stage == 'PASS': - self.cbb_table.item(row, 2).setText('绿色 / Ready') - self.cbb_table.item(row, 3).setText('PASS') - self.ready_names.add(block) - self._mark_row_ready(row) - else: - if getattr(self, '_global_icdb_run', False) and stage != 'SKIPPED': - self.ready_names.discard(block) - self.cbb_table.item(row, 2).setText('未知 / 需确认') - item = self.cbb_table.item(row, 0) - item.setFlags(item.flags() | Qt.ItemIsUserCheckable) - for column in range(self.cbb_table.columnCount()): - self.cbb_table.item(row, column).setBackground(QColor('#FFF8E1')) - detail = str(event.get('detail') or event.get('error') or '').strip() - label = str(stage) if not detail else f'{stage}: {detail[:96]}' - self.cbb_table.item(row, 3).setText(label) - if detail: - self.cbb_table.item(row, 3).setToolTip(detail) + detail = str(event.get('detail') or event.get('error') or '').strip() + label = str(stage) if not detail else f'{stage}: {detail[:96]}' + self.cbb_table.item(row, 3).setText(label) + self.cbb_table.item(row, 3).setToolTip(detail) if self.logging_context: self.logging_context.logger.info('cbb_progress %s', line) def _mark_row_ready(self, row): - """Make a verified row visibly complete and remove it from the queue.""" + """Color authoritative READY rows while retaining management selection.""" name_item = self.cbb_table.item(row, 0) if name_item is None: return - name_item.setCheckState(Qt.Unchecked) - name_item.setFlags(name_item.flags() & ~Qt.ItemIsUserCheckable) + name_item.setFlags(name_item.flags() | Qt.ItemIsUserCheckable) for column in range(self.cbb_table.columnCount()): cell = self.cbb_table.item(row, column) if cell is not None: cell.setBackground(QColor('#E8F5E9')) def _batch_done(self, outcome): - results, report = outcome + results, report = outcome[:2] + if len(outcome) > 2: + _, _, affected, refresh = outcome + if refresh is None: + self._defer_status_refresh(affected) + else: + self._apply_refreshed_records(refresh) if self.library_lease is not None and any(not result.safe_to_continue for result in results): self.library_lease.retain_for_recovery() for result in results: @@ -522,22 +686,22 @@ class MainWindow(QWidget): if self._global_icdb_run: from cbb_refresh_verify.icdb_maintenance import maintenance_counts counts = maintenance_counts(results) - text = ('全库 ICDB 检查已结束\n' - f'校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped},预检跳过 {len(self.skipped_names)}\n' + text = ('非 READY ICDB 检查已结束\n' + f'校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' f'原生修复完成 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}\n' f'完整日志:{report}\n仅失败日志:{failure_report_path(report)}') - self.icdb_phase.setText('已停止,存在待处理项目' if failed or skipped else '已完成全库检查') + self.icdb_phase.setText('已停止,存在待处理项目' if failed or skipped else '已完成非 READY 检查') if any(not result.safe_to_continue for result in results): text += '\n未确认数据库或会话安全,已停止剩余校验。请查看恢复现场。' status_text = text if self._global_icdb_run: - status_text = (f'全库检查结束:通过 {passed},失败/受阻 {failed},未执行 {skipped}。\n' + status_text = (f'非 READY 检查结束:通过 {passed},失败/受阻 {failed},未执行 {skipped}。\n' f'原生修复 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}。') self.status_label.setText(status_text); self.output.append(text) self.summary = QMessageBox(self) - self.summary.setWindowTitle('全库 ICDB 检查与修复结果' if self._global_icdb_run else 'CBB 校验结果'); self.summary.setText(text) + self.summary.setWindowTitle('非 READY ICDB 检查与修复结果' if self._global_icdb_run else 'CBB 校验结果'); self.summary.setText(text) self.summary.setIcon(QMessageBox.Warning if failed else QMessageBox.Information) # Modeless notification: do not force activation or steal keyboard focus. from PySide6.QtCore import Qt diff --git a/tests/test_backup_recovery_dialog.py b/tests/test_backup_recovery_dialog.py index 071dfe6..e6a6fff 100644 --- a/tests/test_backup_recovery_dialog.py +++ b/tests/test_backup_recovery_dialog.py @@ -51,11 +51,12 @@ def test_backup_dialog_keeps_ui_responsive_and_cannot_close_during_restore(qtbot assert dialog.close_button.isEnabled() -def test_main_window_opens_simple_dialog_and_failure_clears_ready(qtbot, tmp_path): +def test_main_window_opens_non_ready_dialog_and_failure_retains_ready_untargeted(qtbot, tmp_path): from cbb_refresh_verify.ui.main_window import MainWindow from cbb_refresh_verify.library_reader import CbbRecord, LibraryReadResult window = MainWindow(reader=lambda _: LibraryReadResult(( - CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), data_root=tmp_path) + CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), data_root=tmp_path, + status_refresher=lambda names, _: {name: 'UNKNOWN' for name in names}) qtbot.addWidget(window) window.lmc_edit.setText('lib.lmc'); window.load_library() qtbot.waitUntil(lambda: not window.busy) @@ -63,12 +64,12 @@ def test_main_window_opens_simple_dialog_and_failure_clears_ready(qtbot, tmp_pat window.backup_restore_button.click() dialog = window.backup_dialog assert dialog.isVisible() - assert dialog.block_combo.currentText() == 'A' - dialog.started.emit('A') + assert dialog.block_combo.currentText() == 'B' + dialog.started.emit('B') assert window.busy - assert 'A' not in window.ready_names - dialog.completed.emit('A', VerifyResult('FAIL', stderr='损坏')) - assert window.cbb_table.item(0, 2).text() == '未知 / 需确认' + assert 'A' in window.ready_names + dialog.completed.emit('B', VerifyResult('FAIL', stderr='无法确认', safe_to_continue=False)) + assert window.cbb_table.item(1, 2).text() == '未知 / 需确认' dialog.reject() qtbot.waitUntil(lambda: not window.busy) diff --git a/tests/test_ui.py b/tests/test_ui.py index 52e6b2b..6277c69 100644 --- a/tests/test_ui.py +++ b/tests/test_ui.py @@ -1,4 +1,5 @@ import os +import pytest from pathlib import Path os.environ.setdefault('QT_QPA_PLATFORM','offscreen') from cbb_refresh_verify.models import CbbRecord, LibraryReadResult @@ -21,7 +22,15 @@ def test_application_palette_keeps_table_text_readable(qtbot): def fake_reader(_): return LibraryReadResult((CbbRecord('CBB1','Reusable Blocks'),)) -def test_global_icdb_action_confirms_and_includes_ready_rows(qtbot, tmp_path, monkeypatch): + +@pytest.fixture(autouse=True) +def isolate_authoritative_native_refresh(monkeypatch): + # Production refresh crosses the native Mentor boundary; these are UI tests. + monkeypatch.setattr('cbb_refresh_verify.ui.main_window.authoritative_status_map', + lambda names, *_: {name: 'READY' for name in names}) + + +def test_global_icdb_action_confirms_and_excludes_ready_rows(qtbot, tmp_path, monkeypatch): from PySide6.QtWidgets import QMessageBox calls = [] window = MainWindow(reader=lambda _: LibraryReadResult(( @@ -37,7 +46,7 @@ def test_global_icdb_action_confirms_and_includes_ready_rows(qtbot, tmp_path, mo monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) window.icdb_repair_button.click() qtbot.waitUntil(lambda: not window.busy) - assert calls == ['READY', 'PENDING'] + assert calls == ['PENDING'] def test_global_icdb_action_disabled_without_library_or_while_busy(qtbot): window = MainWindow(reader=fake_reader) @@ -49,11 +58,11 @@ def test_global_icdb_action_disabled_without_library_or_while_busy(qtbot): window.busy = True; window._update() assert not window.icdb_repair_button.isEnabled() -def test_global_icdb_failure_clears_ready_and_stops_queue(qtbot, tmp_path, monkeypatch): +def test_global_icdb_failure_preserves_untargeted_ready_and_stops_queue(qtbot, tmp_path, monkeypatch): from PySide6.QtWidgets import QMessageBox calls = [] window = MainWindow(reader=lambda _: LibraryReadResult(( - CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), + CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'), CbbRecord('C', 'LP'))), verifier=lambda req: (calls.append(req.block_name) or VerifyResult( 'FAIL', stderr='unconfirmed', safe_to_continue=False)), data_root=tmp_path) qtbot.addWidget(window) @@ -62,10 +71,10 @@ def test_global_icdb_failure_clears_ready_and_stops_queue(qtbot, tmp_path, monke monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) window.icdb_repair_button.click() qtbot.waitUntil(lambda: not window.busy) - assert calls == ['A'] - assert 'A' not in window.ready_names - assert window.cbb_table.item(0, 2).text() == '未知 / 需确认' - assert window.cbb_table.item(1, 3).text().startswith('SKIPPED') + assert calls == ['B'] + assert 'A' in window.ready_names + assert window.cbb_table.item(1, 2).text() == '未知 / 需确认' + assert window.cbb_table.item(2, 3).text().startswith('SKIPPED') def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch): from PySide6.QtWidgets import QMessageBox @@ -83,11 +92,11 @@ def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch): window.icdb_repair_button.click() qtbot.waitUntil(lambda: not window.busy) assert 'lib.lmc' in confirmations[0] - assert 'Ready' in confirmations[0] + assert '非 READY' in confirmations[0] assert window.icdb_progress.value() == 1 assert window.icdb_progress.maximum() == 1 assert '恢复备份待复核 1' in window.status_label.text() - assert window.summary.windowTitle() == '全库 ICDB 检查与修复结果' + assert window.summary.windowTitle() == '非 READY ICDB 检查与修复结果' reports = list((tmp_path/'reports').glob('icdb-*.json')) assert len(reports) == 1 @@ -102,7 +111,7 @@ def test_ready_row_missing_project_is_skipped_by_global_maintenance(qtbot, tmp_p assert window.skipped_names == {'MISSING'} assert 'MISSING' not in window.ready_names assert not window.icdb_repair_button.isEnabled() - assert window.cbb_table.item(0, 2).text().startswith('跳过') + assert '缺少工程' in window.cbb_table.item(0, 2).text() def test_verify_requires_loaded_library_and_invalidates_on_path_change(qtbot): window=MainWindow(reader=fake_reader, verifier=lambda _: VerifyResult('PASS')); qtbot.addWidget(window) @@ -156,8 +165,8 @@ def test_import_incomplete_rows_are_skipped_and_disabled(qtbot, tmp_path): window.lmc_edit.setText(str(lmc)) window.load_library() qtbot.waitUntil(lambda: window.batch_button.isEnabled()) - assert window.cbb_table.item(0, 2).text().startswith('跳过:') - assert not (window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable) + assert '缺少工程' in window.cbb_table.item(0, 2).text() + assert window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable window.verify_all() qtbot.waitUntil(lambda: not window.busy) assert calls == ['VALID_CBB'] @@ -218,7 +227,7 @@ def test_large_library_load_does_not_update_controls_for_every_table_cell(qtbot, assert window._row_by_name['CBB_0299'] == 299 assert len(calls) < 20 -def test_ready_rows_show_status_and_cannot_be_selected_or_verified(qtbot, tmp_path): +def test_ready_rows_allow_management_selection_but_cannot_be_verified(qtbot, tmp_path): def reader(_): return LibraryReadResult((CbbRecord('READY_CBB', 'LP', status='READY'), CbbRecord('PENDING_CBB', 'LP', status='PENDING'))) calls = [] @@ -229,15 +238,16 @@ def test_ready_rows_show_status_and_cannot_be_selected_or_verified(qtbot, tmp_pa qtbot.waitUntil(lambda: window.batch_button.isEnabled()) assert window.cbb_table.columnCount() == 4 assert window.cbb_table.item(0, 2).text() == '绿色 / Ready' - assert not (window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable) + assert window.cbb_table.item(0, 0).flags() & Qt.ItemIsUserCheckable assert window.cbb_table.item(0, 0).background().color().name().lower() == '#e8f5e9' window.cbb_table.item(0, 0).setCheckState(Qt.Checked) - assert 'READY_CBB' not in window._selected_names() + assert 'READY_CBB' in window._selected_names() + assert not window.selected_batch_button.isEnabled() window.verify_all() qtbot.waitUntil(lambda: not window.busy) assert calls == ['PENDING_CBB'] assert window.cbb_table.item(1, 0).checkState() == Qt.Unchecked - assert not (window.cbb_table.item(1, 0).flags() & Qt.ItemIsUserCheckable) + assert window.cbb_table.item(1, 0).flags() & Qt.ItemIsUserCheckable assert window.cbb_table.item(1, 0).background().color().name().lower() == '#e8f5e9' diff --git a/tests/test_ui_filters.py b/tests/test_ui_filters.py new file mode 100644 index 0000000..edd026a --- /dev/null +++ b/tests/test_ui_filters.py @@ -0,0 +1,290 @@ +"""Local catalog scope and authoritative status refresh regression tests.""" +import json +import time +from threading import Event +from pathlib import Path +from types import SimpleNamespace + +import pytest +from PySide6.QtCore import Qt +from PySide6.QtWidgets import QMessageBox + +from cbb_refresh_verify.models import CbbRecord, LibraryReadResult +from cbb_refresh_verify.ui.main_window import MainWindow +from cbb_refresh_verify.verify_runner import VerifyResult + + +MIXED = ( + CbbRecord('Alpha', 'Power', status='READY'), + CbbRecord('Beta', 'Power', status='PENDING'), + CbbRecord('Gamma', 'Analog', status='UNKNOWN'), + CbbRecord('Delta', 'Analog', status='ERROR'), + CbbRecord('Missing', 'Power', status='READY'), +) + + +def load_window(qtbot, tmp_path, monkeypatch, records=MIXED, **kwargs): + import cbb_refresh_verify.ui.main_window as ui + monkeypatch.setattr(ui, 'preflight_skip_reason', + lambda _, name: 'missing project' if name == 'Missing' else None) + lease = SimpleNamespace(mentor_path=Path('test.lmc'), close=lambda: None, + retain_for_recovery=lambda: None) + window = MainWindow(reader=lambda _: LibraryReadResult(records), data_root=tmp_path, + path_lease_factory=lambda _: lease, **kwargs) + qtbot.addWidget(window) + window.lmc_edit.setText('test.lmc') + window.load_library() + qtbot.waitUntil(lambda: not window.busy) + return window + + +def names(records): + return [record.name for record in records] + + +def test_search_and_filters_use_cached_catalog_and_count_visible_states(qtbot, tmp_path, monkeypatch): + window = load_window(qtbot, tmp_path, monkeypatch) + window.reader = lambda _: pytest.fail('filter reread Mentor') + assert window.visible_counts() == {'visible': 5, 'ready': 2, 'error': 3} + expected = { + 'ready': ['Alpha', 'Missing'], 'non_ready': ['Beta', 'Gamma', 'Delta'], + 'pending': ['Beta'], 'errors': ['Gamma', 'Delta'], 'missing': ['Missing'], + } + for mode, wanted in expected.items(): + window.status_filter.setCurrentIndex(window.status_filter.findData(mode)) + assert names(window.visible_records()) == wanted + window.status_filter.setCurrentIndex(window.status_filter.findData('all')) + window.search_edit.setText(' ANALOG ') + assert names(window.visible_records()) == ['Gamma', 'Delta'] + assert window.visible_counts() == {'visible': 2, 'ready': 0, 'error': 2} + window.search_edit.setText('aLpH') + assert names(window.visible_records()) == ['Alpha'] + assert '1' in window.counts_label.text() + + +def test_management_selection_includes_ready_and_missing_but_hiding_clears_checks(qtbot, tmp_path, monkeypatch): + window = load_window(qtbot, tmp_path, monkeypatch) + window.select_visible_button.click() + assert names(window.selected_records()) == ['Alpha', 'Beta', 'Gamma', 'Delta', 'Missing'] + assert window.selected_records()[0].logical_type == 'LP' + window.search_edit.setText('power') + assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing'] + window.search_edit.clear() + assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing'] + window.clear_selection_button.click() + assert window.selected_records() == () + + +def test_verify_all_and_selected_only_target_visible_non_ready_with_projects(qtbot, tmp_path, monkeypatch): + calls = [] + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')), + status_refresher=lambda names, _: {name: 'READY' for name in names}) + window.search_edit.setText('power') + window.select_visible_button.click() + window.verify_selected() + qtbot.waitUntil(lambda: not window.busy) + assert calls == ['Beta'] + window.search_edit.setText('analog') + window.verify_all() + qtbot.waitUntil(lambda: not window.busy) + assert calls == ['Beta', 'Gamma', 'Delta'] + + +def test_maintenance_confirmation_progress_and_report_use_visible_checked_subset(qtbot, tmp_path, monkeypatch): + calls, confirmations, refreshes = [], [], [] + def refresh(targets, _): + refreshes.append(tuple(targets)) + return {name: 'PENDING' for name in targets} + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')), + status_refresher=refresh) + window.search_edit.setText('analog') + window.cbb_table.item(3, 0).setCheckState(Qt.Checked) + monkeypatch.setattr(QMessageBox, 'question', lambda *args, **_: ( + confirmations.append(args[2]) or QMessageBox.Yes)) + window.repair_icdb_all() + qtbot.waitUntil(lambda: not window.busy) + assert calls == ['Delta'] + assert refreshes == [('Delta',)] + assert '1' in confirmations[0] and '非 READY' in confirmations[0] + assert window.icdb_progress.maximum() == 1 and window.icdb_progress.value() == 1 + report = json.loads(next((tmp_path/'reports').glob('icdb-*.json')).read_text(encoding='utf-8')) + assert report['includes_ready'] is False + assert report['target_names'] == ['Delta'] + assert report['preflight_skipped'] == [] + assert report['counts']['total'] == 1 + assert window.search_edit.text() == 'analog' + assert window.cbb_table.item(3, 3).text() == 'PASS' + assert names(window.visible_records()) == ['Gamma', 'Delta'] + assert window.visible_records()[1].status == 'PENDING' + + +def test_maintenance_defaults_to_visible_eligible_and_ready_only_checks_do_not_fallback(qtbot, tmp_path, monkeypatch): + calls = [] + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')), + status_refresher=lambda targets, _: {name: 'PENDING' for name in targets}) + monkeypatch.setattr(QMessageBox, 'question', lambda *_, **__: QMessageBox.Yes) + window.cbb_table.item(0, 0).setCheckState(Qt.Checked) + window.repair_icdb_all() + assert not window.busy and calls == [] + window.clear_selection_button.click() + window.search_edit.setText('power') + window.repair_icdb_all() + qtbot.waitUntil(lambda: not window.busy) + assert calls == ['Beta'] + + +def test_recovery_defaults_to_visible_checked_non_ready_and_skips_ready(qtbot, tmp_path, monkeypatch): + window = load_window(qtbot, tmp_path, monkeypatch) + window.search_edit.setText('power') + window.restore_backup() + assert [window.backup_dialog.block_combo.itemText(i) + for i in range(window.backup_dialog.block_combo.count())] == ['Beta'] + window.backup_dialog.reject() + window.search_edit.clear() + window.cbb_table.item(3, 0).setCheckState(Qt.Checked) + window.restore_backup() + assert window.backup_dialog.block_combo.currentText() == 'Delta' + assert window.backup_dialog.block_combo.count() == 1 + window.backup_dialog.reject() + + +def test_action_pass_does_not_promote_pending_without_authoritative_ready(qtbot, tmp_path, monkeypatch): + entered = [] + window = load_window(qtbot, tmp_path, monkeypatch, + status_refresher=lambda targets, _: (entered.extend(targets) or {'Beta': 'READY'})) + window.status_filter.setCurrentIndex(window.status_filter.findData('pending')) + window.cbb_table.item(1, 0).setCheckState(Qt.Checked) + window._progress({'block': 'Beta', 'stage': 'PASS'}) + assert names(window.visible_records()) == ['Beta'] + assert window.visible_records()[0].status == 'PENDING' + window.refresh_affected_records(['Beta']) + qtbot.waitUntil(lambda: not window.busy) + assert entered == ['Beta'] + assert window.visible_records() == () + assert window.selected_records() == () + assert window.status_filter.currentData() == 'pending' + window.status_filter.setCurrentIndex(window.status_filter.findData('ready')) + assert names(window.visible_records()) == ['Alpha', 'Beta', 'Missing'] + assert window.cbb_table.item(1, 3).text() == 'PASS' + + +def test_refresh_failure_demotes_affected_without_losing_other_statuses(qtbot, tmp_path, monkeypatch): + def fail(*_): + raise OSError('bridge unavailable') + window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=fail) + window.refresh_affected_records(['Alpha']) + qtbot.waitUntil(lambda: not window.busy) + assert window.visible_records()[0].status == 'UNKNOWN' + assert window.visible_records()[1].status == 'PENDING' + assert 'Alpha' not in window.ready_names + assert 'bridge unavailable' in window.output.toPlainText() + + +def test_unsafe_result_defers_native_refresh_and_retains_action_evidence(qtbot, tmp_path, monkeypatch): + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda _: VerifyResult('FAIL', stderr='owner release unconfirmed', + safe_to_continue=False, evidence_path=tmp_path/'evidence.json'), + status_refresher=lambda *_: pytest.fail('unsafe owner was reopened')) + window.search_edit.setText('beta') + window.verify_all() + qtbot.waitUntil(lambda: not window.busy) + assert window.visible_records()[0].status == 'UNKNOWN' + assert window.cbb_table.item(1, 3).text() == 'FAIL: owner release unconfirmed' + assert str(tmp_path/'evidence.json') in window.output.toPlainText() + assert '暂缓原生状态复核' in window.cbb_table.item(1, 2).toolTip() + + +def test_refresh_callback_uses_only_known_affected_records_and_preserves_logical_type(qtbot, tmp_path, monkeypatch): + observed, callbacks = [], [] + def refresh(targets, path): + observed.append((targets, path)) + return {'X': 'READY'} + window = load_window(qtbot, tmp_path, monkeypatch, + records=(CbbRecord('X', 'Display label', logical_type='RULP', status='PENDING'),), + status_refresher=refresh) + assert window.refresh_affected_records(['unknown']) is False + assert window.refresh_affected_records(['X', 'X', 'unknown'], callbacks.append) is True + assert window.refresh_affected_records(['X']) is False + qtbot.waitUntil(lambda: not window.busy) + assert observed == [(('X',), Path('test.lmc'))] + assert callbacks == [(CbbRecord('X', 'Display label', logical_type='RULP', status='READY'),)] + + +def test_backup_completion_refreshes_status_without_dialog_close_unlocking_pending_refresh(qtbot, tmp_path, monkeypatch): + entered, release = Event(), Event() + def refresh(targets, _): + entered.set() + release.wait(3) + return {name: 'PENDING' for name in targets} + window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=refresh) + window.search_edit.setText('beta') + window.restore_backup() + window.backup_dialog.started.emit('Beta') + window.backup_dialog.completed.emit('Beta', VerifyResult('PASS')) + try: + qtbot.waitUntil(entered.is_set) + window.backup_dialog.reject() + assert window.busy + finally: + release.set() + qtbot.waitUntil(lambda: not window.busy) + assert window.visible_records()[0].status == 'PENDING' + assert window.cbb_table.item(1, 3).text() == 'PASS' + + +def test_missing_project_keeps_explicit_authoritative_status(qtbot, tmp_path, monkeypatch): + window = load_window(qtbot, tmp_path, monkeypatch) + assert 'Ready' in window.cbb_table.item(4, 2).text() + assert '缺少工程' in window.cbb_table.item(4, 2).text() + + +def test_open_backup_dialog_cannot_restore_target_that_became_ready(qtbot, tmp_path, monkeypatch): + calls = [] + window = load_window(qtbot, tmp_path, monkeypatch, + status_refresher=lambda targets, _: {name: 'READY' for name in targets}) + window.search_edit.setText('beta') + window.restore_backup() + dialog = window.backup_dialog + source = tmp_path/'Beta.prj' + source.touch() + dialog.source_edit.setText(str(source)) + dialog.recoverer = lambda *_, **__: (calls.append('restore') or VerifyResult('PASS')) + window.refresh_affected_records(['Beta']) + qtbot.waitUntil(lambda: not window.busy) + dialog.restore_button.click() + qtbot.waitUntil(lambda: not dialog.busy) + assert calls == [] + assert '非 READY' in dialog.status_label.text() + dialog.reject() + + +@pytest.mark.parametrize('status', ['UNKNOWN', 'UNAVAILABLE', 'ERROR', 'TIMEOUT', 'MALFORMED', 'CRASHED', + 'UNRECOGNIZED']) +def test_unconfirmed_statuses_are_non_ready_and_error_filter_candidates(qtbot, tmp_path, monkeypatch, status): + window = load_window(qtbot, tmp_path, monkeypatch, records=(CbbRecord('X', 'LP', status=status),)) + window.status_filter.setCurrentIndex(window.status_filter.findData('non_ready')) + assert names(window.visible_records()) == ['X'] + window.status_filter.setCurrentIndex(window.status_filter.findData('errors')) + assert names(window.visible_records()) == ['X'] + + +@pytest.mark.parametrize('size', [381, 3000]) +def test_large_catalog_local_filter_and_selection_remain_responsive(qtbot, tmp_path, monkeypatch, size): + records = tuple(CbbRecord(f'CBB{i:04}', 'Power' if i % 2 else 'Analog', + status='READY' if i % 3 == 0 else 'PENDING') for i in range(size)) + start = time.perf_counter() + window = load_window(qtbot, tmp_path, monkeypatch, records=records) + loaded = time.perf_counter() - start + window.reader = lambda _: pytest.fail('local actions reread Mentor') + start = time.perf_counter() + window.search_edit.setText('power') + window.select_visible_button.click() + window.status_filter.setCurrentIndex(window.status_filter.findData('pending')) + window.clear_selection_button.click() + elapsed = time.perf_counter() - start + print(f'catalog rows={size} load={loaded:.3f}s local_actions={elapsed:.3f}s') + assert elapsed < 2.0 + assert window.selected_records() == () -- 2.39.5 From b90a9bbfab15c7ef3ee3375ea8bb80366f58110e 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: Mon, 2 Nov 2026 18:52:04 +0800 Subject: [PATCH 06/59] fix: allow explicit native repair owner process queries --- .../2026-11-02-issue-2-startup-recovery.md | 9 ++++ src/cbb_refresh_verify/mentor_adapter.py | 2 + tests/test_native_repair.py | 43 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/docs/releases/2026-11-02-issue-2-startup-recovery.md b/docs/releases/2026-11-02-issue-2-startup-recovery.md index 67b5ffd..65f13f6 100644 --- a/docs/releases/2026-11-02-issue-2-startup-recovery.md +++ b/docs/releases/2026-11-02-issue-2-startup-recovery.md @@ -155,3 +155,12 @@ Task 1 review fix round: 259 related tests passed (17.86s); compileall and `git diff --check` passed. These checks include busy retention, real readback failure branches, 30 malformed-success variants at both release and reopen/ counting boundaries, and a complete actual worker-result replay control. + +Runtime fix round 2 adds `iCDBProjectBackup.exe` and `ReuseBlockImporter.exe` to +the process helper's fixed query whitelist. Without these entries, the real +parent release probe rejected its own required queries and always denied safe +release. New tests keep the release validator and process helper real and stub +only WMI: no owners permits release, either native owner prevents it, and +arbitrary query names remain rejected. Focused validation: 172 passed. Candidate +`c823a4b` must be rebuilt with this correction before frozen/native validation; +field recovery remains unverified. diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index b877b7e..21fe87a 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -220,6 +220,8 @@ def _process_details(name): queries = { 'ExpeditionPCB.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='ExpeditionPCB.exe'", 'LibraryManager.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='LibraryManager.exe'", + 'iCDBProjectBackup.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='iCDBProjectBackup.exe'", + 'ReuseBlockImporter.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='ReuseBlockImporter.exe'", 'cscript.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='cscript.exe'", 'viewdraw.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='viewdraw.exe'", } diff --git a/tests/test_native_repair.py b/tests/test_native_repair.py index 5485477..af34a42 100644 --- a/tests/test_native_repair.py +++ b/tests/test_native_repair.py @@ -811,3 +811,46 @@ def test_malformed_native_success_never_reopens_or_counts_repaired(tmp_path, mon assert result.recovery_attempts[0]['status'] == 'UNCONFIRMED' assert result.recovery_attempts[0]['release']['safe_to_continue'] is False assert maintenance_counts([result])['repaired'] == 0 + + +@pytest.mark.parametrize('owner', [None, 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe']) +def test_repair_release_uses_real_process_query_contract(tmp_path, monkeypatch, owner): + from types import SimpleNamespace + import win32com.client + from cbb_refresh_verify.native_repair import repair_release_state + request, diagnosis, target = target_fixture(tmp_path) + queried = [] + names = ('ExpeditionPCB.exe', 'LibraryManager.exe', 'iCDBProjectBackup.exe', + 'ReuseBlockImporter.exe', 'cscript.exe') + queries = {"SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine " + f"FROM Win32_Process WHERE Name='{name}'": name for name in names} + def query(wql): + name = queries[wql] + queried.append(name) + return ([SimpleNamespace(ProcessId='456', ParentProcessId='1', + ExecutablePath=f'C:\\Mentor\\{name}', CommandLine=f'{name} target')] + if name == owner else []) + def service(moniker): + assert moniker == 'winmgmts:' + return SimpleNamespace(ExecQuery=query) + monkeypatch.setattr(win32com.client, 'GetObject', service) + # Keep pcb_processes, _processes, _process_details and release validation real. + state = repair_release_state(request, target, complete_native_success(target)) + assert 'error' not in state + assert queried == list(names) + assert state['safe_to_continue'] is (owner is None) + assert state['remaining_pids'] == ([] if owner is None else [456]) + assert state['database_exclusive'] is (owner is None) + + +@pytest.mark.parametrize('name', [ + 'other.exe', 'icdbprojectbackup.exe', + "iCDBProjectBackup.exe' OR Name='other.exe", + "ReuseBlockImporter.exe' OR Name='other.exe", +]) +def test_repair_process_queries_still_reject_arbitrary_input(monkeypatch, name): + import win32com.client + from cbb_refresh_verify.mentor_adapter import _process_details + monkeypatch.setattr(win32com.client, 'GetObject', lambda *a: pytest.fail('Unsupported name reached WMI')) + with pytest.raises(ValueError, match='Unsupported process query'): + _process_details(name) -- 2.39.5 From 877e3b4c76b571f6288c2f7bf86cf8e9ffada3d1 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: Mon, 2 Nov 2026 19:12:12 +0800 Subject: [PATCH 07/59] fix: wait for native repair project shell and ignore known input indicators --- .../2026-11-02-issue-2-startup-recovery.md | 16 +++ src/cbb_refresh_verify/native_repair_ui.py | 14 ++- ...icdb-repair-startup-windows-sanitized.json | 26 +++++ tests/test_native_repair.py | 102 ++++++++++++++++++ 4 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/icdb-repair-startup-windows-sanitized.json diff --git a/docs/releases/2026-11-02-issue-2-startup-recovery.md b/docs/releases/2026-11-02-issue-2-startup-recovery.md index 65f13f6..59c96a2 100644 --- a/docs/releases/2026-11-02-issue-2-startup-recovery.md +++ b/docs/releases/2026-11-02-issue-2-startup-recovery.md @@ -164,3 +164,19 @@ only WMI: no owners permits release, either native owner prevents it, and arbitrary query names remain rejected. Focused validation: 172 passed. Candidate `c823a4b` must be rebuilt with this correction before frozen/native validation; field recovery remains unverified. + +Runtime fix round 3 uses the actual frozen-worker window capture. A single Afx +shell titled exactly `iCDB Project Backup` now waits, before invocation only, +for the exact project title within the existing deadline. Two owned-process +input-indicator classes (`UAC_InputIndicatorOverlayWnd`, `UAC Input Indicator`) +are ignored only with empty titles. Wrong/duplicate target windows, ambiguous +shells, named/lookalike auxiliary windows, unknown prompts, missing Output and +busy verdicts still retain the tool without unsafe commands. The native loading +line `Checking if project is in use.` is excluded from busy evidence; the actual +ownership verdict continues to control the busy gate. + +Sanitized recorded-shape replay includes real shell children, RichEdit Output +readback, startup-to-loaded transition and both input indicators before and +after load. Focused validation: 189 passed (12.40s); compile and scoped diff +checks passed. New source/frozen native validation and rebuilding the candidate +remain coordinator work; these replay results do not establish field recovery. diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index 1b4339d..c219b2d 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -34,6 +34,8 @@ def repair_completed(diagnostic_text, database_text, *, native_backup): def classify_repair_evidence(diagnostic, database, restore, output): """Classify this invocation only; an error alone does not prove native idle.""" combined = '\n'.join((diagnostic, database, restore, output)) + # This observed loading-progress line precedes the actual ownership verdict. + combined = re.sub(r'(?im)^\s*(?:\[[^\]\r\n]+\]\s*)?Checking if project is in use\.\s*$', '', combined) terminal_error = bool(re.search(r'Repair project error\s*\[[^\r\n]+\]', output, re.I)) closed_error = ('Diagnostic: FINISH' in diagnostic and 'IC: Closing project' in database) if re.search(r'project is in use|database.*(?:in use|busy)|other clients are connected', combined, re.I): @@ -225,12 +227,20 @@ def repair_worker_main(job_path): output_baseline = None output = '' while process.poll() is None and time.monotonic()= 4 and polls[2] >= .4 + assert result['status'] == expected + assert actions[:2] == [(100, win32con.WM_COMMAND), (201, win32con.BM_CLICK)] + assert ((100, win32con.WM_CLOSE) in actions) is closed + assert result['release']['safe_to_continue'] is closed + assert 'Project is not in use' in json.loads((root/'native-output.json').read_text('utf-8'))['baseline'] + + +@pytest.mark.parametrize('state,expected', [ + ('shell_stuck', 'TIMEOUT_UNRELEASED'), ('wrong_target', 'UNKNOWN_DIALOG'), + ('duplicate_shell', 'UNKNOWN_DIALOG'), ('duplicate_target', 'UNKNOWN_DIALOG'), + ('named_aux', 'UNKNOWN_DIALOG'), ('lookalike_aux', 'UNKNOWN_DIALOG'), + ('missing_output', 'TIMEOUT_UNRELEASED'), ('busy_loading', 'DATABASE_BUSY'), +]) +def test_recorded_startup_shape_retains_unconfirmed_or_unknown_state(tmp_path, monkeypatch, state, expected): + ui, root, job, actions, polls = real_shape_repair_fixture(tmp_path, monkeypatch, 'failed', state) + assert ui.repair_worker_main(job) == 1 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['status'] == expected + assert actions == [] + assert result['release']['safe_to_continue'] is False + + +def test_generic_shell_after_invocation_is_not_a_startup_exception(tmp_path, monkeypatch): + import win32con + ui, root, job, actions, polls = real_shape_repair_fixture(tmp_path, monkeypatch, 'failed', 'shell_after_invoke') + assert ui.repair_worker_main(job) == 1 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['status'] == 'UNKNOWN_DIALOG' + assert actions == [(100, win32con.WM_COMMAND)] + assert result['release']['safe_to_continue'] is False + + +@pytest.mark.parametrize('verdict,expected', [ + ('', 'UNCONFIRMED'), + ("Project is not in use. Full iCDB Project Backup's functionality is available.", 'UNCONFIRMED'), + ('Project is in use. Full functionality is unavailable.', 'DATABASE_BUSY'), +]) +def test_native_usage_check_progress_waits_for_actual_busy_verdict(verdict, expected): + from cbb_refresh_verify.native_repair_ui import classify_repair_evidence + output = '[2026-11-02 12:00:00] Checking if project is in use.\r\n' + output += '[2026-11-02 12:00:00] '+verdict + assert classify_repair_evidence('', '', '', output)['outcome'] == expected -- 2.39.5 From 29c154d2ca0d3b8b384e7cc38a298590bbc70848 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: Mon, 2 Nov 2026 19:32:07 +0800 Subject: [PATCH 08/59] feat: scope recovery manifests while verifying complete backups --- .../task-3-report.md | 187 ++++++++++++ src/cbb_refresh_verify/backup_recovery.py | 135 ++------- src/cbb_refresh_verify/scoped_manifest.py | 265 ++++++++++++++++++ tests/test_backup_recovery.py | 92 ++++++ tests/test_scoped_manifest.py | 215 ++++++++++++++ 5 files changed, 789 insertions(+), 105 deletions(-) create mode 100644 .superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md create mode 100644 src/cbb_refresh_verify/scoped_manifest.py create mode 100644 tests/test_scoped_manifest.py diff --git a/.superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md b/.superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md new file mode 100644 index 0000000..1c03415 --- /dev/null +++ b/.superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md @@ -0,0 +1,187 @@ +# Task 3 report: scoped primary manifests and complete verified backups + +Status: implementation and verification complete. Started on b90a9bb; paused at +the RED checkpoint for the coordinator's native startup-window repair fix and +resumed on 877e3b4. No native UI/process/live-library actions were performed by +this implementer. Original untracked review.diff was preserved and not staged. + +## Result and scope + +Complete backup trees are still copied and retained. SHA256 and semantic +before/after comparisons now cover only primary design/project/database/ +constraint files and required central-library dependencies. Runtime changes +after a successful snapshot do not stop install. Primary content changes, +additions and deletions do stop install, with copies retained as evidence. + +The validate-copy -> conflict checks -> retain original Work -> install -> +reopen/native verification flow is preserved. Source equality now runs after +incoming Work preparation, immediately before target equality, closing the old +late-source-change window. Source idle/address ownership is rechecked there as +well: excluding address bytes from semantic hashes does not bypass idle checks. + +Scoped production files: backup_recovery.py and new scoped_manifest.py. Scoped +tests: test_backup_recovery.py and new test_scoped_manifest.py. No Verify public +types, Task 2 helpers/READY logic, native repair classifiers/process whitelist, +or library binary contents were changed. + +## Exact classification rules + +Project scope retains every unclassified file as primary. This includes PRJ, +PCB, DXP, icdb.dat, Layout databases, schematic/logic/symbol assets, CES/config, +unknown binary/engineering formats, and text files outside explicit excluded +categories. Rules are case-insensitive; manifests preserve relative path case. + +Explicit semantic exclusions, still copied and metadata-validated: + +- Directory components: log, logs, temp, tmp, __pycache__, backup, backups, + output, outputs, report, reports, gerber, gerbers, odb++, and historical + Work.before-restore-* directory components. +- File suffixes: .log, .tmp, .temp, .lck, .lock, .bak, .backup, .pdf. +- File names: sAddress.adr and editor files starting with ~$. + +The uploader-compatible ..cbb-upload.lock falls under .lock, so it is +runtime and never primary. It is **not** silently removed from complete copies. + +Central-library scope includes only target ReusableBlocksLP//Work and +Cert, root configuration files (the selected .lmc, other root config files), +and shared/configuration dependency directories outside ReusableBlocksLP. Known +examples include SymbolLibs/__ReusableBlocksSymb, CellDB and PartDB; new unknown +dependency directory names remain primary conservatively. Other root .lmc files, +other ReusableBlocksLP CBBs, target historical/non-Work/non-Cert trees and the +explicit runtime/generated categories above do not enter semantic equality. +Those unrelated files still enter the complete copy and its metadata checks. + +Later semantic scans prune ignored/runtime/unrelated CBB directories before +entering them, so transient/unavailable runtime or unrelated trees cannot +interfere with the post-snapshot content check. + +## Reusable API contract for Task 4 + +Import from cbb_refresh_verify.scoped_manifest: + +```python +scope = ManifestScope.library(block_name, selected_lmc.name) +before = copy_verified(library_root, private_backup, scope=scope, progress=callback) +current = build_manifest(library_root, scope=scope, exclude=private_incoming) +if current != before: + # stop install; retain evidence + ... +``` + +- `ManifestScope()` is project scope. `ManifestScope.library(block_name, + library_name)` requires nonempty single-component names; no path traversal. +- `build_manifest(root, *, scope=None, exclude=None, progress=None, + hash_function=hash_file)` returns a semantic manifest. `exclude` is an optional + private incoming subtree, **not** a copy exclusion. Hash injection is a test + hook; normal callers use the default. +- `copy_verified(source, destination, *, scope=None, progress=None, + hash_function=hash_file)` requires a fresh destination outside source. Copies + all files/directories, returns the same semantic schema, retains partial + destination on failure, and raises ValueError/OSError for unsafe/changed data. +- `scan_tree(root)` scans complete membership and refuses links/junctions and + special files. Its optional scope/exclude is used by semantic scans only; + complete copies always scan without filters. +- `hash_file(path)` streams SHA256 in 1 MiB chunks. Files are processed with up + to eight workers, retaining the existing concurrency bound. +- Progress callback accepts **one dict**, not the old private `(count,total)` + pair: `stage` (`copy` or `hash`), `hashed_files`, `hashed_bytes`, `total_files`, + plus `copied_files` for copy. Hash counts/bytes count unique primary snapshot + files/logical bytes, not aggregate disk IO. `total_files` counts all files for + copies and primary files for standalone manifest generation. Initial zero and + each 256/final-file event are emitted. Recovery keeps existing public progress + stage/message keys and adds `manifest_stage` and these counters. + +Version 1 JSON-compatible output (relative paths are POSIX, no machine-specific +absolute paths or mtimes in equality): + +```json +{ + "version": 1, + "scope": {"kind": "library", "block_name": "A", "library_name": "target.lmc"}, + "files": { + "ReusableBlocksLP/A/Work/A.prj": { + "size": 123, "sha256": "", "scope": "target_work" + } + } +} +``` + +Entry scope labels are project, target_work, target_cert, library_config and +shared_dependency. Default project's descriptor uses empty block/library names. +Recovery persists both library-before-manifest.json and +source-before-manifest.json. The shape changed only for private backup helpers; +VerifyRequest/VerifyResult contracts are unchanged. There is no old-manifest +loader or destructive migration: existing historical evidence remains intact. + +## Copy integrity and old strict equality compatibility + +Copy validation remains strict for the **entire** snapshot: before/after source +file/directory membership, file type, size and nanosecond mtime must agree; +destination membership/type/size/mtime must match too. Primary regular source +files are SHA256 read before copy and again for validation; destination hashes +must match. All native icdb.dat and .lmc files retain exclusive read handles +during copying. For scoped primary native files, the expected digest is built +from the source ReadFile stream independently of destination SHA256. Identical +size/mtime destination corruption is therefore rejected, including locked DBs. +Unrelated native DBs are still locked/copied; their bytes are not SHA256 hashed. + +The old final `_manifest(source) == complete_copy_record` was an all-file hash +comparison that rehashed every runtime and unrelated project file. It is +replaced by full-tree copy metadata validation and primary byte verification at +snapshot time. Later equality compares version/scope and primary relative +path/size/hash/scope only. Thus log-only additions/changes/deletions after a +successful copy are accepted, while primary keyset/content changes remain real +conflicts. No conflict tests were weakened or deleted; additional source, +target, Cert, shared dependency, late-source and active-address regressions were +added. Empty directory metadata is intentionally not part of semantic equality. + +## RED/GREEN and verification evidence + +1. Initial RED, before production edits: + `python -m pytest tests/test_scoped_manifest.py tests/test_backup_recovery.py -q` + -> **29 failed, 26 passed in 7.89s**. Failures demonstrated missing versioned + manifest/schema/progress and post-snapshot runtime/unrelated tolerance. + Copy/conflict protections already present continued passing. +2. Initial GREEN -> **55 passed in 11.93s**. +3. Additional RED for semantic traversal of an unavailable ignored/unrelated + tree -> **1 failed, 36 passed in 2.79s**; fixed by pruning semantic scans. +4. Additional RED for source mutation during incoming Work preparation and + source address reappearance -> **2 failed, 20 deselected in 1.56s**; both had + incorrectly installed before the final-source-check changes. +5. Final focused: + `python -m pytest tests/test_scoped_manifest.py tests/test_backup_recovery.py tests/test_backup_recovery_dialog.py -q` + -> **64 passed in 9.93s**. +6. Coordinator was notified **before** the full suite and explicitly confirmed + no native owner/mutex and paused all native probes. Exactly one final suite: + `python -m pytest -q` -> **693 passed, 1 warning in 47.99s**. Warning: + pywinauto's existing "Revert to STA COM threading mode" warning in + test_library_reader.py::test_status_log_records_unseen_id_inference. +7. `git diff --check` passed. Review confirmed no unrelated/native-helper edits. + +Coverage includes every primary category, unknown engineering formats, +runtime/history/generated exclusions with complete-copy retention, hash-path +scope, relocatability, source changed/deleted/added during copy/validation, +target changes/deletions/additions, Cert/shared-library conflicts, unrelated +CBB tolerance, copied corruption with preserved metadata, locked stream bytes, +source content changes with preserved metadata, complete copy membership and +nonprimary size consistency, idle/address rechecks, retained copies on failure, +and unchanged native reopen/retained-Work recovery behavior. + +## Limitations / follow-up boundary + +- No real field lost-session reproduction, native GUI success reproduction, or + Windows 10 run was performed for this task; file-based tests do not substitute + for those required native/field validations. +- Task 4 must adapt complete-copy access to a live uploader lock **only when the + operation actually owns that coordination lock**. The current helper copies + readable locks and fails closed on unreadable/live exclusively owned files; + it does not delete locks or broadly drop engineering data. +- Native idle guards and later coordination locks remain necessary. Final + equality and filesystem rename are not one cross-process atomic transaction + against arbitrary noncooperating external writers. +- Nonprimary byte hashes are intentionally absent. Their copy integrity uses + complete membership and size/mtime checks, as specified; same-size, + timestamp-preserving nonprimary corruption is outside the semantic guarantee. +- All snapshots must be stable while copied, including runtime files. This + deliberately differs from post-snapshot semantic checks, which tolerate + runtime changes. Original and partial recovery evidence is retained on failure. diff --git a/src/cbb_refresh_verify/backup_recovery.py b/src/cbb_refresh_verify/backup_recovery.py index 9b434e2..5a5e38a 100644 --- a/src/cbb_refresh_verify/backup_recovery.py +++ b/src/cbb_refresh_verify/backup_recovery.py @@ -3,12 +3,8 @@ This restores project data for an existing library entry. It is not a native cross-library import and does not delete library records or shared symbols. """ -from contextlib import ExitStack -from concurrent.futures import ThreadPoolExecutor from dataclasses import asdict, replace -import hashlib import json -import os from pathlib import Path import re import shutil @@ -16,105 +12,22 @@ import stat from uuid import uuid4 from .verify_runner import VerifyResult, preflight +from .scoped_manifest import (ManifestScope, build_manifest, copy_verified, + hash_file as _hash, scan_tree as _scan) -def _hash(path): - digest = hashlib.sha256() - with path.open('rb') as stream: - for data in iter(lambda: stream.read(1024 * 1024), b''): - digest.update(data) - return digest.hexdigest() +def _manifest(root, *, scope=None, exclude=None, progress=None): + return build_manifest(root, scope=scope, exclude=exclude, progress=progress, hash_function=_hash) -def _scan(root): - if root.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: - raise ValueError(f'不支持通过目录链接恢复:{root}') - entries = {} - folders = [root] - while folders: - with os.scandir(folders.pop()) as scanned: - for entry in scanned: - metadata = entry.stat(follow_symlinks=False) - if metadata.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: - raise ValueError(f'目录含文件或目录链接,停止恢复:{entry.path}') - path = Path(entry.path) - entries[path] = metadata - if stat.S_ISDIR(metadata.st_mode): - folders.append(path) - return dict(sorted(entries.items())) +def _copy_verified(source, destination, *, scope=None, progress=None): + return copy_verified(source, destination, scope=scope, progress=progress, hash_function=_hash) def _paths(root): return list(_scan(root)) -def _manifest(root, *, exclude=None): - entries = _scan(root) - paths = [path for path in entries - if exclude is None or not path.is_relative_to(exclude)] - def entry(path): - return str(path.relative_to(root)), _hash(path) if stat.S_ISREG(entries[path].st_mode) else None - with ThreadPoolExecutor(max_workers=8) as pool: - return dict(pool.map(entry, paths)) - - -def _copy_verified(source, destination, *, progress=None): - """Copy every file, refusing links and changed files; lock native databases.""" - import win32con, win32file - entries = _scan(source) - paths = list(entries) - destination.mkdir(parents=True, exist_ok=False) - record = {} - with ExitStack() as stack: - locks = {} - for path in paths: - if stat.S_ISREG(entries[path].st_mode) and (path.name.casefold() == 'icdb.dat' or path.suffix.casefold() == '.lmc'): - handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, - win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) - stack.callback(handle.Close) - locks[path] = handle - files = [] - for path in paths: - relative = str(path.relative_to(source)) - copied = destination / relative - if stat.S_ISDIR(entries[path].st_mode): - copied.mkdir(parents=True, exist_ok=True) - record[relative] = None - continue - files.append(path) - def copy_file(path): - relative = str(path.relative_to(source)) - copied = destination / relative - before = entries[path] - copied.parent.mkdir(parents=True, exist_ok=True) - if path in locks: - with copied.open('wb') as stream: - while True: - _, data = win32file.ReadFile(locks[path], 1024 * 1024) - if not data: - break - stream.write(data) - shutil.copystat(path, copied) - digest = _hash(copied) - else: - digest = _hash(path) - shutil.copy2(path, copied) - if _hash(copied) != digest: - raise OSError(f'副本校验失败:{path}') - after = path.stat() - if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns): - raise OSError(f'复制期间文件变化:{path}') - return relative, digest - with ThreadPoolExecutor(max_workers=8) as pool: - for count, (relative, digest) in enumerate(pool.map(copy_file, files), 1): - record[relative] = digest - if progress and (count % 256 == 0 or count == len(files)): - progress(count, len(files)) - if _manifest(source) != record: - raise OSError(f'复制期间目录内容变化:{source}') - return record - - def _idle_check(library, project, pcb): from .mentor_adapter import pcb_processes, _process_details from .icdb_ownership import unreleased_addresses @@ -213,9 +126,15 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c record = {'operation': 'same_cbb_complete_project_restore', 'source': str(source), 'library': str(library), 'block': request.block_name, 'installed': False, 'validation_library': str(validation_dir/library.name)} - def emit(stage, message): + def emit(stage, message, **details): if progress: - progress({'stage': stage, 'block': request.block_name, 'message': message}) + progress({'stage': stage, 'block': request.block_name, 'message': message, **details}) + def copy_progress(stage, message): + def report(event): + details = {key: value for key, value in event.items() if key != 'stage'} + emit(stage, f'{message} {event["copied_files"]} / {event["total_files"]}', + manifest_stage=event['stage'], **details) + return report def save(result): record.update({'installed': installed, 'result': asdict(result)}) root.mkdir(parents=True, exist_ok=True) @@ -245,13 +164,16 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c raise ValueError('磁盘空间不足:需要容纳中心库回退副本和验证副本。') (root/'recovery-request.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8') emit('backup', '正在保留完整中心库副本…') - original_manifest = _copy_verified(library_root, root/'library-before', progress=lambda n, total: - emit('backup', f'正在保留中心库副本:{n} / {total} 个文件…')) + library_scope = ManifestScope.library(request.block_name, library.name) + original_manifest = _copy_verified(library_root, root/'library-before', scope=library_scope, + progress=copy_progress('backup', '正在保留中心库副本:')) (root/'library-before-manifest.json').write_text(json.dumps(original_manifest, indent=2), encoding='utf-8') emit('prepare_source', '正在准备备份验证副本…') - source_manifest = _copy_verified(source.parent, root/'source-before') - _copy_verified(root/'library-before', validation_dir, progress=lambda n, total: - emit('prepare_source', f'正在准备验证库:{n} / {total} 个文件…')) + source_manifest = _copy_verified(source.parent, root/'source-before', + progress=copy_progress('prepare_source', '正在保留备份工程副本:')) + (root/'source-before-manifest.json').write_text(json.dumps(source_manifest, indent=2), encoding='utf-8') + _copy_verified(root/'library-before', validation_dir, scope=library_scope, + progress=copy_progress('prepare_source', '正在准备验证库:')) staged_library = validation_dir/library.name staged_work = staged_library.parent/'ReusableBlocksLP'/request.block_name/'Work' # Both paths are newly created private copies; originals are retained. @@ -259,7 +181,8 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c if not resolved_work.is_relative_to(validation_dir.resolve()): raise ValueError('验证副本工程路径越界。') shutil.rmtree(resolved_work) - _copy_verified(root/'source-before', staged_work) + _copy_verified(root/'source-before', staged_work, + progress=copy_progress('prepare_source', '正在准备备份验证工程:')) staged_project = staged_work/source.name staged_pcb = staged_work/source_pcb.relative_to(source.parent) _align_library(staged_project, staged_pcb, staged_library) @@ -277,15 +200,17 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c '备份校验未通过,目标未更改。\n' + (source_result.stderr or source_result.library_flag_detail))) idle_check(library, project, pcb) idle_check(staged_library, staged_project, staged_pcb) - if _manifest(source.parent) != source_manifest: - raise ValueError('备份来源在验证期间发生变化,目标未更改。') token = root.name.removeprefix('backup-restore-') work = project.parent.resolve() incoming = work.with_name('Work.restore-' + token) retained = work.with_name('Work.before-restore-' + token) - _copy_verified(staged_work, incoming) + _copy_verified(staged_work, incoming, + progress=copy_progress('prepare_source', '正在准备恢复工程:')) _align_library(incoming/source.name, incoming/source_pcb.relative_to(source.parent), library) - if _manifest(library_root, exclude=incoming.resolve()) != original_manifest: + idle_check(library, source, source_pcb) + if _manifest(source.parent) != source_manifest: + raise ValueError('备份来源在验证期间发生变化,目标未更改。') + if _manifest(library_root, scope=library_scope, exclude=incoming.resolve()) != original_manifest: raise ValueError('目标中心库在验证期间发生变化,停止恢复。') emit('install', '备份已通过验证,正在保留原工程并恢复…') work.rename(retained) diff --git a/src/cbb_refresh_verify/scoped_manifest.py b/src/cbb_refresh_verify/scoped_manifest.py new file mode 100644 index 0000000..35a82ea --- /dev/null +++ b/src/cbb_refresh_verify/scoped_manifest.py @@ -0,0 +1,265 @@ +"""Complete, metadata-verified snapshots with scoped semantic SHA256 manifests. + +Unknown engineering files are primary. Only explicit runtime, history and +generated categories are ignored by semantic comparisons; they are still copied +and their membership/size/mtime must remain stable throughout the snapshot. +""" +from concurrent.futures import ThreadPoolExecutor +from contextlib import ExitStack +from dataclasses import asdict, dataclass +import hashlib +import os +from pathlib import Path +import shutil +import stat + + +MANIFEST_VERSION = 1 +_NONPRIMARY_DIRECTORIES = frozenset({ + 'log', 'logs', 'temp', 'tmp', '__pycache__', 'backup', 'backups', + 'output', 'outputs', 'report', 'reports', 'gerber', 'gerbers', 'odb++', +}) +_NONPRIMARY_SUFFIXES = frozenset({ + '.log', '.tmp', '.temp', '.lck', '.lock', '.bak', '.backup', '.pdf', +}) + + +@dataclass(frozen=True) +class ManifestScope: + """Project root by default; library selects one CBB and shared dependencies.""" + + kind: str = 'project' + block_name: str = '' + library_name: str = '' + + def __post_init__(self): + if self.kind not in ('project', 'library'): + raise ValueError('Unknown manifest scope') + if self.kind == 'library': + for value in (self.block_name, self.library_name): + if not value or value in ('.', '..') or any(c in value for c in '/\\:'): + raise ValueError('Library scope requires single-component block/library names') + + @classmethod + def library(cls, block_name, library_name): + return cls('library', block_name, library_name) + + def includes_directory(self, relative): + parts = tuple(part.casefold() for part in Path(relative).parts) + if any(part in _NONPRIMARY_DIRECTORIES or part.startswith('work.before-restore-') + for part in parts): + return False + if self.kind == 'project' or parts[0] != 'reusableblockslp': + return True + return ((len(parts) < 2 or parts[1] == self.block_name.casefold()) + and (len(parts) < 3 or parts[2] in ('work', 'cert'))) + + def classify(self, relative): + """Return a semantic scope label or None; paths are relative to the root.""" + parts = tuple(part.casefold() for part in Path(relative).parts) + if (any(part in _NONPRIMARY_DIRECTORIES or part.startswith('work.before-restore-') + for part in parts[:-1]) + or Path(parts[-1]).suffix in _NONPRIMARY_SUFFIXES + or parts[-1] == 'saddress.adr' or parts[-1].startswith('~$')): + return None + if self.kind == 'project': + return 'project' + if parts[0] == 'reusableblockslp': + if len(parts) < 4 or parts[1] != self.block_name.casefold(): + return None + return {'work': 'target_work', 'cert': 'target_cert'}.get(parts[2]) + if len(parts) == 1: + if Path(parts[0]).suffix == '.lmc' and parts[0] != self.library_name.casefold(): + return None + return 'library_config' + # Shared DB/config/dependency trees are conservative: new dependency + # directory names remain primary without adding them to an allowlist. + return 'shared_dependency' + + +def hash_file(path): + digest = hashlib.sha256() + with Path(path).open('rb') as stream: + for data in iter(lambda: stream.read(1024 * 1024), b''): + digest.update(data) + return digest.hexdigest() + + +def scan_tree(root, *, scope=None, exclude=None): + """Refuse links/special files; optionally prune trees outside semantic scope. + + Copies always call this without a scope, validating complete membership. + Later semantic scans avoid entering runtime or unrelated CBB directories. + """ + root = Path(root) + if root.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: + raise ValueError(f'不支持通过目录链接恢复:{root}') + entries = {} + folders = [root] + while folders: + with os.scandir(folders.pop()) as scanned: + for entry in scanned: + path = Path(entry.path) + if exclude is not None and path.is_relative_to(exclude): + continue + if scope is not None: + relative = path.relative_to(root) + if entry.is_dir(follow_symlinks=False): + if not scope.includes_directory(relative): + continue + elif scope.classify(relative) is None: + continue + metadata = entry.stat(follow_symlinks=False) + if metadata.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: + raise ValueError(f'目录含文件或目录链接,停止恢复:{entry.path}') + if not (stat.S_ISDIR(metadata.st_mode) or stat.S_ISREG(metadata.st_mode)): + raise ValueError(f'不支持的特殊文件:{entry.path}') + entries[path] = metadata + if stat.S_ISDIR(metadata.st_mode): + folders.append(path) + return dict(sorted(entries.items())) + + +def _metadata_record(root, entries): + return {path.relative_to(root).as_posix(): + ('file', info.st_size, info.st_mtime_ns) if stat.S_ISREG(info.st_mode) else ('dir',) + for path, info in entries.items()} + + +def _manifest(scope, files): + return {'version': MANIFEST_VERSION, 'scope': asdict(scope), 'files': dict(sorted(files.items()))} + + +def _emit(progress, stage, hashed_files, hashed_bytes, **extra): + if progress: + progress({'stage': stage, 'hashed_files': hashed_files, 'hashed_bytes': hashed_bytes, **extra}) + + +def build_manifest(root, *, scope=None, exclude=None, progress=None, hash_function=hash_file): + """Hash primary files only. Excluded subtrees are for private incoming Work. + + The returned dict is versioned, JSON-compatible and relocatable. Equality + compares primary path membership, size, hash and scope, never runtime mtimes. + """ + root = Path(root) + scope = scope or ManifestScope() + entries = scan_tree(root, scope=scope, exclude=exclude) + selected = {path: scope.classify(path.relative_to(root)) for path, info in entries.items() + if stat.S_ISREG(info.st_mode)} + selected = {path: label for path, label in selected.items() if label is not None} + def hashed(path): + digest = hash_function(path) + after = path.stat() + before = entries[path] + if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns): + raise OSError(f'计算哈希期间文件变化:{path}') + return path.relative_to(root).as_posix(), { + 'size': before.st_size, 'sha256': digest, 'scope': selected[path]} + files = {} + hashed_bytes = 0 + _emit(progress, 'hash', 0, 0, total_files=len(selected)) + with ThreadPoolExecutor(max_workers=8) as pool: + for count, (relative, item) in enumerate(pool.map(hashed, selected), 1): + files[relative] = item + hashed_bytes += item['size'] + if count % 256 == 0 or count == len(selected): + _emit(progress, 'hash', count, hashed_bytes, total_files=len(selected)) + # Check primary membership and metadata again after every hash completed. + # Runtime files can change/add/disappear without a semantic conflict. + after = scan_tree(root, scope=scope, exclude=exclude) + before_primary = {p: entries[p] for p in selected} + after_primary = {p: info for p, info in after.items() if stat.S_ISREG(info.st_mode) + and (exclude is None or not p.is_relative_to(exclude)) + and scope.classify(p.relative_to(root)) is not None} + if _metadata_record(root, before_primary) != _metadata_record(root, after_primary): + raise OSError(f'计算哈希期间主要文件变化:{root}') + return _manifest(scope, files) + + +def copy_verified(source, destination, *, scope=None, progress=None, hash_function=hash_file): + """Copy the whole tree, validate all metadata, SHA256 only scoped primary files. + + Native icdb.dat/LMC files are held with exclusive read handles for the copy. + Their expected SHA256 comes from the source read stream, independently of + hashing destination bytes. Failures retain the partial destination evidence. + Progress receives one dict with stage, hashed_files/bytes and copied_files. + """ + import win32con + import win32file + + source, destination = Path(source), Path(destination) + scope = scope or ManifestScope() + if destination.resolve().is_relative_to(source.resolve()): + raise ValueError('复制目标不能位于源目录内。') + entries = scan_tree(source) + expected_metadata = _metadata_record(source, entries) + destination.mkdir(parents=True, exist_ok=False) + files = [path for path, info in entries.items() if stat.S_ISREG(info.st_mode)] + record = {} + hashed_count = hashed_bytes = 0 + _emit(progress, 'copy', 0, 0, copied_files=0, total_files=len(files)) + with ExitStack() as stack: + locks = {} + for path in files: + if path.name.casefold() == 'icdb.dat' or path.suffix.casefold() == '.lmc': + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + stack.callback(handle.Close) + locks[path] = handle + for path, info in entries.items(): + if stat.S_ISDIR(info.st_mode): + (destination/path.relative_to(source)).mkdir(parents=True, exist_ok=True) + + def copy_file(path): + relative = path.relative_to(source) + copied = destination/relative + copied.parent.mkdir(parents=True, exist_ok=True) + label = scope.classify(relative) + digest = None + if path in locks: + stream_hash = hashlib.sha256() if label is not None else None + with copied.open('wb') as stream: + while True: + _, data = win32file.ReadFile(locks[path], 1024 * 1024) + if not data: + break + if stream_hash is not None: + stream_hash.update(data) + stream.write(data) + shutil.copystat(path, copied) + if stream_hash is not None: + digest = stream_hash.hexdigest() + else: + if label is not None: + digest = hash_function(path) + shutil.copy2(path, copied) + before, after = entries[path], path.stat() + if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns): + raise OSError(f'复制期间文件变化:{path}') + return relative.as_posix(), digest, label + + with ThreadPoolExecutor(max_workers=8) as pool: + for count, (relative, digest, label) in enumerate(pool.map(copy_file, files), 1): + if label is not None: + size = entries[source/relative].st_size + record[relative] = {'size': size, 'sha256': digest, 'scope': label} + hashed_count += 1 + hashed_bytes += size + if count % 256 == 0 or count == len(files): + _emit(progress, 'copy', hashed_count, hashed_bytes, + copied_files=count, total_files=len(files)) + + def validate_bytes(relative): + item = record[relative] + path = source/relative + if path not in locks and hash_function(path) != item['sha256']: + raise OSError(f'复制期间主要文件内容变化:{path}') + if hash_function(destination/relative) != item['sha256']: + raise OSError(f'复制内容校验失败:{path}') + with ThreadPoolExecutor(max_workers=8) as pool: + list(pool.map(validate_bytes, record)) + if _metadata_record(source, scan_tree(source)) != expected_metadata: + raise OSError(f'复制期间目录内容变化:{source}') + if _metadata_record(destination, scan_tree(destination)) != expected_metadata: + raise OSError(f'复制目标目录或文件元数据不一致:{destination}') + return _manifest(scope, record) diff --git a/tests/test_backup_recovery.py b/tests/test_backup_recovery.py index ca78f41..0d79ee9 100644 --- a/tests/test_backup_recovery.py +++ b/tests/test_backup_recovery.py @@ -188,3 +188,95 @@ def test_equivalent_library_reference_preserves_native_project_bytes_and_date(re before = original.read_bytes(), original.stat().st_mtime_ns _align_library(original, original.parent/'PCB/A.pcb', request.lmc_file) assert (original.read_bytes(), original.stat().st_mtime_ns) == before + + +def test_logs_and_unrelated_cbb_changes_after_snapshot_do_not_block_install(recovery_files): + from cbb_refresh_verify.backup_recovery import recover_from_backup + request, original, source = recovery_files + (source.parent/'session.log').write_bytes(b'snapshot log') + other = project(request.lmc_file.parent/'ReusableBlocksLP/B/Work', 'B', b'unrelated') + calls = [] + def verify(req, **kwargs): + calls.append(req) + if len(calls) == 1: + (source.parent/'session.log').write_bytes(b'updated source log') + (original.parent/'new.log').write_bytes(b'new target log') + (other.parent/'database/icdb.dat').write_bytes(b'unrelated update') + return VerifyResult('PASS', library_flag='GREEN') + result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None) + assert result.status == 'PASS' + assert len(calls) == 2 + assert (original.parent/'database/icdb.dat').read_bytes() == b'healthy' + assert (other.parent/'database/icdb.dat').read_bytes() == b'unrelated update' + assert list(request.evidence_dir.rglob('source-before/session.log'))[0].read_bytes() == b'snapshot log' + + +@pytest.mark.parametrize('location,change', [ + ('source', 'edit'), ('source', 'delete'), ('source', 'add'), + ('target', 'delete'), ('target', 'add'), ('cert', 'edit'), ('shared', 'edit'), +]) +def test_primary_conflicts_preserve_target_and_complete_recovery_copies(recovery_files, location, change): + from cbb_refresh_verify.backup_recovery import recover_from_backup + request, original, source = recovery_files + if location == 'source': + conflict = source.parent/'database/icdb.dat' + elif location == 'target': + conflict = original.parent/'PCB/Layout/LayoutDB.lyt' + elif location == 'cert': + conflict = original.parent.parent/'Cert/A.prj' + else: + conflict = request.lmc_file.parent/'SymbolLibs/__ReusableBlocksSymb/icdb.dat' + conflict.parent.mkdir(parents=True, exist_ok=True) + if location in ('cert', 'shared'): + conflict.write_bytes(b'before') + calls = [] + def verify(*args, **kwargs): + calls.append(args) + if change == 'delete': + conflict.unlink() + elif change == 'add': + (conflict.parent/'new-engineering.bin').write_bytes(b'new') + else: + conflict.write_bytes(b'concurrent edit') + return VerifyResult('PASS', library_flag='GREEN') + result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None) + assert result.status == 'BLOCKED' + assert len(calls) == 1 + assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged' + assert not list(original.parent.parent.glob('Work.before-restore-*')) + assert list(request.evidence_dir.rglob('library-before/target.lmc')) + assert list(request.evidence_dir.rglob('source-before/database/icdb.dat')) + + +def test_source_conflict_while_preparing_incoming_work_blocks_install(recovery_files, monkeypatch): + from cbb_refresh_verify import backup_recovery as recovery + request, original, source = recovery_files + copy = recovery._copy_verified + def copy_then_change_source(src, dst, **kwargs): + result = copy(src, dst, **kwargs) + if dst.name.startswith('Work.restore-'): + (source.parent/'database/icdb.dat').write_bytes(b'late concurrent change') + return result + monkeypatch.setattr(recovery, '_copy_verified', copy_then_change_source) + result = recovery.recover_from_backup(request, source, + verifier=lambda *a, **k: VerifyResult('PASS', library_flag='GREEN'), idle_check=lambda *_: None) + assert result.status == 'BLOCKED' + assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged' + + +def test_source_address_record_is_rechecked_after_validation(recovery_files): + from cbb_refresh_verify.backup_recovery import recover_from_backup + request, original, source = recovery_files + address = source.parent/'database/cdbsvr/sAddress.adr' + def idle(library, project, pcb): + if project == source and address.exists(): + raise ValueError('Source database ownership is still active') + def verify(*args, **kwargs): + address.parent.mkdir(parents=True, exist_ok=True) + address.write_bytes(b'live owner') + return VerifyResult('PASS', library_flag='GREEN') + result = recover_from_backup(request, source, verifier=verify, idle_check=idle) + assert result.status == 'BLOCKED' + assert 'ownership' in result.stderr + assert address.read_bytes() == b'live owner' + assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged' diff --git a/tests/test_scoped_manifest.py b/tests/test_scoped_manifest.py new file mode 100644 index 0000000..9d30080 --- /dev/null +++ b/tests/test_scoped_manifest.py @@ -0,0 +1,215 @@ +"""File-level regressions: complete snapshots, scoped semantic conflicts.""" +import hashlib +import os +from pathlib import Path +import shutil + +import pytest + +from cbb_refresh_verify import backup_recovery as recovery + + +def put(root, relative, data=b'engineering'): + path = root / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + return path + + +@pytest.mark.parametrize('relative', [ + 'A.prj', 'PCB/A.pcb', 'PCB/A.dxp', 'database/icdb.dat', + 'PCB/Layout/LayoutDB.lyt', 'schematic/sheet.1', 'logic/block.1', + 'symbols/device.sym', 'CES/constraints.ces', 'config/native.cfg', + 'unknown/new-engineering-format.bin', 'notes.txt', +]) +def test_primary_edits_additions_and_deletions_change_semantics(tmp_path, relative): + path = put(tmp_path, relative) + before = recovery._manifest(tmp_path) + assert before['version'] == 1 + entry = before['files'][relative] + assert entry == {'size': len(b'engineering'), + 'sha256': hashlib.sha256(b'engineering').hexdigest(), 'scope': 'project'} + path.write_bytes(b'changed') + assert recovery._manifest(tmp_path) != before + path.unlink() + assert recovery._manifest(tmp_path) != before + put(tmp_path, relative, b'engineering') + put(tmp_path, 'additional.dat') + assert recovery._manifest(tmp_path) != before + + +@pytest.mark.parametrize('relative', [ + 'logs/session.txt', 'log/native.log', 'session.log', 'temp/native.bin', + 'tmp/native.bin', 'session.tmp', 'database/icdb.lck', + 'database/cdbsvr/sAddress.adr', '.target.cbb-upload.lock', + 'backups/old/A.prj', 'backup/old/icdb.dat', 'A.prj.bak', + 'drawing.pdf', 'output/netlist.txt', 'reports/check.txt', +]) +def test_runtime_history_and_generated_content_is_copied_but_not_hashed(tmp_path, relative): + source = tmp_path/'source' + put(source, 'A.prj') + runtime = put(source, relative, b'old') + destination = tmp_path/'copy' + progress = [] + manifest = recovery._copy_verified(source, destination, progress=progress.append) + assert (destination/relative).read_bytes() == b'old' + assert manifest['version'] == 1 + assert set(manifest['files']) == {'A.prj'} + assert progress[-1]['hashed_files'] == 1 + assert progress[-1]['hashed_bytes'] == len(b'engineering') + assert progress[-1]['stage'] == 'copy' + runtime.write_bytes(b'new runtime content') + put(source, 'logs/new-session.log') + assert recovery._manifest(source) == manifest + runtime.unlink() + assert recovery._manifest(source) == manifest + + +def test_central_scope_hashes_target_work_cert_root_config_and_shared_dependencies(tmp_path): + from cbb_refresh_verify.scoped_manifest import ManifestScope + scope = ManifestScope.library('A', 'target.lmc') + expected = { + 'target.lmc': 'library_config', + 'library.xml': 'library_config', + 'ReusableBlocksLP/A/Work/A.prj': 'target_work', + 'ReusableBlocksLP/A/Cert/A.prj': 'target_cert', + 'SymbolLibs/__ReusableBlocksSymb/icdb.dat': 'shared_dependency', + 'CellDB/custom.cell': 'shared_dependency', + 'PartDB/parts.pdb': 'shared_dependency', + 'NewRequiredDependency/native.bin': 'shared_dependency', + } + for relative in expected: + put(tmp_path, relative) + unrelated = put(tmp_path, 'ReusableBlocksLP/B/Work/B.prj') + put(tmp_path, 'ReusableBlocksLP/A/Backups/old.prj') + put(tmp_path, 'other.lmc') + before = recovery._manifest(tmp_path, scope=scope) + assert before['scope'] == {'kind': 'library', 'block_name': 'A', 'library_name': 'target.lmc'} + assert {p: e['scope'] for p, e in before['files'].items()} == expected + unrelated.write_bytes(b'other project changed') + put(tmp_path, 'ReusableBlocksLP/C/Work/C.prj') + assert recovery._manifest(tmp_path, scope=scope) == before + for relative in expected: + path = tmp_path/relative + path.write_bytes(b'changed') + assert recovery._manifest(tmp_path, scope=scope) != before + path.write_bytes(b'engineering') + destination = tmp_path.parent/(tmp_path.name + '-copy') + assert recovery._copy_verified(tmp_path, destination, scope=scope) == before + assert (destination/'ReusableBlocksLP/B/Work/B.prj').read_bytes() == b'other project changed' + + +@pytest.mark.parametrize('relative', ['A.prj', 'database/icdb.dat', 'target.lmc']) +def test_corrupted_destination_bytes_are_rejected_and_evidence_is_preserved(tmp_path, monkeypatch, relative): + source = tmp_path/'source' + put(source, relative) + destination = tmp_path/'destination' + real_hash = recovery._hash + def corrupt_then_hash(path): + if path == destination/relative: + metadata = path.stat() + path.write_bytes(b'X' * metadata.st_size) + os.utime(path, ns=(metadata.st_atime_ns, metadata.st_mtime_ns)) + return real_hash(path) + monkeypatch.setattr(recovery, '_hash', corrupt_then_hash) + with pytest.raises(OSError): + recovery._copy_verified(source, destination) + assert (source/relative).read_bytes() == b'engineering' + assert (destination/relative).exists() + + +def test_source_byte_changes_with_preserved_metadata_are_rejected(tmp_path, monkeypatch): + source, destination = tmp_path/'source', tmp_path/'destination' + primary = put(source, 'A.prj') + real_copy = shutil.copy2 + def copy_then_change_source(src, dst, *args, **kwargs): + result = real_copy(src, dst, *args, **kwargs) + metadata = primary.stat() + primary.write_bytes(b'X' * metadata.st_size) + os.utime(primary, ns=(metadata.st_atime_ns, metadata.st_mtime_ns)) + return result + monkeypatch.setattr(shutil, 'copy2', copy_then_change_source) + with pytest.raises(OSError): + recovery._copy_verified(source, destination) + assert (destination/'A.prj').read_bytes() == b'engineering' + + +@pytest.mark.parametrize('change', ['changed', 'deleted', 'added']) +def test_complete_tree_metadata_must_stay_stable_during_copy(tmp_path, monkeypatch, change): + source = tmp_path/'source' + put(source, 'A.prj') + runtime = put(source, 'logs/session.log') + destination = tmp_path/'destination' + real_copy = shutil.copy2 + def copy_then_mutate(src, dst, *args, **kwargs): + result = real_copy(src, dst, *args, **kwargs) + if Path(src).name == 'A.prj': + if change == 'changed': + runtime.write_bytes(b'changed while copying') + elif change == 'deleted': + runtime.unlink() + else: + put(source, 'logs/added.log') + return result + monkeypatch.setattr(shutil, 'copy2', copy_then_mutate) + with pytest.raises(OSError): + recovery._copy_verified(source, destination) + assert destination.exists() + + +def test_destination_nonprimary_membership_and_size_are_validated(tmp_path, monkeypatch): + source = tmp_path/'source' + put(source, 'logs/session.log') + destination = tmp_path/'destination' + real_copy = shutil.copy2 + def truncated(src, dst, *args, **kwargs): + result = real_copy(src, dst, *args, **kwargs) + Path(dst).write_bytes(b'x') + return result + monkeypatch.setattr(shutil, 'copy2', truncated) + with pytest.raises(OSError): + recovery._copy_verified(source, destination) + + +def test_semantic_scan_does_not_visit_runtime_or_unrelated_cbb_trees(tmp_path, monkeypatch): + from cbb_refresh_verify.scoped_manifest import ManifestScope + scope = ManifestScope.library('A', 'target.lmc') + put(tmp_path, 'target.lmc') + put(tmp_path, 'ReusableBlocksLP/A/Work/A.prj') + put(tmp_path, 'ReusableBlocksLP/B/Work/B.prj') + put(tmp_path, 'logs/transient.log') + before = recovery._manifest(tmp_path, scope=scope) + real_scan = os.scandir + def runtime_unavailable(path): + # These trees may be actively changing after the successful snapshot. + if Path(path) in (tmp_path/'logs', tmp_path/'ReusableBlocksLP/B'): + raise OSError('transient runtime/unrelated tree is unavailable') + return real_scan(path) + monkeypatch.setattr(os, 'scandir', runtime_unavailable) + assert recovery._manifest(tmp_path, scope=scope) == before + + +def test_only_primary_paths_are_read_for_sha256_and_manifest_is_relocatable(tmp_path, monkeypatch): + from cbb_refresh_verify.scoped_manifest import ManifestScope + scope = ManifestScope.library('A', 'target.lmc') + source, destination = tmp_path/'source', tmp_path/'destination' + primary = {'target.lmc', 'ReusableBlocksLP/A/Work/A.prj', + 'SymbolLibs/shared/icdb.dat', 'config.xml'} + excluded = {'ReusableBlocksLP/B/Work/B.prj', 'logs/native.log', 'export.pdf', + '.target.cbb-upload.lock', 'backup/old/icdb.dat'} + for relative in primary | excluded: + put(source, relative) + real_hash = recovery._hash + hashed = [] + def record_hash(path): + root = source if path.is_relative_to(source) else destination + relative = path.relative_to(root).as_posix() + assert relative in primary, f'Nonprimary content hashed: {relative}' + hashed.append(relative) + return real_hash(path) + monkeypatch.setattr(recovery, '_hash', record_hash) + manifest = recovery._copy_verified(source, destination, scope=scope) + assert set(hashed) == primary + assert recovery._manifest(source, scope=scope) == manifest + assert recovery._manifest(destination, scope=scope) == manifest + assert {p.relative_to(destination).as_posix() for p in destination.rglob('*') if p.is_file()} == primary | excluded -- 2.39.5 From 3415e010ffa8233d0347848e81eda506587a350c 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: Mon, 2 Nov 2026 19:50:26 +0800 Subject: [PATCH 09/59] fix: preserve engineering identity in scoped runtime exclusions --- .../2026-11-02-scoped-recovery-manifests.md | 74 ++++++++++- src/cbb_refresh_verify/scoped_manifest.py | 60 ++++++++- tests/test_backup_recovery.py | 46 +++++++ tests/test_scoped_manifest.py | 120 ++++++++++++++++++ 4 files changed, 288 insertions(+), 12 deletions(-) rename .superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md => docs/releases/2026-11-02-scoped-recovery-manifests.md (70%) diff --git a/.superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md b/docs/releases/2026-11-02-scoped-recovery-manifests.md similarity index 70% rename from .superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md rename to docs/releases/2026-11-02-scoped-recovery-manifests.md index 1c03415..c899059 100644 --- a/.superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md +++ b/docs/releases/2026-11-02-scoped-recovery-manifests.md @@ -1,6 +1,6 @@ -# Task 3 report: scoped primary manifests and complete verified backups +# Scoped recovery manifests and complete verified backups -Status: implementation and verification complete. Started on b90a9bb; paused at +Status: implemented; review finding P1 addressed with focused validation. Started on b90a9bb; paused at the RED checkpoint for the coordinator's native startup-window repair fix and resumed on 877e3b4. No native UI/process/live-library actions were performed by this implementer. Original untracked review.diff was preserved and not staged. @@ -19,6 +19,10 @@ incoming Work preparation, immediately before target equality, closing the old late-source-change window. Source idle/address ownership is rechecked there as well: excluding address bytes from semantic hashes does not bypass idle checks. +Initial implementation: 29c154d. The review correction is described below. +Durable documentation lives here; an ignored review copy remains at +.superpowers/sdd/2026-11-02-issue-2-management/task-3-report.md. + Scoped production files: backup_recovery.py and new scoped_manifest.py. Scoped tests: test_backup_recovery.py and new test_scoped_manifest.py. No Verify public types, Task 2 helpers/READY logic, native repair classifiers/process whitelist, @@ -33,12 +37,33 @@ categories. Rules are case-insensitive; manifests preserve relative path case. Explicit semantic exclusions, still copied and metadata-validated: -- Directory components: log, logs, temp, tmp, __pycache__, backup, backups, - output, outputs, report, reports, gerber, gerbers, odb++, and historical - Work.before-restore-* directory components. +- Runtime/history child directories **at a project workspace root**, its PCB + workspace root, a selected Work/Cert root, or the library root: log, logs, + temp, tmp, __pycache__, backup, backups, output, outputs, report, reports, + gerber, gerbers, odb++, Log Files, LogFiles, and Work.before-restore-*. +- ProjectBackup/backups only at those workspace roots. Other ProjectBackup + content remains conservative primary data. +- Known native runtime below database/cdbsvr and PCB/database/cdbsvr: log, + dbcheck and dbrestore subtrees. In shared library databases, those same + subtrees below SymbolLibs//cdbsvr or /cdbsvr. Other + unknown files below cdbsvr remain primary; the directory is not dropped. - File suffixes: .log, .tmp, .temp, .lck, .lock, .bak, .backup, .pdf. - File names: sAddress.adr and editor files starting with ~$. +Directory exclusions are contextual rather than arbitrary ancestor matching. +ReusableBlocksLP//Work and Cert are structural identity +components, never runtime labels. CBB names Logs, Temp, Backup, Output, Report, +Log Files, ProjectBackup and Work.before-restore-engineering remain valid and +their primary files are hashed in both semantic manifests and complete-copy +validation. Case variants have the same protection. + +Shared dependency partitions and nested engineering folders keep their names: +SymbolLibs/temp/icdb.dat, SymbolLibs/Temp_a/device.sym, +SymbolLibs/Backup/device.sym, hdl/Logs/design.v and hdl/Backup/design.v are +primary. Runtime directory labels apply at the known roots above, not inside +arbitrary engineering/dependency hierarchies. File suffix/name exclusions are +applied to files, not to engineering directories with matching suffixes. + The uploader-compatible ..cbb-upload.lock falls under .lock, so it is runtime and never primary. It is **not** silently removed from complete copies. @@ -137,6 +162,10 @@ added. Empty directory metadata is intentionally not part of semantic equality. ## RED/GREEN and verification evidence +The original full-suite evidence below belongs to initial commit 29c154d. The +P1 review correction has fresh focused validation, and has **not** rerun the +full suite; the coordinator requested focused tests only for this fix. + 1. Initial RED, before production edits: `python -m pytest tests/test_scoped_manifest.py tests/test_backup_recovery.py -q` -> **29 failed, 26 passed in 7.89s**. Failures demonstrated missing versioned @@ -167,6 +196,41 @@ source content changes with preserved metadata, complete copy membership and nonprimary size consistency, idle/address rechecks, retained copies on failure, and unchanged native reopen/retained-Work recovery behavior. +## Review correction: contextual runtime directory classification + +Review found that the initial unconditional directory ancestor rules could +classify valid selected CBB names (Logs/Temp/Backup) or real SymbolLibs/temp +engineering partitions as runtime. That removed primary paths from both the +baseline manifest and later conflict comparison, and from copy byte checks. + +The classifier now resolves the structural role first and only applies the +runtime directory rules at documented workspace/native DB locations. Semantic +directory traversal and complete-copy byte classification share that rule. +Complete tree copy/metadata validation remains unchanged. This also protects +nested engineering folders with names Logs/Backup, and avoids broad dropping +of unknown cdbsvr files. + +Independent regressions cover Work and Cert under 11 colliding CBB names/case +variants, real SymbolLibs/temp and Temp_a partitions, arbitrary nested +engineering folders, source and destination corruption with unchanged size/ +mtime, independent locked database destination corruption, and end-to-end +recovery conflicts that previously overwrote concurrent target/shared edits. +Known native Log Files, ProjectBackup/backups and DB server log/dbcheck/ +dbrestore data remain semantically ignored and completely copied. + +Review-fix RED: +`python -m pytest tests/test_scoped_manifest.py tests/test_backup_recovery.py -q --tb=short` +-> **47 failed, 74 passed in 18.41s**, before production changes. The failures +included silently accepted primary corruption and installs over concurrent +target/shared edits. Additional conservative-boundary regressions prevented +reintroducing the same bug through Log Files/ProjectBackup/retained-Work names +or broad cdbsvr directory exclusion. + +Final review-fix focused GREEN: +`python -m pytest tests/test_scoped_manifest.py tests/test_backup_recovery.py tests/test_backup_recovery_dialog.py -q --tb=short` +-> **141 passed in 18.28s**. No native UI/process actions, live-library mutation, +or full-suite run was performed during this fix round. + ## Limitations / follow-up boundary - No real field lost-session reproduction, native GUI success reproduction, or diff --git a/src/cbb_refresh_verify/scoped_manifest.py b/src/cbb_refresh_verify/scoped_manifest.py index 35a82ea..402fda3 100644 --- a/src/cbb_refresh_verify/scoped_manifest.py +++ b/src/cbb_refresh_verify/scoped_manifest.py @@ -18,12 +18,35 @@ MANIFEST_VERSION = 1 _NONPRIMARY_DIRECTORIES = frozenset({ 'log', 'logs', 'temp', 'tmp', '__pycache__', 'backup', 'backups', 'output', 'outputs', 'report', 'reports', 'gerber', 'gerbers', 'odb++', + 'log files', 'logfiles', }) _NONPRIMARY_SUFFIXES = frozenset({ '.log', '.tmp', '.temp', '.lck', '.lock', '.bak', '.backup', '.pdf', }) +def _root_runtime(directories): + """Known runtime/history children of a library or project workspace root.""" + return bool(directories and ( + directories[0] in _NONPRIMARY_DIRECTORIES + or directories[0].startswith('work.before-restore-') + or directories[:2] == ('projectbackup', 'backups'))) + + +def _native_runtime(directories, database_prefix): + runtime_prefix = database_prefix + ('cdbsvr',) + return (directories[:len(runtime_prefix)] == runtime_prefix + and len(directories) > len(runtime_prefix) + and directories[len(runtime_prefix)] in ('log', 'dbcheck', 'dbrestore')) + + +def _project_runtime(directories): + return (_root_runtime(directories) + or (directories[:1] == ('pcb',) and _root_runtime(directories[1:])) + or _native_runtime(directories, ('database',)) + or _native_runtime(directories, ('pcb', 'database'))) + + @dataclass(frozen=True) class ManifestScope: """Project root by default; library selects one CBB and shared dependencies.""" @@ -46,21 +69,44 @@ class ManifestScope: def includes_directory(self, relative): parts = tuple(part.casefold() for part in Path(relative).parts) - if any(part in _NONPRIMARY_DIRECTORIES or part.startswith('work.before-restore-') - for part in parts): + if self._runtime_path(parts, directory=True): return False - if self.kind == 'project' or parts[0] != 'reusableblockslp': + if self.kind == 'project' or not parts or parts[0] != 'reusableblockslp': return True return ((len(parts) < 2 or parts[1] == self.block_name.casefold()) and (len(parts) < 3 or parts[2] in ('work', 'cert'))) + def _runtime_path(self, parts, *, directory=False): + """Recognize runtime locations only in their structural context. + + Generic names such as ``Logs`` are runtime when they are a project-root + or selected Work/Cert child. The same name deeper in an engineering + partition (or in shared SymbolLibs data) remains primary. + """ + directories = parts if directory else parts[:-1] + if not directory and (Path(parts[-1]).suffix in _NONPRIMARY_SUFFIXES + or parts[-1] == 'saddress.adr' or parts[-1].startswith('~$')): + return True + if self.kind == 'project': + return _project_runtime(directories) + if directories[:1] == ('reusableblockslp',): + # Identity and Work/Cert role components never act as runtime names. + if (len(directories) >= 3 and directories[1] == self.block_name.casefold() + and directories[2] in ('work', 'cert')): + return _project_runtime(directories[3:]) + return False + if _root_runtime(directories): + return True + # Shared dependency names/partitions are engineering data. Only native + # server runtime subtrees within known DB positions are excluded. + if directories[:1] == ('symbollibs',) and len(directories) >= 2: + return _native_runtime(directories, directories[:2]) + return bool(directories and _native_runtime(directories, directories[:1])) + def classify(self, relative): """Return a semantic scope label or None; paths are relative to the root.""" parts = tuple(part.casefold() for part in Path(relative).parts) - if (any(part in _NONPRIMARY_DIRECTORIES or part.startswith('work.before-restore-') - for part in parts[:-1]) - or Path(parts[-1]).suffix in _NONPRIMARY_SUFFIXES - or parts[-1] == 'saddress.adr' or parts[-1].startswith('~$')): + if self._runtime_path(parts): return None if self.kind == 'project': return 'project' diff --git a/tests/test_backup_recovery.py b/tests/test_backup_recovery.py index 0d79ee9..bb7924c 100644 --- a/tests/test_backup_recovery.py +++ b/tests/test_backup_recovery.py @@ -280,3 +280,49 @@ def test_source_address_record_is_rechecked_after_validation(recovery_files): assert 'ownership' in result.stderr assert address.read_bytes() == b'live owner' assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged' + + +@pytest.mark.parametrize('name', ['Logs', 'tEmP', 'bAcKuP', 'Output']) +@pytest.mark.parametrize('role', ['Work', 'Cert']) +def test_cbb_names_matching_runtime_labels_do_not_hide_target_conflicts(tmp_path, monkeypatch, name, role): + from cbb_refresh_verify.backup_recovery import recover_from_backup + monkeypatch.setattr(Path, 'home', lambda: tmp_path/'profile') + library = tmp_path/'target/target.lmc' + original = project(library.parent/f'ReusableBlocksLP/{name}/Work', name, b'damaged') + library.write_bytes(b'library') + source = project(tmp_path/'backup/Work', name, b'healthy') + request = VerifyRequest(library, name, tmp_path/'evidence') + conflict = original.parent.parent/role/'database/icdb.dat' + conflict.parent.mkdir(parents=True, exist_ok=True) + if role == 'Cert': + conflict.write_bytes(b'cert before') + calls = [] + def verify(*args, **kwargs): + calls.append(args) + conflict.write_bytes(b'concurrent target edit') + return VerifyResult('PASS', library_flag='GREEN') + result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None) + assert result.status == 'BLOCKED' + assert len(calls) == 1 + assert conflict.read_bytes() == b'concurrent target edit' + assert not list(original.parent.parent.glob('Work.before-restore-*')) + + +@pytest.mark.parametrize('relative', ['SymbolLibs/temp/icdb.dat', 'SymbolLibs/Temp_a/device.sym', + 'SharedDependency/hdl/Logs/design.v']) +def test_shared_partition_engineering_conflicts_block_install(recovery_files, relative): + from cbb_refresh_verify.backup_recovery import recover_from_backup + request, original, source = recovery_files + conflict = request.lmc_file.parent/relative + conflict.parent.mkdir(parents=True, exist_ok=True) + conflict.write_bytes(b'shared before') + calls = [] + def verify(*args, **kwargs): + calls.append(args) + conflict.write_bytes(b'concurrent shared edit') + return VerifyResult('PASS', library_flag='GREEN') + result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None) + assert result.status == 'BLOCKED' + assert len(calls) == 1 + assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged' + assert conflict.read_bytes() == b'concurrent shared edit' diff --git a/tests/test_scoped_manifest.py b/tests/test_scoped_manifest.py index 9d30080..d4d5647 100644 --- a/tests/test_scoped_manifest.py +++ b/tests/test_scoped_manifest.py @@ -213,3 +213,123 @@ def test_only_primary_paths_are_read_for_sha256_and_manifest_is_relocatable(tmp_ assert recovery._manifest(source, scope=scope) == manifest assert recovery._manifest(destination, scope=scope) == manifest assert {p.relative_to(destination).as_posix() for p in destination.rglob('*') if p.is_file()} == primary | excluded + + +@pytest.mark.parametrize('block', ['Logs', 'lOgS', 'Temp', 'tEmP', 'Backup', 'bAcKuP', 'Output', 'rEpOrT', + 'Log Files', 'ProjectBackup', 'Work.before-restore-engineering']) +@pytest.mark.parametrize('role', ['Work', 'Cert']) +def test_structural_cbb_identity_is_primary_in_semantic_and_copy_manifests(tmp_path, block, role): + from cbb_refresh_verify.scoped_manifest import ManifestScope + scope = ManifestScope.library(block, 'target.lmc') + source, destination = tmp_path/'source', tmp_path/'destination' + relative = f'ReusableBlocksLP/{block}/{role}/database/icdb.dat' + primary = put(source, relative) + runtime = f'ReusableBlocksLP/{block}/{role}/logs/session.txt' + put(source, runtime) + before = recovery._copy_verified(source, destination, scope=scope) + assert set(before['files']) == {relative} + assert (destination/runtime).read_bytes() == b'engineering' + assert recovery._manifest(source, scope=scope) == before + primary.write_bytes(b'concurrent primary edit') + assert recovery._manifest(source, scope=scope) != before + + +@pytest.mark.parametrize('relative', [ + 'SymbolLibs/temp/icdb.dat', 'SymbolLibs/Temp_a/device.sym', + 'SymbolLibs/Logs/device.sym', 'SymbolLibs/BACKUP/device.sym', + 'SymbolLibs/temp/cdbsvr/persistent.bin', + 'ReusableBlocksLP/A/Work/hdl/Logs/design.v', + 'ReusableBlocksLP/A/Cert/hdl/bAcKuP/design.v', + 'SymbolLibs/temp/hdl/Backup/design.v', 'SharedDependency/hdl/Logs/design.v', +]) +def test_library_engineering_partition_names_are_not_runtime_categories(tmp_path, relative): + from cbb_refresh_verify.scoped_manifest import ManifestScope + scope = ManifestScope.library('A', 'target.lmc') + source, destination = tmp_path/'source', tmp_path/'destination' + primary = put(source, relative) + before = recovery._copy_verified(source, destination, scope=scope) + assert relative in before['files'] + assert recovery._manifest(source, scope=scope) == before + primary.write_bytes(b'concurrent shared/design edit') + assert recovery._manifest(source, scope=scope) != before + + +@pytest.mark.parametrize('relative', ['hdl/Logs/design.v', 'hdl/Backup/design.v', 'symbols/Temp/device.sym', + 'hdl/Log Files/design.v', 'hdl/ProjectBackup/backups/design.v', + 'hdl/Work.before-restore-engineering/design.v', 'database/cdbsvr/persistent.bin']) +def test_nested_project_engineering_names_remain_primary(tmp_path, relative): + primary = put(tmp_path, relative) + before = recovery._manifest(tmp_path) + assert relative in before['files'] + primary.unlink() + assert recovery._manifest(tmp_path) != before + + +@pytest.mark.parametrize('relative', [ + 'ReusableBlocksLP/Logs/Work/Logs.prj', + 'ReusableBlocksLP/Backup/Cert/Backup.prj', + 'SymbolLibs/temp/device.sym', 'SharedDependency/hdl/Logs/design.v', +]) +@pytest.mark.parametrize('side', ['source', 'destination']) +def test_colliding_engineering_names_cannot_bypass_primary_copy_byte_checks(tmp_path, monkeypatch, relative, side): + from cbb_refresh_verify.scoped_manifest import ManifestScope + block = relative.split('/')[1] if relative.startswith('ReusableBlocksLP/') else 'A' + scope = ManifestScope.library(block, 'target.lmc') + source, destination = tmp_path/'source', tmp_path/'destination' + put(source, relative) + real_copy = shutil.copy2 + def copy_then_corrupt(src, dst, *args, **kwargs): + result = real_copy(src, dst, *args, **kwargs) + corrupt = Path(src) if side == 'source' else Path(dst) + metadata = corrupt.stat() + corrupt.write_bytes(b'X' * metadata.st_size) + os.utime(corrupt, ns=(metadata.st_atime_ns, metadata.st_mtime_ns)) + return result + monkeypatch.setattr(shutil, 'copy2', copy_then_corrupt) + with pytest.raises(OSError): + recovery._copy_verified(source, destination, scope=scope) + assert (destination/relative).exists() + + +@pytest.mark.parametrize('prefix', ['', 'ReusableBlocksLP/Logs/Work/', 'ReusableBlocksLP/Temp/Cert/']) +@pytest.mark.parametrize('relative', [ + 'Log Files/ProjectBackupLog.txt', 'ProjectBackup/backups/old.zip', + 'database/cdbsvr/log/session/text.txt', 'database/cdbsvr/dbcheck/session/text.txt', + 'PCB/database/cdbsvr/dbrestore/session/text.txt', +]) +def test_known_native_runtime_and_history_locations_are_ignored_contextually(tmp_path, prefix, relative): + from cbb_refresh_verify.scoped_manifest import ManifestScope + scope = ManifestScope.library(prefix.split('/')[1], 'target.lmc') if prefix else None + runtime = put(tmp_path, prefix + relative) + before = recovery._manifest(tmp_path, scope=scope) + assert not before['files'] + runtime.write_bytes(b'updated runtime') + assert recovery._manifest(tmp_path, scope=scope) == before + + +@pytest.mark.parametrize('relative', ['SymbolLibs/temp/icdb.dat', + 'ReusableBlocksLP/Logs/Work/database/icdb.dat']) +def test_locked_critical_bytes_in_colliding_names_are_checked_independently(tmp_path, relative): + from cbb_refresh_verify.scoped_manifest import ManifestScope + source, destination = tmp_path/'source', tmp_path/'destination' + put(source, relative) + def corrupt_after_copy(event): + if event['copied_files'] == event['total_files']: + copied = destination/relative + metadata = copied.stat() + copied.write_bytes(b'X' * metadata.st_size) + os.utime(copied, ns=(metadata.st_atime_ns, metadata.st_mtime_ns)) + with pytest.raises(OSError): + recovery._copy_verified(source, destination, + scope=ManifestScope.library('Logs', 'target.lmc'), progress=corrupt_after_copy) + assert (source/relative).read_bytes() == b'engineering' + assert (destination/relative).read_bytes() == b'XXXXXXXXXXX' + + +@pytest.mark.parametrize('relative', ['SymbolLibs/temp/cdbsvr/log/session/text.txt', + 'SymbolLibs/Temp_a/cdbsvr/dbcheck/session/text.txt', + 'CellDB/cdbsvr/dbrestore/session/text.txt']) +def test_shared_native_runtime_has_explicit_database_context(tmp_path, relative): + from cbb_refresh_verify.scoped_manifest import ManifestScope + put(tmp_path, relative) + assert not recovery._manifest(tmp_path, scope=ManifestScope.library('A', 'target.lmc'))['files'] -- 2.39.5 From 797a79c30ea9049487dc31b9821c8bf9b2011343 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: Mon, 2 Nov 2026 21:13:24 +0800 Subject: [PATCH 10/59] feat: add guarded native CBB management and recovery coordination --- docs/releases/2026-11-02-cbb-management.md | 87 +++ packaging/CbbRefreshVerify.spec | 2 +- src/cbb_refresh_verify/application.py | 4 + src/cbb_refresh_verify/backup_recovery.py | 24 +- src/cbb_refresh_verify/com_runtime.py | 18 + src/cbb_refresh_verify/library_lock.py | 288 +++++++++ src/cbb_refresh_verify/management.py | 334 ++++++++++ src/cbb_refresh_verify/native_management.py | 576 ++++++++++++++++++ src/cbb_refresh_verify/scoped_manifest.py | 21 +- src/cbb_refresh_verify/ui/main_window.py | 117 +++- .../ui/management_dialog.py | 188 ++++++ src/cbb_refresh_verify/verify_runner.py | 24 +- tests/test_library_lock.py | 209 +++++++ tests/test_management.py | 254 ++++++++ tests/test_management_ui.py | 99 +++ tests/test_native_management.py | 165 +++++ tools/validate_native_management.py | 28 + 17 files changed, 2423 insertions(+), 15 deletions(-) create mode 100644 docs/releases/2026-11-02-cbb-management.md create mode 100644 src/cbb_refresh_verify/com_runtime.py create mode 100644 src/cbb_refresh_verify/library_lock.py create mode 100644 src/cbb_refresh_verify/management.py create mode 100644 src/cbb_refresh_verify/native_management.py create mode 100644 src/cbb_refresh_verify/ui/management_dialog.py create mode 100644 tests/test_library_lock.py create mode 100644 tests/test_management.py create mode 100644 tests/test_management_ui.py create mode 100644 tests/test_native_management.py create mode 100644 tools/validate_native_management.py diff --git a/docs/releases/2026-11-02-cbb-management.md b/docs/releases/2026-11-02-cbb-management.md new file mode 100644 index 0000000..f49c693 --- /dev/null +++ b/docs/releases/2026-11-02-cbb-management.md @@ -0,0 +1,87 @@ +# CBB 管理候选实现(Issue #2 Task 4) + +本次在当前应用增加新增、同名更新和移除入口、逐项检查点、库外完整备份、 +来源工程暂存和明确的结果状态。READY 条目可以勾选用于管理;原有校验和恢复 +仍仅处理当前可见的非 READY 合格条目。 + +## 原生能力边界 + +移除路线来自 EE 7.9.5 随附 Tcl 的 `CleanWGLib F4 `,输入为 +`REUSE_BLOCK\tLogical-Physical\t`。协调端已在独立库副本完成真实移除 +和原生目录缺失复读。运行时核对已验证的 LibraryManager、CleanWGLib、 +LMCDLL70 和 LMCDocAddin70 文件 SHA256;仅允许匹配版本、DxDesigner 流程、 +Logical-Physical 分区及 ASCII 名称。输出必须包含对应名称/分区的 Removed +消息且不含 Warning/Error,之后重新打开原生目录确认条目缺失。 + +新增和同名更新默认禁用,界面说明缺少哪项隔离验证。已发现原生新增对话框 +不等于完成库注册;不能仅凭返回值或出现目录启用。通用引擎已实现新增冲突 +拒绝、暂存来源、名称/分区/工程内容复读、可选自动校验,以及明确更新时的 +原生移除后重新导入。更新必须同时有两个原生路线和同名端到端验证才可启用。 +移除后重新导入失败会报告 `RECOVERY_REQUIRED`,停止队列,不自动回滚或重放。 + +## 操作与恢复证据 + +每项结果包含 operation、library、name、partition、source、auto_verify、stage、 +status、safe_to_continue 和 evidence_path。`management/management-/` +保存原子更新的 `checkpoint.json`、完整 `library-before`、主数据清单、来源 +暂存副本、原生请求/输出和后续校验记录。结果区区分: + +- `UPLOAD_FAILED`:上传失败;不能解释为已上传。 +- `UPLOADED`:原生注册与工程身份已确认,本批次未自动校验。 +- `UPLOADED_VERIFY_FAILED`:已上传,但自动校验没有达到 PASS/GREEN。 +- `UPLOADED_VERIFIED`:已上传且自动校验达到 PASS/GREEN 并确认释放。 +- `REMOVED`:原生移除且重新读取目录确认缺失。 +- `RECOVERY_REQUIRED`:原生写入可能发生,或更新已移除但重新导入未完成; + 保留完整库备份和所有中间状态,需要人工审查。 + +启动时显示未完成和释放不确定的检查点,不自动执行。存在同一库的未完成 +检查点时,新的管理任务被阻止。先由管理员检查原生进程、目标目录和备份, +按现场恢复流程处理;确认恢复完毕后把已审查的操作证据目录移至独立归档位置, +再重新读取库。不要删除锁文件,不要强制结束持有数据库的原生程序。 + +## 并发和来源保护 + +管理、校验和备份恢复共享 `Local\CbbRefreshVerify795` 及原上传工具兼容的 +`..cbb-upload.lock` 独占句柄。只有相同线程持有的相同库锁允许嵌套; +完整备份通过已经持有的句柄读取锁内容,不重新打开或删除独占锁。 +管理任务不能确认原生所有者释放时,在本应用进程内保留该独占句柄,阻止旧上传 +工具抢入;不会仅因自动化线程返回或超时而释放锁。 +直接校验与备份恢复也保留释放不明确的锁。库旁保存 +`..cbb-upload.blocked.json`,本应用重启后拒绝写入和原生读取。 +该记录在取得库锁后、原生调用前写入,正常确认释放才清除。只有精确保留文件名、 +协议版本和所有权字段匹配的记录被归类为运行时元数据;完整副本仍保留其字节, +主数据比较忽略其生命周期变化。同名普通文件或损坏记录保持原样并阻止自动解除。 +“检查会话释放并解除协调阻止”入口先确认管理工作进程已返回、相关原生进程 +均已退出、库内不存在 sAddress.adr,且能同时以独占只读句柄打开所有 LMC、 +icdb.dat 和 LayoutDB.lyt。全部通过才归档阻止记录并关闭本应用保留的句柄; +不会删除上传锁文件,也不把会话释放解释为数据恢复成功。 + +进程崩溃会使 Windows 释放其句柄;旧上传工具不认识新增阻止记录,因此本方案 +不能保证本应用崩溃后旧上传工具仍被排除。崩溃后应先正常结束原生会话,并通过 +上述释放检查和未完成任务审查,再使用本工具或旧上传工具继续写入。 + +备份与来源不能置于目标库内部,来源必须是完整且引用位于工程内的 PRJ/PCB。 +链接、外部数据库引用、源文件变化、备份失败和同名新增冲突均会阻止写入。 +来源先完整复制;工程身份比较使用顶层块、原理图数据库和布局数据库内容指纹, +原生规范化若改变数据库字节则保守拒绝确认,需要进一步语义验证,不能声称等价。 + +原生管理在私有 Windows 桌面运行,不移动鼠标或占用前台输入。只操作当前 +已识别原生进程。读取目录遇到旧库升级或 Select flow type 对话框时取消并报错, +绝不将升级作为只读读取的一部分。超时保留原生所有者和证据,不强杀数据库进程; +确认前不继续下一项,不启动最终 Verify。 + +## 独立新增诊断 + +`python tools/validate_native_management.py SOURCE.prj NEW_OUTPUT_DIRECTORY NAME` +只接受不存在的新目录,先完整复制来源,再由私有工作进程调用原生 +`CreateLibrary(..., kFLOW_DX=1)` 创建固定的新库。随后使用已验证的 +Reusable Blocks / UIA Logical - Physical 页签和 New 按钮、1103 对话框字段。 +诊断会记录原生消息、关闭后的重新打开清单、工程内容比较和原始来源不变检查。 +它不接收生产库参数,不存在跳过生产能力门禁的开关,也不自动修改能力门禁。 +可加 `--replacement-source OTHER.prj` 在同一个新建隔离库中验证同名更新: +先备份完整旧库,再原生移除并检查缺失,然后重新导入替换工程。重新导入必须 +匹配本次 CreateLibrary 记录的固定路径和随机所有权令牌,不能指向外部库。 + +本阶段单元测试通过注入文件模拟的原生边界执行,不启动 Mentor、不操作实际库。 +候选冻结包、最终 Windows 11 原生场景由协调端单独验收。Windows 10 和真实 +现场失联样本仍未验证;文件头损坏与模拟故障不能替代这些验收。 diff --git a/packaging/CbbRefreshVerify.spec b/packaging/CbbRefreshVerify.spec index d1264eb..f535d9f 100644 --- a/packaging/CbbRefreshVerify.spec +++ b/packaging/CbbRefreshVerify.spec @@ -18,7 +18,7 @@ datas=[(str(project_root / 'src' / 'cbb_refresh_verify' / 'save_verified_block.v (str(assets_root / 'cbb_refresh_verify.png'), 'cbb_refresh_verify/assets'), (str(assets_root / 'cbb_refresh_verify.svg'), 'cbb_refresh_verify/assets'), (str(assets_root / 'dialog_rules.v1.json'), 'cbb_refresh_verify/assets')], - hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'], + hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.ui.management_dialog','cbb_refresh_verify.management','cbb_refresh_verify.native_management','cbb_refresh_verify.library_lock','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'], excludes=['PySide6.QtDesigner','PySide6.QtMultimedia','PySide6.QtMultimediaWidgets','PySide6.QtOpenGL','PySide6.QtOpenGLWidgets','PySide6.QtPdf','PySide6.QtPdfWidgets','PySide6.QtQml','PySide6.QtQuick','PySide6.QtQuickWidgets','PySide6.QtWebChannel','PySide6.QtWebEngineCore','PySide6.QtWebEngineWidgets'], ) excluded_qt_binaries = {'icudt78.dll','icuuc.dll','qpdf.dll','qt6pdf.dll','qt6qml.dll','qt6qmlmeta.dll','qt6qmlmodels.dll','qt6qmlworkerscript.dll','qt6quick.dll','qt6quickwidgets.dll'} diff --git a/src/cbb_refresh_verify/application.py b/src/cbb_refresh_verify/application.py index 8e1679d..ca9c9c2 100644 --- a/src/cbb_refresh_verify/application.py +++ b/src/cbb_refresh_verify/application.py @@ -47,6 +47,7 @@ def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser(prog="CbbRefreshVerify") parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--repair-worker', type=Path, help=argparse.SUPPRESS) + parser.add_argument('--management-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS) parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS) parser.add_argument('--batch-lmc', type=Path, help=argparse.SUPPRESS) @@ -56,6 +57,9 @@ def main(argv: Sequence[str] | None = None) -> int: parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS) parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS); parser.add_argument("--data-root", type=Path, help=argparse.SUPPRESS) args = parser.parse_args(list(argv) if argv is not None else None) + if args.management_worker: + from .native_management import management_worker_main + return management_worker_main(args.management_worker) if args.repair_worker: from .native_repair_ui import repair_worker_main return repair_worker_main(args.repair_worker) diff --git a/src/cbb_refresh_verify/backup_recovery.py b/src/cbb_refresh_verify/backup_recovery.py index 5a5e38a..b6f3fc3 100644 --- a/src/cbb_refresh_verify/backup_recovery.py +++ b/src/cbb_refresh_verify/backup_recovery.py @@ -122,6 +122,8 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c return VerifyResult('BLOCKED', stderr='本机验证目录不能位于目标库或备份工程内。') mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') acquired = False + library_lock = None + release_confirmed = True installed = False record = {'operation': 'same_cbb_complete_project_restore', 'source': str(source), 'library': str(library), 'block': request.block_name, 'installed': False, @@ -141,12 +143,16 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c report = root/'recovery-result.json' report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8') return replace(result, evidence_path=report) - pythoncom.CoInitialize() + from .com_runtime import initialize_com, uninitialize_com + initialized = initialize_com() try: acquired = win32event.WaitForSingleObject(mutex, 0) in ( win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) if not acquired: return VerifyResult('BLOCKED', stderr='已有 CBB 任务运行,请等待结束。', safe_to_continue=False) + from .library_lock import LibraryLock + library_lock = LibraryLock(library, safe_to_release=lambda: release_confirmed) + library_lock.__enter__() if source.is_relative_to(library_root): raise ValueError('请选择目标中心库之外的独立备份工程。') project, pcb = preflight(replace(request, lmc_file=library)) @@ -192,7 +198,9 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c native_lm_verify=False, interactive_input=False) preflight(validation) emit('validate_source', '正在副本中打开备份工程并执行原生校验…') + release_confirmed = False source_result = verifier(validation, progress=progress, flag_reader=read_library_flag_if_available) + release_confirmed = source_result.safe_to_continue record['source_validation'] = asdict(source_result) if (source_result.status != 'PASS' or source_result.library_flag != 'GREEN' or not source_result.safe_to_continue): @@ -222,9 +230,11 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c installed = True record['retained_work'] = str(retained) emit('verify_target', '正在重新打开恢复后的目标工程并校验…') + release_confirmed = False final = verifier(replace(request, lmc_file=library, evidence_dir=root/'target-verify', auto_repair=False, native_lm_verify=False, interactive_input=False), progress=progress, flag_reader=read_library_flag_if_available) + release_confirmed = final.safe_to_continue if final.status == 'PASS' and (final.library_flag != 'GREEN' or not final.safe_to_continue): final = replace(final, status='FAIL', stderr='目标工程恢复后的 Ready 状态未确认。') if final.status != 'PASS': @@ -234,7 +244,11 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c return save(VerifyResult('FAIL' if installed else 'BLOCKED', stderr=str(error), safe_to_continue=False)) finally: - if acquired: - win32event.ReleaseMutex(mutex) - mutex.Close() - pythoncom.CoUninitialize() + try: + if library_lock is not None: + library_lock.__exit__(None, None, None) + finally: + if acquired: + win32event.ReleaseMutex(mutex) + mutex.Close() + uninitialize_com(initialized) diff --git a/src/cbb_refresh_verify/com_runtime.py b/src/cbb_refresh_verify/com_runtime.py new file mode 100644 index 0000000..0718c58 --- /dev/null +++ b/src/cbb_refresh_verify/com_runtime.py @@ -0,0 +1,18 @@ +"""Balanced COM initialization without tearing down an existing MTA caller.""" + + +def initialize_com(): + import pythoncom + try: + pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED) + except pythoncom.com_error as error: + if error.hresult != -2147417850: # RPC_E_CHANGED_MODE + raise + return False # Existing apartment is usable; no reference was added. + return True + + +def uninitialize_com(initialized): + if initialized: + import pythoncom + pythoncom.CoUninitialize() diff --git a/src/cbb_refresh_verify/library_lock.py b/src/cbb_refresh_verify/library_lock.py new file mode 100644 index 0000000..364cd27 --- /dev/null +++ b/src/cbb_refresh_verify/library_lock.py @@ -0,0 +1,288 @@ +"""Uploader-compatible exclusive sidecar lease. Metadata never proves ownership.""" +from datetime import datetime, timezone +from contextlib import contextmanager, ExitStack +import json +import re +import os +from pathlib import Path +import socket +import threading +from uuid import uuid4 + +_guard = threading.RLock() +_owners = {} + + +class LibraryBusy(OSError): + pass + + +def _key(path): + return str(Path(path).resolve()).casefold() + + +def blocker_path(library): + library = Path(library).resolve() + return library.with_name('.' + library.stem + '.cbb-upload.blocked.json') + + +def checkpoint_record(path): + """Recognize only our reserved filename and complete versioned protocol.""" + path = Path(path) + if not (path.name.endswith('.cbb-upload.blocked.json') or path.name.endswith('.cbb-upload.blocked.pending')): + return None + try: + import stat + if path.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: + return None + record = json.loads(path.read_text(encoding='utf-8')) + library = Path(record['library']) + expected = '.' + library.stem + '.cbb-upload.blocked' + if (record.get('protocol') != 'cbb-refresh-verify-library-lease' or record.get('version') != 1 + or not library.is_absolute() or library.suffix.casefold() != '.lmc' + or path.name not in (expected+'.json', expected+'.pending') + or not re.fullmatch('[0-9a-f]{32}', str(record.get('lease_id', ''))) + or not isinstance(record.get('pid'), int) or record['pid'] <= 0 + or record.get('status') not in ('OPERATION_IN_PROGRESS', 'OWNER_RELEASE_UNCONFIRMED') + or not isinstance(record.get('time'), str)): + return None + return record + except (OSError, ValueError, TypeError, KeyError): + return None + + +def has_blocker(library): + path = blocker_path(library) + if path.with_suffix('.pending').exists(): + return True + if not path.exists(): + return False + record = checkpoint_record(path) + # Complete snapshots retain every file, including their source lease + # checkpoint. Only a valid relocated snapshot record can be ignored. + return record is None or _key(record['library']) == _key(library) + + +def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id): + path = blocker_path(library) + pending = path.with_suffix('.pending') + with pending.open('w', encoding='utf-8') as stream: + json.dump({'protocol': 'cbb-refresh-verify-library-lease', 'version': 1, + 'lease_id': lease_id, 'library': str(library), 'status': status, + 'pid': os.getpid(), 'time': datetime.now(timezone.utc).isoformat()}, stream) + stream.flush(); os.fsync(stream.fileno()) + pending.replace(path) + + +class LibraryLock: + def __init__(self, library, *, safe_to_release=None): + library = Path(library).resolve() + self.library = library + self.path = library.with_name('.' + library.stem + '.cbb-upload.lock') + self.key = _key(self.path) + self.entered = False + self.safe_to_release = safe_to_release + + def __enter__(self): + import win32con, win32file + with _guard: + owner = _owners.get(self.key) + if owner: + if owner['thread'] != threading.get_ident() or owner['retained']: + raise LibraryBusy(f'中心库被其他任务占用:{self.path}') + owner['depth'] += 1 + else: + if has_blocker(self.library): + raise LibraryBusy(f'中心库存在未解除的会话阻止记录,请先执行释放检查:{blocker_path(self.library)}') + try: + lease_id = uuid4().hex + handle = win32file.CreateFile(str(self.path), + win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, + win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) + except Exception as error: + raise LibraryBusy(f'中心库已锁定或无法创建协调锁:{self.path}: {error}') from error + try: + data = (f'machine={socket.gethostname()}; pid={os.getpid()}; ' + f'startedUtc={datetime.now(timezone.utc).isoformat()}; ' + f'library={self.library}').encode('utf-8') + win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN) + win32file.SetEndOfFile(handle) + win32file.WriteFile(handle, data) + win32file.FlushFileBuffers(handle) + # Closing flushes the last-write timestamp on Windows. Set + # it explicitly before complete-copy metadata is captured. + import pywintypes + win32file.SetFileTime(handle, None, None, pywintypes.Time(datetime.now())) + _persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id) + except Exception: + handle.Close() + raise + _owners[self.key] = {'thread': threading.get_ident(), 'depth': 1, + 'handle': handle, 'retained': False, 'lease_id': lease_id} + self.entered = True + return self + + def snapshot_bytes(self): + data = owned_snapshot(self.path) + if data is None: + raise LibraryBusy('当前线程不拥有此库协调锁') + return data + + def retain(self): + with _guard: + if self.entered: + _owners[self.key]['retained'] = True + + def __exit__(self, *_): + with _guard: + if not self.entered: + return + owner = _owners[self.key] + if self.safe_to_release is not None: + try: + safe = self.safe_to_release() is True + except Exception: + safe = False + if not safe: + # A supervisor can time out while its native child still + # owns the DB. Keep excluding the uploader until app exit. + owner['retained'] = True + owner['depth'] -= 1 + self.entered = False + if owner['depth'] == 0 and not owner['retained']: + # This JSON is our durable operation checkpoint, not the + # uploader's .lock file. Clear only after proven normal release. + marker = blocker_path(self.library) + record = checkpoint_record(marker) + if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']): + owner['retained'] = True + raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。') + try: + marker.unlink(missing_ok=True) + except OSError: + owner['retained'] = True + raise + owner['handle'].Close() + del _owners[self.key] + elif owner['retained']: + marker = blocker_path(self.library) + record = checkpoint_record(marker) + if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']): + raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。') + _persist_blocker(self.library, lease_id=owner['lease_id']) + + +def owned_snapshot(path): + """Read only a lease held by this thread, never reopen an exclusive file.""" + import win32con, win32file + with _guard: + owner = _owners.get(_key(path)) + if not owner or owner['thread'] != threading.get_ident() or owner['retained']: + return None + handle = owner['handle'] + position = win32file.SetFilePointer(handle, 0, win32con.FILE_CURRENT) + try: + win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN) + size = win32file.GetFileSize(handle) + return win32file.ReadFile(handle, size)[1] if size else b'' + finally: + win32file.SetFilePointer(handle, position, win32con.FILE_BEGIN) + + +@contextmanager +def _prove_library_released(library): + """Hold all native database read handles during explicit lease release.""" + import pythoncom, win32com.client, win32con, win32file + from .scoped_manifest import scan_tree + from .native_management import _retained_workers + if any(worker.poll() is None for worker in _retained_workers): + raise LibraryBusy('原生管理工作进程尚未返回,不能解除协调阻止。') + from .com_runtime import initialize_com, uninitialize_com + initialized = initialize_com() + try: + service = win32com.client.GetObject('winmgmts:') + names = ('LibraryManager.exe', 'ExpeditionPCB.exe', 'viewdraw.exe', + 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe') + query = 'SELECT ProcessId, Name FROM Win32_Process WHERE ' + ' OR '.join( + f"Name='{name}'" for name in names) + owners = [{'pid': int(item.ProcessId), 'name': str(item.Name)} for item in service.ExecQuery(query)] + service = None + if owners: + raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}') + entries = scan_tree(Path(library).parent) + addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr'] + if addresses: + raise LibraryBusy(f'库中仍有数据库地址记录,不能自动解除:{addresses}') + native = [path for path in entries if path.is_file() + and (path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))] + if Path(library).resolve() not in native: + raise LibraryBusy('目标库身份缺失,不能解除协调阻止。') + with ExitStack() as stack: + for path in native: + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + stack.callback(handle.Close) + yield {'native_owners': owners, 'addresses': addresses, + 'exclusive_files': [str(path) for path in native]} + finally: + uninitialize_com(initialized) + + +def release_retained_library(library, evidence_dir): + """Explicit user-requested release, after fresh owner/address/access proof. + + The upload sidecar is never deleted. This acknowledges process release, + not the correctness of a partially changed library or its recovery state. + """ + import win32con, win32event, win32file + library = Path(library).resolve(strict=True) + evidence = Path(evidence_dir).resolve() + if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence): + raise ValueError('释放检查证据必须位于目标库之外。') + lease = LibraryLock(library) + mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') + acquired = False + external = None + try: + acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) + if not acquired: + raise LibraryBusy('另一个 CBB 操作仍在运行。') + with _guard: + owner = _owners.get(lease.key) + if owner and (not owner['retained'] or owner['depth']): + raise LibraryBusy('本应用操作仍在使用库锁。') + if owner is None: + try: + external = win32file.CreateFile(str(lease.path), + win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, + win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) + except Exception as error: + raise LibraryBusy('其他进程仍持有库锁,不能解除。') from error + with _prove_library_released(library) as proof: + marker = blocker_path(library) + checkpoints = (marker, marker.with_suffix('.pending')) + if any(path.exists() and checkpoint_record(path) is None for path in checkpoints): + raise LibraryBusy('协调检查点格式不明或已损坏,保留原文件供人工审查。') + root = evidence/('release-' + uuid4().hex) + root.mkdir(parents=True, exist_ok=False) + record = {'library': str(library), 'status': 'RELEASE_CONFIRMED', **proof, + 'data_recovery_confirmed': False, 'evidence_path': str(root/'release.json')} + (root/'release.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8') + for index, checkpoint in enumerate(checkpoints): + if checkpoint.exists(): + data = checkpoint.read_bytes() + archived = root/f'blocker-before-{index}.json' + archived.write_bytes(data) + if archived.read_bytes() != data: + raise OSError('阻止记录归档校验失败,保持库锁。') + checkpoint.unlink() # Checkpoint JSON only; never .lock. + if owner: + owner['handle'].Close() + del _owners[lease.key] + return record + finally: + if external is not None: + external.Close() + if acquired: + win32event.ReleaseMutex(mutex) + mutex.Close() diff --git a/src/cbb_refresh_verify/management.py b/src/cbb_refresh_verify/management.py new file mode 100644 index 0000000..1664344 --- /dev/null +++ b/src/cbb_refresh_verify/management.py @@ -0,0 +1,334 @@ +"""Checkpointed CBB mutations with native evidence and no automatic replay.""" +from dataclasses import asdict, dataclass, field, replace +from datetime import datetime, timezone +import json +from pathlib import Path, PureWindowsPath +import re +from uuid import uuid4 + +from .library_lock import LibraryLock, LibraryBusy +from .scoped_manifest import ManifestScope, build_manifest, copy_verified, hash_file, scan_tree + + +@dataclass(frozen=True) +class Capability: + supported: bool + reason: str + + +@dataclass(frozen=True) +class ManagementRequest: + operation: str + library: Path + name: str + partition: str = 'Logical-Physical' + source: Path | None = None + auto_verify: bool = True + evidence_dir: Path = Path('management-evidence') + top_level_block: str = '' + timeout_seconds: float = 180 + + +@dataclass(frozen=True) +class CatalogEntry: + name: str + partition: str + engineering_identity: dict = field(default_factory=dict) + + +@dataclass(frozen=True) +class Catalog: + library: Path + flow: int + entries: tuple[CatalogEntry, ...] + safe_to_continue: bool = True + + +@dataclass(frozen=True) +class NativeOutcome: + success: bool + safe_to_continue: bool + detail: str + + +@dataclass(frozen=True) +class ManagementResult: + operation: str + library: Path + name: str + partition: str + source: Path | None + auto_verify: bool + stage: str + status: str + safe_to_continue: bool + detail: str = '' + evidence_path: Path | None = None + + +def validate_name(value): + if (not value or value != value.strip() or value.endswith('.') or value in ('.', '..') + or any(ord(c) < 32 or c in '<>:"/\\|?*' for c in value) + or value.split('.')[0].upper() in {'CON', 'PRN', 'AUX', 'NUL', + *(f'COM{i}' for i in range(1, 10)), *(f'LPT{i}' for i in range(1, 10))}): + raise ValueError('CBB 名称/分区不是有效单一名称,禁止路径、控制字符或保留名称。') + + +def source_project(source, top_level_block=''): + """Require complete PRJ/PCB and self-contained engineering references.""" + from .backup_recovery import _project_text + source = Path(source).resolve(strict=True) + if source.suffix.casefold() != '.prj' or not source.is_file(): + raise ValueError('请选择完整工程的 PRJ 文件。') + scan_tree(source.parent) # Reject reparse points, including file links. + text = _project_text(source) + roots = re.findall(r'^KEY RootBlock "([^"]+)"', text, re.M) + if len(roots) != 1 or top_level_block and roots[0] != top_level_block: + raise ValueError('工程顶层块不唯一或与输入不一致。') + validate_name(roots[0]) + def reference(path, key): + values = re.findall(r'^KEY ' + re.escape(key) + r' "([^"]+)"', _project_text(path), re.M) + if len(values) != 1 or PureWindowsPath(values[0]).is_absolute() or PureWindowsPath(values[0]).drive: + raise ValueError(f'{key} 必须为唯一工程内相对引用。') + resolved = (path.parent/values[0]).resolve() + if not resolved.is_relative_to(source.parent): + raise ValueError(f'{key} 越出来源工程。') + return resolved + pcb = reference(source, 'PCBDesignPath') + if not pcb.is_file() or reference(pcb, 'ProjectFile') != source: + raise ValueError('PCB 未引用所选来源工程。') + for path in (source, pcb.parent/'pcb.prj'): + if not path.is_file(): + raise ValueError('工程缺少 PCB 工程配置。') + db = reference(path, 'iCDBDir') + if not (db/'icdb.dat').is_file(): + raise ValueError('工程缺少原理图/PCB 数据库。') + if not (pcb.parent/'Layout/LayoutDB.lyt').is_file(): + raise ValueError('工程缺少布局数据库。') + return source, pcb, roots[0] + + +def engineering_identity(source): + """Compare actual registered engineering bytes, not a name-only acknowledgment. + + Conservative until native normalization has an independently proven semantic + comparison: changed database bytes cannot be claimed to match the source. + """ + source, pcb, top = source_project(source) + from .backup_recovery import _project_text + db = re.findall(r'^KEY iCDBDir "([^"]+)"', _project_text(source), re.M)[0] + return {'top_level_block': top, 'schematic_sha256': hash_file(source.parent/db/'icdb.dat'), + 'layout_sha256': hash_file(pcb.parent/'Layout/LayoutDB.lyt')} + + +def _write_json(path, value): + path = Path(path) + pending = path.with_suffix('.pending') + with pending.open('w', encoding='utf-8') as stream: + json.dump(value, stream, ensure_ascii=False, default=str, indent=2) + stream.flush() + import os + os.fsync(stream.fileno()) + pending.replace(path) + + +def unfinished_operations(root): + results = [] + for path in sorted(Path(root).glob('*/checkpoint.json')): + try: + record = json.loads(path.read_text(encoding='utf-8')) + if record.get('stage') != 'complete' or record.get('safe_to_continue') is False: + results.append({**record, 'evidence_path': str(path)}) + except (OSError, ValueError): + results.append({'status': 'CHECKPOINT_UNREADABLE', 'evidence_path': str(path)}) + return results + + +def run_management(request, *, adapter=None, verifier=None, idle_check=None, progress=None): + from .verify_runner import VerifyRequest, run_verify + from .backup_recovery import _idle_check + import win32con, win32event + import pythoncom + if adapter is None: + from .native_management import NativeManagementAdapter + adapter = NativeManagementAdapter() + verifier = verifier or run_verify + idle_check = idle_check or _idle_check + root = report = None + stage = 'preflight' + history = [] + mutated = uploaded = removed = False + owner_safe = True + mutex = None + acquired = False + def checkpoint(next_stage, **details): + nonlocal stage + stage = next_stage + history.append({'stage': stage, 'time': datetime.now(timezone.utc).isoformat(), **details}) + if report: + _write_json(report, {**asdict(request), 'stage': stage, 'status': 'RUNNING', + 'safe_to_continue': False, 'history': history}) + if progress: + progress({'stage': stage, 'block': request.name, 'operation': request.operation, **details}) + def finish(status, detail='', safe=True): + result = ManagementResult(request.operation, request.library, request.name, request.partition, + request.source, request.auto_verify, 'complete', status, safe, detail, report) + if report: + _write_json(report, {**asdict(result), 'history': history}) + return result + from .com_runtime import initialize_com, uninitialize_com + initialized = initialize_com() + try: + if request.operation not in ('add', 'update', 'remove'): + raise ValueError('不支持的管理操作。') + validate_name(request.name); validate_name(request.partition) + library = Path(request.library).resolve(strict=True) + if library.suffix.casefold() != '.lmc' or not library.is_file(): + raise ValueError('中心库必须为现有 LMC 文件。') + evidence = Path(request.evidence_dir).resolve() + if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence): + raise ValueError('操作记录与备份必须位于目标中心库之外,且不能包含目标库。') + capability = adapter.capabilities().get(request.operation, Capability(False, '未验证的原生接口')) + if not capability.supported: + return finish('UNSUPPORTED', capability.reason) + for unfinished in unfinished_operations(evidence): + if unfinished.get('library') and Path(unfinished['library']).resolve() == library: + return finish('BLOCKED', f'存在未完成/需恢复任务,禁止自动重放:{unfinished["evidence_path"]}', False) + source = pcb = None + if request.operation in ('add', 'update'): + if request.source is None: + raise ValueError('新增/更新需要完整来源工程。') + source, pcb, top = source_project(request.source, request.top_level_block) + if (source.is_relative_to(library.parent) or evidence.is_relative_to(source.parent) + or source.parent.is_relative_to(evidence)): + raise ValueError('来源工程、目标库与操作证据必须使用独立目录。') + mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') + acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) + if not acquired: + return finish('BLOCKED', '已有 CBB 校验/管理/恢复任务运行。', False) + with LibraryLock(library, safe_to_release=lambda: owner_safe): + root = evidence/('management-' + uuid4().hex) + root.mkdir(parents=True, exist_ok=False) + report = root/'checkpoint.json' + work = library.parent/'ReusableBlocksLP'/request.name/'Work' + target_project, target_pcb = work/(request.name+'.prj'), work/'PCB/pcb.pcb' + if request.operation != 'add': + # Read actual engineering references when available; fallback + # paths still permit removal of catalog entries missing Work. + from .verify_runner import preflight + try: + target_project, target_pcb = preflight(VerifyRequest(library, request.name, root)) + except ValueError: + pass + idle_check(library, target_project, target_pcb) + if source: + idle_check(library, source, pcb) + checkpoint('catalog_before') + owner_safe = False + before = adapter.catalog(library, root/'before') + owner_safe = before.safe_to_continue + if not owner_safe or Path(before.library).resolve() != library or before.flow != 1: + return finish('BLOCKED', '原生目录身份/流程或所有者释放未确认。仅支持已验证 DxDesigner 流程。', owner_safe) + same_name = [entry for entry in before.entries if entry.name.casefold() == request.name.casefold()] + matches = [entry for entry in same_name if entry.partition == request.partition and entry.name == request.name] + if request.operation == 'add' and same_name: + return finish('UPLOAD_FAILED', '同名 CBB 已存在;新增禁止覆盖,请使用明确的同名更新。') + if request.operation != 'add' and len(matches) != 1: + return finish('BLOCKED', '目标名称和分区必须与原生目录唯一匹配。') + if hasattr(adapter, 'preflight'): + adapter.preflight(request, before) + # Native/project owners must be idle before snapshot or mutation. + idle_check(library, target_project, target_pcb) + scope = ManifestScope.library(request.name, library.name) + checkpoint('backup') + original = copy_verified(library.parent, root/'library-before', scope=scope, progress=progress) + _write_json(root/'library-before-manifest.json', original) + staged = request + identity = None + if source: + checkpoint('stage_source') + source_manifest = copy_verified(source.parent, root/'source', progress=progress) + _write_json(root/'source-manifest.json', source_manifest) + staged = replace(request, source=root/'source'/source.name, top_level_block=top) + identity = engineering_identity(staged.source) + if build_manifest(source.parent) != source_manifest: + raise OSError('来源工程在暂存期间变化。') + if build_manifest(library.parent, scope=scope) != original: + raise OSError('备份后中心库内容变化,停止写入。') + if request.operation in ('remove', 'update'): + checkpoint('native_remove') + mutated = True; owner_safe = False + native = adapter.remove(request, root/'remove') + owner_safe = native.safe_to_continue + if not owner_safe or not native.success: + return finish('RECOVERY_REQUIRED', native.detail, False) + owner_safe = False + idle_check(library, target_project, target_pcb) + owner_safe = False + after = adapter.catalog(library, root/'after-remove') + owner_safe = after.safe_to_continue + if (not owner_safe or Path(after.library).resolve() != library or after.flow != 1 + or any(e.name == request.name and e.partition == request.partition for e in after.entries)): + return finish('RECOVERY_REQUIRED', '原生移除后目录未确认目标缺失。', False) + removed = True + checkpoint('removed') + if request.operation == 'remove': + return finish('REMOVED', '已备份并由原生操作移除,重新读取目录确认不存在。') + checkpoint('native_add') + mutated = True; owner_safe = False + native = adapter.add(staged, root/'add') + owner_safe = native.safe_to_continue + if not owner_safe or not native.success: + return finish('RECOVERY_REQUIRED' if removed or not owner_safe else 'UPLOAD_FAILED', + native.detail, owner_safe and not removed) + owner_safe = False + idle_check(library, target_project, target_pcb) + after = adapter.catalog(library, root/'after-add') + owner_safe = after.safe_to_continue + matches = [entry for entry in after.entries if entry.name == request.name + and entry.partition == request.partition] + if (not owner_safe or Path(after.library).resolve() != library or after.flow != 1 + or len(matches) != 1 or matches[0].engineering_identity != identity): + return finish('RECOVERY_REQUIRED', '原生新增后名称、分区或来源工程内容未确认一致。', False) + uploaded = True + checkpoint('uploaded') + if not request.auto_verify: + return finish('UPLOADED', '上传已确认;本批次未启用自动校验。') + # Adapter catalog cannot return until its exact native owner exits. + checkpoint('verify') + owner_safe = False + verified = verifier(VerifyRequest(library, request.name, root/'verify'), progress=progress) + owner_safe = verified.safe_to_continue + checkpoint('verified', verification=asdict(verified)) + good = verified.status == 'PASS' and verified.library_flag == 'GREEN' and owner_safe + return finish('UPLOADED_VERIFIED' if good else 'UPLOADED_VERIFY_FAILED', + verified.stderr or verified.library_flag_detail, owner_safe) + except Exception as error: + if uploaded: + return finish('UPLOADED_VERIFY_FAILED', str(error), False) + status = 'RECOVERY_REQUIRED' if mutated else ('UPLOAD_FAILED' if request.operation in ('add', 'update') else 'BLOCKED') + # Bad input is never described as a native upload attempt. + if root is None and isinstance(error, ValueError): + status = 'BLOCKED' + return finish(status, str(error), not mutated and owner_safe and not isinstance(error, LibraryBusy)) + finally: + if mutex is not None: + if acquired: + win32event.ReleaseMutex(mutex) + mutex.Close() + uninitialize_com(initialized) + + +def run_management_batch(requests, *, stop=None, progress=None, **kwargs): + results = [] + for request in requests: + if stop is not None and stop.is_set(): + break + result = run_management(request, progress=progress, **kwargs) + results.append(result) + if progress: + progress({'stage': result.status, 'block': result.name, 'detail': result.detail, + 'evidence_path': str(result.evidence_path or '')}) + if not result.safe_to_continue: + break + return results diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py new file mode 100644 index 0000000..2e305d8 --- /dev/null +++ b/src/cbb_refresh_verify/native_management.py @@ -0,0 +1,576 @@ +"""Version-pinned native adapter, private desktop transport and strict catalog. + +The installed Tcl CleanWGLib/F4 contract has isolated removal proof. Discovery +of an Add dialog is deliberately insufficient to enable production Add/Update. +""" +from dataclasses import asdict +import ctypes +import json +import os +from pathlib import Path +import re +import subprocess +import sys +import threading +import time +from uuid import uuid4 + +from .management import (Capability, Catalog, CatalogEntry, NativeOutcome, + engineering_identity, validate_name, _write_json) +from .scoped_manifest import hash_file + +_retained_workers = [] +_BINARY_PROOF = { + 'lm/win32/bin/CleanWGLib.exe': '7808ded326555f1fd5d5c14f68803dbb4f970d624f9ad6e1c7982dc9ed867917', + 'lm/win32/bin/LibraryManager.exe': 'a9c4c3d806794dbdc56d2bc4c637a1cf25e87e98644a7d5fd4eb0067ca5845e0', + 'common/win32/lib/LMCDLL70.dll': 'fc614d43ed0a2fd28dbe37116510e303fc0e71542da585ac03aaa147630629b8', + 'common/win32/lib/LMCDocAddin70.dll': 'ff022ce285544ddd7eb639e4eb90216899b63adabcef8b3448236b4916150154', +} + + +def removal_input(name, partition): + validate_name(name); validate_name(partition) + try: + return f'REUSE_BLOCK\t{partition}\t{name}\n'.encode('ascii') + except UnicodeEncodeError as error: + raise ValueError('已验证的 CleanWGLib 输入仅支持 ASCII 名称和分区。') from error + + +def removal_output_ok(code, output, name, partition): + return (code == 0 and '# Warning:' not in output and '# Error:' not in output + and re.search(r"# Note: Removed [^\r\n]+ '" + re.escape(name) + + r"' from partition '" + re.escape(partition) + r"'\.", output) is not None) + + +def catalog_dialog_action(title, text): + value = (title + '\n' + text).casefold() + if ('this central library must be updated' in value or 'select flow type' in value): + return 'cancel' + return 'observe' + + +class NativeManagementAdapter: + def __init__(self, *, mentor_home=None, worker_factory=None, timeout_seconds=180): + from .mentor_paths import discover_sdd_home + self.home = Path(mentor_home) if mentor_home is not None else discover_sdd_home() + self.worker_factory = worker_factory + self.timeout_seconds = timeout_seconds + + def capabilities(self): + reason = '' + for relative, expected in _BINARY_PROOF.items(): + path = self.home/relative if self.home else Path(relative) + try: + if not self.home or not path.is_file() or hash_file(path) != expected: + reason = f'{Path(relative).name} 缺失或与已隔离验证的 EE 7.9.5 版本不一致。' + break + except OSError as error: + reason = f'无法核对原生程序 {path}: {error}' + break + return { + 'remove': Capability(not reason, reason or '已验证 CleanWGLib/F4;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'), + 'add': Capability(False, '原生新增尚未完成隔离注册及工程内容复读验证;请先运行独立诊断。'), + 'update': Capability(False, '同名更新需要原生移除、重新导入及完整端到端隔离验证,目前不可用。'), + } + + def _worker(self, job, evidence): + from .desktop_worker import DesktopWorker + evidence = Path(evidence) + evidence.mkdir(parents=True, exist_ok=False) + request_path = evidence/'request.json' + desktop = 'CbbManagement_' + uuid4().hex[:12] + timeout = float(job.get('timeout_seconds', self.timeout_seconds)) + job = {**job, 'desktop': desktop, 'mentor_home': str(self.home or ''), + 'timeout_seconds': timeout, 'supervisor_pid': os.getpid()} + _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) + return {'success': False, 'safe_to_continue': False, + 'detail': f'TIMEOUT: 原生调用尚未返回,保留私有桌面 {desktop},请检查 {evidence}'} + try: + result = json.loads((evidence/'result.json').read_text(encoding='utf-8')) + if not isinstance(result.get('safe_to_continue'), bool): + raise ValueError('原生释放记录缺失') + return result + except (OSError, ValueError) as error: + return {'success': False, 'safe_to_continue': False, 'detail': str(error)} + finally: + worker.close() + + def catalog(self, library, evidence): + result = self._worker({'operation': 'catalog', 'library': str(library)}, evidence) + if not result.get('success') or not result.get('safe_to_continue'): + raise RuntimeError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。')) + return Catalog(Path(result['library']), int(result['flow']), + tuple(CatalogEntry(**entry) for entry in result['entries']), result['safe_to_continue']) + + def remove(self, request, evidence): + caps = self.capabilities() + if not caps['remove'].supported: + return NativeOutcome(False, True, caps['remove'].reason) + removal_input(request.name, request.partition) + if request.partition != 'Logical-Physical': + return NativeOutcome(False, True, '仅已验证 Logical-Physical 原生移除。') + result = self._worker({'operation': 'remove', 'library': str(request.library), + 'name': request.name, 'partition': request.partition, + 'timeout_seconds': request.timeout_seconds}, evidence) + return NativeOutcome(bool(result.get('success')), bool(result.get('safe_to_continue')), result.get('detail', '')) + + def preflight(self, request, catalog): + if request.operation in ('remove', 'update'): + removal_input(request.name, request.partition) + if request.partition != 'Logical-Physical' or catalog.flow != 1: + raise ValueError('仅验证了 DxDesigner / Logical-Physical 管理操作。') + + def add(self, request, evidence): + return NativeOutcome(False, True, self.capabilities()['add'].reason) + + +def prepare_probe_library(job, root): + """Diagnostic create is restricted to a new fixed descendant of this job.""" + expected = Path(root).resolve()/'fixture/mentor_lib.lmc' + if Path(job['library']).resolve() != expected or expected.parent.exists(): + raise ValueError('诊断只能创建当前任务下全新独占 fixture/mentor_lib.lmc。') + expected.parent.mkdir() + return expected + + +def validate_probe_readd(job, root): + expected = Path(root).parent.resolve()/'native-add/fixture/mentor_lib.lmc' + try: + owner = json.loads((Path(root).parent/'probe-owner.json').read_text(encoding='utf-8')) + except (OSError, ValueError) as error: + raise ValueError('没有本次原生创建的隔离库所有权记录。') from error + if (Path(job['library']).resolve() != expected or not expected.is_file() + or owner.get('library') != str(expected) or not job.get('probe_token') + or owner.get('token') != job['probe_token']): + raise ValueError('重新导入诊断仅允许本次原生新建的固定隔离库。') + return expected + + +def _close_probe_dialogs(desktop, owned_pids): + """Normal child-first close; never terminate a native owner.""" + import pythoncom, win32con, win32gui, win32process + for title, control in (('New Reusable Block', 1082), ('Reusable Blocks', 1052)): + for hwnd in desktop.EnumDesktopWindows(): + if (win32process.GetWindowThreadProcessId(hwnd)[1] in owned_pids + and win32gui.GetWindowText(hwnd) == title and win32gui.IsWindowVisible(hwnd)): + button = win32gui.GetDlgItem(hwnd, control) + if not button or not win32gui.IsWindowEnabled(button): + return + win32gui.PostMessage(button, win32con.BM_CLICK, 0, 0) + deadline = time.monotonic() + 3 + while win32gui.IsWindow(hwnd) and time.monotonic() < deadline: + pythoncom.PumpWaitingMessages(); time.sleep(.1) + if win32gui.IsWindow(hwnd): + return # Never close a parent with an unreleased child. + + +def _probe_add_dialog(app, job, desktop, owned_pids, root): + try: + return _probe_add_dialog_impl(app, job, desktop, owned_pids, root) + finally: + import gc + gc.collect() # Release UIA wrappers before closing native child dialogs. + _close_probe_dialogs(desktop, owned_pids) + + +def _probe_add_dialog_impl(app, job, desktop, owned_pids, root): + """Diagnostic only: proven editor/UIA tab and resource1103 controls. + + This function has no production capability override. It is called only + after this worker itself creates a new disposable library. + """ + import pythoncom, win32con, win32gui, win32process + from pywinauto import Desktop + from .backup_recovery import _align_library + from .management import source_project + source, pcb, top = source_project(job['source'], job.get('top_level_block', '')) + source_folder = 'source-replacement' if job['operation'] == 'probe_readd' else 'source' + if not source.is_relative_to(Path(root).parent.resolve()/source_folder): + raise ValueError('隔离新增只允许使用本次复制的来源工程。') + _align_library(source, pcb, Path(job['library'])) + validate_name(job['name']) + deadline = time.monotonic() + float(job['timeout_seconds']) + def find_window(title): + for hwnd in desktop.EnumDesktopWindows(): + if (win32process.GetWindowThreadProcessId(hwnd)[1] in owned_pids + and win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) == title): + return hwnd + return None + def await_window(title): + while time.monotonic() < deadline: + hwnd = find_window(title) + if hwnd: + return hwnd + pythoncom.PumpWaitingMessages(); time.sleep(.1) + raise TimeoutError(f'原生窗口未出现:{title}') + app.OnReusableBlocksEditor() + 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'] + if len(physical) != 1: + raise RuntimeError('无法唯一识别已验证的 Logical - Physical 页签。') + if not physical[0].is_selected(): + physical[0].select() + if not physical[0].is_selected(): + raise RuntimeError('Logical - Physical 页签未选中。') + # UIA objects hold COM references into Library Manager. Do not retain them + # during the native write or app.Quit / process-release observation. + physical = tabs = None + import gc + gc.collect() + new_button = win32gui.GetDlgItem(editor, 1066) + if not new_button or not win32gui.IsWindowEnabled(new_button): + raise RuntimeError('原生 New 按钮不可用。') + win32gui.PostMessage(new_button, win32con.BM_CLICK, 0, 0) + dialog = await_window('New Reusable Block') + fields = {1217: str(source), 1215: job['name'], 1218: top, 1216: 'Isolated CBB management validation'} + for control, value in fields.items(): + hwnd = win32gui.GetDlgItem(dialog, control) + if not hwnd or win32gui.GetClassName(hwnd) != 'Edit': + raise RuntimeError(f'原生新增字段身份不匹配:{control}') + win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, 0, value) + if win32gui.GetWindowText(hwnd) != value: + raise RuntimeError(f'原生新增字段回读失败:{control}') + _write_json(root/'submitted-fields.json', fields) + ok = win32gui.GetDlgItem(dialog, 1083) + if not ok: + raise RuntimeError('原生新增确认按钮缺失。') + win32gui.PostMessage(ok, win32con.BM_CLICK, 0, 0) + success = None + while time.monotonic() < deadline: + pythoncom.PumpWaitingMessages() + for hwnd in desktop.EnumDesktopWindows(): + if win32process.GetWindowThreadProcessId(hwnd)[1] not in owned_pids or not win32gui.IsWindowVisible(hwnd): + continue + children = [] + win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None) + text = '\n'.join(win32gui.GetWindowText(child) for child in children) + if 'New Logical-Physical Reuse Block was successfully added.' in text: + success = text + button = win32gui.GetDlgItem(hwnd, 2) or win32gui.GetDlgItem(hwnd, 1) + if button: + win32gui.PostMessage(button, win32con.BM_CLICK, 0, 0) + break + if success: + break + time.sleep(.1) + _write_json(root/'native-add-output.json', {'success_message': success}) + close = win32gui.GetDlgItem(editor, 1052) + if success and close: + win32gui.PostMessage(close, win32con.BM_CLICK, 0, 0) + for _ in range(20): + pythoncom.PumpWaitingMessages(); time.sleep(.1) + if not win32gui.IsWindow(editor): + break + if not success: + raise TimeoutError('未取得原生新增成功消息;保留副本与私有桌面证据。') + + +def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_source=None): + """Create a fresh native fixture and test Add without enabling production. + + Root must not exist. Source is copied completely before native access. A + successful command alone is not acceptance: close/reopen and byte identity + checks are recorded independently for human review. + """ + from .management import source_project, ManagementRequest + from .library_lock import LibraryLock + from .scoped_manifest import ManifestScope + from .scoped_manifest import copy_verified, build_manifest + import pythoncom, win32con, win32event + root = Path(root).resolve() + if root.exists(): + raise ValueError('诊断目录必须是全新目录,不能复用现有中心库或旧 fixture。') + source, _, top = source_project(source) + validate_name(name) + if root.is_relative_to(source.parent) or source.parent.is_relative_to(root): + raise ValueError('诊断目录不能与来源工程相互包含。') + mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') + acquired = False + token = uuid4().hex + record = {'operation': 'isolated_add_probe', 'source': str(source), 'name': name, + 'production_capability_enabled': False} + from .com_runtime import initialize_com, uninitialize_com + initialized = initialize_com() + try: + acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) + if not acquired: + raise RuntimeError('已有 CBB 校验/恢复/管理任务,请稍后运行隔离诊断。') + root.mkdir(parents=True, exist_ok=False) + manifest = copy_verified(source.parent, root/'source') + staged = root/'source'/source.name + identity = engineering_identity(staged) + adapter = NativeManagementAdapter(mentor_home=mentor_home) + if not adapter.capabilities()['remove'].supported: + raise RuntimeError('诊断要求已识别的 EE7.9.5 原生安装。') + library = root/'native-add/fixture/mentor_lib.lmc' + native = adapter._worker({'operation': 'probe_create_add', 'library': str(library), + 'source': str(staged), 'name': name, 'top_level_block': top, + 'probe_token': token}, root/'native-add') + record['native'] = native + record['source_unchanged'] = build_manifest(source.parent) == manifest + if native.get('success') and native.get('safe_to_continue'): + catalog = adapter.catalog(library, root/'reopen') + record['catalog'] = asdict(catalog) + matches = [entry for entry in catalog.entries if entry.name == name and entry.partition == 'Logical-Physical'] + record['identity_matches'] = len(matches) == 1 and matches[0].engineering_identity == identity + record['accepted'] = record['identity_matches'] and record['source_unchanged'] + else: + record['accepted'] = False + _write_json(root/'probe-result.json', record) + if record['accepted'] and replacement_source is not None: + replacement, _, replacement_top = source_project(replacement_source) + if replacement.is_relative_to(root) or root.is_relative_to(replacement.parent): + raise ValueError('更新诊断来源必须位于隔离库与证据目录之外。') + replacement_manifest = copy_verified(replacement.parent, root/'source-replacement') + staged_replacement = root/'source-replacement'/replacement.name + replacement_identity = engineering_identity(staged_replacement) + record['update'] = {'stage': 'backup', 'accepted': False, 'source': str(replacement)} + record['accepted'] = False + with LibraryLock(library): + copy_verified(library.parent, root/'library-before-update', + scope=ManifestScope.library(name, library.name)) + request = ManagementRequest('remove', library, name, evidence_dir=root) + removal = adapter.remove(request, root/'native-remove') + record['update']['removal'] = asdict(removal) + _write_json(root/'probe-result.json', record) + if not removal.success or not removal.safe_to_continue: + return record + missing = adapter.catalog(library, root/'after-remove') + if any(e.name == name for e in missing.entries): + record['update']['error'] = '原生移除后条目仍存在' + _write_json(root/'probe-result.json', record) + return record + record['update']['stage'] = 'removed' + _write_json(root/'probe-result.json', record) + readd = adapter._worker({'operation': 'probe_readd', 'library': str(library), + 'source': str(staged_replacement), 'name': name, 'top_level_block': replacement_top, + 'probe_token': token}, root/'native-readd') + record['update']['readd'] = readd + if readd.get('success') and readd.get('safe_to_continue'): + updated = adapter.catalog(library, root/'reopen-update') + record['update']['catalog'] = asdict(updated) + entries = [e for e in updated.entries if e.name == name and e.partition == 'Logical-Physical'] + record['update']['accepted'] = (len(entries) == 1 + and entries[0].engineering_identity == replacement_identity + and build_manifest(replacement.parent) == replacement_manifest) + record['update']['stage'] = 'complete' if record['update']['accepted'] else 'RECOVERY_REQUIRED' + record['accepted'] = record['update']['accepted'] + _write_json(root/'probe-result.json', record) + return record + except Exception as error: + record.update(accepted=False, error=str(error)) + if root.exists(): + _write_json(root/'probe-result.json', record) + raise + finally: + if acquired: + win32event.ReleaseMutex(mutex) + mutex.Close() + uninitialize_com(initialized) + + +def _current_desktop(): + import win32api, win32service + desktop = win32service.GetThreadDesktop(win32api.GetCurrentThreadId()) + user32 = ctypes.WinDLL('user32', use_last_error=True) + user32.GetUserObjectInformationW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, + ctypes.c_uint, ctypes.POINTER(ctypes.c_uint)] + buffer = ctypes.create_unicode_buffer(256) + needed = ctypes.c_uint() + if not user32.GetUserObjectInformationW(int(desktop), 2, buffer, ctypes.sizeof(buffer), ctypes.byref(needed)): + raise OSError('无法确认工作进程桌面身份') + return desktop, buffer.value + + +def _wait_native_exit(pids, timeout=10): + """Observe exact process handles after COM teardown, without COM/WMI.""" + import pywintypes, win32api, win32con, win32event + deadline = time.monotonic() + timeout + for pid in pids: + try: + handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid)) + except pywintypes.error as error: + if error.winerror == 87: # Process no longer exists. + continue + return False + try: + remaining = max(0, int((deadline - time.monotonic()) * 1000)) + if win32event.WaitForSingleObject(handle, remaining) != win32con.WAIT_OBJECT_0: + return False + finally: + handle.Close() + return True + + +def _catalog_native(job, root): + import gc, pythoncom + from .com_runtime import initialize_com, uninitialize_com + initialized = initialize_com() + try: + result = _catalog_com(job, root) + finally: + gc.collect() + uninitialize_com(initialized) + # A failed COM activation can have created an unaccounted server. An empty + # PID list is missing release evidence, not proof that nothing was launched. + result['safe_to_continue'] = bool(result.get('native_pids')) and _wait_native_exit(result['native_pids']) + return result + + +def _catalog_com(job, root): + import pythoncom, win32com.client, win32con, win32gui, win32process + from .mentor_adapter import _process_details + desktop, name = _current_desktop() + if name != job['desktop'] or not name.startswith('CbbManagement_'): + raise RuntimeError('拒绝在非本次私有桌面读取原生目录。') + if _process_details('LibraryManager.exe'): + raise RuntimeError('已有 Library Manager 运行,请关闭后重试。') + stop = threading.Event() + refused = [] + observed = [] + owned_pids = set() + def observe_dialogs(): + while not stop.wait(.1): + for hwnd in desktop.EnumDesktopWindows(): + try: + pid = win32process.GetWindowThreadProcessId(hwnd)[1] + if pid not in owned_pids or not win32gui.IsWindowVisible(hwnd): + continue + children = [] + win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None) + title = win32gui.GetWindowText(hwnd) + text = '\n'.join(win32gui.GetWindowText(child) for child in children) + if catalog_dialog_action(title, text) == 'cancel': + if hwnd not in observed: + observed.append(hwnd) + refused.append({'title': title, 'text': text}) + _write_json(root/'refused-dialogs.json', refused) + cancel = win32gui.GetDlgItem(hwnd, 2) + if cancel: + win32gui.PostMessage(cancel, win32con.BM_CLICK, 0, 0) + except Exception: + continue + app = library = None + result = {'success': False, 'safe_to_continue': False} + watcher = None + try: + app = win32com.client.DispatchEx('LibraryManager.Application') + app.Visible = True + processes = _process_details('LibraryManager.exe') + owned_pids.update(int(p[0]) for p in processes) + if len(owned_pids) != 1: + raise RuntimeError('无法唯一确认本次 Library Manager 进程。') + expected_binary = Path(job['mentor_home'])/'lm/win32/bin/LibraryManager.exe' + if Path(processes[0][1]).resolve() != expected_binary.resolve(): + raise RuntimeError('COM 注册指向不同的 Library Manager 安装,拒绝打开中心库。') + watcher = threading.Thread(target=observe_dialogs, daemon=True) + watcher.start() + if job['operation'] == 'probe_create_add': + target = prepare_probe_library(job, root) + library = app.CreateLibrary(str(target), 1) + _write_json(root.parent/'probe-owner.json', {'library': str(target), 'token': job['probe_token']}) + else: + if job['operation'] == 'probe_readd': + validate_probe_readd(job, root) + library = app.OpenLibrary(str(Path(job['library']).resolve())) + if refused: + raise RuntimeError('目录读取需要升级中心库或选择流程;已拒绝,目标未获准自动升级。') + if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve(): + raise RuntimeError('原生返回中心库身份不一致。') + if job['operation'] in ('probe_create_add', 'probe_readd'): + _probe_add_dialog(app, job, desktop, owned_pids, root) + entries = [] + parts = library.Partitions(7) + for i in range(1, int(parts.Count) + 1): + partition = parts.Item(i) + for j in range(1, int(partition.Count) + 1): + item = partition.Item(j) + block_name, partition_name = str(item.Name), str(item.PartitionName) + validate_name(block_name) + identity = {} + work = Path(job['library']).parent/'ReusableBlocksLP'/block_name/'Work' + projects = list(work.glob('*.prj')) + if len(projects) == 1: + try: + identity = engineering_identity(projects[0]) + except (ValueError, OSError): + pass + entries.append(asdict(CatalogEntry(block_name, partition_name, identity))) + item = None + partition = None + parts = None + result.update(success=True, library=str(library.FullName), flow=int(library.FlowType), entries=entries) + except Exception as error: + result['detail'] = str(error) + finally: + if library is not None: + try: library.Close() + except Exception as error: result['close_detail'] = str(error) + library = None + if app is not None: + try: app.Quit() + except Exception as error: result['quit_detail'] = str(error) + app = None + stop.set() + if watcher: + watcher.join(1) + result['native_pids'] = sorted(owned_pids) + if refused: + result.update(success=False, detail='拒绝目录读取触发的中心库升级/流程选择。') + return result + + +def management_worker_main(request_path): + """Internal worker. Desktop identity and capability gates are rechecked here.""" + request_path = Path(request_path) + root = request_path.parent + job = json.loads(request_path.read_text(encoding='utf-8')) + result = {'success': False, 'safe_to_continue': False} + try: + _, desktop = _current_desktop() + if desktop != job['desktop'] or not desktop.startswith('CbbManagement_'): + raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。') + if job['operation'] in ('catalog', 'probe_create_add', 'probe_readd'): + result = _catalog_native(job, root) + elif job['operation'] == 'remove': + adapter = NativeManagementAdapter(mentor_home=job['mentor_home']) + cap = adapter.capabilities()['remove'] + if not cap.supported: + raise RuntimeError(cap.reason) + if job['partition'] != 'Logical-Physical': + raise ValueError('未验证的分区类型') + input_path = root/'remove-list.txt' + input_path.write_bytes(removal_input(job['name'], job['partition'])) + command = [str(adapter.home/'lm/win32/bin/CleanWGLib.exe'), str(job['library']), 'F4', str(input_path)] + _write_json(root/'command.json', command) + with (root/'native-output.txt').open('wb') as output: + process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT, + creationflags=subprocess.CREATE_NO_WINDOW) + _write_json(root/'native-process.json', {'pid': process.pid, 'command': command}) + # Never terminate this native database owner on timeout. + try: + code = process.wait(timeout=float(job['timeout_seconds'])) + except subprocess.TimeoutExpired: + result.update(detail=f'TIMEOUT: CleanWGLib PID {process.pid} 尚未释放') + else: + text = (root/'native-output.txt').read_bytes().decode('mbcs', errors='replace') + result.update(success=removal_output_ok(code, text, job['name'], job['partition']), + safe_to_continue=True, detail=text, exit_code=code) + else: + raise ValueError('未验证的原生管理操作') + except Exception as error: + result['detail'] = str(error) + finally: + _write_json(root/'result.json', result) + return 0 if result['success'] and result['safe_to_continue'] else 2 diff --git a/src/cbb_refresh_verify/scoped_manifest.py b/src/cbb_refresh_verify/scoped_manifest.py index 402fda3..6f8286b 100644 --- a/src/cbb_refresh_verify/scoped_manifest.py +++ b/src/cbb_refresh_verify/scoped_manifest.py @@ -131,6 +131,11 @@ def hash_file(path): return digest.hexdigest() +def _coordination_checkpoint(path): + from .library_lock import checkpoint_record + return checkpoint_record(path) is not None + + def scan_tree(root, *, scope=None, exclude=None): """Refuse links/special files; optionally prune trees outside semantic scope. @@ -153,7 +158,7 @@ def scan_tree(root, *, scope=None, exclude=None): if entry.is_dir(follow_symlinks=False): if not scope.includes_directory(relative): continue - elif scope.classify(relative) is None: + elif scope.classify(relative) is None or _coordination_checkpoint(path): continue metadata = entry.stat(follow_symlinks=False) if metadata.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: @@ -241,6 +246,11 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi expected_metadata = _metadata_record(source, entries) destination.mkdir(parents=True, exist_ok=False) files = [path for path, info in entries.items() if stat.S_ISREG(info.st_mode)] + from .library_lock import owned_snapshot + # Resolve ownership on the operation thread, before parallel copy workers. + owned = {path: data for path in files + if path.name.endswith('.cbb-upload.lock') + and (data := owned_snapshot(path)) is not None} record = {} hashed_count = hashed_bytes = 0 _emit(progress, 'copy', 0, 0, copied_files=0, total_files=len(files)) @@ -260,9 +270,14 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi relative = path.relative_to(source) copied = destination/relative copied.parent.mkdir(parents=True, exist_ok=True) - label = scope.classify(relative) + label = None if _coordination_checkpoint(path) else scope.classify(relative) digest = None - if path in locks: + if path in owned: + copied.write_bytes(owned[path]) + shutil.copystat(path, copied) + if label is not None: + digest = hashlib.sha256(owned[path]).hexdigest() + elif path in locks: stream_hash = hashlib.sha256() if label is not None else None with copied.open('wb') as stream: while True: diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index f8a51d5..0e7df00 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -46,7 +46,8 @@ def application_icon_path(): class MainWindow(QWidget): def __init__(self, reader=None, verifier=None, logging_context=None, data_root=None, - authoritative_status=True, path_lease_factory=None, status_refresher=None): + authoritative_status=True, path_lease_factory=None, status_refresher=None, + management_adapter=None): super().__init__() self.setWindowTitle('CBB 刷新校验') icon_path = application_icon_path() @@ -59,6 +60,10 @@ class MainWindow(QWidget): self.library_lease = None self.mentor_lmc_path = None self.data_root = Path(data_root or (logging_context.root if logging_context else Path(os.environ.get('LOCALAPPDATA', '.'))/'CbbRefreshVerify')) + from cbb_refresh_verify.native_management import NativeManagementAdapter + self.management_adapter = management_adapter or NativeManagementAdapter() + self.management_capabilities = self.management_adapter.capabilities() + self._management_unsafe = False if reader is not None: self.reader = reader elif authoritative_status and os.environ.get('CBB_STATUS_BRIDGE_MODE', 'authoritative').casefold() != 'legacy': @@ -189,12 +194,36 @@ class MainWindow(QWidget): maintenance_layout.addWidget(self.icdb_progress) maintenance_layout.addWidget(self.icdb_phase) layout.addRow(maintenance) + management = QGroupBox('CBB 管理(可处理 READY 项)') + management_layout = QVBoxLayout(management) + management_buttons = QHBoxLayout() + self.add_button = QPushButton('新增 CBB…') + self.update_button = QPushButton('同名更新已选…') + self.remove_button = QPushButton('移除已选…') + for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): + widget.clicked.connect(lambda _=False, op=operation: self.manage_cbb(op)) + widget.setToolTip(self.management_capabilities[operation].reason) + management_buttons.addWidget(widget) + management_layout.addLayout(management_buttons) + self.management_capability_label = QLabel('\n'.join( + f'{label}:{self.management_capabilities[op].reason}' + for op, label in (('add', '新增'), ('update', '同名更新'), ('remove', '移除')) + if not self.management_capabilities[op].supported)) + self.management_capability_label.setWordWrap(True) + management_layout.addWidget(self.management_capability_label) + self.unfinished_label = QLabel(); self.unfinished_label.setWordWrap(True) + management_layout.addWidget(self.unfinished_label) + self.release_library_button = QPushButton('检查会话释放并解除协调阻止…') + self.release_library_button.clicked.connect(self.check_library_release) + management_layout.addWidget(self.release_library_button) + layout.addRow(management) layout.addRow(self.status_label); layout.addRow(self.output) layout.addRow(self.recovery_button) self.lmc_edit.textChanged.connect(self._invalidate) for field in (self.libspec_edit, self.dms_edit, self.mentor_bin_edit): field.textChanged.connect(lambda _text: self._update()) self.native_lm_verify_check.toggled.connect(lambda _checked: self._update()) + self._show_unfinished_management() self._update() def _invalidate(self): @@ -219,11 +248,16 @@ class MainWindow(QWidget): def _update(self): # The refresh flow needs only the library and a CBB; LibSpec/DMS config # are optional and only enable the extra contents check. - ready = not self.busy and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 + ready = not self.busy and not self._management_unsafe and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 self.batch_button.setEnabled(ready and bool(self._eligible_names())) self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True))) self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names())) self.backup_restore_button.setEnabled(ready and bool(self._maintenance_names())) + self.release_library_button.setEnabled(not self.busy and bool(self.lmc_edit.text().strip())) + loaded = not self.busy and not self._management_unsafe and self.loaded_path == self.lmc_edit.text() + for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): + widget.setEnabled(loaded and self.management_capabilities[operation].supported + and (operation == 'add' or bool(self.selected_records()))) for widget in (self.lmc_edit, self.browse_button, self.load_button, self.cbb_table, self.timeout_spin, self.advanced_toggle, self.advanced_panel, self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button): @@ -233,6 +267,78 @@ class MainWindow(QWidget): f'READY {counts["ready"]} · 异常/待确认 {counts["error"]}') self.stop_button.setEnabled(self.busy and self.loaded_path is not None and not self.stop.is_set()) + def _show_unfinished_management(self): + from cbb_refresh_verify.management import unfinished_operations + unfinished = unfinished_operations(self.data_root/'management') + self.unfinished_label.setText(f'发现 {len(unfinished)} 项未完成或需恢复的管理任务;只显示记录,不自动重放。' + if unfinished else '') + for item in unfinished: + self.output.append(f'未完成:{item.get("name", "")} {item.get("status", "")} ' + f'{item.get("stage", "")}\n{item["evidence_path"]}') + + def check_library_release(self): + if self.busy or not self.lmc_edit.text().strip(): + return + if QMessageBox.question(self, '检查原生会话释放', + '检查所有原生编辑器/管理进程、库内数据库地址记录及数据库独占可读性。' + '\n全部通过后归档阻止记录并释放本应用保留的库锁。' + '\n数据恢复结果和未完成管理任务仍需单独审查。继续?', + QMessageBox.Yes | QMessageBox.No, QMessageBox.No) != QMessageBox.Yes: + return + from cbb_refresh_verify.library_lock import release_retained_library + library = Path(self.mentor_lmc_path or self.lmc_edit.text().strip()) + if library.is_dir(): + library = select_library_file(library) + def completed(result): + self._management_unsafe = False + message = f'原生会话释放检查通过。请审查未完成任务后重新读取中心库。\n{result["evidence_path"]}' + self.status_label.setText(message); self.output.append(message) + self._start(lambda: release_retained_library(library, self.data_root/'release-checks'), completed) + + def manage_cbb(self, operation): + if (self.busy or self._management_unsafe or self.loaded_path != self.lmc_edit.text() + or not self.management_capabilities[operation].supported): + return + from cbb_refresh_verify.management import ManagementRequest + from .management_dialog import ManagementDialog + records = self.selected_records() + if operation != 'add' and not records: + return + def request(name, partition): + return ManagementRequest(operation, Path(self.mentor_lmc_path or self.loaded_path), name, + partition, evidence_dir=self.data_root/'management', timeout_seconds=self.timeout_spin.value()) + requests = ([request('', 'Logical-Physical')] if operation == 'add' else + [request(record.name, 'Logical-Physical' if record.logical_type == 'LP' + and record.partition_name in ('Reusable Blocks', 'LP') else record.partition_name) + for record in records]) + self.management_dialog = ManagementDialog(requests, self, adapter=self.management_adapter) + self._management_results = [] + self.management_dialog.completed.connect(self._management_completed) + self.management_dialog.finished.connect(self._management_closed) + self.busy = True; self._update() + self.management_dialog.show() + + def _management_completed(self, results): + self._management_results = results + from .management_dialog import RESULT_LABELS + for result in results: + self.output.append(f'{result.name}:{RESULT_LABELS.get(result.status, result.status)} ' + f'{result.detail}\n{result.evidence_path or ""}') + if any(not result.safe_to_continue for result in results): + self._management_unsafe = True + if self.library_lease: + self.library_lease.retain_for_recovery() + self.status_label.setText('管理操作需要人工确认恢复/释放;已停止写入,未再次读取原生目录。') + self._show_unfinished_management() + + def _management_closed(self, *_): + from PySide6.QtCore import QTimer + self.busy = False; self._update() + if self._management_results and not self._management_unsafe: + # Whole catalog reload is necessary for inserted/deleted identities; + # existing local search/filter controls are intentionally retained. + QTimer.singleShot(0, self.load_library) + def visible_records(self): """Cached records in table order, restricted to the current local view.""" return tuple(record for name, record in self._records_by_name.items() @@ -326,7 +432,12 @@ class MainWindow(QWidget): 'library_path_lease display=%s mentor=%s drive=%s remote=%s created=%s', lease.display_path, mentor_path, lease.drive or '-', lease.remote_root or '-', lease.created) - result = self.reader(mentor_path) + from cbb_refresh_verify.library_lock import has_blocker + if has_blocker(mentor_path): + from cbb_refresh_verify.models import LibraryReadResult + result = LibraryReadResult((), '此库有未确认的原生会话,请先执行“释放检查并解除协调阻止”。') + else: + result = self.reader(mentor_path) if self.logging_context: statuses = Counter(record.status for record in result.records) unknown = [record.name for record in result.records if record.status in {'UNKNOWN', 'UNAVAILABLE'}] diff --git a/src/cbb_refresh_verify/ui/management_dialog.py b/src/cbb_refresh_verify/ui/management_dialog.py new file mode 100644 index 0000000..0f9d92e --- /dev/null +++ b/src/cbb_refresh_verify/ui/management_dialog.py @@ -0,0 +1,188 @@ +"""Explicit preview and asynchronous, checkpointed CBB management batch.""" +from dataclasses import replace +from pathlib import Path +from threading import Event + +from PySide6.QtCore import QObject, QThreadPool, Signal, Qt +from PySide6.QtWidgets import (QCheckBox, QDialog, QFileDialog, QHBoxLayout, QLabel, + QMessageBox, QPushButton, QTableWidget, QTableWidgetItem, QTextEdit, QVBoxLayout) + +from .workers import ServiceWorker +from ..management import run_management_batch, validate_name + +RESULT_LABELS = { + 'REMOVED': '已移除', 'UPLOADED': '已上传 / 未自动校验', + 'UPLOADED_VERIFIED': '已上传且校验通过', 'UPLOADED_VERIFY_FAILED': '已上传但校验未通过', + 'UPLOAD_FAILED': '上传失败', 'RECOVERY_REQUIRED': '需要人工恢复 / 队列已停止', + 'BLOCKED': '操作被阻止', 'UNSUPPORTED': '当前环境不支持', +} +STAGE_LABELS = { + 'catalog_before': '正在核对原生目录', 'backup': '正在备份完整中心库', + 'stage_source': '正在复制并检查来源工程', 'native_remove': '正在原生移除', + 'removed': '原条目已移除', 'native_add': '正在原生上传', 'uploaded': '上传身份已确认', + 'verify': '正在校验上传结果', 'verified': '校验已返回', 'copy': '正在复制备份', +} + + +class ManagementSignals(QObject): + progress = Signal(object) + + +class ManagementDialog(QDialog): + completed = Signal(object) + + def __init__(self, requests, parent=None, *, adapter=None, runner=None): + super().__init__(parent) + self.templates = tuple(requests) + self.template = self.templates[0] + self.operation = self.template.operation + self.adapter = adapter + self.runner = runner or run_management_batch + self.busy = False + self.executed = False + self.stop = Event() + self.pool = QThreadPool(self); self.pool.setMaxThreadCount(1) + self.signals = ManagementSignals(self) + self.signals.progress.connect(self._progress) + self.setWindowTitle({'add': '新增 CBB', 'update': '同名更新 CBB', 'remove': '移除 CBB'}[self.operation]) + self.resize(850, 450) + self.table = QTableWidget(0, 4) + self.table.setHorizontalHeaderLabels(['CBB 名称', '分区', '来源 PRJ(完整工程)', '顶层块(空白时读取工程)']) + self.table.horizontalHeader().setStretchLastSection(True) + self.table.setColumnWidth(2, 300) + self.sources_button = QPushButton('选择来源工程…') + self.sources_button.setVisible(self.operation != 'remove') + self.sources_button.clicked.connect(self._browse) + self.auto_verify = QCheckBox('本批次上传后自动校验') + self.auto_verify.setChecked(self.template.auto_verify) + self.auto_verify.setVisible(self.operation != 'remove') + self.status_label = QLabel('先预览目标和来源;完整备份保存在目标库之外。更新会先移除再重新导入,失败时保留恢复证据。') + self.status_label.setWordWrap(True) + self.output = QTextEdit(); self.output.setReadOnly(True) + self.execute_button = QPushButton('预览并确认执行…') + self.execute_button.clicked.connect(self._execute) + self.stop_button = QPushButton('当前项结束后停止'); self.stop_button.setEnabled(False) + self.stop_button.clicked.connect(self.stop.set) + self.close_button = QPushButton('关闭'); self.close_button.clicked.connect(self.reject) + layout = QVBoxLayout(self) + layout.addWidget(QLabel(f'目标库:{self.template.library}\n备份/证据:{self.template.evidence_dir}')) + layout.addWidget(self.table) + layout.addWidget(self.sources_button); layout.addWidget(self.auto_verify) + layout.addWidget(self.status_label); layout.addWidget(self.output) + buttons = QHBoxLayout() + for widget in (self.execute_button, self.stop_button, self.close_button): buttons.addWidget(widget) + layout.addLayout(buttons) + for request in self.templates: + if request.name: + self._append(request) + + def _append(self, request): + row = self.table.rowCount(); self.table.insertRow(row) + for column, value in enumerate((request.name, request.partition, str(request.source or ''), request.top_level_block)): + item = QTableWidgetItem(value) + if column == 1 or column == 0 and self.operation != 'add' or self.operation == 'remove': + item.setFlags(item.flags() & ~Qt.ItemIsEditable) + self.table.setItem(row, column, item) + + def add_sources(self, sources): + if self.operation == 'add': + for source in sources: + source = Path(source) + self._append(replace(self.template, name=source.stem, source=source)) + elif self.operation == 'update' and sources: + row = max(0, self.table.currentRow()) + self.table.item(row, 2).setText(str(sources[0])) + + def _browse(self): + if self.operation == 'add': + paths, _ = QFileDialog.getOpenFileNames(self, '选择完整来源工程', '', '工程 (*.prj)') + else: + path, _ = QFileDialog.getOpenFileName(self, '为当前行选择同名更新来源', '', '工程 (*.prj)') + paths = [path] if path else [] + self.add_sources(paths) + + def requests(self): + result = [] + for row in range(self.table.rowCount()): + name, partition, source, top = (self.table.item(row, i).text().strip() for i in range(4)) + result.append(replace(self.template, name=name, partition=partition, + source=Path(source) if source else None, top_level_block=top, + auto_verify=self.operation != 'remove' and self.auto_verify.isChecked())) + return result + + def _execute(self): + if self.busy or self.executed: + return + requests = self.requests() + try: + if not requests: + raise ValueError('请先选择来源工程。') + for request in requests: + validate_name(request.name) + if request.operation != 'remove' and (request.source is None or not request.source.is_file()): + raise ValueError(f'{request.name}:请选择完整来源 PRJ。') + if len({(r.partition.casefold(), r.name.casefold()) for r in requests}) != len(requests): + raise ValueError('批次包含重复目标名称。') + except ValueError as error: + self.status_label.setText(str(error)); return + preview = '\n'.join(f'{r.partition} / {r.name}' + (f' ← {r.source}' if r.source else '') for r in requests) + description = ('同名更新先备份,再原生移除并重新导入;导入失败需要人工恢复。' if self.operation == 'update' + else '移除前保留完整工程和中心库备份。' if self.operation == 'remove' + else '新增同名冲突会拒绝覆盖,完整来源工程将先复制到暂存区。') + answer = QMessageBox.question(self, '确认管理操作', + f'{description}\n\n目标库:{self.template.library}\n{preview}\n\n' + f'完整备份与检查点:{self.template.evidence_dir}\n确认执行?', QMessageBox.Yes | QMessageBox.No, + QMessageBox.No) + if answer != QMessageBox.Yes: + return + self.busy = True; self.executed = True + self._enabled(False) + self.worker = ServiceWorker(lambda: self.runner(requests, adapter=self.adapter, stop=self.stop, + progress=self.signals.progress.emit)) + self.worker.signals.succeeded.connect(self._result) + self.worker.signals.failed.connect(self._failed) + self.worker.signals.finished.connect(self._finished) + self.pool.start(self.worker) + + def _enabled(self, enabled): + for widget in (self.table, self.sources_button, self.auto_verify, self.close_button): + widget.setEnabled(enabled) + self.execute_button.setEnabled(enabled and not self.executed) + self.stop_button.setEnabled(not enabled) + + def _progress(self, event): + stage = str(event.get('stage', '')) + text = f'{event.get("block", "")} {STAGE_LABELS.get(stage, RESULT_LABELS.get(stage, stage))}' + if 'copied_files' in event: + text += f' {event["copied_files"]}/{event.get("total_files", "?")}' + self.status_label.setText(text) + + def _result(self, results): + lines = [] + for result in results: + text = f'{result.name}:{RESULT_LABELS.get(result.status, result.status)} — {result.detail}' + if result.evidence_path: + text += f'\n记录:{result.evidence_path}' + lines.append(text) + message = '\n'.join(lines) if lines else '队列已取消,未执行任何项。' + self.status_label.setText(message); self.output.append(message) + self.completed.emit(results) + + def _failed(self, error): + from ..management import ManagementResult + request = self.template + self._result([ManagementResult(request.operation, request.library, request.name, request.partition, + request.source, request.auto_verify, 'complete', 'RECOVERY_REQUIRED', False, str(error))]) + + def _finished(self): + self.busy = False; self._enabled(True) + + def reject(self): + if not self.busy: + super().reject() + + def closeEvent(self, event): + if self.busy: + self.stop.set(); event.ignore() + else: + super().closeEvent(event) diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 6b370bc..561592f 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -506,14 +506,26 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, # Cover contents checking, native work and final state reads with one # session-wide mutex. The worker-stage lock below is reentrant on Windows. import win32event, win32con + from .com_runtime import initialize_com, uninitialize_com + initialized = initialize_com() mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') acquired = False + library_lock = None + release_confirmed = True try: acquired = win32event.WaitForSingleObject(mutex, 0) in ( win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) if not acquired: return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。', safe_to_continue=False) + from .library_lock import LibraryLock, LibraryBusy + if Path(request.lmc_file).is_file(): + try: + library_lock = LibraryLock(request.lmc_file, safe_to_release=lambda: release_confirmed) + library_lock.__enter__() + except LibraryBusy as error: + return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False) + release_confirmed = False result = _run_verify_serialized( request, progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, @@ -595,11 +607,17 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, ensure_ascii=False, default=str, indent=2), encoding='utf-8') except OSError: pass # Keep the native/repair errors even if evidence storage fails. + release_confirmed = result.safe_to_continue return result finally: - if acquired: - win32event.ReleaseMutex(mutex) - mutex.Close() + try: + if library_lock is not None: + library_lock.__exit__(None, None, None) + finally: + if acquired: + win32event.ReleaseMutex(mutex) + mutex.Close() + uninitialize_com(initialized) def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_runner=None, diff --git a/tests/test_library_lock.py b/tests/test_library_lock.py new file mode 100644 index 0000000..eeb01f9 --- /dev/null +++ b/tests/test_library_lock.py @@ -0,0 +1,209 @@ +from concurrent.futures import ThreadPoolExecutor + +import pytest +import win32con +import win32file + + +def test_library_lock_excludes_other_thread_and_uploader_but_reenters(tmp_path): + from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy + library = tmp_path/'test.lmc' + library.write_bytes(b'fixture') + with LibraryLock(library) as lock: + assert lock.path.name == '.test.cbb-upload.lock' + with LibraryLock(library): + assert lock.snapshot_bytes() + with pytest.raises(Exception): + win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, + 0, None, win32con.OPEN_ALWAYS, 0, None) + def competing(): + with pytest.raises(LibraryBusy): + with LibraryLock(library): + pass + with ThreadPoolExecutor(1) as pool: + pool.submit(competing).result() + assert lock.path.exists() + with LibraryLock(library): + pass + + +def test_complete_copy_reads_owned_lock_without_reopening(tmp_path): + from cbb_refresh_verify.library_lock import LibraryLock + from cbb_refresh_verify.scoped_manifest import copy_verified + source = tmp_path/'library' + source.mkdir() + library = source/'test.lmc' + library.write_bytes(b'fixture') + with LibraryLock(library) as lock: + expected = lock.snapshot_bytes() + copy_verified(source, tmp_path/'snapshot') + assert (tmp_path/'snapshot'/lock.path.name).read_bytes() == expected + + +def test_external_lock_never_gets_snapshot_exception(tmp_path): + from cbb_refresh_verify.scoped_manifest import copy_verified + source = tmp_path/'library' + source.mkdir() + path = source/'.test.cbb-upload.lock' + path.write_bytes(b'external') + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, + 0, None, win32con.OPEN_EXISTING, 0, None) + try: + with pytest.raises(OSError): + copy_verified(source, tmp_path/'snapshot') + finally: + handle.Close() + + +def test_verification_refuses_uploader_exclusive_lock_before_native_work(tmp_path, monkeypatch): + import cbb_refresh_verify.verify_runner as runner + library = tmp_path/'lib.lmc' + library.touch() + lock = tmp_path/'.lib.cbb-upload.lock' + handle = win32file.CreateFile(str(lock), win32con.GENERIC_READ | win32con.GENERIC_WRITE, + 0, None, win32con.OPEN_ALWAYS, 0, None) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('native work bypassed lock')) + try: + result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) + assert result.status == 'BLOCKED' and not result.safe_to_continue + finally: + handle.Close() + + +def test_unsafe_verify_retains_lease_and_persistent_blocker(tmp_path, monkeypatch): + import cbb_refresh_verify.verify_runner as runner + import cbb_refresh_verify.library_lock as locks + library = tmp_path/'lib.lmc'; library.touch() + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: + runner.VerifyResult('FAIL', safe_to_continue=False)) + result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) + assert not result.safe_to_continue + assert locks.blocker_path(library).is_file() + with pytest.raises(locks.LibraryBusy): + with locks.LibraryLock(library): pass + # Simulate OS handle loss on process exit; persistent marker still blocks. + with locks._guard: + owner = locks._owners.pop(str((tmp_path/'.lib.cbb-upload.lock').resolve()).casefold(), None) + if owner: owner['handle'].Close() + with pytest.raises(locks.LibraryBusy): + with locks.LibraryLock(library): pass + + +def test_explicit_release_requires_fresh_proof_and_never_deletes_sidecar(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() + with locks.LibraryLock(library, safe_to_release=lambda: False) as lease: + pass + try: + def denied(*a): + raise ValueError('native owner remains') + monkeypatch.setattr(locks, '_prove_library_released', denied) + with pytest.raises(ValueError, match='native owner'): + locks.release_retained_library(library, tmp_path/'release') + assert locks.blocker_path(library).is_file() + from contextlib import contextmanager + @contextmanager + def proof(*a): + yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} + monkeypatch.setattr(locks, '_prove_library_released', proof) + result = locks.release_retained_library(library, tmp_path/'release') + assert result['status'] == 'RELEASE_CONFIRMED' + assert lease.path.is_file() and not locks.blocker_path(library).exists() + with locks.LibraryLock(library): pass + finally: + with locks._guard: + owner = locks._owners.pop(str(lease.path.resolve()).casefold(), None) + if owner: owner['handle'].Close() + + +def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + import win32com.client + from types import SimpleNamespace + monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=lambda *_: [])) + library = tmp_path/'lib.lmc'; library.touch() + address = tmp_path/'database/cdbsvr/sAddress.adr'; address.parent.mkdir(parents=True) + address.touch() + with pytest.raises(locks.LibraryBusy): + with locks._prove_library_released(library): pass + address.unlink() # Test-created empty marker, never a native address file. + handle = win32file.CreateFile(str(library), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, 0, None) + try: + with pytest.raises(Exception): + with locks._prove_library_released(library): pass + finally: + handle.Close() + with locks._prove_library_released(library) as proof: + assert proof['exclusive_files'] == [str(library)] + + +def test_public_verify_initializes_com_in_its_operation_thread(tmp_path, monkeypatch): + import ctypes + import cbb_refresh_verify.verify_runner as runner + library = tmp_path/'lib.lmc'; library.touch() + def serialized(*a, **k): + apartment, qualifier = ctypes.c_int(), ctypes.c_int() + ole32 = ctypes.WinDLL('ole32') + ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)] + initialized = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0 + return runner.VerifyResult('PASS' if initialized else 'FAIL', stderr='COM apartment check') + monkeypatch.setattr(runner, '_run_verify_serialized', serialized) + with ThreadPoolExecutor(1) as pool: + result = pool.submit(runner.run_verify, runner.VerifyRequest(library, 'Block', tmp_path/'out')).result() + assert result.status == 'PASS' + + +@pytest.mark.parametrize('mode,expected', [(0, 1), (2, 0)]) +def test_public_verify_preserves_existing_apartment(tmp_path, monkeypatch, mode, expected): + import ctypes, pythoncom + import cbb_refresh_verify.verify_runner as runner + library = tmp_path/'lib.lmc'; library.touch() + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('PASS')) + def existing_mta(): + pythoncom.CoInitializeEx(mode) + try: + runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) + apartment, qualifier = ctypes.c_int(), ctypes.c_int() + ole32 = ctypes.WinDLL('ole32') + ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)] + result = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) + return result, apartment.value + finally: + pythoncom.CoUninitialize() + with ThreadPoolExecutor(1) as pool: + assert pool.submit(existing_mta).result() == (0, expected) + + +def test_nested_snapshot_retains_runtime_checkpoint_bytes_without_semantic_conflicts(tmp_path): + import cbb_refresh_verify.library_lock as locks + from cbb_refresh_verify.scoped_manifest import copy_verified, build_manifest, ManifestScope + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.write_bytes(b'library') + scope = ManifestScope.library('Block', library.name) + with locks.LibraryLock(library) as lease: + before = copy_verified(library.parent, tmp_path/'snapshot', scope=scope) + marker = locks.blocker_path(library) + assert (tmp_path/'snapshot'/marker.name).read_bytes() == marker.read_bytes() + with locks.LibraryLock(library): + owner = locks._owners[lease.key] + locks._persist_blocker(library, lease_id=owner['lease_id']) + assert build_manifest(library.parent, scope=scope) == before + assert not marker.exists() and lease.path.exists() + + +def test_arbitrary_guard_filename_is_preserved_and_primary(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + from cbb_refresh_verify.scoped_manifest import build_manifest, ManifestScope + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() + marker = locks.blocker_path(library); marker.write_bytes(b'{not a guard record') + with pytest.raises(locks.LibraryBusy): + with locks.LibraryLock(library): pass + manifest = build_manifest(library.parent, scope=ManifestScope.library('Block', library.name)) + assert marker.name in manifest['files'] + from contextlib import contextmanager + @contextmanager + def proof(*a): yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} + monkeypatch.setattr(locks, '_prove_library_released', proof) + with pytest.raises(locks.LibraryBusy): + locks.release_retained_library(library, tmp_path/'release') + assert marker.read_bytes() == b'{not a guard record' diff --git a/tests/test_management.py b/tests/test_management.py new file mode 100644 index 0000000..79adaef --- /dev/null +++ b/tests/test_management.py @@ -0,0 +1,254 @@ +"""Operation safety tests use a file-only native boundary; never launch Mentor.""" +from dataclasses import replace +import json +from pathlib import Path +from threading import Event + +import pytest + + +def project(root, name='Source', marker=b'original'): + root.mkdir(parents=True) + (root/'database').mkdir() + (root/'database/icdb.dat').write_bytes(marker) + (root/'PCB/Layout').mkdir(parents=True) + (root/'PCB/Layout/LayoutDB.lyt').write_bytes(marker) + (root/'PCB/pcb.prj').write_text('KEY iCDBDir "../database"\n', encoding='utf-8') + (root/'PCB/main.pcb').write_text(f'KEY ProjectFile "../{name}.prj"\n', encoding='utf-8') + path = root/f'{name}.prj' + path.write_text('KEY RootBlock "Top"\nKEY PCBDesignPath "PCB/main.pcb"\n' + 'KEY iCDBDir "database"\n', encoding='utf-8') + return path + + +class FileAdapter: + def __init__(self): + self.records = [] + self.calls = [] + self.fail_add = False + self.lie = False + self.released = True + + def capabilities(self): + from cbb_refresh_verify.management import Capability + return {op: Capability(True, 'test boundary') for op in ('add', 'update', 'remove')} + + def catalog(self, library, evidence): + from cbb_refresh_verify.management import Catalog + return Catalog(library, 1, tuple(self.records), safe_to_continue=self.released) + + def remove(self, request, evidence): + from cbb_refresh_verify.management import NativeOutcome + self.calls.append('remove') + if not self.lie: + self.records = [r for r in self.records if r.name != request.name] + return NativeOutcome(True, self.released, 'native removal accepted') + + def add(self, request, evidence): + from cbb_refresh_verify.management import NativeOutcome, CatalogEntry, engineering_identity + self.calls.append(('add', request.source)) + if self.fail_add: + return NativeOutcome(False, self.released, 'native add failed') + if not self.lie: + self.records.append(CatalogEntry(request.name, request.partition, + engineering_identity(request.source))) + return NativeOutcome(True, self.released, 'native add accepted') + + +@pytest.fixture +def setup(tmp_path): + from cbb_refresh_verify.management import ManagementRequest, CatalogEntry + library = tmp_path/'library/mentor.lmc' + library.parent.mkdir() + library.write_bytes(b'library') + old = project(library.parent/'ReusableBlocksLP/Block/Work', 'Block') + source = project(tmp_path/'source') + adapter = FileAdapter() + adapter.records = [CatalogEntry('Block', 'Logical-Physical')] + request = ManagementRequest('remove', library, 'Block', evidence_dir=tmp_path/'evidence') + yield request, adapter, source + # FileAdapter never launches native owners. Release only this test's + # intentionally retained lease so pytest can remove its temporary tree. + from cbb_refresh_verify.library_lock import _guard, _owners + key = str(library.with_name('.mentor.cbb-upload.lock').resolve()).casefold() + with _guard: + owner = _owners.pop(key, None) + if owner: + owner['handle'].Close() + + +def execute(request, adapter, **kwargs): + from cbb_refresh_verify.management import run_management + return run_management(request, adapter=adapter, idle_check=lambda *_: None, **kwargs) + + +def test_remove_backup_and_strict_absence(setup): + request, adapter, _ = setup + result = execute(request, adapter) + assert result.status == 'REMOVED' and result.safe_to_continue + assert (result.evidence_path.parent/'library-before/mentor.lmc').read_bytes() == b'library' + assert (result.evidence_path.parent/'library-before/ReusableBlocksLP/Block/Work/Block.prj').is_file() + record = json.loads(result.evidence_path.read_text(encoding='utf-8')) + assert record['operation'] == 'remove' and record['partition'] == 'Logical-Physical' + assert record['stage'] == 'complete' + + +def test_add_conflict_cannot_overwrite(setup): + request, adapter, source = setup + result = execute(replace(request, operation='add', source=source), adapter) + assert result.status == 'UPLOAD_FAILED' and adapter.calls == [] + + +@pytest.mark.parametrize('verify_status,flag,expected', [('PASS', 'GREEN', 'UPLOADED_VERIFIED'), + ('FAIL', 'RED', 'UPLOADED_VERIFY_FAILED'), ('PASS', 'UNKNOWN', 'UPLOADED_VERIFY_FAILED')]) +def test_add_stages_source_and_requires_authoritative_verify(setup, verify_status, flag, expected): + from cbb_refresh_verify.verify_runner import VerifyResult + request, adapter, source = setup + original = source.read_bytes() + def verifier(req, **_): + assert adapter.released + return VerifyResult(verify_status, library_flag=flag) + result = execute(replace(request, operation='add', name='New', source=source), adapter, + verifier=verifier) + assert result.status == expected and source.read_bytes() == original + assert adapter.calls[0][1] != source + assert adapter.calls[0][1].is_relative_to(result.evidence_path.parent) + + +def test_add_no_auto_verify_and_reimport_after_remove(setup): + request, adapter, source = setup + assert execute(request, adapter).status == 'REMOVED' + result = execute(replace(request, operation='add', source=source, auto_verify=False), adapter, + verifier=lambda *_: pytest.fail('auto verify disabled')) + assert result.status == 'UPLOADED' + + +def test_update_failed_readd_keeps_removed_checkpoint_and_recovery_snapshot(setup): + request, adapter, source = setup + adapter.fail_add = True + result = execute(replace(request, operation='update', source=source), adapter) + assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue + assert adapter.records == [] + history = json.loads(result.evidence_path.read_text(encoding='utf-8'))['history'] + assert 'removed' in [x['stage'] for x in history] + assert (result.evidence_path.parent/'library-before/mentor.lmc').is_file() + + +@pytest.mark.parametrize('operation', ['remove', 'add']) +def test_lying_native_output_does_not_report_success(setup, operation): + request, adapter, source = setup + adapter.lie = True + request = replace(request, operation=operation, source=source, + name='New' if operation == 'add' else 'Block') + result = execute(request, adapter) + assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue + + +def test_unreleased_native_owner_prevents_postread_and_verification(setup): + request, adapter, _ = setup + original = adapter.remove + def unreleased(*args): + adapter.released = False + return original(*args) + adapter.remove = unreleased + reads = [] + catalog = adapter.catalog + adapter.catalog = lambda *a: (reads.append(1) or catalog(*a)) + result = execute(request, adapter, verifier=lambda *_: pytest.fail('unsafe verify')) + assert not result.safe_to_continue and len(reads) == 1 + from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy + with pytest.raises(LibraryBusy): + with LibraryLock(request.library): + pass + + +@pytest.mark.parametrize('name', ['../escape', 'A\tB', 'A\nB', 'A/B', 'NUL', 'a.', '']) +def test_invalid_native_identity_rejected_before_writes(setup, name): + request, adapter, _ = setup + result = execute(replace(request, name=name), adapter) + assert result.status == 'BLOCKED' and adapter.calls == [] + + +def test_evidence_inside_target_rejected(setup): + request, adapter, _ = setup + result = execute(replace(request, evidence_dir=request.library.parent/'evidence'), adapter) + assert result.status == 'BLOCKED' and not (request.library.parent/'evidence').exists() + + +def test_cancel_and_restart_never_replay(setup): + from cbb_refresh_verify.management import run_management_batch, unfinished_operations + request, adapter, _ = setup + stop = Event(); stop.set() + assert run_management_batch([request], stop=stop, adapter=adapter) == [] + checkpoint = request.evidence_dir/'interrupted/checkpoint.json' + checkpoint.parent.mkdir(parents=True) + checkpoint.write_text('{"stage":"native_remove", "status":"RUNNING"}', encoding='utf-8') + found = unfinished_operations(request.evidence_dir) + assert len(found) == 1 and found[0]['evidence_path'] == str(checkpoint) + assert adapter.calls == [] + + +def test_capability_gate_enforced_below_ui(setup): + from cbb_refresh_verify.management import Capability + request, adapter, source = setup + adapter.capabilities = lambda: {'add': Capability(False, 'isolated add not validated')} + result = execute(replace(request, operation='add', name='New', source=source), adapter) + assert result.status == 'UNSUPPORTED' and 'isolated' in result.detail + assert adapter.calls == [] + + +def test_timeout_stops_batch_and_persists_ambiguous_native_stage(setup): + from cbb_refresh_verify.management import run_management_batch + request, adapter, _ = setup + def timeout(*_): + raise TimeoutError('network timeout') + adapter.remove = timeout + results = run_management_batch([request, replace(request, name='Other')], adapter=adapter, + idle_check=lambda *_: None) + assert len(results) == 1 and not results[0].safe_to_continue + assert results[0].status == 'RECOVERY_REQUIRED' + + +def test_catalog_timeout_is_not_safe_to_continue(setup): + request, adapter, _ = setup + def timeout(*_): + raise TimeoutError('catalog owner still running') + adapter.catalog = timeout + result = execute(request, adapter) + assert not result.safe_to_continue and adapter.calls == [] + + +def test_replaced_source_bytes_fail_identity_even_with_same_name(setup): + request, adapter, source = setup + real_add = adapter.add + def wrong_source(*args): + from cbb_refresh_verify.management import CatalogEntry + result = real_add(*args) + entry = adapter.records[-1] + adapter.records[-1] = CatalogEntry(entry.name, entry.partition, {'wrong': 'project'}) + return result + adapter.add = wrong_source + result = execute(replace(request, operation='add', name='New', source=source), adapter) + assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue + + +def test_update_success_requires_same_identity_and_no_implicit_partition_move(setup): + request, adapter, source = setup + result = execute(replace(request, operation='update', source=source, partition='Other'), adapter) + assert result.status == 'BLOCKED' and not adapter.calls + result = execute(replace(request, operation='update', source=source, auto_verify=False), adapter) + assert result.status == 'UPLOADED' + assert adapter.calls[0] == 'remove' and adapter.calls[1][0] == 'add' + + +def test_verify_exception_after_upload_retains_coordination_lock(setup): + from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy + request, adapter, source = setup + def broken_verifier(*a, **k): + raise OSError('verification worker disconnected') + result = execute(replace(request, operation='add', name='New', source=source), adapter, + verifier=broken_verifier) + assert result.status == 'UPLOADED_VERIFY_FAILED' and not result.safe_to_continue + with pytest.raises(LibraryBusy): + with LibraryLock(request.library): + pass diff --git a/tests/test_management_ui.py b/tests/test_management_ui.py new file mode 100644 index 0000000..9074555 --- /dev/null +++ b/tests/test_management_ui.py @@ -0,0 +1,99 @@ +from dataclasses import replace +from pathlib import Path +from types import SimpleNamespace + +from PySide6.QtCore import Qt +from PySide6.QtWidgets import QMessageBox + +from cbb_refresh_verify.models import CbbRecord, LibraryReadResult + + +def test_management_can_select_ready_and_capability_disabled_explanation(qtbot, tmp_path, monkeypatch): + from cbb_refresh_verify.management import Capability + from cbb_refresh_verify.ui.main_window import MainWindow + caps = {name: Capability(name == 'remove', name + ' not validated') for name in ('add', 'update', 'remove')} + window = MainWindow(reader=lambda _: LibraryReadResult((CbbRecord('Ready', 'Logical-Physical', status='READY'),)), + data_root=tmp_path, management_adapter=SimpleNamespace(capabilities=lambda: caps)) + qtbot.addWidget(window) + window.lmc_edit.setText('library.lmc'); window.load_library() + qtbot.waitUntil(lambda: not window.busy) + window.select_visible_button.click() + assert window.remove_button.isEnabled() + assert not window.add_button.isEnabled() and not window.update_button.isEnabled() + assert 'not validated' in window.management_capability_label.text() + assert not window.selected_batch_button.isEnabled() + window.remove_button.click() + assert window.management_dialog.requests()[0].name == 'Ready' + assert window.management_dialog.requests()[0].partition == 'Logical-Physical' + window.management_dialog.reject() + + +def test_management_dialog_preview_confirmation_and_result_status(qtbot, tmp_path, monkeypatch): + from cbb_refresh_verify.management import ManagementRequest, ManagementResult + from cbb_refresh_verify.ui.management_dialog import ManagementDialog + requests = [ManagementRequest('remove', tmp_path/'lib.lmc', 'Block', evidence_dir=tmp_path/'out')] + calls, confirmations = [], [] + def runner(items, **_): + calls.extend(items) + return [ManagementResult('remove', items[0].library, 'Block', 'Logical-Physical', None, + False, 'complete', 'REMOVED', True, 'removed', tmp_path/'checkpoint.json')] + dialog = ManagementDialog(requests, runner=runner) + qtbot.addWidget(dialog) + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (confirmations.append(a[2]) or QMessageBox.No)) + dialog.execute_button.click() + assert not calls and 'Block' in confirmations[0] and '完整' in confirmations[0] + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) + dialog.execute_button.click() + qtbot.waitUntil(lambda: not dialog.busy) + assert len(calls) == 1 and '已移除' in dialog.status_label.text() + assert str(tmp_path/'checkpoint.json') in dialog.output.toPlainText() + assert not dialog.execute_button.isEnabled() + + +def test_add_dialog_default_auto_verify_and_source_identity_rows(qtbot, tmp_path): + from cbb_refresh_verify.management import ManagementRequest + from cbb_refresh_verify.ui.management_dialog import ManagementDialog + dialog = ManagementDialog([ManagementRequest('add', tmp_path/'lib.lmc', '', evidence_dir=tmp_path/'out')]) + qtbot.addWidget(dialog) + dialog.add_sources([tmp_path/'One.prj', tmp_path/'Two.prj']) + requests = dialog.requests() + assert [r.name for r in requests] == ['One', 'Two'] + assert all(r.auto_verify and r.source.suffix == '.prj' for r in requests) + dialog.auto_verify.setChecked(False) + assert not any(r.auto_verify for r in dialog.requests()) + + +def test_restart_shows_unfinished_without_starting_worker(qtbot, tmp_path): + import json + from cbb_refresh_verify.management import Capability + from cbb_refresh_verify.ui.main_window import MainWindow + checkpoint = tmp_path/'management/interrupted/checkpoint.json' + checkpoint.parent.mkdir(parents=True) + checkpoint.write_text(json.dumps({'status': 'RUNNING', 'stage': 'native_remove', 'name': 'Block'}), encoding='utf-8') + adapter = SimpleNamespace(capabilities=lambda: {n: Capability(False, 'disabled') for n in ('add', 'update', 'remove')}) + window = MainWindow(data_root=tmp_path, management_adapter=adapter) + qtbot.addWidget(window) + assert '未完成' in window.unfinished_label.text() and not window.busy + assert 'Block' in window.output.toPlainText() + + +def test_persistent_owner_blocker_prevents_native_load_and_offers_checked_release(qtbot, tmp_path, monkeypatch): + import pytest + import cbb_refresh_verify.library_lock as locks + from cbb_refresh_verify.ui.main_window import MainWindow + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() + locks.blocker_path(library).write_text('{}', encoding='utf-8') + window = MainWindow(reader=lambda _: pytest.fail('unconfirmed owner reopened'), data_root=tmp_path/'data') + qtbot.addWidget(window) + window.lmc_edit.setText(str(library)); window.load_library() + qtbot.waitUntil(lambda: not window.busy) + assert window.loaded_path is None + assert window.release_library_button.isEnabled() + assert '释放检查' in window.status_label.text() + calls = [] + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) + monkeypatch.setattr(locks, 'release_retained_library', lambda *a: + (calls.append(a) or {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'proof.json'})) + window.release_library_button.click() + qtbot.waitUntil(lambda: not window.busy) + assert len(calls) == 1 and 'proof.json' in window.output.toPlainText() diff --git a/tests/test_native_management.py b/tests/test_native_management.py new file mode 100644 index 0000000..336a7ce --- /dev/null +++ b/tests/test_native_management.py @@ -0,0 +1,165 @@ +import json +from pathlib import Path +from types import SimpleNamespace + +import pytest + + +def test_installed_binary_mismatch_disables_removal(tmp_path): + from cbb_refresh_verify.native_management import NativeManagementAdapter + adapter = NativeManagementAdapter(mentor_home=tmp_path) + caps = adapter.capabilities() + assert not caps['remove'].supported and 'CleanWGLib' in caps['remove'].reason + assert not caps['add'].supported and not caps['update'].supported + + +@pytest.mark.parametrize('name', ['A\tB', 'A\nB', '../A', '中文']) +def test_delete_record_rejects_ambiguous_encoding_or_injection(name): + from cbb_refresh_verify.native_management import removal_input + with pytest.raises(ValueError): + removal_input(name, 'Logical-Physical') + + +def test_delete_record_is_exact_shipped_tcl_contract(): + from cbb_refresh_verify.native_management import removal_input, removal_output_ok + assert removal_input('Block', 'Logical-Physical') == b'REUSE_BLOCK\tLogical-Physical\tBlock\n' + assert not removal_output_ok(0, '', 'Block', 'Logical-Physical') + assert not removal_output_ok(0, "# Warning: Unable to remove REUSE_BLOCK 'Block' from partition 'Logical-Physical'.", 'Block', 'Logical-Physical') + assert removal_output_ok(0, "# Note: Removed REUSE_BLOCK 'Block' from partition 'Logical-Physical'.", 'Block', 'Logical-Physical') + + +def test_upgrade_dialog_is_refused_and_only_cancel_is_allowed(): + from cbb_refresh_verify.native_management import catalog_dialog_action + assert catalog_dialog_action('Library Manager', 'This Central Library must be updated before it can be opened in Library Manager. Select OK to continue.') == 'cancel' + assert catalog_dialog_action('Select flow type', '') == 'cancel' + assert catalog_dialog_action('Library Manager', 'Would you like to save?') == 'observe' + + +def test_catalog_never_falls_back_to_folder_list(tmp_path): + from cbb_refresh_verify.native_management import NativeManagementAdapter + library = tmp_path/'lib.lmc'; library.touch() + (tmp_path/'ReusableBlocksLP/Ghost').mkdir(parents=True) + adapter = NativeManagementAdapter() + adapter._worker = lambda *_: {'success': False, 'safe_to_continue': True, 'detail': 'upgrade refused'} + with pytest.raises(RuntimeError, match='upgrade refused'): + adapter.catalog(library, tmp_path/'evidence') + + +def test_private_worker_timeout_retains_native_and_never_kills(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + monkeypatch.setattr(native, '_retained_workers', []) + from cbb_refresh_verify.native_management import NativeManagementAdapter + class Running: + def __init__(self, *a): pass + def poll(self): return None + def close(self): pytest.fail('closing live DesktopWorker kills automation') + def kill(self): pytest.fail('must preserve native owner') + adapter = NativeManagementAdapter(worker_factory=Running, timeout_seconds=0) + result = adapter._worker({'operation': 'catalog', 'library': str(tmp_path/'lib.lmc')}, tmp_path/'out') + assert result['safe_to_continue'] is False + assert 'TIMEOUT' in result['detail'] + + +def test_hidden_worker_and_probe_dispatch_before_qt(monkeypatch, tmp_path): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.application import main + seen = [] + monkeypatch.setattr(native, 'management_worker_main', lambda p: (seen.append(p) or 17)) + assert main(['--management-worker', str(tmp_path/'job.json')]) == 17 + assert seen == [tmp_path/'job.json'] + + +def test_probe_cannot_use_existing_fixture_or_mutate_source(tmp_path): + from cbb_refresh_verify.native_management import run_isolated_probe + existing = tmp_path/'existing' + existing.mkdir() + with pytest.raises(ValueError, match='new|新'): + run_isolated_probe(tmp_path/'source.prj', existing, 'Block') + + +def test_probe_requires_owned_new_native_library_path(tmp_path): + from cbb_refresh_verify.native_management import prepare_probe_library + outside = tmp_path/'production.lmc'; outside.write_bytes(b'unchanged') + root = tmp_path/'worker'; root.mkdir() + with pytest.raises(ValueError): + prepare_probe_library({'library': str(outside)}, root) + assert outside.read_bytes() == b'unchanged' + owned = root/'fixture/mentor_lib.lmc' + assert prepare_probe_library({'library': str(owned)}, root) == owned + owned.write_bytes(b'first creation') + with pytest.raises(ValueError): + prepare_probe_library({'library': str(owned)}, root) + + +@pytest.mark.parametrize('records', [[], [('Actual', 'Logical-Physical')]]) +def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records): + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.mentor_adapter as mentor + import win32com.client + library_path = tmp_path/'lib.lmc'; library_path.touch() + (tmp_path/'ReusableBlocksLP/Ghost').mkdir(parents=True) + class Collection: + def __init__(self, items): self.items = items; self.Count = len(items) + def Item(self, index): return self.items[index - 1] + items = Collection([SimpleNamespace(Name=name, PartitionName=partition) for name, partition in records]) + class Library: + FullName = str(library_path) + FlowType = 1 + def Partitions(self, kind): + assert kind == 7 + return Collection([items]) + def Close(self): pass + state = {'running': False} + class App: + def OpenLibrary(self, path): + assert path == str(library_path) + return Library() + def Quit(self): state['running'] = False + def dispatch(_): state['running'] = True; return App() + monkeypatch.setattr(win32com.client, 'DispatchEx', dispatch) + monkeypatch.setattr(native, '_current_desktop', lambda: (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test')) + monkeypatch.setattr(mentor, '_process_details', lambda _: [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if state['running'] else []) + monkeypatch.setattr(native, '_wait_native_exit', lambda _: not state['running']) + result = native._catalog_native({'operation': 'catalog', 'library': str(library_path), + 'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path)}, tmp_path) + assert result['success'] and result['safe_to_continue'] + assert [(entry['name'], entry['partition']) for entry in result['entries']] == records + assert result['flow'] == 1 + + +def test_production_caps_require_all_validated_binary_hashes(tmp_path, monkeypatch): + import hashlib + import cbb_refresh_verify.native_management as native + proof = {'lm/win32/bin/CleanWGLib.exe': hashlib.sha256(b'verified').hexdigest()} + monkeypatch.setattr(native, '_BINARY_PROOF', proof) + path = tmp_path/next(iter(proof)); path.parent.mkdir(parents=True); path.write_bytes(b'verified') + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + assert adapter.capabilities()['remove'].supported + assert not adapter.capabilities()['add'].supported + path.write_bytes(b'different') + assert not adapter.capabilities()['remove'].supported + + +def test_probe_readd_requires_creation_token_and_fixed_owned_library(tmp_path): + from cbb_refresh_verify.native_management import validate_probe_readd + root = tmp_path/'native-readd'; root.mkdir() + library = tmp_path/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.touch() + job = {'library': str(library), 'probe_token': 'owned-token'} + with pytest.raises(ValueError): + validate_probe_readd(job, root) + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'other'}), encoding='utf-8') + with pytest.raises(ValueError): + validate_probe_readd(job, root) + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned-token'}), encoding='utf-8') + assert validate_probe_readd(job, root) == library + outside = tmp_path/'production.lmc'; outside.touch() + with pytest.raises(ValueError): + validate_probe_readd({**job, 'library': str(outside)}, root) + + +def test_com_failure_without_owner_identity_cannot_claim_release(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + monkeypatch.setattr(native, '_catalog_com', lambda *a: {'success': False, + 'safe_to_continue': False, 'native_pids': [], 'detail': 'COM startup ambiguous'}) + assert native._catalog_native({}, tmp_path)['safe_to_continue'] is False diff --git a/tools/validate_native_management.py b/tools/validate_native_management.py new file mode 100644 index 0000000..6dc94d9 --- /dev/null +++ b/tools/validate_native_management.py @@ -0,0 +1,28 @@ +"""Explicit isolated Add research, never a production capability override. + +python tools/validate_native_management.py SOURCE.prj NEW_OUTPUT_DIRECTORY NAME +""" +import argparse +from pathlib import Path +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parents[1]/'src')) +from cbb_refresh_verify.native_management import run_isolated_probe + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('source', type=Path) + parser.add_argument('new_output_directory', type=Path) + parser.add_argument('name') + parser.add_argument('--replacement-source', type=Path, + help='同一新建隔离库中额外验证同名移除并重新导入此完整工程') + args = parser.parse_args() + result = run_isolated_probe(args.source, args.new_output_directory, args.name, + replacement_source=args.replacement_source) + print(args.new_output_directory/'probe-result.json') + return 0 if result.get('accepted') else 2 + + +if __name__ == '__main__': + raise SystemExit(main()) -- 2.39.5 From dd1fcea7e284176b56fcc650ce6bc4d537deeba3 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: Mon, 2 Nov 2026 21:19:57 +0800 Subject: [PATCH 11/59] fix: accept layout-based PCB projects without optional icdb path --- src/cbb_refresh_verify/management.py | 5 +++ tests/test_management_source.py | 58 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/test_management_source.py diff --git a/src/cbb_refresh_verify/management.py b/src/cbb_refresh_verify/management.py index 1664344..8501d7b 100644 --- a/src/cbb_refresh_verify/management.py +++ b/src/cbb_refresh_verify/management.py @@ -100,6 +100,11 @@ def source_project(source, top_level_block=''): for path in (source, pcb.parent/'pcb.prj'): if not path.is_file(): raise ValueError('工程缺少 PCB 工程配置。') + # Healthy Expedition PCB projects can use Layout/LayoutDB.lyt without + # declaring an independent iCDBDir. The schematic DB stays mandatory; + # an explicitly declared PCB DB must still pass every reference check. + if path != source and not re.search(r'^KEY iCDBDir\b', _project_text(path), re.M): + continue db = reference(path, 'iCDBDir') if not (db/'icdb.dat').is_file(): raise ValueError('工程缺少原理图/PCB 数据库。') diff --git a/tests/test_management_source.py b/tests/test_management_source.py new file mode 100644 index 0000000..a024368 --- /dev/null +++ b/tests/test_management_source.py @@ -0,0 +1,58 @@ +"""Real EE7.9.5 PRJ shape: PCB pcb.prj may use LayoutDB without iCDBDir.""" +import pytest + +from cbb_refresh_verify.management import source_project, engineering_identity + + +@pytest.fixture +def healthy_source(tmp_path): + (tmp_path/'database').mkdir() + (tmp_path/'database/icdb.dat').write_bytes(b'schematic database') + (tmp_path/'PCB/Layout').mkdir(parents=True) + (tmp_path/'PCB/Layout/LayoutDB.lyt').write_bytes(b'layout database') + source = tmp_path/'IP101GR.prj' + source.write_text('KEY RootBlock "IP101GR"\nKEY FlowType "DX"\n' + 'KEY iCDBDir ".\\database"\nKEY PCBDesignPath "PCB\\IP101GR.pcb"\n', encoding='utf-8') + (tmp_path/'PCB/IP101GR.pcb').write_text('KEY ProjectFile "..\\IP101GR.prj"\n', encoding='utf-8') + (tmp_path/'PCB/pcb.prj').write_text('SECTION Project\nKEY CentralLibrary "old.lmc"\nENDSECTION\n', encoding='utf-8') + return source + + +def test_layout_based_pcb_without_independent_icdb_directory_is_complete(healthy_source): + source, pcb, top = source_project(healthy_source) + assert source == healthy_source + assert pcb == healthy_source.parent/'PCB/IP101GR.pcb' + assert top == 'IP101GR' + assert engineering_identity(source)['top_level_block'] == 'IP101GR' + + +@pytest.mark.parametrize('missing', ['database/icdb.dat', 'PCB/Layout/LayoutDB.lyt', 'PCB/pcb.prj']) +def test_optional_pcb_icdb_does_not_relax_required_engineering_files(healthy_source, missing): + (healthy_source.parent/missing).unlink() + with pytest.raises(ValueError): + source_project(healthy_source) + + +def test_main_schematic_icdb_declaration_remains_required(healthy_source): + text = healthy_source.read_text(encoding='utf-8').replace('KEY iCDBDir ".\\database"\n', '') + healthy_source.write_text(text, encoding='utf-8') + with pytest.raises(ValueError, match='iCDBDir'): + source_project(healthy_source) + + +@pytest.mark.parametrize('declared', [ + 'KEY iCDBDir "missing"\n', + 'KEY iCDBDir "..\\..\\outside"\n', + 'KEY iCDBDir "C:\\external"\n', + 'KEY iCDBDir ""\n', + 'KEY iCDBDir "..\\database"\nKEY iCDBDir "..\\database"\n', +]) +def test_explicit_pcb_icdb_reference_must_be_unique_contained_and_exist(healthy_source, declared): + (healthy_source.parent/'PCB/pcb.prj').write_text(declared, encoding='utf-8') + with pytest.raises(ValueError): + source_project(healthy_source) + + +def test_valid_explicit_pcb_icdb_directory_remains_supported(healthy_source): + (healthy_source.parent/'PCB/pcb.prj').write_text('KEY iCDBDir "..\\database"\n', encoding='utf-8') + assert source_project(healthy_source)[0] == healthy_source -- 2.39.5 From 46c3e7236462081d2e0bbd29677ec394b247f919 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: Mon, 2 Nov 2026 21:48:40 +0800 Subject: [PATCH 12/59] fix: preserve mapped native paths and retain probe ownership guards --- docs/releases/2026-11-02-cbb-management.md | 8 + src/cbb_refresh_verify/management.py | 29 +-- src/cbb_refresh_verify/native_management.py | 99 +++++++--- src/cbb_refresh_verify/ui/main_window.py | 4 +- tests/test_management.py | 49 +++++ tests/test_management_probe_guards.py | 189 ++++++++++++++++++++ tests/test_native_management.py | 23 ++- 7 files changed, 365 insertions(+), 36 deletions(-) create mode 100644 tests/test_management_probe_guards.py diff --git a/docs/releases/2026-11-02-cbb-management.md b/docs/releases/2026-11-02-cbb-management.md index f49c693..00eed36 100644 --- a/docs/releases/2026-11-02-cbb-management.md +++ b/docs/releases/2026-11-02-cbb-management.md @@ -61,6 +61,7 @@ icdb.dat 和 LayoutDB.lyt。全部通过才归档阻止记录并关闭本应用 上述释放检查和未完成任务审查,再使用本工具或旧上传工具继续写入。 备份与来源不能置于目标库内部,来源必须是完整且引用位于工程内的 PRJ/PCB。 +来源工程根目录和目标库根目录也不能相互包含或相同,避免操作嵌套库时改变原始来源。 链接、外部数据库引用、源文件变化、备份失败和同名新增冲突均会阻止写入。 来源先完整复制;工程身份比较使用顶层块、原理图数据库和布局数据库内容指纹, 原生规范化若改变数据库字节则保守拒绝确认,需要进一步语义验证,不能声称等价。 @@ -69,6 +70,9 @@ icdb.dat 和 LayoutDB.lyt。全部通过才归档阻止记录并关闭本应用 已识别原生进程。读取目录遇到旧库升级或 Select flow type 对话框时取消并报错, 绝不将升级作为只读读取的一部分。超时保留原生所有者和证据,不强杀数据库进程; 确认前不继续下一项,不启动最终 Verify。 +通过网络盘租约选择的盘符路径在原生 OpenLibrary、CLI、暂存工程库引用和后续 +Verify 中保持原样;规范化路径仅用于身份、包含关系和锁键比较。工作进程按原 +租约的盘符/共享目录上下文确认映射,不把原生盘符路径改回 UNC。 ## 独立新增诊断 @@ -81,6 +85,10 @@ Reusable Blocks / UIA Logical - Physical 页签和 New 按钮、1103 对话框 可加 `--replacement-source OTHER.prj` 在同一个新建隔离库中验证同名更新: 先备份完整旧库,再原生移除并检查缺失,然后重新导入替换工程。重新导入必须 匹配本次 CreateLibrary 记录的固定路径和随机所有权令牌,不能指向外部库。 +监督进程先创建新的空 fixture 目录并持有库锁,原生工作进程核对准备令牌、 +检查点协议和监督 PID 后才创建 LMC。初次创建、所有复读和替换阶段均按生产 +操作的释放规则持锁;超时或释放异常保留独占句柄和重启阻止记录,必须重新 +通过会话释放检查才能解除。 本阶段单元测试通过注入文件模拟的原生边界执行,不启动 Mentor、不操作实际库。 候选冻结包、最终 Windows 11 原生场景由协调端单独验收。Windows 10 和真实 diff --git a/src/cbb_refresh_verify/management.py b/src/cbb_refresh_verify/management.py index 8501d7b..de85280 100644 --- a/src/cbb_refresh_verify/management.py +++ b/src/cbb_refresh_verify/management.py @@ -27,6 +27,8 @@ class ManagementRequest: evidence_dir: Path = Path('management-evidence') top_level_block: str = '' timeout_seconds: float = 180 + mapped_drive: str = '' + mapped_remote_root: str = '' @dataclass(frozen=True) @@ -187,30 +189,36 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro if request.operation not in ('add', 'update', 'remove'): raise ValueError('不支持的管理操作。') validate_name(request.name); validate_name(request.partition) - library = Path(request.library).resolve(strict=True) + from .network_path import absolute_preserving_drive + library = absolute_preserving_drive(request.library) + canonical_library = library.resolve(strict=True) + library_root = canonical_library.parent if library.suffix.casefold() != '.lmc' or not library.is_file(): raise ValueError('中心库必须为现有 LMC 文件。') evidence = Path(request.evidence_dir).resolve() - if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence): + if evidence.is_relative_to(library_root) or library_root.is_relative_to(evidence): raise ValueError('操作记录与备份必须位于目标中心库之外,且不能包含目标库。') capability = adapter.capabilities().get(request.operation, Capability(False, '未验证的原生接口')) if not capability.supported: return finish('UNSUPPORTED', capability.reason) for unfinished in unfinished_operations(evidence): - if unfinished.get('library') and Path(unfinished['library']).resolve() == library: + if unfinished.get('library') and Path(unfinished['library']).resolve() == canonical_library: return finish('BLOCKED', f'存在未完成/需恢复任务,禁止自动重放:{unfinished["evidence_path"]}', False) source = pcb = None if request.operation in ('add', 'update'): if request.source is None: raise ValueError('新增/更新需要完整来源工程。') source, pcb, top = source_project(request.source, request.top_level_block) - if (source.is_relative_to(library.parent) or evidence.is_relative_to(source.parent) + if (source.parent.is_relative_to(library_root) or library_root.is_relative_to(source.parent) + or evidence.is_relative_to(source.parent) or source.parent.is_relative_to(evidence)): raise ValueError('来源工程、目标库与操作证据必须使用独立目录。') mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) if not acquired: return finish('BLOCKED', '已有 CBB 校验/管理/恢复任务运行。', False) + if hasattr(adapter, 'bind_library_mapping'): + adapter.bind_library_mapping(request.mapped_drive, request.mapped_remote_root) with LibraryLock(library, safe_to_release=lambda: owner_safe): root = evidence/('management-' + uuid4().hex) root.mkdir(parents=True, exist_ok=False) @@ -232,7 +240,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro owner_safe = False before = adapter.catalog(library, root/'before') owner_safe = before.safe_to_continue - if not owner_safe or Path(before.library).resolve() != library or before.flow != 1: + if not owner_safe or Path(before.library).resolve() != canonical_library or before.flow != 1: return finish('BLOCKED', '原生目录身份/流程或所有者释放未确认。仅支持已验证 DxDesigner 流程。', owner_safe) same_name = [entry for entry in before.entries if entry.name.casefold() == request.name.casefold()] matches = [entry for entry in same_name if entry.partition == request.partition and entry.name == request.name] @@ -246,7 +254,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro idle_check(library, target_project, target_pcb) scope = ManifestScope.library(request.name, library.name) checkpoint('backup') - original = copy_verified(library.parent, root/'library-before', scope=scope, progress=progress) + original = copy_verified(library_root, root/'library-before', scope=scope, progress=progress) _write_json(root/'library-before-manifest.json', original) staged = request identity = None @@ -258,7 +266,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro identity = engineering_identity(staged.source) if build_manifest(source.parent) != source_manifest: raise OSError('来源工程在暂存期间变化。') - if build_manifest(library.parent, scope=scope) != original: + if build_manifest(library_root, scope=scope) != original: raise OSError('备份后中心库内容变化,停止写入。') if request.operation in ('remove', 'update'): checkpoint('native_remove') @@ -272,7 +280,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro owner_safe = False after = adapter.catalog(library, root/'after-remove') owner_safe = after.safe_to_continue - if (not owner_safe or Path(after.library).resolve() != library or after.flow != 1 + if (not owner_safe or Path(after.library).resolve() != canonical_library or after.flow != 1 or any(e.name == request.name and e.partition == request.partition for e in after.entries)): return finish('RECOVERY_REQUIRED', '原生移除后目录未确认目标缺失。', False) removed = True @@ -292,7 +300,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro owner_safe = after.safe_to_continue matches = [entry for entry in after.entries if entry.name == request.name and entry.partition == request.partition] - if (not owner_safe or Path(after.library).resolve() != library or after.flow != 1 + if (not owner_safe or Path(after.library).resolve() != canonical_library or after.flow != 1 or len(matches) != 1 or matches[0].engineering_identity != identity): return finish('RECOVERY_REQUIRED', '原生新增后名称、分区或来源工程内容未确认一致。', False) uploaded = True @@ -302,7 +310,8 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro # Adapter catalog cannot return until its exact native owner exits. checkpoint('verify') owner_safe = False - verified = verifier(VerifyRequest(library, request.name, root/'verify'), progress=progress) + verified = verifier(VerifyRequest(library, request.name, root/'verify', + mapped_drive=request.mapped_drive, mapped_remote_root=request.mapped_remote_root), progress=progress) owner_safe = verified.safe_to_continue checkpoint('verified', verification=asdict(verified)) good = verified.status == 'PASS' and verified.library_flag == 'GREEN' and owner_safe diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 2e305d8..3a35f35 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -18,6 +18,7 @@ from uuid import uuid4 from .management import (Capability, Catalog, CatalogEntry, NativeOutcome, engineering_identity, validate_name, _write_json) from .scoped_manifest import hash_file +from .network_path import absolute_preserving_drive _retained_workers = [] _BINARY_PROOF = { @@ -55,6 +56,12 @@ class NativeManagementAdapter: self.home = Path(mentor_home) if mentor_home is not None else discover_sdd_home() self.worker_factory = worker_factory self.timeout_seconds = timeout_seconds + self.mapped_drive = '' + self.mapped_remote_root = '' + + def bind_library_mapping(self, drive, remote_root): + self.mapped_drive = drive or '' + self.mapped_remote_root = remote_root or '' def capabilities(self): reason = '' @@ -74,14 +81,36 @@ class NativeManagementAdapter: } def _worker(self, job, evidence): - from .desktop_worker import DesktopWorker evidence = Path(evidence) evidence.mkdir(parents=True, exist_ok=False) - request_path = evidence/'request.json' desktop = 'CbbManagement_' + uuid4().hex[:12] timeout = float(job.get('timeout_seconds', self.timeout_seconds)) job = {**job, 'desktop': desktop, 'mentor_home': str(self.home or ''), + 'mapped_drive': job.get('mapped_drive', self.mapped_drive), + 'mapped_remote_root': job.get('mapped_remote_root', self.mapped_remote_root), 'timeout_seconds': timeout, 'supervisor_pid': os.getpid()} + if job['operation'] == 'probe_create_add': + # Prepare the fixed NEW fixture before a native owner can start. + # The parent owns its sidecar for the entire create/add invocation. + from .library_lock import LibraryLock + library = absolute_preserving_drive(evidence)/'fixture/mentor_lib.lmc' + if Path(job['library']).resolve() != library.resolve() or not job.get('probe_token'): + raise ValueError('隔离创建目标必须为本次工作目录内的新库。') + library.parent.mkdir(exist_ok=False) + _write_json(evidence/'probe-prepared.json', {'library': str(library), 'token': job['probe_token'], + 'supervisor_pid': os.getpid()}) + owner_safe = True + with LibraryLock(library, safe_to_release=lambda: owner_safe): + owner_safe = False + result = self._launch_worker(job, evidence) + owner_safe = result.get('safe_to_continue') is True + return result + return self._launch_worker(job, evidence) + + def _launch_worker(self, job, evidence): + from .desktop_worker import DesktopWorker + request_path = evidence/'request.json' + desktop, timeout = job['desktop'], job['timeout_seconds'] _write_json(request_path, job) command = ([sys.executable] if getattr(sys, 'frozen', False) else [sys.executable, '-m', 'cbb_refresh_verify']) @@ -106,7 +135,7 @@ class NativeManagementAdapter: worker.close() def catalog(self, library, evidence): - result = self._worker({'operation': 'catalog', 'library': str(library)}, evidence) + result = self._worker({'operation': 'catalog', 'library': str(absolute_preserving_drive(library))}, evidence) if not result.get('success') or not result.get('safe_to_continue'): raise RuntimeError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。')) return Catalog(Path(result['library']), int(result['flow']), @@ -119,8 +148,9 @@ class NativeManagementAdapter: removal_input(request.name, request.partition) if request.partition != 'Logical-Physical': return NativeOutcome(False, True, '仅已验证 Logical-Physical 原生移除。') - result = self._worker({'operation': 'remove', 'library': str(request.library), + result = self._worker({'operation': 'remove', 'library': str(absolute_preserving_drive(request.library)), 'name': request.name, 'partition': request.partition, + 'mapped_drive': request.mapped_drive, 'mapped_remote_root': request.mapped_remote_root, 'timeout_seconds': request.timeout_seconds}, evidence) return NativeOutcome(bool(result.get('success')), bool(result.get('safe_to_continue')), result.get('detail', '')) @@ -135,22 +165,36 @@ class NativeManagementAdapter: def prepare_probe_library(job, root): - """Diagnostic create is restricted to a new fixed descendant of this job.""" - expected = Path(root).resolve()/'fixture/mentor_lib.lmc' - if Path(job['library']).resolve() != expected or expected.parent.exists(): - raise ValueError('诊断只能创建当前任务下全新独占 fixture/mentor_lib.lmc。') - expected.parent.mkdir() + """Validate a NEW fixed fixture already guarded by its supervisor.""" + from .library_lock import blocker_path, checkpoint_record + expected = absolute_preserving_drive(root)/'fixture/mentor_lib.lmc' + try: + prepared = json.loads((Path(root)/'probe-prepared.json').read_text(encoding='utf-8')) + guard = checkpoint_record(blocker_path(expected)) + allowed = {'.mentor_lib.cbb-upload.lock', '.mentor_lib.cbb-upload.blocked.json'} + valid = (Path(job['library']).resolve() == expected.resolve() and not expected.exists() + and expected.parent.is_dir() and {p.name for p in expected.parent.iterdir()} == allowed + and prepared.get('token') == job.get('probe_token') and bool(job.get('probe_token')) + and Path(prepared['library']).resolve() == expected.resolve() + and prepared.get('supervisor_pid') == job.get('supervisor_pid') + and guard is not None and guard['pid'] == job.get('supervisor_pid') + and Path(guard['library']).resolve() == expected.resolve() + and guard['status'] == 'OPERATION_IN_PROGRESS') + except (OSError, ValueError, KeyError): + valid = False + if not valid: + raise ValueError('诊断只能创建当前监督进程已持锁的全新 fixture/mentor_lib.lmc。') return expected def validate_probe_readd(job, root): - expected = Path(root).parent.resolve()/'native-add/fixture/mentor_lib.lmc' + expected = absolute_preserving_drive(root).parent/'native-add/fixture/mentor_lib.lmc' try: owner = json.loads((Path(root).parent/'probe-owner.json').read_text(encoding='utf-8')) except (OSError, ValueError) as error: raise ValueError('没有本次原生创建的隔离库所有权记录。') from error - if (Path(job['library']).resolve() != expected or not expected.is_file() - or owner.get('library') != str(expected) or not job.get('probe_token') + if (Path(job['library']).resolve() != expected.resolve() or not expected.is_file() + or not owner.get('library') or Path(owner['library']).resolve() != expected.resolve() or not job.get('probe_token') or owner.get('token') != job['probe_token']): raise ValueError('重新导入诊断仅允许本次原生新建的固定隔离库。') return expected @@ -288,18 +332,19 @@ def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_sour from .scoped_manifest import ManifestScope from .scoped_manifest import copy_verified, build_manifest import pythoncom, win32con, win32event - root = Path(root).resolve() + root = absolute_preserving_drive(root) if root.exists(): raise ValueError('诊断目录必须是全新目录,不能复用现有中心库或旧 fixture。') source, _, top = source_project(source) validate_name(name) - if root.is_relative_to(source.parent) or source.parent.is_relative_to(root): + if root.resolve().is_relative_to(source.parent) or source.parent.is_relative_to(root.resolve()): raise ValueError('诊断目录不能与来源工程相互包含。') mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') acquired = False token = uuid4().hex record = {'operation': 'isolated_add_probe', 'source': str(source), 'name': name, 'production_capability_enabled': False} + owner_safe = True from .com_runtime import initialize_com, uninitialize_com initialized = initialize_com() try: @@ -314,13 +359,20 @@ def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_sour if not adapter.capabilities()['remove'].supported: raise RuntimeError('诊断要求已识别的 EE7.9.5 原生安装。') library = root/'native-add/fixture/mentor_lib.lmc' + def read_catalog(evidence): + nonlocal owner_safe + owner_safe = False + catalog = adapter.catalog(library, evidence) + owner_safe = catalog.safe_to_continue + return catalog native = adapter._worker({'operation': 'probe_create_add', 'library': str(library), 'source': str(staged), 'name': name, 'top_level_block': top, 'probe_token': token}, root/'native-add') record['native'] = native record['source_unchanged'] = build_manifest(source.parent) == manifest if native.get('success') and native.get('safe_to_continue'): - catalog = adapter.catalog(library, root/'reopen') + with LibraryLock(library, safe_to_release=lambda: owner_safe): + catalog = read_catalog(root/'reopen') record['catalog'] = asdict(catalog) matches = [entry for entry in catalog.entries if entry.name == name and entry.partition == 'Logical-Physical'] record['identity_matches'] = len(matches) == 1 and matches[0].engineering_identity == identity @@ -330,35 +382,39 @@ def run_isolated_probe(source, root, name, *, mentor_home=None, replacement_sour _write_json(root/'probe-result.json', record) if record['accepted'] and replacement_source is not None: replacement, _, replacement_top = source_project(replacement_source) - if replacement.is_relative_to(root) or root.is_relative_to(replacement.parent): + if replacement.is_relative_to(root.resolve()) or root.resolve().is_relative_to(replacement.parent): raise ValueError('更新诊断来源必须位于隔离库与证据目录之外。') replacement_manifest = copy_verified(replacement.parent, root/'source-replacement') staged_replacement = root/'source-replacement'/replacement.name replacement_identity = engineering_identity(staged_replacement) record['update'] = {'stage': 'backup', 'accepted': False, 'source': str(replacement)} record['accepted'] = False - with LibraryLock(library): + with LibraryLock(library, safe_to_release=lambda: owner_safe): copy_verified(library.parent, root/'library-before-update', scope=ManifestScope.library(name, library.name)) request = ManagementRequest('remove', library, name, evidence_dir=root) + owner_safe = False removal = adapter.remove(request, root/'native-remove') + owner_safe = removal.safe_to_continue record['update']['removal'] = asdict(removal) _write_json(root/'probe-result.json', record) if not removal.success or not removal.safe_to_continue: return record - missing = adapter.catalog(library, root/'after-remove') + missing = read_catalog(root/'after-remove') if any(e.name == name for e in missing.entries): record['update']['error'] = '原生移除后条目仍存在' _write_json(root/'probe-result.json', record) return record record['update']['stage'] = 'removed' _write_json(root/'probe-result.json', record) + owner_safe = False readd = adapter._worker({'operation': 'probe_readd', 'library': str(library), 'source': str(staged_replacement), 'name': name, 'top_level_block': replacement_top, 'probe_token': token}, root/'native-readd') + owner_safe = readd.get('safe_to_continue') is True record['update']['readd'] = readd if readd.get('success') and readd.get('safe_to_continue'): - updated = adapter.catalog(library, root/'reopen-update') + updated = read_catalog(root/'reopen-update') record['update']['catalog'] = asdict(updated) entries = [e for e in updated.entries if e.name == name and e.partition == 'Logical-Physical'] record['update']['accepted'] = (len(entries) == 1 @@ -483,7 +539,7 @@ def _catalog_com(job, root): else: if job['operation'] == 'probe_readd': validate_probe_readd(job, root) - library = app.OpenLibrary(str(Path(job['library']).resolve())) + library = app.OpenLibrary(str(absolute_preserving_drive(job['library']))) if refused: raise RuntimeError('目录读取需要升级中心库或选择流程;已拒绝,目标未获准自动升级。') if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve(): @@ -541,6 +597,9 @@ def management_worker_main(request_path): _, desktop = _current_desktop() if desktop != job['desktop'] or not desktop.startswith('CbbManagement_'): raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。') + if job.get('mapped_drive') and job.get('mapped_remote_root'): + from .network_path import ensure_mapped_drive_visible + ensure_mapped_drive_visible(job['mapped_drive'], job['mapped_remote_root']) if job['operation'] in ('catalog', 'probe_create_add', 'probe_readd'): result = _catalog_native(job, root) elif job['operation'] == 'remove': diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 0e7df00..28bc487 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -306,7 +306,9 @@ class MainWindow(QWidget): return def request(name, partition): return ManagementRequest(operation, Path(self.mentor_lmc_path or self.loaded_path), name, - partition, evidence_dir=self.data_root/'management', timeout_seconds=self.timeout_spin.value()) + partition, evidence_dir=self.data_root/'management', timeout_seconds=self.timeout_spin.value(), + mapped_drive=getattr(self.library_lease, 'drive', ''), + mapped_remote_root=getattr(self.library_lease, 'remote_root', '')) requests = ([request('', 'Logical-Physical')] if operation == 'add' else [request(record.name, 'Logical-Physical' if record.logical_type == 'LP' and record.partition_name in ('Reusable Blocks', 'LP') else record.partition_name) diff --git a/tests/test_management.py b/tests/test_management.py index 79adaef..8326d77 100644 --- a/tests/test_management.py +++ b/tests/test_management.py @@ -252,3 +252,52 @@ def test_verify_exception_after_upload_retains_coordination_lock(setup): with pytest.raises(LibraryBusy): with LibraryLock(request.library): pass + + +@pytest.mark.parametrize('nested', [True, False]) +def test_library_inside_source_tree_rejected_before_catalog_or_guard(setup, nested): + request, adapter, source = setup + library = source.parent/('nested/lib.lmc' if nested else 'lib.lmc') + library.parent.mkdir(exist_ok=True) + library.write_bytes(b'library') + adapter.catalog = lambda *a: pytest.fail('overlapping library reached native catalog') + result = execute(replace(request, operation='add', name='New', source=source, library=library), adapter) + assert result.status == 'BLOCKED' and not adapter.calls + assert not library.with_name('.lib.cbb-upload.lock').exists() + assert not request.evidence_dir.exists() + + +def test_mapped_library_spelling_and_context_survive_engine_verify_boundary(setup, monkeypatch): + import shutil + from cbb_refresh_verify.verify_runner import VerifyResult + request, adapter, source = setup + alias_root = request.library.parent + canonical_root = alias_root.with_name('canonical-library') + shutil.copytree(alias_root, canonical_root) + original_resolve = Path.resolve + def resolve(path, *a, **k): + if path.is_relative_to(alias_root): + return original_resolve(canonical_root/path.relative_to(alias_root), *a, **k) + return original_resolve(path, *a, **k) + monkeypatch.setattr(Path, 'resolve', resolve) + catalogs, verified = [], [] + catalog = adapter.catalog + adapter.catalog = lambda library, evidence: (catalogs.append(library) or catalog(library, evidence)) + def verifier(req, **k): + verified.append(req) + return VerifyResult('PASS', library_flag='GREEN') + request = replace(request, operation='add', name='New', source=source, + mapped_drive='S:', mapped_remote_root=r'\\server\share') + result = execute(request, adapter, verifier=verifier) + assert result.status == 'UPLOADED_VERIFIED' + assert catalogs == [request.library, request.library] + assert verified[0].lmc_file == request.library + assert verified[0].mapped_drive == 'S:' and verified[0].mapped_remote_root == r'\\server\share' + + +def test_source_inside_target_library_rejected_before_catalog(setup): + request, adapter, _ = setup + source = request.library.parent/'ReusableBlocksLP/Block/Work/Block.prj' + adapter.catalog = lambda *a: pytest.fail('overlapping source reached native catalog') + result = execute(replace(request, operation='add', name='New', source=source), adapter) + assert result.status == 'BLOCKED' and not request.evidence_dir.exists() diff --git a/tests/test_management_probe_guards.py b/tests/test_management_probe_guards.py new file mode 100644 index 0000000..96cf4a0 --- /dev/null +++ b/tests/test_management_probe_guards.py @@ -0,0 +1,189 @@ +"""Exercise actual guards/snapshots; only the native worker transport is fake.""" +from dataclasses import asdict +import json +from pathlib import Path + +import pytest + + +@pytest.fixture(autouse=True) +def prohibit_native_launch(monkeypatch): + """Backstop independent of the boundary under development: no live apps.""" + import subprocess, win32process, win32com.client + import cbb_refresh_verify.desktop_worker as desktop + def forbidden(*a, **k): + raise AssertionError('Native/process launch is forbidden in probe-guard tests') + monkeypatch.setattr(desktop, 'DesktopWorker', forbidden) + monkeypatch.setattr(win32process, 'CreateProcess', forbidden) + monkeypatch.setattr(subprocess, 'Popen', forbidden) + monkeypatch.setattr(win32com.client, 'Dispatch', forbidden) + monkeypatch.setattr(win32com.client, 'DispatchEx', forbidden) + + +@pytest.fixture +def probe_files(tmp_path): + def source(root, data): + (root/'database').mkdir(parents=True) + (root/'database/icdb.dat').write_bytes(data) + (root/'PCB/Layout').mkdir(parents=True) + (root/'PCB/Layout/LayoutDB.lyt').write_bytes(data) + (root/'PCB/pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n') + (root/'PCB/Block.pcb').write_text('KEY ProjectFile "../Block.prj"\n') + project = root/'Block.prj' + project.write_text('KEY RootBlock "Block"\nKEY iCDBDir "database"\nKEY PCBDesignPath "PCB/Block.pcb"\n') + return project + original = source(tmp_path/'original', b'original') + replacement = source(tmp_path/'replacement', b'replacement') + root = tmp_path/'probe' + yield original, replacement, root + # Only file fakes ran; close this test's deliberately retained OS handle. + import cbb_refresh_verify.library_lock as locks + library = root/'native-add/fixture/mentor_lib.lmc' + with locks._guard: + owner = locks._owners.pop(locks._key(library.with_name('.mentor_lib.cbb-upload.lock')), None) + if owner: + owner['handle'].Close() + + +@pytest.mark.parametrize('fault', ['create_unsafe', 'first_catalog_exception', 'remove_unsafe', + 'remove_exception', 'after_remove_exception', 'readd_unsafe', 'readd_exception', 'final_catalog_exception']) +def test_isolated_probe_retains_guard_on_uncertain_native_lifecycle(probe_files, monkeypatch, fault): + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.library_lock as locks + from cbb_refresh_verify.management import Capability, CatalogEntry, engineering_identity + original, replacement, root = probe_files + records = [] + monkeypatch.setattr(native.NativeManagementAdapter, 'capabilities', lambda self: + {'remove': Capability(True, 'file transport fixture')}) + def launch(self, job, evidence): + nonlocal records + evidence = Path(evidence) + operation = job['operation'] + library = Path(job['library']) + if operation == 'probe_create_add': + # Directory creation belongs to the real supervising adapter. + assert native.prepare_probe_library(job, evidence) == library + library.write_bytes(b'native-created fixture') + (root/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': job['probe_token']})) + records = [CatalogEntry(job['name'], 'Logical-Physical', engineering_identity(job['source']))] + if fault == 'create_unsafe': + return {'success': False, 'safe_to_continue': False, 'detail': 'creation owner retained'} + elif operation == 'remove': + if fault == 'remove_exception': + raise TimeoutError('native removal transport disconnected') + if fault == 'remove_unsafe': + return {'success': False, 'safe_to_continue': False, 'detail': 'removal owner retained'} + records = [] + elif operation == 'probe_readd': + if fault == 'readd_exception': + raise TimeoutError('native readd transport disconnected') + if fault == 'readd_unsafe': + return {'success': False, 'safe_to_continue': False, 'detail': 'readd owner retained'} + records = [CatalogEntry(job['name'], 'Logical-Physical', engineering_identity(job['source']))] + elif operation == 'catalog': + stage_fault = {'reopen': 'first_catalog_exception', 'after-remove': 'after_remove_exception', + 'reopen-update': 'final_catalog_exception'}.get(evidence.name) + if stage_fault == fault: + raise TimeoutError('catalog owner release unknown') + return {'success': True, 'safe_to_continue': True, 'library': str(library), 'flow': 1, + 'entries': [asdict(entry) for entry in records], 'detail': 'file fixture result'} + class Worker: + def __init__(self, command, desktop): + request = Path(command[-1]) + job = json.loads(request.read_text(encoding='utf-8')) + result = launch(None, job, request.parent) + (request.parent/'result.json').write_text(json.dumps(result), encoding='utf-8') + def poll(self): return 0 + def close(self): pass + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker) + try: + native.run_isolated_probe(original, root, 'Block', replacement_source=replacement) + except TimeoutError: + pass + library = root/'native-add/fixture/mentor_lib.lmc' + assert locks.has_blocker(library) + with pytest.raises(locks.LibraryBusy): + with locks.LibraryLock(library): pass + assert (root/'probe-result.json').is_file() + from contextlib import contextmanager + @contextmanager + def denied(*a): + raise locks.LibraryBusy('fresh proof still sees a native owner') + yield + monkeypatch.setattr(locks, '_prove_library_released', denied) + with pytest.raises(locks.LibraryBusy): + locks.release_retained_library(library, root/'release-checks') + assert locks.has_blocker(library) + @contextmanager + def released(*a): + yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} + monkeypatch.setattr(locks, '_prove_library_released', released) + assert locks.release_retained_library(library, root/'release-checks')['status'] == 'RELEASE_CONFIRMED' + with locks.LibraryLock(library): pass + + +def test_mapping_context_is_serialized_and_restored_before_catalog(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.network_path as network + captured = [] + class Worker: + def __init__(self, command, desktop): + request = Path(command[-1]) + job = json.loads(request.read_text(encoding='utf-8')) + captured.append(job) + (request.parent/'result.json').write_text(json.dumps({'success': True, 'safe_to_continue': True, + 'library': job['library'], 'flow': 1, 'entries': []}), encoding='utf-8') + def poll(self): return 0 + def close(self): pass + adapter = native.NativeManagementAdapter(worker_factory=Worker) + adapter.bind_library_mapping('S:', r'\\server\share') + library = Path('S:/library/mentor.lmc') + adapter.catalog(library, tmp_path/'catalog') + assert captured[0]['library'] == str(library) + assert captured[0]['mapped_drive'] == 'S:' + order = [] + monkeypatch.setattr(native, '_current_desktop', lambda: (None, captured[0]['desktop'])) + monkeypatch.setattr(network, 'ensure_mapped_drive_visible', lambda drive, remote: + order.append(('mapping', drive, remote))) + monkeypatch.setattr(native, '_catalog_native', lambda job, root: + (order.append(('catalog', job['library'])) or {'success': True, 'safe_to_continue': True})) + assert native.management_worker_main(tmp_path/'catalog/request.json') == 0 + assert order == [('mapping', 'S:', r'\\server\share'), ('catalog', str(library))] + + +def test_removal_cli_keeps_mapped_drive_spelling(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.management import Capability + job = {'operation': 'remove', 'library': 'S:\\library\\mentor.lmc', 'name': 'Block', + 'partition': 'Logical-Physical', 'desktop': 'CbbManagement_fixture', + 'mentor_home': str(tmp_path), 'timeout_seconds': 10} + request = tmp_path/'request.json'; request.write_text(json.dumps(job), encoding='utf-8') + monkeypatch.setattr(native, '_current_desktop', lambda: (None, job['desktop'])) + monkeypatch.setattr(native.NativeManagementAdapter, 'capabilities', lambda self: {'remove': Capability(True, 'fake transport')}) + commands = [] + class Process: + pid = 1234 + def __init__(self, command, *, stdout, **kwargs): + commands.append(command) + stdout.write(b"# Note: Removed REUSE_BLOCK 'Block' from partition 'Logical-Physical'.\n") + stdout.flush() + def wait(self, **kwargs): return 0 + monkeypatch.setattr(native.subprocess, 'Popen', Process) + assert native.management_worker_main(request) == 0 + assert commands[0][1] == job['library'] + + +def test_staged_project_alignment_keeps_selected_mapped_drive(tmp_path, monkeypatch): + from cbb_refresh_verify.backup_recovery import _align_library + project = tmp_path/'Block.prj' + project.write_text('KEY CentralLibrary "old.lmc"\n', encoding='utf-8') + pcb = tmp_path/'PCB/Block.pcb'; pcb.parent.mkdir(); pcb.touch() + pcb_project = pcb.parent/'pcb.prj' + pcb_project.write_text('KEY CentralLibrary "old.lmc"\n', encoding='utf-8') + library = Path('S:/library/mentor.lmc') + original_resolve = Path.resolve + monkeypatch.setattr(Path, 'resolve', lambda path, *a, **k: + Path(r'\\server\share\library\mentor.lmc') if path == library else original_resolve(path, *a, **k)) + _align_library(project, pcb, library) + for path in (project, pcb_project): + assert 'KEY CentralLibrary "S:\\library\\mentor.lmc"' in path.read_text(encoding='utf-8') diff --git a/tests/test_native_management.py b/tests/test_native_management.py index 336a7ce..0597702 100644 --- a/tests/test_native_management.py +++ b/tests/test_native_management.py @@ -78,25 +78,38 @@ def test_probe_cannot_use_existing_fixture_or_mutate_source(tmp_path): def test_probe_requires_owned_new_native_library_path(tmp_path): + import os from cbb_refresh_verify.native_management import prepare_probe_library + from cbb_refresh_verify.library_lock import LibraryLock outside = tmp_path/'production.lmc'; outside.write_bytes(b'unchanged') root = tmp_path/'worker'; root.mkdir() with pytest.raises(ValueError): prepare_probe_library({'library': str(outside)}, root) assert outside.read_bytes() == b'unchanged' owned = root/'fixture/mentor_lib.lmc' - assert prepare_probe_library({'library': str(owned)}, root) == owned - owned.write_bytes(b'first creation') - with pytest.raises(ValueError): - prepare_probe_library({'library': str(owned)}, root) + owned.parent.mkdir() + job = {'library': str(owned), 'probe_token': 'test-token', 'supervisor_pid': os.getpid()} + (root/'probe-prepared.json').write_text(json.dumps({'library': str(owned), 'token': 'test-token', + 'supervisor_pid': os.getpid()}), encoding='utf-8') + with LibraryLock(owned): + assert prepare_probe_library(job, root) == owned + owned.write_bytes(b'first creation') + with pytest.raises(ValueError): + prepare_probe_library(job, root) @pytest.mark.parametrize('records', [[], [('Actual', 'Logical-Physical')]]) -def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records): +@pytest.mark.parametrize('mapped', [False, True]) +def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records, mapped): import cbb_refresh_verify.native_management as native import cbb_refresh_verify.mentor_adapter as mentor import win32com.client library_path = tmp_path/'lib.lmc'; library_path.touch() + if mapped: + original_resolve = Path.resolve + def resolve(path, *a, **k): + return tmp_path/'canonical/lib.lmc' if path == library_path else original_resolve(path, *a, **k) + monkeypatch.setattr(Path, 'resolve', resolve) (tmp_path/'ReusableBlocksLP/Ghost').mkdir(parents=True) class Collection: def __init__(self, items): self.items = items; self.Count = len(items) -- 2.39.5 From ffa7e84d43f44a10ac30835f648186e421d9378a 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: Mon, 2 Nov 2026 22:19:59 +0800 Subject: [PATCH 13/59] fix: retain unknown startup windows and expose native dispatch errors --- src/cbb_refresh_verify/failed_session.py | 11 +++- src/cbb_refresh_verify/native_management.py | 45 +++++++++++++-- tests/test_failed_startup.py | 44 +++++++++++++++ tests/test_management_diagnostics.py | 62 +++++++++++++++++++++ 4 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 tests/test_management_diagnostics.py diff --git a/src/cbb_refresh_verify/failed_session.py b/src/cbb_refresh_verify/failed_session.py index 4ecdf13..8f60548 100644 --- a/src/cbb_refresh_verify/failed_session.py +++ b/src/cbb_refresh_verify/failed_session.py @@ -103,7 +103,14 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: and win32gui.GetWindowText(w).strip() in ( 'Expedition PCB', 'Expedition PCB - Reusable Block') and win32gui.IsWindowEnabled(w)] - dialogs = any(win32gui.GetClassName(w) == '#32770' for w in windows) + observed = [{'hwnd': w, 'class': win32gui.GetClassName(w), + 'title': win32gui.GetWindowText(w), 'enabled': bool(win32gui.IsWindowEnabled(w))} + for w in windows] + benign = [w for w in observed if w['title'] == '' and w['class'] in ( + 'UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator')] + unknown = [w for w in observed if w['hwnd'] not in main and w not in benign] + result.update(owned_windows=observed, unknown_windows=unknown, + benign_auxiliary_windows=benign) documents = [] def child(hwnd, _): if (win32gui.GetClassName(hwnd) == 'MDIClient' @@ -112,7 +119,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: if len(main) == 1: win32gui.EnumChildWindows(main[0], child, None) addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb']) - if len(main) == 1 and not dialogs and not documents and not addresses: + if len(main) == 1 and not unknown and not documents and not addresses: win32gui.PostMessage(main[0], win32con.WM_CLOSE, 0, 0) sent = True emit(stage='startup_close_requested', pcb_pid=pcb_pid, hwnd=main[0]) diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 3a35f35..bfcdd93 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -218,23 +218,48 @@ def _close_probe_dialogs(desktop, owned_pids): return # Never close a parent with an unreleased child. -def _probe_add_dialog(app, job, desktop, owned_pids, root): +def _exception_details(error): + import traceback + return {'type': f'{type(error).__module__}.{type(error).__name__}', 'message': str(error), + 'args': json.loads(json.dumps(error.args, default=str)), + 'hresult': getattr(error, 'hresult', None), + 'traceback': ''.join(traceback.format_exception(type(error), error, error.__traceback__))} + + +def _dispatch_diagnostics(root, stage, app, library, *, paths=None): + """Read only three known DISPIDs, preserving errors hidden by dynamic COM.""" + record = {'stage': stage, 'members': {}} + for label, obj, name in (('application.OnReusableBlocksEditor', app, 'OnReusableBlocksEditor'), + ('application.Quit', app, 'Quit'), ('library.Close', library, 'Close')): + try: + record['members'][label] = {'dispid': obj._oleobj_.GetIDsOfNames(name)} + except Exception as error: + record['members'][label] = {'error': _exception_details(error)} + if paths is not None: + record['paths'] = paths + with (Path(root)/'dispatch-diagnostics.jsonl').open('a', encoding='utf-8') as stream: + stream.write(json.dumps(record, ensure_ascii=False, default=str) + '\n') + + +def _probe_add_dialog(app, job, desktop, owned_pids, root, *, library=None): try: - return _probe_add_dialog_impl(app, job, desktop, owned_pids, root) + return _probe_add_dialog_impl(app, job, desktop, owned_pids, root, library=library) finally: import gc gc.collect() # Release UIA wrappers before closing native child dialogs. _close_probe_dialogs(desktop, owned_pids) -def _probe_add_dialog_impl(app, job, desktop, owned_pids, root): +def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None): """Diagnostic only: proven editor/UIA tab and resource1103 controls. This function has no production capability override. It is called only after this worker itself creates a new disposable library. """ import pythoncom, win32con, win32gui, win32process + _dispatch_diagnostics(root, 'before-pywinauto-import', app, library) from pywinauto import Desktop + _dispatch_diagnostics(root, 'after-pywinauto-import', app, library) from .backup_recovery import _align_library from .management import source_project source, pcb, top = source_project(job['source'], job.get('top_level_block', '')) @@ -242,6 +267,8 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root): if not source.is_relative_to(Path(root).parent.resolve()/source_folder): raise ValueError('隔离新增只允许使用本次复制的来源工程。') _align_library(source, pcb, Path(job['library'])) + _dispatch_diagnostics(root, 'after-source-alignment', app, library, + paths={'source': str(source), 'pcb': str(pcb), 'library_argument': job['library']}) validate_name(job['name']) deadline = time.monotonic() + float(job['timeout_seconds']) def find_window(title): @@ -545,7 +572,7 @@ def _catalog_com(job, root): if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve(): raise RuntimeError('原生返回中心库身份不一致。') if job['operation'] in ('probe_create_add', 'probe_readd'): - _probe_add_dialog(app, job, desktop, owned_pids, root) + _probe_add_dialog(app, job, desktop, owned_pids, root, library=library) entries = [] parts = library.Partitions(7) for i in range(1, int(parts.Count) + 1): @@ -569,14 +596,19 @@ def _catalog_com(job, root): result.update(success=True, library=str(library.FullName), flow=int(library.FlowType), entries=entries) except Exception as error: result['detail'] = str(error) + result['exception'] = _exception_details(error) finally: if library is not None: try: library.Close() - except Exception as error: result['close_detail'] = str(error) + except Exception as error: + result['close_detail'] = str(error) + result['close_exception'] = _exception_details(error) library = None if app is not None: try: app.Quit() - except Exception as error: result['quit_detail'] = str(error) + except Exception as error: + result['quit_detail'] = str(error) + result['quit_exception'] = _exception_details(error) app = None stop.set() if watcher: @@ -630,6 +662,7 @@ def management_worker_main(request_path): raise ValueError('未验证的原生管理操作') except Exception as error: result['detail'] = str(error) + result['exception'] = _exception_details(error) finally: _write_json(root/'result.json', result) return 0 if result['success'] and result['safe_to_continue'] else 2 diff --git a/tests/test_failed_startup.py b/tests/test_failed_startup.py index 28b652f..c01fb0f 100644 --- a/tests/test_failed_startup.py +++ b/tests/test_failed_startup.py @@ -186,3 +186,47 @@ def test_startup_exit_does_not_hide_late_database_address(tmp_path, monkeypatch) result = failed_session.close_failed_startup(job, 12, timeout_seconds=0) assert result['status'] == 'RETAINED' assert result['release']['addresses'] + + +@pytest.mark.parametrize('window_class,title', [('CustomPrompt', 'Database action pending'), + ('Afx:tool', ''), ('UAC_InputIndicatorOverlayWnd', 'Unknown prompt'), ('UAC Input Indicator', ' ')]) +def test_enabled_empty_main_with_unaccounted_owned_window_is_never_closed(tmp_path, monkeypatch, window_class, title): + import win32gui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(hwnd, arg) for hwnd in (100, 200)]) + monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'Afx:main' if hwnd == 100 else window_class) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else title) + result = failed_session.close_failed_startup(job, 12, timeout_seconds=0) + assert result['status'] == 'RETAINED' and not result['release']['safe_to_continue'] + assert messages == [] and records + assert result['unknown_windows'] == [{'hwnd': 200, 'class': window_class, 'title': title, 'enabled': True}] + assert json.loads((tmp_path/'startup-close.json').read_text(encoding='utf-8'))['unknown_windows'] == result['unknown_windows'] + + +def test_empty_title_known_uac_windows_are_accounted_for_before_normal_close(tmp_path, monkeypatch): + import win32gui, win32con + job, records, messages = startup_fixture(tmp_path, monkeypatch) + classes = {100: 'Afx:main', 200: 'UAC_InputIndicatorOverlayWnd', 201: 'UAC Input Indicator'} + monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(hwnd, arg) for hwnd in classes]) + monkeypatch.setattr(win32gui, 'GetClassName', classes.__getitem__) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else '') + result = failed_session.close_failed_startup(job, 12, timeout_seconds=0) + assert result['status'] == 'CLOSED' and messages == [(100, win32con.WM_CLOSE)] + assert result['unknown_windows'] == [] + assert [entry['hwnd'] for entry in result['benign_auxiliary_windows']] == [200, 201] + + +def test_custom_prompt_must_disappear_before_main_close(tmp_path, monkeypatch): + from types import SimpleNamespace + import win32gui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + ticks = [0.0] + monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: + [fn(hwnd, arg) for hwnd in ((100, 200) if ticks[0] == 0 else (100,))]) + monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'Afx:main' if hwnd == 100 else 'CustomPrompt') + monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else 'Still processing') + monkeypatch.setattr(failed_session, 'time', SimpleNamespace(monotonic=lambda: ticks[0], + sleep=lambda seconds: ticks.__setitem__(0, ticks[0] + seconds))) + result = failed_session.close_failed_startup(job, 12, timeout_seconds=1) + assert result['status'] == 'CLOSED' and ticks[0] > 0 + assert len(messages) == 1 diff --git a/tests/test_management_diagnostics.py b/tests/test_management_diagnostics.py new file mode 100644 index 0000000..a96b802 --- /dev/null +++ b/tests/test_management_diagnostics.py @@ -0,0 +1,62 @@ +import json +from pathlib import Path +from types import ModuleType, SimpleNamespace + +import pytest + + +def test_raw_dispatch_diagnostics_preserve_hresult_and_known_member_ids(tmp_path): + from cbb_refresh_verify.native_management import _dispatch_diagnostics + class Error(RuntimeError): + hresult = -2147221008 + class Raw: + def GetIDsOfNames(self, name): + if name == 'Quit': raise Error('COM apartment unavailable') + return 17 if name == 'Close' else 21 + obj = SimpleNamespace(_oleobj_=Raw()) + _dispatch_diagnostics(tmp_path, 'before-import', obj, obj) + record = json.loads((tmp_path/'dispatch-diagnostics.jsonl').read_text(encoding='utf-8')) + assert record['members']['application.OnReusableBlocksEditor']['dispid'] == 21 + assert record['members']['library.Close']['dispid'] == 17 + error = record['members']['application.Quit']['error'] + assert error['hresult'] == -2147221008 and error['args'] == ['COM apartment unavailable'] + assert error['type'].endswith('Error') + + +def test_probe_records_dispatch_before_after_import_and_after_source_alignment(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + import sys + source = tmp_path/'source/Block.prj' + source.parent.mkdir() + source.write_text('KEY RootBlock "Block"\nKEY PCBDesignPath "PCB/Block.pcb"\n' + 'KEY iCDBDir "database"\nKEY CentralLibrary "old.lmc"\n') + (source.parent/'database').mkdir(); (source.parent/'database/icdb.dat').write_bytes(b'fixture') + pcb = source.parent/'PCB/Block.pcb'; (pcb.parent/'Layout').mkdir(parents=True) + pcb.write_text('KEY ProjectFile "../Block.prj"\n') + (pcb.parent/'pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n') + (pcb.parent/'Layout/LayoutDB.lyt').write_bytes(b'fixture') + root = tmp_path/'native-add'; root.mkdir() + library = root/'fixture/mentor_lib.lmc'; library.parent.mkdir(); library.touch() + phase = ['before'] + class Raw: + def GetIDsOfNames(self, name): return 1 if phase[0] == 'before' else 2 + class App: + _oleobj_ = Raw() + def OnReusableBlocksEditor(self): raise RuntimeError('stop before native UI') + class Uia(ModuleType): + def __getattr__(self, name): + if name == 'Desktop': + phase[0] = 'after' + return None + raise AttributeError(name) + monkeypatch.setitem(sys.modules, 'pywinauto', Uia('pywinauto')) + job = {'operation': 'probe_create_add', 'source': str(source), 'library': str(library), + 'name': 'Block', 'timeout_seconds': 0} + with pytest.raises(RuntimeError, match='stop before native UI'): + native._probe_add_dialog_impl(App(), job, None, set(), root, library=SimpleNamespace(_oleobj_=Raw())) + records = [json.loads(line) for line in (root/'dispatch-diagnostics.jsonl').read_text(encoding='utf-8').splitlines()] + assert [r['stage'] for r in records] == ['before-pywinauto-import', 'after-pywinauto-import', 'after-source-alignment'] + assert records[0]['members']['application.Quit']['dispid'] == 1 + assert records[1]['members']['application.Quit']['dispid'] == 2 + assert records[2]['paths'] == {'source': str(source), 'pcb': str(pcb), 'library_argument': str(library)} + assert 'old.lmc' not in source.read_text() -- 2.39.5 From 1f69480ed81c37921e8fc9a6b549507547e265f4 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: Mon, 2 Nov 2026 22:32:32 +0800 Subject: [PATCH 14/59] fix: preload UIA before native proxies and package timezone support --- packaging/CbbRefreshVerify.spec | 2 +- src/cbb_refresh_verify/native_management.py | 4 ++ tests/test_management_diagnostics.py | 46 +++++++++++++++++++++ tests/test_packaging.py | 14 +++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/packaging/CbbRefreshVerify.spec b/packaging/CbbRefreshVerify.spec index f535d9f..69e2bcc 100644 --- a/packaging/CbbRefreshVerify.spec +++ b/packaging/CbbRefreshVerify.spec @@ -18,7 +18,7 @@ datas=[(str(project_root / 'src' / 'cbb_refresh_verify' / 'save_verified_block.v (str(assets_root / 'cbb_refresh_verify.png'), 'cbb_refresh_verify/assets'), (str(assets_root / 'cbb_refresh_verify.svg'), 'cbb_refresh_verify/assets'), (str(assets_root / 'dialog_rules.v1.json'), 'cbb_refresh_verify/assets')], - hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.ui.management_dialog','cbb_refresh_verify.management','cbb_refresh_verify.native_management','cbb_refresh_verify.library_lock','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'], + hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32timezone','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.ui.management_dialog','cbb_refresh_verify.management','cbb_refresh_verify.native_management','cbb_refresh_verify.library_lock','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'], excludes=['PySide6.QtDesigner','PySide6.QtMultimedia','PySide6.QtMultimediaWidgets','PySide6.QtOpenGL','PySide6.QtOpenGLWidgets','PySide6.QtPdf','PySide6.QtPdfWidgets','PySide6.QtQml','PySide6.QtQuick','PySide6.QtQuickWidgets','PySide6.QtWebChannel','PySide6.QtWebEngineCore','PySide6.QtWebEngineWidgets'], ) excluded_qt_binaries = {'icudt78.dll','icuuc.dll','qpdf.dll','qt6pdf.dll','qt6qml.dll','qt6qmlmeta.dll','qt6qmlmodels.dll','qt6qmlworkerscript.dll','qt6quick.dll','qt6quickwidgets.dll'} diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index bfcdd93..571ca2a 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -499,6 +499,10 @@ def _wait_native_exit(pids, timeout=10): def _catalog_native(job, root): import gc, pythoncom from .com_runtime import initialize_com, uninitialize_com + if job.get('operation') in ('probe_create_add', 'probe_readd'): + # The installed UIA import disconnects existing Mentor proxies + # (CO_E_OBJNOTCONNECTED). Finish its initialization before creating any. + from pywinauto import Desktop initialized = initialize_com() try: result = _catalog_com(job, root) diff --git a/tests/test_management_diagnostics.py b/tests/test_management_diagnostics.py index a96b802..aa52c73 100644 --- a/tests/test_management_diagnostics.py +++ b/tests/test_management_diagnostics.py @@ -60,3 +60,49 @@ def test_probe_records_dispatch_before_after_import_and_after_source_alignment(t assert records[1]['members']['application.Quit']['dispid'] == 2 assert records[2]['paths'] == {'source': str(source), 'pcb': str(pcb), 'library_argument': str(library)} assert 'old.lmc' not in source.read_text() + + +@pytest.mark.parametrize('operation', ['probe_create_add', 'probe_readd', 'catalog']) +def test_probe_preloads_uia_before_com_proxy_creation_but_catalog_stays_read_only(tmp_path, monkeypatch, operation): + import sys, win32com.client + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.mentor_adapter as mentor + root = tmp_path/'job'; root.mkdir() + library_path = tmp_path/'fixture.lmc'; library_path.touch() + events, running = [], [False] + class Uia(ModuleType): + def __getattr__(self, name): + if name == 'Desktop': + events.append('uia-loaded') + return object + raise AttributeError(name) + monkeypatch.setitem(sys.modules, 'pywinauto', Uia('pywinauto')) + library = SimpleNamespace(FullName=str(library_path), FlowType=1, + Partitions=lambda kind: SimpleNamespace(Count=0), Close=lambda: events.append('library-close')) + class App: + def OpenLibrary(self, path): return library + def CreateLibrary(self, path, flow): return library + def Quit(self): events.append('quit'); running[0] = False + def dispatch(progid): + events.append('dispatch'); running[0] = True + return App() + monkeypatch.setattr(win32com.client, 'DispatchEx', dispatch) + monkeypatch.setattr(native, '_current_desktop', lambda: + (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test')) + monkeypatch.setattr(mentor, '_process_details', lambda name: + [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if running[0] else []) + monkeypatch.setattr(native, 'prepare_probe_library', lambda *a: library_path) + monkeypatch.setattr(native, 'validate_probe_readd', lambda *a: library_path) + class Disconnected(RuntimeError): + hresult = -2147220995 + def editor(*a, **k): raise Disconnected('controlled native failure') + monkeypatch.setattr(native, '_probe_add_dialog', editor) + monkeypatch.setattr(native, '_wait_native_exit', lambda pids: True) + result = native._catalog_native({'operation': operation, 'desktop': 'CbbManagement_test', + 'library': str(library_path), 'mentor_home': str(tmp_path), 'probe_token': 'fixture'}, root) + assert events[:2] == (['dispatch', 'library-close'] if operation == 'catalog' else ['uia-loaded', 'dispatch']) + if operation == 'catalog': + assert result['success'] and 'uia-loaded' not in events + else: + assert result['exception']['hresult'] == -2147220995 + assert result['exception']['args'] == ['controlled native failure'] diff --git a/tests/test_packaging.py b/tests/test_packaging.py index b568cfc..315c4b9 100644 --- a/tests/test_packaging.py +++ b/tests/test_packaging.py @@ -12,3 +12,17 @@ def test_spec_packages_adaptive_dialog_rules_and_uia_modules(): text = Path('packaging/CbbRefreshVerify.spec').read_text(encoding='utf-8') assert 'dialog_rules.v1.json' in text assert 'pywinauto' in text + + +def test_spec_analysis_includes_pywin32_timezone_dynamic_import(): + from types import SimpleNamespace + captured = {} + def analysis(*args, **kwargs): + captured.update(kwargs) + return SimpleNamespace(pure=[], scripts=[], binaries=[], zipfiles=[], datas=[]) + path = Path('packaging/CbbRefreshVerify.spec') + namespace = {'SPECPATH': str(path.parent.resolve()), 'Analysis': analysis, + 'PYZ': lambda *a, **k: None, 'EXE': lambda *a, **k: None, + 'COLLECT': lambda *a, **k: None} + exec(compile(path.read_text(encoding='utf-8'), str(path), 'exec'), namespace) + assert 'win32timezone' in captured['hiddenimports'] -- 2.39.5 From bb98fe66dd4f5e128f438b7882ee692f21f3240a 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: Mon, 2 Nov 2026 22:36:23 +0800 Subject: [PATCH 15/59] fix: normalize native editor handles before UIA calls --- src/cbb_refresh_verify/native_management.py | 2 +- tests/test_management_diagnostics.py | 30 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 571ca2a..1721f9a 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -275,7 +275,7 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) for hwnd in desktop.EnumDesktopWindows(): if (win32process.GetWindowThreadProcessId(hwnd)[1] in owned_pids and win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) == title): - return hwnd + return int(hwnd) return None def await_window(title): while time.monotonic() < deadline: diff --git a/tests/test_management_diagnostics.py b/tests/test_management_diagnostics.py index aa52c73..e1dfd5d 100644 --- a/tests/test_management_diagnostics.py +++ b/tests/test_management_diagnostics.py @@ -106,3 +106,33 @@ def test_probe_preloads_uia_before_com_proxy_creation_but_catalog_stays_read_onl else: assert result['exception']['hresult'] == -2147220995 assert result['exception']['args'] == ['controlled native failure'] + + +def test_probe_converts_desktop_pyhandle_before_uia_boundary(tmp_path, monkeypatch): + import sys, win32gui, win32process + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.management as management + import cbb_refresh_verify.backup_recovery as recovery + source = tmp_path/'source/Block.prj' + pcb = source.parent/'PCB/Block.pcb' + root = tmp_path/'native-add'; root.mkdir() + class Handle: + def __int__(self): return 200 + handle = Handle() + desktop = SimpleNamespace(EnumDesktopWindows=lambda: [handle]) + monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 9)) + monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda hwnd: True) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Reusable Blocks') + monkeypatch.setattr(management, 'source_project', lambda *a: (source, pcb, 'Block')) + monkeypatch.setattr(recovery, '_align_library', lambda *a: None) + class Desktop: + def __init__(self, **kwargs): pass + def window(self, *, handle): + assert type(handle) is int and handle == 200 + raise RuntimeError('integer UIA boundary reached') + monkeypatch.setitem(sys.modules, 'pywinauto', SimpleNamespace(Desktop=Desktop)) + app = SimpleNamespace(OnReusableBlocksEditor=lambda: None) + job = {'operation': 'probe_create_add', 'source': str(source), 'name': 'Block', + 'library': str(root/'fixture/mentor_lib.lmc'), 'timeout_seconds': 5} + with pytest.raises(RuntimeError, match='integer UIA boundary reached'): + native._probe_add_dialog_impl(app, job, desktop, {9}, root) -- 2.39.5 From 8e486c109700b13768af6ca5492a34721dc11b91 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: Mon, 2 Nov 2026 22:44:50 +0800 Subject: [PATCH 16/59] fix: bound native edit messages and identify empty IE infrastructure --- src/cbb_refresh_verify/failed_session.py | 9 ++++ src/cbb_refresh_verify/native_management.py | 45 ++++++++++++++-- tests/test_failed_startup.py | 23 ++++++++ tests/test_native_management_fields.py | 58 +++++++++++++++++++++ 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 tests/test_native_management_fields.py diff --git a/src/cbb_refresh_verify/failed_session.py b/src/cbb_refresh_verify/failed_session.py index 8f60548..6f233fb 100644 --- a/src/cbb_refresh_verify/failed_session.py +++ b/src/cbb_refresh_verify/failed_session.py @@ -108,6 +108,15 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: for w in windows] benign = [w for w in observed if w['title'] == '' and w['class'] in ( 'UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator')] + for window in observed: + if window['class'] == 'Internet Explorer_Hidden' and window['title'] == '': + window['rect'] = list(win32gui.GetWindowRect(window['hwnd'])) + window['children'] = [] + win32gui.EnumChildWindows(window['hwnd'], + lambda hwnd, _: window['children'].append(int(hwnd)), None) + left, top, right, bottom = window['rect'] + if left == right and top == bottom and not window['children']: + benign.append(window) unknown = [w for w in observed if w['hwnd'] not in main and w not in benign] result.update(owned_windows=observed, unknown_windows=unknown, benign_auxiliary_windows=benign) diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 1721f9a..5bb908f 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -241,6 +241,47 @@ def _dispatch_diagnostics(root, stage, app, library, *, paths=None): stream.write(json.dumps(record, ensure_ascii=False, default=str) + '\n') +def _set_probe_field(hwnd, control_id, expected, root): + """Set/read a remote Edit with a one-second total message budget.""" + from ctypes import wintypes + record = {'control_id': control_id, 'hwnd': int(hwnd), 'expected': expected, + 'actual': None, 'matched': False} + deadline = time.monotonic() + 1.0 + try: + user32 = ctypes.WinDLL('user32', use_last_error=True) + send = user32.SendMessageTimeoutW + send.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM, + wintypes.UINT, wintypes.UINT, ctypes.POINTER(ctypes.c_size_t)] + send.restype = wintypes.LPARAM + def message(code, count, buffer): + remaining = min(1000, int((deadline - time.monotonic()) * 1000)) + if remaining <= 0: + raise TimeoutError(f'原生字段消息超时:{control_id}') + returned = ctypes.c_size_t() + if not send(int(hwnd), code, count, ctypes.addressof(buffer), 0x0002, + remaining, ctypes.byref(returned)): + raise TimeoutError(f'原生字段消息超时或失败:{control_id}, Windows={ctypes.get_last_error()}') + return returned.value + text = ctypes.create_unicode_buffer(expected) + if not message(0x000C, 0, text): # WM_SETTEXT + raise RuntimeError(f'原生字段拒绝写入:{control_id}') + buffer = ctypes.create_unicode_buffer(32768) + copied = message(0x000D, len(buffer), buffer) # WM_GETTEXT, remote Edit marshalled by Windows. + record['actual'] = buffer.value + if copied >= len(buffer) - 1: + raise RuntimeError(f'原生字段复读可能截断:{control_id}') + if record['actual'] != expected: + raise RuntimeError(f'原生新增字段回读失败:{control_id}') + record['matched'] = True + return record['actual'] + except Exception as error: + record['exception'] = _exception_details(error) + raise + finally: + with (Path(root)/'native-field-readback.jsonl').open('a', encoding='utf-8') as stream: + stream.write(json.dumps(record, ensure_ascii=False, default=str) + '\n') + + def _probe_add_dialog(app, job, desktop, owned_pids, root, *, library=None): try: return _probe_add_dialog_impl(app, job, desktop, owned_pids, root, library=library) @@ -309,9 +350,7 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) hwnd = win32gui.GetDlgItem(dialog, control) if not hwnd or win32gui.GetClassName(hwnd) != 'Edit': raise RuntimeError(f'原生新增字段身份不匹配:{control}') - win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, 0, value) - if win32gui.GetWindowText(hwnd) != value: - raise RuntimeError(f'原生新增字段回读失败:{control}') + _set_probe_field(hwnd, control, value, root) _write_json(root/'submitted-fields.json', fields) ok = win32gui.GetDlgItem(dialog, 1083) if not ok: diff --git a/tests/test_failed_startup.py b/tests/test_failed_startup.py index c01fb0f..0062239 100644 --- a/tests/test_failed_startup.py +++ b/tests/test_failed_startup.py @@ -230,3 +230,26 @@ def test_custom_prompt_must_disappear_before_main_close(tmp_path, monkeypatch): result = failed_session.close_failed_startup(job, 12, timeout_seconds=1) assert result['status'] == 'CLOSED' and ticks[0] > 0 assert len(messages) == 1 + + +@pytest.mark.parametrize('obstacle', ['', 'title', 'area', 'children', 'near_class']) +def test_only_exact_empty_zero_area_childless_ie_infrastructure_is_benign(tmp_path, monkeypatch, obstacle): + import win32gui, win32con + job, records, messages = startup_fixture(tmp_path, monkeypatch) + monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(hwnd, arg) for hwnd in (100, 200)]) + monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'Afx:main' if hwnd == 100 else + 'Internet Explorer_Hidden_other' if obstacle == 'near_class' else 'Internet Explorer_Hidden') + monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else + 'Active prompt' if obstacle == 'title' else '') + monkeypatch.setattr(win32gui, 'GetWindowRect', lambda hwnd: (0, 0, 100, 50) if obstacle == 'area' else (0, 0, 0, 0)) + monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, fn, arg: + fn(201, arg) if hwnd == 200 and obstacle == 'children' else None) + result = failed_session.close_failed_startup(job, 12, timeout_seconds=0) + if obstacle: + assert result['status'] == 'RETAINED' and messages == [] + assert result['unknown_windows'][0]['hwnd'] == 200 + else: + assert result['status'] == 'CLOSED' and messages == [(100, win32con.WM_CLOSE)] + entry = result['benign_auxiliary_windows'][0] + assert entry['class'] == 'Internet Explorer_Hidden' and entry['rect'] == [0, 0, 0, 0] + assert entry['children'] == [] diff --git a/tests/test_native_management_fields.py b/tests/test_native_management_fields.py new file mode 100644 index 0000000..0e4a731 --- /dev/null +++ b/tests/test_native_management_fields.py @@ -0,0 +1,58 @@ +import ctypes +import json +from types import SimpleNamespace + +import pytest + + +@pytest.mark.parametrize('expected,actual', [('S:\\source\\项目.prj', 'S:\\source\\项目.prj'), ('', '')]) +def test_field_uses_bounded_set_and_remote_gettext_with_exact_readback(tmp_path, monkeypatch, expected, actual): + from cbb_refresh_verify import native_management as native + calls = [] + class Send: + def __call__(self, hwnd, message, wparam, lparam, flags, timeout, result): + calls.append((hwnd, message, flags, timeout)) + if message == 0x000C: + assert ctypes.wstring_at(lparam) == expected + result._obj.value = 1 + elif message == 0x000D: + buffer = ctypes.create_unicode_buffer(actual) + ctypes.memmove(lparam, ctypes.addressof(buffer), ctypes.sizeof(buffer)) + result._obj.value = len(actual) + else: + pytest.fail('unproven field message') + return 1 + monkeypatch.setattr(native.ctypes, 'WinDLL', lambda *a, **k: SimpleNamespace(SendMessageTimeoutW=Send())) + assert native._set_probe_field(123, 1217, expected, tmp_path) == actual + assert [item[1] for item in calls] == [0x000C, 0x000D] + assert all(item[0] == 123 and item[2] == 0x0002 and 0 < item[3] <= 1000 for item in calls) + record = json.loads((tmp_path/'native-field-readback.jsonl').read_text(encoding='utf-8')) + assert record['expected'] == expected and record['actual'] == actual and record['matched'] + + +@pytest.mark.parametrize('failure', ['empty', 'mismatch', 'set_timeout', 'read_timeout', 'set_rejected', 'truncated']) +def test_field_failure_is_recorded_before_rejection_without_foreground_fallback(tmp_path, monkeypatch, failure): + from cbb_refresh_verify import native_management as native + calls = [] + class Send: + def __call__(self, hwnd, message, wparam, lparam, flags, timeout, result): + calls.append(message) + if message == 0x000C: + result._obj.value = 0 if failure == 'set_rejected' else 1 + return 0 if failure == 'set_timeout' else 1 + if failure == 'read_timeout': return 0 + actual = '' if failure == 'empty' else 'other project' + buffer = ctypes.create_unicode_buffer(actual) + ctypes.memmove(lparam, ctypes.addressof(buffer), ctypes.sizeof(buffer)) + result._obj.value = wparam - 1 if failure == 'truncated' else len(actual) + return 1 + monkeypatch.setattr(native.ctypes, 'WinDLL', lambda *a, **k: SimpleNamespace(SendMessageTimeoutW=Send())) + with pytest.raises((RuntimeError, TimeoutError)): + native._set_probe_field(123, 1217, 'expected project', tmp_path) + record = json.loads((tmp_path/'native-field-readback.jsonl').read_text(encoding='utf-8')) + assert record['expected'] == 'expected project' and not record['matched'] + assert record['control_id'] == 1217 and record['exception']['type'] + if failure in ('empty', 'mismatch'): + assert record['actual'] == ('' if failure == 'empty' else 'other project') + if failure in ('set_timeout', 'set_rejected'): + assert calls == [0x000C] -- 2.39.5 From 122bafcc7aeef408f843be17cb82b6083a59f90e 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: Mon, 2 Nov 2026 22:56:30 +0800 Subject: [PATCH 17/59] test: scope deletion safeguard to authorized runtime checkpoints --- tests/test_safety.py | 91 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests/test_safety.py b/tests/test_safety.py index 2aa7853..2392f03 100644 --- a/tests/test_safety.py +++ b/tests/test_safety.py @@ -7,6 +7,10 @@ dialog and not user input. Native Project Backup Repair is restricted to an owned private desktop/PID, a resolved resource command and an exact PRJ. """ from pathlib import Path +import ast +import json + +import pytest SOURCE_ROOT = Path(__file__).resolve().parents[1]/'src'/'cbb_refresh_verify' @@ -104,9 +108,44 @@ def test_desktop_worker_close_kills_unbound_worker_tree(): assert 'self.kill()' in text +def _without_authorized_checkpoint_cleanup(text): + """Exempt exact protocol-checkpoint callsites, never the whole module.""" + tree = ast.parse(text) + parents = {child: parent for parent in ast.walk(tree) for child in ast.iter_child_nodes(parent)} + allowed = {('LibraryLock', '__exit__'): 'marker', ('release_retained_library',): 'checkpoint'} + used, spans = set(), [] + for call in ast.walk(tree): + if not (isinstance(call, ast.Call) and isinstance(call.func, ast.Attribute) + and call.func.attr == 'unlink' and isinstance(call.func.value, ast.Name) and not call.args): + continue + scope, parent = [], parents.get(call) + while parent is not None: + if isinstance(parent, (ast.ClassDef, ast.FunctionDef, ast.AsyncFunctionDef)): + scope.insert(0, parent.name) + parent = parents.get(parent) + scope = tuple(scope) + if scope in used or allowed.get(scope) != call.func.value.id: + continue + expected = ([] if scope == ('release_retained_library',) else [('missing_ok', True)]) + keywords = [(k.arg, k.value.value if isinstance(k.value, ast.Constant) else None) for k in call.keywords] + if keywords == expected and call.lineno == call.end_lineno: + spans.append((call.lineno - 1, call.col_offset, call.end_col_offset)) + used.add(scope) + lines = text.splitlines(keepends=True) + for line, start, end in sorted(spans, reverse=True): + # AST column offsets are UTF-8 byte offsets. + data = lines[line].encode('utf-8') + lines[line] = (data[:start] + b'checkpoint_cleanup' + data[end:]).decode('utf-8') + return ''.join(lines) + + def test_verification_never_deletes_or_moves_library_files(): for path in production_sources(): text = path.read_text(encoding='utf-8') + if path == SOURCE_ROOT/'library_lock.py': + # Approved runtime JSON lifecycle only. Actual sidecar/engineering + # protection is also exercised against real files below. + text = _without_authorized_checkpoint_cleanup(text) for operation in ('os.remove', '.unlink('): assert operation not in text, f'{path.name} 不得删除/移动中心库文件' adapter = (SOURCE_ROOT/'mentor_adapter.py').read_text(encoding='utf-8') @@ -116,3 +155,55 @@ def test_verification_never_deletes_or_moves_library_files(): # cdbsvr address advertisement; stable CBB content remains untouched. assert 'shutil.move' not in adapter assert "sAddress.adr" in adapter + + +def test_checkpoint_cleanup_exception_does_not_allow_other_deletions(): + source = ''' +class LibraryLock: + def __exit__(self): + marker.unlink(missing_ok=True) + self.path.unlink() +def release_retained_library(): + checkpoint.unlink() + os.remove('lib.lmc') +def other_operation(): + marker.unlink(missing_ok=True) +''' + checked = _without_authorized_checkpoint_cleanup(source) + assert 'self.path.unlink()' in checked and 'os.remove(' in checked + assert checked.count('marker.unlink(missing_ok=True)') == 1 + + +@pytest.mark.parametrize('replacement', ['', 'malformed', 'foreign_lease']) +def test_guard_cleanup_only_removes_current_validated_checkpoint(tmp_path, replacement): + from cbb_refresh_verify import library_lock as locks + library = tmp_path/'library.lmc' + protected = {library: b'LMC engineering', tmp_path/'icdb.dat': b'schematic engineering', + tmp_path/'LayoutDB.lyt': b'layout engineering', tmp_path/'ordinary.json': b'ordinary data'} + for path, data in protected.items(): path.write_bytes(data) + lease = locks.LibraryLock(library) + lease.__enter__() + marker = locks.blocker_path(library) + try: + if replacement: + if replacement == 'malformed': + data = b'ordinary file replacing a checkpoint' + else: + record = json.loads(marker.read_text(encoding='utf-8')) + record['lease_id'] = '0' * 32 + data = json.dumps(record).encode('utf-8') + marker.write_bytes(data) + with pytest.raises(locks.LibraryBusy): lease.__exit__(None, None, None) + assert marker.read_bytes() == data + with pytest.raises(locks.LibraryBusy): + with locks.LibraryLock(library): pass + else: + lease.__exit__(None, None, None) + assert not marker.exists() + assert lease.path.is_file() # The uploader's actual exclusive sidecar is never deleted. + assert all(path.read_bytes() == data for path, data in protected.items()) + finally: + # This test never launches native owners; release only its retained fake lease. + with locks._guard: + owner = locks._owners.pop(lease.key, None) + if owner: owner['handle'].Close() -- 2.39.5 From 4f741cd17680ff4eb98f444c6458ea00f77cc66d 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: Mon, 2 Nov 2026 22:59:50 +0800 Subject: [PATCH 18/59] docs: record final issue 2 candidate evidence and acceptance gaps --- ...2026-11-02-issue-2-candidate-validation.md | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 docs/releases/2026-11-02-issue-2-candidate-validation.md diff --git a/docs/releases/2026-11-02-issue-2-candidate-validation.md b/docs/releases/2026-11-02-issue-2-candidate-validation.md new file mode 100644 index 0000000..78213fa --- /dev/null +++ b/docs/releases/2026-11-02-issue-2-candidate-validation.md @@ -0,0 +1,77 @@ +# 工单 #2 候选包验收记录 + +分支:`codex/temp-issue-2-icdb-recovery`。本记录区分软件回归、隔离原生试验和现场验收;未合并、发布或关闭工单。 + +最终候选程序源码提交:`8e486c1`。新增和同名更新保持禁用;原生新增虽已成功注册并复读,但原理图数据库被原生工具重写,尚未证明内容等价。移除已在新建隔离库通过完整管理流程验证。 + +最终全量回归:`python -m pytest -q` 在 `122bafc` 上 **887 passed,1 个已有 pywinauto STA 模式警告,68.62 秒**。`122bafc` 只调整安全测试,`src` 和 `packaging` 的 Git 树与打包提交 `8e486c1` 完全一致。编译检查及 `git diff --check` 通过;整分支审查和后续修正复审均无未处理问题。 +日志:`build/issue2-final-tests-122bafc.log`。安全测试明确保留工程文件、上传锁以及损坏/外来检查点,只有本工具拥有且验证通过的运行时检查点允许清理。 + +## 已实现范围 + +- ICDB 启动失败后,仅在任务进程、工程路径、私有桌面、空闲窗口及地址释放可确认时正常关闭 PCB;同时等待启动器退出。Repair 区分缺少健康备份、原生失败、占用、未知窗口和释放超时。最终重新打开、校验及 READY 状态共同决定修复成功。 +- 名称/分区搜索和状态筛选使用已加载数据;隐藏条目清除勾选。READY 可以用于管理,不能进入默认校验、维护和恢复队列。受影响条目刷新后重新判断资格。 +- 完整备份保留全部文件;版本化清单对主要工程文件和库依赖计算 SHA-256,复制时同时核对文件集合、大小和时间。日志及其他运行时变化不再等同于工程内容变化。 +- 管理请求、逐项结果、完整旧库备份、检查点、来源暂存、共享锁和安全释放入口已集成。新增/更新未达到原生验收门槛时禁用。重启只展示未完成任务,不自动重放写操作。 + +## 原生证据与边界 + +| 场景 | 实际观察 | 限制 | +| --- | --- | --- | +| 最终冻结程序启动失败退出 | `8e486c1` 的 PCB 和启动器自动正常退出、无数据库地址;`startup-close.json` 为 CLOSED,safe_to_continue=true | 人工损坏副本,不是现场服务器失联 | +| 最终冻结程序 Repair 缺少健康备份 | `8e486c1` 返回 MISSING_HEALTHY_BACKUP,原生工具自动退出、地址为空、允许安全继续 | 验证明确失败和释放,不代表修复成功 | +| 官方原生移除 | 独立副本中 CleanWGLib/F4 返回 0,之后原生目录重新读取为空 | 旧库升级存在告警;生产读取遇到升级必须拒绝 | +| 隔离新增首次完整探测 | 新建库后打开编辑器失败,尚未提交新增;来源保持不变,保留失败证据及协调阻止记录 | 不启用新增或同名更新,不作为成功验收 | +| 最终移除适配器命令 | 实际捕获 `# Note: Removed REUSE_BLOCK 'IP101GR' from partition 'Logical-Physical'.`,释放确认成功,来源清单未变 | 随后的旧库复读拒绝 Select flow type;不能宣称该库完整自动移除验收通过 | +| 新建隔离库完整移除 | 管理引擎完成原生读取、完整旧库备份、原生删除、重新读取确认缺失;最终 REMOVED,safe_to_continue=true | 仅代表此安装版本、库流程和 Logical-Physical 分区 | +| 新建隔离库原生新增 | 收到成功消息,正常关闭、重新打开仍有正确名称/分区,原始来源不变;布局哈希一致 | 原理图数据库哈希改变,identity_matches=false;新增/更新不启用,未执行同名更新和重新导入恢复验收 | + +原始证据位于 `diagnostics/issue2-frozen-20261102/`、 +`diagnostics/issue2-management-native-20261102/` 和 +`C:/Users/ABS_Windows/.cbb-management-e2e-20261102-a/`。 +首次新增失败后,仅向已确认的隔离 Library Manager 主窗口发送正常 WM_CLOSE; +随后进程查询为空,没有强制终止数据库所有者。 + +第二次新增探测的 `dispatch-diagnostics.jsonl` 在加载 pywinauto 前记录方法 +DISPID 17/26/6,加载后全部返回 `CO_E_OBJNOTCONNECTED`(-2147220995)。 +这证明失效发生在 UI 自动化依赖初始化边界,尚未提交新增;来源未变。 +第二次隔离会话同样正常关闭。移除适配器及旧库拒绝证据保存在 +`diagnostics/issue2-remove-adapter-20261102/`;旧库拒绝流程后出现的 +“Unable to determine flow type”通知由协调端确认后正常退出,不能记为无人值守释放通过。 + +`ffa7e84` 冻结包的 GUI 冒烟通过,但真实批处理在启动 Mentor 前报 +`No module named 'win32timezone'`。此包不能作为最终可用包交付;必须补齐打包依赖并重建验证。 +`1f69480` 补齐依赖后运行了真实 PCB 流程;`8e486c1` 又收紧了隐藏基础设施窗口的精确识别条件,最终冻结测试自动通过退出分支。 + +最终证据: + +- `diagnostics/issue2-frozen-20261102/full-8e486c1-startup/evidence/verify-97173dae8a21/startup-close.json` +- `diagnostics/issue2-frozen-20261102/full-8e486c1-nohistory/repair-result.json` +- `diagnostics/issue2-management-final-20261102/probe-result.json` +- `diagnostics/issue2-management-final-20261102/full-remove-result.json` +- 移除前完整旧库保留在 `C:/Users/ABS_Windows/.cbb-management-e2e-20261102-e/full-remove/management-e90d69ad7f624fb18f5401119b96856d/library-before/`。 + +冻结启动失败试验的批处理退出码为 2,因为输入是损坏工程;通过项是自动安全退出,而不是工程校验成功。缺少健康备份的 Repair 同样是已验证的明确失败分支。 + +## 仍需现场验收 + +工单附件只有日志,没有现场 `1GPHY_8211` 故障工程及其必要共享库的一致副本。 +因此现场服务器会话失联尚未复现,也没有完成该现场故障经应用实际维护按钮触发的完整自动链路验收。 +必须取得副本、记录来源/工程身份/主要文件哈希,在隔离环境记录失败、退出、Repair 调用、重新打开、保存关闭和 READY 查询的完整证据,并核对来源未改变。 +复制后不能复现时应记录“未复现”,不能用数据库文件头损坏试验替代。 + +Windows 10 尚未验证。现有原生试验环境为 Windows 11 / Mentor EE 7.9.5。 +历史备份恢复保持“已恢复,版本待核对”,不宣称无损修复。 + +## 候选包 + +`dist/issue2-repair-candidate-877e3b4.zip` 保留独立修复候选包及构建清单。 +完整候选程序位于 `dist/issue2-full-candidate-8e486c1/`,由 `git archive 8e486c1` 的不可变源码构建。构建与 GUI 冒烟通过,真实冻结原生证据见上。 +分发压缩包:`dist/issue2-full-candidate-8e486c1.zip`。解压后运行 `CbbRefreshVerify-Background/CbbRefreshVerify.exe`;包根目录保留构建清单、本文及关键验证证据。 +`dist/issue2-full-candidate/` 和 `dist/issue2-full-candidate-1f69480/` 是被后续修正取代的中间试验构建,不作为交付包。 + +## 后续验收顺序 + +1. 取得现场故障工程及必要共享库一致副本,优先完成实际维护按钮触发的完整自动链路验收。 +2. 针对原生导入会重写 ICDB 的行为,使用经过验证的 Mentor 语义读取/导出建立工程内容等价证据;不能只放松哈希检查。之后才能启用新增,并验证重复冲突、同名更新、部分失败及备份重新导入。 +3. 在 Windows 10 / EE 7.9.5 重跑冻结关键路径,并在具备网络测试环境时验证真实断网、取消和恢复场景。现有模拟测试不能替代这些环境验收。 -- 2.39.5 From e39dc63deded5ff9164a03f68e2f5385d84bc298 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: Tue, 3 Nov 2026 09:13:32 +0800 Subject: [PATCH 19/59] fix: admit explicit ICDB maintenance with preserved address evidence --- src/cbb_refresh_verify/library_lock.py | 89 ++++++++ src/cbb_refresh_verify/maintenance_entry.py | 189 ++++++++++++++++ src/cbb_refresh_verify/native_repair.py | 6 +- src/cbb_refresh_verify/native_repair_ui.py | 16 +- src/cbb_refresh_verify/ui/main_window.py | 56 +++-- src/cbb_refresh_verify/verify_runner.py | 14 +- tests/test_library_lock.py | 12 +- tests/test_maintenance_entry.py | 233 ++++++++++++++++++++ tests/test_maintenance_ui.py | 97 ++++++++ tests/test_safety.py | 4 +- 10 files changed, 691 insertions(+), 25 deletions(-) create mode 100644 src/cbb_refresh_verify/maintenance_entry.py create mode 100644 tests/test_maintenance_entry.py create mode 100644 tests/test_maintenance_ui.py diff --git a/src/cbb_refresh_verify/library_lock.py b/src/cbb_refresh_verify/library_lock.py index 364cd27..6b1b173 100644 --- a/src/cbb_refresh_verify/library_lock.py +++ b/src/cbb_refresh_verify/library_lock.py @@ -82,8 +82,19 @@ class LibraryLock: self.key = _key(self.path) self.entered = False self.safe_to_release = safe_to_release + self.maintenance_guard = None + self.maintenance_mutex = None + + @classmethod + def for_maintenance(cls, library, archive, guard, *, safe_to_release=None): + lease = cls(library, safe_to_release=safe_to_release) + lease.maintenance_guard = guard + lease.maintenance_archive = Path(archive) + return lease def __enter__(self): + if self.maintenance_guard is not None: + return _enter_maintenance(self) import win32con, win32file with _guard: owner = _owners.get(self.key) @@ -134,6 +145,16 @@ class LibraryLock: _owners[self.key]['retained'] = True def __exit__(self, *_): + try: + self._exit_owned() + finally: + if self.maintenance_mutex is not None: + import win32event + win32event.ReleaseMutex(self.maintenance_mutex) + self.maintenance_mutex.Close() + self.maintenance_mutex = None + + def _exit_owned(self): with _guard: if not self.entered: return @@ -172,6 +193,74 @@ class LibraryLock: _persist_blocker(self.library, lease_id=owner['lease_id']) +def _supervisor_alive(pid): + import pywintypes, win32api, win32con, win32event + try: + handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid)) + except pywintypes.error as error: + if error.winerror == 87: return False + raise LibraryBusy('无法确认原监督进程退出') from error + try: + return win32event.WaitForSingleObject(handle, 0) != win32con.WAIT_OBJECT_0 + finally: + handle.Close() + + +def _enter_maintenance(lease): + """Transfer only a valid known lease, after project admission under lock.""" + import win32con, win32event, win32file + mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') + acquired = False + handle = None + existing = None + try: + acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) + if not acquired: raise LibraryBusy('已有 CBB 操作运行,维护不能接续') + with _guard: + marker = blocker_path(lease.library) + if marker.with_suffix('.pending').exists(): + raise LibraryBusy('未完成检查点写入,不能自动接续') + old = marker.read_bytes() if marker.exists() else None + record = checkpoint_record(marker) if old is not None else None + if old is not None and (record is None or _key(record['library']) != _key(lease.library)): + raise LibraryBusy('维护拒绝未知、损坏或其他库的检查点') + existing = _owners.get(lease.key) + if existing: + if (not existing['retained'] or existing['depth'] != 0 or record is None + or record['pid'] != os.getpid() or record['lease_id'] != existing['lease_id']): + raise LibraryBusy('本进程库锁仍在使用或检查点身份不符') + handle = existing['handle'] + else: + if record is not None and _supervisor_alive(record['pid']): + raise LibraryBusy('原监督进程仍存在,不能接续其库锁') + handle = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, + 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) + # All target/process/address/exclusive-file checks finish BEFORE + # any old checkpoint replacement. Keep the exact sidecar open. + with lease.maintenance_guard() as proof: + if (marker.read_bytes() if marker.exists() else None) != old: + raise LibraryBusy('维护检查期间检查点变化') + if old is not None: + lease.maintenance_archive.mkdir(parents=True, exist_ok=False) + saved = lease.maintenance_archive/'previous-checkpoint.json' + with saved.open('xb') as stream: + stream.write(old); stream.flush(); os.fsync(stream.fileno()) + if saved.read_bytes() != old: raise OSError('原检查点归档校验失败') + lease_id = uuid4().hex + _persist_blocker(lease.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id) + _owners[lease.key] = {'thread': threading.get_ident(), 'depth': 1, 'handle': handle, + 'retained': False, 'lease_id': lease_id} + lease.maintenance_proof = proof + lease.entered = True + lease.maintenance_mutex = mutex + return lease + except Exception: + if handle is not None and existing is None: handle.Close() + if acquired: win32event.ReleaseMutex(mutex) + mutex.Close() + raise + + def owned_snapshot(path): """Read only a lease held by this thread, never reopen an exclusive file.""" import win32con, win32file diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py new file mode 100644 index 0000000..fe0ff0b --- /dev/null +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -0,0 +1,189 @@ +"""User-requested native inspection of a project with an existing advertisement. + +An advertisement is never deleted, quarantined or called stale by this module. +Exclusive access permits a backed-up official inspection, not ordinary Verify. +""" +from contextlib import contextmanager, ExitStack +from dataclasses import asdict, replace +import json +import os +from pathlib import Path, PureWindowsPath +import re +from uuid import uuid4 + + +def assert_native_idle(): + import win32com.client + from .native_management import _retained_workers + 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') + service = win32com.client.GetObject('winmgmts:') + query = 'SELECT ProcessId, Name, CommandLine FROM Win32_Process WHERE ' + ' OR '.join( + f"Name='{name}'" for name in names) + owners = [] + for item in service.ExecQuery(query): + if int(item.ProcessId) == os.getpid(): + continue + name, command = str(item.Name).casefold(), str(item.CommandLine or '').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({'pid': int(item.ProcessId), 'name': str(item.Name)}) + 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 select_maintenance_target(request): + 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) + candidates = [] + for candidate in (project, pcb.parent/'pcb.prj'): + 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 = [path for path in scan_tree(Path(request.lmc_file).parent) + if path.name.casefold() == 'saddress.adr'] + 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): + """Simultaneously hold native database files, preserving every address.""" + import win32con, win32file + from .scoped_manifest import scan_tree, hash_file + target, project, pcb, addresses = select_maintenance_target(request) + 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 (project, pcb, pcb.parent/'pcb.prj')}} + if expected is not None and context != expected: + raise ValueError('MAINTENANCE_SOURCE_CHANGED: 目标或地址记录在检查后变化') + assert_native_idle() + entries = scan_tree(Path(request.lmc_file).parent) + files = [path for path in entries if path.is_file() and ( + path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))] + with ExitStack() as stack: + for path in files: + 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_addresses = [p for p in scan_tree(Path(request.lmc_file).parent) + if p.name.casefold() == 'saddress.adr'] + if address_snapshot(current_addresses) != baseline: + raise ValueError('MAINTENANCE_SOURCE_CHANGED: 地址记录在检查期间变化') + yield {'target': target, 'context': context, 'project': project, 'pcb': pcb} + + +def maintenance_needed(request): + 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): + from .library_lock import LibraryLock + from .native_repair import run_native_repair, native_success_confirmed, repair_release_state + 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 + try: + lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover', + lambda: maintenance_target_guard(request), safe_to_release=lambda: owner_safe) + with lease: + checked = lease.maintenance_proof + target, context = checked['target'], checked['context'] + record['maintenance_context'] = context + 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; worker repeats after complete + # backup and before launching its native process as well. + with maintenance_target_guard(request, context): pass + recovery = run_native_repair(target, native_root, mentor_bin=request.mentor_bin, + timeout_seconds=max(60, request.timeout_seconds), progress=progress, + maintenance_context=context) + 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 + owner_safe = safe + if recovery.get('status') == 'REPAIRED' and safe and native_success_confirmed(target, recovery): + emit('repair_verify', detail='原生修复及释放已确认,重新打开同一工程校验') + owner_safe = False + verified = verify(replace(request, maintenance=False, auto_repair=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 '')) + 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')) diff --git a/src/cbb_refresh_verify/native_repair.py b/src/cbb_refresh_verify/native_repair.py index 9390c8d..67772fa 100644 --- a/src/cbb_refresh_verify/native_repair.py +++ b/src/cbb_refresh_verify/native_repair.py @@ -239,7 +239,8 @@ def repair_release_state(request, target, native_result): return result -def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None): +def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None, + maintenance_context=None): """Supervise the GUI on its own desktop; a repair timeout never kills it.""" from .desktop_worker import DesktopWorker root = Path(root) @@ -249,7 +250,8 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr desktop = 'CbbRepair_'+uuid4().hex[:12] job_path.write_text(json.dumps({'project': str(target.project), 'database': str(target.database), 'root': str(root), 'mentor_bin': str(mentor_bin or ''), 'timeout': timeout_seconds, - 'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop}, + 'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop, + 'maintenance_context': maintenance_context}, ensure_ascii=False), encoding='utf-8') command = ([sys.executable] if getattr(sys, 'frozen', False) else [sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)] diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index c219b2d..b1366ff 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -212,10 +212,22 @@ def repair_worker_main(job_path): from .mentor_adapter import pcb_processes, _process_details if pcb_processes() or _process_details('LibraryManager.exe'): raise ValueError('REPAIR_DATABASE_IN_USE') + maintenance_context = job.get('maintenance_context') + if maintenance_context is not None: + from .maintenance_entry import maintenance_target_guard + from .verify_runner import VerifyRequest + maintenance_request = VerifyRequest(Path(maintenance_context['library']), + maintenance_context['block'], root, maintenance=True) + with maintenance_target_guard(maintenance_request, maintenance_context) as checked: + if (checked['target'].project.resolve() != project.resolve() + or checked['target'].database.resolve() != database.resolve()): + raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED') backup_project(RepairTarget(project, database), root/'complete-backup') emit(stage='complete_backup_verified', path=str(root/'complete-backup')) if Path(job['cancel_signal']).exists(): raise ValueError('REPAIR_CANCELLED_AFTER_BACKUP') + if maintenance_context is not None: + with maintenance_target_guard(maintenance_request, maintenance_context): pass baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt') started_ns = time.time_ns() process = subprocess.Popen([str(executable), '-p', str(project)], cwd=str(project.parent)) @@ -247,7 +259,9 @@ def repair_worker_main(job_path): if busy_before_start: output = current_output result.update(status='DATABASE_BUSY', detail='原生工具加载工程时报告占用;未请求 Repair') - if (not invoked and isinstance(current_output, str) and not busy_before_start + maintenance_ready = (maintenance_context is None or isinstance(current_output, str) + and 'Project is not in use. Full iCDB Project Backup\'s functionality is available.' in current_output) + if (not invoked and isinstance(current_output, str) and not busy_before_start and maintenance_ready and main and not dialogs and not unknown and main['enabled']): if Path(job['cancel_signal']).exists(): raise ValueError('REPAIR_CANCELLED_BEFORE_START') diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 28bc487..c2811f5 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -64,6 +64,8 @@ class MainWindow(QWidget): self.management_adapter = management_adapter or NativeManagementAdapter() self.management_capabilities = self.management_adapter.capabilities() self._management_unsafe = False + self._maintenance_recovery_view = False + self._reload_after_maintenance = False if reader is not None: self.reader = reader elif authoritative_status and os.environ.get('CBB_STATUS_BRIDGE_MODE', 'authoritative').casefold() != 'legacy': @@ -249,12 +251,12 @@ class MainWindow(QWidget): # The refresh flow needs only the library and a CBB; LibSpec/DMS config # are optional and only enable the extra contents check. ready = not self.busy and not self._management_unsafe and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 - self.batch_button.setEnabled(ready and bool(self._eligible_names())) - self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True))) + self.batch_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._eligible_names())) + self.selected_batch_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._eligible_names(checked_only=True))) self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names())) - self.backup_restore_button.setEnabled(ready and bool(self._maintenance_names())) + self.backup_restore_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._maintenance_names())) self.release_library_button.setEnabled(not self.busy and bool(self.lmc_edit.text().strip())) - loaded = not self.busy and not self._management_unsafe and self.loaded_path == self.lmc_edit.text() + loaded = not self.busy and not self._management_unsafe and not self._maintenance_recovery_view and self.loaded_path == self.lmc_edit.text() for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): widget.setEnabled(loaded and self.management_capabilities[operation].supported and (operation == 'add' or bool(self.selected_records()))) @@ -296,7 +298,7 @@ class MainWindow(QWidget): self._start(lambda: release_retained_library(library, self.data_root/'release-checks'), completed) def manage_cbb(self, operation): - if (self.busy or self._management_unsafe or self.loaded_path != self.lmc_edit.text() + if (self.busy or self._management_unsafe or self._maintenance_recovery_view or self.loaded_path != self.lmc_edit.text() or not self.management_capabilities[operation].supported): return from cbb_refresh_verify.management import ManagementRequest @@ -411,6 +413,7 @@ class MainWindow(QWidget): def load_library(self): if self.busy or not self.lmc_edit.text(): return + self._maintenance_recovery_view = False self._global_icdb_run = False self.icdb_progress.hide(); self.icdb_phase.hide() self._release_library_lease() @@ -434,10 +437,19 @@ class MainWindow(QWidget): 'library_path_lease display=%s mentor=%s drive=%s remote=%s created=%s', lease.display_path, mentor_path, lease.drive or '-', lease.remote_root or '-', lease.created) - from cbb_refresh_verify.library_lock import has_blocker - if has_blocker(mentor_path): + from cbb_refresh_verify.library_lock import blocker_path, checkpoint_record + marker = blocker_path(mentor_path) + recovery_view = False + if marker.exists() or marker.with_suffix('.pending').exists(): from cbb_refresh_verify.models import LibraryReadResult - result = LibraryReadResult((), '此库有未确认的原生会话,请先执行“释放检查并解除协调阻止”。') + checkpoint = checkpoint_record(marker) + if (checkpoint is not None and not marker.with_suffix('.pending').exists() + and Path(checkpoint['library']).resolve() == Path(mentor_path).resolve()): + result = read_library_fast(mentor_path, + status_reader=lambda _, names: {name: 'UNKNOWN' for name in names}) + recovery_view = True + else: + result = LibraryReadResult((), '此库检查点损坏或身份不符,保留原记录;请先审查并执行释放检查。') else: result = self.reader(mentor_path) if self.logging_context: @@ -453,14 +465,14 @@ class MainWindow(QWidget): reason = preflight_skip_reason(mentor_path, record.name) if reason: skip_reasons[record.name] = reason - return result, skip_reasons, lease, mentor_path + return result, skip_reasons, lease, mentor_path, recovery_view except Exception: if 'lease' in locals(): lease.close() raise finally: pythoncom.CoUninitialize() def loaded(payload): - result, skip_reasons, lease, mentor_path = payload + result, skip_reasons, lease, mentor_path, recovery_view = payload if path != self.lmc_edit.text(): lease.close() return @@ -468,6 +480,7 @@ class MainWindow(QWidget): lease.close() self.status_label.setText(result.error); return self.library_lease = lease + self._maintenance_recovery_view = recovery_view self.mentor_lmc_path = mentor_path self.loaded_path = path self.cbb_table.setRowCount(0) @@ -524,6 +537,9 @@ class MainWindow(QWidget): self.status_label.setText( f'已读取 {len(result.records)} 个 CBB,其中 Ready {ready_count} 个,' f'缺文件跳过 {len(self.skipped_names)} 个。{suffix}') + if recovery_view: + self.status_label.setText('协调阻止恢复视图:仅按目录显示工程,状态未确认(UNKNOWN),不代表损坏。' + '仅可明确执行 ICDB 维护或释放检查,其他写入与原生状态读取已禁用。') self._apply_filters() self._start(read, loaded) @@ -554,14 +570,15 @@ class MainWindow(QWidget): f'当前中心库:{self.loaded_path}\n\n' f'将检查 {len(names)} 个可见非 READY CBB' f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '操作会重新校验并保存工程。确认失联问题后,完整备份目标工程并尝试一次原生修复。\n' + '无地址阻止时逐项校验;已有工程地址记录时先核对所有者和独占访问、完整备份,' + '再由官方工具确认未占用并检查/修复。不会删除地址文件。\n' '共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;' '会话安全无法确认时停止队列。\n\n是否开始检查与修复?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: self._run(names, maintenance=True) def restore_backup(self): - if self.busy or not self.loaded_path: + if self.busy or not self.loaded_path or self._maintenance_recovery_view: return from .backup_recovery_dialog import BackupRecoveryDialog names = self._maintenance_names() @@ -580,7 +597,7 @@ class MainWindow(QWidget): def _backup_request(self, name): # A still-open dialog must recheck a target after an earlier recovery # or an authoritative refresh changed its eligibility. - if self.busy or name not in self._maintenance_names(): + if self.busy or self._maintenance_recovery_view or name not in self._maintenance_names(): raise ValueError('目标需为当前可见、可用的非 READY CBB;请重新选择。') return self._request(name) @@ -620,13 +637,14 @@ class MainWindow(QWidget): def _run(self, names, *, maintenance=False): if self.busy or not self.loaded_path: return + if self._maintenance_recovery_view and not maintenance: return eligible = set(self._eligible_names()) names = [name for name in names if name in eligible] if not names: self.status_label.setText('当前可见选择中没有可校验的非 READY CBB。') self._update() return - requests = [self._request(name) for name in names] + requests = [replace(self._request(name), maintenance=maintenance) for name in names] self._global_icdb_run = maintenance self._icdb_completed = 0 self.icdb_progress.setVisible(maintenance) @@ -657,7 +675,7 @@ class MainWindow(QWidget): write_maintenance_report(requests, results, report, ()) affected = [req.block_name for req, result in zip(requests, results) if result.status != 'SKIPPED'] safe = all(result.safe_to_continue for result in results) - refresh = self._read_affected_records(affected) if safe else None + refresh = self._read_affected_records(affected) if safe and not self._maintenance_recovery_view else None return results, report, affected, refresh self._start(action, self._batch_done) @@ -725,7 +743,7 @@ class MainWindow(QWidget): (UNKNOWN on read failure). It runs before the worker's finished signal. Search, filter, original logical_type and action-result cells are retained. """ - if self.busy or not self.loaded_path: + if self.busy or not self.loaded_path or self._maintenance_recovery_view: return False targets = tuple(dict.fromkeys(name for name in names if name in self._records_by_name)) if not targets: @@ -779,6 +797,8 @@ class MainWindow(QWidget): def _batch_done(self, outcome): results, report = outcome[:2] + if self._maintenance_recovery_view and self._global_icdb_run and results: + self._reload_after_maintenance = all(result.safe_to_continue for result in results) if len(outcome) > 2: _, _, affected, refresh = outcome if refresh is None: @@ -832,6 +852,10 @@ class MainWindow(QWidget): def _finished(self): self.busy = False; self._update() + if self._reload_after_maintenance: + from PySide6.QtCore import QTimer + self._reload_after_maintenance = False + QTimer.singleShot(0, self.load_library) def open_logs(self): self.data_root.mkdir(parents=True, exist_ok=True); os.startfile(str(self.data_root)) diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 561592f..595af8b 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -63,6 +63,7 @@ class VerifyRequest: mapped_drive: str = '' mapped_remote_root: str = '' auto_repair: bool = True + maintenance: bool = False @dataclass(frozen=True) @@ -518,6 +519,17 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, if not acquired: return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。', safe_to_continue=False) + if request.maintenance: + from .maintenance_entry import maintenance_needed, run_existing_address_maintenance + try: + needed = maintenance_needed(request) + except (ValueError, OSError) as error: + return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False) + if needed: + return run_existing_address_maintenance(request, progress=progress, + verify=lambda req: _run_verify_serialized(req, progress=progress, flag_reader=flag_reader, + process_runner=process_runner, port_check=port_check, + dialog_supervisor_factory=dialog_supervisor_factory)) from .library_lock import LibraryLock, LibraryBusy if Path(request.lmc_file).is_file(): try: @@ -563,7 +575,7 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, result = replace(original, recovery_attempts=(recovery,)) recovery['status'] = 'REPAIR_VALIDATION_FAILED' result = _run_verify_serialized( - replace(request, auto_repair=False), progress=progress, flag_reader=flag_reader, + replace(request, auto_repair=False, maintenance=False), progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, dialog_supervisor_factory=dialog_supervisor_factory) recovery['validation_evidence'] = str(result.evidence_path or '') diff --git a/tests/test_library_lock.py b/tests/test_library_lock.py index eeb01f9..d65c417 100644 --- a/tests/test_library_lock.py +++ b/tests/test_library_lock.py @@ -163,16 +163,22 @@ def test_public_verify_preserves_existing_apartment(tmp_path, monkeypatch, mode, def existing_mta(): pythoncom.CoInitializeEx(mode) try: - runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) apartment, qualifier = ctypes.c_int(), ctypes.c_int() ole32 = ctypes.WinDLL('ole32') ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)] + assert ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0 + before = apartment.value + # A thread can be the main STA (3) when it creates the process's + # first remaining STA. Verify must preserve either STA flavor. + assert before in ((1,) if mode == 0 else (0, 3)) + runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) result = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) - return result, apartment.value + return result, apartment.value, before finally: pythoncom.CoUninitialize() with ThreadPoolExecutor(1) as pool: - assert pool.submit(existing_mta).result() == (0, expected) + result, after, before = pool.submit(existing_mta).result() + assert result == 0 and after == before def test_nested_snapshot_retains_runtime_checkpoint_bytes_without_semantic_conflicts(tmp_path): diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py new file mode 100644 index 0000000..f71d425 --- /dev/null +++ b/tests/test_maintenance_entry.py @@ -0,0 +1,233 @@ +"""Explicit maintenance for preexisting advertisements; native writes forbidden.""" +from dataclasses import replace +from contextlib import contextmanager +import json +import os +from pathlib import Path +from types import SimpleNamespace + +import pytest + + +@pytest.fixture(autouse=True) +def no_native_launch(monkeypatch): + import subprocess, win32process, win32com.client + import cbb_refresh_verify.desktop_worker as desktop + def forbidden(*a, **k): raise AssertionError('No native launch allowed in maintenance tests') + monkeypatch.setattr(subprocess, 'Popen', forbidden) + monkeypatch.setattr(win32process, 'CreateProcess', forbidden) + monkeypatch.setattr(win32com.client, 'Dispatch', forbidden) + monkeypatch.setattr(win32com.client, 'DispatchEx', forbidden) + monkeypatch.setattr(desktop, 'DesktopWorker', forbidden) + + +@pytest.fixture +def case(tmp_path, monkeypatch): + from cbb_refresh_verify.verify_runner import VerifyRequest + import cbb_refresh_verify.maintenance_entry as entry + library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.write_bytes(b'library') + work = library.parent/'ReusableBlocksLP/IP101GR/Work' + (work/'database/cdbsvr').mkdir(parents=True) + (work/'database/icdb.dat').write_bytes(b'schematic') + address = work/'database/cdbsvr/sAddress.adr'; address.write_bytes(b'old server address - preserved') + (work/'PCB/Layout').mkdir(parents=True) + (work/'PCB/Layout/LayoutDB.lyt').write_bytes(b'layout') + project = work/'IP101GR.prj' + project.write_text('KEY iCDBDir ".\\database"\nKEY PCBDesignPath "PCB/IP101GR.pcb"\n') + (work/'PCB/IP101GR.pcb').write_text('KEY ProjectFile "../IP101GR.prj"\n') + (work/'PCB/pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n') + monkeypatch.setattr(entry, 'assert_native_idle', lambda: None) + request = VerifyRequest(library, 'IP101GR', tmp_path/'evidence', maintenance=True) + yield request, project, address + import cbb_refresh_verify.library_lock as locks + key = locks.LibraryLock(library).key + with locks._guard: + owner = locks._owners.pop(key, None) + if owner: owner['handle'].Close() + + +def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, native_repair + from test_native_repair import complete_native_success + request, project, address = case + calls = [] + def repair(target, root, **kwargs): + calls.append('repair') + assert address.read_bytes() == b'old server address - preserved' + assert kwargs['maintenance_context']['addresses'][str(address.resolve())]['sha256'] + # Native boundary fake simulates official removal of its address. + native_repair.backup_project(target, root/'complete-backup') + assert (root/'complete-backup/database/cdbsvr/sAddress.adr').read_bytes() == address.read_bytes() + address.unlink() + return complete_native_success(target) + monkeypatch.setattr(native_repair, 'run_native_repair', repair) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a: + {'safe_to_continue': True, 'native_exited': True, 'addresses': []}) + def verify(req, **kwargs): + calls.append('verify') + assert not req.maintenance and not req.auto_repair + assert req.lmc_file == request.lmc_file and req.block_name == request.block_name + return runner.VerifyResult('PASS', library_flag='GREEN') + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + result = runner.run_verify(request) + assert calls == ['repair', 'verify'] and result.status == 'PASS' + assert result.recovery_attempts[0]['validation_passed'] is True + record = json.loads(result.evidence_path.read_text(encoding='utf-8')) + assert record['operation'] == 'explicit_project_maintenance' + assert 'session' not in record['maintenance_context'] + + +def test_ordinary_verify_still_blocks_existing_address_without_repair(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, native_repair + request, _, address = case + monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('ordinary verify must not repair')) + result = runner.run_verify(replace(request, maintenance=False)) + assert result.status == 'BLOCKED' and not result.safe_to_continue + assert address.read_bytes() == b'old server address - preserved' + + +@pytest.mark.parametrize('obstacle', ['active_owner', 'exclusive', 'shared_address', 'ambiguous', 'address_mutation']) +def test_maintenance_guard_blocks_before_native(case, monkeypatch, obstacle): + from cbb_refresh_verify import maintenance_entry as entry, native_repair, verify_runner as runner + import win32file, win32con + request, project, address = case + handle = None + if obstacle == 'active_owner': + monkeypatch.setattr(entry, 'assert_native_idle', lambda: (_ for _ in ()).throw(ValueError('native owner'))) + if obstacle == 'exclusive': + handle = win32file.CreateFile(str(project.parent/'database/icdb.dat'), win32con.GENERIC_READ, + 0, None, win32con.OPEN_EXISTING, 0, None) + if obstacle == 'shared_address': + shared = request.lmc_file.parent/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr' + shared.parent.mkdir(parents=True); shared.write_bytes(b'shared owner') + if obstacle == 'ambiguous': project.write_text(project.read_text() + 'KEY iCDBDir "other"\n') + if obstacle == 'address_mutation': + original = entry.address_snapshot + def mutate(paths): + result = original(paths) + address.write_bytes(b'changed after snapshot') + return result + monkeypatch.setattr(entry, 'address_snapshot', mutate) + monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('guard bypass')) + try: + result = runner.run_verify(request) + assert result.status == 'BLOCKED' and not result.safe_to_continue + finally: + if handle: handle.Close() + + +@pytest.mark.parametrize('change', ['dead', 'same_process', 'live_foreign', 'malformed', 'foreign_library', 'guard_failure']) +def test_takeover_preserves_old_checkpoint_until_every_guard_passes(case, monkeypatch, change): + from cbb_refresh_verify import library_lock as locks, maintenance_entry as entry + request, _, address = case + library = request.lmc_file + with locks.LibraryLock(library, safe_to_release=lambda: False): pass + marker = locks.blocker_path(library) + record = json.loads(marker.read_text(encoding='utf-8')) + if change != 'same_process': + with locks._guard: locks._owners.pop(locks.LibraryLock(library).key)['handle'].Close() + record['pid'] = 789123 + if change == 'foreign_library': record['library'] = str(library.parent.parent/'foreign/mentor.lmc') + marker.write_text('broken json' if change == 'malformed' else json.dumps(record), encoding='utf-8') + old = marker.read_bytes() + monkeypatch.setattr(locks, '_supervisor_alive', lambda pid: change == 'live_foreign') + if change == 'guard_failure': + monkeypatch.setattr(entry, 'assert_native_idle', lambda: (_ for _ in ()).throw(ValueError('guard failed'))) + archive = request.evidence_dir/'takeover' + guard = lambda: entry.maintenance_target_guard(request) + if change in ('dead', 'same_process'): + lease = locks.LibraryLock.for_maintenance(library, archive, guard, safe_to_release=lambda: True) + with lease: + assert (archive/'previous-checkpoint.json').read_bytes() == old + assert address.exists() + import win32file, win32con + with pytest.raises(Exception): + win32file.CreateFile(str(lease.path), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None) + assert lease.path.exists() and not marker.exists() + else: + with pytest.raises((ValueError, OSError)): + with locks.LibraryLock.for_maintenance(library, archive, guard): pass + assert marker.read_bytes() == old + + +@pytest.mark.parametrize('status', ['MISSING_HEALTHY_BACKUP', 'RESTORED_REVIEW_REQUIRED', 'DATABASE_BUSY', 'TIMEOUT_UNRELEASED']) +def test_nonrepair_outcomes_never_verify_or_count_success(case, monkeypatch, status): + from cbb_refresh_verify import native_repair, verify_runner as runner + request, _, _ = case + monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: { + 'status': status, 'release': {'safe_to_continue': False}, 'detail': 'native result'}) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a: {'safe_to_continue': False}) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('unsafe or nonrepair outcome reopened')) + result = runner.run_verify(request) + assert result.status != 'PASS' and not result.safe_to_continue + assert result.recovery_attempts[0]['status'] == status + + +def test_native_success_cannot_bypass_remaining_address(case, monkeypatch): + from cbb_refresh_verify import native_repair, verify_runner as runner + from test_native_repair import complete_native_success + request, _, _ = case + monkeypatch.setattr(native_repair, 'run_native_repair', lambda target, *a, **k: complete_native_success(target)) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('retained address reopened')) + # Native release queries remain mocked, actual address/exclusive checks stay real. + monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: []) + monkeypatch.setattr('cbb_refresh_verify.mentor_adapter._process_details', lambda name: []) + result = runner.run_verify(request) + assert result.status != 'PASS' and not result.safe_to_continue + + +@pytest.mark.parametrize('output', ['', 'Checking if project is in use.', + "Project is not in use. Full iCDB Project Backup's functionality is available.", + "Project is not in use. Full iCDB Project Backup's functionality is available.\nProject is in use."]) +def test_explicit_worker_requires_positive_native_idle_before_repair(tmp_path, monkeypatch, output): + import win32con + from test_native_repair import repair_worker_fixture + import cbb_refresh_verify.maintenance_entry as entry + from cbb_refresh_verify.native_repair import RepairTarget + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + values = json.loads(job.read_text()) + values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'} + job.write_text(json.dumps(values)) + @contextmanager + def guard(*args): + yield {'target': RepairTarget(Path(values['project']), Path(values['database']))} + monkeypatch.setattr(entry, 'maintenance_target_guard', guard) + monkeypatch.setattr(ui, '_native_output', lambda windows: output) + ui.repair_worker_main(job) + expected = output == "Project is not in use. Full iCDB Project Backup's functionality is available." + assert any(message == win32con.WM_COMMAND for _, message in actions) is expected + + +def test_worker_rechecks_address_after_complete_backup_before_native_launch(tmp_path, monkeypatch): + from test_native_repair import repair_worker_fixture + import cbb_refresh_verify.maintenance_entry as entry + from cbb_refresh_verify.native_repair import RepairTarget + ui, root, job, _ = repair_worker_fixture(tmp_path, monkeypatch, 'active') + values = json.loads(job.read_text()); values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'} + job.write_text(json.dumps(values)) + calls = [] + @contextmanager + def guard(*args): + calls.append('guard') + if len(calls) == 2: raise ValueError('address changed during complete backup') + yield {'target': RepairTarget(Path(values['project']), Path(values['database']))} + monkeypatch.setattr(entry, 'maintenance_target_guard', guard) + monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **k: pytest.fail('mutation must stop native launch')) + ui.repair_worker_main(job) + assert calls == ['guard', 'guard'] + assert 'address changed' in json.loads((root/'repair-result.json').read_text())['detail'] + + +def test_guard_failure_keeps_takeover_checkpoint_byte_exact_when_address_changes(case, monkeypatch): + from cbb_refresh_verify import library_lock as locks, maintenance_entry as entry + request, _, address = case + with locks.LibraryLock(request.lmc_file, safe_to_release=lambda: False): pass + marker = locks.blocker_path(request.lmc_file); old = marker.read_bytes() + snapshot = entry.address_snapshot + def mutate(paths): + result = snapshot(paths); address.write_bytes(b'changed'); return result + monkeypatch.setattr(entry, 'address_snapshot', mutate) + with pytest.raises(ValueError): + with locks.LibraryLock.for_maintenance(request.lmc_file, request.evidence_dir/'takeover', + lambda: entry.maintenance_target_guard(request)): pass + assert marker.read_bytes() == old diff --git a/tests/test_maintenance_ui.py b/tests/test_maintenance_ui.py new file mode 100644 index 0000000..8a2ec5e --- /dev/null +++ b/tests/test_maintenance_ui.py @@ -0,0 +1,97 @@ +import json +from pathlib import Path + +import pytest +from PySide6.QtCore import Qt +from PySide6.QtWidgets import QMessageBox + + +def test_only_confirmed_maintenance_sets_explicit_flag(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + from cbb_refresh_verify.verify_runner import VerifyResult + calls = [] + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda req: (calls.append(req) or VerifyResult('FAIL', safe_to_continue=True)), + status_refresher=lambda names, path: {name: 'PENDING' for name in names}) + window.search_edit.setText('Beta') + window.verify_all(); qtbot.waitUntil(lambda: not window.busy) + assert calls and calls[-1].maintenance is False + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.No) + window.repair_icdb_all(); assert len(calls) == 1 + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) + window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy) + assert len(calls) == 2 and calls[-1].maintenance is True + + +@pytest.mark.parametrize('marker_kind', ['valid', 'foreign', 'malformed']) +def test_blocked_library_recovery_view_is_filesystem_only_and_maintenance_only(qtbot, tmp_path, monkeypatch, marker_kind): + from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard + from cbb_refresh_verify.ui.main_window import MainWindow + from cbb_refresh_verify.verify_runner import VerifyResult + import cbb_refresh_verify.ui.main_window as ui + library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch() + (library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True) + with LibraryLock(library, safe_to_release=lambda: False) as lock: pass + with _guard: _owners.pop(lock.key)['handle'].Close() + marker = blocker_path(library) + if marker_kind == 'malformed': marker.write_text('{}') + if marker_kind == 'foreign': + record = json.loads(marker.read_text()); record['library'] = str(tmp_path/'foreign/mentor.lmc') + marker.write_text(json.dumps(record)) + monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None) + calls = [] + window = MainWindow(reader=lambda *a: pytest.fail('blocked view called native reader'), + verifier=lambda req: (calls.append(req) or VerifyResult('BLOCKED', safe_to_continue=False)), + status_refresher=lambda *a: pytest.fail('unsafe view refreshed native status'), data_root=tmp_path/'data') + qtbot.addWidget(window) + window.lmc_edit.setText(str(library)); window.load_library() + qtbot.waitUntil(lambda: not window.busy) + assert window.release_library_button.isEnabled() + if marker_kind != 'valid': + assert window.loaded_path is None and not window.icdb_repair_button.isEnabled() + return + assert window.visible_records()[0].name == 'IP101GR' + assert window.visible_records()[0].status == 'UNKNOWN' + assert '未确认' in window.status_label.text() + window.select_visible_button.click() + assert window.icdb_repair_button.isEnabled() + assert not any(button.isEnabled() for button in (window.batch_button, window.selected_batch_button, + window.backup_restore_button, window.add_button, window.update_button, window.remove_button)) + window.verify_all(); window.verify_selected(); window.restore_backup(); window.manage_cbb('remove') + assert window.refresh_affected_records(['IP101GR']) is False + assert calls == [] and not window.busy + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) + window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy) + assert len(calls) == 1 and calls[0].maintenance is True + + +def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qtbot, tmp_path, monkeypatch): + from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard + from cbb_refresh_verify.ui.main_window import MainWindow + from cbb_refresh_verify.models import LibraryReadResult, CbbRecord + from cbb_refresh_verify.verify_runner import VerifyResult + import cbb_refresh_verify.ui.main_window as ui + library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch() + (library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True) + with LibraryLock(library, safe_to_release=lambda: False) as lease: pass + with _guard: _owners.pop(lease.key)['handle'].Close() + reads = [] + def reader(path): + reads.append(path) + return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='READY'),)) + def verify(req): + assert req.maintenance + blocker_path(library).unlink() # Simulate normal cleanup at the injected boundary. + return VerifyResult('PASS', library_flag='GREEN') + monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None) + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) + window = MainWindow(reader=reader, verifier=verify, data_root=tmp_path/'data', + status_refresher=lambda *a: pytest.fail('recovery view must reload after worker completion')) + qtbot.addWidget(window) + window.lmc_edit.setText(str(library)); window.load_library(); qtbot.waitUntil(lambda: not window.busy) + assert reads == [] + window.search_edit.setText('IP101') + window.repair_icdb_all() + qtbot.waitUntil(lambda: not window.busy and bool(reads)) + assert window.visible_records()[0].status == 'READY' + assert not window._maintenance_recovery_view and window.search_edit.text() == 'IP101' diff --git a/tests/test_safety.py b/tests/test_safety.py index 2392f03..f439448 100644 --- a/tests/test_safety.py +++ b/tests/test_safety.py @@ -112,7 +112,7 @@ def _without_authorized_checkpoint_cleanup(text): """Exempt exact protocol-checkpoint callsites, never the whole module.""" tree = ast.parse(text) parents = {child: parent for parent in ast.walk(tree) for child in ast.iter_child_nodes(parent)} - allowed = {('LibraryLock', '__exit__'): 'marker', ('release_retained_library',): 'checkpoint'} + allowed = {('LibraryLock', '_exit_owned'): 'marker', ('release_retained_library',): 'checkpoint'} used, spans = set(), [] for call in ast.walk(tree): if not (isinstance(call, ast.Call) and isinstance(call.func, ast.Attribute) @@ -160,7 +160,7 @@ def test_verification_never_deletes_or_moves_library_files(): def test_checkpoint_cleanup_exception_does_not_allow_other_deletions(): source = ''' class LibraryLock: - def __exit__(self): + def _exit_owned(self): marker.unlink(missing_ok=True) self.path.unlink() def release_retained_library(): -- 2.39.5 From 4cbca0b5a69a3602ab10f360a6d1ffef22212ede 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: Tue, 3 Nov 2026 09:15:06 +0800 Subject: [PATCH 20/59] docs: explain explicit maintenance for existing ICDB addresses --- .../2026-11-03-explicit-icdb-maintenance.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 docs/releases/2026-11-03-explicit-icdb-maintenance.md diff --git a/docs/releases/2026-11-03-explicit-icdb-maintenance.md b/docs/releases/2026-11-03-explicit-icdb-maintenance.md new file mode 100644 index 0000000..470b896 --- /dev/null +++ b/docs/releases/2026-11-03-explicit-icdb-maintenance.md @@ -0,0 +1,38 @@ +# 既存地址记录下的明确 ICDB 维护入口 + +2026-11-03 的现场报告显示,点击“非 READY ICDB 检查与修复”后,IP101GR 在 +0.172 秒内返回 BLOCKED,没有启动官方 Repair。原先维护按钮和普通校验使用 +相同请求;普通校验对任何 sAddress.adr 的阻止条件也挡住了明确维护入口。 +随后保留的库协调检查点使重新读取与维护都无法继续。 + +本次为用户明确确认的维护增加独立入口。普通校验遇到地址记录仍停止,不依据 +文件日期、TCP 不通或本机无 PCB 进程判断地址失效,不删除、移动或隔离地址。 +没有地址阻止时,维护继续沿用原有校验及本轮失联后的自动修复流程。 + +存在地址记录时,明确维护需要唯一可确认的工程 PRJ/database 映射。共享符号库 +地址、多个地址、外部引用、歧义目标、不完整工程均会阻止执行。工具检查相关 +原生程序和保留工作进程,并同时独占读取库中的原生数据库文件;记录目标工程 +引用指纹和地址字节哈希,检查期间及完整备份后重新核对。完整工程备份保留 +原地址文件,再由官方 iCDB Project Backup 打开工程。只有其实际 Output 明确 +报告 `Project is not in use. Full iCDB Project Backup's functionality is available.`, +且没有占用或未知窗口时才请求 Repair。 + +库旁已有协调检查点时,明确维护可以接续有效的同库记录,条件是原监督进程 +已退出,或当前进程确实持有已闲置的保留锁。相关所有者和目标独占访问检查 +必须先全部通过,再原样归档旧检查点并建立本次检查点;库 sidecar 和全局 mutex +期间始终持有。未知、损坏、其他库的记录或仍存活的外部监督进程不会被接管。 +普通释放检查的地址要求没有放宽,上传锁文件不删除。 + +重启后有有效同库检查点时,界面仅按工程目录显示 UNKNOWN 状态,明确标注 +“状态未确认,不代表损坏”。此视图不调用原生状态读取,只允许明确维护或严格 +释放检查;普通校验、上传/更新/移除及备份恢复均禁用。安全完成维护后再重新 +读取权威状态,保留当前搜索条件。 + +Repair、原生退出、地址消失和最终校验仍是不同的判据。只有完整原生修复证据、 +所有者与地址释放及同一工程后验 PASS/GREEN 全部成立才计为修复通过。缺健康 +备份、恢复旧版本、占用、超时或不完整证据不会提升为成功,也不会盲目重试。 + +协调端在保留旧地址字节的用户工程副本上观察到:官方工具报告数据库曾在服务器 +运行时被复制、工程当前未使用,随后官方 Repair 自行清除副本地址并返回成功。 +这证明了明确维护入口的必要性;原始用户库没有由本次开发或测试修改。实际按钮、 +冻结包及最终全套回归由协调端在提交后的隔离副本继续验收。 -- 2.39.5 From b635432cff8daaa6fa2c0694cd0897679a61e1d9 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: Tue, 3 Nov 2026 09:16:13 +0800 Subject: [PATCH 21/59] fix: scope maintenance addresses to selected project and shared stores --- src/cbb_refresh_verify/maintenance_entry.py | 25 +++++++++++++++++---- tests/test_maintenance_entry.py | 12 ++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index fe0ff0b..a310343 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -43,6 +43,25 @@ def address_snapshot(paths): for path in paths} +def _scoped_addresses(request): + from .scoped_manifest import scan_tree + root = Path(request.lmc_file).parent + addresses = [] + for path in scan_tree(root): + 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 select_maintenance_target(request): from .verify_runner import preflight from .backup_recovery import _project_text @@ -71,8 +90,7 @@ def select_maintenance_target(request): if not (database/'icdb.dat').is_file(): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少 icdb.dat') candidates.append(RepairTarget(candidate, candidate.parent/'database')) - addresses = [path for path in scan_tree(Path(request.lmc_file).parent) - if path.name.casefold() == 'saddress.adr'] + addresses = _scoped_addresses(request) if len(addresses) != 1: raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 需要唯一工程地址记录;共享或多个数据库不能推测修复') matches = [target for target in candidates @@ -106,8 +124,7 @@ def maintenance_target_guard(request, expected=None): win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) stack.callback(handle.Close) assert_native_idle() - current_addresses = [p for p in scan_tree(Path(request.lmc_file).parent) - if p.name.casefold() == 'saddress.adr'] + current_addresses = _scoped_addresses(request) if address_snapshot(current_addresses) != baseline: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 地址记录在检查期间变化') yield {'target': target, 'context': context, 'project': project, 'pcb': pcb} diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index f71d425..04d8954 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -231,3 +231,15 @@ def test_guard_failure_keeps_takeover_checkpoint_byte_exact_when_address_changes with locks.LibraryLock.for_maintenance(request.lmc_file, request.evidence_dir/'takeover', lambda: entry.maintenance_target_guard(request)): pass assert marker.read_bytes() == old + + +def test_independent_cbb_address_does_not_select_or_block_current_project(case): + from cbb_refresh_verify.maintenance_entry import maintenance_target_guard + request, project, address = case + other = request.lmc_file.parent/'ReusableBlocksLP/1GPHY_8212/Work/database/cdbsvr/sAddress.adr' + other.parent.mkdir(parents=True); other.write_bytes(b'independent CBB original address') + with maintenance_target_guard(request) as checked: + assert checked['target'].project == project + assert set(checked['context']['addresses']) == {str(address.resolve())} + assert other.read_bytes() == b'independent CBB original address' + assert other.read_bytes() == b'independent CBB original address' -- 2.39.5 From 5a758ee3b6138cc3321566bc30c7f12ca6830025 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: Tue, 3 Nov 2026 09:18:36 +0800 Subject: [PATCH 22/59] fix: separate maintenance release from safe queue continuation --- src/cbb_refresh_verify/maintenance_entry.py | 14 +++++++++--- tests/test_maintenance_entry.py | 25 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index a310343..92c2d18 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -142,7 +142,8 @@ def maintenance_needed(request): def run_existing_address_maintenance(request, *, verify, progress=None): from .library_lock import LibraryLock - from .native_repair import run_native_repair, native_success_confirmed, repair_release_state + 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 @@ -185,8 +186,15 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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 - owner_safe = safe - if recovery.get('status') == 'REPAIRED' and safe and native_success_confirmed(target, recovery): + 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: emit('repair_verify', detail='原生修复及释放已确认,重新打开同一工程校验') owner_safe = False verified = verify(replace(request, maintenance=False, auto_repair=False)) diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index 04d8954..d06f098 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -243,3 +243,28 @@ def test_independent_cbb_address_does_not_select_or_block_current_project(case): assert set(checked['context']['addresses']) == {str(address.resolve())} assert other.read_bytes() == b'independent CBB original address' assert other.read_bytes() == b'independent CBB original address' + + +@pytest.mark.parametrize('status,complete,expected_safe', [ + ('RESTORED_REVIEW_REQUIRED', True, False), ('DATABASE_BUSY', True, False), + ('UNKNOWN_DIALOG', True, False), ('UNCONFIRMED', True, False), + ('MISSING_HEALTHY_BACKUP', False, False), ('NATIVE_REPAIR_FAILED', False, False), + ('MISSING_HEALTHY_BACKUP', True, True), ('NATIVE_REPAIR_FAILED', True, True)]) +def test_physical_release_alone_does_not_authorize_queue(case, monkeypatch, status, complete, expected_safe): + from cbb_refresh_verify import native_repair, verify_runner as runner, library_lock + from test_native_repair import complete_native_success + request, _, address = case + def repair(target, *args, **kwargs): + address.unlink() # Simulate official process releasing its test-only advertisement. + result = complete_native_success(target) + result.update(status=status, detail='native completed') + result['evidence']['failure_complete'] = complete + return result + monkeypatch.setattr(native_repair, 'run_native_repair', repair) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('nonrepair outcome reopened')) + result = runner.run_verify(request) + assert result.status != 'PASS' + assert result.safe_to_continue is expected_safe + assert result.recovery_attempts[0]['status'] == status + assert library_lock.has_blocker(request.lmc_file) is not expected_safe -- 2.39.5 From aa6684b588fc5a34f2a06a6737115a174f7239d7 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: Tue, 3 Nov 2026 09:36:33 +0800 Subject: [PATCH 23/59] fix: bound post-repair readwrite consent to proven local startup --- src/cbb_refresh_verify/dialog_agent.py | 8 + src/cbb_refresh_verify/maintenance_entry.py | 5 +- src/cbb_refresh_verify/mentor_adapter.py | 3 + src/cbb_refresh_verify/post_repair_consent.py | 266 ++++++++++++++++++ src/cbb_refresh_verify/ui/main_window.py | 2 + src/cbb_refresh_verify/verify_runner.py | 18 ++ tests/test_maintenance_entry.py | 2 + tests/test_post_repair_consent.py | 197 +++++++++++++ 8 files changed, 500 insertions(+), 1 deletion(-) create mode 100644 src/cbb_refresh_verify/post_repair_consent.py create mode 100644 tests/test_post_repair_consent.py diff --git a/src/cbb_refresh_verify/dialog_agent.py b/src/cbb_refresh_verify/dialog_agent.py index b9cf101..a78608c 100644 --- a/src/cbb_refresh_verify/dialog_agent.py +++ b/src/cbb_refresh_verify/dialog_agent.py @@ -236,6 +236,14 @@ class DialogActionExecutor: def perform(self, snapshot: WindowSnapshot, action: dict) -> ActionResult: channels = tuple(action.get("channels", ())) action_name = action.get("action") + if action_name == 'post_repair_local_read_write': + try: + from .post_repair_consent import validate_action_window + validate_action_window(snapshot, action) + if channels != ('win32_button_click',) or action.get('target') != {'control_id': 6}: + raise ValueError('POST_REPAIR_ACTION_MISMATCH') + except Exception as error: + return ActionResult(error_code='DATABASE_RECOVERY_REQUIRED', detail=str(error)) if action_name == "accept_license": target = self._license_target(snapshot) elif action_name == "restore_last_saved": diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 92c2d18..802105c 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -196,8 +196,11 @@ def run_existing_address_maintenance(request, *, verify, progress=None): owner_safe = safe and (confirmed or completed_failure) if confirmed: emit('repair_verify', detail='原生修复及释放已确认,重新打开同一工程校验') + from .post_repair_consent import PostRepairProof owner_safe = False - verified = verify(replace(request, maintenance=False, auto_repair=False)) + proof = PostRepairProof.create(request, target, recovery) + verified = verify(replace(request, maintenance=False, auto_repair=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', diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index 21fe87a..a848449 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -736,6 +736,9 @@ def execute_job(job, emit): emit(stage='lm_session_ready', lm_pid=lm_pid, block=job['block_name']) else: command = build_pcb_launch_command(job['pcb'], job.get('mentor_bin')) + if job.get('post_repair_consent') is not None: + from .post_repair_consent import prepare_launch + prepare_launch(job, command) emit(stage='pcb_launch', command=list(command), strategy='reusable_circuit_direct') launcher_process = launch_pcb_process(command, Path(job['pcb']).parent) worker_parent_pid = int(launcher_process.pid) diff --git a/src/cbb_refresh_verify/post_repair_consent.py b/src/cbb_refresh_verify/post_repair_consent.py new file mode 100644 index 0000000..2d4d7bd --- /dev/null +++ b/src/cbb_refresh_verify/post_repair_consent.py @@ -0,0 +1,266 @@ +"""One-use, local-only read/write consent after proven explicit native Repair. + +No normal Verify or declarative rule can enable this path. Database recovery +choices other than the captured read/write question remain unsupported. +""" +from contextlib import ExitStack +from dataclasses import dataclass +import json +import os +from pathlib import Path +import stat +import time +from uuid import uuid4 + +PROMPT_TEXT = ("If an XDS or ExpeditionPCB is running on this design, it's likely that\n" + "your file system and/or network system was too slow to determine\n" + "which product the design is open in within the 15 second timeout\nperiod.\n\n" + "Otherwise, the design may not have been closed properly due to\n" + "the XDS or ExpeditionPCB crashing in the last session.\n\n" + "Before continuing, please confirm that no other users have the\ndesign open.\n\n" + "Continue to open the design with read/write access permission?") + + +def _drive_type(path): + import win32file + return win32file.GetDriveType(str(Path(path).anchor)) + + +def require_local_fixed(path): + from .network_path import absolute_preserving_drive + path = absolute_preserving_drive(path) + if path.drive.startswith('\\') or _drive_type(path) != 3: + raise ValueError('LOCAL_FIXED_REQUIRED: 修复后读写确认仅支持本地固定磁盘;UNC/网络盘不自动授权') + for item in (path, *path.parents): + if item.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT: + raise ValueError('LOCAL_FIXED_REQUIRED: 不允许重解析点、目录链接或挂载路径') + return path + + +def _same(a, b): + return Path(a).resolve() == Path(b).resolve() + + +def _process_records(): + import win32com.client + names = ('ExpeditionPCB.exe', 'LibraryManager.exe', 'viewdraw.exe', 'XDS.exe', + 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe', + 'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe') + service = win32com.client.GetObject('winmgmts:') + rows = service.ExecQuery('SELECT ProcessId, Name, ExecutablePath, CommandLine, ParentProcessId ' + 'FROM Win32_Process WHERE ' + ' OR '.join(f"Name='{n}'" for n in names)) + records = [] + for item in rows: + name, command = str(item.Name), str(item.CommandLine or '') + if name.casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe') and command and not any( + flag in command for flag in ('--verify-worker', '--repair-worker', '--management-worker', '--dialog-agent')): + continue + records.append((int(item.ProcessId), name, str(item.ExecutablePath or ''), command, int(item.ParentProcessId or 0))) + return records + + +@dataclass(frozen=True) +class PostRepairProof: + library: str + project: str + pcb: str + block: str + repair_project: str + repair_database: str + repair_json: str + nonce: str + + @classmethod + def create(cls, request, target, recovery): + from .native_repair import native_success_confirmed + from .verify_runner import preflight + if request.maintenance is not True or not native_success_confirmed(target, recovery): + raise ValueError('POST_REPAIR_PROOF_REQUIRED: 必须是明确维护且严格确认的原生修复') + if request.mapped_remote_root: + raise ValueError('LOCAL_FIXED_REQUIRED: 网络映射库不自动确认修复后的读写访问') + project, pcb = preflight(request) + for path in (request.lmc_file, project, pcb, target.database/'icdb.dat', pcb.parent/'Layout/LayoutDB.lyt'): + require_local_fixed(path) + return cls(str(request.lmc_file), str(project), str(pcb), request.block_name, + str(target.project), str(target.database), json.dumps(recovery, ensure_ascii=False, default=str), uuid4().hex) + + def for_job(self, job): + if (job.get('interactive_input') or job.get('native_lm_verify') + or not str(job.get('desktop', '')).startswith('CbbVerify_') + or job['block_name'] != self.block + or not all(_same(job[key], value) for key, value in ( + ('lmc_file', self.library), ('project', self.project), ('pcb', self.pcb)))): + raise ValueError('POST_REPAIR_TARGET_MISMATCH') + return {'nonce': self.nonce, 'intent': 'explicit_maintenance_after_confirmed_repair', + 'library': self.library, 'project': self.project, 'pcb': self.pcb, 'block': self.block, + 'repair_project': self.repair_project, 'repair_database': self.repair_database, + 'repair': json.loads(self.repair_json), 'desktop': job['desktop'], 'root': job['root']} + + +def _validate_grant(job): + from .native_repair import RepairTarget, native_success_confirmed + grant = job.get('post_repair_consent') + if (not isinstance(grant, dict) or grant.get('intent') != 'explicit_maintenance_after_confirmed_repair' + or not isinstance(grant.get('nonce'), str) or len(grant['nonce']) != 32 + or job.get('interactive_input') or job.get('native_lm_verify') or job.get('mapped_remote_root') + or grant.get('desktop') != job.get('desktop') or not str(job.get('desktop', '')).startswith('CbbVerify_') + or not _same(grant['root'], job['root']) or grant.get('block') != job.get('block_name') + or not all(_same(grant[key], job[field]) for key, field in ( + ('library', 'lmc_file'), ('project', 'project'), ('pcb', 'pcb')))): + raise ValueError('POST_REPAIR_PROOF_INVALID') + target = RepairTarget(Path(grant['repair_project']), Path(grant['repair_database'])) + if not native_success_confirmed(target, grant['repair']): + raise ValueError('POST_REPAIR_PROOF_INVALID: 原生成功证据不完整') + return grant + + +def prepare_launch(job, command): + """Called on the private worker immediately before direct PCB launch.""" + import win32file, win32con + from .icdb_ownership import database_directories, unreleased_addresses + from .management import _write_json + grant = _validate_grant(job) + from .native_repair_ui import _current_desktop_name + if _current_desktop_name() != job['desktop']: + raise ValueError('POST_REPAIR_PRIVATE_DESKTOP_REQUIRED') + library, project, pcb = (Path(job[key]) for key in ('lmc_file', 'project', 'pcb')) + for path in (library, project, pcb, pcb.parent/'pcb.prj'): + require_local_fixed(path) + if len(command) != 3 or not _same(command[1], pcb) or command[2] != '-ReusableCircuit': + raise ValueError('POST_REPAIR_LAUNCH_MISMATCH') + def require_idle(): + # Self is an expected automation worker, never a native owner. + others = [r for r in _process_records() if r[0] not in (os.getpid(), int(job['supervisor_pid'])) + and not (r[1].casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe') + and '--dialog-agent' in r[3] and r[4] == int(job['supervisor_pid']))] + if others: raise ValueError('POST_REPAIR_FOREIGN_OWNER: 存在其他原生进程或助手') + require_idle() + if unreleased_addresses(library, project, pcb): raise ValueError('POST_REPAIR_ADDRESS_REMAINS') + files = [library, pcb.parent/'Layout/LayoutDB.lyt'] + files.extend(folder/'icdb.dat' for folder in database_directories(library, project, pcb) if (folder/'icdb.dat').is_file()) + with ExitStack() as stack: + for path in dict.fromkeys(files): + require_local_fixed(path) + try: + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + except Exception as error: + raise ValueError(f'POST_REPAIR_EXCLUSIVE_ACCESS_REQUIRED: {path}: {error}') from error + stack.callback(handle.Close) + if unreleased_addresses(library, project, pcb): raise ValueError('POST_REPAIR_ADDRESS_CHANGED') + require_idle() + _write_json(Path(job['root'])/'post-repair-prelaunch.json', { + 'nonce': grant['nonce'], 'desktop': job['desktop'], 'project': str(project), 'pcb': str(pcb), + 'library': str(library), 'worker_pid': os.getpid(), 'supervisor_pid': job['supervisor_pid'], + 'command': list(command), 'exclusive_files': [str(path) for path in dict.fromkeys(files)], + 'addresses': [], 'monotonic': time.monotonic()}) + + +def exact_prompt(snapshot): + from .dialog_models import DialogPhase + if (snapshot.phase != DialogPhase.PCB_STARTING or snapshot.process_name.casefold() != 'expeditionpcb.exe' + or snapshot.process_role != 'pcb' or snapshot.window_class != '#32770' or snapshot.title != 'Expedition PCB'): + return False + buttons = [c for c in snapshot.controls if c.class_name == 'Button'] + static = [c for c in snapshot.controls if c.class_name == 'Static' and c.name.strip()] + return (len(buttons) == 2 and {c.control_id for c in buttons} == {6, 7} + and all(c.enabled for c in buttons) and len(static) == 1 and static[0].control_id == 65535 + and ' '.join(static[0].name.split()) == ' '.join(PROMPT_TEXT.split()) + and all(c.class_name == 'Button' or c.class_name == 'Static' and c.control_id in (20, 65535) + for c in snapshot.controls)) + + +def _validate_current_processes(job, prelaunch, target_pid, launcher_pid, worker_pid, companion_pids): + from .mentor_adapter import is_reusable_circuit_pcb + rows = _process_records() + by_pid = {r[0]: r for r in rows} + if target_pid not in by_pid or launcher_pid not in by_pid: + raise ValueError('POST_REPAIR_PROCESS_UNCONFIRMED') + target, launcher = by_pid[target_pid], by_pid[launcher_pid] + executable = Path(prelaunch['command'][0]) + expected_worker = executable.parents[3]/'wg/win32/bin/ExpeditionPCB.exe' + if (not _same(launcher[2], executable) or not _same(target[2], expected_worker) + or launcher[4] != worker_pid or target[4] != launcher_pid + or not is_reusable_circuit_pcb(target[2], target[3], job['pcb']) + or not is_reusable_circuit_pcb(launcher[2], launcher[3], job['pcb'])): + raise ValueError('POST_REPAIR_PROCESS_IDENTITY_MISMATCH') + allowed = {target_pid, launcher_pid, worker_pid, int(job['supervisor_pid']), *companion_pids} + if any(r[0] not in allowed for r in rows): raise ValueError('POST_REPAIR_FOREIGN_OWNER') + + +class ConsentController: + def __init__(self, proof, job): + if not isinstance(proof, PostRepairProof): raise ValueError('POST_REPAIR_PROOF_REQUIRED') + self.grant = proof.for_job(job) + self.job = json.loads(json.dumps(job)) + self.used = False + self.snapshot = None + self.sent_at = 0.0 + + def handle(self, snapshot, snapshots, *, target_pid, launcher_pid, worker_pid, companion_pids, perform): + from .dialog_supervisor import DialogPollResult + from .management import _write_json + try: + if not exact_prompt(snapshot) or snapshot.pid != target_pid: + raise ValueError('POST_REPAIR_PROMPT_MISMATCH') + if len(snapshots) != 1: + raise ValueError('POST_REPAIR_ADDITIONAL_DIALOG') + if self.used: + if (self.snapshot.hwnd == snapshot.hwnd and self.snapshot.signature == snapshot.signature + and time.monotonic() - self.sent_at < 2): + return DialogPollResult(activity=False, observed_snapshots=tuple(snapshots)) + raise ValueError('POST_REPAIR_CONSENT_ALREADY_USED') + pre = json.loads((Path(self.job['root'])/'post-repair-prelaunch.json').read_text(encoding='utf-8')) + required = {Path(self.grant['library']).resolve(), + (Path(self.grant['pcb']).parent/'Layout/LayoutDB.lyt').resolve(), + (Path(self.grant['repair_database'])/'icdb.dat').resolve()} + if (pre.get('nonce') != self.grant['nonce'] or pre.get('desktop') != self.job['desktop'] + or pre.get('worker_pid') != worker_pid or pre.get('addresses') != [] + or pre.get('supervisor_pid') != self.job['supervisor_pid'] + or not required.issubset({Path(p).resolve() for p in pre.get('exclusive_files', [])}) + or not 0 <= time.monotonic() - pre.get('monotonic', 0) <= 180 + or not all(_same(pre[key], self.grant[key]) for key in ('project', 'pcb', 'library'))): + raise ValueError('POST_REPAIR_PRELAUNCH_PROOF_INVALID') + for path in (self.grant['library'], self.grant['project'], self.grant['pcb']): require_local_fixed(path) + _validate_current_processes(self.job, pre, target_pid, launcher_pid, worker_pid, companion_pids) + payload = {'action': 'post_repair_local_read_write', 'channels': ['win32_button_click'], + 'target': {'control_id': 6}, 'postcondition': 'window_gone', + 'desktop': self.job['desktop'], 'pid': target_pid} + # Consume before messaging: failure/timeout cannot authorize another click. + self.used = True; self.snapshot = snapshot; self.sent_at = time.monotonic() + _write_json(Path(self.job['root'])/'post-repair-consent.json', { + 'nonce': self.grant['nonce'], 'consumed': True, 'target_pid': target_pid, + 'launcher_pid': launcher_pid, 'worker_pid': worker_pid, 'snapshot': snapshot.to_record()}) + result = perform(snapshot, payload, snapshot.phase) + return DialogPollResult(activity=True, action_sent=bool(result.accepted_controls), snapshot=snapshot, + error_code=result.error_code, detail=result.detail, accepted_controls=result.accepted_controls, + action=payload['action'], channel=result.channel, rule_id='explicit-local-post-repair-consent', + observed_snapshots=tuple(snapshots)) + except Exception as error: + return DialogPollResult(error_code='DATABASE_RECOVERY_REQUIRED', detail=str(error), + snapshot=snapshot, observed_snapshots=tuple(snapshots)) + + +def validate_action_window(snapshot, action): + """Final check on the agent's actual private desktop, immediately before Yes.""" + import win32gui, win32process + from .native_repair_ui import _current_desktop_name + from .dialog_models import ControlSnapshot + if (not exact_prompt(snapshot) or snapshot.pid != action.get('pid') + or _current_desktop_name() != action.get('desktop') + or not str(action.get('desktop', '')).startswith('CbbVerify_') + or win32process.GetWindowThreadProcessId(snapshot.hwnd)[1] != snapshot.pid + or win32gui.GetClassName(snapshot.hwnd) != snapshot.window_class + or win32gui.GetWindowText(snapshot.hwnd) != snapshot.title or not win32gui.IsWindowVisible(snapshot.hwnd)): + raise ValueError('POST_REPAIR_LIVE_WINDOW_MISMATCH') + children = [] + win32gui.EnumChildWindows(snapshot.hwnd, lambda hwnd, _: children.append(int(hwnd)), None) + live = tuple(ControlSnapshot(hwnd, win32gui.GetDlgCtrlID(hwnd) & 0xFFFF, + win32gui.GetClassName(hwnd), win32gui.GetWindowText(hwnd), enabled=bool(win32gui.IsWindowEnabled(hwnd))) + for hwnd in children) + from dataclasses import replace + if not exact_prompt(replace(snapshot, controls=live)): + raise ValueError('POST_REPAIR_LIVE_PROMPT_CHANGED') + yes = next(c for c in live if c.control_id == 6) + if yes.handle != next(c.handle for c in snapshot.controls if c.control_id == 6): + raise ValueError('POST_REPAIR_BUTTON_CHANGED') diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index c2811f5..fb35e4e 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -572,6 +572,8 @@ class MainWindow(QWidget): f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' '无地址阻止时逐项校验;已有工程地址记录时先核对所有者和独占访问、完整备份,' '再由官方工具确认未占用并检查/修复。不会删除地址文件。\n' + '仅本地固定磁盘:严格确认修复并重新独占检查后,可一次确认本次异常退出的读写访问提示。' + '网络盘、UNC 或链接路径不自动确认该提示。\n' '共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;' '会话安全无法确认时停止队列。\n\n是否开始检查与修复?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 595af8b..17f7d0f 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -64,6 +64,7 @@ class VerifyRequest: mapped_remote_root: str = '' auto_repair: bool = True maintenance: bool = False + post_repair_proof: object | None = field(default=None, repr=False, compare=False) @dataclass(frozen=True) @@ -808,11 +809,19 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run 'desktop': 'CbbVerify_' + uuid4().hex[:12], 'cancel_signal': str(root/'cancel-worker.signal'), 'start_gate': str(root/'start-worker.signal')} + consent = None + if request.post_repair_proof is not None: + from .post_repair_consent import PostRepairProof, ConsentController + if not isinstance(request.post_repair_proof, PostRepairProof): + return finish(VerifyResult('BLOCKED', stderr='POST_REPAIR_PROOF_REQUIRED', safe_to_continue=False)) + job['post_repair_consent'] = request.post_repair_proof.for_job(job) + consent = ConsentController(request.post_repair_proof, job) job_path = root/'request.json' job_path.write_text(json.dumps(job, ensure_ascii=False), encoding='utf-8') events = root/'events.jsonl' offset = 0 target_pid = None + launcher_pid = None lm_pid = None verify_invoked = False invoke_returned = False @@ -875,6 +884,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run break timing.record(event) if event.get('pcb_pid'): target_pid = event['pcb_pid'] + if event.get('launcher_pid'): launcher_pid = event['launcher_pid'] if event.get('lm_pid'): lm_pid = event['lm_pid'] stage = event.get('stage') if stage == 'verify_invoke': verify_invoked = True @@ -926,6 +936,14 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run dialog_result = dialog_supervisor.poll() else: dialog_result = dialog_supervisor.poll() + if (consent is not None and dialog_agent is not None + and dialog_result.error_code == 'DATABASE_RECOVERY_REQUIRED' + and dialog_result.snapshot is not None): + consent_snapshot = dialog_result.snapshot + if consent_snapshot.hwnd in {int(h) for h in process.desktop.EnumDesktopWindows()}: + dialog_result = consent.handle(consent_snapshot, dialog_result.observed_snapshots, + target_pid=target_pid, launcher_pid=launcher_pid, worker_pid=process.pid, + companion_pids=tuple(process.companion_pids), perform=dialog_supervisor.backend.perform) # Normal progress stays in memory. Only actions/errors and the # last failure snapshots are persisted, not every elapsed tick. latest_snapshots = dialog_result.observed_snapshots diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index d06f098..f498a9b 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -65,6 +65,8 @@ def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fa {'safe_to_continue': True, 'native_exited': True, 'addresses': []}) def verify(req, **kwargs): calls.append('verify') + from cbb_refresh_verify.post_repair_consent import PostRepairProof + assert isinstance(req.post_repair_proof, PostRepairProof) assert not req.maintenance and not req.auto_repair assert req.lmc_file == request.lmc_file and req.block_name == request.block_name return runner.VerifyResult('PASS', library_flag='GREEN') diff --git a/tests/test_post_repair_consent.py b/tests/test_post_repair_consent.py new file mode 100644 index 0000000..e3fe60e --- /dev/null +++ b/tests/test_post_repair_consent.py @@ -0,0 +1,197 @@ +"""Local recovery consent boundaries. No native application may be launched.""" +from dataclasses import replace +import json +import os +from pathlib import Path +from types import SimpleNamespace + +import pytest + + +@pytest.fixture(autouse=True) +def no_native_launch(monkeypatch): + import subprocess, win32process, win32com.client + def forbidden(*a, **k): raise AssertionError('native launching forbidden in consent tests') + monkeypatch.setattr(subprocess, 'Popen', forbidden) + monkeypatch.setattr(win32process, 'CreateProcess', forbidden) + monkeypatch.setattr(win32com.client, 'DispatchEx', forbidden) + monkeypatch.setattr(win32com.client, 'Dispatch', forbidden) + + +@pytest.fixture +def context(tmp_path, monkeypatch): + from test_native_repair import target_fixture, complete_native_success + from cbb_refresh_verify import post_repair_consent as consent + request, _, target = target_fixture(tmp_path) + request = replace(request, maintenance=True) + # Existing fixture contains only required native files and no address. + (target.project.parent/'PCB/pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n') + monkeypatch.setattr(consent, '_process_records', lambda: []) + monkeypatch.setattr('cbb_refresh_verify.native_repair_ui._current_desktop_name', lambda: 'CbbVerify_fixture') + proof = consent.PostRepairProof.create(request, target, complete_native_success(target)) + job = {'root': str(tmp_path/'verify'), 'lmc_file': str(request.lmc_file), + 'project': str(target.project), 'pcb': str(target.project.parent/'PCB/actual.pcb'), + 'block_name': request.block_name, 'desktop': 'CbbVerify_fixture', 'supervisor_pid': 55, + 'interactive_input': False, 'native_lm_verify': False} + Path(job['root']).mkdir() + job['post_repair_consent'] = proof.for_job(job) + return request, target, proof, job + + +def snapshot(): + from cbb_refresh_verify.post_repair_consent import PROMPT_TEXT + from cbb_refresh_verify.dialog_models import WindowSnapshot, ControlSnapshot, DialogPhase + return WindowSnapshot(100, 20, 'ExpeditionPCB.exe', 'pcb', '#32770', 'Expedition PCB', + DialogPhase.PCB_STARTING, ( + ControlSnapshot(106, 6, 'Button', '是(&Y)'), ControlSnapshot(107, 7, 'Button', '否(&N)'), + ControlSnapshot(120, 20, 'Static', ''), ControlSnapshot(199, 65535, 'Static', PROMPT_TEXT))) + + +def test_no_generic_maintenance_flag_can_mint_consent(tmp_path): + from cbb_refresh_verify.post_repair_consent import PostRepairProof + from test_native_repair import target_fixture, complete_native_success + request, _, target = target_fixture(tmp_path) + with pytest.raises(ValueError): PostRepairProof.create(request, target, complete_native_success(target)) + for status in ('RESTORED_REVIEW_REQUIRED', 'UNCONFIRMED', 'FAIL'): + result = complete_native_success(target); result['status'] = status + with pytest.raises(ValueError): PostRepairProof.create(replace(request, maintenance=True), target, result) + + +@pytest.mark.parametrize('kind', ['unc', 'network', 'removable', 'reparse']) +def test_consent_rejects_nonlocal_or_reparse_paths(context, monkeypatch, kind): + from cbb_refresh_verify import post_repair_consent as consent + request, _, _, _ = context + path = request.lmc_file + if kind == 'unc': path = Path(r'\\server\share\library.lmc') + monkeypatch.setattr(consent, '_drive_type', lambda path: {'network': 4, 'removable': 2}.get(kind, 3)) + if kind == 'reparse': + original = Path.lstat + monkeypatch.setattr(Path, 'lstat', lambda self: SimpleNamespace(st_file_attributes=0x400) + if self == path.parent else original(self)) + with pytest.raises(ValueError, match='LOCAL_FIXED'): + consent.require_local_fixed(path) + + +@pytest.mark.parametrize('obstacle', ['owner', 'address', 'exclusive', 'wrong_target', 'network', 'desktop']) +def test_prelaunch_proof_blocks_without_launch(context, monkeypatch, obstacle): + import win32file, win32con + from cbb_refresh_verify import post_repair_consent as consent + _, target, _, job = context + handle = None + if obstacle == 'owner': monkeypatch.setattr(consent, '_process_records', lambda: [(777, 'viewdraw.exe', '', '', 0)]) + if obstacle == 'address': + p = target.database/'cdbsvr/sAddress.adr'; p.parent.mkdir(parents=True); p.touch() + if obstacle == 'exclusive': + handle = win32file.CreateFile(str(target.project.parent/'PCB/Layout/LayoutDB.lyt'), win32con.GENERIC_READ, + 0, None, win32con.OPEN_EXISTING, 0, None) + if obstacle == 'wrong_target': job['pcb'] = str(Path(job['pcb']).with_name('other.pcb')) + if obstacle == 'network': monkeypatch.setattr(consent, '_drive_type', lambda path: 4) + if obstacle == 'desktop': monkeypatch.setattr('cbb_refresh_verify.native_repair_ui._current_desktop_name', lambda: 'Default') + try: + with pytest.raises((ValueError, OSError)): + consent.prepare_launch(job, ['C:/Mentor/common/win32/bin/ExpeditionPCB.exe', job['pcb'], '-ReusableCircuit']) + assert not (Path(job['root'])/'post-repair-prelaunch.json').exists() + finally: + if handle: handle.Close() + + +def test_exact_dialog_after_bound_prelaunch_is_allowed_only_once(context, monkeypatch): + from cbb_refresh_verify import post_repair_consent as consent + _, _, proof, job = context + command = ['C:/Mentor/common/win32/bin/ExpeditionPCB.exe', job['pcb'], '-ReusableCircuit'] + consent.prepare_launch(job, command) + controller = consent.ConsentController(proof, job) + monkeypatch.setattr(consent, '_validate_current_processes', lambda *args: None) + calls = [] + def perform(snap, payload, phase): + calls.append(payload) + return SimpleNamespace(accepted_controls=(6,), channel='win32_button_click', error_code='', detail='') + first = controller.handle(snapshot(), (snapshot(),), target_pid=20, launcher_pid=19, + worker_pid=os.getpid(), companion_pids=(), perform=perform) + assert first.action_sent and calls[0]['target'] == {'control_id': 6} + again = controller.handle(snapshot(), (snapshot(),), target_pid=20, launcher_pid=19, + worker_pid=os.getpid(), companion_pids=(), perform=perform) + assert not again.action_sent and len(calls) == 1 + changed = replace(snapshot(), hwnd=101) + rejected = controller.handle(changed, (changed,), target_pid=20, launcher_pid=19, + worker_pid=os.getpid(), companion_pids=(), perform=perform) + assert rejected.error_code == 'DATABASE_RECOVERY_REQUIRED' and len(calls) == 1 + + +@pytest.mark.parametrize('change', ['pid', 'phase', 'text', 'button', 'extra', 'proof', 'foreign_owner']) +def test_consent_rejects_unbound_or_changed_prompt(context, monkeypatch, change): + from cbb_refresh_verify import post_repair_consent as consent + from cbb_refresh_verify.dialog_models import DialogPhase, ControlSnapshot + _, _, proof, job = context + consent.prepare_launch(job, ['C:/Mentor/common/win32/bin/ExpeditionPCB.exe', job['pcb'], '-ReusableCircuit']) + controller = consent.ConsentController(proof, job) + snap = snapshot() + if change == 'pid': snap = replace(snap, pid=99) + if change == 'phase': snap = replace(snap, phase=DialogPhase.SAVE) + if change == 'text': snap = replace(snap, controls=(*snap.controls[:-1], replace(snap.controls[-1], name='Confirm read/write?'))) + if change == 'button': snap = replace(snap, controls=(replace(snap.controls[0], enabled=False), *snap.controls[1:])) + if change == 'extra': snap = replace(snap, controls=(*snap.controls, ControlSnapshot(300, 99, 'Button', 'Overwrite'))) + if change == 'proof': (Path(job['root'])/'post-repair-prelaunch.json').write_text('{}') + def processes(*a): + if change == 'foreign_owner': raise ValueError('unexpected owner') + monkeypatch.setattr(consent, '_validate_current_processes', processes) + result = controller.handle(snap, (snap,), target_pid=20, launcher_pid=19, worker_pid=os.getpid(), + companion_pids=(), perform=lambda *a: pytest.fail('unapproved consent sent')) + assert result.error_code == 'DATABASE_RECOVERY_REQUIRED' + + +@pytest.mark.parametrize('change', ['', 'other_owner', 'target_command', 'launcher_command', 'target_parent', + 'launcher_parent', 'executable', 'missing_launcher']) +def test_current_process_identity_requires_exact_owned_launch_chain(context, monkeypatch, change): + from cbb_refresh_verify import post_repair_consent as consent + _, _, _, job = context + launcher = r'C:\Mentor\common\win32\bin\ExpeditionPCB.exe' + worker = r'C:\Mentor\wg\win32\bin\ExpeditionPCB.exe' + command = lambda exe: f'"{exe}" "{job["pcb"]}" -ReusableCircuit' + rows = [(19, 'ExpeditionPCB.exe', launcher, command(launcher), 18), + (20, 'ExpeditionPCB.exe', worker, command(worker), 19)] + if change == 'other_owner': rows.append((99, 'viewdraw.exe', 'viewdraw.exe', '', 0)) + if change == 'target_command': rows[1] = (20, 'ExpeditionPCB.exe', worker, 'other.pcb -ReusableCircuit', 19) + if change == 'launcher_command': rows[0] = (19, 'ExpeditionPCB.exe', launcher, 'other.pcb -ReusableCircuit', 18) + if change == 'target_parent': rows[1] = (*rows[1][:4], 999) + if change == 'launcher_parent': rows[0] = (*rows[0][:4], 999) + if change == 'executable': rows[1] = (20, 'ExpeditionPCB.exe', 'C:/unrelated/ExpeditionPCB.exe', command(worker), 19) + if change == 'missing_launcher': rows.pop(0) + monkeypatch.setattr(consent, '_process_records', lambda: rows) + args = (job, {'command': [launcher, job['pcb'], '-ReusableCircuit']}, 20, 19, 18, ()) + if change: + with pytest.raises(ValueError): consent._validate_current_processes(*args) + else: + consent._validate_current_processes(*args) + + +@pytest.mark.parametrize('change', ['', 'desktop', 'pid', 'text', 'button', 'generic_action']) +def test_agent_revalidates_real_window_before_single_button_message(monkeypatch, change): + import win32gui, win32process + from cbb_refresh_verify.dialog_agent import DialogActionExecutor + from cbb_refresh_verify import native_repair_ui + value = snapshot() + controls = {c.handle: c for c in value.controls} + monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'Default' if change == 'desktop' else 'CbbVerify_fixture') + monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 99 if change == 'pid' else 20)) + monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: '#32770' if hwnd == 100 else controls[hwnd].class_name) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else + 'changed prompt' if change == 'text' and hwnd == 199 else controls[hwnd].name) + monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda hwnd: True) + monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda hwnd: not (change == 'button' and hwnd == 106)) + monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda hwnd: controls[hwnd].control_id) + monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, callback, arg: + [callback(child, arg) for child in controls]) + calls = [] + monkeypatch.setattr(win32gui, 'PostMessage', lambda *args: calls.append(args)) + payload = {'action': 'post_repair_local_read_write', 'channels': ['win32_button_click'], + 'target': {'control_id': 6}, 'pid': 20, 'desktop': 'CbbVerify_fixture'} + if change == 'generic_action': payload.pop('desktop') + result = DialogActionExecutor().perform(value, payload) + assert bool(result.accepted_controls) is (not change) + assert len(calls) == (0 if change else 1) + + +def test_ordinary_rule_remains_hard_blocked_for_exact_recovery_prompt(): + from cbb_refresh_verify.dialog_rules import decide + assert decide(snapshot(), ()).error_code == 'DATABASE_RECOVERY_REQUIRED' -- 2.39.5 From 9806ef8f66dac7134d203230ffee25df13a54e27 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: Tue, 3 Nov 2026 09:37:52 +0800 Subject: [PATCH 24/59] docs: record local-only post-repair consent boundary --- .../2026-11-03-explicit-icdb-maintenance.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/releases/2026-11-03-explicit-icdb-maintenance.md b/docs/releases/2026-11-03-explicit-icdb-maintenance.md index 470b896..9b9b7a6 100644 --- a/docs/releases/2026-11-03-explicit-icdb-maintenance.md +++ b/docs/releases/2026-11-03-explicit-icdb-maintenance.md @@ -36,3 +36,25 @@ Repair、原生退出、地址消失和最终校验仍是不同的判据。只 运行时被复制、工程当前未使用,随后官方 Repair 自行清除副本地址并返回成功。 这证明了明确维护入口的必要性;原始用户库没有由本次开发或测试修改。实际按钮、 冻结包及最终全套回归由协调端在提交后的隔离副本继续验收。 + +## 修复后的本地读写访问提示 + +实际副本验证发现,原理图 iCDB Repair 成功并清除地址后,PCB 仍可能显示上次 +异常退出后的读写访问确认。这与选择恢复旧数据库不同。明确维护确认现在说明: +仅在本地固定磁盘、严格确认修复后,工具可对本次启动的这一完整提示确认一次。 + +该授权不能由普通 Verify 或通用对话框规则开启。维护代码先验证完整原生修复 +证据,生成绑定原库、PRJ、PCB 的内部凭据。实际 PCB 启动前,重新确认地址缺失、 +无其他原生所有者,并同时独占读取 LMC、相关 icdb.dat 和选中 LayoutDB.lyt, +在本次私有桌面生成短时有效的启动证据。出现提示时再核对当前 PCB 与启动器的 +实际 PID、父子关系、安装路径和完整设计参数,以及其他所有者缺失。 + +只接受原始完整英文提示正文(仅忽略空白差异)、精确窗口类型/标题和唯一可用 +Yes6/No7 按钮。专用代理在自己的私有桌面再次读取实际窗口和控件;先消耗授权, +再发送一次按钮消息。没有证据、提示变更、重复提示、额外弹窗或身份不符均停止, +不使用鼠标、前台输入或通用 Yes 回退。后续文档身份、保存关闭和 READY 判据不变。 + +路径必须经 GetDriveType 确认为 LOCAL_FIXED,且没有重解析点祖先。UNC、网络 +映射盘、可移动盘和链接路径不自动确认。该检查不宣称具备跨主机分布式排他能力; +数据库只读句柄在交给官方 PCB 启动前释放,任意非协作外部写入者之间仍不存在 +跨进程原子事务保证。普通校验及其他数据库恢复选择继续保持硬性阻断。 -- 2.39.5 From 99a708b40c6ce1fb078002ad89a652ce55f2220f 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: Tue, 3 Nov 2026 09:43:16 +0800 Subject: [PATCH 25/59] fix: distinguish UIA dialog chrome from native consent controls --- src/cbb_refresh_verify/post_repair_consent.py | 19 ++++++-- tests/test_post_repair_consent.py | 45 +++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/cbb_refresh_verify/post_repair_consent.py b/src/cbb_refresh_verify/post_repair_consent.py index 2d4d7bd..58d0167 100644 --- a/src/cbb_refresh_verify/post_repair_consent.py +++ b/src/cbb_refresh_verify/post_repair_consent.py @@ -161,13 +161,26 @@ def exact_prompt(snapshot): if (snapshot.phase != DialogPhase.PCB_STARTING or snapshot.process_name.casefold() != 'expeditionpcb.exe' or snapshot.process_role != 'pcb' or snapshot.window_class != '#32770' or snapshot.title != 'Expedition PCB'): return False - buttons = [c for c in snapshot.controls if c.class_name == 'Button'] - static = [c for c in snapshot.controls if c.class_name == 'Static' and c.name.strip()] + controls = [] + for control in snapshot.controls: + self_window = (control.handle == snapshot.hwnd and control.control_id == 0 + and control.class_name == snapshot.window_class and control.name == snapshot.title + and control.control_type == 'Window' and control.patterns == ('Window',)) + uia_chrome = (control.handle == 0 and control.control_id == 0 + and control.class_name == '' and control.automation_id == '' + and not control.selected and not control.checked and ( + control.control_type == 'TitleBar' and control.name == '' and control.patterns == ('Value',) + or control.control_type == 'Button' and not control.enabled + and control.name in ('关闭', 'Close') and control.patterns == ('Invoke',))) + if not self_window and not uia_chrome: + controls.append(control) + buttons = [c for c in controls if c.class_name == 'Button'] + static = [c for c in controls if c.class_name == 'Static' and c.name.strip()] return (len(buttons) == 2 and {c.control_id for c in buttons} == {6, 7} and all(c.enabled for c in buttons) and len(static) == 1 and static[0].control_id == 65535 and ' '.join(static[0].name.split()) == ' '.join(PROMPT_TEXT.split()) and all(c.class_name == 'Button' or c.class_name == 'Static' and c.control_id in (20, 65535) - for c in snapshot.controls)) + for c in controls)) def _validate_current_processes(job, prelaunch, target_pid, launcher_pid, worker_pid, companion_pids): diff --git a/tests/test_post_repair_consent.py b/tests/test_post_repair_consent.py index e3fe60e..03632af 100644 --- a/tests/test_post_repair_consent.py +++ b/tests/test_post_repair_consent.py @@ -195,3 +195,48 @@ def test_agent_revalidates_real_window_before_single_button_message(monkeypatch, def test_ordinary_rule_remains_hard_blocked_for_exact_recovery_prompt(): from cbb_refresh_verify.dialog_rules import decide assert decide(snapshot(), ()).error_code == 'DATABASE_RECOVERY_REQUIRED' + + +def enriched_snapshot(): + """Sanitized actual aa-r2 merged Win32/UIA capture (only HWNDs changed).""" + from cbb_refresh_verify.dialog_models import ControlSnapshot + value = snapshot() + native = (replace(value.controls[0], name='是(Y)', automation_id='6', control_type='Button', patterns=('Invoke',)), + replace(value.controls[1], name='否(N)', automation_id='7', control_type='Button', patterns=('Invoke',)), + replace(value.controls[2], automation_id='20', control_type='Image'), + replace(value.controls[3], automation_id='65535', control_type='Text')) + return replace(value, controls=(*native, + ControlSnapshot(value.hwnd, 0, '#32770', 'Expedition PCB', control_type='Window', patterns=('Window',)), + ControlSnapshot(0, 0, '', '', control_type='TitleBar', patterns=('Value',)), + ControlSnapshot(0, 0, '', '关闭', control_type='Button', enabled=False, patterns=('Invoke',)))) + + +@pytest.mark.parametrize('change', ['', 'extra_real_child', 'enabled_close', 'titlebar_text', 'other_self']) +def test_enriched_native_prompt_ignores_only_observed_uia_chrome(change): + from cbb_refresh_verify.post_repair_consent import exact_prompt + from cbb_refresh_verify.dialog_models import ControlSnapshot + value = enriched_snapshot() + if change == 'extra_real_child': + value = replace(value, controls=(*value.controls, ControlSnapshot(888, 0, '', '关闭', + control_type='Button', enabled=False, patterns=('Invoke',)))) + if change == 'enabled_close': value = replace(value, controls=(*value.controls[:-1], replace(value.controls[-1], enabled=True))) + if change == 'titlebar_text': value = replace(value, controls=(*value.controls[:-2], replace(value.controls[-2], name='unexpected'), value.controls[-1])) + if change == 'other_self': value = replace(value, controls=(*value.controls[:4], replace(value.controls[4], handle=999), *value.controls[5:])) + assert exact_prompt(value) is (not change) + + +def test_enriched_prompt_still_reaches_live_native_child_check(monkeypatch): + import win32gui, win32process + from cbb_refresh_verify.post_repair_consent import validate_action_window + from cbb_refresh_verify import native_repair_ui + value = enriched_snapshot() + native = {c.handle: c for c in value.controls[:4]} + monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'CbbVerify_fixture') + monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 20)) + monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda hwnd: True) + monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda hwnd: True) + monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: value.window_class if hwnd == value.hwnd else native[hwnd].class_name) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: value.title if hwnd == value.hwnd else native[hwnd].name) + monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda hwnd: native[hwnd].control_id) + monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, fn, arg: [fn(child, arg) for child in native]) + validate_action_window(value, {'pid':20, 'desktop':'CbbVerify_fixture'}) -- 2.39.5 From d76475a1d762a2dd5fb37e70c9d3fbc124dc9662 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: Tue, 3 Nov 2026 09:54:01 +0800 Subject: [PATCH 26/59] fix: report pending PCB recovery version without obscuring native repair --- src/cbb_refresh_verify/dialog_rules.py | 40 ++++ src/cbb_refresh_verify/icdb_maintenance.py | 6 +- src/cbb_refresh_verify/maintenance_entry.py | 6 + src/cbb_refresh_verify/ui/main_window.py | 2 + src/cbb_refresh_verify/verify_runner.py | 10 +- .../pcb-version-choice-sanitized.json | 183 ++++++++++++++++++ tests/test_maintenance_entry.py | 20 ++ tests/test_pcb_version_choice.py | 51 +++++ 8 files changed, 315 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/pcb-version-choice-sanitized.json create mode 100644 tests/test_pcb_version_choice.py diff --git a/src/cbb_refresh_verify/dialog_rules.py b/src/cbb_refresh_verify/dialog_rules.py index 204bfdb..a92884d 100644 --- a/src/cbb_refresh_verify/dialog_rules.py +++ b/src/cbb_refresh_verify/dialog_rules.py @@ -4,6 +4,7 @@ from __future__ import annotations from dataclasses import dataclass, fields, replace import json +import re from pathlib import Path import sys from typing import Any, Sequence @@ -185,6 +186,39 @@ def _decision(rule: DialogRule) -> RuleDecision: ) +def pcb_version_choice(snapshot): + """Describe the captured native choice; never select or approve a version.""" + if (snapshot.title != 'Database Restoration' or snapshot.window_class != '#32770' + or snapshot.process_name.casefold() != 'expeditionpcb.exe' or snapshot.process_role != 'pcb' + or snapshot.phase != DialogPhase.PCB_STARTING): + return None + explanation = ('The system has detected this design abnormally terminated the last time it was used. ' + 'The user has to decide what database (if any) to associate with the design.') + if not any(c.control_id == 6208 and ' '.join(c.name.split()) == explanation for c in snapshot.controls): + return None + candidates = [] + for identifier, kind, prefix in ((6213, 'last_user_saved', 'Load the last user saved database dated '), + (6206, 'autosave', 'Load the autosave database dated ')): + controls = [c for c in snapshot.controls if c.control_id == identifier and c.class_name == 'Button'] + if len(controls) != 1: return None + control = controls[0] + match = re.fullmatch(re.escape(prefix) + r'(.+)\.', ' '.join(control.name.split())) + if not match: return None + candidates.append({'kind': kind, 'control_id': identifier, 'label': control.name, + 'date': match.group(1), 'enabled': control.enabled, + 'selected': control.selected or control.checked}) + return {'candidates': candidates, 'automatic_selection': False} + + +def archive_version_choice(snapshot, root): + choice = pcb_version_choice(snapshot) + if choice is None: raise ValueError('Not the recognized native PCB version choice') + path = Path(root)/'pcb-version-choice.json' + path.write_text(json.dumps({**choice, 'snapshot': snapshot.to_record()}, + ensure_ascii=False, indent=2), encoding='utf-8') + return path + + def decide(snapshot: WindowSnapshot, rules: Sequence[DialogRule]) -> RuleDecision: # Windows Text Services Framework language-bar window exposed by Mentor's # process is not a Mentor modal: it has no controls and must not abort a run. @@ -197,6 +231,12 @@ def decide(snapshot: WindowSnapshot, rules: Sequence[DialogRule]) -> RuleDecisio if parse_session_loss(combined): return RuleDecision(error_code=ERROR_CODE, detail=' | '.join(snapshot.texts) or snapshot.title) + choice = pcb_version_choice(snapshot) + if choice is not None: + labels = {'last_user_saved': '最后用户保存', 'autosave': '自动保存'} + detail = ';'.join(f'{labels[c["kind"]]}:{c["date"]}' for c in choice['candidates']) + return RuleDecision(error_code='PCB_VERSION_CHOICE_REQUIRED', + detail=f'PCB需选择恢复版本,尚未校验通过。候选版本:{detail}。未自动选择或确认任何版本。') # These prompts require knowledge of database ownership or a recovery # choice. A generic Yes/restore rule cannot establish either fact. if any(marker in combined for marker in ( diff --git a/src/cbb_refresh_verify/icdb_maintenance.py b/src/cbb_refresh_verify/icdb_maintenance.py index 4edac2f..ef22fc2 100644 --- a/src/cbb_refresh_verify/icdb_maintenance.py +++ b/src/cbb_refresh_verify/icdb_maintenance.py @@ -12,10 +12,14 @@ def maintenance_counts(results): and result.safe_to_continue for result in results for attempt in result.recovery_attempts) review = sum(attempt.get('status') == 'RESTORED_REVIEW_REQUIRED' for attempt in attempts) + version_choice = sum(attempt.get('status') == 'PCB_VERSION_CHOICE_REQUIRED' + and attempt.get('native_status') == 'REPAIRED' + and attempt.get('validation_passed') is False for attempt in attempts) return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results), 'skipped': sum(r.status == 'SKIPPED' for r in results), 'repaired': repaired, 'review_required': review, - 'repair_unconfirmed': len(attempts)-repaired-review} + 'version_choice_required': version_choice, + 'repair_unconfirmed': len(attempts)-repaired-review-version_choice} def write_maintenance_report(requests, results, batch_report, skipped_names): diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 802105c..e11703e 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -205,6 +205,12 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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 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': diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index fb35e4e..4bbdfce 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -824,6 +824,7 @@ class MainWindow(QWidget): text = ('非 READY ICDB 检查已结束\n' f'校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' f'原生修复完成 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' + f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}\n' f'完整日志:{report}\n仅失败日志:{failure_report_path(report)}') self.icdb_phase.setText('已停止,存在待处理项目' if failed or skipped else '已完成非 READY 检查') @@ -833,6 +834,7 @@ class MainWindow(QWidget): if self._global_icdb_run: status_text = (f'非 READY 检查结束:通过 {passed},失败/受阻 {failed},未执行 {skipped}。\n' f'原生修复 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' + f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}。') self.status_label.setText(status_text); self.output.append(text) self.summary = QMessageBox(self) diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 17f7d0f..122747e 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -264,6 +264,8 @@ def read_library_flag_with_retry(reader, library, block_name, *, attempts=3, def classify_failure_category(detail): text = str(detail or '') lower = text.casefold() + if 'pcb_version_choice_required' in lower: + return 'PCB_VERSION_CHOICE_REQUIRED' from .icdb_recovery import ERROR_CODE, parse_session_loss if ERROR_CODE.casefold() in lower or parse_session_loss(text): return ERROR_CODE @@ -811,7 +813,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run 'start_gate': str(root/'start-worker.signal')} consent = None if request.post_repair_proof is not None: - from .post_repair_consent import PostRepairProof, ConsentController + from .post_repair_consent import PostRepairProof, ConsentController, exact_prompt if not isinstance(request.post_repair_proof, PostRepairProof): return finish(VerifyResult('BLOCKED', stderr='POST_REPAIR_PROOF_REQUIRED', safe_to_continue=False)) job['post_repair_consent'] = request.post_repair_proof.for_job(job) @@ -938,12 +940,16 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run dialog_result = dialog_supervisor.poll() if (consent is not None and dialog_agent is not None and dialog_result.error_code == 'DATABASE_RECOVERY_REQUIRED' - and dialog_result.snapshot is not None): + and dialog_result.snapshot is not None and exact_prompt(dialog_result.snapshot)): consent_snapshot = dialog_result.snapshot if consent_snapshot.hwnd in {int(h) for h in process.desktop.EnumDesktopWindows()}: dialog_result = consent.handle(consent_snapshot, dialog_result.observed_snapshots, target_pid=target_pid, launcher_pid=launcher_pid, worker_pid=process.pid, companion_pids=tuple(process.companion_pids), perform=dialog_supervisor.backend.perform) + if dialog_result.error_code == 'PCB_VERSION_CHOICE_REQUIRED' and dialog_result.snapshot is not None: + from .dialog_rules import archive_version_choice + choice_path = archive_version_choice(dialog_result.snapshot, root) + dialog_result = replace(dialog_result, detail=dialog_result.detail + f'\n版本候选与原生窗口证据:{choice_path}') # Normal progress stays in memory. Only actions/errors and the # last failure snapshots are persisted, not every elapsed tick. latest_snapshots = dialog_result.observed_snapshots diff --git a/tests/fixtures/pcb-version-choice-sanitized.json b/tests/fixtures/pcb-version-choice-sanitized.json new file mode 100644 index 0000000..7a02f95 --- /dev/null +++ b/tests/fixtures/pcb-version-choice-sanitized.json @@ -0,0 +1,183 @@ +{ + "hwnd": 100, + "pid": 20, + "process_name": "ExpeditionPCB.exe", + "process_role": "pcb", + "window_class": "#32770", + "title": "Database Restoration", + "phase": "PCB_STARTING", + "controls": [ + { + "handle": 101, + "control_id": 65535, + "class_name": "Static", + "name": "", + "automation_id": "65535", + "control_type": "Image", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [] + }, + { + "handle": 102, + "control_id": 6208, + "class_name": "Static", + "name": "The system has detected this design abnormally terminated the last time it was used. The user has to decide what database (if any) to associate with the design.", + "automation_id": "6208", + "control_type": "Text", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [] + }, + { + "handle": 103, + "control_id": 6212, + "class_name": "Button", + "name": "Restore Database", + "automation_id": "6212", + "control_type": "Group", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [] + }, + { + "handle": 104, + "control_id": 6213, + "class_name": "Button", + "name": "Load the last user saved database dated 2026/9/24 23:06.", + "automation_id": "6213", + "control_type": "RadioButton", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [ + "Invoke", + "SelectionItem" + ] + }, + { + "handle": 105, + "control_id": 6206, + "class_name": "Button", + "name": "Load the autosave database dated 2026/9/24 23:06.", + "automation_id": "6206", + "control_type": "RadioButton", + "enabled": true, + "selected": true, + "checked": true, + "patterns": [ + "Invoke", + "SelectionItem" + ] + }, + { + "handle": 106, + "control_id": 6207, + "class_name": "Static", + "name": "This will restore the design to the last autosave state.", + "automation_id": "6207", + "control_type": "Text", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [] + }, + { + "handle": 107, + "control_id": 6209, + "class_name": "Static", + "name": "This will restore the design to the data from the last user save.", + "automation_id": "6209", + "control_type": "Text", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [] + }, + { + "handle": 108, + "control_id": 6210, + "class_name": "Static", + "name": "Autosaved changes will be lost.", + "automation_id": "6210", + "control_type": "Text", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [] + }, + { + "handle": 109, + "control_id": 6211, + "class_name": "Static", + "name": "* Cancelling will load no design.", + "automation_id": "6211", + "control_type": "Text", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [] + }, + { + "handle": 110, + "control_id": 1, + "class_name": "Button", + "name": "OK", + "automation_id": "1", + "control_type": "Button", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [ + "Invoke" + ] + }, + { + "handle": 111, + "control_id": 2, + "class_name": "Button", + "name": "Cancel", + "automation_id": "2", + "control_type": "Button", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [ + "Invoke" + ] + }, + { + "handle": 100, + "control_id": 0, + "class_name": "#32770", + "name": "Database Restoration", + "automation_id": "", + "control_type": "Window", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [ + "Window" + ] + }, + { + "handle": 0, + "control_id": 0, + "class_name": "", + "name": "", + "automation_id": "", + "control_type": "TitleBar", + "enabled": true, + "selected": false, + "checked": false, + "patterns": [ + "Value" + ] + } + ], + "owner_hwnd": 90, + "diagnostics": [] +} \ No newline at end of file diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index f498a9b..35c57e3 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -270,3 +270,23 @@ def test_physical_release_alone_does_not_authorize_queue(case, monkeypatch, stat assert result.safe_to_continue is expected_safe assert result.recovery_attempts[0]['status'] == status assert library_lock.has_blocker(request.lmc_file) is not expected_safe + + +def test_successful_icdb_repair_then_version_choice_reports_partial_completion(case, monkeypatch): + from cbb_refresh_verify import native_repair, verify_runner as runner + from test_native_repair import complete_native_success + request, _, address = case + def repair(target, *a, **k): + address.unlink() + return complete_native_success(target) + monkeypatch.setattr(native_repair, 'run_native_repair', repair) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('FAIL', + error_category='PCB_VERSION_CHOICE_REQUIRED', stderr='PCB版本待选:最后用户保存/自动保存 2026/9/24 23:06', + safe_to_continue=False, evidence_path=request.evidence_dir/'verify/result.json')) + result = runner.run_verify(request) + assert result.status == 'FAIL' and not result.safe_to_continue + assert 'ICDB原生修复已完成' in result.stderr and '尚未校验通过' in result.stderr + assert result.recovery_attempts[0]['native_status'] == 'REPAIRED' + assert result.recovery_attempts[0]['status'] == 'PCB_VERSION_CHOICE_REQUIRED' + assert result.recovery_attempts[0]['validation_passed'] is False diff --git a/tests/test_pcb_version_choice.py b/tests/test_pcb_version_choice.py new file mode 100644 index 0000000..2128aa7 --- /dev/null +++ b/tests/test_pcb_version_choice.py @@ -0,0 +1,51 @@ +from dataclasses import replace +import json +from pathlib import Path + +from cbb_refresh_verify.dialog_models import WindowSnapshot + + +def captured(): + path = Path(__file__).parent/'fixtures/pcb-version-choice-sanitized.json' + return WindowSnapshot.from_record(json.loads(path.read_text(encoding='utf-8'))) + + +def test_actual_version_choice_is_descriptive_fatal_without_action(tmp_path): + from cbb_refresh_verify.dialog_rules import decide, pcb_version_choice, archive_version_choice + from cbb_refresh_verify.post_repair_consent import exact_prompt + value = captured() + decision = decide(value, ()) + assert decision.error_code == 'PCB_VERSION_CHOICE_REQUIRED' and not decision.action and not decision.channels + assert '2026/9/24 23:06' in decision.detail + assert '最后用户保存' in decision.detail and '自动保存' in decision.detail + assert not exact_prompt(value) + choice = pcb_version_choice(value) + assert [c['kind'] for c in choice['candidates']] == ['last_user_saved', 'autosave'] + assert [c['date'] for c in choice['candidates']] == ['2026/9/24 23:06'] * 2 + assert [c['selected'] for c in choice['candidates']] == [False, True] + path = archive_version_choice(value, tmp_path) + record = json.loads(path.read_text(encoding='utf-8')) + assert WindowSnapshot.from_record(record['snapshot']) == value + assert record['candidates'] == choice['candidates'] + assert record['automatic_selection'] is False + + +def test_nonmatching_restoration_remains_blocked_with_original_context(): + from cbb_refresh_verify.dialog_rules import decide, pcb_version_choice + value = replace(captured(), title='Other recovery choice') + decision = decide(value, ()) + assert pcb_version_choice(value) is None + assert decision.error_code == 'DATABASE_RECOVERY_REQUIRED' and not decision.action + assert 'last user saved' in decision.detail + + +def test_version_choice_counts_as_pending_review_not_unknown_native_repair(): + from cbb_refresh_verify.icdb_maintenance import maintenance_counts + from cbb_refresh_verify.verify_runner import VerifyResult, classify_failure_category + result = VerifyResult('FAIL', error_category='PCB_VERSION_CHOICE_REQUIRED', safe_to_continue=False, + recovery_attempts=({'status': 'PCB_VERSION_CHOICE_REQUIRED', 'native_status': 'REPAIRED', + 'validation_passed': False},)) + counts = maintenance_counts([result]) + assert counts['repaired'] == 0 and counts['version_choice_required'] == 1 + assert counts['repair_unconfirmed'] == 0 and counts['passed'] == 0 + assert classify_failure_category('PCB_VERSION_CHOICE_REQUIRED: version pending\nPCB_RELEASE_UNCONFIRMED') == 'PCB_VERSION_CHOICE_REQUIRED' -- 2.39.5 From 2085ec770fb0e84d642fcf11770d30cf10e0bdf6 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: Tue, 3 Nov 2026 09:55:24 +0800 Subject: [PATCH 27/59] docs: explain pending PCB recovery version decision --- docs/releases/2026-11-03-explicit-icdb-maintenance.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/releases/2026-11-03-explicit-icdb-maintenance.md b/docs/releases/2026-11-03-explicit-icdb-maintenance.md index 9b9b7a6..6af832b 100644 --- a/docs/releases/2026-11-03-explicit-icdb-maintenance.md +++ b/docs/releases/2026-11-03-explicit-icdb-maintenance.md @@ -58,3 +58,14 @@ Yes6/No7 按钮。专用代理在自己的私有桌面再次读取实际窗口 映射盘、可移动盘和链接路径不自动确认。该检查不宣称具备跨主机分布式排他能力; 数据库只读句柄在交给官方 PCB 启动前释放,任意非协作外部写入者之间仍不存在 跨进程原子事务保证。普通校验及其他数据库恢复选择继续保持硬性阻断。 + +## PCB 恢复版本待用户决定 + +后续实际副本已完成 iCDB Repair 和一次本地读写确认,但 PCB 又要求在“最后 +用户保存数据库”和“自动保存数据库”之间选择恢复版本。本次两项显示的日期 +均为 `2026/9/24 23:06`;同日期不代表内容相同,工具没有自动选择任何版本。 + +这类结果单独标识为 `PCB_VERSION_CHOICE_REQUIRED`,展示候选日期,并在 +`pcb-version-choice.json` 保存完整窗口和候选状态。维护摘要显示“ICDB 已修复 / +PCB 版本待选”,原生状态仍为 REPAIRED,但整项保持未完成、需人工决定,不能 +计为 PASS/GREEN。工具也不会因为显示了某个默认选中项就认可该版本。 -- 2.39.5 From f8f2dd53aada24d6eda4c76838aebd6fd8fcb953 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: Tue, 3 Nov 2026 10:25:59 +0800 Subject: [PATCH 28/59] docs: record actual maintenance button and user-selected recovery validation --- .../2026-11-03-explicit-icdb-maintenance.md | 55 ++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/releases/2026-11-03-explicit-icdb-maintenance.md b/docs/releases/2026-11-03-explicit-icdb-maintenance.md index 6af832b..79e50c9 100644 --- a/docs/releases/2026-11-03-explicit-icdb-maintenance.md +++ b/docs/releases/2026-11-03-explicit-icdb-maintenance.md @@ -10,7 +10,8 @@ 没有地址阻止时,维护继续沿用原有校验及本轮失联后的自动修复流程。 存在地址记录时,明确维护需要唯一可确认的工程 PRJ/database 映射。共享符号库 -地址、多个地址、外部引用、歧义目标、不完整工程均会阻止执行。工具检查相关 +地址、所选工程内多个地址、外部引用、歧义目标、不完整工程均会阻止执行。 +其他独立 CBB 的 Work 数据库地址不纳入当前修复目标,原文件保持不变。工具检查相关 原生程序和保留工作进程,并同时独占读取库中的原生数据库文件;记录目标工程 引用指纹和地址字节哈希,检查期间及完整备份后重新核对。完整工程备份保留 原地址文件,再由官方 iCDB Project Backup 打开工程。只有其实际 Output 明确 @@ -35,7 +36,7 @@ Repair、原生退出、地址消失和最终校验仍是不同的判据。只 协调端在保留旧地址字节的用户工程副本上观察到:官方工具报告数据库曾在服务器 运行时被复制、工程当前未使用,随后官方 Repair 自行清除副本地址并返回成功。 这证明了明确维护入口的必要性;原始用户库没有由本次开发或测试修改。实际按钮、 -冻结包及最终全套回归由协调端在提交后的隔离副本继续验收。 +冻结包及最终全套回归的实际验收结果见下。 ## 修复后的本地读写访问提示 @@ -69,3 +70,53 @@ Yes6/No7 按钮。专用代理在自己的私有桌面再次读取实际窗口 `pcb-version-choice.json` 保存完整窗口和候选状态。维护摘要显示“ICDB 已修复 / PCB 版本待选”,原生状态仍为 REPAIRED,但整项保持未完成、需人工决定,不能 计为 PASS/GREEN。工具也不会因为显示了某个默认选中项就认可该版本。 + +## 最终验证(2026-11-03) + +程序源码提交 `d76475a`:全量 `python -m pytest -q` **969 passed,1 条已有 +pywinauto STA 模式警告,107.69 秒**;编译、diff 检查和代码复审通过。 +由不可变 `git archive d76475a` 构建的 EXE 通过 GUI 冒烟。 + +在第二份完整隔离库上,通过打包 EXE 的实际“非 READY ICDB 检查与修复”按钮 +确认并运行 IP101GR:有效旧阻止记录接续、旧地址完整备份、官方 Repair 成功并 +正常释放、一次受约束的读写确认均实际执行。之后准确停在原生 PCB 版本选择, +显示 `version_choice_required=1`、`repair_unconfirmed=0`、`repaired=0`。 +这是维护入口和结果分类通过验收,不代表未经用户选版本就达到 READY。 + +原始证据: + +- 用户最初 0.172 秒失败:`diagnostics/issue2-user-maintenance-20261103/user-reports/`。 +- 冻结 EXE 实际按钮:`diagnostics/issue2-user-maintenance-20261103/actual-button-frozen-d76475a/outcome.json`。 +- 原生版本选项:上述目录的 `data/evidence/verify-8fa461b6d6fe/pcb-version-choice.json`。 +- 全量测试:`build/issue2-maintenance-final-tests-d76475a.log`。 + +### 用户明确选择最后手动保存版本后的副本验证 + +用户选择“用最后手动保存版本继续验证”,授权范围仅为隔离副本。诊断续跑使用 +真实校验器、原生工作进程、对话框代理及权威状态读取;只为这次用户决定提供 +版本动作:先点击 6213,重新观察其已选中且 6206 未选中,再确认 OK 1。 +选择前保留完整工程副本;没有改变程序的默认版本选择规则。 + +工程成功打开并核对身份,但在原长路径下两次 Project Integration 均产生本轮 +JobPrefsDB.jpf / local Cell Library 加载错误。原文件、临时目录及备份中的 JPF/CEL +可读且哈希一致,日志前缀与时间证明是本轮错误,不能断言二进制数据已损坏。 + +随后保持同一工程数据,通过临时本地 `T:` 短路径进行对照,完成原生 Integration、 +保存关闭、进程释放及权威状态查询,结果为 **PASS / GREEN / READY +(IsVerifiedReuseBlock, raw=1),safe_to_continue=true**。所有原生进程退出后已移除 +该临时盘符。此结果说明短路径试验可通过,尚不能证明确切路径长度限制或永久 +解决原长路径下的 JobPrefs/Cell Library 兼容问题;产品没有自动增加短路径映射。 + +这是**用户指定历史版本后的恢复与验证**,不是无人值守或无损自动修复验收。 +验证后的完整库副本保留在 `diagnostics/issue2-user-maintenance-20261103/frozen-library/`, +原用户库没有被替换。结果和选择证据分别位于: + +- `user-authorized-last-saved-r2/user-selection-events.jsonl`(实际 6213 → 复读 → OK 1)。 +- `user-authorized-last-saved-short-path/result.json`(最终 PASS/GREEN)。 +- `short-path-trial.json`(临时盘符及正常退出后清理记录)。 +- `source-unchanged-after-user-choice.json`(全部试验后原库 21,013 个主要文件清单仍一致,原地址字节哈希未变)。 + +以上相对路径均位于 `diagnostics/issue2-user-maintenance-20261103/`。 +现场原工单其他工程、网络共享读写确认和 Windows 10 仍不能由此验收代替。 + +交付候选包为 `dist/issue2-maintenance-20261103-d76475a.zip`。不合并发布、不自动关闭工单。 -- 2.39.5 From 07a9c1eaafe040b4a6eb657f86678fdda965b041 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: Tue, 3 Nov 2026 11:11:36 +0800 Subject: [PATCH 29/59] feat: integrate shared native Add worker behind identity gate --- .../task-5-native-add-report.md | 7 ++ src/cbb_refresh_verify/management.py | 2 + src/cbb_refresh_verify/native_management.py | 98 +++++++++++++++--- tests/test_native_management.py | 99 +++++++++++++++++++ 4 files changed, 193 insertions(+), 13 deletions(-) create mode 100644 .superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md diff --git a/.superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md b/.superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md new file mode 100644 index 0000000..e8a144b --- /dev/null +++ b/.superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md @@ -0,0 +1,7 @@ +# Task 5: shared native Add route + +The adapter now routes Add on an existing owned library through the same private-desktop UIA executor used by the isolated probe. Worker startup checks the pinned route and staged source identity; the executor retains bounded field readback, exact native success message, child-first close, then closes/reopens the library for strict catalog readback. Exact name/partition absence is a native failure; registered content with different raw engineering identity is native success with `identity_matches=False` and an evidence directory. + +`route_capabilities()['add']` reports binary-pinned route readiness independently of `capabilities()`. Production Add remains disabled with the explicit engineering-identity reason. Update remains disabled and the management engine still composes remove plus Add. Probe CreateLibrary and readd token/fixture guards remain unchanged. No native tools, Mentor processes, or builds were launched in this task. + +Verification: three new boundary tests failed for the missing route method before implementation, then passed. The focused native/management/source/probe-guard/UI suite passed 86 tests. Two additional mocked COM tests passed for close/reopen ordering and strict name/partition versus identity mismatch. Source bytes remained unchanged at the adapter boundary. `git diff --check` passed. Full suite and serialized native validation remain with the coordinator. diff --git a/src/cbb_refresh_verify/management.py b/src/cbb_refresh_verify/management.py index de85280..e5ccee9 100644 --- a/src/cbb_refresh_verify/management.py +++ b/src/cbb_refresh_verify/management.py @@ -51,6 +51,8 @@ class NativeOutcome: success: bool safe_to_continue: bool detail: str + identity_matches: bool | None = None + evidence_path: Path | None = None @dataclass(frozen=True) diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 5bb908f..017fc5f 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -39,7 +39,7 @@ def removal_input(name, partition): def removal_output_ok(code, output, name, partition): return (code == 0 and '# Warning:' not in output and '# Error:' not in output - and re.search(r"# Note: Removed [^\r\n]+ '" + re.escape(name) + and re.search(r"# Note: Removed [^\n]+ '" + re.escape(name) + r"' from partition '" + re.escape(partition) + r"'\.", output) is not None) @@ -63,7 +63,7 @@ class NativeManagementAdapter: self.mapped_drive = drive or '' self.mapped_remote_root = remote_root or '' - def capabilities(self): + def _binary_capability(self): reason = '' for relative, expected in _BINARY_PROOF.items(): path = self.home/relative if self.home else Path(relative) @@ -74,9 +74,22 @@ class NativeManagementAdapter: except OSError as error: reason = f'无法核对原生程序 {path}: {error}' break + return reason + + def route_capabilities(self): + """Report the proven native transport separately from production gates.""" + reason = self._binary_capability() + return { + 'remove': Capability(not reason, reason or 'CleanWGLib/F4 route ready.'), + 'add': Capability(not reason, reason or 'Library Manager Logical-Physical Add route ready.'), + 'update': Capability(False, 'Update remains remove plus Add while identity readback is gated.'), + } + + def capabilities(self): + reason = self._binary_capability() return { 'remove': Capability(not reason, reason or '已验证 CleanWGLib/F4;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'), - 'add': Capability(False, '原生新增尚未完成隔离注册及工程内容复读验证;请先运行独立诊断。'), + 'add': Capability(False, '原生新增已调用并注册,但工程身份复读尚未通过;生产新增保持禁用。'), 'update': Capability(False, '同名更新需要原生移除、重新导入及完整端到端隔离验证,目前不可用。'), } @@ -161,7 +174,41 @@ class NativeManagementAdapter: raise ValueError('仅验证了 DxDesigner / Logical-Physical 管理操作。') def add(self, request, evidence): - return NativeOutcome(False, True, self.capabilities()['add'].reason) + evidence = Path(evidence) + validate_name(request.name) + if request.partition != 'Logical-Physical': + return NativeOutcome(False, True, 'Only the verified Logical-Physical native Add route is supported.', + evidence_path=evidence) + source = Path(request.source) if request.source is not None else None + if source is None or not source.is_file(): + return NativeOutcome(False, True, 'Native Add requires an existing staged PRJ source project.', + evidence_path=evidence) + try: + source_identity = engineering_identity(source) + except (ValueError, OSError) as error: + return NativeOutcome(False, True, f'Unable to read source engineering identity: {error}', + evidence_path=evidence) + route = self.route_capabilities()['add'] + if not route.supported: + return NativeOutcome(False, True, route.reason, evidence_path=evidence) + result = self._worker({ + 'operation': 'add', + 'library': str(absolute_preserving_drive(request.library)), + 'source': str(source), + 'name': request.name, + 'partition': request.partition, + 'top_level_block': request.top_level_block, + 'mapped_drive': request.mapped_drive or self.mapped_drive, + 'mapped_remote_root': request.mapped_remote_root or self.mapped_remote_root, + 'timeout_seconds': request.timeout_seconds, + 'source_identity': source_identity, + }, evidence) + identity_matches = result.get('identity_matches') + detail = result.get('detail', '') + if result.get('success') and identity_matches is False and not detail: + detail = 'Native Add registered the block, but engineering identity readback differs from the staged source.' + return NativeOutcome(bool(result.get('success')), bool(result.get('safe_to_continue')), + detail, identity_matches=identity_matches, evidence_path=evidence) def prepare_probe_library(job, root): @@ -292,11 +339,7 @@ def _probe_add_dialog(app, job, desktop, owned_pids, root, *, library=None): def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None): - """Diagnostic only: proven editor/UIA tab and resource1103 controls. - - This function has no production capability override. It is called only - after this worker itself creates a new disposable library. - """ + """Submit the proven Logical-Physical Add dialog on an owned library.""" import pythoncom, win32con, win32gui, win32process _dispatch_diagnostics(root, 'before-pywinauto-import', app, library) from pywinauto import Desktop @@ -345,7 +388,8 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) raise RuntimeError('原生 New 按钮不可用。') win32gui.PostMessage(new_button, win32con.BM_CLICK, 0, 0) dialog = await_window('New Reusable Block') - fields = {1217: str(source), 1215: job['name'], 1218: top, 1216: 'Isolated CBB management validation'} + fields = {1217: str(source), 1215: job['name'], 1218: top, + 1216: job.get('description', 'CBB native management validation')} for control, value in fields.items(): hwnd = win32gui.GetDlgItem(dialog, control) if not hwnd or win32gui.GetClassName(hwnd) != 'Edit': @@ -538,7 +582,7 @@ def _wait_native_exit(pids, timeout=10): def _catalog_native(job, root): import gc, pythoncom from .com_runtime import initialize_com, uninitialize_com - if job.get('operation') in ('probe_create_add', 'probe_readd'): + if job.get('operation') in ('add', 'probe_create_add', 'probe_readd'): # The installed UIA import disconnects existing Mentor proxies # (CO_E_OBJNOTCONNECTED). Finish its initialization before creating any. from pywinauto import Desktop @@ -614,8 +658,16 @@ def _catalog_com(job, root): raise RuntimeError('目录读取需要升级中心库或选择流程;已拒绝,目标未获准自动升级。') if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve(): raise RuntimeError('原生返回中心库身份不一致。') - if job['operation'] in ('probe_create_add', 'probe_readd'): + if int(library.FlowType) != 1: + raise RuntimeError('Native Add requires the DxDesigner Logical-Physical library flow.') + if job['operation'] in ('add', 'probe_create_add', 'probe_readd'): _probe_add_dialog(app, job, desktop, owned_pids, root, library=library) + library.Close() + library = None + library = app.OpenLibrary(str(absolute_preserving_drive(job['library']))) + if (library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve() + or int(library.FlowType) != 1): + raise RuntimeError('Native Add reopen did not confirm the exact library and flow.') entries = [] parts = library.Partitions(7) for i in range(1, int(parts.Count) + 1): @@ -637,6 +689,17 @@ def _catalog_com(job, root): partition = None parts = None result.update(success=True, library=str(library.FullName), flow=int(library.FlowType), entries=entries) + if job['operation'] == 'add': + matches = [entry for entry in entries + if entry['name'] == job['name'] and entry['partition'] == job['partition']] + identity_matches = (len(matches) == 1 + and matches[0]['engineering_identity'] == job.get('source_identity')) + result['identity_matches'] = identity_matches + if len(matches) != 1: + result.update(success=False, detail='Native Add reopen did not confirm the exact name and partition.') + elif not identity_matches: + result['detail'] = ('Native Add registered the block, but engineering identity readback ' + 'differs from the staged source.') except Exception as error: result['detail'] = str(error) result['exception'] = _exception_details(error) @@ -675,7 +738,16 @@ def management_worker_main(request_path): if job.get('mapped_drive') and job.get('mapped_remote_root'): from .network_path import ensure_mapped_drive_visible ensure_mapped_drive_visible(job['mapped_drive'], job['mapped_remote_root']) - if job['operation'] in ('catalog', 'probe_create_add', 'probe_readd'): + if job['operation'] in ('catalog', 'add', 'probe_create_add', 'probe_readd'): + if job['operation'] == 'add': + route = NativeManagementAdapter(mentor_home=job['mentor_home']).route_capabilities()['add'] + if not route.supported: + raise RuntimeError(route.reason) + validate_name(job['name']) + if job.get('partition') != 'Logical-Physical': + raise ValueError('Native Add requires Logical-Physical.') + if engineering_identity(job['source']) != job.get('source_identity'): + raise ValueError('Staged source engineering identity changed before native Add.') result = _catalog_native(job, root) elif job['operation'] == 'remove': adapter = NativeManagementAdapter(mentor_home=job['mentor_home']) diff --git a/tests/test_native_management.py b/tests/test_native_management.py index 0597702..add05ba 100644 --- a/tests/test_native_management.py +++ b/tests/test_native_management.py @@ -13,6 +13,66 @@ def test_installed_binary_mismatch_disables_removal(tmp_path): assert not caps['add'].supported and not caps['update'].supported +def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_path, monkeypatch): + import hashlib + import cbb_refresh_verify.native_management as native + proof = {'lm/win32/bin/CleanWGLib.exe': hashlib.sha256(b'verified').hexdigest()} + monkeypatch.setattr(native, '_BINARY_PROOF', proof) + path = tmp_path/next(iter(proof)); path.parent.mkdir(parents=True); path.write_bytes(b'verified') + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + assert adapter.route_capabilities()['add'].supported + assert not adapter.capabilities()['add'].supported + assert '身份' in adapter.capabilities()['add'].reason + + +def test_native_add_passes_existing_library_and_staged_source_to_worker(tmp_path, monkeypatch): + from cbb_refresh_verify.management import ManagementRequest + import cbb_refresh_verify.native_management as native + source = tmp_path/'source.prj'; source.write_bytes(b'source') + library = tmp_path/'library.lmc'; library.write_bytes(b'library') + seen = {} + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + monkeypatch.setattr(adapter, 'route_capabilities', lambda: { + 'add': native.Capability(True, 'route ready')}) + monkeypatch.setattr(native, 'engineering_identity', lambda path: {'source': str(path)}) + def worker(job, evidence): + seen.update(job=job, evidence=evidence) + return {'success': True, 'safe_to_continue': True, 'identity_matches': True, + 'detail': 'native add accepted'} + monkeypatch.setattr(adapter, '_worker', worker) + request = ManagementRequest('add', library, 'Block', source=source, + mapped_drive='S:', mapped_remote_root=r'\\server\share', timeout_seconds=23) + result = adapter.add(request, tmp_path/'evidence') + assert result.success and result.safe_to_continue and result.identity_matches is True + assert seen['job'] == { + 'operation': 'add', 'library': str(library), 'source': str(source), + 'name': 'Block', 'partition': 'Logical-Physical', 'top_level_block': '', + 'mapped_drive': 'S:', 'mapped_remote_root': r'\\server\share', + 'timeout_seconds': 23, 'source_identity': {'source': str(source)}, + } + + +def test_native_add_identity_mismatch_is_native_success_with_evidence(tmp_path, monkeypatch): + from cbb_refresh_verify.management import ManagementRequest + import cbb_refresh_verify.native_management as native + source = tmp_path/'source.prj'; source.write_bytes(b'source') + library = tmp_path/'library.lmc'; library.write_bytes(b'library') + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + monkeypatch.setattr(adapter, 'route_capabilities', lambda: { + 'add': native.Capability(True, 'route ready')}) + monkeypatch.setattr(native, 'engineering_identity', lambda path: {'source': str(path)}) + monkeypatch.setattr(adapter, '_worker', lambda job, evidence: { + 'success': True, 'safe_to_continue': True, 'identity_matches': False, + 'detail': 'engineering identity readback mismatch', + }) + original = source.read_bytes() + result = adapter.add(ManagementRequest('add', library, 'Block', source=source), tmp_path/'evidence') + assert result.success and result.identity_matches is False + assert 'identity' in result.detail + assert result.evidence_path == tmp_path/'evidence' + assert source.read_bytes() == original + + @pytest.mark.parametrize('name', ['A\tB', 'A\nB', '../A', '中文']) def test_delete_record_rejects_ambiguous_encoding_or_injection(name): from cbb_refresh_verify.native_management import removal_input @@ -176,3 +236,42 @@ def test_com_failure_without_owner_identity_cannot_claim_release(tmp_path, monke monkeypatch.setattr(native, '_catalog_com', lambda *a: {'success': False, 'safe_to_continue': False, 'native_pids': [], 'detail': 'COM startup ambiguous'}) assert native._catalog_native({}, tmp_path)['safe_to_continue'] is False + + +@pytest.mark.parametrize('registered', [True, False]) +def test_shared_add_reopens_exact_catalog_and_reports_identity_mismatch(tmp_path, monkeypatch, registered): + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.mentor_adapter as mentor + import win32com.client + library_path = tmp_path/'lib.lmc'; library_path.touch() + events = [] + class Collection: + def __init__(self, items): self.items = items; self.Count = len(items) + def Item(self, index): return self.items[index - 1] + class Library: + FullName = str(library_path) + FlowType = 1 + def Partitions(self, kind): + events.append('catalog') + return Collection([Collection([SimpleNamespace(Name='Block', PartitionName='Logical-Physical')] + if registered else [])]) + def Close(self): events.append('close') + state = {'running': False} + class App: + def OpenLibrary(self, path): + assert path == str(library_path) + events.append('open') + return Library() + def Quit(self): state['running'] = False + def dispatch(_): state['running'] = True; return App() + monkeypatch.setattr(win32com.client, 'DispatchEx', dispatch) + monkeypatch.setattr(native, '_current_desktop', lambda: (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test')) + monkeypatch.setattr(mentor, '_process_details', lambda _: [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if state['running'] else []) + monkeypatch.setattr(native, '_probe_add_dialog', lambda *a, **k: events.append('add')) + result = native._catalog_com({'operation': 'add', 'library': str(library_path), + 'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path), + 'name': 'Block', 'partition': 'Logical-Physical', 'source_identity': {'expected': 'bytes'}}, tmp_path) + assert events == ['open', 'add', 'close', 'open', 'catalog', 'close'] + assert result['success'] is registered + assert result['identity_matches'] is False + assert ('engineering identity' if registered else 'exact name and partition') in result['detail'] -- 2.39.5 From b97556dcc110421f733adf92fc8314e179c1c05b 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: Tue, 3 Nov 2026 11:22:27 +0800 Subject: [PATCH 30/59] fix: require owned fixture authority for native Add research --- .../task-5-native-add-report.md | 7 -- docs/releases/2026-11-03-native-crud.md | 11 ++ src/cbb_refresh_verify/native_management.py | 115 +++++++++++++++++- tests/test_native_management.py | 93 ++++++++++++-- 4 files changed, 208 insertions(+), 18 deletions(-) delete mode 100644 .superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md create mode 100644 docs/releases/2026-11-03-native-crud.md diff --git a/.superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md b/.superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md deleted file mode 100644 index e8a144b..0000000 --- a/.superpowers/sdd/2026-11-02-issue-2-management/task-5-native-add-report.md +++ /dev/null @@ -1,7 +0,0 @@ -# Task 5: shared native Add route - -The adapter now routes Add on an existing owned library through the same private-desktop UIA executor used by the isolated probe. Worker startup checks the pinned route and staged source identity; the executor retains bounded field readback, exact native success message, child-first close, then closes/reopens the library for strict catalog readback. Exact name/partition absence is a native failure; registered content with different raw engineering identity is native success with `identity_matches=False` and an evidence directory. - -`route_capabilities()['add']` reports binary-pinned route readiness independently of `capabilities()`. Production Add remains disabled with the explicit engineering-identity reason. Update remains disabled and the management engine still composes remove plus Add. Probe CreateLibrary and readd token/fixture guards remain unchanged. No native tools, Mentor processes, or builds were launched in this task. - -Verification: three new boundary tests failed for the missing route method before implementation, then passed. The focused native/management/source/probe-guard/UI suite passed 86 tests. Two additional mocked COM tests passed for close/reopen ordering and strict name/partition versus identity mismatch. Source bytes remained unchanged at the adapter boundary. `git diff --check` passed. Full suite and serialized native validation remain with the coordinator. diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md new file mode 100644 index 0000000..7de651f --- /dev/null +++ b/docs/releases/2026-11-03-native-crud.md @@ -0,0 +1,11 @@ +# Native CRUD integration and review follow-up + +The shared native Add transport uses the proven private-desktop UIA executor, bounded field readback and exact native success message. It closes/reopens the library before comparing exact catalog name/partition and raw engineering identity. Native registration with different engineering bytes returns `success=True`, `identity_matches=False`; missing registration is a native failure. Production Add and Update remain disabled pending independent engineering identity proof. + +The review of 07a9c1e identified that route readiness alone did not authorize existing-library mutation. The follow-up makes public `add()` refuse before reading a source or launching a worker. Research callers must use `add_owned_fixture(..., probe_token=...)` against the fixed library created by the diagnostic workflow and matching probe-owner record. It requires the calling thread's live library lease, checks its lease ID against the checkpoint, owns the management mutex during submission, creates a fresh staged source copy and verified library snapshot, and binds the transaction nonce to target, name, partition, source identity and manifests. + +Before native activation the worker revalidates that evidence, exact current lease and live supervisor. It duplicates the supervisor's still-open exclusive sidecar handle and checks its final path, so JSON metadata alone cannot establish ownership. Source alignment is confined to the newly copied worker source. Existing probe CreateLibrary/readd guards remain intact. Removal success matching again excludes both carriage returns and newlines. + +Verification is mocked/file-only: the original focused suite passed 86 tests plus two close/reopen checks. Follow-up tests reject public bypass, unprepared activation, missing current-thread ownership, invalid nonce/token/lease, substituted original source, changed backup and expired supervisor, and exercise a valid leased fixture with unchanged original source bytes. No Mentor processes, builds or full suite were launched by this implementation agent; serialized native evidence and semantic-identity research remain coordinator-owned. + +The follow-up focused suite passed 99 tests. After restoring ordinary catalog queries for non-DxDesigner flow readback, the native boundary/field subset passed 47 tests (including four additional flow cases). `git diff --check` passed. The original ignored scratch report remains on disk but is removed from version control. diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 017fc5f..5064810 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -39,7 +39,7 @@ def removal_input(name, partition): def removal_output_ok(code, output, name, partition): return (code == 0 and '# Warning:' not in output and '# Error:' not in output - and re.search(r"# Note: Removed [^\n]+ '" + re.escape(name) + and re.search(r"# Note: Removed [^\r\n]+ '" + re.escape(name) + r"' from partition '" + re.escape(partition) + r"'\.", output) is not None) @@ -102,6 +102,20 @@ class NativeManagementAdapter: 'mapped_drive': job.get('mapped_drive', self.mapped_drive), 'mapped_remote_root': job.get('mapped_remote_root', self.mapped_remote_root), 'timeout_seconds': timeout, 'supervisor_pid': os.getpid()} + if job['operation'] == 'add': + import win32con, win32event + mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') + acquired = False + try: + acquired = win32event.WaitForSingleObject(mutex, 0) in ( + win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) + if not acquired: + raise RuntimeError('Another CBB operation owns the management mutex.') + job = prepare_owned_fixture_add(job, evidence) + return self._launch_worker(job, evidence) + finally: + if acquired: win32event.ReleaseMutex(mutex) + mutex.Close() if job['operation'] == 'probe_create_add': # Prepare the fixed NEW fixture before a native owner can start. # The parent owns its sidecar for the entire create/add invocation. @@ -174,6 +188,12 @@ class NativeManagementAdapter: raise ValueError('仅验证了 DxDesigner / Logical-Physical 管理操作。') def add(self, request, evidence): + # Route readiness never grants production mutation authority. + return NativeOutcome(False, True, self.capabilities()['add'].reason, + evidence_path=Path(evidence)) + + def add_owned_fixture(self, request, evidence, *, probe_token): + """Research only: stage and back up a token-bound, actively leased fixture.""" evidence = Path(evidence) validate_name(request.name) if request.partition != 'Logical-Physical': @@ -202,6 +222,7 @@ class NativeManagementAdapter: 'mapped_remote_root': request.mapped_remote_root or self.mapped_remote_root, 'timeout_seconds': request.timeout_seconds, 'source_identity': source_identity, + 'probe_token': probe_token, }, evidence) identity_matches = result.get('identity_matches') detail = result.get('detail', '') @@ -211,6 +232,89 @@ class NativeManagementAdapter: detail, identity_matches=identity_matches, evidence_path=evidence) +def prepare_owned_fixture_add(job, root): + """Create fresh transaction evidence while this thread owns the fixture.""" + from .library_lock import LibraryLock, owned_snapshot, checkpoint_record, blocker_path, _guard, _owners + from .scoped_manifest import copy_verified, ManifestScope + library = validate_probe_readd(job, root) + lease = LibraryLock(library) + if owned_snapshot(lease.path) is None: + raise ValueError('Owned fixture Add requires the current thread library lease.') + guard = checkpoint_record(blocker_path(library)) + with _guard: + owner = _owners.get(lease.key) + if (guard is None or guard['pid'] != os.getpid() + or guard['status'] != 'OPERATION_IN_PROGRESS' or owner is None + or guard['lease_id'] != owner['lease_id']): + raise ValueError('Owned fixture lease is not active.') + lease_handle = int(owner['handle']) + source = Path(job['source']).resolve(strict=True) + if (source.parent.is_relative_to(root.resolve()) or root.resolve().is_relative_to(source.parent) + or source.parent.is_relative_to(library.parent.resolve())): + raise ValueError('Research source must be independent of fixture and transaction evidence.') + source_manifest = copy_verified(source.parent, root/'source') + backup = copy_verified(library.parent, root/'library-before', + scope=ManifestScope.library(job['name'], library.name)) + nonce = uuid4().hex + prepared = {'nonce': nonce, 'library': str(library), 'lease_id': guard['lease_id'], + 'supervisor_pid': os.getpid(), 'source_name': source.name, + 'source_manifest': source_manifest, 'backup_manifest': backup, + 'name': job['name'], 'partition': job['partition'], + 'source_identity': job['source_identity'], + 'probe_token': job['probe_token'], 'lease_handle': lease_handle} + _write_json(root/'add-prepared.json', prepared) + return {**job, 'source': str(root/'source'/source.name), 'transaction_nonce': nonce} + + +def validate_owned_fixture_add(job, root): + """Reject unprepared, stale or foreign Add jobs before any native activation.""" + from .library_lock import checkpoint_record, blocker_path, _supervisor_alive + from .scoped_manifest import build_manifest, ManifestScope + library = validate_probe_readd(job, root) + prepared = json.loads((root/'add-prepared.json').read_text(encoding='utf-8')) + guard = checkpoint_record(blocker_path(library)) + source = root/'source'/prepared['source_name'] + valid = (bool(job.get('transaction_nonce')) + and prepared['nonce'] == job['transaction_nonce'] + and prepared['probe_token'] == job['probe_token'] + and prepared['name'] == job['name'] and prepared['partition'] == job['partition'] + and prepared['source_identity'] == job['source_identity'] + and Path(prepared['library']).resolve() == library.resolve() + and prepared['supervisor_pid'] == job['supervisor_pid'] + and guard is not None and guard['pid'] == job['supervisor_pid'] + and guard['lease_id'] == prepared['lease_id'] + and guard['status'] == 'OPERATION_IN_PROGRESS' + and Path(guard['library']).resolve() == library.resolve() + and _supervisor_alive(job['supervisor_pid']) + and Path(job['source']).resolve() == source.resolve() + and source.resolve().parent == (root/'source').resolve() + and build_manifest(root/'source') == prepared['source_manifest'] + and build_manifest(root/'library-before', scope=ManifestScope.library(job['name'], library.name)) + == prepared['backup_manifest']) + if not valid: + raise ValueError('Owned fixture Add authority, staging or backup proof is invalid.') + # Metadata alone does not prove ownership. Duplicate the supervisor's + # still-open exclusive sidecar handle and verify its exact final path. + import win32api, win32con, win32file + process = win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE, False, job['supervisor_pid']) + duplicate = None + try: + duplicate = win32api.DuplicateHandle(process, prepared['lease_handle'], + win32api.GetCurrentProcess(), 0, False, win32con.DUPLICATE_SAME_ACCESS) + final_path = win32file.GetFinalPathNameByHandle(duplicate, 0) + if final_path.startswith('\\\\?\\UNC\\'): + final_path = '\\\\' + final_path[8:] + elif final_path.startswith('\\\\?\\'): + final_path = final_path[4:] + expected_lock = library.with_name('.' + library.stem + '.cbb-upload.lock') + if Path(final_path).resolve() != expected_lock.resolve(): + raise ValueError('Supervisor lease handle belongs to a different library.') + finally: + if duplicate is not None: duplicate.Close() + process.Close() + return source + + def prepare_probe_library(job, root): """Validate a NEW fixed fixture already guarded by its supervisor.""" from .library_lock import blocker_path, checkpoint_record @@ -348,7 +452,8 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) from .management import source_project source, pcb, top = source_project(job['source'], job.get('top_level_block', '')) source_folder = 'source-replacement' if job['operation'] == 'probe_readd' else 'source' - if not source.is_relative_to(Path(root).parent.resolve()/source_folder): + source_root = Path(root).resolve()/'source' if job['operation'] == 'add' else Path(root).parent.resolve()/source_folder + if not source.is_relative_to(source_root): raise ValueError('隔离新增只允许使用本次复制的来源工程。') _align_library(source, pcb, Path(job['library'])) _dispatch_diagnostics(root, 'after-source-alignment', app, library, @@ -601,6 +706,8 @@ def _catalog_native(job, root): def _catalog_com(job, root): import pythoncom, win32com.client, win32con, win32gui, win32process from .mentor_adapter import _process_details + if job['operation'] == 'add': + validate_owned_fixture_add(job, root) desktop, name = _current_desktop() if name != job['desktop'] or not name.startswith('CbbManagement_'): raise RuntimeError('拒绝在非本次私有桌面读取原生目录。') @@ -658,9 +765,9 @@ def _catalog_com(job, root): raise RuntimeError('目录读取需要升级中心库或选择流程;已拒绝,目标未获准自动升级。') if library is None or Path(str(library.FullName)).resolve() != Path(job['library']).resolve(): raise RuntimeError('原生返回中心库身份不一致。') - if int(library.FlowType) != 1: - raise RuntimeError('Native Add requires the DxDesigner Logical-Physical library flow.') if job['operation'] in ('add', 'probe_create_add', 'probe_readd'): + if int(library.FlowType) != 1: + raise RuntimeError('Native Add requires the DxDesigner Logical-Physical library flow.') _probe_add_dialog(app, job, desktop, owned_pids, root, library=library) library.Close() library = None diff --git a/tests/test_native_management.py b/tests/test_native_management.py index add05ba..e2193d6 100644 --- a/tests/test_native_management.py +++ b/tests/test_native_management.py @@ -13,6 +13,82 @@ def test_installed_binary_mismatch_disables_removal(tmp_path): assert not caps['add'].supported and not caps['update'].supported +def test_public_add_cannot_bypass_disabled_capability(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.management import ManagementRequest + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + monkeypatch.setattr(adapter, '_worker', lambda *a: pytest.fail('public disabled Add launched worker')) + monkeypatch.setattr(native, 'engineering_identity', lambda *a: pytest.fail('public disabled Add accessed source')) + result = adapter.add(ManagementRequest('add', tmp_path/'arbitrary.lmc', 'Block', + source=tmp_path/'source/original.prj'), tmp_path/'add') + assert not result.success and result.safe_to_continue + + +def test_unprepared_add_rejected_before_native_activation(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + import win32com.client + monkeypatch.setattr(win32com.client, 'DispatchEx', lambda *a: pytest.fail('unprepared native activation')) + with pytest.raises(ValueError): + native._catalog_com({'operation': 'add', 'library': str(tmp_path/'arbitrary.lmc')}, tmp_path/'add') + + +def test_removed_marker_cannot_cross_carriage_return_record(): + from cbb_refresh_verify.native_management import removal_output_ok + assert not removal_output_ok(0, "# Note: Removed unrelated\rrecord 'Block' from partition 'Logical-Physical'.", + 'Block', 'Logical-Physical') + + +@pytest.mark.parametrize('invalid', ['', 'nonce', 'lease', 'source', 'backup', 'expired', 'token']) +def test_fixture_add_authority_requires_live_lease_and_fresh_staging(tmp_path, monkeypatch, invalid): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.management import ManagementRequest + from cbb_refresh_verify.library_lock import LibraryLock + library = tmp_path/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.write_bytes(b'owned fixture') + source = tmp_path/'original/Source.prj' + source.parent.mkdir(); source.write_bytes(b'original source') + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8') + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + monkeypatch.setattr(adapter, 'route_capabilities', lambda: {'add': native.Capability(True, 'ready')}) + monkeypatch.setattr(native, 'engineering_identity', lambda _: {'source': 'bytes'}) + def launch(job, root): + staged = Path(job['source']) + assert staged == root/'source/Source.prj' and staged != source + prepared_path = root/'add-prepared.json' + prepared = json.loads(prepared_path.read_text(encoding='utf-8')) + if invalid == 'nonce': job['transaction_nonce'] = 'foreign' + if invalid == 'token': job['probe_token'] = 'foreign' + if invalid == 'lease': prepared['lease_id'] = 'foreign' + if invalid == 'source': job['source'] = str(source) + if invalid == 'backup': (root/'library-before/mentor_lib.lmc').write_bytes(b'changed') + if invalid == 'expired': + import cbb_refresh_verify.library_lock as locks + monkeypatch.setattr(locks, '_supervisor_alive', lambda _: False) + prepared_path.write_text(json.dumps(prepared), encoding='utf-8') + if invalid: + with pytest.raises(ValueError): native.validate_owned_fixture_add(job, root) + else: + assert native.validate_owned_fixture_add(job, root) == staged + assert source.read_bytes() == b'original source' + return {'success': not invalid, 'safe_to_continue': True, 'identity_matches': not invalid} + monkeypatch.setattr(adapter, '_launch_worker', launch) + with LibraryLock(library): + result = adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source), + tmp_path/'shared-add', probe_token='owned') + assert result.success is (not invalid) + + +def test_fixture_metadata_without_current_thread_lease_cannot_stage_source(tmp_path): + import cbb_refresh_verify.native_management as native + library = tmp_path/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.touch() + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8') + root = tmp_path/'add'; root.mkdir() + with pytest.raises(ValueError, match='current thread'): + native.prepare_owned_fixture_add({'library': str(library), 'probe_token': 'owned'}, root) + assert not (root/'source').exists() + + def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_path, monkeypatch): import hashlib import cbb_refresh_verify.native_management as native @@ -25,7 +101,7 @@ def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_p assert '身份' in adapter.capabilities()['add'].reason -def test_native_add_passes_existing_library_and_staged_source_to_worker(tmp_path, monkeypatch): +def test_owned_fixture_add_passes_existing_library_and_source_to_worker(tmp_path, monkeypatch): from cbb_refresh_verify.management import ManagementRequest import cbb_refresh_verify.native_management as native source = tmp_path/'source.prj'; source.write_bytes(b'source') @@ -42,13 +118,13 @@ def test_native_add_passes_existing_library_and_staged_source_to_worker(tmp_path monkeypatch.setattr(adapter, '_worker', worker) request = ManagementRequest('add', library, 'Block', source=source, mapped_drive='S:', mapped_remote_root=r'\\server\share', timeout_seconds=23) - result = adapter.add(request, tmp_path/'evidence') + result = adapter.add_owned_fixture(request, tmp_path/'evidence', probe_token='owned') assert result.success and result.safe_to_continue and result.identity_matches is True assert seen['job'] == { 'operation': 'add', 'library': str(library), 'source': str(source), 'name': 'Block', 'partition': 'Logical-Physical', 'top_level_block': '', 'mapped_drive': 'S:', 'mapped_remote_root': r'\\server\share', - 'timeout_seconds': 23, 'source_identity': {'source': str(source)}, + 'timeout_seconds': 23, 'source_identity': {'source': str(source)}, 'probe_token': 'owned', } @@ -66,7 +142,8 @@ def test_native_add_identity_mismatch_is_native_success_with_evidence(tmp_path, 'detail': 'engineering identity readback mismatch', }) original = source.read_bytes() - result = adapter.add(ManagementRequest('add', library, 'Block', source=source), tmp_path/'evidence') + result = adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source), + tmp_path/'evidence', probe_token='owned') assert result.success and result.identity_matches is False assert 'identity' in result.detail assert result.evidence_path == tmp_path/'evidence' @@ -160,7 +237,8 @@ def test_probe_requires_owned_new_native_library_path(tmp_path): @pytest.mark.parametrize('records', [[], [('Actual', 'Logical-Physical')]]) @pytest.mark.parametrize('mapped', [False, True]) -def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records, mapped): +@pytest.mark.parametrize('flow', [1, 2]) +def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, monkeypatch, records, mapped, flow): import cbb_refresh_verify.native_management as native import cbb_refresh_verify.mentor_adapter as mentor import win32com.client @@ -177,7 +255,7 @@ def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, items = Collection([SimpleNamespace(Name=name, PartitionName=partition) for name, partition in records]) class Library: FullName = str(library_path) - FlowType = 1 + FlowType = flow def Partitions(self, kind): assert kind == 7 return Collection([items]) @@ -197,7 +275,7 @@ def test_native_catalog_only_uses_com_records_and_checks_exact_process(tmp_path, 'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path)}, tmp_path) assert result['success'] and result['safe_to_continue'] assert [(entry['name'], entry['partition']) for entry in result['entries']] == records - assert result['flow'] == 1 + assert result['flow'] == flow def test_production_caps_require_all_validated_binary_hashes(tmp_path, monkeypatch): @@ -268,6 +346,7 @@ def test_shared_add_reopens_exact_catalog_and_reports_identity_mismatch(tmp_path monkeypatch.setattr(native, '_current_desktop', lambda: (SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test')) monkeypatch.setattr(mentor, '_process_details', lambda _: [(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if state['running'] else []) monkeypatch.setattr(native, '_probe_add_dialog', lambda *a, **k: events.append('add')) + monkeypatch.setattr(native, 'validate_owned_fixture_add', lambda *a: None) result = native._catalog_com({'operation': 'add', 'library': str(library_path), 'desktop': 'CbbManagement_test', 'mentor_home': str(tmp_path), 'name': 'Block', 'partition': 'Logical-Physical', 'source_identity': {'expected': 'bytes'}}, tmp_path) -- 2.39.5 From ca84bce24f8fe46a162976ea9dfa855f0e85e8cd 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: Tue, 3 Nov 2026 11:27:34 +0800 Subject: [PATCH 31/59] fix: retain uncertain fixture owners and reject source overlap --- docs/releases/2026-11-03-native-crud.md | 2 + src/cbb_refresh_verify/native_management.py | 13 ++++- tests/test_native_management.py | 61 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md index 7de651f..eccda39 100644 --- a/docs/releases/2026-11-03-native-crud.md +++ b/docs/releases/2026-11-03-native-crud.md @@ -9,3 +9,5 @@ Before native activation the worker revalidates that evidence, exact current lea Verification is mocked/file-only: the original focused suite passed 86 tests plus two close/reopen checks. Follow-up tests reject public bypass, unprepared activation, missing current-thread ownership, invalid nonce/token/lease, substituted original source, changed backup and expired supervisor, and exercise a valid leased fixture with unchanged original source bytes. No Mentor processes, builds or full suite were launched by this implementation agent; serialized native evidence and semantic-identity research remain coordinator-owned. The follow-up focused suite passed 99 tests. After restoring ordinary catalog queries for non-DxDesigner flow readback, the native boundary/field subset passed 47 tests (including four additional flow cases). `git diff --check` passed. The original ignored scratch report remains on disk but is removed from version control. + +A second review found ownership lifetime and reverse containment gaps. The fixture route now nests the already-proven caller lease around worker dispatch and retains that same exclusive handle/checkpoint on unsafe return or exception, including callers that supply no release callback. It also rejects a fixture nested anywhere inside the original source tree before staging. Three new failure cases were observed before the fix; the final native-boundary/field subset passed 51 tests, including normal release and uploader-handle denial after uncertain release. No native processes were launched. diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 5064810..85236d4 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -112,7 +112,15 @@ class NativeManagementAdapter: if not acquired: raise RuntimeError('Another CBB operation owns the management mutex.') job = prepare_owned_fixture_add(job, evidence) - return self._launch_worker(job, evidence) + from .library_lock import LibraryLock + owner_safe = False + # Nest only after preparation proves an existing caller lease. + # An unsafe result/exception retains that same owner even when + # the caller used a plain LibraryLock without a callback. + with LibraryLock(job['library'], safe_to_release=lambda: owner_safe): + result = self._launch_worker(job, evidence) + owner_safe = result.get('safe_to_continue') is True + return result finally: if acquired: win32event.ReleaseMutex(mutex) mutex.Close() @@ -250,7 +258,8 @@ def prepare_owned_fixture_add(job, root): lease_handle = int(owner['handle']) source = Path(job['source']).resolve(strict=True) if (source.parent.is_relative_to(root.resolve()) or root.resolve().is_relative_to(source.parent) - or source.parent.is_relative_to(library.parent.resolve())): + or source.parent.is_relative_to(library.parent.resolve()) + or library.parent.resolve().is_relative_to(source.parent)): raise ValueError('Research source must be independent of fixture and transaction evidence.') source_manifest = copy_verified(source.parent, root/'source') backup = copy_verified(library.parent, root/'library-before', diff --git a/tests/test_native_management.py b/tests/test_native_management.py index e2193d6..72826d1 100644 --- a/tests/test_native_management.py +++ b/tests/test_native_management.py @@ -89,6 +89,67 @@ def test_fixture_metadata_without_current_thread_lease_cannot_stage_source(tmp_p assert not (root/'source').exists() +@pytest.mark.parametrize('outcome', ['unsafe', 'exception', 'safe']) +def test_owned_fixture_retains_default_caller_lease_until_native_release(tmp_path, monkeypatch, outcome): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.management import ManagementRequest + from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy, blocker_path, _owners, _guard + library = tmp_path/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.write_bytes(b'fixture') + source = tmp_path/'original/Source.prj'; source.parent.mkdir(); source.write_bytes(b'source') + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8') + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + monkeypatch.setattr(adapter, 'route_capabilities', lambda: {'add': native.Capability(True, 'ready')}) + monkeypatch.setattr(native, 'engineering_identity', lambda _: {'source': 'bytes'}) + def launch(*args): + if outcome == 'exception': raise OSError('worker result lost after dispatch') + return {'success': outcome == 'safe', 'safe_to_continue': outcome == 'safe'} + monkeypatch.setattr(adapter, '_launch_worker', launch) + lease = LibraryLock(library) + try: + with lease: + if outcome == 'exception': + with pytest.raises(OSError, match='after dispatch'): + adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source), + tmp_path/'add', probe_token='owned') + else: + adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source), + tmp_path/'add', probe_token='owned') + assert blocker_path(library).exists() is (outcome != 'safe') + if outcome != 'safe': + with pytest.raises(LibraryBusy): + with LibraryLock(library): pass + import win32file, win32con, pywintypes + with pytest.raises(pywintypes.error): + win32file.CreateFile(str(lease.path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, 0, None) + else: + with LibraryLock(library): pass + finally: + # Only the file-only fake worker above ran; release its test lease. + with _guard: + owner = _owners.pop(lease.key, None) + if owner: owner['handle'].Close() + blocker_path(library).unlink(missing_ok=True) + + +def test_fixture_nested_inside_source_tree_rejected_before_staging(tmp_path): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.library_lock import LibraryLock + library = tmp_path/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.write_bytes(b'fixture') + source = tmp_path/'native-add/Source.prj'; source.write_bytes(b'original source') + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8') + root = tmp_path/'add'; root.mkdir() + with LibraryLock(library): + with pytest.raises(ValueError, match='independent'): + native.prepare_owned_fixture_add({'library': str(library), 'source': str(source), + 'name': 'Block', 'partition': 'Logical-Physical', 'source_identity': {}, + 'probe_token': 'owned'}, root) + assert source.read_bytes() == b'original source' and library.read_bytes() == b'fixture' + assert not (root/'source').exists() + + def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_path, monkeypatch): import hashlib import cbb_refresh_verify.native_management as native -- 2.39.5 From 63f3417f194693444a6db891fd014d9e0b537836 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: Tue, 3 Nov 2026 11:39:38 +0800 Subject: [PATCH 32/59] feat: capture gated native logical and physical identity evidence --- .../native_engineering_identity.py | 331 ++++++++++++++++++ tests/test_native_engineering_identity.py | 152 ++++++++ 2 files changed, 483 insertions(+) create mode 100644 src/cbb_refresh_verify/native_engineering_identity.py create mode 100644 tests/test_native_engineering_identity.py diff --git a/src/cbb_refresh_verify/native_engineering_identity.py b/src/cbb_refresh_verify/native_engineering_identity.py new file mode 100644 index 0000000..5c8cdc7 --- /dev/null +++ b/src/cbb_refresh_verify/native_engineering_identity.py @@ -0,0 +1,331 @@ +"""Pinned EE7.9.5 native logical exports plus conservative physical/assets proof. + +This contract is not raw iCDB equivalence or a claim of exhaustive arbitrary CES +semantics. It compares the proven hierarchy/flat ASCII and CSV/property exports, +and preserves physical design and all other primary engineering assets exactly. +""" +from dataclasses import dataclass +import hashlib +from pathlib import Path +import re +import subprocess +import time + +from .management import source_project, _write_json +from .scoped_manifest import ManifestScope, build_manifest, hash_file + +POLICY = 'mentor795-native-content-v1' +COVERAGE = {'logical_connectivity_and_properties': 'native hierarchy/flat ASCII and CSV', + 'physical_design_and_primary_assets': 'raw SHA256', + 'schematic_graphical_geometry': 'unproven', 'production_acceptance': False} +EXPORTER_PROOF = { + 'icdb2ascii.exe': 'e37c87356eaf57c0b97e4183458bc8c5eb6eb0ac497cc7ad40dcd585d8cd213b', + 'icdb2csv.exe': '22059f4a432e8eabf30a5326ebcbd463ed6fa6495af21d7aad5ab5d2f892f255', +} +INF_HEADER = (b'Design Name\tSystem Name\tSystem Version\tSystem_Date\tCompany_Name\tDepartment_Name' + b'\tAddress\tAddress1\tCity\tState_Province\tCountry\tZip\tPhone\tPhone_Ext\tFax\tFax_Ext') +CSV_SUFFIXES = ('', '.esf', '.inf', '.npr', '.ppn', '.ppr', '.prm', '.prt', '.pth', + '.sht', '.spn', '.spr', '.sym', '.thn') +_retained_exports = [] +_OWNER_NAMES = frozenset({'icdb2ascii.exe', 'icdb2csv.exe', 'icdbnetserver.exe', + 'icdbnetlauncher.exe', 'icdbservermanager.exe', 'icdbservermonitor.exe'}) + + +@dataclass(frozen=True) +class IdentityOutcome: + identity: dict | None + safe_to_continue: bool + detail: str + evidence_path: Path + + +def _digest(data): + return hashlib.sha256(data).hexdigest() + + +def normalize_ascii(data, top): + """Only the three observed, position-bound report metadata values vary.""" + lines = data.splitlines(keepends=True) + prefixes = {0: b'iCDB Ascii Report Generation Date: ', + 4: b'\t iCDB directory: ', 5: b'\t DATE: '} + if (len(lines) < 12 or lines[3].rstrip(b'\r\n') != b'\tReport Criteria:' + or any(not lines[i].startswith(prefix) or not lines[i][len(prefix):].strip() + for i, prefix in prefixes.items())): + raise ValueError('Incomplete or unknown native ASCII report header.') + header = b''.join(lines[:next((i for i, line in enumerate(lines) if line.startswith(b'Block ')), len(lines))]) + required = (b'\t REPORT FORMAT: FREEFORM', b'\t MODE: HIER AND FLAT BLOCKS', + b'\t CONNECTION LISTS: ON', b'\t TEXT LISTS: ON') + if any(header.count(value) != 1 for value in required): + raise ValueError('Native ASCII export mode does not match the proven contract.') + top_bytes = top.encode('mbcs') + if (not any(line.rstrip(b'\r\n') == b'Block '+top_bytes+b' \tRoot' for line in lines) + or not any(line.startswith(b'Config ') for line in lines)): + raise ValueError('Native ASCII export lacks the exact root or flat configuration.') + for index, prefix in prefixes.items(): + ending = b'\r\n' if lines[index].endswith(b'\r\n') else b'\n' + lines[index] = prefix+b''+ending + return b''.join(lines) + + +def normalize_inf(data): + lines = data.splitlines(keepends=True) + if len(lines) != 2 or lines[0].rstrip(b'\r\n') != INF_HEADER: + raise ValueError('Unknown native CSV information schema.') + values = lines[1].rstrip(b'\r\n').split(b'\t') + if len(values) != 16 or values[:3] != [b'identity', b'Design Capture', b'VB99.0']: + raise ValueError('Malformed native CSV information row.') + if not re.fullmatch(rb'\d{2}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}', values[3]): + raise ValueError('Unknown native CSV System_Date format.') + values[3] = b'' + ending = b'\r\n' if lines[1].endswith(b'\r\n') else b'\n' + return lines[0]+b'\t'.join(values)+ending + + +def csv_identity(root): + from .scoped_manifest import scan_tree + root = Path(root) + paths = {p.relative_to(root).as_posix(): p for p in scan_tree(root) if p.is_file()} + expected = {'work/identity'+suffix for suffix in CSV_SUFFIXES} + if paths.keys() != expected or any(not p.stat().st_size for p in paths.values()): + raise ValueError('Native CSV export is empty, incomplete or an unsupported snapshot.') + index = paths['work/identity'].read_bytes().splitlines() + expected_index = {b'file:identity'+suffix.encode('ascii') for suffix in CSV_SUFFIXES if suffix} + if len(index) != len(expected_index) or set(index) != expected_index: + raise ValueError('Native CSV index does not bind every exported table.') + return {name: _digest(normalize_inf(path.read_bytes()) if name.endswith('.inf') else path.read_bytes()) + for name, path in sorted(paths.items())} + + +def _project_encoding(data): + if data.startswith((b'\xff\xfe', b'\xfe\xff')): return 'utf-16' + if data.startswith(b'\xef\xbb\xbf'): return 'utf-8-sig' + try: data.decode('utf-8'); return 'utf-8' + except UnicodeDecodeError: return 'mbcs' + + +def project_library(source): + """Require one existing approved source library across every project ref.""" + from .backup_recovery import _project_text + source, pcb, _ = source_project(source) + libraries = set() + for path in (source, pcb.parent/'pcb.prj'): + values = re.findall(r'^KEY CentralLibrary "([^"\r\n]+)"', _project_text(path), re.M) + if not values: raise ValueError('Project lacks CentralLibrary references.') + for value in values: + target = (path.parent/value).resolve(strict=True) + if target.suffix.casefold() != '.lmc' or not target.is_file(): + raise ValueError('CentralLibrary is not an existing LMC.') + libraries.add(target) + if len(libraries) != 1: raise ValueError('Project CentralLibrary references disagree.') + return libraries.pop() + + +def _normalized_project(path, expected_library): + data = path.read_bytes(); encoding = _project_encoding(data) + text = data.decode(encoding) + refs = re.findall(r'^KEY CentralLibrary "([^"\r\n]*)"', text, re.M) + if not refs or any((path.parent/value).resolve() != expected_library.resolve() for value in refs): + raise ValueError(f'CentralLibrary reference does not resolve to the approved library: {path}') + return re.sub(r'(?m)^(KEY CentralLibrary ")[^"\r\n]*(")', + r'\1\2', text).encode(encoding) + + +def asset_identity(source, expected_library): + from .backup_recovery import _project_text + source, pcb, top = source_project(source) + expected_library = Path(expected_library) + if not expected_library.is_file(): raise ValueError('Approved CentralLibrary is missing.') + root = source.parent + projects = (source, pcb.parent/'pcb.prj') + databases = [] + for project in projects: + values = re.findall(r'^KEY iCDBDir "([^"\r\n]+)"', _project_text(project), re.M) + databases.extend((project.parent/value).resolve() for value in values) + main_database = databases[0] + runtime = [db/'cdbsvr' for db in databases] + files = build_manifest(root, scope=ManifestScope())['files'] + assets = {} + for relative, record in files.items(): + path = root/relative + if path == main_database/'icdb.dat' or any(path.is_relative_to(p) for p in runtime): + continue + if path in projects: + data = _normalized_project(path, expected_library) + assets[relative] = {'size': len(data), 'sha256': _digest(data)} + else: + assets[relative] = {'size': record['size'], 'sha256': record['sha256']} + return {'policy': POLICY, 'top_level_block': top, + 'layout_sha256': hash_file(pcb.parent/'Layout/LayoutDB.lyt'), 'assets': assets} + + +def check_exporters(mentor_home): + for name, expected in EXPORTER_PROOF.items(): + path = Path(mentor_home)/'iCDB/win32/bin'/name + if not path.is_file() or hash_file(path) != expected: + raise ValueError(f'Native identity exporter is not the proven EE7.9.5 binary: {path}') + + +def validate_export_output(kind, code, report, log): + text = Path(log).read_bytes().decode('mbcs', errors='strict') + if code != 0 or re.search(r'(?im)^\s*(?:#\s*)?(?:error|fatal)\b', text): + raise ValueError('Native identity exporter failed or reported an error.') + if kind == 'ascii': + if (not Path(report).is_file() or not Path(report).stat().st_size + or f'Report generation complete, report file is {report}' not in text): + raise ValueError('Native ASCII export lacks a complete nonempty report.') + elif kind == 'csv': + if 'iCDB to CSV complete' not in [line.strip() for line in text.splitlines()]: + raise ValueError('Native CSV export lacks its completion marker.') + csv_identity(report) + else: + raise ValueError('Unsupported native export kind.') + + +def _process_snapshot(): + import win32com.client + service = win32com.client.GetObject('winmgmts:') + try: + return [{'pid': int(p.ProcessId), 'parent': int(p.ParentProcessId or 0), + 'name': str(p.Name), 'executable': str(p.ExecutablePath or '')} + for p in service.ExecQuery('SELECT ProcessId, ParentProcessId, Name, ExecutablePath FROM Win32_Process')] + finally: + service = None + + +def _database_released(project_root): + """Exclusive reads plus absent addresses prove the copied native DBs idle.""" + import win32file, win32con + from contextlib import ExitStack + root = Path(project_root) + if any(root.rglob('sAddress.adr')): + return False + with ExitStack() as stack: + for path in root.rglob('*'): + if path.is_file() and path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'): + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, 0, None) + stack.callback(handle.Close) + return True + + +def _run_exports(project, root, mentor_home, timeout_seconds): + """Observe exact exporter/descendant handles; never kill a database owner.""" + import win32api, win32con, win32event, pywintypes + root = Path(root) + before = _process_snapshot() + if any(p['name'].casefold() in _OWNER_NAMES for p in before): + return {'success': False, 'safe_to_continue': False, 'detail': 'An existing iCDB owner prevents isolated identity export.'} + if not _database_released(project.parent): + return {'success': False, 'safe_to_continue': False, 'detail': 'Analysis copy has unreleased native addresses or DB handles.'} + commands = [ + ('ascii', [str(Path(mentor_home)/'iCDB/win32/bin/icdb2ascii.exe'), + f'-project={project}', f'-file={root/"design.rpt"}', f'-clog={root/"ascii.log"}', + '-f', '-h', '-c', '-t', '-nostrip'], root/'design.rpt', root/'ascii.log'), + ('csv', [str(Path(mentor_home)/'iCDB/win32/bin/icdb2csv.exe'), + f'-project={project}', '-snap=DxD', '-file=identity', f'-output={root/"tables"}', + f'-clog={root/"csv.log"}', '-properties', '-nostrip'], root/'tables', root/'csv.log'), + ] + for kind, command, report, log in commands: + process = None + handles = {} + released = False + record = {'command': command, 'kind': kind, 'success': False, 'safe_to_continue': False} + try: + deadline = time.monotonic()+float(timeout_seconds) + with (root/(kind+'-stdout.txt')).open('wb') as output: + process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT, + creationflags=subprocess.CREATE_NO_WINDOW) + record['pid'] = process.pid + descendants = {process.pid} + _write_json(root/(kind+'-process.json'), record) + while time.monotonic() < deadline: + snapshot = _process_snapshot() + changed = True + while changed: + previous = len(descendants) + descendants.update(p['pid'] for p in snapshot if p['parent'] in descendants) + changed = len(descendants) != previous + owners = [p for p in snapshot if p['pid'] in descendants or p['name'].casefold() in _OWNER_NAMES] + for owner in owners: + if owner['pid'] not in handles: + try: handles[owner['pid']] = win32api.OpenProcess(win32con.SYNCHRONIZE, False, owner['pid']) + except pywintypes.error as error: + if error.winerror != 87: raise + record['observed_pids'] = sorted(descendants | handles.keys()) + if (process.poll() is not None and not owners + and all(win32event.WaitForSingleObject(h, 0) == win32con.WAIT_OBJECT_0 for h in handles.values())): + released = _database_released(project.parent) + if released: break + time.sleep(.1) + record.update(exit_code=process.poll(), safe_to_continue=released) + if not released: + record['detail'] = 'TIMEOUT: native export owner or copied database release remains unconfirmed.' + return record + validate_export_output(kind, process.returncode, report, log) + record.update(success=True, detail='Native export complete and owners released.') + except Exception as error: + record.update(detail=str(error), safe_to_continue=released) + return record + finally: + if process is not None and not released: + _retained_exports.append((process, handles)) + else: + for handle in handles.values(): handle.Close() + _write_json(root/(kind+'-process.json'), record) + return {'success': True, 'safe_to_continue': True, 'detail': 'Both native exports completed and released.'} + + +def capture_identity(source, evidence_root, *, expected_library, owner_library, mentor_home, + timeout_seconds=180): + """Capture on a fresh project copy while an existing transaction owns its library.""" + from .backup_recovery import _align_library + from .library_lock import LibraryLock, owned_snapshot + from .scoped_manifest import copy_verified + import win32event, win32con + source, pcb, top = source_project(source) + root = Path(evidence_root).absolute() + library = Path(owner_library) + lease = LibraryLock(library) + if owned_snapshot(lease.path) is None: + raise ValueError('Native identity capture requires a current-thread transaction library lease.') + if (root.resolve().is_relative_to(source.parent) or source.parent.is_relative_to(root.resolve()) + or root.resolve().is_relative_to(library.parent.resolve())): + raise ValueError('Native identity evidence must be independent of source and library.') + check_exporters(mentor_home) + root.mkdir(parents=True, exist_ok=False) + mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') + acquired = False + owner_safe = True + outcome = IdentityOutcome(None, True, 'Identity capture did not start.', root) + try: + acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) + if not acquired: raise RuntimeError('Another CBB operation owns the management mutex.') + with LibraryLock(library, safe_to_release=lambda: owner_safe): + assets = asset_identity(source, Path(expected_library)) + original = copy_verified(source.parent, root/'project') + clone = root/'project'/source.name + _, clone_pcb, _ = source_project(clone) + _align_library(clone, clone_pcb, Path(expected_library)) + if any((root/'project').rglob('sAddress.adr')): + raise ValueError('Analysis source contains an unresolved database address.') + owner_safe = False + native = _run_exports(clone, root, mentor_home, timeout_seconds) + owner_safe = native.get('safe_to_continue') is True + _write_json(root/'native-outcome.json', native) + if not owner_safe or not native.get('success'): + outcome = IdentityOutcome(None, owner_safe, native.get('detail', 'Native export failed.'), root) + else: + ascii_bytes = normalize_ascii((root/'design.rpt').read_bytes(), top) + csv = csv_identity(root/'tables') + if build_manifest(source.parent) != original: + raise ValueError('Identity input changed while exporting its disposable copy.') + fingerprint = {**assets, 'coverage': COVERAGE, 'ascii_sha256': _digest(ascii_bytes), 'csv': csv} + outcome = IdentityOutcome(fingerprint, True, 'Native logical exports and primary assets captured.', root) + except Exception as error: + outcome = IdentityOutcome(None, owner_safe, str(error), root) + finally: + if acquired: win32event.ReleaseMutex(mutex) + mutex.Close() + _write_json(root/'identity.json', {'identity': outcome.identity, 'safe_to_continue': outcome.safe_to_continue, + 'detail': outcome.detail, 'policy': POLICY}) + return outcome diff --git a/tests/test_native_engineering_identity.py b/tests/test_native_engineering_identity.py new file mode 100644 index 0000000..d50c792 --- /dev/null +++ b/tests/test_native_engineering_identity.py @@ -0,0 +1,152 @@ +"""Native export and asset identity boundaries; never launch native tools.""" +from pathlib import Path +import pytest + + +def ascii_report(date=b'Tue Nov 03 11:19:39 2026', directory=b'C:\\copy\\database'): + return (b'iCDB Ascii Report Generation Date: '+date+b'\r\n\r\n\r\n\tReport Criteria:\r\n' + b'\t iCDB directory: '+directory+b'\r\n\t DATE: '+date+b'\r\n\r\n' + b'\t REPORT FORMAT: FREEFORM\r\n\t MODE: HIER AND FLAT BLOCKS\r\n' + b'\t CONNECTION LISTS: ON\r\n\t TEXT LISTS: ON\r\n\r\n' + b'Block Top \tRoot\r\n\tPort A\r\nConfig default\tview none\r\n\tPin A\r\n') + + +def test_ascii_only_normalizes_three_validated_header_values(): + from cbb_refresh_verify.native_engineering_identity import normalize_ascii + first = ascii_report() + changed = ascii_report(b'Wed Nov 04 12:00:00 2026', b'D:\\other\\database') + assert normalize_ascii(first, 'Top') == normalize_ascii(changed, 'Top') + assert normalize_ascii(first, 'Top') != normalize_ascii(first.replace(b'Port A', b'Port B'), 'Top') + body = b'\tiCDB Ascii Report Generation Date: design property\r\n' + assert normalize_ascii(first+body, 'Top') != normalize_ascii(first+body.replace(b'property', b'value'), 'Top') + + +@pytest.mark.parametrize('change', ['empty', 'wrong_root', 'missing_config', 'header_duplicate', 'wrong_mode']) +def test_ascii_rejects_missing_or_unrecognized_structure(change): + from cbb_refresh_verify.native_engineering_identity import normalize_ascii + data = ascii_report() + data = {'empty': b'', 'wrong_root': data.replace(b'Block Top ', b'Block Other '), + 'missing_config': data.replace(b'Config default', b'Partial default'), + 'header_duplicate': data.replace(b'\tReport Criteria:', b'\t DATE: extra'), + 'wrong_mode': data.replace(b'HIER AND FLAT BLOCKS', b'FLAT ONLY')}[change] + with pytest.raises(ValueError): normalize_ascii(data, 'Top') + + +def test_inf_only_normalizes_unique_timestamp_column(): + from cbb_refresh_verify.native_engineering_identity import normalize_inf, INF_HEADER + row = [b'identity', b'Design Capture', b'VB99.0', b'11/03/26 11:21:34']+[b'value']*12 + first = INF_HEADER+b'\r\n'+b'\t'.join(row)+b'\r\n' + assert normalize_inf(first) == normalize_inf(first.replace(b'11:21:34', b'11:21:37')) + assert normalize_inf(first) != normalize_inf(first.replace(b'value', b'changed', 1)) + with pytest.raises(ValueError): normalize_inf(first.replace(b'System_Date', b'Unknown')) + with pytest.raises(ValueError): normalize_inf(first+b'extra row\r\n') + + +@pytest.fixture +def project(tmp_path): + from test_management import project + source = project(tmp_path/'project', name='Source') + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.write_bytes(b'library') + for path in (source, source.parent/'PCB/pcb.prj'): + with path.open('a', encoding='utf-8') as stream: + stream.write(f'KEY CentralLibrary "{library}"\n') + return source, library + + +def test_assets_require_correct_library_references_and_unknown_bytes(project): + from cbb_refresh_verify.native_engineering_identity import asset_identity + source, library = project + before = asset_identity(source, library) + runtime = source.parent/'database/cdbsvr/events/events.dat' + runtime.parent.mkdir(parents=True); runtime.write_bytes(b'runtime') + (source.parent/'database/icdb.dat').write_bytes(b'native normalized database') + assert asset_identity(source, library) == before + unknown = source.parent/'unknown-engineering.bin'; unknown.write_bytes(b'engineering') + assert asset_identity(source, library) != before + unknown.unlink() + (source.parent/'PCB/Layout/LayoutDB.lyt').write_bytes(b'changed physical design') + assert asset_identity(source, library) != before + text = (source.parent/'PCB/pcb.prj').read_text() + (source.parent/'PCB/pcb.prj').write_text(text.replace(str(library), 'missing.lmc')) + with pytest.raises(ValueError, match='CentralLibrary'): + asset_identity(source, library) + + +@pytest.mark.parametrize('failure', ['empty', 'missing_completion', 'error', 'exit']) +def test_export_evidence_requires_successful_complete_native_output(tmp_path, failure): + from cbb_refresh_verify.native_engineering_identity import validate_export_output + report = tmp_path/'design.rpt'; report.write_bytes(b'report') + log = tmp_path/'native.log' + log.write_text(f'Report generation complete, report file is {report}\n') + code = 0 + if failure == 'empty': report.write_bytes(b'') + if failure == 'missing_completion': log.write_text('partial report\n') + if failure == 'error': + with log.open('a') as stream: stream.write('ERROR: database corrupt\n') + if failure == 'exit': code = 2 + with pytest.raises(ValueError): validate_export_output('ascii', code, report, log) + + +def test_capture_only_exports_fresh_copy_and_leaves_source_unchanged(project, tmp_path, monkeypatch): + import cbb_refresh_verify.native_engineering_identity as identity + from cbb_refresh_verify.library_lock import LibraryLock + from cbb_refresh_verify.scoped_manifest import build_manifest + source, library = project + original = build_manifest(source.parent) + monkeypatch.setattr(identity, 'check_exporters', lambda _: None) + def exports(clone, root, *_): + assert clone.parent == root/'project' and clone != source + (clone.parent/'database/icdb.dat').write_bytes(b'export changed disposable copy') + (root/'design.rpt').write_bytes(ascii_report()) + return {'success': True, 'safe_to_continue': True, 'detail': ''} + monkeypatch.setattr(identity, '_run_exports', exports) + monkeypatch.setattr(identity, 'csv_identity', lambda _: {'work/identity': 'csv proof'}) + with LibraryLock(library): + result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library, + owner_library=library, mentor_home=tmp_path) + assert result.identity['policy'] == identity.POLICY and result.safe_to_continue + assert build_manifest(source.parent) == original + + +def test_capture_unreleased_exporter_retains_caller_lease(project, tmp_path, monkeypatch): + import cbb_refresh_verify.native_engineering_identity as identity + from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy, blocker_path, _owners, _guard + source, library = project + monkeypatch.setattr(identity, 'check_exporters', lambda _: None) + monkeypatch.setattr(identity, '_run_exports', lambda *a: { + 'success': False, 'safe_to_continue': False, 'detail': 'TIMEOUT: native exporter retained'}) + lease = LibraryLock(library) + try: + with lease: + result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library, + owner_library=library, mentor_home=tmp_path) + assert result.identity is None and not result.safe_to_continue + with pytest.raises(LibraryBusy): + with LibraryLock(library): pass + finally: + with _guard: + owner = _owners.pop(lease.key, None) + if owner: owner['handle'].Close() + blocker_path(library).unlink(missing_ok=True) + + +def test_export_timeout_retains_process_without_kill(project, tmp_path, monkeypatch): + import cbb_refresh_verify.native_engineering_identity as identity + source, _ = project + root = tmp_path/'exports'; root.mkdir() + seen = [] + class Running: + pid = 987654 + def __init__(self, command, **kwargs): seen.append(command) + def poll(self): return None + def kill(self): pytest.fail('native owner cannot be killed') + def terminate(self): pytest.fail('native owner cannot be terminated') + monkeypatch.setattr(identity, '_retained_exports', []) + monkeypatch.setattr(identity.subprocess, 'Popen', Running) + monkeypatch.setattr(identity, '_process_snapshot', lambda: []) + monkeypatch.setattr(identity, '_database_released', lambda _: True) + result = identity._run_exports(source, root, tmp_path, 0) + assert not result['safe_to_continue'] and 'TIMEOUT' in result['detail'] + assert len(identity._retained_exports) == 1 + assert seen[0][-5:] == ['-f', '-h', '-c', '-t', '-nostrip'] + assert (root/'ascii-process.json').is_file() -- 2.39.5 From 89500cbb5f0c27bb1f087f871c1ed8804e728751 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: Tue, 3 Nov 2026 11:43:45 +0800 Subject: [PATCH 33/59] feat: require native EDIF geometry in engineering identity --- .../native_engineering_identity.py | 79 +++++++++++++++++-- tests/test_native_engineering_identity.py | 20 +++++ 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/cbb_refresh_verify/native_engineering_identity.py b/src/cbb_refresh_verify/native_engineering_identity.py index 5c8cdc7..4cc6c16 100644 --- a/src/cbb_refresh_verify/native_engineering_identity.py +++ b/src/cbb_refresh_verify/native_engineering_identity.py @@ -17,17 +17,18 @@ from .scoped_manifest import ManifestScope, build_manifest, hash_file POLICY = 'mentor795-native-content-v1' COVERAGE = {'logical_connectivity_and_properties': 'native hierarchy/flat ASCII and CSV', 'physical_design_and_primary_assets': 'raw SHA256', - 'schematic_graphical_geometry': 'unproven', 'production_acceptance': False} + 'schematic_graphical_geometry': 'native EDIF schematic', 'production_acceptance': False} EXPORTER_PROOF = { 'icdb2ascii.exe': 'e37c87356eaf57c0b97e4183458bc8c5eb6eb0ac497cc7ad40dcd585d8cd213b', 'icdb2csv.exe': '22059f4a432e8eabf30a5326ebcbd463ed6fa6495af21d7aad5ab5d2f892f255', + 'edifExporter.exe': '8b5f9682007da5b759e7b2f0fa9720f6771b2efb9a546636b270a58e1268a80e', } INF_HEADER = (b'Design Name\tSystem Name\tSystem Version\tSystem_Date\tCompany_Name\tDepartment_Name' b'\tAddress\tAddress1\tCity\tState_Province\tCountry\tZip\tPhone\tPhone_Ext\tFax\tFax_Ext') CSV_SUFFIXES = ('', '.esf', '.inf', '.npr', '.ppn', '.ppr', '.prm', '.prt', '.pth', '.sht', '.spn', '.spr', '.sym', '.thn') _retained_exports = [] -_OWNER_NAMES = frozenset({'icdb2ascii.exe', 'icdb2csv.exe', 'icdbnetserver.exe', +_OWNER_NAMES = frozenset({'icdb2ascii.exe', 'icdb2csv.exe', 'edifexporter.exe', 'icdbnetserver.exe', 'icdbnetlauncher.exe', 'icdbservermanager.exe', 'icdbservermonitor.exe'}) @@ -81,6 +82,61 @@ def normalize_inf(data): return lines[0]+b'\t'.join(values)+ending +def normalize_edif(data, top): + """Bounded structural parse; replace only root/status/written/timestamp.""" + from datetime import datetime + if not data or len(data) > 64*1024*1024: + raise ValueError('EDIF report is empty or exceeds the supported parsing limit.') + token_pattern = re.compile(rb'\s+|[()]|"(?:[^"\\]|\\.)*"|[^\s()"]+') + stack, roots, nodes = [], [], [] + position = 0 + for match in token_pattern.finditer(data): + if match.start() != position: raise ValueError('Malformed EDIF token.') + position = match.end(); token = match.group() + if token.isspace(): continue + if token == b'(': + node = {'start': match.start(), 'items': []} + if stack: stack[-1]['items'].append(node) + else: roots.append(node) + nodes.append(node); stack.append(node) + if len(stack) > 256 or len(nodes) > 1000000: + raise ValueError('EDIF exceeds supported structural limits.') + elif token == b')': + if not stack: raise ValueError('Unbalanced EDIF report.') + node = stack.pop(); node['end'] = match.end() + if not node['items'] or not isinstance(node['items'][0], bytes): + raise ValueError('EDIF node has no keyword.') + else: + if not stack: raise ValueError('Unexpected EDIF content outside the root.') + stack[-1]['items'].append(token) + if position != len(data) or stack or len(roots) != 1 or roots[0]['items'][0] != b'edif': + raise ValueError('Unbalanced or unsupported EDIF root.') + def children(node, tag): + return [n for n in node['items'] if isinstance(n, dict) and n['items'][0] == tag] + def one(node, tag): + matches = children(node, tag) + if len(matches) != 1: raise ValueError(f'EDIF requires one {tag!r} node in the expected context.') + return matches[0] + root = roots[0] + if one(root, b'edifVersion')['items'] != [b'edifVersion', b'2', b'0', b'0']: + raise ValueError('Unsupported EDIF version.') + written = one(one(root, b'status'), b'written') + if one(written, b'program')['items'][1] != b'"DxDesigner Edif Exporter"': + raise ValueError('Unrecognized native EDIF exporter.') + stamp = one(written, b'timestamp') + values = stamp['items'][1:] + if len(values) != 6 or any(not isinstance(v, bytes) or not v.isdigit() for v in values): + raise ValueError('Malformed EDIF header timestamp.') + datetime(*(int(v) for v in values)) + design = one(root, b'design') + if one(design, b'cellRef')['items'][1] != top.encode('mbcs'): + raise ValueError('EDIF top design differs from the requested RootBlock.') + if (not any(n['items'] == [b'viewType', b'SCHEMATIC'] for n in nodes) + or any(not any(n['items'][0] == tag for n in nodes) for tag in (b'page', b'figure', b'pt'))): + raise ValueError('EDIF schematic drawing is absent or unsupported.') + return data[:stamp['start']]+b'(timestamp )'+data[stamp['end']:] + + def csv_identity(root): from .scoped_manifest import scan_tree root = Path(root) @@ -160,7 +216,7 @@ def asset_identity(source, expected_library): def check_exporters(mentor_home): for name, expected in EXPORTER_PROOF.items(): - path = Path(mentor_home)/'iCDB/win32/bin'/name + path = Path(mentor_home)/('wv/win32/bin' if name == 'edifExporter.exe' else 'iCDB/win32/bin')/name if not path.is_file() or hash_file(path) != expected: raise ValueError(f'Native identity exporter is not the proven EE7.9.5 binary: {path}') @@ -177,6 +233,11 @@ def validate_export_output(kind, code, report, log): if 'iCDB to CSV complete' not in [line.strip() for line in text.splitlines()]: raise ValueError('Native CSV export lacks its completion marker.') csv_identity(report) + elif kind == 'edif': + if (not Path(report).is_file() or not Path(report).stat().st_size + or 'Done' not in [line.strip() for line in text.splitlines()] + or f'Dumping edif file {report} ...' not in text): + raise ValueError('Native EDIF export lacks a complete nonempty drawing report.') else: raise ValueError('Unsupported native export kind.') @@ -217,6 +278,9 @@ def _run_exports(project, root, mentor_home, timeout_seconds): return {'success': False, 'safe_to_continue': False, 'detail': 'An existing iCDB owner prevents isolated identity export.'} if not _database_released(project.parent): return {'success': False, 'safe_to_continue': False, 'detail': 'Analysis copy has unreleased native addresses or DB handles.'} + from .backup_recovery import _project_text + _, _, top = source_project(project) + database = (project.parent/re.findall(r'^KEY iCDBDir "([^"\r\n]+)"', _project_text(project), re.M)[0]).resolve() commands = [ ('ascii', [str(Path(mentor_home)/'iCDB/win32/bin/icdb2ascii.exe'), f'-project={project}', f'-file={root/"design.rpt"}', f'-clog={root/"ascii.log"}', @@ -224,6 +288,9 @@ def _run_exports(project, root, mentor_home, timeout_seconds): ('csv', [str(Path(mentor_home)/'iCDB/win32/bin/icdb2csv.exe'), f'-project={project}', '-snap=DxD', '-file=identity', f'-output={root/"tables"}', f'-clog={root/"csv.log"}', '-properties', '-nostrip'], root/'tables', root/'csv.log'), + ('edif', [str(Path(mentor_home)/'wv/win32/bin/edifExporter.exe'), '-schematic', + '-icdb', str(database), '-isnap', 'DxD', '-top', top, '-o', str(root/'design.edf')], + root/'design.edf', root/'edif-stdout.txt'), ] for kind, command, report, log in commands: process = None @@ -272,7 +339,7 @@ def _run_exports(project, root, mentor_home, timeout_seconds): else: for handle in handles.values(): handle.Close() _write_json(root/(kind+'-process.json'), record) - return {'success': True, 'safe_to_continue': True, 'detail': 'Both native exports completed and released.'} + return {'success': True, 'safe_to_continue': True, 'detail': 'All three native exports completed and released.'} def capture_identity(source, evidence_root, *, expected_library, owner_library, mentor_home, @@ -317,9 +384,11 @@ def capture_identity(source, evidence_root, *, expected_library, owner_library, else: ascii_bytes = normalize_ascii((root/'design.rpt').read_bytes(), top) csv = csv_identity(root/'tables') + edif_bytes = normalize_edif((root/'design.edf').read_bytes(), top) if build_manifest(source.parent) != original: raise ValueError('Identity input changed while exporting its disposable copy.') - fingerprint = {**assets, 'coverage': COVERAGE, 'ascii_sha256': _digest(ascii_bytes), 'csv': csv} + fingerprint = {**assets, 'coverage': COVERAGE, 'ascii_sha256': _digest(ascii_bytes), + 'csv': csv, 'edif_sha256': _digest(edif_bytes)} outcome = IdentityOutcome(fingerprint, True, 'Native logical exports and primary assets captured.', root) except Exception as error: outcome = IdentityOutcome(None, owner_safe, str(error), root) diff --git a/tests/test_native_engineering_identity.py b/tests/test_native_engineering_identity.py index d50c792..aba6c69 100644 --- a/tests/test_native_engineering_identity.py +++ b/tests/test_native_engineering_identity.py @@ -98,6 +98,7 @@ def test_capture_only_exports_fresh_copy_and_leaves_source_unchanged(project, tm assert clone.parent == root/'project' and clone != source (clone.parent/'database/icdb.dat').write_bytes(b'export changed disposable copy') (root/'design.rpt').write_bytes(ascii_report()) + (root/'design.edf').write_bytes(edif_report()) return {'success': True, 'safe_to_continue': True, 'detail': ''} monkeypatch.setattr(identity, '_run_exports', exports) monkeypatch.setattr(identity, 'csv_identity', lambda _: {'work/identity': 'csv proof'}) @@ -150,3 +151,22 @@ def test_export_timeout_retains_process_without_kill(project, tmp_path, monkeypa assert len(identity._retained_exports) == 1 assert seen[0][-5:] == ['-f', '-h', '-c', '-t', '-nostrip'] assert (root/'ascii-process.json').is_file() + + +def edif_report(timestamp=b'2026 11 3 11 40 56'): + return (b'(edif DxD (edifVersion 2 0 0) (status (written (timestamp '+timestamp+b')' + b' (program "DxDesigner Edif Exporter"))) (library Board1 (cell Top' + b' (view SCHEMATIC_VIEW (viewType SCHEMATIC) (page sheet1 (figure LINE (pt 1 2)))' + b' (property timestamp (string "2026 11 3 11 40 56")))))' + b' (design Board1 (cellRef Top (libraryRef Board1))))') + + +def test_edif_only_normalizes_root_written_timestamp_preserving_geometry_and_properties(): + from cbb_refresh_verify.native_engineering_identity import normalize_edif + first = edif_report() + assert normalize_edif(first, 'Top') == normalize_edif(edif_report(b'2026 11 3 11 40 59'), 'Top') + assert normalize_edif(first, 'Top') != normalize_edif(first.replace(b'(pt 1 2)', b'(pt 1 3)'), 'Top') + assert normalize_edif(first, 'Top') != normalize_edif(first.replace(b'"2026 11 3 11 40 56"', b'"changed"'), 'Top') + for bad in [first[:-1], first.replace(b'(edifVersion 2 0 0)', b'(edifVersion 1 0 0)'), + first.replace(b'(cellRef Top', b'(cellRef Other'), first.replace(b'(pt 1 2)', b'')]: + with pytest.raises(ValueError): normalize_edif(bad, 'Top') -- 2.39.5 From 55a82fdc5fe05075627b30639b67fd002aa900ff 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: Tue, 3 Nov 2026 11:54:56 +0800 Subject: [PATCH 34/59] feat: bind native CRUD to prepared engine identity transactions --- docs/releases/2026-11-03-native-crud.md | 6 + src/cbb_refresh_verify/management.py | 15 +- .../management_transaction.py | 127 +++++++++++++ src/cbb_refresh_verify/native_management.py | 179 ++++++++++++++++-- src/cbb_refresh_verify/ui/main_window.py | 2 +- tests/test_management.py | 23 +++ tests/test_management_native_transaction.py | 135 +++++++++++++ tests/test_native_management.py | 18 ++ 8 files changed, 492 insertions(+), 13 deletions(-) create mode 100644 src/cbb_refresh_verify/management_transaction.py create mode 100644 tests/test_management_native_transaction.py diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md index eccda39..691daba 100644 --- a/docs/releases/2026-11-03-native-crud.md +++ b/docs/releases/2026-11-03-native-crud.md @@ -11,3 +11,9 @@ Verification is mocked/file-only: the original focused suite passed 86 tests plu The follow-up focused suite passed 99 tests. After restoring ordinary catalog queries for non-DxDesigner flow readback, the native boundary/field subset passed 47 tests (including four additional flow cases). `git diff --check` passed. The original ignored scratch report remains on disk but is removed from version control. A second review found ownership lifetime and reverse containment gaps. The fixture route now nests the already-proven caller lease around worker dispatch and retains that same exclusive handle/checkpoint on unsafe return or exception, including callers that supply no release callback. It also rejects a fixture nested anywhere inside the original source tree before staging. Three new failure cases were observed before the fix; the final native-boundary/field subset passed 51 tests, including normal release and uploader-handle denial after uncertain release. No native processes were launched. + +The subsequent `mentor795-native-content-v1` contract requires pinned native hierarchy/flat ASCII, CSV/property tables and schematic EDIF exports on disposable copies, plus exact raw physical/primary assets. Only the three observed ASCII metadata headers, CSV `System_Date` field and root EDIF `status/written/timestamp` node are normalized. All project library references must resolve to an approved library before their values can be normalized. Unknown primary files, layout data, drawing coordinates, properties and EDIF userData remain part of the comparison. Unsupported exports fail closed; this is not a claim of arbitrary unexported CES equivalence. + +The real management engine now captures a source baseline before any removal. Native Add requires a prepared transaction binding the verified full backup, staged source, baseline proof, exact target/name/partition, supervisor and exclusive lease. After normal native release, it corrects imported PRJ/PCB library references using the existing documented text helpers, reopens the exact native catalog and compares a target-only identity. Update remains remove plus Add; failures retain the recovery snapshot and checkpoint. The UI action is explicitly labeled “用工程替换已选 CBB…(原生重新导入)”; backup Work restoration is unchanged. + +Coordinator-native source/registered capture matched all three exports and primary assets after correcting the observed imported PCB reference; original source manifests were unchanged. Implementation verification remains file-only: 114 focused tests passed before the final bootstrap/lease refinement. `create_owned_fixture` and `run_owned_fixture_management` provide a token-bound empty-library/real-engine route for the coordinator's actual CRUD cycle. Production Add/Update remain disabled until that separate acceptance and promotion step. diff --git a/src/cbb_refresh_verify/management.py b/src/cbb_refresh_verify/management.py index e5ccee9..6e96a05 100644 --- a/src/cbb_refresh_verify/management.py +++ b/src/cbb_refresh_verify/management.py @@ -29,6 +29,7 @@ class ManagementRequest: timeout_seconds: float = 180 mapped_drive: str = '' mapped_remote_root: str = '' + transaction_token: str = '' @dataclass(frozen=True) @@ -53,6 +54,7 @@ class NativeOutcome: detail: str identity_matches: bool | None = None evidence_path: Path | None = None + identity_proof: dict | None = None @dataclass(frozen=True) @@ -268,6 +270,14 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro identity = engineering_identity(staged.source) if build_manifest(source.parent) != source_manifest: raise OSError('来源工程在暂存期间变化。') + if source and hasattr(adapter, 'prepare_add_transaction'): + checkpoint('prepare_identity') + owner_safe = False + staged, proof = adapter.prepare_add_transaction(staged, root, original_source=source) + owner_safe = proof.safe_to_continue + if not owner_safe or proof.identity is None: + return finish('UPLOAD_FAILED', proof.detail, owner_safe) + identity = proof.identity if build_manifest(library_root, scope=scope) != original: raise OSError('备份后中心库内容变化,停止写入。') if request.operation in ('remove', 'update'): @@ -302,8 +312,11 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro owner_safe = after.safe_to_continue matches = [entry for entry in after.entries if entry.name == request.name and entry.partition == request.partition] + proof_matches = (native.identity_matches is True and native.identity_proof == identity + if hasattr(adapter, 'prepare_add_transaction') else + len(matches) == 1 and matches[0].engineering_identity == identity) if (not owner_safe or Path(after.library).resolve() != canonical_library or after.flow != 1 - or len(matches) != 1 or matches[0].engineering_identity != identity): + or len(matches) != 1 or not proof_matches): return finish('RECOVERY_REQUIRED', '原生新增后名称、分区或来源工程内容未确认一致。', False) uploaded = True checkpoint('uploaded') diff --git a/src/cbb_refresh_verify/management_transaction.py b/src/cbb_refresh_verify/management_transaction.py new file mode 100644 index 0000000..ff23f91 --- /dev/null +++ b/src/cbb_refresh_verify/management_transaction.py @@ -0,0 +1,127 @@ +"""Prepared Add authority bound to the engine checkpoint, snapshot and live lease.""" +import json +import os +from pathlib import Path +import threading +from uuid import uuid4 + +from .management import _write_json, engineering_identity +from .scoped_manifest import build_manifest, ManifestScope + + +def _read(path): + return json.loads(Path(path).read_text(encoding='utf-8')) + + +def fixture_authority(root, library, token): + root = Path(root).resolve() + expected = root/'native-add/fixture/mentor_lib.lmc' + owner = _read(root/'probe-owner.json') + if (not token or owner.get('token') != token or not expected.is_file() + or Path(library).resolve() != expected.resolve() + or Path(owner.get('library', '')).resolve() != expected.resolve()): + raise ValueError('Management research requires the exact newly created owned fixture.') + return {'root': str(root), 'token': token} + + +def current_lease(library): + from .library_lock import LibraryLock, _guard, _owners, blocker_path, checkpoint_record + lease = LibraryLock(library) + with _guard: + owner = _owners.get(lease.key) + guard = checkpoint_record(blocker_path(library)) + if (owner is None or owner['retained'] or owner['thread'] != threading.get_ident() + or guard is None or guard['pid'] != os.getpid() + or guard['status'] != 'OPERATION_IN_PROGRESS' + or owner['lease_id'] != guard['lease_id'] + or Path(guard['library']).resolve() != Path(library).resolve()): + raise ValueError('Management transaction requires the current thread exact active library lease.') + return {'lease_id': owner['lease_id'], 'lease_handle': int(owner['handle']), 'supervisor_pid': os.getpid()} + + +def _authorize(record, mentor_home): + fixture = record.get('fixture') + if fixture is not None: + fixture_authority(fixture['root'], record['library'], fixture['token']) + else: + from .native_management import NativeManagementAdapter + capability = NativeManagementAdapter(mentor_home=mentor_home).capabilities()['add'] + if not capability.supported: raise ValueError(capability.reason) + + +def prepare_transaction(request, root, expected_identity, *, mentor_home, fixture=None): + root = Path(root).resolve() + source = Path(request.source).resolve() + if source.parent != root/'source': + raise ValueError('Native Add accepts only the management engine staged source.') + library = Path(request.library).resolve() + if root.is_relative_to(library.parent) or library.parent.is_relative_to(root): + raise ValueError('Management transaction evidence overlaps the target library.') + checkpoint = _read(root/'checkpoint.json') + stages = {item['stage'] for item in checkpoint.get('history', [])} + if (checkpoint.get('status') != 'RUNNING' or checkpoint.get('stage') != 'prepare_identity' + or Path(checkpoint['library']).resolve() != library + or checkpoint['name'] != request.name or checkpoint['partition'] != request.partition + or not {'backup', 'stage_source'}.issubset(stages)): + raise ValueError('Native Add requires the engine backup/staging checkpoint.') + backup = _read(root/'library-before-manifest.json') + if build_manifest(root/'library-before', scope=ManifestScope.library(request.name, library.name)) != backup: + raise ValueError('Management backup has not been verified.') + baseline = _read(root/'source-identity/identity.json') + if baseline.get('safe_to_continue') is not True or baseline.get('identity') != expected_identity: + raise ValueError('Native source identity evidence is missing or changed.') + record = {**current_lease(library), 'nonce': uuid4().hex, 'library': str(library), + 'name': request.name, 'partition': request.partition, 'top_level_block': request.top_level_block, + 'source_name': source.name, 'source_identity': engineering_identity(source), + 'source_manifest': build_manifest(source.parent), 'backup_manifest': backup, + 'expected_identity': expected_identity, 'fixture': fixture} + _authorize(record, mentor_home) + _write_json(root/'native-add-prepared.json', record) + return record['nonce'] + + +def validate_transaction(job, root, *, check_source=True): + from .library_lock import blocker_path, checkpoint_record, _supervisor_alive + import win32api, win32con, win32file + root = Path(root).resolve() + record = _read(root/'native-add-prepared.json') + library = Path(job['library']).resolve() + source = root/'source'/record['source_name'] + guard = checkpoint_record(blocker_path(library)) + checkpoint = _read(root/'checkpoint.json') + if (not job.get('transaction_nonce') or job['transaction_nonce'] != record['nonce'] + or Path(record['library']).resolve() != library + or any(job.get(key) != record[key] for key in ('name', 'partition', 'top_level_block', 'source_identity')) + or source.parent != root/'source' or source.resolve().parent != (root/'source').resolve() + or Path(job['source']).resolve() != source.resolve() + or guard is None or guard['lease_id'] != record['lease_id'] + or guard['pid'] != record['supervisor_pid'] or guard['pid'] != job['supervisor_pid'] + or guard['status'] != 'OPERATION_IN_PROGRESS' + or Path(guard['library']).resolve() != library or not _supervisor_alive(guard['pid']) + or checkpoint.get('status') != 'RUNNING' or checkpoint.get('stage') != 'native_add' + or Path(checkpoint['library']).resolve() != library + or checkpoint['name'] != record['name'] or checkpoint['partition'] != record['partition']): + raise ValueError('Native Add transaction authority is missing, stale or foreign.') + _authorize(record, job['mentor_home']) + baseline = _read(root/'source-identity/identity.json') + if (baseline.get('safe_to_continue') is not True or baseline.get('identity') != record['expected_identity'] + or build_manifest(root/'library-before', scope=ManifestScope.library(record['name'], library.name)) + != record['backup_manifest'] + or check_source and (build_manifest(source.parent) != record['source_manifest'] + or engineering_identity(source) != record['source_identity'])): + raise ValueError('Native Add baseline, backup or staged source proof changed.') + process = win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE, False, record['supervisor_pid']) + duplicate = None + try: + duplicate = win32api.DuplicateHandle(process, record['lease_handle'], win32api.GetCurrentProcess(), + 0, False, win32con.DUPLICATE_SAME_ACCESS) + final_path = win32file.GetFinalPathNameByHandle(duplicate, 0) + if final_path.startswith('\\\\?\\UNC\\'): final_path = '\\\\'+final_path[8:] + elif final_path.startswith('\\\\?\\'): final_path = final_path[4:] + expected_lock = library.with_name('.'+library.stem+'.cbb-upload.lock') + if Path(final_path).resolve() != expected_lock.resolve(): + raise ValueError('Native Add supervisor lease belongs to another library.') + finally: + if duplicate is not None: duplicate.Close() + process.Close() + return record diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 85236d4..9bc581a 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -3,7 +3,7 @@ The installed Tcl CleanWGLib/F4 contract has isolated removal proof. Discovery of an Add dialog is deliberately insufficient to enable production Add/Update. """ -from dataclasses import asdict +from dataclasses import asdict, replace import ctypes import json import os @@ -21,6 +21,9 @@ from .scoped_manifest import hash_file from .network_path import absolute_preserving_drive _retained_workers = [] +# Promotion requires coordinator-owned native CRUD evidence, in a separate change. +_PRODUCTION_ADD_VALIDATED = False +_PRODUCTION_UPDATE_VALIDATED = False _BINARY_PROOF = { 'lm/win32/bin/CleanWGLib.exe': '7808ded326555f1fd5d5c14f68803dbb4f970d624f9ad6e1c7982dc9ed867917', 'lm/win32/bin/LibraryManager.exe': 'a9c4c3d806794dbdc56d2bc4c637a1cf25e87e98644a7d5fd4eb0067ca5845e0', @@ -89,8 +92,9 @@ class NativeManagementAdapter: reason = self._binary_capability() return { 'remove': Capability(not reason, reason or '已验证 CleanWGLib/F4;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'), - 'add': Capability(False, '原生新增已调用并注册,但工程身份复读尚未通过;生产新增保持禁用。'), - 'update': Capability(False, '同名更新需要原生移除、重新导入及完整端到端隔离验证,目前不可用。'), + 'add': Capability(not reason and _PRODUCTION_ADD_VALIDATED, '原生新增及工程身份已有隔离证据;完整事务验证通过前生产新增保持禁用。'), + 'update': Capability(not reason and _PRODUCTION_ADD_VALIDATED and _PRODUCTION_UPDATE_VALIDATED, + '同名更新需要原生移除、重新导入及完整端到端隔离验证,目前不可用。'), } def _worker(self, job, evidence): @@ -111,7 +115,11 @@ class NativeManagementAdapter: win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) if not acquired: raise RuntimeError('Another CBB operation owns the management mutex.') - job = prepare_owned_fixture_add(job, evidence) + if job.get('authority_kind') == 'management': + from .management_transaction import validate_transaction + validate_transaction(job, evidence.parent) + else: + job = prepare_owned_fixture_add(job, evidence) from .library_lock import LibraryLock owner_safe = False # Nest only after preparation proves an existing caller lease. @@ -124,7 +132,7 @@ class NativeManagementAdapter: finally: if acquired: win32event.ReleaseMutex(mutex) mutex.Close() - if job['operation'] == 'probe_create_add': + if job['operation'] in ('probe_create_add', 'probe_create'): # Prepare the fixed NEW fixture before a native owner can start. # The parent owns its sidecar for the entire create/add invocation. from .library_lock import LibraryLock @@ -197,8 +205,93 @@ class NativeManagementAdapter: def add(self, request, evidence): # Route readiness never grants production mutation authority. - return NativeOutcome(False, True, self.capabilities()['add'].reason, - evidence_path=Path(evidence)) + capability = self.capabilities()['add'] + if not capability.supported: + return NativeOutcome(False, True, capability.reason, evidence_path=Path(evidence)) + return self._add_transaction(request, Path(evidence)) + + def prepare_add_transaction(self, request, root, *, original_source): + from .native_engineering_identity import project_library, asset_identity, capture_identity + from .management_transaction import current_lease, prepare_transaction + from .management import source_project + from .backup_recovery import _align_library + if not self.capabilities()['add'].supported: + raise ValueError(self.capabilities()['add'].reason) + current_lease(request.library) + original_assets = asset_identity(original_source, project_library(original_source)) + staged, pcb, _ = source_project(request.source, request.top_level_block) + if staged.parent != Path(root).resolve()/'source': + raise ValueError('Only the engine staged source can be aligned for native Add.') + _align_library(staged, pcb, request.library) + if asset_identity(staged, request.library) != original_assets: + raise ValueError('Staged engineering assets differ from the original source.') + proof = capture_identity(staged, Path(root)/'source-identity', expected_library=request.library, + owner_library=request.library, mentor_home=self.home, timeout_seconds=request.timeout_seconds) + if proof.identity is None or not proof.safe_to_continue: + return request, proof + token = prepare_transaction(request, root, proof.identity, mentor_home=self.home, + fixture=getattr(self, '_fixture_authority', None)) + return replace(request, transaction_token=token), proof + + def _add_transaction(self, request, evidence): + from .management_transaction import validate_transaction + from .library_lock import LibraryLock + from .native_engineering_identity import capture_identity, project_library + from .management import source_project + from .backup_recovery import _align_library, _idle_check + job = {'operation': 'add', 'authority_kind': 'management', 'transaction_nonce': request.transaction_token, + 'library': str(absolute_preserving_drive(request.library)), 'source': str(request.source), + 'name': request.name, 'partition': request.partition, 'top_level_block': request.top_level_block, + 'source_identity': engineering_identity(request.source), 'supervisor_pid': os.getpid(), + 'mentor_home': str(self.home), 'timeout_seconds': request.timeout_seconds, + 'mapped_drive': request.mapped_drive, 'mapped_remote_root': request.mapped_remote_root} + prepared = validate_transaction(job, evidence.parent) + owner_safe = False + with LibraryLock(request.library, safe_to_release=lambda: owner_safe): + result = self._worker(job, evidence) + owner_safe = result.get('safe_to_continue') is True + if not owner_safe or not result.get('success'): + return NativeOutcome(False, owner_safe, result.get('detail', 'Native Add failed.'), evidence_path=evidence) + owner_safe = False + # The native editor may leave PCB/pcb.prj relative to the staged + # location. Correct only documented library-reference tokens after + # its owner has exited, then reopen the native catalog independently. + validate_transaction(job, evidence.parent, check_source=False) + work = Path(request.library).parent/'ReusableBlocksLP'/request.name/'Work' + projects = list(work.glob('*.prj')) + if len(projects) != 1: + owner_safe = False + return NativeOutcome(True, False, 'Registered engineering project is not unique.', + identity_matches=False, evidence_path=evidence) + 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')} + _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']}) + 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] + if (not owner_safe or Path(catalog.library).resolve() != Path(request.library).resolve() + or catalog.flow != 1 or len(entries) != 1): + owner_safe = False + return NativeOutcome(True, False, 'Aligned target did not reopen with the exact catalog identity.', + identity_matches=False, evidence_path=evidence) + owner_safe = False + proof = capture_identity(target, evidence/'registered-identity', expected_library=request.library, + owner_library=request.library, mentor_home=self.home, timeout_seconds=request.timeout_seconds) + owner_safe = proof.safe_to_continue + matched = proof.identity is not None and proof.identity == prepared['expected_identity'] + _write_json(evidence/'identity-comparison.json', {'identity_matches': matched, + 'expected': prepared['expected_identity'], 'registered': proof.identity, + 'safe_to_continue': owner_safe, 'detail': proof.detail}) + return NativeOutcome(True, owner_safe, + 'Native ASCII/CSV/EDIF, physical assets and library references match.' if matched else + 'Native registration succeeded but complete engineering identity proof did not match: '+proof.detail, + identity_matches=matched, evidence_path=evidence, identity_proof=proof.identity) def add_owned_fixture(self, request, evidence, *, probe_token): """Research only: stage and back up a token-bound, actively leased fixture.""" @@ -240,6 +333,64 @@ class NativeManagementAdapter: detail, identity_matches=identity_matches, evidence_path=evidence) +class _OwnedFixtureManagementAdapter(NativeManagementAdapter): + """Diagnostic capability grant restricted to a token-bound created fixture.""" + def __init__(self, root, token, **kwargs): + super().__init__(**kwargs) + from .management_transaction import fixture_authority + self._fixture_authority = fixture_authority(root, Path(root)/'native-add/fixture/mentor_lib.lmc', token) + + def capabilities(self): + from .management_transaction import fixture_authority + from .native_engineering_identity import check_exporters + fixture = self._fixture_authority + fixture_authority(fixture['root'], Path(fixture['root'])/'native-add/fixture/mentor_lib.lmc', fixture['token']) + caps = super().capabilities() + reason = self._binary_capability() + try: check_exporters(self.home) + except (ValueError, OSError) as error: reason = str(error) + for operation in ('add', 'update'): + caps[operation] = Capability(not reason, reason or 'Owned fixture engine validation only; production capability unchanged.') + return caps + + def preflight(self, request, catalog): + from .management_transaction import fixture_authority + fixture = self._fixture_authority + fixture_authority(fixture['root'], request.library, fixture['token']) + super().preflight(request, catalog) + + +def run_owned_fixture_management(request, root, *, probe_token, mentor_home=None, **kwargs): + """Run the real engine with a narrow fixture grant, never a public override.""" + from .management import run_management + from .management_transaction import fixture_authority + fixture_authority(root, request.library, probe_token) + adapter = _OwnedFixtureManagementAdapter(root, probe_token, mentor_home=mentor_home) + return run_management(request, adapter=adapter, **kwargs) + + +def create_owned_fixture(root, *, mentor_home=None): + """Diagnostic-only empty native library, guarded by the existing creation protocol.""" + import win32con, win32event + root = absolute_preserving_drive(root) + if root.exists(): raise ValueError('Native fixture root must be entirely new.') + adapter = NativeManagementAdapter(mentor_home=mentor_home) + if not adapter.route_capabilities()['add'].supported: + raise ValueError(adapter.route_capabilities()['add'].reason) + mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795') + acquired = False + token = uuid4().hex + try: + acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) + if not acquired: raise RuntimeError('Another CBB operation owns the management mutex.') + library = root/'native-add/fixture/mentor_lib.lmc' + result = adapter._worker({'operation': 'probe_create', 'library': str(library), 'probe_token': token}, root/'native-add') + return {**result, 'probe_token': token, 'fixture_root': str(root)} + finally: + if acquired: win32event.ReleaseMutex(mutex) + mutex.Close() + + def prepare_owned_fixture_add(job, root): """Create fresh transaction evidence while this thread owns the fixture.""" from .library_lock import LibraryLock, owned_snapshot, checkpoint_record, blocker_path, _guard, _owners @@ -461,7 +612,9 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) from .management import source_project source, pcb, top = source_project(job['source'], job.get('top_level_block', '')) source_folder = 'source-replacement' if job['operation'] == 'probe_readd' else 'source' - source_root = Path(root).resolve()/'source' if job['operation'] == 'add' else Path(root).parent.resolve()/source_folder + source_root = (Path(root).resolve()/'source' + if job['operation'] == 'add' and job.get('authority_kind') != 'management' + else Path(root).parent.resolve()/source_folder) if not source.is_relative_to(source_root): raise ValueError('隔离新增只允许使用本次复制的来源工程。') _align_library(source, pcb, Path(job['library'])) @@ -716,7 +869,11 @@ def _catalog_com(job, root): import pythoncom, win32com.client, win32con, win32gui, win32process from .mentor_adapter import _process_details if job['operation'] == 'add': - validate_owned_fixture_add(job, root) + if job.get('authority_kind') == 'management': + from .management_transaction import validate_transaction + validate_transaction(job, root.parent) + else: + validate_owned_fixture_add(job, root) desktop, name = _current_desktop() if name != job['desktop'] or not name.startswith('CbbManagement_'): raise RuntimeError('拒绝在非本次私有桌面读取原生目录。') @@ -762,7 +919,7 @@ def _catalog_com(job, root): raise RuntimeError('COM 注册指向不同的 Library Manager 安装,拒绝打开中心库。') watcher = threading.Thread(target=observe_dialogs, daemon=True) watcher.start() - if job['operation'] == 'probe_create_add': + if job['operation'] in ('probe_create_add', 'probe_create'): target = prepare_probe_library(job, root) library = app.CreateLibrary(str(target), 1) _write_json(root.parent/'probe-owner.json', {'library': str(target), 'token': job['probe_token']}) @@ -854,7 +1011,7 @@ def management_worker_main(request_path): if job.get('mapped_drive') and job.get('mapped_remote_root'): from .network_path import ensure_mapped_drive_visible ensure_mapped_drive_visible(job['mapped_drive'], job['mapped_remote_root']) - if job['operation'] in ('catalog', 'add', 'probe_create_add', 'probe_readd'): + if job['operation'] in ('catalog', 'add', 'probe_create', 'probe_create_add', 'probe_readd'): if job['operation'] == 'add': route = NativeManagementAdapter(mentor_home=job['mentor_home']).route_capabilities()['add'] if not route.supported: diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 4bbdfce..3ab788f 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -200,7 +200,7 @@ class MainWindow(QWidget): management_layout = QVBoxLayout(management) management_buttons = QHBoxLayout() self.add_button = QPushButton('新增 CBB…') - self.update_button = QPushButton('同名更新已选…') + self.update_button = QPushButton('用工程替换已选 CBB…(原生重新导入)') self.remove_button = QPushButton('移除已选…') for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): widget.clicked.connect(lambda _=False, op=operation: self.manage_cbb(op)) diff --git a/tests/test_management.py b/tests/test_management.py index 8326d77..6a4d3a0 100644 --- a/tests/test_management.py +++ b/tests/test_management.py @@ -301,3 +301,26 @@ def test_source_inside_target_library_rejected_before_catalog(setup): adapter.catalog = lambda *a: pytest.fail('overlapping source reached native catalog') result = execute(replace(request, operation='add', name='New', source=source), adapter) assert result.status == 'BLOCKED' and not request.evidence_dir.exists() + + +@pytest.mark.parametrize('matched', [True, False]) +def test_prepared_native_identity_is_required_after_exact_catalog(setup, matched): + from cbb_refresh_verify.native_engineering_identity import IdentityOutcome + from cbb_refresh_verify.management import NativeOutcome + request, adapter, source = setup + expected = {'policy': 'native-test', 'logical': 'same', 'physical': 'same'} + def prepare(staged, root, *, original_source): + assert original_source == source and staged.source != source + assert (root/'library-before/mentor.lmc').exists() + adapter.calls.append('prepare_identity') + return staged, IdentityOutcome(expected, True, 'baseline captured', root) + adapter.prepare_add_transaction = prepare + original_add = adapter.add + def add(staged, evidence): + original_add(staged, evidence) + return NativeOutcome(True, True, 'native registered', identity_matches=matched, + identity_proof=expected if matched else {'different': True}) + adapter.add = add + result = execute(replace(request, operation='update', source=source, auto_verify=False), adapter) + assert adapter.calls[0:2] == ['prepare_identity', 'remove'] + assert result.status == ('UPLOADED' if matched else 'RECOVERY_REQUIRED') diff --git a/tests/test_management_native_transaction.py b/tests/test_management_native_transaction.py new file mode 100644 index 0000000..99e0781 --- /dev/null +++ b/tests/test_management_native_transaction.py @@ -0,0 +1,135 @@ +"""Run the real management engine with only the native/export boundaries faked.""" +from dataclasses import replace +import json +from pathlib import Path +import shutil + +import pytest + + +@pytest.fixture +def transaction(tmp_path, monkeypatch): + from test_management import project + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.native_engineering_identity as identity + import cbb_refresh_verify.backup_recovery as recovery + from cbb_refresh_verify.management import ManagementRequest, engineering_identity, _write_json + from cbb_refresh_verify.management_transaction import validate_transaction + root = tmp_path/'probe' + library = root/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.write_bytes(b'fixture library') + (root/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8') + source = project(tmp_path/'source') + for path in (source, source.parent/'PCB/pcb.prj'): + with path.open('a') as stream: stream.write(f'KEY CentralLibrary "{library}"\n') + state = {'entries': [], 'calls': [], 'corrupt': False, 'fail_add': False} + monkeypatch.setattr(native.NativeManagementAdapter, '_binary_capability', lambda _: '') + monkeypatch.setattr(identity, 'check_exporters', lambda _: None) + monkeypatch.setattr(recovery, '_idle_check', lambda *_: None) + def capture(source, evidence, *, expected_library, **kwargs): + evidence = Path(evidence); evidence.mkdir(parents=True, exist_ok=False) + value = identity.asset_identity(source, expected_library) + value.update(ascii_sha256='logical', csv={'work/identity': 'tables'}, edif_sha256='geometry') + _write_json(evidence/'identity.json', {'identity': value, 'safe_to_continue': True}) + return identity.IdentityOutcome(value, True, 'file-only export proof', evidence) + monkeypatch.setattr(identity, 'capture_identity', capture) + def worker(adapter, job, evidence): + evidence = Path(evidence); evidence.mkdir(parents=True, exist_ok=False) + state['calls'].append(job['operation']) + if job['operation'] == 'catalog': + return {'success': True, 'safe_to_continue': True, 'library': str(library), 'flow': 1, + 'entries': list(state['entries'])} + if job['operation'] == 'add': + if state.get('tamper') == 'nonce': job['transaction_nonce'] = 'foreign' + if state.get('tamper') == 'supervisor': job['supervisor_pid'] = -1 + if state.get('tamper') == 'backup': + (evidence.parent/'library-before'/library.name).write_bytes(b'corrupted backup') + if state.get('tamper') == 'source': + (Path(job['source']).parent/'database/icdb.dat').write_bytes(b'changed staged input') + validate_transaction(job, evidence.parent) + if state['fail_add']: return {'success': False, 'safe_to_continue': True, 'detail': 'native failure'} + staged = Path(job['source']) + (staged.parent/'database/icdb.dat').write_bytes(b'native database normalization') + work = library.parent/'ReusableBlocksLP'/job['name']/'Work' + shutil.copytree(staged.parent, work) + pcb_project = work/'PCB/pcb.prj' + text = pcb_project.read_text() + import re + pcb_project.write_text(re.sub(r'KEY CentralLibrary "[^"]*"', 'KEY CentralLibrary "wrong-relative.lmc"', text)) + if state['corrupt']: (work/'PCB/Layout/LayoutDB.lyt').write_bytes(b'wrong physical design') + state['entries'] = [{'name': job['name'], 'partition': job['partition'], + 'engineering_identity': engineering_identity(work/staged.name)}] + return {'success': True, 'safe_to_continue': True, 'identity_matches': False} + if job['operation'] == 'remove': + state['entries'] = [] + shutil.rmtree(library.parent/'ReusableBlocksLP'/job['name']) + return {'success': True, 'safe_to_continue': True} + pytest.fail('unexpected native operation') + monkeypatch.setattr(native.NativeManagementAdapter, '_worker', worker) + request = ManagementRequest('add', library, 'Block', source=source, auto_verify=False, + evidence_dir=tmp_path/'evidence') + yield root, request, state + from cbb_refresh_verify.library_lock import LibraryLock, _owners, _guard, blocker_path + with _guard: + owner = _owners.pop(LibraryLock(library).key, None) + if owner: owner['handle'].Close() + blocker_path(library).unlink(missing_ok=True) + + +def execute(root, request): + from cbb_refresh_verify.native_management import run_owned_fixture_management + return run_owned_fixture_management(request, root, probe_token='owned', idle_check=lambda *_: None) + + +def test_owned_fixture_engine_add_conflict_update_remove_and_identity(transaction): + from cbb_refresh_verify.native_engineering_identity import project_library + root, request, state = transaction + original = request.source.read_bytes() + assert execute(root, request).status == 'UPLOADED' + registered = request.library.parent/'ReusableBlocksLP/Block/Work/Source.prj' + assert project_library(registered) == request.library.resolve() + calls = len(state['calls']) + assert execute(root, request).status == 'UPLOAD_FAILED' + assert state['calls'][calls:] == ['catalog'] + (request.source.parent/'PCB/Layout/LayoutDB.lyt').write_bytes(b'replacement physical design') + assert execute(root, replace(request, operation='update')).status == 'UPLOADED' + assert (registered.parent/'PCB/Layout/LayoutDB.lyt').read_bytes() == b'replacement physical design' + assert execute(root, replace(request, operation='remove', source=None)).status == 'REMOVED' + assert execute(root, request).status == 'UPLOADED' + assert request.source.read_bytes() == original + + +def test_native_registered_but_asset_mismatch_retains_recovery(transaction): + root, request, state = transaction + state['corrupt'] = True + result = execute(root, request) + assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue + + +def test_update_readd_failure_preserves_snapshot_and_removed_checkpoint(transaction): + root, request, state = transaction + assert execute(root, request).status == 'UPLOADED' + state['fail_add'] = True + result = execute(root, replace(request, operation='update')) + assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue + assert (result.evidence_path.parent/'library-before/ReusableBlocksLP/Block/Work/Source.prj').exists() + assert 'removed' in [entry['stage'] for entry in json.loads(result.evidence_path.read_text())['history']] + + +def test_fixture_grant_cannot_authorize_arbitrary_library(transaction, tmp_path): + from cbb_refresh_verify.native_management import run_owned_fixture_management + root, request, state = transaction + other = tmp_path/'production.lmc'; other.write_bytes(b'unchanged') + with pytest.raises(ValueError): + run_owned_fixture_management(replace(request, library=other), root, probe_token='owned') + assert other.read_bytes() == b'unchanged' and not state['calls'] + + +@pytest.mark.parametrize('tamper', ['nonce', 'supervisor', 'backup', 'source']) +def test_worker_rejects_modified_management_authority_before_add(transaction, tamper): + root, request, state = transaction + state['tamper'] = tamper + original = request.source.read_bytes() + result = execute(root, request) + assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue + assert state['entries'] == [] and request.source.read_bytes() == original diff --git a/tests/test_native_management.py b/tests/test_native_management.py index 72826d1..eb593ef 100644 --- a/tests/test_native_management.py +++ b/tests/test_native_management.py @@ -24,6 +24,24 @@ def test_public_add_cannot_bypass_disabled_capability(tmp_path, monkeypatch): assert not result.success and result.safe_to_continue +def test_empty_fixture_creation_uses_existing_new_library_guard_route(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + seen = [] + monkeypatch.setattr(native.NativeManagementAdapter, '_binary_capability', lambda _: '') + monkeypatch.setattr(native.NativeManagementAdapter, '_worker', + lambda adapter, job, evidence: (seen.append((job, evidence)) or + {'success': True, 'safe_to_continue': True, 'entries': []})) + root = tmp_path/'fresh' + result = native.create_owned_fixture(root) + assert result['success'] and result['entries'] == [] + assert seen[0][0]['operation'] == 'probe_create' + assert Path(seen[0][0]['library']) == root/'native-add/fixture/mentor_lib.lmc' + assert seen[0][0]['probe_token'] == result['probe_token'] + existing = tmp_path/'existing'; existing.mkdir() + with pytest.raises(ValueError): native.create_owned_fixture(existing) + assert len(seen) == 1 + + def test_unprepared_add_rejected_before_native_activation(tmp_path, monkeypatch): import cbb_refresh_verify.native_management as native import win32com.client -- 2.39.5 From f21781f170915d46898605e157328cb975763bf3 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: Tue, 3 Nov 2026 12:24:54 +0800 Subject: [PATCH 35/59] fix: preserve unknown assets and retained export-copy ownership --- docs/releases/2026-11-03-native-crud.md | 4 + src/cbb_refresh_verify/library_lock.py | 19 ++- src/cbb_refresh_verify/maintenance_entry.py | 5 +- .../native_engineering_identity.py | 20 ++- src/cbb_refresh_verify/native_export_guard.py | 117 +++++++++++++++++ tests/test_management_native_transaction.py | 10 +- tests/test_native_engineering_identity.py | 29 +++++ tests/test_native_export_guard.py | 118 ++++++++++++++++++ 8 files changed, 312 insertions(+), 10 deletions(-) create mode 100644 src/cbb_refresh_verify/native_export_guard.py create mode 100644 tests/test_native_export_guard.py diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md index 691daba..c5386b0 100644 --- a/docs/releases/2026-11-03-native-crud.md +++ b/docs/releases/2026-11-03-native-crud.md @@ -17,3 +17,7 @@ The subsequent `mentor795-native-content-v1` contract requires pinned native hie The real management engine now captures a source baseline before any removal. Native Add requires a prepared transaction binding the verified full backup, staged source, baseline proof, exact target/name/partition, supervisor and exclusive lease. After normal native release, it corrects imported PRJ/PCB library references using the existing documented text helpers, reopens the exact native catalog and compares a target-only identity. Update remains remove plus Add; failures retain the recovery snapshot and checkpoint. The UI action is explicitly labeled “用工程替换已选 CBB…(原生重新导入)”; backup Work restoration is unchanged. Coordinator-native source/registered capture matched all three exports and primary assets after correcting the observed imported PCB reference; original source manifests were unchanged. Implementation verification remains file-only: 114 focused tests passed before the final bootstrap/lease refinement. `create_owned_fixture` and `run_owned_fixture_management` provide a token-bound empty-library/real-engine route for the coordinator's actual CRUD cycle. Production Add/Update remain disabled until that separate acceptance and promotion step. + +The coordinator's native engine cycle subsequently passed empty-library creation, Add A, duplicate refusal, same-name replacement by B (with a different verified layout hash), removal with strict absence, and reimport from a complete backup. Every operation reported safe native release and both original source manifests stayed unchanged; evidence is under `diagnostics/issue2-native-crud-20261103/engine-e2e`. + +Final safety review narrowed the additional iCDB runtime exclusions to known log directories, exact events files and the observed dated GUID log filenames. Unknown `cdbsvr` content remains primary, including unknown events/plog files, and a registered unknown-file change now requires recovery. Export capture binds its analysis copy to the existing library checkpoint before launch. That binding survives retention/restart and cannot be cleared by release or maintenance adoption until recorded processes/child handles have exited, known exporter/server processes are absent, copied addresses are absent and copied database files can be held exclusively. Missing/malformed ownership evidence remains blocked. The final focused safety/engine suite passed 167 tests; no native operations or full-suite run were performed by the implementation agent. Production promotion remains a separate reviewed change. diff --git a/src/cbb_refresh_verify/library_lock.py b/src/cbb_refresh_verify/library_lock.py index 6b1b173..8c4d1af 100644 --- a/src/cbb_refresh_verify/library_lock.py +++ b/src/cbb_refresh_verify/library_lock.py @@ -66,8 +66,11 @@ def has_blocker(library): def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id): path = blocker_path(library) pending = path.with_suffix('.pending') + previous = checkpoint_record(path) + export_record = ({'export_analysis': previous['export_analysis']} + if previous and previous['lease_id'] == lease_id and 'export_analysis' in previous else {}) with pending.open('w', encoding='utf-8') as stream: - json.dump({'protocol': 'cbb-refresh-verify-library-lease', 'version': 1, + json.dump({**export_record, 'protocol': 'cbb-refresh-verify-library-lease', 'version': 1, 'lease_id': lease_id, 'library': str(library), 'status': status, 'pid': os.getpid(), 'time': datetime.now(timezone.utc).isoformat()}, stream) stream.flush(); os.fsync(stream.fileno()) @@ -159,6 +162,9 @@ class LibraryLock: if not self.entered: return owner = _owners[self.key] + checkpoint = checkpoint_record(blocker_path(self.library)) + if checkpoint is not None and 'export_analysis' in checkpoint: + owner['retained'] = True if self.safe_to_release is not None: try: safe = self.safe_to_release() is True @@ -237,7 +243,8 @@ def _enter_maintenance(lease): 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) # All target/process/address/exclusive-file checks finish BEFORE # any old checkpoint replacement. Keep the exact sidecar open. - with lease.maintenance_guard() as proof: + from .native_export_guard import prove_export_copies_released + with prove_export_copies_released(lease.library), lease.maintenance_guard() as proof: if (marker.read_bytes() if marker.exists() else None) != old: raise LibraryBusy('维护检查期间检查点变化') if old is not None: @@ -284,6 +291,9 @@ def _prove_library_released(library): import pythoncom, win32com.client, win32con, win32file from .scoped_manifest import scan_tree from .native_management import _retained_workers + from .native_export_guard import assert_retained_exports_idle, prove_export_copies_released + from .native_engineering_identity import _OWNER_NAMES + assert_retained_exports_idle() if any(worker.poll() is None for worker in _retained_workers): raise LibraryBusy('原生管理工作进程尚未返回,不能解除协调阻止。') from .com_runtime import initialize_com, uninitialize_com @@ -291,7 +301,7 @@ def _prove_library_released(library): try: service = win32com.client.GetObject('winmgmts:') names = ('LibraryManager.exe', 'ExpeditionPCB.exe', 'viewdraw.exe', - 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe') + 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe', *sorted(_OWNER_NAMES)) query = 'SELECT ProcessId, Name FROM Win32_Process WHERE ' + ' OR '.join( f"Name='{name}'" for name in names) owners = [{'pid': int(item.ProcessId), 'name': str(item.Name)} for item in service.ExecQuery(query)] @@ -307,11 +317,12 @@ def _prove_library_released(library): if Path(library).resolve() not in native: raise LibraryBusy('目标库身份缺失,不能解除协调阻止。') with ExitStack() as stack: + export_copies = stack.enter_context(prove_export_copies_released(library)) for path in native: handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) stack.callback(handle.Close) - yield {'native_owners': owners, 'addresses': addresses, + yield {'native_owners': owners, 'addresses': addresses, 'export_copies': export_copies, 'exclusive_files': [str(path) for path in native]} finally: uninitialize_com(initialized) diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index e11703e..62c94ad 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -15,11 +15,14 @@ from uuid import uuid4 def assert_native_idle(): import win32com.client 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') + 'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES)) service = win32com.client.GetObject('winmgmts:') query = 'SELECT ProcessId, Name, CommandLine FROM Win32_Process WHERE ' + ' OR '.join( f"Name='{name}'" for name in names) diff --git a/src/cbb_refresh_verify/native_engineering_identity.py b/src/cbb_refresh_verify/native_engineering_identity.py index 4cc6c16..4528d28 100644 --- a/src/cbb_refresh_verify/native_engineering_identity.py +++ b/src/cbb_refresh_verify/native_engineering_identity.py @@ -203,7 +203,17 @@ def asset_identity(source, expected_library): assets = {} for relative, record in files.items(): path = root/relative - if path == main_database/'icdb.dat' or any(path.is_relative_to(p) for p in runtime): + known_runtime = False + for server_root in runtime: + if not path.is_relative_to(server_root): continue + parts = path.relative_to(server_root).parts + folded = tuple(p.casefold() for p in parts) + known_runtime = (len(parts) > 1 and folded[0] in ('log', 'dbcheck', 'dbrestore', 'dbupgrade', 'pimlog') + or folded in (('events', 'events.dat'), ('events', 'events.idx')) + or len(parts) == 2 and folded[0] == 'plog' + and re.fullmatch(r'\d{4}\.\d{2}\.\d{2}\.guid\.\d+\.txt', parts[1]) is not None) + break + if path == main_database/'icdb.dat' or known_runtime: continue if path in projects: data = _normalized_project(path, expected_library) @@ -273,6 +283,7 @@ def _run_exports(project, root, mentor_home, timeout_seconds): """Observe exact exporter/descendant handles; never kill a database owner.""" import win32api, win32con, win32event, pywintypes root = Path(root) + _write_json(root/'export-launch.json', {'started': False}) before = _process_snapshot() if any(p['name'].casefold() in _OWNER_NAMES for p in before): return {'success': False, 'safe_to_continue': False, 'detail': 'An existing iCDB owner prevents isolated identity export.'} @@ -300,6 +311,7 @@ def _run_exports(project, root, mentor_home, timeout_seconds): try: deadline = time.monotonic()+float(timeout_seconds) with (root/(kind+'-stdout.txt')).open('wb') as output: + _write_json(root/'export-launch.json', {'started': True}) process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT, creationflags=subprocess.CREATE_NO_WINDOW) record['pid'] = process.pid @@ -348,6 +360,7 @@ def capture_identity(source, evidence_root, *, expected_library, owner_library, from .backup_recovery import _align_library from .library_lock import LibraryLock, owned_snapshot from .scoped_manifest import copy_verified + from .native_export_guard import begin_export_analysis, finish_export_analysis import win32event, win32con source, pcb, top = source_project(source) root = Path(evidence_root).absolute() @@ -375,10 +388,13 @@ def capture_identity(source, evidence_root, *, expected_library, owner_library, _align_library(clone, clone_pcb, Path(expected_library)) if any((root/'project').rglob('sAddress.adr')): raise ValueError('Analysis source contains an unresolved database address.') + export_token = begin_export_analysis(library, root, clone) owner_safe = False native = _run_exports(clone, root, mentor_home, timeout_seconds) - owner_safe = native.get('safe_to_continue') is True _write_json(root/'native-outcome.json', native) + if native.get('safe_to_continue') is True: + finish_export_analysis(library, export_token) + owner_safe = True if not owner_safe or not native.get('success'): outcome = IdentityOutcome(None, owner_safe, native.get('detail', 'Native export failed.'), root) else: diff --git a/src/cbb_refresh_verify/native_export_guard.py b/src/cbb_refresh_verify/native_export_guard.py new file mode 100644 index 0000000..1470abb --- /dev/null +++ b/src/cbb_refresh_verify/native_export_guard.py @@ -0,0 +1,117 @@ +"""Export-copy ownership bound to the existing per-library lease checkpoint.""" +from contextlib import contextmanager, ExitStack +import json +from pathlib import Path +from uuid import uuid4 + +from .management import _write_json + + +def assert_retained_exports_idle(): + import win32con, win32event + from .native_engineering_identity import _retained_exports + from .library_lock import LibraryBusy + for process, handles in _retained_exports: + if process.poll() is None or any(win32event.WaitForSingleObject(handle, 0) != win32con.WAIT_OBJECT_0 + for handle in handles.values()): + raise LibraryBusy('Native identity exporter or descendant has not released.') + + +def begin_export_analysis(library, root, project): + from .library_lock import _guard, blocker_path, checkpoint_record, LibraryBusy + from .management_transaction import current_lease + library, root, project = Path(library).resolve(), Path(root).resolve(), Path(project).resolve() + owner = current_lease(library) + if (project.parent != root/'project' or not project.is_file() + or root.is_relative_to(library.parent) or library.parent.is_relative_to(root)): + raise LibraryBusy('Export-copy ownership paths are not independent and exact.') + record = {'protocol': 'cbb-native-export-analysis', 'version': 1, 'token': uuid4().hex, + 'library': str(library), 'lease_id': owner['lease_id'], 'supervisor_pid': owner['supervisor_pid'], + 'analysis_root': str(root), 'project': str(project)} + with _guard: + marker = blocker_path(library) + checkpoint = checkpoint_record(marker) + if (checkpoint is None or checkpoint['lease_id'] != owner['lease_id'] + or 'export_analysis' in checkpoint): + raise LibraryBusy('A prior export analysis is unresolved or the lease changed.') + _write_json(root/'export-owner.json', record) + _write_json(marker, {**checkpoint, 'export_analysis': [record]}) + return record['token'] + + +@contextmanager +def prove_export_copies_released(library): + """Fresh process and exclusive-copy proof, including after interpreter exit.""" + import win32file, win32con, pywintypes + from .library_lock import blocker_path, checkpoint_record, _supervisor_alive, LibraryBusy + from .native_engineering_identity import _process_snapshot, _OWNER_NAMES + from .scoped_manifest import scan_tree + assert_retained_exports_idle() + library = Path(library).resolve() + marker = blocker_path(library) + checkpoint = checkpoint_record(marker) + if marker.exists() and checkpoint is None: + raise LibraryBusy('Invalid library checkpoint cannot release export ownership.') + if checkpoint is None or 'export_analysis' not in checkpoint: + yield [] + return + records = checkpoint['export_analysis'] + if not isinstance(records, list) or not records: + raise LibraryBusy('Invalid export-analysis ownership record.') + owners = [p for p in _process_snapshot() if p['name'].casefold() in _OWNER_NAMES] + if owners: raise LibraryBusy(f'Native identity owners remain active: {owners}') + copies = [] + with ExitStack() as stack: + for record in records: + try: + root, project = Path(record['analysis_root']), Path(record['project']) + if (record.get('protocol') != 'cbb-native-export-analysis' or record.get('version') != 1 + or not record.get('token') or record['lease_id'] != checkpoint['lease_id'] + or record['supervisor_pid'] != checkpoint['pid'] + or Path(record['library']).resolve() != library + or not root.is_absolute() or not project.is_absolute() or root.resolve() != root + or project.parent != root/'project' or not project.is_file() + or root.resolve().is_relative_to(library.parent) + or library.parent.is_relative_to(root.resolve()) + or json.loads((root/'export-owner.json').read_text(encoding='utf-8')) != record): + raise ValueError('Export record does not match the exact library, lease and copy.') + processes = [root/(kind+'-process.json') for kind in ('ascii', 'csv', 'edif') + if (root/(kind+'-process.json')).exists()] + if not processes: + launch = json.loads((root/'export-launch.json').read_text(encoding='utf-8')) + if launch != {'started': False}: + raise ValueError('Export launch has no confirmed process record.') + for path in processes: + process = json.loads(path.read_text(encoding='utf-8')) + pids = process.get('observed_pids', []) + [process['pid']] + if any(not isinstance(pid, int) or pid <= 0 for pid in pids): + raise ValueError('Invalid export process identity.') + if any(_supervisor_alive(pid) for pid in pids): + raise LibraryBusy('A recorded export process or descendant remains alive.') + entries = scan_tree(root/'project') + if any(path.name.casefold() == 'saddress.adr' for path in entries): + raise LibraryBusy('The retained analysis copy still has a database address.') + databases = [path for path in entries if path.is_file() + and path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')] + if not any(path.name.casefold() == 'icdb.dat' for path in databases): + raise ValueError('The recorded analysis database is missing.') + for path in databases: + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, 0, None) + stack.callback(handle.Close) + copies.append(str(root/'project')) + except (KeyError, TypeError, ValueError, OSError, pywintypes.error) as error: + raise LibraryBusy(f'Export analysis release is not proven: {error}') from error + yield copies + + +def finish_export_analysis(library, token): + from .library_lock import _guard, blocker_path, checkpoint_record, LibraryBusy + with _guard, prove_export_copies_released(library): + marker = blocker_path(library) + checkpoint = checkpoint_record(marker) + records = checkpoint.get('export_analysis', []) if checkpoint else [] + if len(records) != 1 or records[0].get('token') != token: + raise LibraryBusy('Export ownership changed before confirmed cleanup.') + del checkpoint['export_analysis'] + _write_json(marker, checkpoint) diff --git a/tests/test_management_native_transaction.py b/tests/test_management_native_transaction.py index 99e0781..12c5890 100644 --- a/tests/test_management_native_transaction.py +++ b/tests/test_management_native_transaction.py @@ -56,7 +56,10 @@ def transaction(tmp_path, monkeypatch): text = pcb_project.read_text() import re pcb_project.write_text(re.sub(r'KEY CentralLibrary "[^"]*"', 'KEY CentralLibrary "wrong-relative.lmc"', text)) - if state['corrupt']: (work/'PCB/Layout/LayoutDB.lyt').write_bytes(b'wrong physical design') + if state['corrupt'] == 'unknown_cdbsvr': + unknown = work/'database/cdbsvr/persistent.dat'; unknown.parent.mkdir(parents=True, exist_ok=True) + unknown.write_bytes(b'unexpected engineering content') + elif state['corrupt']: (work/'PCB/Layout/LayoutDB.lyt').write_bytes(b'wrong physical design') state['entries'] = [{'name': job['name'], 'partition': job['partition'], 'engineering_identity': engineering_identity(work/staged.name)}] return {'success': True, 'safe_to_continue': True, 'identity_matches': False} @@ -99,9 +102,10 @@ def test_owned_fixture_engine_add_conflict_update_remove_and_identity(transactio assert request.source.read_bytes() == original -def test_native_registered_but_asset_mismatch_retains_recovery(transaction): +@pytest.mark.parametrize('corrupt', [True, 'unknown_cdbsvr']) +def test_native_registered_but_asset_mismatch_retains_recovery(transaction, corrupt): root, request, state = transaction - state['corrupt'] = True + state['corrupt'] = corrupt result = execute(root, request) assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue diff --git a/tests/test_native_engineering_identity.py b/tests/test_native_engineering_identity.py index aba6c69..2d2f93b 100644 --- a/tests/test_native_engineering_identity.py +++ b/tests/test_native_engineering_identity.py @@ -72,6 +72,33 @@ def test_assets_require_correct_library_references_and_unknown_bytes(project): asset_identity(source, library) +@pytest.mark.parametrize('relative', ['database/cdbsvr/persistent.dat', + 'database/cdbsvr/new-engineering/config.bin', 'database/cdbsvr/events/custom.dat', + 'database/cdbsvr/plog/unknown-engineering.bin']) +def test_unknown_cdbsvr_assets_remain_part_of_identity(project, relative): + from cbb_refresh_verify.native_engineering_identity import asset_identity + source, library = project + baseline = asset_identity(source, library) + unknown = source.parent/relative; unknown.parent.mkdir(parents=True, exist_ok=True) + unknown.write_bytes(b'first engineering content') + added = asset_identity(source, library) + assert added != baseline + unknown.write_bytes(b'changed engineering content') + assert asset_identity(source, library) != added + unknown.unlink() + assert asset_identity(source, library) == baseline + + +def test_observed_guid_and_events_runtime_does_not_change_identity(project): + from cbb_refresh_verify.native_engineering_identity import asset_identity + source, library = project + baseline = asset_identity(source, library) + for relative in ['database/cdbsvr/plog/2026.11.03.guid.48432.txt', + 'database/cdbsvr/events/events.dat', 'database/cdbsvr/events/events.idx']: + path = source.parent/relative; path.parent.mkdir(parents=True, exist_ok=True); path.write_bytes(b'runtime') + assert asset_identity(source, library) == baseline + + @pytest.mark.parametrize('failure', ['empty', 'missing_completion', 'error', 'exit']) def test_export_evidence_requires_successful_complete_native_output(tmp_path, failure): from cbb_refresh_verify.native_engineering_identity import validate_export_output @@ -94,11 +121,13 @@ def test_capture_only_exports_fresh_copy_and_leaves_source_unchanged(project, tm source, library = project original = build_manifest(source.parent) monkeypatch.setattr(identity, 'check_exporters', lambda _: None) + monkeypatch.setattr(identity, '_process_snapshot', lambda: []) def exports(clone, root, *_): assert clone.parent == root/'project' and clone != source (clone.parent/'database/icdb.dat').write_bytes(b'export changed disposable copy') (root/'design.rpt').write_bytes(ascii_report()) (root/'design.edf').write_bytes(edif_report()) + (root/'export-launch.json').write_text('{"started": false}') return {'success': True, 'safe_to_continue': True, 'detail': ''} monkeypatch.setattr(identity, '_run_exports', exports) monkeypatch.setattr(identity, 'csv_identity', lambda _: {'work/identity': 'csv proof'}) diff --git a/tests/test_native_export_guard.py b/tests/test_native_export_guard.py new file mode 100644 index 0000000..2fc4786 --- /dev/null +++ b/tests/test_native_export_guard.py @@ -0,0 +1,118 @@ +"""Fresh release/adoption proof for retained export copies; no native launch.""" +from contextlib import contextmanager +import json +import os +from types import SimpleNamespace + +import pytest + + +@pytest.fixture +def export_case(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.native_engineering_identity as identity + import win32com.client + monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=lambda *_: [])) + monkeypatch.setattr(identity, '_process_snapshot', lambda: []) + monkeypatch.setattr(identity, '_retained_exports', []) + monkeypatch.setattr(locks, '_supervisor_alive', lambda _: False) + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.write_bytes(b'original library') + root = tmp_path/'analysis'; (root/'project/database').mkdir(parents=True) + source = root/'project/Source.prj'; source.write_bytes(b'copied project') + (root/'project/database/icdb.dat').write_bytes(b'copied database') + lease = locks.LibraryLock(library, safe_to_release=lambda: False) + with lease: + from cbb_refresh_verify.native_export_guard import begin_export_analysis + token = begin_export_analysis(library, root, source) + (root/'ascii-process.json').write_text(json.dumps({'pid': 123456, 'observed_pids': [123456], + 'command': ['icdb2ascii.exe'], 'safe_to_continue': False})) + yield library, root, token, lease + with locks._guard: + owner = locks._owners.pop(lease.key, None) + if owner: owner['handle'].Close() + locks.blocker_path(library).unlink(missing_ok=True) + + +@pytest.mark.parametrize('action', ['release', 'maintenance']) +@pytest.mark.parametrize('obstacle', ['active_exporter', 'active_child', 'copy_address', 'copy_exclusive', 'invalid_record', 'missing_copy']) +def test_release_and_takeover_cannot_bypass_export_copy(export_case, monkeypatch, tmp_path, action, obstacle): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.native_engineering_identity as identity + import win32api, win32con, win32file + library, root, token, lease = export_case + handle = None + if obstacle == 'active_exporter': + # Restart case: no in-memory exporter, but a known native owner remains. + monkeypatch.setattr(identity, '_process_snapshot', lambda: [ + {'pid': 123456, 'name': 'iCDBNetServer.exe', 'parent': 0, 'executable': 'native'}]) + if obstacle == 'active_child': + handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, os.getpid()) + monkeypatch.setattr(identity, '_retained_exports', [(SimpleNamespace(poll=lambda: 0), {os.getpid(): handle})]) + if obstacle == 'copy_address': + address = root/'project/database/cdbsvr/sAddress.adr'; address.parent.mkdir(); address.write_bytes(b'copy owner') + if obstacle == 'copy_exclusive': + handle = win32file.CreateFile(str(root/'project/database/icdb.dat'), win32con.GENERIC_READ, + 0, None, win32con.OPEN_EXISTING, 0, None) + if obstacle == 'invalid_record': (root/'export-owner.json').write_text('{invalid') + if obstacle == 'missing_copy': (root/'project/database/icdb.dat').unlink() + @contextmanager + def target_guard(): yield {'target': 'file-only maintenance proof'} + try: + marker = locks.blocker_path(library); before = marker.read_bytes() + with pytest.raises((ValueError, OSError)): + if action == 'release': locks.release_retained_library(library, tmp_path/'release') + else: + with locks.LibraryLock.for_maintenance(library, tmp_path/'takeover', target_guard, + safe_to_release=lambda: True): pass + assert marker.read_bytes() == before + assert lease.path.exists() + finally: + if handle: handle.Close() + + +@pytest.mark.parametrize('action', ['release', 'maintenance']) +def test_restarted_export_copy_can_release_only_after_fresh_proof(export_case, tmp_path, action): + import cbb_refresh_verify.library_lock as locks + library, root, token, lease = export_case + # Simulate interpreter exit: all process-local registries/lease handles lost. + with locks._guard: locks._owners.pop(lease.key)['handle'].Close() + @contextmanager + def target_guard(): yield {'target': 'file-only maintenance proof'} + if action == 'release': + assert locks.release_retained_library(library, tmp_path/'release')['status'] == 'RELEASE_CONFIRMED' + else: + with locks.LibraryLock.for_maintenance(library, tmp_path/'takeover', target_guard, + safe_to_release=lambda: True): pass + assert not locks.blocker_path(library).exists() + assert (root/'project/database/icdb.dat').read_bytes() == b'copied database' + + +def test_maintenance_idle_guard_recognizes_new_exporter_names(monkeypatch): + import win32com.client + from cbb_refresh_verify.maintenance_entry import assert_native_idle + def query(sql): + assert 'icdb2ascii.exe' in sql.casefold() and 'icdbnetserver.exe' in sql.casefold() + return [SimpleNamespace(ProcessId=123456, Name='icdb2ascii.exe', CommandLine='export')] + monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=query)) + with pytest.raises(ValueError, match='MAINTENANCE_OWNER_ACTIVE'): assert_native_idle() + + +def test_successful_export_clears_only_its_bound_guard_after_fresh_copy_proof(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.native_engineering_identity as identity + from cbb_refresh_verify.native_export_guard import begin_export_analysis, finish_export_analysis + monkeypatch.setattr(identity, '_process_snapshot', lambda: []) + monkeypatch.setattr(identity, '_retained_exports', []) + monkeypatch.setattr(locks, '_supervisor_alive', lambda _: False) + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() + root = tmp_path/'analysis'; (root/'project/database').mkdir(parents=True) + project = root/'project/Source.prj'; project.touch() + (project.parent/'database/icdb.dat').write_bytes(b'copied db') + with locks.LibraryLock(library): + token = begin_export_analysis(library, root, project) + marker = locks.blocker_path(library) + assert json.loads(marker.read_text())['export_analysis'][0]['token'] == token + (root/'export-launch.json').write_text('{"started": false}') + finish_export_analysis(library, token) + assert 'export_analysis' not in json.loads(marker.read_text()) + assert not marker.exists() -- 2.39.5 From e915aaa8659bbc00b9ab596b028c36f8d681bfbe 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: Tue, 3 Nov 2026 12:29:42 +0800 Subject: [PATCH 36/59] feat: enable validated pinned native Add and replacement routes --- docs/releases/2026-11-03-native-crud.md | 6 +++- .../native_engineering_identity.py | 2 +- src/cbb_refresh_verify/native_management.py | 25 ++++++++----- .../ui/management_dialog.py | 1 + tests/test_management_ui.py | 3 ++ tests/test_native_management.py | 35 ++++++++++++++++++- 6 files changed, 60 insertions(+), 12 deletions(-) diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md index c5386b0..e2a9777 100644 --- a/docs/releases/2026-11-03-native-crud.md +++ b/docs/releases/2026-11-03-native-crud.md @@ -1,6 +1,6 @@ # Native CRUD integration and review follow-up -The shared native Add transport uses the proven private-desktop UIA executor, bounded field readback and exact native success message. It closes/reopens the library before comparing exact catalog name/partition and raw engineering identity. Native registration with different engineering bytes returns `success=True`, `identity_matches=False`; missing registration is a native failure. Production Add and Update remain disabled pending independent engineering identity proof. +The shared native Add transport uses the proven private-desktop UIA executor, bounded field readback and exact native success message. It closes/reopens the library before comparing exact catalog name/partition and engineering identity. Missing registration or mismatched native logical/drawing/physical proof cannot become an accepted upload. Production Add/Update are now enabled only with the exact pinned EE7.9.5 core and all three identity exporters, for ASCII target names in the proven DxDesigner / Logical-Physical scope. Every mutation still requires the prepared engine transaction and complete recovery snapshot. The review of 07a9c1e identified that route readiness alone did not authorize existing-library mutation. The follow-up makes public `add()` refuse before reading a source or launching a worker. Research callers must use `add_owned_fixture(..., probe_token=...)` against the fixed library created by the diagnostic workflow and matching probe-owner record. It requires the calling thread's live library lease, checks its lease ID against the checkpoint, owns the management mutex during submission, creates a fresh staged source copy and verified library snapshot, and binds the transaction nonce to target, name, partition, source identity and manifests. @@ -21,3 +21,7 @@ Coordinator-native source/registered capture matched all three exports and prima The coordinator's native engine cycle subsequently passed empty-library creation, Add A, duplicate refusal, same-name replacement by B (with a different verified layout hash), removal with strict absence, and reimport from a complete backup. Every operation reported safe native release and both original source manifests stayed unchanged; evidence is under `diagnostics/issue2-native-crud-20261103/engine-e2e`. Final safety review narrowed the additional iCDB runtime exclusions to known log directories, exact events files and the observed dated GUID log filenames. Unknown `cdbsvr` content remains primary, including unknown events/plog files, and a registered unknown-file change now requires recovery. Export capture binds its analysis copy to the existing library checkpoint before launch. That binding survives retention/restart and cannot be cleared by release or maintenance adoption until recorded processes/child handles have exited, known exporter/server processes are absent, copied addresses are absent and copied database files can be held exclusively. Missing/malformed ownership evidence remains blocked. The final focused safety/engine suite passed 167 tests; no native operations or full-suite run were performed by the implementation agent. Production promotion remains a separate reviewed change. + +After scoped review found both safety issues corrected, the separate capability-promotion change enabled the public prepared engine routes. The basis is the accepted native cycle in `engine-e2e/cycle-result.json` plus the matching integrated three-export/asset identity evidence. That cycle deliberately used `auto_verify=False`: it proves native registration/replacement/removal/reimport and content identity, not the separate default Verify result or frozen UI acceptance. Unsupported exporter versions, flows, partitions or export formats still fail closed. Replacement remains non-atomic Remove+Add with recovery evidence, and arbitrary unexported CES/version equivalence is not claimed. + +The promotion focused suite passed 93 tests, including every required binary mismatch, unsupported flow/partition, engine identity/recovery and UI progress behavior. Native default description is blank; progress reads “正在核对来源工程内容”. Source input and backup Work restoration behavior are unchanged. Full-suite/build and fresh public frozen-UI acceptance remain coordinator-owned. diff --git a/src/cbb_refresh_verify/native_engineering_identity.py b/src/cbb_refresh_verify/native_engineering_identity.py index 4528d28..d7d7f7e 100644 --- a/src/cbb_refresh_verify/native_engineering_identity.py +++ b/src/cbb_refresh_verify/native_engineering_identity.py @@ -17,7 +17,7 @@ from .scoped_manifest import ManifestScope, build_manifest, hash_file POLICY = 'mentor795-native-content-v1' COVERAGE = {'logical_connectivity_and_properties': 'native hierarchy/flat ASCII and CSV', 'physical_design_and_primary_assets': 'raw SHA256', - 'schematic_graphical_geometry': 'native EDIF schematic', 'production_acceptance': False} + 'schematic_graphical_geometry': 'native EDIF schematic', 'production_acceptance': True} EXPORTER_PROOF = { 'icdb2ascii.exe': 'e37c87356eaf57c0b97e4183458bc8c5eb6eb0ac497cc7ad40dcd585d8cd213b', 'icdb2csv.exe': '22059f4a432e8eabf30a5326ebcbd463ed6fa6495af21d7aad5ab5d2f892f255', diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 9bc581a..70cd864 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -21,9 +21,10 @@ from .scoped_manifest import hash_file from .network_path import absolute_preserving_drive _retained_workers = [] -# Promotion requires coordinator-owned native CRUD evidence, in a separate change. -_PRODUCTION_ADD_VALIDATED = False -_PRODUCTION_UPDATE_VALIDATED = False +# Coordinator-owned Create/Add/conflict/update/remove/backup-reimport evidence: +# diagnostics/issue2-native-crud-20261103/engine-e2e/cycle-result.json +_PRODUCTION_ADD_VALIDATED = True +_PRODUCTION_UPDATE_VALIDATED = True _BINARY_PROOF = { 'lm/win32/bin/CleanWGLib.exe': '7808ded326555f1fd5d5c14f68803dbb4f970d624f9ad6e1c7982dc9ed867917', 'lm/win32/bin/LibraryManager.exe': 'a9c4c3d806794dbdc56d2bc4c637a1cf25e87e98644a7d5fd4eb0067ca5845e0', @@ -85,16 +86,22 @@ class NativeManagementAdapter: return { 'remove': Capability(not reason, reason or 'CleanWGLib/F4 route ready.'), 'add': Capability(not reason, reason or 'Library Manager Logical-Physical Add route ready.'), - 'update': Capability(False, 'Update remains remove plus Add while identity readback is gated.'), + 'update': Capability(False, 'Update is composed from Remove and Add; there is no separate native Update call.'), } def capabilities(self): reason = self._binary_capability() + add_reason = reason + if not add_reason: + from .native_engineering_identity import check_exporters + try: check_exporters(self.home) + except (ValueError, OSError, TypeError) as error: add_reason = str(error) return { 'remove': Capability(not reason, reason or '已验证 CleanWGLib/F4;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'), - 'add': Capability(not reason and _PRODUCTION_ADD_VALIDATED, '原生新增及工程身份已有隔离证据;完整事务验证通过前生产新增保持禁用。'), - 'update': Capability(not reason and _PRODUCTION_ADD_VALIDATED and _PRODUCTION_UPDATE_VALIDATED, - '同名更新需要原生移除、重新导入及完整端到端隔离验证,目前不可用。'), + 'add': Capability(not add_reason and _PRODUCTION_ADD_VALIDATED, add_reason or + '已验证 EE7.9.5 原生导入及完整工程复读;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'), + 'update': Capability(not add_reason and _PRODUCTION_ADD_VALIDATED and _PRODUCTION_UPDATE_VALIDATED, + add_reason or '已验证同名原生替换:完整备份后移除并重新导入,失败时保留恢复证据。'), } def _worker(self, job, evidence): @@ -198,7 +205,7 @@ class NativeManagementAdapter: return NativeOutcome(bool(result.get('success')), bool(result.get('safe_to_continue')), result.get('detail', '')) def preflight(self, request, catalog): - if request.operation in ('remove', 'update'): + if request.operation in ('add', 'remove', 'update'): removal_input(request.name, request.partition) if request.partition != 'Logical-Physical' or catalog.flow != 1: raise ValueError('仅验证了 DxDesigner / Logical-Physical 管理操作。') @@ -656,7 +663,7 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) win32gui.PostMessage(new_button, win32con.BM_CLICK, 0, 0) dialog = await_window('New Reusable Block') fields = {1217: str(source), 1215: job['name'], 1218: top, - 1216: job.get('description', 'CBB native management validation')} + 1216: job.get('description', '')} for control, value in fields.items(): hwnd = win32gui.GetDlgItem(dialog, control) if not hwnd or win32gui.GetClassName(hwnd) != 'Edit': diff --git a/src/cbb_refresh_verify/ui/management_dialog.py b/src/cbb_refresh_verify/ui/management_dialog.py index 0f9d92e..fc0542c 100644 --- a/src/cbb_refresh_verify/ui/management_dialog.py +++ b/src/cbb_refresh_verify/ui/management_dialog.py @@ -17,6 +17,7 @@ RESULT_LABELS = { 'BLOCKED': '操作被阻止', 'UNSUPPORTED': '当前环境不支持', } STAGE_LABELS = { + 'prepare_identity': '正在核对来源工程内容', 'catalog_before': '正在核对原生目录', 'backup': '正在备份完整中心库', 'stage_source': '正在复制并检查来源工程', 'native_remove': '正在原生移除', 'removed': '原条目已移除', 'native_add': '正在原生上传', 'uploaded': '上传身份已确认', diff --git a/tests/test_management_ui.py b/tests/test_management_ui.py index 9074555..486e603 100644 --- a/tests/test_management_ui.py +++ b/tests/test_management_ui.py @@ -61,6 +61,9 @@ def test_add_dialog_default_auto_verify_and_source_identity_rows(qtbot, tmp_path assert all(r.auto_verify and r.source.suffix == '.prj' for r in requests) dialog.auto_verify.setChecked(False) assert not any(r.auto_verify for r in dialog.requests()) + dialog._progress({'stage': 'prepare_identity', 'block': 'One'}) + assert '正在核对来源工程内容' in dialog.status_label.text() + assert 'prepare_identity' not in dialog.status_label.text() def test_restart_shows_unfinished_without_starting_worker(qtbot, tmp_path): diff --git a/tests/test_native_management.py b/tests/test_native_management.py index eb593ef..824a7b5 100644 --- a/tests/test_native_management.py +++ b/tests/test_native_management.py @@ -177,7 +177,40 @@ def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_p adapter = native.NativeManagementAdapter(mentor_home=tmp_path) assert adapter.route_capabilities()['add'].supported assert not adapter.capabilities()['add'].supported - assert '身份' in adapter.capabilities()['add'].reason + assert 'icdb2ascii' in adapter.capabilities()['add'].reason + + +@pytest.mark.parametrize('bad', [None, 'CleanWGLib.exe', 'LibraryManager.exe', 'LMCDLL70.dll', + 'LMCDocAddin70.dll', 'icdb2ascii.exe', 'icdb2csv.exe', 'edifExporter.exe']) +def test_public_crud_requires_pinned_core_and_every_identity_exporter(tmp_path, monkeypatch, bad): + import hashlib + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.native_engineering_identity as identity + core = {relative: hashlib.sha256(Path(relative).name.encode()).hexdigest() for relative in native._BINARY_PROOF} + exporters = {name: hashlib.sha256(name.encode()).hexdigest() for name in identity.EXPORTER_PROOF} + monkeypatch.setattr(native, '_BINARY_PROOF', core) + monkeypatch.setattr(identity, 'EXPORTER_PROOF', exporters) + for relative in core: + path = tmp_path/relative; path.parent.mkdir(parents=True, exist_ok=True); path.write_bytes(path.name.encode()) + for name in exporters: + path = tmp_path/('wv/win32/bin' if name == 'edifExporter.exe' else 'iCDB/win32/bin')/name + path.parent.mkdir(parents=True, exist_ok=True); path.write_bytes(name.encode()) + if bad: + next(tmp_path.rglob(bad)).write_bytes(b'changed binary') + caps = native.NativeManagementAdapter(mentor_home=tmp_path).capabilities() + assert caps['add'].supported is (bad is None) + assert caps['update'].supported is (bad is None) + if bad in exporters: assert caps['remove'].supported + + +@pytest.mark.parametrize('operation', ['add', 'update', 'remove']) +@pytest.mark.parametrize('flow,partition', [(2, 'Logical-Physical'), (1, 'Unsupported')]) +def test_public_crud_scope_refuses_unproven_flow_or_partition(tmp_path, operation, flow, partition): + from cbb_refresh_verify.management import ManagementRequest, Catalog + from cbb_refresh_verify.native_management import NativeManagementAdapter + request = ManagementRequest(operation, tmp_path/'lib.lmc', 'Block', partition=partition) + with pytest.raises(ValueError): + NativeManagementAdapter(mentor_home=tmp_path).preflight(request, Catalog(request.library, flow, ())) def test_owned_fixture_add_passes_existing_library_and_source_to_worker(tmp_path, monkeypatch): -- 2.39.5 From 9ac2f91dde200fc60d35720ae8d5c8d7d35c7a01 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: Tue, 3 Nov 2026 12:51:35 +0800 Subject: [PATCH 37/59] fix: reject legacy copy paths and surface native Add failures --- docs/releases/2026-11-03-native-crud.md | 4 + src/cbb_refresh_verify/native_add_failures.py | 110 ++++++++++++++++++ src/cbb_refresh_verify/native_management.py | 13 ++- tests/test_management_native_transaction.py | 13 +++ tests/test_native_add_failures.py | 109 +++++++++++++++++ 5 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 src/cbb_refresh_verify/native_add_failures.py create mode 100644 tests/test_native_add_failures.py diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md index e2a9777..ab4e3f2 100644 --- a/docs/releases/2026-11-03-native-crud.md +++ b/docs/releases/2026-11-03-native-crud.md @@ -25,3 +25,7 @@ Final safety review narrowed the additional iCDB runtime exclusions to known log After scoped review found both safety issues corrected, the separate capability-promotion change enabled the public prepared engine routes. The basis is the accepted native cycle in `engine-e2e/cycle-result.json` plus the matching integrated three-export/asset identity evidence. That cycle deliberately used `auto_verify=False`: it proves native registration/replacement/removal/reimport and content identity, not the separate default Verify result or frozen UI acceptance. Unsupported exporter versions, flows, partitions or export formats still fail closed. Replacement remains non-atomic Remove+Add with recovery evidence, and arbitrary unexported CES/version equivalence is not claimed. The promotion focused suite passed 93 tests, including every required binary mismatch, unsupported flow/partition, engine identity/recovery and UI progress behavior. Native default description is blank; progress reads “正在核对来源工程内容”. Source input and backup Work restoration behavior are unchanged. Full-suite/build and fresh public frozen-UI acceptance remain coordinator-owned. + +Frozen public Add exposed a confirmed legacy path limit: the staged tree contained a 264-character historical CES log path, versus a maximum of 253 in the successful engine cycle. Native output reported `Cannot copy iCDB base of Reusable Block project` and an owned “系统找不到指定的路径。” dialog. The coordinator reproduced Windows error 3 with legacy 32-bit `CopyFile` on that exact file; the same frozen executable, same target library and blank description then completed Add safely when only the operation data/staging root was shortened (`frozen-public-ui-shortpath-control/result.json`). + +The complete staged and projected target trees are now checked against the legacy 260-WCHAR path buffer before native identity capture or Update removal, and again before dialog submission. Unsupported paths produce an actionable shorter-data/target-root message; no source/log files are excluded or deleted. Native copy failure detection uses a bounded pre-submit log size/hash baseline and exact current block name, so old log errors do not fail later operations. The exact observed owned error dialog is recorded and acknowledged only through its known id-2 “确定” button; unknown prompts are recorded without confirmation. Normal child-first close and exact owner-release checks remain required. Confirmed errors now return promptly instead of waiting for the full operation timeout. Focused verification passed 142 tests, including unchanged source/history and no native Remove on long-path update rejection; final frozen acceptance remains coordinator-owned. diff --git a/src/cbb_refresh_verify/native_add_failures.py b/src/cbb_refresh_verify/native_add_failures.py new file mode 100644 index 0000000..e016c3d --- /dev/null +++ b/src/cbb_refresh_verify/native_add_failures.py @@ -0,0 +1,110 @@ +"""Observed native copy failure and conservative legacy Win32 path admission.""" +import hashlib +import json +from pathlib import Path +import re +import time + +_COPY_ERROR = b'Error! Cannot copy iCDB base of Reusable Block project.' +_LOG_LIMIT = 2*1024*1024 + + +def snapshot_add_log(library): + path = Path(library).parent/'LogFiles/ReuseBlockLogLO.txt' + try: + data = b'' + if path.exists(): + with path.open('rb') as stream: data = stream.read(_LOG_LIMIT+1) + if len(data) > _LOG_LIMIT: raise ValueError('Native Add log exceeds the bounded observation limit.') + return {'path': str(path), 'size': len(data), 'sha256': hashlib.sha256(data).hexdigest()} + except (OSError, ValueError) as error: + return {'path': str(path), 'unavailable': str(error)} + + +def fresh_add_failure(baseline, name): + if 'unavailable' in baseline: return None + try: + with Path(baseline['path']).open('rb') as stream: + data = stream.read(_LOG_LIMIT+1) + offset = baseline['size'] + if (len(data) > _LOG_LIMIT or len(data) < offset + or hashlib.sha256(data[:offset]).hexdigest() != baseline['sha256']): + return None + marker = re.compile(rb'REUSE BLOCK:\s*INFORMATION:\s*'+re.escape(name.encode('mbcs')) + +rb'\s+'+re.escape(_COPY_ERROR)) + for line in data[offset:].splitlines(): + if marker.search(line): return line.decode('mbcs') + except (OSError, UnicodeError): + pass + return None + + +def failure_ack_button(title, static_text, buttons): + if title != 'Library Manager' or static_text.strip() != '系统找不到指定的路径。' or len(buttons) != 1: + return None + button = buttons[0] + if (button.get('id') == 2 and button.get('class') == 'Button' + and button.get('text') == '确定' and button.get('enabled') is True): + return button['hwnd'] + return None + + +def validate_native_copy_paths(source, library, name): + from .scoped_manifest import scan_tree + from .network_path import absolute_preserving_drive + source = absolute_preserving_drive(source) + target = absolute_preserving_drive(library).parent/'ReusableBlocksLP'/name/'Work' + paths = [source.parent, *scan_tree(source.parent)] + for path in paths: + relative = path.relative_to(source.parent) + for label, candidate in (('暂存来源', path), ('目标', target/relative)): + # Legacy CopyFile's 260-WCHAR buffer includes the terminating NUL. + length = len(str(candidate).encode('utf-16-le'))//2 + if length >= 260: + raise ValueError(f'NATIVE_PATH_TOO_LONG: {label}路径为 {length} 个字符,已超过原生复制支持范围。' + f'请缩短操作数据/证据目录或目标库路径后重试;所有工程和日志文件均保留。路径:{candidate}') + + +def observe_add_failure(desktop, owned_pids, root, baseline, name, state): + """Record owned modal evidence and acknowledge only the observed error box.""" + import pythoncom, win32con, win32gui, win32process + from .management import _write_json + fresh_log = fresh_add_failure(baseline, name) + for handle in desktop.EnumDesktopWindows(): + hwnd = int(handle) + if (win32process.GetWindowThreadProcessId(hwnd)[1] not in owned_pids + or not win32gui.IsWindowVisible(hwnd) or win32gui.GetClassName(hwnd) != '#32770'): + continue + title = win32gui.GetWindowText(hwnd) + if title in ('New Reusable Block', 'Reusable Blocks'): continue + children = [] + win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(int(child)), None) + controls = [{'hwnd': child, 'id': win32gui.GetDlgCtrlID(child), 'class': win32gui.GetClassName(child), + 'text': win32gui.GetWindowText(child), 'enabled': bool(win32gui.IsWindowEnabled(child))} + for child in children] + static = '\n'.join(c['text'] for c in controls if c['class'] == 'Static') + buttons = [c for c in controls if c['class'] == 'Button'] + record = {'hwnd': hwnd, 'title': title, 'controls': controls, 'fresh_log_failure': fresh_log} + key = json.dumps(record, ensure_ascii=False, sort_keys=True) + if key not in state.setdefault('observed', set()): + state['observed'].add(key) + with (Path(root)/'native-add-dialogs.jsonl').open('a', encoding='utf-8') as stream: + stream.write(json.dumps(record, ensure_ascii=False)+'\n') + button = failure_ack_button(title, static, buttons) + if button is not None and win32process.GetWindowThreadProcessId(button)[1] in owned_pids: + win32gui.PostMessage(button, win32con.BM_CLICK, 0, 0) + deadline = time.monotonic()+2 + while win32gui.IsWindow(hwnd) and time.monotonic() < deadline: + pythoncom.PumpWaitingMessages(); time.sleep(.05) + record['acknowledged'] = not win32gui.IsWindow(hwnd) + _write_json(Path(root)/'native-add-output.json', {'success_message': None, 'failure': record}) + raise RuntimeError('NATIVE_ADD_FAILED: '+static.strip() + + (' '+fresh_log if fresh_log else '') + + ('' if record['acknowledged'] else ';原生错误对话框尚未确认关闭。')) + if fresh_log: + first_seen = state.setdefault('failure_seen_at', time.monotonic()) + # Allow the paired native error popup to appear for normal acknowledgement. + if time.monotonic()-first_seen >= 2: + _write_json(Path(root)/'native-add-output.json', {'success_message': None, + 'failure': {'fresh_log_failure': fresh_log, 'acknowledged': False}}) + raise RuntimeError('NATIVE_ADD_FAILED: '+fresh_log) diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 70cd864..b5a1c45 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -218,12 +218,17 @@ class NativeManagementAdapter: return self._add_transaction(request, Path(evidence)) def prepare_add_transaction(self, request, root, *, original_source): - from .native_engineering_identity import project_library, asset_identity, capture_identity + from .native_engineering_identity import project_library, asset_identity, capture_identity, IdentityOutcome + from .native_add_failures import validate_native_copy_paths from .management_transaction import current_lease, prepare_transaction from .management import source_project from .backup_recovery import _align_library if not self.capabilities()['add'].supported: raise ValueError(self.capabilities()['add'].reason) + try: + validate_native_copy_paths(request.source, request.library, request.name) + except ValueError as error: + return request, IdentityOutcome(None, True, str(error), Path(root)) current_lease(request.library) original_assets = asset_identity(original_source, project_library(original_source)) staged, pcb, _ = source_project(request.source, request.top_level_block) @@ -617,6 +622,7 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) _dispatch_diagnostics(root, 'after-pywinauto-import', app, library) from .backup_recovery import _align_library from .management import source_project + from .native_add_failures import validate_native_copy_paths, snapshot_add_log, observe_add_failure source, pcb, top = source_project(job['source'], job.get('top_level_block', '')) source_folder = 'source-replacement' if job['operation'] == 'probe_readd' else 'source' source_root = (Path(root).resolve()/'source' @@ -624,6 +630,7 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) else Path(root).parent.resolve()/source_folder) if not source.is_relative_to(source_root): raise ValueError('隔离新增只允许使用本次复制的来源工程。') + validate_native_copy_paths(source, job['library'], job['name']) _align_library(source, pcb, Path(job['library'])) _dispatch_diagnostics(root, 'after-source-alignment', app, library, paths={'source': str(source), 'pcb': str(pcb), 'library_argument': job['library']}) @@ -673,10 +680,14 @@ def _probe_add_dialog_impl(app, job, desktop, owned_pids, root, *, library=None) ok = win32gui.GetDlgItem(dialog, 1083) if not ok: raise RuntimeError('原生新增确认按钮缺失。') + log_baseline = snapshot_add_log(job['library']) + _write_json(root/'native-add-log-baseline.json', log_baseline) + observed_failures = {} win32gui.PostMessage(ok, win32con.BM_CLICK, 0, 0) success = None while time.monotonic() < deadline: pythoncom.PumpWaitingMessages() + observe_add_failure(desktop, owned_pids, root, log_baseline, job['name'], observed_failures) for hwnd in desktop.EnumDesktopWindows(): if win32process.GetWindowThreadProcessId(hwnd)[1] not in owned_pids or not win32gui.IsWindowVisible(hwnd): continue diff --git a/tests/test_management_native_transaction.py b/tests/test_management_native_transaction.py index 12c5890..cad6073 100644 --- a/tests/test_management_native_transaction.py +++ b/tests/test_management_native_transaction.py @@ -137,3 +137,16 @@ def test_worker_rejects_modified_management_authority_before_add(transaction, ta result = execute(root, request) assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue assert state['entries'] == [] and request.source.read_bytes() == original + + +def test_long_staged_runtime_path_blocks_update_before_native_remove(transaction): + root, request, state = transaction + assert execute(root, request).status == 'UPLOADED' + runtime = request.source.parent/'CES/LogFiles'/('long-'+'x'*100)/('y'*60+'.log') + runtime.parent.mkdir(parents=True); runtime.write_bytes(b'complete original runtime history') + before = len(state['calls']) + result = execute(root, replace(request, operation='update')) + assert result.status == 'UPLOAD_FAILED' and result.safe_to_continue + assert 'NATIVE_PATH_TOO_LONG' in result.detail + assert state['calls'][before:] == ['catalog'] + assert len(state['entries']) == 1 and runtime.read_bytes() == b'complete original runtime history' diff --git a/tests/test_native_add_failures.py b/tests/test_native_add_failures.py new file mode 100644 index 0000000..2072bae --- /dev/null +++ b/tests/test_native_add_failures.py @@ -0,0 +1,109 @@ +"""File-only native failure and legacy path-boundary checks.""" +from pathlib import Path + +import pytest + + +ERROR = b'REUSE BLOCK: INFORMATION: Block\tError! Cannot copy iCDB base of Reusable Block project.\r\n' + + +def test_copy_failure_log_must_be_new_and_match_current_block(tmp_path): + from cbb_refresh_verify.native_add_failures import snapshot_add_log, fresh_add_failure + library = tmp_path/'lib.lmc'; library.touch() + log = tmp_path/'LogFiles/ReuseBlockLogLO.txt'; log.parent.mkdir(); log.write_bytes(ERROR) + baseline = snapshot_add_log(library) + assert fresh_add_failure(baseline, 'Block') is None + with log.open('ab') as stream: stream.write(ERROR.replace(b'Block\t', b'Other\t')) + assert fresh_add_failure(baseline, 'Block') is None + with log.open('ab') as stream: stream.write(ERROR) + assert 'Cannot copy iCDB base' in fresh_add_failure(baseline, 'Block') + # A replaced/rotated prefix is not evidence that an old error is new. + log.write_bytes(b'replaced\n'+ERROR) + assert fresh_add_failure(baseline, 'Block') is None + + +def test_newly_created_failure_log_is_current_round_evidence(tmp_path): + from cbb_refresh_verify.native_add_failures import snapshot_add_log, fresh_add_failure + library = tmp_path/'lib.lmc' + baseline = snapshot_add_log(library) + log = tmp_path/'LogFiles/ReuseBlockLogLO.txt'; log.parent.mkdir(); log.write_bytes(ERROR) + assert fresh_add_failure(baseline, 'Block') + + +@pytest.mark.parametrize('change', ['none', 'title', 'question', 'button_id', 'button_text', 'disabled', 'extra_button']) +def test_only_observed_path_error_dialog_can_be_acknowledged(change): + from cbb_refresh_verify.native_add_failures import failure_ack_button + title, text = 'Library Manager', '\n系统找不到指定的路径。\r\n' + buttons = [{'hwnd': 123, 'id': 2, 'class': 'Button', 'text': '确定', 'enabled': True}] + if change == 'title': title = 'Unrelated' + if change == 'question': text += '是否继续?' + if change == 'button_id': buttons[0]['id'] = 1 + if change == 'button_text': buttons[0]['text'] = '是' + if change == 'disabled': buttons[0]['enabled'] = False + if change == 'extra_button': buttons.append({**buttons[0], 'hwnd': 456}) + assert failure_ack_button(title, text, buttons) == (123 if change == 'none' else None) + + +def test_native_path_guard_includes_runtime_files_and_projected_target(tmp_path): + from cbb_refresh_verify.native_add_failures import validate_native_copy_paths + source = tmp_path/'source/Block.prj'; source.parent.mkdir(); source.touch() + validate_native_copy_paths(source, tmp_path/'library/lib.lmc', 'Block') + runtime = source.parent/'CES/LogFiles'/('x'*120)/('y'*90+'.log') + runtime.parent.mkdir(parents=True); runtime.touch() + with pytest.raises(ValueError, match='NATIVE_PATH_TOO_LONG'): + validate_native_copy_paths(source, tmp_path/'library/lib.lmc', 'Block') + assert runtime.exists() + runtime.unlink() + # Even a short staged tree can exceed the legacy limit at its target. + target = tmp_path/('library-'+'x'*150)/'lib.lmc' + with pytest.raises(ValueError, match='目标'): + validate_native_copy_paths(source, target, 'Block') + + +@pytest.mark.parametrize('kind', ['known', 'unknown', 'foreign']) +def test_failure_observer_only_acknowledges_owned_known_dialog(tmp_path, monkeypatch, kind): + import json + from types import SimpleNamespace + import win32gui, win32process + from cbb_refresh_verify.native_add_failures import observe_add_failure, snapshot_add_log + state = {'open': True, 'clicks': []} + monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda _: (1, 999 if kind == 'foreign' else 123)) + monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda _: True) + monkeypatch.setattr(win32gui, 'GetClassName', lambda h: {1: '#32770', 10: 'Button', 11: 'Static'}[h]) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda h: {1: 'Library Manager', 10: '确定', + 11: '系统找不到指定的路径。' if kind == 'known' else '未确认的原生提示'}[h]) + monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, callback, p: [callback(c, p) for c in [10, 11]]) + monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda h: 2 if h == 10 else 0) + monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda _: True) + monkeypatch.setattr(win32gui, 'IsWindow', lambda _: state['open']) + def post(hwnd, message, *_): + state['clicks'].append(hwnd); state['open'] = False + monkeypatch.setattr(win32gui, 'PostMessage', post) + monkeypatch.setattr(win32gui, 'SetForegroundWindow', lambda *_: pytest.fail('foreground input is forbidden')) + args = (SimpleNamespace(EnumDesktopWindows=lambda: [1]), {123}, tmp_path, + snapshot_add_log(tmp_path/'lib.lmc'), 'Block', {}) + if kind == 'known': + with pytest.raises(RuntimeError, match='NATIVE_ADD_FAILED'): observe_add_failure(*args) + result = json.loads((tmp_path/'native-add-output.json').read_text(encoding='utf-8')) + assert result['success_message'] is None and result['failure']['acknowledged'] + assert state['clicks'] == [10] + else: + observe_add_failure(*args) + assert not state['clicks'] and not (tmp_path/'native-add-output.json').exists() + assert (tmp_path/'native-add-dialogs.jsonl').exists() is (kind == 'unknown') + + +def test_path_rejection_returns_safe_before_any_native_identity_or_alignment(tmp_path, monkeypatch): + from cbb_refresh_verify.management import Capability, ManagementRequest + from cbb_refresh_verify.native_management import NativeManagementAdapter + import cbb_refresh_verify.management_transaction as authority + source = tmp_path/'source/Block.prj'; source.parent.mkdir(); source.touch() + runtime = source.parent/('x'*150)/('y'*100+'.log') + runtime.parent.mkdir(); runtime.write_bytes(b'preserved history') + adapter = NativeManagementAdapter(mentor_home=tmp_path) + monkeypatch.setattr(adapter, 'capabilities', lambda: {'add': Capability(True, 'test boundary')}) + monkeypatch.setattr(authority, 'current_lease', lambda _: pytest.fail('long path reached native transaction setup')) + request = ManagementRequest('update', tmp_path/'lib.lmc', 'Block', source=source) + staged, result = adapter.prepare_add_transaction(request, tmp_path/'evidence', original_source=source) + assert staged == request and result.identity is None and result.safe_to_continue + assert 'NATIVE_PATH_TOO_LONG' in result.detail and runtime.read_bytes() == b'preserved history' -- 2.39.5 From b26e2f2f6232c22a94ee1269bd4449fe0fe06623 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: Tue, 3 Nov 2026 12:54:21 +0800 Subject: [PATCH 38/59] test: create source fixture for native UIA path admission --- tests/test_management_diagnostics.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_management_diagnostics.py b/tests/test_management_diagnostics.py index e1dfd5d..7595aa1 100644 --- a/tests/test_management_diagnostics.py +++ b/tests/test_management_diagnostics.py @@ -114,6 +114,8 @@ def test_probe_converts_desktop_pyhandle_before_uia_boundary(tmp_path, monkeypat import cbb_refresh_verify.management as management import cbb_refresh_verify.backup_recovery as recovery source = tmp_path/'source/Block.prj' + source.parent.mkdir() + source.write_bytes(b'file-only UIA boundary fixture') pcb = source.parent/'PCB/Block.pcb' root = tmp_path/'native-add'; root.mkdir() class Handle: -- 2.39.5 From 5eac654c36e52341da03735f4940049a935bfcea 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: Tue, 3 Nov 2026 13:06:07 +0800 Subject: [PATCH 39/59] docs: record native CRUD frozen acceptance and candidate limits --- docs/releases/2026-11-03-native-crud.md | 78 +++++++++++++++---- .../2026-11-03-native-crud-completion.md | 32 ++++++++ 2 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 docs/superpowers/plans/2026-11-03-native-crud-completion.md diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md index ab4e3f2..2108476 100644 --- a/docs/releases/2026-11-03-native-crud.md +++ b/docs/releases/2026-11-03-native-crud.md @@ -1,31 +1,77 @@ -# Native CRUD integration and review follow-up +# 原生 CBB 增删改查接入与候选包验证 -The shared native Add transport uses the proven private-desktop UIA executor, bounded field readback and exact native success message. It closes/reopens the library before comparing exact catalog name/partition and engineering identity. Missing registration or mismatched native logical/drawing/physical proof cannot become an accepted upload. Production Add/Update are now enabled only with the exact pinned EE7.9.5 core and all three identity exporters, for ASCII target names in the proven DxDesigner / Logical-Physical scope. Every mutation still requires the prepared engine transaction and complete recovery snapshot. +程序源码提交:`b26e2f2f6232c22a94ee1269bd4449fe0fe06623`。 +分支:`codex/temp-issue-2-icdb-recovery`。本地候选包,未合并、未发布、未关闭工单。 -The review of 07a9c1e identified that route readiness alone did not authorize existing-library mutation. The follow-up makes public `add()` refuse before reading a source or launching a worker. Research callers must use `add_owned_fixture(..., probe_token=...)` against the fixed library created by the diagnostic workflow and matching probe-owner record. It requires the calling thread's live library lease, checks its lease ID against the checkpoint, owns the management mutex during submission, creates a fresh staged source copy and verified library snapshot, and binds the transaction nonce to target, name, partition, source identity and manifests. +## 已接入的操作 -Before native activation the worker revalidates that evidence, exact current lease and live supervisor. It duplicates the supervisor's still-open exclusive sidecar handle and checks its final path, so JSON metadata alone cannot establish ownership. Source alignment is confined to the newly copied worker source. Existing probe CreateLibrary/readd guards remain intact. Removal success matching again excludes both carriage returns and newlines. +| 操作 | 原生入口与验收条件 | +| --- | --- | +| 读取 | `LibraryManager.Application.OpenLibrary`、`Partitions(7)`;核对库身份、分区和条目,正常关闭会话。 | +| 新增 CBB | `OnReusableBlocksEditor` → Logical-Physical → New;私有桌面 UIA 和有时限的控件消息提交完整 PRJ,确认成功提示后关闭重开库,再核对工程内容。重名拒绝覆盖。 | +| 用工程替换已选 CBB | 完整工程及中心库备份 → 原生移除 → 原生重新导入 → 重新读库和内容核对。失败保留恢复证据并停止后续写操作。 | +| 移除已选 | 安装脚本确认的 `CleanWGLib.exe F4 `,输入为 `REUSE_BLOCK`;先完整备份,要求明确 Removed 输出,再重新读库确认不存在。 | -Verification is mocked/file-only: the original focused suite passed 86 tests plus two close/reopen checks. Follow-up tests reject public bypass, unprepared activation, missing current-thread ownership, invalid nonce/token/lease, substituted original source, changed backup and expired supervisor, and exercise a valid leased fixture with unchanged original source bytes. No Mentor processes, builds or full suite were launched by this implementation agent; serialized native evidence and semantic-identity research remain coordinator-owned. +“用工程替换已选 CBB…(原生重新导入)”与已有“从备份恢复 CBB…”是不同操作。前者更新中心库注册条目,采用移除后重新导入,**不是原子替换**;后者沿用备份 Work 的副本验证与安装恢复流程。 -The follow-up focused suite passed 99 tests. After restoring ordinary catalog queries for non-DxDesigner flow readback, the native boundary/field subset passed 47 tests (including four additional flow cases). `git diff --check` passed. The original ignored scratch report remains on disk but is removed from version control. +新增和替换默认继续自动校验,允许批次开始前取消。结果分别显示上传失败、上传成功但校验失败、上传并校验通过;取消自动校验时明确显示“已上传 / 未自动校验”。 -A second review found ownership lifetime and reverse containment gaps. The fixture route now nests the already-proven caller lease around worker dispatch and retains that same exclusive handle/checkpoint on unsafe return or exception, including callers that supply no release callback. It also rejects a fixture nested anywhere inside the original source tree before staging. Three new failure cases were observed before the fix; the final native-boundary/field subset passed 51 tests, including normal release and uploader-handle denial after uncertain release. No native processes were launched. +## 支持范围与保护 -The subsequent `mentor795-native-content-v1` contract requires pinned native hierarchy/flat ASCII, CSV/property tables and schematic EDIF exports on disposable copies, plus exact raw physical/primary assets. Only the three observed ASCII metadata headers, CSV `System_Date` field and root EDIF `status/written/timestamp` node are normalized. All project library references must resolve to an approved library before their values can be normalized. Unknown primary files, layout data, drawing coordinates, properties and EDIF userData remain part of the comparison. Unsupported exports fail closed; this is not a claim of arbitrary unexported CES equivalence. +- 已验证本机 Mentor EE7.9.5、DxDesigner、Logical-Physical、ASCII CBB 名称。原生核心组件和三种内容导出程序均核对固定 SHA-256;接口或版本不符时禁用并显示原因。 +- 所有写操作使用串行任务、共享互斥和上传工具兼容的库锁;暂存来源、完整备份、目标身份、事务随机标识、监督进程及实际独占句柄必须相互匹配。 +- 不直接修改 LMC 二进制,不依赖独立上传工具安装目录,不退回前台鼠标。遇到中心库升级或流程选择提示拒绝自动迁移。 +- 原生导入会改写 ICDB 存储字节。内容身份采用官方 ASCII、CSV 和 EDIF 原理图导出,加上 Layout、约束及其他主要资产的 SHA-256。只规范化已实测的报告时间和路径字段;未知工程文件继续参与比较。 +- 原生复制可能留下不正确的 PCB 库相对引用。仅在本任务导入、会话释放及库锁持有得到确认后校正已导入副本,再复读目录与内容。原始来源不改写。 +- 导出副本所有权随库检查点持久保存;超时、重启、地址记录仍存在或进程、独占访问无法确认时,不能解除库锁或接续维护。 +- 完整来源和预计目标路径达到 260 个 UTF-16 单元时,在原生写入前拒绝;同名替换在移除旧条目前检查。保留所有文件,提示缩短操作数据或目标库路径,不宣称已实现原生长路径支持。 +- 本轮已确认的原生复制失败即时记录。只正常确认本任务进程的已验证错误对话框;未知提示不自动点击,会话释放不明确时保留阻止状态。 -The real management engine now captures a source baseline before any removal. Native Add requires a prepared transaction binding the verified full backup, staged source, baseline proof, exact target/name/partition, supervisor and exclusive lease. After normal native release, it corrects imported PRJ/PCB library references using the existing documented text helpers, reopens the exact native catalog and compares a target-only identity. Update remains remove plus Add; failures retain the recovery snapshot and checkpoint. The UI action is explicitly labeled “用工程替换已选 CBB…(原生重新导入)”; backup Work restoration is unchanged. +## 实际验证 -Coordinator-native source/registered capture matched all three exports and primary assets after correcting the observed imported PCB reference; original source manifests were unchanged. Implementation verification remains file-only: 114 focused tests passed before the final bootstrap/lease refinement. `create_owned_fixture` and `run_owned_fixture_management` provide a token-bound empty-library/real-engine route for the coordinator's actual CRUD cycle. Production Add/Update remain disabled until that separate acceptance and promotion step. +环境:Windows 11,版本 `10.0.26200`;Mentor EE7.9.5。 -The coordinator's native engine cycle subsequently passed empty-library creation, Add A, duplicate refusal, same-name replacement by B (with a different verified layout hash), removal with strict absence, and reimport from a complete backup. Every operation reported safe native release and both original source manifests stayed unchanged; evidence is under `diagnostics/issue2-native-crud-20261103/engine-e2e`. +### 原生完整管理引擎 -Final safety review narrowed the additional iCDB runtime exclusions to known log directories, exact events files and the observed dated GUID log filenames. Unknown `cdbsvr` content remains primary, including unknown events/plog files, and a registered unknown-file change now requires recovery. Export capture binds its analysis copy to the existing library checkpoint before launch. That binding survives retention/restart and cannot be cleared by release or maintenance adoption until recorded processes/child handles have exited, known exporter/server processes are absent, copied addresses are absent and copied database files can be held exclusively. Missing/malformed ownership evidence remains blocked. The final focused safety/engine suite passed 167 tests; no native operations or full-suite run were performed by the implementation agent. Production promotion remains a separate reviewed change. +真实新建隔离库完成:新增 A、拒绝重复名称、替换为 B、备份后移除、将独立备份工程重新导入。各项会话均安全释放。B 的 Layout 哈希与 A 不同,并与来源 B 一致。原始来源主要文件清单未变。 -After scoped review found both safety issues corrected, the separate capability-promotion change enabled the public prepared engine routes. The basis is the accepted native cycle in `engine-e2e/cycle-result.json` plus the matching integrated three-export/asset identity evidence. That cycle deliberately used `auto_verify=False`: it proves native registration/replacement/removal/reimport and content identity, not the separate default Verify result or frozen UI acceptance. Unsupported exporter versions, flows, partitions or export formats still fail closed. Replacement remains non-atomic Remove+Add with recovery evidence, and arbitrary unexported CES/version equivalence is not claimed. +证据:`diagnostics/issue2-native-crud-20261103/engine-e2e/cycle-result.json`。 -The promotion focused suite passed 93 tests, including every required binary mismatch, unsupported flow/partition, engine identity/recovery and UI progress behavior. Native default description is blank; progress reads “正在核对来源工程内容”. Source input and backup Work restoration behavior are unchanged. Full-suite/build and fresh public frozen-UI acceptance remain coordinator-owned. +### 最终冻结程序公开界面 -Frozen public Add exposed a confirmed legacy path limit: the staged tree contained a 264-character historical CES log path, versus a maximum of 253 in the successful engine cycle. Native output reported `Cannot copy iCDB base of Reusable Block project` and an owned “系统找不到指定的路径。” dialog. The coordinator reproduced Windows error 3 with legacy 32-bit `CopyFile` on that exact file; the same frozen executable, same target library and blank description then completed Add safely when only the operation data/staging root was shortened (`frozen-public-ui-shortpath-control/result.json`). +从应用真实按钮、来源文件选择器和确认清单执行,没有替换原生适配器或绕过能力检查。 -The complete staged and projected target trees are now checked against the legacy 260-WCHAR path buffer before native identity capture or Update removal, and again before dialog submission. Unsupported paths produce an actionable shorter-data/target-root message; no source/log files are excluded or deleted. Native copy failure detection uses a bounded pre-submit log size/hash baseline and exact current block name, so old log errors do not fail later operations. The exact observed owned error dialog is recorded and acknowledged only through its known id-2 “确定” button; unknown prompts are recorded without confirmation. Normal child-first close and exact owner-release checks remain required. Confirmed errors now return promptly instead of waiting for the full operation timeout. Focused verification passed 142 tests, including unchanged source/history and no native Remove on long-path update rejection; final frozen acceptance remains coordinator-owned. +| 试验 | 结果 | +| --- | --- | +| 275 字符暂存路径 | `UPLOAD_FAILED`,明确 `NATIVE_PATH_TOO_LONG`,`safe_to_continue=true`;未启动内容导出或原生 Add。 | +| 正常路径新增 A | `UPLOADED`,重新读库与工程内容核对通过。 | +| 同名替换为 B | `UPLOADED`,实际 Layout 匹配 B 且不同于 A。 | +| 原生移除 | `REMOVED`,重新读库确认不存在。 | +| 最终退出与来源检查 | 原生进程已退出;两份来源的 153/147 个主要文件哈希及 541/506 个文件的大小、修改时间保持一致。 | + +证据位于 `diagnostics/issue2-native-crud-20261103/`: + +- `frozen-public-ui-path-block/result.json`:写入前长路径拒绝。 +- `frozen-public-ui/result.json`:最终冻结程序完整按钮流程及截图。 +- `final-source-baselines.json`、`final-acceptance.json`:来源和会话释放证据。 +- `build-result.json`、`validation.json`、`package-result.json`:源码、测试、构建和包校验记录。 + +本轮 CRUD 原生及冻结界面试验均在批次开始前取消自动校验,**上传成功不等于 READY**。这些结果不能代替上传后完整 Verify/Save/Close 或现场 ICDB 失联修复验收。 + +### 实测发现并修正的长路径问题 + +初版冻结程序在 264 字符暂存文件路径下弹出“系统找不到指定的路径”,日志记录 `Cannot copy iCDB base of Reusable Block project`,旧逻辑只检查成功提示,直到超时才返回。该失败未计入通过。 + +对同一文件,旧式 32 位 Win32 CopyFile 复现错误码 3;保持同一冻结程序、测试库、完整来源和空描述,仅缩短暂存路径后,原生新增和内容核对通过。最终修订增加完整路径预检和本轮失败识别,并在新冻结包实测提前拒绝。没有删减长路径中的日志或其他文件来绕过问题。 + +保留原始失败、正常关闭及库锁重新核验记录:`frozen-public-ui-longpath-failed/`、`longpath-failure-release/`、`longpath-failure-catalog/`、`path-copy-probe/`、`frozen-public-ui-shortpath-control/`。 + +## 自动化验证与交付 + +- 全量 `python -m pytest -q`:**1071 passed**,90.98 秒,1 条已有 pywinauto COM 线程模式警告。 +- `python -m compileall -q src tests`、`git diff --check`:通过。 +- 独立整体代码审查及长路径修订复审:无未解决的实质问题。 +- 从提交归档构建冻结程序,启动与退出冒烟测试通过;上述公开界面验收使用同一构建。 +- 候选包:`dist/issue2-native-crud-20261103-b26e2f2.zip`,旁附 SHA-256;包内含使用说明及验证摘要。 + +尚未完成 Windows 10 实机验证,也未将本轮 CRUD 试验视作现场服务器会话失联修复通过。旧版库迁移、其他 Mentor 版本、非 Logical-Physical 分区及原生长路径支持不在已验证范围。原用户库不参与本轮写入试验。 diff --git a/docs/superpowers/plans/2026-11-03-native-crud-completion.md b/docs/superpowers/plans/2026-11-03-native-crud-completion.md new file mode 100644 index 0000000..613b378 --- /dev/null +++ b/docs/superpowers/plans/2026-11-03-native-crud-completion.md @@ -0,0 +1,32 @@ +# 原生 CBB 管理接入续作 + +沿用用户已批准的工单 #2 管理范围与临时分支。此次用户要求找到并尝试接入库的增删改查原生接口。 +不直接修改 LMC 二进制,不采用前台鼠标,不强制结束持有数据库的原生进程;原用户库不参与写入试验。 + +## 已确认接口 + +- 查询:LibraryManager.Application.OpenLibrary → Partitions(7) → 名称、分区,核对库身份并正常退出。 +- 新增:OnReusableBlocksEditor → 原生 Logical-Physical 页签 → New → 源 PRJ、名称、顶层块,提交原生对话框。字段使用有时限的窗口消息复读,提交后确认成功消息,再关闭重开目录。 +- 移除:安装脚本确认的 CleanWGLib/F4/REUSE_BLOCK,必须完整备份且重新读库确认缺失。 +- 同名替换:复用经过验证的移除与新增;它不是原子更新,删除后导入失败时保留恢复检查点并停止队列。与仅替换 Work 的“从备份恢复”明确区分。 + +## 接入步骤 + +1. 共享原生新增适配层,保留私有桌面、二进制版本、同库租约、监督进程、来源暂存及完整备份的验证。生产能力未通过时只有带隔离库所有权证据的研究入口可写。 +2. 原生 ICDB 导入会重写存储。用安装版本锁定的 ASCII、CSV 和 EDIF 原理图导出建立内容指纹,并保留布局及其他工程资产的字节哈希。不能把注册成功或名称相同等同于工程内容一致。 +3. 导出仅使用新建的完整工程副本。严格确认退出、有效输出与原生完成标记;仅规范化已经实际证明会变化的报告时间/路径字段。布局、坐标、连接、属性及未知工程文件变化一律阻止接受。 +4. 新增后原生程序可能未正确重定位 PCB 配置的 CentralLibrary。仅在本任务注册成功、原生释放和租约持有时修正该已导入工程的库引用,再重新打开核对目录与内容。原始输入工程不改动。 +5. 在实际新建隔离库运行新增、同名冲突、不同版本同名替换、备份后移除、从备份重新导入;逐项保存证据,验证源文件未变。随后才提升对应生产能力。 +6. 运行相关回归、独立审查、全量测试与冻结包实际管理入口验证。缺少原生证据的能力继续禁用并准确说明原因。 + +## 已完成的内容指纹取证 + +`diagnostics/issue2-native-crud-20261103/` 保存真实导出对照: + +- ASCII:层次与展开连接、组件、端口和属性,3,531 行;只差生成时间和数据库目录三行报告元数据。 +- CSV:14 个表格文件;只差信息表的 System_Date 字段。 +- EDIF `-schematic`:约 580 KB,包含坐标、图形、连线、旋转、文本位置及属性;只差根级 status/written/timestamp。 +- 139 个主要工程资产一致;其余变化为 ICDB 数据文件、其运行时记录及两处 PCB CentralLibrary 引用。 + +以上为已验证格式的内容身份合同,不声称任意未知版本、任意外部工具私有元数据都可无差别转换。 +导出格式、安装版本、必需文件或引用不符合时拒绝操作。 -- 2.39.5 From d36b6bcad0d464a53ac6ada5d215377b5bdc8ca9 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: Tue, 3 Nov 2026 13:51:05 +0800 Subject: [PATCH 40/59] fix: admit linked legacy ICDB projects and explain blocked actions --- src/cbb_refresh_verify/application.py | 3 + src/cbb_refresh_verify/maintenance_entry.py | 25 +++++++- src/cbb_refresh_verify/post_repair_consent.py | 3 +- src/cbb_refresh_verify/ui/main_window.py | 60 +++++++++++++++---- tests/test_incident_safety.py | 4 +- tests/test_maintenance_entry.py | 40 ++++++++++++- tests/test_maintenance_ui.py | 14 +++++ tests/test_post_repair_consent.py | 7 ++- 8 files changed, 138 insertions(+), 18 deletions(-) diff --git a/src/cbb_refresh_verify/application.py b/src/cbb_refresh_verify/application.py index ca9c9c2..0208624 100644 --- a/src/cbb_refresh_verify/application.py +++ b/src/cbb_refresh_verify/application.py @@ -27,6 +27,8 @@ def configure_qt_application(app): } for role, color in colors.items(): palette.setColor(role, QColor(color)) + for role in (QPalette.ColorRole.WindowText, QPalette.ColorRole.Text, QPalette.ColorRole.ButtonText): + palette.setColor(QPalette.ColorGroup.Disabled, role, QColor('#858b94')) app.setPalette(palette) app.setStyleSheet( 'QWidget { color: #202124; }' @@ -34,6 +36,7 @@ def configure_qt_application(app): 'background: #ffffff; color: #202124; selection-color: #202124; }' 'QHeaderView::section { background: #e9edf2; color: #202124; }' 'QPushButton, QToolButton { color: #202124; }' + 'QPushButton:disabled, QToolButton:disabled { color: #858b94; background: #e3e6eb; }' ) return app diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 62c94ad..a033e10 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -65,6 +65,25 @@ def _scoped_addresses(request): 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): from .verify_runner import preflight from .backup_recovery import _project_text @@ -75,7 +94,7 @@ def select_maintenance_target(request): project, pcb = preflight(request) scan_tree(project.parent) candidates = [] - for candidate in (project, pcb.parent/'pcb.prj'): + for candidate in maintenance_project_files(project, pcb): if not candidate.is_file(): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少工程配置') text = _project_text(candidate) @@ -114,7 +133,7 @@ def maintenance_target_guard(request, expected=None): 'project': str(target.project.resolve()), 'database': str(target.database.resolve()), 'addresses': baseline, 'engineering_references': {str(p.resolve()): hash_file(p) - for p in (project, pcb, pcb.parent/'pcb.prj')}} + 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() @@ -223,4 +242,4 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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_category='ICDB_MAINTENANCE_BLOCKED', error_stage=record['stage'])) diff --git a/src/cbb_refresh_verify/post_repair_consent.py b/src/cbb_refresh_verify/post_repair_consent.py index 58d0167..3946569 100644 --- a/src/cbb_refresh_verify/post_repair_consent.py +++ b/src/cbb_refresh_verify/post_repair_consent.py @@ -124,7 +124,8 @@ def prepare_launch(job, command): if _current_desktop_name() != job['desktop']: raise ValueError('POST_REPAIR_PRIVATE_DESKTOP_REQUIRED') library, project, pcb = (Path(job[key]) for key in ('lmc_file', 'project', 'pcb')) - for path in (library, project, pcb, pcb.parent/'pcb.prj'): + from .maintenance_entry import maintenance_project_files + for path in (library, pcb, *maintenance_project_files(project, pcb)): require_local_fixed(path) if len(command) != 3 or not _same(command[1], pcb) or command[2] != '-ReusableCircuit': raise ValueError('POST_REPAIR_LAUNCH_MISMATCH') diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 3ab788f..aabecf8 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -167,7 +167,14 @@ class MainWindow(QWidget): self.output.document().setMaximumBlockCount(2000) layout = QFormLayout(self) row = QHBoxLayout(); row.addWidget(self.lmc_edit); row.addWidget(self.browse_button); row.addWidget(self.load_button) - layout.addRow('中心库 .lmc', row); layout.addRow(self.advanced_toggle); layout.addRow(self.advanced_panel) + layout.addRow('中心库 .lmc', row) + self.library_notice = QLabel() + self.library_notice.setWordWrap(True) + self.library_notice.setTextFormat(Qt.PlainText) + self.library_notice.setStyleSheet('QLabel { color: #8a4b08; background: #fff3d6; padding: 6px; }') + self.library_notice.hide() + layout.addRow(self.library_notice) + layout.addRow(self.advanced_toggle); layout.addRow(self.advanced_panel) filters = QHBoxLayout() for widget in (self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button): filters.addWidget(widget) @@ -207,12 +214,9 @@ class MainWindow(QWidget): widget.setToolTip(self.management_capabilities[operation].reason) management_buttons.addWidget(widget) management_layout.addLayout(management_buttons) - self.management_capability_label = QLabel('\n'.join( - f'{label}:{self.management_capabilities[op].reason}' - for op, label in (('add', '新增'), ('update', '同名更新'), ('remove', '移除')) - if not self.management_capabilities[op].supported)) + self.management_capability_label = QLabel() self.management_capability_label.setWordWrap(True) - management_layout.addWidget(self.management_capability_label) + management_layout.insertWidget(0, self.management_capability_label) self.unfinished_label = QLabel(); self.unfinished_label.setWordWrap(True) management_layout.addWidget(self.unfinished_label) self.release_library_button = QPushButton('检查会话释放并解除协调阻止…') @@ -256,10 +260,21 @@ class MainWindow(QWidget): self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names())) self.backup_restore_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._maintenance_names())) self.release_library_button.setEnabled(not self.busy and bool(self.lmc_edit.text().strip())) - loaded = not self.busy and not self._management_unsafe and not self._maintenance_recovery_view and self.loaded_path == self.lmc_edit.text() + reasons = [] for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): - widget.setEnabled(loaded and self.management_capabilities[operation].supported - and (operation == 'add' or bool(self.selected_records()))) + reason = self._management_block_reason(operation) + widget.setEnabled(not reason) + widget.setToolTip(reason or self.management_capabilities[operation].reason) + if reason: + reasons.append(reason) + self.management_capability_label.setText('\n'.join(dict.fromkeys(reasons))) + notice = ('该库有未确认的会话记录,新增、替换、移除和普通校验已暂停。' + '请先执行 ICDB 维护;仍有数据库地址记录时,单独点击释放检查不会解除阻止。' + if self._maintenance_recovery_view else + '上次管理操作尚未确认安全释放,已暂停库操作。请先查看异常证据并检查会话释放。' + if self._management_unsafe else '') + self.library_notice.setText(notice) + self.library_notice.setVisible(bool(notice)) for widget in (self.lmc_edit, self.browse_button, self.load_button, self.cbb_table, self.timeout_spin, self.advanced_toggle, self.advanced_panel, self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button): @@ -297,9 +312,26 @@ class MainWindow(QWidget): self.status_label.setText(message); self.output.append(message) self._start(lambda: release_retained_library(library, self.data_root/'release-checks'), completed) + def _management_block_reason(self, operation): + if self.busy: + return '当前任务尚未结束,请等待后再执行库管理操作。' + if self._maintenance_recovery_view: + return '库的会话释放尚未确认,管理操作已禁用;请先执行 ICDB 维护并查看逐项结果。' + if self._management_unsafe: + return '上次管理操作的会话尚未安全释放,请先查看异常证据并执行释放检查。' + capability = self.management_capabilities[operation] + if not capability.supported: + return capability.reason + if not self.loaded_path or self.loaded_path != self.lmc_edit.text(): + return '请先选择并读取中心库。' + if operation != 'add' and not self.selected_records(): + return '替换和移除需要先勾选当前可见的 CBB。' + return '' + def manage_cbb(self, operation): - if (self.busy or self._management_unsafe or self._maintenance_recovery_view or self.loaded_path != self.lmc_edit.text() - or not self.management_capabilities[operation].supported): + reason = self._management_block_reason(operation) + if reason: + self.status_label.setText(reason) return from cbb_refresh_verify.management import ManagementRequest from .management_dialog import ManagementDialog @@ -836,6 +868,12 @@ class MainWindow(QWidget): f'原生修复 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}。') + first_failure = next((result.stderr for result in results + if result.status not in ('PASS', 'SKIPPED') and result.stderr), '') + if first_failure: + explanation = '\n首个未完成原因:' + first_failure[:1500] + status_text += explanation + text += explanation self.status_label.setText(status_text); self.output.append(text) self.summary = QMessageBox(self) self.summary.setWindowTitle('非 READY ICDB 检查与修复结果' if self._global_icdb_run else 'CBB 校验结果'); self.summary.setText(text) diff --git a/tests/test_incident_safety.py b/tests/test_incident_safety.py index 0b4bae0..8078025 100644 --- a/tests/test_incident_safety.py +++ b/tests/test_incident_safety.py @@ -209,7 +209,9 @@ def test_real_windows_job_abort_preserves_native_like_child(tmp_path): script.write_text( 'import subprocess, sys, time\nfrom pathlib import Path\n' 'child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])\n' - f'Path({str(pid_path)!r}).write_text(str(child.pid))\n' + f'pid_file = Path({str(pid_path)!r})\n' + 'pending = pid_file.with_suffix(".pending")\n' + 'pending.write_text(str(child.pid)); pending.replace(pid_file)\n' 'time.sleep(30)\n', encoding='utf-8') worker = DesktopWorker([sys.executable, str(script)], 'CbbSafetyTest_' + uuid.uuid4().hex[:8]) child_handle = None diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index 35c57e3..f1db622 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -46,10 +46,13 @@ def case(tmp_path, monkeypatch): if owner: owner['handle'].Close() -def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch): +@pytest.mark.parametrize('pcb_config', ['standard', 'legacy_template']) +def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch, pcb_config): from cbb_refresh_verify import verify_runner as runner, native_repair from test_native_repair import complete_native_success request, project, address = case + if pcb_config == 'legacy_template': + (project.parent/'PCB/pcb.prj').rename(project.parent/'PCB/template.prj') calls = [] def repair(target, root, **kwargs): calls.append('repair') @@ -79,6 +82,40 @@ def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fa assert 'session' not in record['maintenance_context'] +@pytest.mark.parametrize('back_reference', ['', 'KEY ProjectFile "../other.prj"\n', + 'KEY ProjectFile "../IP101GR.prj"\nKEY ProjectFile "../IP101GR.prj"\n']) +def test_legacy_maintenance_requires_unambiguous_pcb_back_reference(case, back_reference): + from cbb_refresh_verify.maintenance_entry import select_maintenance_target + request, project, _ = case + (project.parent/'PCB/pcb.prj').rename(project.parent/'PCB/template.prj') + (project.parent/'PCB/IP101GR.pcb').write_text(back_reference) + with pytest.raises(ValueError, match='MAINTENANCE_TARGET_UNCONFIRMED'): + select_maintenance_target(request) + + +def test_legacy_target_does_not_guess_template_for_physical_address(case): + from cbb_refresh_verify.maintenance_entry import select_maintenance_target + request, project, address = case + (project.parent/'PCB/pcb.prj').rename(project.parent/'PCB/template.prj') + address.unlink() + physical = project.parent/'PCB/database/cdbsvr/sAddress.adr' + physical.parent.mkdir(parents=True); physical.write_bytes(b'physical owner') + with pytest.raises(ValueError, match='SHARED_DATABASE_REPAIR_UNSUPPORTED'): + select_maintenance_target(request) + + +def test_optional_pcb_project_appearance_invalidates_maintenance_proof(case): + from cbb_refresh_verify.maintenance_entry import maintenance_target_guard + request, project, _ = case + config = project.parent/'PCB/pcb.prj' + config.rename(config.with_name('template.prj')) + with maintenance_target_guard(request) as proof: + before = proof['context'] + config.write_text('KEY CentralLibrary "different.lmc"\n') + with pytest.raises(ValueError, match='MAINTENANCE_SOURCE_CHANGED'): + with maintenance_target_guard(request, before): pass + + def test_ordinary_verify_still_blocks_existing_address_without_repair(case, monkeypatch): from cbb_refresh_verify import verify_runner as runner, native_repair request, _, address = case @@ -114,6 +151,7 @@ def test_maintenance_guard_blocks_before_native(case, monkeypatch, obstacle): try: result = runner.run_verify(request) assert result.status == 'BLOCKED' and not result.safe_to_continue + assert result.error_stage == 'preflight' finally: if handle: handle.Close() diff --git a/tests/test_maintenance_ui.py b/tests/test_maintenance_ui.py index 8a2ec5e..f3716b5 100644 --- a/tests/test_maintenance_ui.py +++ b/tests/test_maintenance_ui.py @@ -53,6 +53,8 @@ def test_blocked_library_recovery_view_is_filesystem_only_and_maintenance_only(q assert window.visible_records()[0].name == 'IP101GR' assert window.visible_records()[0].status == 'UNKNOWN' assert '未确认' in window.status_label.text() + assert 'ICDB' in window.management_capability_label.text() + assert '会话' in window.add_button.toolTip() window.select_visible_button.click() assert window.icdb_repair_button.isEnabled() assert not any(button.isEnabled() for button in (window.batch_button, window.selected_batch_button, @@ -65,6 +67,18 @@ def test_blocked_library_recovery_view_is_filesystem_only_and_maintenance_only(q assert len(calls) == 1 and calls[0].maintenance is True +def test_maintenance_summary_exposes_first_failure_reason(qtbot, tmp_path): + from cbb_refresh_verify.ui.main_window import MainWindow + from cbb_refresh_verify.verify_runner import VerifyResult + window = MainWindow(data_root=tmp_path) + qtbot.addWidget(window) + window._global_icdb_run = True + detail = 'MAINTENANCE_TARGET_UNCONFIRMED: PCB 引用与当前工程不一致' + window._batch_done(([VerifyResult('BLOCKED', stderr=detail, safe_to_continue=False)], tmp_path/'batch.log')) + assert detail in window.summary.text() + assert detail in window.status_label.text() + + def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qtbot, tmp_path, monkeypatch): from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard from cbb_refresh_verify.ui.main_window import MainWindow diff --git a/tests/test_post_repair_consent.py b/tests/test_post_repair_consent.py index 03632af..d9d371c 100644 --- a/tests/test_post_repair_consent.py +++ b/tests/test_post_repair_consent.py @@ -95,9 +95,14 @@ def test_prelaunch_proof_blocks_without_launch(context, monkeypatch, obstacle): if handle: handle.Close() -def test_exact_dialog_after_bound_prelaunch_is_allowed_only_once(context, monkeypatch): +@pytest.mark.parametrize('pcb_config', ['standard', 'legacy_template']) +def test_exact_dialog_after_bound_prelaunch_is_allowed_only_once(context, monkeypatch, pcb_config): from cbb_refresh_verify import post_repair_consent as consent _, _, proof, job = context + if pcb_config == 'legacy_template': + pcb = Path(job['pcb']) + (pcb.parent/'pcb.prj').rename(pcb.parent/'template.prj') + pcb.write_text('KEY ProjectFile "../'+Path(job['project']).name+'"\n') command = ['C:/Mentor/common/win32/bin/ExpeditionPCB.exe', job['pcb'], '-ReusableCircuit'] consent.prepare_launch(job, command) controller = consent.ConsentController(proof, job) -- 2.39.5 From 752780012880c27555eaf351d3d6a586b995d6ae 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: Tue, 3 Nov 2026 16:03:14 +0800 Subject: [PATCH 41/59] fix: complete legacy CBB import recovery and guard late native submission --- docs/releases/2026-11-03-field-feedback.md | 46 ++++++++++ src/cbb_refresh_verify/backup_recovery.py | 13 +-- src/cbb_refresh_verify/integration.py | 27 ++++-- src/cbb_refresh_verify/management.py | 3 +- src/cbb_refresh_verify/native_add_failures.py | 30 +++++++ .../native_engineering_identity.py | 18 +++- src/cbb_refresh_verify/native_management.py | 44 +++++++++- tests/test_integration.py | 39 +++++++++ tests/test_management_diagnostics.py | 7 +- tests/test_management_source.py | 10 ++- tests/test_native_add_failures.py | 85 +++++++++++++++++++ tests/test_native_engineering_identity.py | 29 +++++++ 12 files changed, 326 insertions(+), 25 deletions(-) create mode 100644 docs/releases/2026-11-03-field-feedback.md diff --git a/docs/releases/2026-11-03-field-feedback.md b/docs/releases/2026-11-03-field-feedback.md new file mode 100644 index 0000000..1594d48 --- /dev/null +++ b/docs/releases/2026-11-03-field-feedback.md @@ -0,0 +1,46 @@ +# 工单 #2:用户实测失败后的修订记录 + +分支 `codex/temp-issue-2-icdb-recovery`。本轮修订不能视为现场验收通过;未合并、发布或关闭工单。 + +## 用户失败的实际原因 + +用户运行 `b26e2f2` 冻结包。真实日志位于程序旁的 `data` 目录。中心库存在有效的未释放会话检查点,应用进入文件系统恢复视图,因此管理按钮被禁用;旧界面没有清楚显示原因,禁用外观也像可用按钮。该次没有启动增删改操作。 + +ICDB 批次的首项 `1GPHY_8212` 在预检查报 `MAINTENANCE_TARGET_UNCONFIRMED`,原生 Repair 没有执行。旧检查硬性要求 `PCB/pcb.prj`,但该工程的 PCB 明确反向引用主 `1GPHY_8212.prj`。主 PRJ 指向 `database`;存在的 `PCB/template.prj` 不能当作活动工程猜测使用。后续八项被跳过。 + +## 修订 + +- 只有 PCB 唯一、精确反向引用主 PRJ 时,允许缺少辅助 `pcb.prj`。预检查、Repair 后重新打开、管理来源身份及库引用核对使用一致规则。`template.prj` 保持原样并参与资产哈希。 +- 显示库会话阻止横幅、管理禁用原因、灰色禁用按钮及首项具体失败阶段。 +- 集成校验跳过原生明确禁止调用、且已同步的动作;仍要求四项最终全部同步。必需动作被禁止或原生拒绝时继续判失败,不伪造通过。 +- 识别原生 EDIF 中数字开头的精确标识符,例如 `&1GPHY_8212`;不放松设计内容比较。 +- 私有桌面上的原生库编辑器若提示其他已确认缺少 PCB 的条目,只确认准确匹配的已知通知。当前目标、未知弹窗或身份不符的窗口不自动确认。 +- 原生新增共享明确截止时间;父进程超时发出取消信号。阻塞调用返回后,以及所有日志和证据 I/O 完成、提交 OK 之前,重新检查监督进程、取消状态及期限,防止超时后迟到写入。 + +## 隔离原生证据及限制 + +证据根目录为 `diagnostics/issue2-user-feedback-20261103/`。 + +完整现场库副本保留原生地址和所有工程文件,共 38,954 个文件。原用户库始终不参与写入,完整恢复新增结束后 `source-final-recheck.json` 再次确认 25,214 个主要文件与试验前完全一致。 + +`d36b6bc` 冻结程序的实际维护按钮已在该副本完成完整备份、官方 Repair、数据库地址释放及同一 PCB 重新打开。随后原生要求选择历史 PCB 数据,应用返回 `PCB_VERSION_CHOICE_REQUIRED`,没有显示修复完成。 + +用户选择最后手动保存版本(2026/9/24 15:15)后,在确切所属的隔离会话中继续。SynchCES 和 LoadCES 成功,但原生 BackAnnotate 仍报已验证工程不允许执行。未保存、未达到 READY,失败 PCB 已正常关闭。这段人工续接不算无人值守自动修复通过。 + +空隔离库中的重新导入及工程身份核对通过,但自动校验因目标缺少 PartsDB 分区失败,不能算完整校验通过。完整依赖库中的首次同名替换完成原生移除后,新增被其他条目缺少 PCB 的提示阻塞而超时;该部分失败的检查点及完整旧库备份保留在 `diagnostics/full-update-events/`,没有自动重放。 + +完整隔离库中 IP101GR 的旧数据库地址通过官方 Repair 释放,证据为 `dependency-ip101gr-native-only/result.json`。这只是依赖数据库的原生修复诊断,没有重新打开或 READY 验收。 + +修订后对缺失的 `1GPHY_8212` 显式执行恢复新增,启用自动校验;独立证据目录为 `diagnostics/full-recovery-events/`。结果为 `UPLOADED_VERIFIED`,`safe_to_continue=true`。已知的其他缺 PCB 条目提示被自动准确确认;新增后独立重读库、三种原生逻辑导出及主要资产比较通过。随后自动执行 ForwardAnnotate、SynchCES、LoadCES、BackAnnotate,四项最终均同步,保存、关闭和进程退出确认完成,权威 `IsVerifiedReuseBlock` 返回 `raw=1 / READY`。自动校验链路耗时 126.422 秒(不含完整备份和新增身份检查)。 + +最终结果为 `full-recovery-add-result.json`,完整检查点为 `diagnostics/full-recovery-events/management-03a9dbc537f84e0faaea2ad5a2a732c2/checkpoint.json`。此结果证明修订后**重新导入加校验**的恢复路径有效,不能替代直接 ICDB Repair 后仍被原生 BackAnnotate 拒绝的失败结论,也不把用户选择历史数据后的工程称为无损修复。 + +## 尚未满足的门槛 + +服务器会话失联经实际按钮触发、自动 Repair、重新打开、保存关闭及权威 READY 的完整链路仍需通过。历史数据选择及重新导入均可能改变恢复版本,不能宣称无损恢复。Windows 10 尚未验证;当前实机为 Windows 11 / Mentor EE7.9.5。 + +## 自动化验证 + +修订代码全量 `python -X utf8 -m pytest -q`:**1095 passed,171.09 秒**。保留一条已有 pywinauto STA COM 线程模式警告。`compileall`、`git diff --check` 通过。取消发生在日志证据 I/O 中的回归先复现错误提交,再验证修复;相关集中测试 108 项通过。独立审查已复核全部代码修订,最后一项取消时序 P2 已解决,没有剩余实质问题。 + +候选包按提交归档构建。最终冻结按钮冒烟结果及构建、压缩包校验记录分别保存在证据目录的 `final-frozen-buttons/result.json`、`build-result.json`、`package-result.json`;按钮冒烟只证明界面响应,不等于现场 ICDB 自动修复验收。 diff --git a/src/cbb_refresh_verify/backup_recovery.py b/src/cbb_refresh_verify/backup_recovery.py index b6f3fc3..44655f1 100644 --- a/src/cbb_refresh_verify/backup_recovery.py +++ b/src/cbb_refresh_verify/backup_recovery.py @@ -86,17 +86,18 @@ def _source_check(source, name, original): def _align_library(project, pcb, library): - from .mentor_adapter import ensure_project_central_library, ensure_pcb_central_library - for path in (project, pcb.parent/'pcb.prj'): + from .mentor_adapter import ensure_project_central_library + from .maintenance_entry import maintenance_project_files + projects = maintenance_project_files(project, pcb) + for path in projects: if not re.findall(r'^KEY CentralLibrary "([^"]*)"', _project_text(path), re.M): raise ValueError('备份工程缺少 CentralLibrary 引用。') def already_references(path): return all((path.parent/value).resolve() == Path(library).resolve() for value in re.findall(r'^KEY CentralLibrary "([^"]*)"', _project_text(path), re.M)) - if not already_references(project): - ensure_project_central_library(project, library) - if not already_references(pcb.parent/'pcb.prj'): - ensure_pcb_central_library(pcb, library) + for path in projects: + if not already_references(path): + ensure_project_central_library(path, library) def recover_from_backup(request, source, *, progress=None, verifier=None, idle_check=None): diff --git a/src/cbb_refresh_verify/integration.py b/src/cbb_refresh_verify/integration.py index 6902479..4b5a3fa 100644 --- a/src/cbb_refresh_verify/integration.py +++ b/src/cbb_refresh_verify/integration.py @@ -41,6 +41,7 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time deadline = ActivityDeadline(timeout_seconds, clock=clock) if timeout_seconds is not None else None in_flight = {} executed = set() + protected_green = set() action_history = [] green_entry = None previous_state = None @@ -65,6 +66,8 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time # NoCES=3 is recorded as unsupported, never claimed green. if status not in (1, 2): raise IntegrationError(f'{name}: unsupported status {status}') + if status != 2: + protected_green.discard(name) for name in tuple(in_flight): if state[name] == 2: started, call_elapsed = in_flight.pop(name) @@ -74,11 +77,14 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time # When all four lamps start green, mirror the manual refresh: one # ForwardAnnotate, then only repair statuses that actually become Required. # Non-green entry keeps the previously validated four-action route. - refreshed = ('ForwardAnnotation' in executed if green_entry - else len(executed) == len(ACTIONS)) + checked = executed | protected_green + refreshed = ('ForwardAnnotation' in checked if green_entry + else len(checked) == len(ACTIONS)) if refreshed and not in_flight and all(status == 2 for status in state.values()): emit(stage='integration_complete', statuses=state, actions=action_history, - policy='green_first_only' if green_entry else 'required_full_sequence') + policy='green_first_only' if green_entry else + 'required_with_native_gates' if protected_green else 'required_full_sequence', + skipped_synchronized=sorted(protected_green)) return state if deadline is not None and deadline.expired(): raise IntegrationError('Project Integration timeout') @@ -87,14 +93,19 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time continue action_started = False for name, method in ACTIONS: - if state[name] == 2 and (name in executed or + if state[name] == 2 and (name in checked or (green_entry and name != 'ForwardAnnotation')): continue allowed = bool(value(pi, 'Is' + name + 'Allowed')) - # A green item still has to receive one Verify refresh attempt. - # Mentor may report the capability as false for that item; this - # flag is a gate only for a genuinely Required (status=1) item. - if state[name] != 2 and not allowed: + # Restored, previously verified CBBs can forbid a synchronized + # action even while another action is Required. Do not invoke a + # native-prohibited method or misreport the skip as a performed call. + if state[name] == 2 and not allowed: + protected_green.add(name) + emit(stage='integration_action_skipped', action=method, initial_status=2, + reason='synchronized_native_action_disallowed') + continue + if not allowed: emit(stage='integration_action_unavailable', action=method, initial_status=state[name], reason='mentor_not_allowed') raise IntegrationError(f'{name}: required but not allowed') diff --git a/src/cbb_refresh_verify/management.py b/src/cbb_refresh_verify/management.py index 6e96a05..38b66cf 100644 --- a/src/cbb_refresh_verify/management.py +++ b/src/cbb_refresh_verify/management.py @@ -103,7 +103,8 @@ def source_project(source, top_level_block=''): pcb = reference(source, 'PCBDesignPath') if not pcb.is_file() or reference(pcb, 'ProjectFile') != source: raise ValueError('PCB 未引用所选来源工程。') - for path in (source, pcb.parent/'pcb.prj'): + from .maintenance_entry import maintenance_project_files + for path in maintenance_project_files(source, pcb): if not path.is_file(): raise ValueError('工程缺少 PCB 工程配置。') # Healthy Expedition PCB projects can use Layout/LayoutDB.lyt without diff --git a/src/cbb_refresh_verify/native_add_failures.py b/src/cbb_refresh_verify/native_add_failures.py index e016c3d..9bbac0b 100644 --- a/src/cbb_refresh_verify/native_add_failures.py +++ b/src/cbb_refresh_verify/native_add_failures.py @@ -9,6 +9,36 @@ _COPY_ERROR = b'Error! Cannot copy iCDB base of Reusable Block project.' _LOG_LIMIT = 2*1024*1024 +def missing_other_pcb_notice(library, target, title, text, buttons): + """Only the observed editor-open information about unrelated missing PCBs.""" + from .management import validate_name + from .verify_runner import preflight_skip_reason + heading = ('The following Reusable Blocks do not contain PCB files and will not appear ' + 'in the list of available reusable blocks:') + if title != 'Library Manager' or len(text) > 131072 or len(buttons) != 1: + return None + button = buttons[0] + if (button.get('id') != 2 or button.get('class') != 'Button' + or button.get('text') not in ('OK', '确定') or button.get('enabled') is not True): + return None + lines = text.strip().splitlines() + if not lines or lines[0] != heading: + return None + names = [] + for line in lines[1:]: + if not line.strip(): continue + if not line.startswith('\t'): return None + name = line[1:] + try: validate_name(name) + except ValueError: return None + if name.casefold() == str(target).casefold() or not preflight_skip_reason(library, name): + return None + names.append(name) + if not names or len(names) != len({name.casefold() for name in names}): + return None + return {'names': names, 'button': button['hwnd']} + + def snapshot_add_log(library): path = Path(library).parent/'LogFiles/ReuseBlockLogLO.txt' try: diff --git a/src/cbb_refresh_verify/native_engineering_identity.py b/src/cbb_refresh_verify/native_engineering_identity.py index d7d7f7e..742bad6 100644 --- a/src/cbb_refresh_verify/native_engineering_identity.py +++ b/src/cbb_refresh_verify/native_engineering_identity.py @@ -129,7 +129,17 @@ def normalize_edif(data, top): raise ValueError('Malformed EDIF header timestamp.') datetime(*(int(v) for v in values)) design = one(root, b'design') - if one(design, b'cellRef')['items'][1] != top.encode('mbcs'): + top_bytes = top.encode('mbcs') + if re.fullmatch(rb'[A-Za-z][A-Za-z0-9_]*', top_bytes): + expected_identifier = top_bytes + elif re.fullmatch(rb'[0-9][A-Za-z0-9_]*', top_bytes): + # The installed exporter encodes digit-initial EDIF identifiers with + # '&' (observed cellRef &1GPHY_8212). Retain all report bytes; this is + # an exact identity check, not general name stripping or normalization. + expected_identifier = b'&' + top_bytes + else: + raise ValueError('Unsupported native EDIF RootBlock identifier representation.') + if one(design, b'cellRef')['items'][1] != expected_identifier: raise ValueError('EDIF top design differs from the requested RootBlock.') if (not any(n['items'] == [b'viewType', b'SCHEMATIC'] for n in nodes) or any(not any(n['items'][0] == tag for n in nodes) for tag in (b'page', b'figure', b'pt'))): @@ -163,8 +173,9 @@ def project_library(source): """Require one existing approved source library across every project ref.""" from .backup_recovery import _project_text source, pcb, _ = source_project(source) + from .maintenance_entry import maintenance_project_files libraries = set() - for path in (source, pcb.parent/'pcb.prj'): + for path in maintenance_project_files(source, pcb): values = re.findall(r'^KEY CentralLibrary "([^"\r\n]+)"', _project_text(path), re.M) if not values: raise ValueError('Project lacks CentralLibrary references.') for value in values: @@ -192,7 +203,8 @@ def asset_identity(source, expected_library): expected_library = Path(expected_library) if not expected_library.is_file(): raise ValueError('Approved CentralLibrary is missing.') root = source.parent - projects = (source, pcb.parent/'pcb.prj') + from .maintenance_entry import maintenance_project_files + projects = maintenance_project_files(source, pcb) databases = [] for project in projects: values = re.findall(r'^KEY iCDBDir "([^"\r\n]+)"', _project_text(project), re.M) diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index b5a1c45..fa8223c 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -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 diff --git a/tests/test_integration.py b/tests/test_integration.py index 258de82..9c6c588 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -290,6 +290,45 @@ def test_refresh_integration_skips_native_disabled_green_action(): assert pi.actions == [] +@pytest.mark.parametrize('ces_allowed', [True, False]) +def test_partial_sync_respects_protected_green_native_actions(ces_allowed): + class RestoredVerifiedBlock(GreenProjectIntegration): + IsForwardAnnotationAllowed = False + BackAnnotationStatus = 1 + + def ForwardAnnotate(self): + raise RuntimeError('Forward Annotation is not allowed on verified Reusable Blocks') + + def BackAnnotate(self): + super().BackAnnotate() + self.BackAnnotationStatus = 2 + return True + + pi = RestoredVerifiedBlock() + pi.IsSynchCESAllowed = pi.IsLoadCESAllowed = ces_allowed + events = [] + result = refresh_integration(pi, settle_seconds=0, emit=lambda **e: events.append(e)) + assert set(result.values()) == {2} + assert pi.actions == (['SynchCES', 'LoadCES', 'BackAnnotate'] if ces_allowed else ['BackAnnotate']) + assert [e['action'] for e in events if e['stage'] == 'integration_action'] == pi.actions + + +def test_skipped_green_action_cannot_hide_a_later_required_native_gate(): + class ChangedAfterCES(GreenProjectIntegration): + IsForwardAnnotationAllowed = False + BackAnnotationStatus = 1 + + def SynchCES(self): + super().SynchCES() + self.ForwardAnnotationStatus = 1 + return True + + pi = ChangedAfterCES() + with pytest.raises(IntegrationError, match='ForwardAnnotation: required but not allowed'): + refresh_integration(pi, settle_seconds=0) + assert pi.actions == ['SynchCES'] + + def test_refresh_integration_never_counts_rejected_green_action_as_completed(): class VerifiedProjectIntegration(GreenProjectIntegration): def _blocked(self, action): diff --git a/tests/test_management_diagnostics.py b/tests/test_management_diagnostics.py index 7595aa1..f8d9069 100644 --- a/tests/test_management_diagnostics.py +++ b/tests/test_management_diagnostics.py @@ -25,7 +25,7 @@ def test_raw_dispatch_diagnostics_preserve_hresult_and_known_member_ids(tmp_path def test_probe_records_dispatch_before_after_import_and_after_source_alignment(tmp_path, monkeypatch): import cbb_refresh_verify.native_management as native - import sys + import sys, os source = tmp_path/'source/Block.prj' source.parent.mkdir() source.write_text('KEY RootBlock "Block"\nKEY PCBDesignPath "PCB/Block.pcb"\n' @@ -51,7 +51,7 @@ def test_probe_records_dispatch_before_after_import_and_after_source_alignment(t raise AttributeError(name) monkeypatch.setitem(sys.modules, 'pywinauto', Uia('pywinauto')) job = {'operation': 'probe_create_add', 'source': str(source), 'library': str(library), - 'name': 'Block', 'timeout_seconds': 0} + 'name': 'Block', 'timeout_seconds': 0, 'supervisor_pid': os.getpid()} with pytest.raises(RuntimeError, match='stop before native UI'): native._probe_add_dialog_impl(App(), job, None, set(), root, library=SimpleNamespace(_oleobj_=Raw())) records = [json.loads(line) for line in (root/'dispatch-diagnostics.jsonl').read_text(encoding='utf-8').splitlines()] @@ -134,7 +134,8 @@ def test_probe_converts_desktop_pyhandle_before_uia_boundary(tmp_path, monkeypat raise RuntimeError('integer UIA boundary reached') monkeypatch.setitem(sys.modules, 'pywinauto', SimpleNamespace(Desktop=Desktop)) app = SimpleNamespace(OnReusableBlocksEditor=lambda: None) - job = {'operation': 'probe_create_add', 'source': str(source), 'name': 'Block', + import os + job = {'operation': 'probe_create_add', 'source': str(source), 'name': 'Block', 'supervisor_pid': os.getpid(), 'library': str(root/'fixture/mentor_lib.lmc'), 'timeout_seconds': 5} with pytest.raises(RuntimeError, match='integer UIA boundary reached'): native._probe_add_dialog_impl(app, job, desktop, {9}, root) diff --git a/tests/test_management_source.py b/tests/test_management_source.py index a024368..0ac2766 100644 --- a/tests/test_management_source.py +++ b/tests/test_management_source.py @@ -26,13 +26,21 @@ def test_layout_based_pcb_without_independent_icdb_directory_is_complete(healthy assert engineering_identity(source)['top_level_block'] == 'IP101GR' -@pytest.mark.parametrize('missing', ['database/icdb.dat', 'PCB/Layout/LayoutDB.lyt', 'PCB/pcb.prj']) +@pytest.mark.parametrize('missing', ['database/icdb.dat', 'PCB/Layout/LayoutDB.lyt']) def test_optional_pcb_icdb_does_not_relax_required_engineering_files(healthy_source, missing): (healthy_source.parent/missing).unlink() with pytest.raises(ValueError): source_project(healthy_source) +def test_linked_legacy_pcb_uses_explicit_main_project_without_guessing_template(healthy_source): + (healthy_source.parent/'PCB/pcb.prj').rename(healthy_source.parent/'PCB/template.prj') + assert source_project(healthy_source)[0] == healthy_source + (healthy_source.parent/'PCB/IP101GR.pcb').write_text('KEY ProjectFile "../wrong.prj"\n') + with pytest.raises(ValueError, match='PCB'): + source_project(healthy_source) + + def test_main_schematic_icdb_declaration_remains_required(healthy_source): text = healthy_source.read_text(encoding='utf-8').replace('KEY iCDBDir ".\\database"\n', '') healthy_source.write_text(text, encoding='utf-8') diff --git a/tests/test_native_add_failures.py b/tests/test_native_add_failures.py index 2072bae..c261193 100644 --- a/tests/test_native_add_failures.py +++ b/tests/test_native_add_failures.py @@ -7,6 +7,91 @@ import pytest ERROR = b'REUSE BLOCK: INFORMATION: Block\tError! Cannot copy iCDB base of Reusable Block project.\r\n' +@pytest.mark.parametrize('change', ['', 'target', 'pcb_present', 'question', 'extra_button', 'wrong_button', 'escape']) +def test_editor_missing_pcb_notice_is_only_for_unrelated_incomplete_entries(tmp_path, change): + from cbb_refresh_verify.native_add_failures import missing_other_pcb_notice + library = tmp_path/'lib.lmc'; library.touch() + work = tmp_path/'ReusableBlocksLP/Missing/Work' + (work/'PCB/Layout').mkdir(parents=True) + (work/'Missing.prj').write_text('KEY PCBDesignPath "PCB/Missing.pcb"\n') + text = ('The following Reusable Blocks do not contain PCB files and will not appear in the list of available reusable blocks:' + '\n\n\tMissing') + buttons = [{'hwnd':123, 'id':2, 'class':'Button', 'text':'确定', 'enabled':True}] + target = 'Missing' if change == 'target' else 'Current' + if change == 'pcb_present': + (work/'PCB/Missing.pcb').touch(); (work/'PCB/Layout/LayoutDB.lyt').touch() + if change == 'question': text += '\nOverwrite existing files?' + if change == 'extra_button': buttons.append({**buttons[0], 'hwnd':456}) + if change == 'wrong_button': buttons[0]['text']='Yes' + if change == 'escape': text=text.replace('\tMissing', '\t../other') + result = missing_other_pcb_notice(library, target, 'Library Manager', text, buttons) + assert result == ({'names':['Missing'], 'button':123} if not change else None) + + +@pytest.mark.parametrize('reason', ['cancelled', 'supervisor_exited']) +def test_add_cannot_submit_after_timeout_or_supervisor_exit(tmp_path, monkeypatch, reason): + from cbb_refresh_verify.native_management import _require_add_supervisor + import cbb_refresh_verify.library_lock as locks + if reason == 'cancelled': (tmp_path/'cancel-native-add.signal').touch() + monkeypatch.setattr(locks, '_supervisor_alive', lambda _:reason != 'supervisor_exited') + with pytest.raises(RuntimeError, match='NATIVE_ADD_CANCELLED'): + _require_add_supervisor({'supervisor_pid':123}, tmp_path) + + +def test_add_timeout_publishes_cancellation_without_killing_native_owner(tmp_path, monkeypatch): + import cbb_refresh_verify.native_management as native + monkeypatch.setattr(native, '_retained_workers', []) + class Running: + def __init__(self, *args): pass + def poll(self): return None + def close(self): pytest.fail('live worker must not be killed') + adapter=native.NativeManagementAdapter(worker_factory=Running) + result=adapter._launch_worker({'operation':'add','desktop':'CbbManagement_test','timeout_seconds':0},tmp_path) + assert not result['safe_to_continue'] and (tmp_path/'cancel-native-add.signal').exists() + assert len(native._retained_workers)==1 + + +def test_cancellation_during_log_evidence_prevents_native_submit(tmp_path, monkeypatch): + import os,sys + from types import SimpleNamespace + import win32gui,win32process + import cbb_refresh_verify.native_management as native + import cbb_refresh_verify.native_add_failures as failures + from test_management import project + source=project(tmp_path/'source',name='Block') + root=tmp_path/'native-add';root.mkdir() + library=root/'fixture/lib.lmc';library.parent.mkdir();library.touch() + for path in (source,source.parent/'PCB/pcb.prj'): + with path.open('a',encoding='utf-8') as stream:stream.write(f'KEY CentralLibrary "{library}"\n') + state={'new':False,'posted':[]} + tab=SimpleNamespace(window_text=lambda:'Logical - Physical',is_selected=lambda:True) + monkeypatch.setitem(sys.modules,'pywinauto',SimpleNamespace(Desktop=lambda **k:SimpleNamespace( + window=lambda **k:SimpleNamespace(descendants=lambda **k:[tab])))) + monkeypatch.setattr(win32process,'GetWindowThreadProcessId',lambda h:(1,99)) + monkeypatch.setattr(win32gui,'IsWindowVisible',lambda h:True) + monkeypatch.setattr(win32gui,'IsWindowEnabled',lambda h:True) + monkeypatch.setattr(win32gui,'GetWindowText',lambda h:{100:'Reusable Blocks',200:'New Reusable Block'}[int(h)]) + monkeypatch.setattr(win32gui,'GetDlgItem',lambda h,c:110 if h==100 else 210 if c==1083 else c+1000) + monkeypatch.setattr(win32gui,'GetClassName',lambda h:'Edit') + monkeypatch.setattr(native,'_set_probe_field',lambda *a:None) + def post(h,*args): + state['posted'].append(h) + if h==110:state['new']=True + monkeypatch.setattr(win32gui,'PostMessage',post) + original=native._write_json + def write(path,data): + original(path,data) + if path.name=='native-add-log-baseline.json':(root/'cancel-native-add.signal').touch() + monkeypatch.setattr(native,'_write_json',write) + monkeypatch.setattr(failures,'observe_add_failure',lambda *a:(_ for _ in ()).throw(RuntimeError('unsafe OK posted'))) + desktop=SimpleNamespace(EnumDesktopWindows=lambda:[100,200] if state['new'] else [100]) + job={'operation':'probe_create_add','source':str(source),'library':str(library), + 'name':'Block','timeout_seconds':5,'supervisor_pid':os.getpid()} + with pytest.raises(RuntimeError,match='NATIVE_ADD_CANCELLED'): + native._probe_add_dialog_impl(SimpleNamespace(OnReusableBlocksEditor=lambda:None),job,desktop,{99},root) + assert state['posted']==[110] + + def test_copy_failure_log_must_be_new_and_match_current_block(tmp_path): from cbb_refresh_verify.native_add_failures import snapshot_add_log, fresh_add_failure library = tmp_path/'lib.lmc'; library.touch() diff --git a/tests/test_native_engineering_identity.py b/tests/test_native_engineering_identity.py index 2d2f93b..aa44ab1 100644 --- a/tests/test_native_engineering_identity.py +++ b/tests/test_native_engineering_identity.py @@ -72,6 +72,24 @@ def test_assets_require_correct_library_references_and_unknown_bytes(project): asset_identity(source, library) +def test_linked_legacy_identity_preserves_template_bytes_and_aligns_only_active_project(project, tmp_path): + from cbb_refresh_verify.native_engineering_identity import asset_identity, project_library + from cbb_refresh_verify.backup_recovery import _align_library + source, library = project + template = source.parent/'PCB/template.prj' + (source.parent/'PCB/pcb.prj').rename(template) + original = template.read_bytes() + before = asset_identity(source, library) + assert 'PCB/template.prj' in before['assets'] + other = tmp_path/'other.lmc'; other.write_bytes(b'other library') + _align_library(source, next((source.parent/'PCB').glob('*.pcb')), other) + assert template.read_bytes() == original + assert project_library(source) == other + assert asset_identity(source, other) == before + template.write_bytes(original+b'unknown engineering change') + assert asset_identity(source, other) != before + + @pytest.mark.parametrize('relative', ['database/cdbsvr/persistent.dat', 'database/cdbsvr/new-engineering/config.bin', 'database/cdbsvr/events/custom.dat', 'database/cdbsvr/plog/unknown-engineering.bin']) @@ -199,3 +217,14 @@ def test_edif_only_normalizes_root_written_timestamp_preserving_geometry_and_pro for bad in [first[:-1], first.replace(b'(edifVersion 2 0 0)', b'(edifVersion 1 0 0)'), first.replace(b'(cellRef Top', b'(cellRef Other'), first.replace(b'(pt 1 2)', b'')]: with pytest.raises(ValueError): normalize_edif(bad, 'Top') + + +def test_edif_binds_native_escaped_digit_initial_root_without_changing_design_bytes(): + from cbb_refresh_verify.native_engineering_identity import normalize_edif + report = edif_report().replace(b'cell Top', b'cell &1GPHY_8212').replace(b'cellRef Top', b'cellRef &1GPHY_8212') + normalized = normalize_edif(report, '1GPHY_8212') + assert b'(cell &1GPHY_8212' in normalized and b'(cellRef &1GPHY_8212' in normalized + for wrong in ('1GPHY_8211', '&1GPHY_8212'): + with pytest.raises(ValueError): normalize_edif(report, wrong) + with pytest.raises(ValueError): + normalize_edif(report.replace(b'&1GPHY_8212', b'1GPHY_8212'), '1GPHY_8212') -- 2.39.5 From c85696080de2a6a00ba28ba926b0d8ec4fc401f7 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: Tue, 3 Nov 2026 16:44:30 +0800 Subject: [PATCH 42/59] perf: replace automatic backups with manual backup reminders --- README.md | 6 ++- docs/releases/2026-11-03-manual-backup.md | 53 +++++++++++++++++++ .../plans/2026-11-03-manual-backup.md | 36 +++++++++++++ src/cbb_refresh_verify/backup_recovery.py | 26 ++++----- src/cbb_refresh_verify/maintenance_entry.py | 2 +- src/cbb_refresh_verify/management.py | 21 ++++---- .../management_transaction.py | 19 +++---- src/cbb_refresh_verify/mentor_adapter.py | 19 ++----- src/cbb_refresh_verify/native_management.py | 2 +- src/cbb_refresh_verify/native_repair.py | 17 +++++- src/cbb_refresh_verify/native_repair_ui.py | 9 ++-- src/cbb_refresh_verify/scoped_manifest.py | 37 +++++++++++++ src/cbb_refresh_verify/ui/backup_notice.py | 5 ++ .../ui/backup_recovery_dialog.py | 10 +++- src/cbb_refresh_verify/ui/main_window.py | 24 ++++++--- .../ui/management_dialog.py | 20 ++++--- tests/test_backup_recovery.py | 14 +++-- tests/test_backup_recovery_dialog.py | 7 +++ tests/test_management.py | 48 ++++++++++++++--- tests/test_management_native_transaction.py | 11 ++-- tests/test_management_ui.py | 2 +- tests/test_manual_backup_ui.py | 51 ++++++++++++++++++ tests/test_native_repair.py | 36 ++++++++++--- tests/test_ui.py | 2 + tests/test_ui_filters.py | 1 + tests/test_verify_worker_stability.py | 9 ++++ 26 files changed, 385 insertions(+), 102 deletions(-) create mode 100644 docs/releases/2026-11-03-manual-backup.md create mode 100644 docs/superpowers/plans/2026-11-03-manual-backup.md create mode 100644 src/cbb_refresh_verify/ui/backup_notice.py create mode 100644 tests/test_manual_backup_ui.py diff --git a/README.md b/README.md index 468924a..7cbf311 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # CBB 中心库上传后校验工具 + +当前临时分支的备份策略(2026-11-03):应用不再自动备份中心库或 CBB 工程。新增、替换、移除、校验、ICDB 维护和恢复在执行前提示手动备份;请自行保留可用副本。导入暂存、恢复用隔离验证库和轻量操作证据仍会生成,Mentor 原生 Repair 自身的备份行为保持不变。详见[自动备份检查与调整](docs/releases/2026-11-03-manual-backup.md)。此条优先于下方历史版本的备份说明。 当前补丁:**[v0.2.3-beta.1](docs/releases/v0.2.3-beta.1.md)(Beta 预发布,待现场验收)**,替代 0.2.2-beta.1。数据库所有权或关闭状态不确定时停止批次、保留现场;不自动移走地址文件、接管带锁设计或强杀 PCB 进程树。新增全库 ICDB 维护、同名 CBB 完整备份恢复弹窗及严格的原生复核流程。详见 [v0.2.3-beta.1 发布说明](docs/releases/v0.2.3-beta.1.md)。下方日期记录描述历史版本,以本补丁说明为准。 @@ -7,7 +9,7 @@ 针对 `The last server session ... lost the connection to the database`,修复分支增加 `ICDB_REPAIR_REQUIRED` 专项识别、共享符号库路径诊断及停止批次处理。现场原生修复步骤及实现边界见 [iCDB 旧服务器会话失联分析](docs/diagnostics/2026-10-27-icdb-lost-server-session.md)。 -当前分支会在目标明确、完整备份和所有权释放证据齐全时尝试一次同版本原生 Repair;共享符号库没有 PRJ 入口时停止并要求人工处理。 +当前分支会在目标明确、所有权释放和独占访问证据齐全时尝试一次同版本原生 Repair;操作前请手动备份,共享符号库没有 PRJ 入口时停止并要求人工处理。 ## 功能边界 @@ -201,7 +203,7 @@ Mentor 桥耗时需在目标机器另行测量。 - `save.log`:`LICENSE_OK / SAVE_OK / IS_SAVED / POST_SAVE_SYNCHRONIZED / CLOSE_OK / QUIT_OK`; - `result.json`:状态、中心库标志读取结果、阻断原因; - `image-status-map.json`:按中心库路径隔离的本机动态 `image_id` 校准快照(当前格式 `schema_version=2`);旧版本或无版本缓存会自动忽略并重新校准,日志会记录当前样本、缓存样本、Ready 主 ID、未映射 ID 和逐项决策原因; -- `backup/`:仅本地盘操作前的稳定文件副本;网络盘/UNC 跳过前置递归备份(`backup_skipped`);`database/LogFiles/tmp` 等易变目录和日志文件会排除,备份超时或失败不会阻断 PCB 校验。仅在成功且确认安全后清理该目录,失败时保留已有副本及日志;它不是完整数据库回滚备份。 +- `backup/`:历史版本的辅助文件副本;当前分支不再生成,所有路径记录 `backup_skipped / manual_backup_policy`。已有历史副本不由本轮改动清理。 超时采用无进展计时:界面和批量请求默认 600 秒无进展超时;它不是整个 CBB 的固定总时限,而是连续 600 秒没有可观察进展时才终止。PCB/COM 阶段切换、Project Integration 状态变化、保存标记变化和弹窗处理都会刷新计时器;重复的状态轮询不算进展。需要更快失败时仍可在界面将单项预算调低,但 30 秒只适合已确认 PCB 启动稳定的环境。 diff --git a/docs/releases/2026-11-03-manual-backup.md b/docs/releases/2026-11-03-manual-backup.md new file mode 100644 index 0000000..bb12a5d --- /dev/null +++ b/docs/releases/2026-11-03-manual-backup.md @@ -0,0 +1,53 @@ +# 自动备份检查与调整 + +按用户 2026-11-03 最新要求:取消应用操作前自动备份,改为弹窗提示手动备份。本要求覆盖早期计划中的“必须完整备份后执行”。仍在 `codex/temp-issue-2-icdb-recovery` 临时分支,未合并或发布。 + +## 检查结果与调整范围 + +| 操作 | 检查到的旧行为 | 当前调整 | +| --- | --- | --- | +| 新增 CBB | 每项复制整个中心库,即使没有旧同名工程 | 取消,记录只读内容基线 | +| 同名替换 | 每项复制整个中心库,再移除并导入 | 取消,保留逐项阶段及失败记录;失败时使用手动备份恢复 | +| 移除 | 每项复制整个中心库 | 取消,原生移除后重新读库确认缺失 | +| 普通校验 | 本地工程辅助备份,网络路径跳过 | 统一取消,记录 `manual_backup_policy` | +| ICDB 修复 | 应用复制完整工程,原生工具另外备份 | 取消应用复制,保留独占访问、原生自身备份及成功证据要求 | +| 从备份恢复 | 整库回退备份,再从中复制验证库;额外保留来源副本 | 去掉回退备份和重复来源副本,直接生成一份隔离验证库 | +| 读取、搜索、筛选 | 无自动备份 | 保持只读 | + +## 仍需生成的数据 + +- 新增/替换的来源暂存及原生内容导出副本:防止原生工具直接改写用户提供的源工程。 +- 恢复的隔离验证库:用于在安装前完成实际校验,会占用磁盘并花费复制时间,不能等同于未经修改的整库备份。 +- 恢复安装时保留旧 Work:仅同卷目录改名,不复制数据,用于安装失败时还原目录交换。 +- 工程库引用修正前的单个 PRJ 原文、日志、清单及检查点:属于轻量变更证据,不是工程或中心库备份。 +- Mentor 原生 Repair 自身生成的备份:属于原生修复流程,应用没有绕过。 + +独立研究探针中显式创建的实验备份及现存历史备份未删除。桌面生产路径不再触发上述旧的自动工程/整库备份辅助函数。 + +## 用户交互和约束 + +管理操作沿用原确认窗口,增加“应用不会自动备份,请先手动备份中心库及相关工程”。普通校验、维护、恢复同样在启动前提示;每批一次,取消不启动后台任务。应用不检查也不声称用户已经创建了手动备份。 + +取消备份不会取消串行任务、库锁、数据库独占检查、目标变更检查、来源身份核对、重新读库验证或未完成任务检查点。替换仍是移除后重新导入,非原子操作;部分失败必须使用手动备份及检查点恢复,不自动重放。 + +直接 ICDB Repair 的现场完整验收缺口仍保留,本次备份策略调整不代表该故障已解决。 + +## 隔离原生验证 + +在既有独立实验库 `diagnostics/reimport-8212/native-add/fixture/mentor_lib.lmc` 通过正式管理引擎实测: + +- 原生移除 `1GPHY_8212` 后复读确认缺失,结果 `REMOVED`,安全释放,耗时 39.391 秒。 +- 随后原生重新导入同一独立来源,复读名称/分区、原生逻辑导出及主要资产身份确认通过,结果 `UPLOADED`,安全释放,耗时 101.359 秒。 +- 两项操作均没有生成 `library-before` 整库备份,来源主要文件清单未变。测试未启用自动校验,不将上传成功解释为 READY。 + +证据:`diagnostics/manual-backup-20261103/native-cycle-result.json`、`native-cycle-accepted.json` 和 `diagnostics/mb-events/`。上述耗时包含原生启动、复读和内容检查,不能当作与旧版同环境对照的提速比例。 + +开始实测前发现上一版应用在完整隔离库 IP101GR 上保留了“继续以读写方式打开”提示。仅向已核对进程命令和私有桌面的该提示发送“否”,两个 PCB 进程正常退出;没有强杀、保存或删除原生地址。原失败证据与取消记录保留在 `previous-test-cancel.json`。随后通过正式释放检查解除小实验库旧阻止记录,未绕过库锁。 + +## 代码验证 + +最终全量 Python 回归:**1111 passed,192.49 秒**,一条已有 pywinauto STA COM 线程模式警告。集中回归 297 项通过;占用检查修订后相关 151 项通过。编译及 diff 检查通过,独立复审无剩余实质问题。 + +审查发现取消整库复制时一并丢失了全库数据库独占检查,已改为不复制数据的 `build_exclusive_library_manifest`:记录基线和写入前检查均同时持有所有 `.lmc` / `icdb.dat` 的独占读取句柄。回归覆盖主库、共享库、其他 CBB 数据库仍被共享读取句柄占用的情况,确认未进入原生写入。 + +候选包从提交归档构建。最终冻结程序手动备份提示及取消测试记录位于 `diagnostics/manual-backup-20261103/frozen-prompts/result.json`,构建和交付信息保存在同一证据目录。 diff --git a/docs/superpowers/plans/2026-11-03-manual-backup.md b/docs/superpowers/plans/2026-11-03-manual-backup.md new file mode 100644 index 0000000..640bc21 --- /dev/null +++ b/docs/superpowers/plans/2026-11-03-manual-backup.md @@ -0,0 +1,36 @@ +# Manual Backup Implementation Plan + +**Goal:** 按用户最新要求取消应用操作前的自动备份,改为提示手动备份,减少整库复制和重复哈希耗时。 + +**Architecture:** 管理操作使用只读内容基线、暂存来源、原生身份核对和现有库锁。备份责任转为用户;应用不验证或声称手动备份已存在。恢复操作只创建用于隔离校验的库副本,不再额外复制完整回退库。原生 Repair 自身备份保持原生行为。 + +**Tech Stack:** Python 3.12 / PySide6 / Mentor EE7.9.5。 + +用户已明确指定取消自动备份并提示手动备份,本计划直接执行。沿用临时分支,不合并发布、不修改原用户库。保留既有 `review.diff`。 + +## 范围与检查结果 + +| 操作 | 旧行为 | 新行为 | +| --- | --- | --- | +| 新增、替换、移除 | 每项完整中心库备份 | 手动备份提示;只读基线、检查点及来源暂存 | +| 普通校验 | 本地 CBB 辅助备份,网络跳过 | 所有路径统一取消应用辅助备份 | +| ICDB Repair | 应用完整工程备份,加原生自身备份 | 取消应用复制,保留独占读取/身份检查及原生自身行为 | +| 从备份恢复 | 整库回退备份再复制验证库;额外来源副本 | 直接复制一次隔离验证库,来源直接暂存到验证工程 | +| 读取/搜索/筛选 | 无备份 | 保持只读 | + +验证工程副本和来源暂存是任务输入,不能删除后直接改原始来源。恢复安装中的旧 Work 改名保留是目录交换保护,不复制数据,继续保留。历史证据与备份不清理。独立研究工具的显式备份不属于桌面自动操作。 + +## Tasks + +- [ ] 管理:先增加不复制整库及基线变化阻止写入的回归;移除事务授权对备份目录的依赖,绑定基线清单,保留身份、监督进程、活跃库锁和检查点验证;更新对应测试。 +- [ ] 校验/Repair/恢复:先写不调用自动备份、保持独占检查及单一验证库副本的回归;改调用路径和进度说明,测试取消/占用/来源及目标变化继续阻止操作。 +- [ ] 界面:将手动备份提示加入管理、校验、维护、恢复执行确认;取消不启动任务,每批一次,不伪称备份已完成。移除自动备份承诺。 +- [ ] 验证:集中测试、全量 Python 测试、编译与 diff 检查、独立代码审查;隔离原生移除验证不创建整库备份;新冻结包验证提示和取消反馈。更新验证记录,提交并构建候选。 + +## Review Focus + +- 没有备份目录仍能通过真实管理事务校验;错库、过期锁、来源变化、基线记录篡改仍拒绝。 +- 替换移除成功但新增失败必须保留部分失败检查点,明确使用用户手动备份恢复。 +- 删除备份调用不能同时删掉数据库独占和取消检查。 +- 恢复仍隔离验证,并保留来源/目标变更检查与失败安装回退。 +- UI 取消确认无写入;逐项自动校验不重复弹窗;没有目标时不弹无意义提示。 diff --git a/src/cbb_refresh_verify/backup_recovery.py b/src/cbb_refresh_verify/backup_recovery.py index 44655f1..9223dbe 100644 --- a/src/cbb_refresh_verify/backup_recovery.py +++ b/src/cbb_refresh_verify/backup_recovery.py @@ -128,7 +128,7 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c installed = False record = {'operation': 'same_cbb_complete_project_restore', 'source': str(source), 'library': str(library), 'block': request.block_name, 'installed': False, - 'validation_library': str(validation_dir/library.name)} + 'validation_library': str(validation_dir/library.name), 'backup_policy': 'manual'} def emit(stage, message, **details): if progress: progress({'stage': stage, 'block': request.block_name, 'message': message, **details}) @@ -165,22 +165,17 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c library_size = sum(info.st_size for info in _scan(library_root).values() if stat.S_ISREG(info.st_mode)) source_size = sum(info.st_size for info in _scan(source.parent).values() if stat.S_ISREG(info.st_mode)) same_drive = root.anchor.casefold() == validation_dir.anchor.casefold() - required = library_size * (2 if same_drive else 1) + source_size * 3 + required = (library_size + source_size * 2) if same_drive else 0 if (shutil.disk_usage(root).free < required - or shutil.disk_usage(validation_base).free < library_size + source_size * 2): - raise ValueError('磁盘空间不足:需要容纳中心库回退副本和验证副本。') + or shutil.disk_usage(validation_base).free < library_size + source_size * 2 + or shutil.disk_usage(library_root).free < source_size): + raise ValueError('磁盘空间不足:需要容纳隔离验证库和待安装工程。') (root/'recovery-request.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8') - emit('backup', '正在保留完整中心库副本…') + emit('prepare_source', '正在准备隔离验证库;应用不自动创建回退备份…') library_scope = ManifestScope.library(request.block_name, library.name) - original_manifest = _copy_verified(library_root, root/'library-before', scope=library_scope, - progress=copy_progress('backup', '正在保留中心库副本:')) - (root/'library-before-manifest.json').write_text(json.dumps(original_manifest, indent=2), encoding='utf-8') - emit('prepare_source', '正在准备备份验证副本…') - source_manifest = _copy_verified(source.parent, root/'source-before', - progress=copy_progress('prepare_source', '正在保留备份工程副本:')) - (root/'source-before-manifest.json').write_text(json.dumps(source_manifest, indent=2), encoding='utf-8') - _copy_verified(root/'library-before', validation_dir, scope=library_scope, + original_manifest = _copy_verified(library_root, validation_dir, scope=library_scope, progress=copy_progress('prepare_source', '正在准备验证库:')) + (root/'library-baseline-manifest.json').write_text(json.dumps(original_manifest, indent=2), encoding='utf-8') staged_library = validation_dir/library.name staged_work = staged_library.parent/'ReusableBlocksLP'/request.block_name/'Work' # Both paths are newly created private copies; originals are retained. @@ -188,8 +183,9 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c if not resolved_work.is_relative_to(validation_dir.resolve()): raise ValueError('验证副本工程路径越界。') shutil.rmtree(resolved_work) - _copy_verified(root/'source-before', staged_work, + source_manifest = _copy_verified(source.parent, staged_work, progress=copy_progress('prepare_source', '正在准备备份验证工程:')) + (root/'source-manifest.json').write_text(json.dumps(source_manifest, indent=2), encoding='utf-8') staged_project = staged_work/source.name staged_pcb = staged_work/source_pcb.relative_to(source.parent) _align_library(staged_project, staged_pcb, staged_library) @@ -239,7 +235,7 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c if final.status == 'PASS' and (final.library_flag != 'GREEN' or not final.safe_to_continue): final = replace(final, status='FAIL', stderr='目标工程恢复后的 Ready 状态未确认。') if final.status != 'PASS': - final = replace(final, stderr=(final.stderr + '\n原工程和完整库副本已保留,请查看恢复记录。').strip()) + final = replace(final, stderr=(final.stderr + '\n原 Work 已改名保留;应用未创建完整库备份,请查看恢复记录并使用手动备份。').strip()) return save(final) except Exception as error: return save(VerifyResult('FAIL' if installed else 'BLOCKED', stderr=str(error), diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index a033e10..670beb4 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -197,7 +197,7 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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='先完整备份工程与地址记录,再由官方工具检查/修复') + emit('native_repair_start', detail='核对占用后由官方工具检查/修复;应用不自动备份') # Repeat immediately before dispatch; worker repeats after complete # backup and before launching its native process as well. with maintenance_target_guard(request, context): pass diff --git a/src/cbb_refresh_verify/management.py b/src/cbb_refresh_verify/management.py index 38b66cf..b7be27d 100644 --- a/src/cbb_refresh_verify/management.py +++ b/src/cbb_refresh_verify/management.py @@ -7,7 +7,8 @@ import re from uuid import uuid4 from .library_lock import LibraryLock, LibraryBusy -from .scoped_manifest import ManifestScope, build_manifest, copy_verified, hash_file, scan_tree +from .scoped_manifest import (ManifestScope, build_manifest, build_exclusive_library_manifest, + copy_verified, hash_file, scan_tree) @dataclass(frozen=True) @@ -179,14 +180,14 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro history.append({'stage': stage, 'time': datetime.now(timezone.utc).isoformat(), **details}) if report: _write_json(report, {**asdict(request), 'stage': stage, 'status': 'RUNNING', - 'safe_to_continue': False, 'history': history}) + 'safe_to_continue': False, 'history': history, 'backup_policy': 'manual'}) if progress: progress({'stage': stage, 'block': request.name, 'operation': request.operation, **details}) def finish(status, detail='', safe=True): result = ManagementResult(request.operation, request.library, request.name, request.partition, request.source, request.auto_verify, 'complete', status, safe, detail, report) if report: - _write_json(report, {**asdict(result), 'history': history}) + _write_json(report, {**asdict(result), 'history': history, 'backup_policy': 'manual'}) return result from .com_runtime import initialize_com, uninitialize_com initialized = initialize_com() @@ -202,7 +203,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro raise ValueError('中心库必须为现有 LMC 文件。') evidence = Path(request.evidence_dir).resolve() if evidence.is_relative_to(library_root) or library_root.is_relative_to(evidence): - raise ValueError('操作记录与备份必须位于目标中心库之外,且不能包含目标库。') + raise ValueError('操作记录与暂存目录必须位于目标中心库之外,且不能包含目标库。') capability = adapter.capabilities().get(request.operation, Capability(False, '未验证的原生接口')) if not capability.supported: return finish('UNSUPPORTED', capability.reason) @@ -258,9 +259,9 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro # Native/project owners must be idle before snapshot or mutation. idle_check(library, target_project, target_pcb) scope = ManifestScope.library(request.name, library.name) - checkpoint('backup') - original = copy_verified(library_root, root/'library-before', scope=scope, progress=progress) - _write_json(root/'library-before-manifest.json', original) + checkpoint('baseline') + original = build_exclusive_library_manifest(library_root, scope=scope, progress=progress) + _write_json(root/'library-baseline-manifest.json', original) staged = request identity = None if source: @@ -279,8 +280,8 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro if not owner_safe or proof.identity is None: return finish('UPLOAD_FAILED', proof.detail, owner_safe) identity = proof.identity - if build_manifest(library_root, scope=scope) != original: - raise OSError('备份后中心库内容变化,停止写入。') + if build_exclusive_library_manifest(library_root, scope=scope, progress=progress) != original: + raise OSError('内容基线记录后中心库发生变化,停止写入。') if request.operation in ('remove', 'update'): checkpoint('native_remove') mutated = True; owner_safe = False @@ -299,7 +300,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro removed = True checkpoint('removed') if request.operation == 'remove': - return finish('REMOVED', '已备份并由原生操作移除,重新读取目录确认不存在。') + return finish('REMOVED', '已由原生操作移除,重新读取目录确认不存在;应用未创建备份。') checkpoint('native_add') mutated = True; owner_safe = False native = adapter.add(staged, root/'add') diff --git a/src/cbb_refresh_verify/management_transaction.py b/src/cbb_refresh_verify/management_transaction.py index ff23f91..6b2743d 100644 --- a/src/cbb_refresh_verify/management_transaction.py +++ b/src/cbb_refresh_verify/management_transaction.py @@ -1,4 +1,4 @@ -"""Prepared Add authority bound to the engine checkpoint, snapshot and live lease.""" +"""Prepared Add authority bound to the engine checkpoint, baseline and live lease.""" import json import os from pathlib import Path @@ -6,7 +6,7 @@ import threading from uuid import uuid4 from .management import _write_json, engineering_identity -from .scoped_manifest import build_manifest, ManifestScope +from .scoped_manifest import build_manifest def _read(path): @@ -62,18 +62,16 @@ def prepare_transaction(request, root, expected_identity, *, mentor_home, fixtur if (checkpoint.get('status') != 'RUNNING' or checkpoint.get('stage') != 'prepare_identity' or Path(checkpoint['library']).resolve() != library or checkpoint['name'] != request.name or checkpoint['partition'] != request.partition - or not {'backup', 'stage_source'}.issubset(stages)): - raise ValueError('Native Add requires the engine backup/staging checkpoint.') - backup = _read(root/'library-before-manifest.json') - if build_manifest(root/'library-before', scope=ManifestScope.library(request.name, library.name)) != backup: - raise ValueError('Management backup has not been verified.') + or not {'baseline', 'stage_source'}.issubset(stages)): + raise ValueError('Native Add requires the engine baseline/staging checkpoint.') + library_baseline = _read(root/'library-baseline-manifest.json') baseline = _read(root/'source-identity/identity.json') if baseline.get('safe_to_continue') is not True or baseline.get('identity') != expected_identity: raise ValueError('Native source identity evidence is missing or changed.') record = {**current_lease(library), 'nonce': uuid4().hex, 'library': str(library), 'name': request.name, 'partition': request.partition, 'top_level_block': request.top_level_block, 'source_name': source.name, 'source_identity': engineering_identity(source), - 'source_manifest': build_manifest(source.parent), 'backup_manifest': backup, + 'source_manifest': build_manifest(source.parent), 'library_baseline': library_baseline, 'expected_identity': expected_identity, 'fixture': fixture} _authorize(record, mentor_home) _write_json(root/'native-add-prepared.json', record) @@ -105,11 +103,10 @@ def validate_transaction(job, root, *, check_source=True): _authorize(record, job['mentor_home']) baseline = _read(root/'source-identity/identity.json') if (baseline.get('safe_to_continue') is not True or baseline.get('identity') != record['expected_identity'] - or build_manifest(root/'library-before', scope=ManifestScope.library(record['name'], library.name)) - != record['backup_manifest'] + or _read(root/'library-baseline-manifest.json') != record['library_baseline'] or check_source and (build_manifest(source.parent) != record['source_manifest'] or engineering_identity(source) != record['source_identity'])): - raise ValueError('Native Add baseline, backup or staged source proof changed.') + raise ValueError('Native Add library baseline or staged source proof changed.') process = win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE, False, record['supervisor_pid']) duplicate = None try: diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index a848449..bf03284 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -621,28 +621,15 @@ def execute_job(job, emit): pcb_before = {pid for pid, _, _ in pcb_processes()} if pcb_before: raise RuntimeError(f'PCB_SESSION_ACTIVE: 已有 PCB 会话,请正常关闭后再校验: {sorted(pcb_before)}') - # Do not recursively snapshot a live CBB over SMB immediately before - # launching Mentor. The backup is evidence only; on Win10 a timed-out - # robocopy can retain network handles/oplocks and make Verify nondeterministic. + # Backups are user-managed on local and network libraries alike. mapped_drive = str(job.get('mapped_drive') or '') mapped_remote_root = str(job.get('mapped_remote_root') or '') if mapped_drive and mapped_remote_root: from .network_path import ensure_mapped_drive_visible mapping = ensure_mapped_drive_visible(mapped_drive, mapped_remote_root) emit(stage='mapped_drive_context', **mapping) - emit(stage='backup', block=job['block_name']) - if is_network_backed_path(block_root, mapped_drive=mapped_drive): - backup = {'status': 'SKIPPED', - 'detail': '网络盘 CBB 跳过递归备份,避免 robocopy 与 Mentor 竞争 SMB 文件锁'} - emit(stage='backup_skipped', reason='network_source', source=str(block_root)) - else: - backup_budget = backup_timeout_seconds(block_root) - emit(stage='backup_budget', timeout_seconds=backup_budget) - backup = best_effort_backup(block_root, root/'backup'/block_root.name, - timeout_seconds=backup_budget, emit=emit) - emit(stage='backup_done', status=backup.get('status'), detail=backup.get('detail')) - # Backup is evidence only, never a prerequisite for launching PCB. Volatile - # database/log directories are intentionally excluded to avoid file locks. + emit(stage='backup_skipped', reason='manual_backup_policy', source=str(block_root), + detail='应用不自动备份;请在操作前手动备份。') from .icdb_ownership import database_directories, unreleased_addresses for database in database_directories(job['lmc_file'], job['project'], job['pcb']): icdb_address = prepare_icdb_address(database, root, emit=emit) diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index fa8223c..8673dcc 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -101,7 +101,7 @@ class NativeManagementAdapter: 'add': Capability(not add_reason and _PRODUCTION_ADD_VALIDATED, add_reason or '已验证 EE7.9.5 原生导入及完整工程复读;仅 ASCII 名称、Logical-Physical 和 DxDesigner 库。'), 'update': Capability(not add_reason and _PRODUCTION_ADD_VALIDATED and _PRODUCTION_UPDATE_VALIDATED, - add_reason or '已验证同名原生替换:完整备份后移除并重新导入,失败时保留恢复证据。'), + add_reason or '已验证同名原生替换:移除并重新导入;操作前请手动备份。'), } def _worker(self, job, evidence): diff --git a/src/cbb_refresh_verify/native_repair.py b/src/cbb_refresh_verify/native_repair.py index 67772fa..3bf3723 100644 --- a/src/cbb_refresh_verify/native_repair.py +++ b/src/cbb_refresh_verify/native_repair.py @@ -1,4 +1,4 @@ -"""Backed-up, single-attempt recovery through the installed native tool.""" +"""Single-attempt recovery through the installed native tool.""" from dataclasses import dataclass import hashlib import json @@ -48,6 +48,19 @@ def _hash(path): return digest.hexdigest() +def check_repair_access(target): + """Preserve the former pre-copy ownership checks without copying data.""" + import win32con, win32file, pywintypes + from .scoped_manifest import scan_tree + scan_tree(target.project.parent) # Reject links before native traversal. + try: + handle = win32file.CreateFile(str(target.database/'icdb.dat'), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + except pywintypes.error as error: + raise OSError(f'REPAIR_DATABASE_IN_USE: 无法独占读取数据库:{error}') from error + handle.Close() + + def backup_project(target, destination): """Keep every original file, holding icdb.dat exclusively during the copy.""" import win32con, win32file, pywintypes @@ -130,7 +143,7 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None): target = select_repair_target(request, diagnosis) if progress: progress({'stage': 'native_repair_start', 'block': request.block_name, - 'detail': '正在备份并尝试原生数据库修复', 'project': str(target.project)}) + 'detail': '正在核对占用并尝试原生数据库修复;应用不自动备份', 'project': str(target.project)}) from .mentor_adapter import pcb_processes, _process_details if pcb_processes() or _process_details('LibraryManager.exe'): raise ValueError('REPAIR_DATABASE_IN_USE: 原生 PCB/Library Manager 会话尚未退出') diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index b1366ff..a7dda85 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -182,7 +182,7 @@ def _native_evidence(project, database, pid, started_ns, baseline, *, output_tex def repair_worker_main(job_path): import win32con, win32gui - from .native_repair import RepairTarget, backup_project + from .native_repair import RepairTarget, check_repair_access from .mentor_paths import discover_sdd_home from .icdb_recovery import snapshot_log job = json.loads(Path(job_path).read_text('utf-8')) @@ -222,10 +222,11 @@ def repair_worker_main(job_path): if (checked['target'].project.resolve() != project.resolve() or checked['target'].database.resolve() != database.resolve()): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED') - backup_project(RepairTarget(project, database), root/'complete-backup') - emit(stage='complete_backup_verified', path=str(root/'complete-backup')) + check_repair_access(RepairTarget(project, database)) + emit(stage='backup_skipped', reason='manual_backup_policy', + detail='应用不自动备份;Mentor 原生修复仍可能生成自身备份。') if Path(job['cancel_signal']).exists(): - raise ValueError('REPAIR_CANCELLED_AFTER_BACKUP') + raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') if maintenance_context is not None: with maintenance_target_guard(maintenance_request, maintenance_context): pass baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt') diff --git a/src/cbb_refresh_verify/scoped_manifest.py b/src/cbb_refresh_verify/scoped_manifest.py index 6f8286b..597e82c 100644 --- a/src/cbb_refresh_verify/scoped_manifest.py +++ b/src/cbb_refresh_verify/scoped_manifest.py @@ -227,6 +227,43 @@ def build_manifest(root, *, scope=None, exclude=None, progress=None, hash_functi return _manifest(scope, files) +def build_exclusive_library_manifest(root, *, scope, progress=None): + """Check every native database is idle while hashing, without making a copy.""" + import win32con, win32file, pywintypes + root = Path(root) + entries = scan_tree(root) # Preserve full-library link and ownership checks. + with ExitStack() as stack: + handles = {} + for path, metadata in entries.items(): + if stat.S_ISREG(metadata.st_mode) and (path.suffix.casefold() == '.lmc' + or path.name.casefold() == 'icdb.dat'): + try: + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + except pywintypes.error as error: + raise OSError(f'LIBRARY_DATABASE_IN_USE: 无法独占读取 {path}:{error}') from error + handles[path] = handle + stack.callback(handle.Close) + + def digest(path): + if path not in handles: + return hash_file(path) + value = hashlib.sha256() + while True: + try: + _, data = win32file.ReadFile(handles[path], 1024 * 1024) + except pywintypes.error as error: + if error.winerror == 38: # ERROR_HANDLE_EOF + break + raise + if not data: + break + value.update(data) + return value.hexdigest() + + return build_manifest(root, scope=scope, progress=progress, hash_function=digest) + + def copy_verified(source, destination, *, scope=None, progress=None, hash_function=hash_file): """Copy the whole tree, validate all metadata, SHA256 only scoped primary files. diff --git a/src/cbb_refresh_verify/ui/backup_notice.py b/src/cbb_refresh_verify/ui/backup_notice.py new file mode 100644 index 0000000..c4e3f75 --- /dev/null +++ b/src/cbb_refresh_verify/ui/backup_notice.py @@ -0,0 +1,5 @@ +"""Shared wording for user-managed backups before write operations.""" + +MANUAL_BACKUP_NOTICE = ( + '应用不会自动备份。请先手动备份中心库及相关工程,并确认备份可用于恢复。' +) diff --git a/src/cbb_refresh_verify/ui/backup_recovery_dialog.py b/src/cbb_refresh_verify/ui/backup_recovery_dialog.py index 635ee27..7e08a0c 100644 --- a/src/cbb_refresh_verify/ui/backup_recovery_dialog.py +++ b/src/cbb_refresh_verify/ui/backup_recovery_dialog.py @@ -3,9 +3,10 @@ from pathlib import Path from PySide6.QtCore import QObject, QThreadPool, Signal from PySide6.QtWidgets import (QComboBox, QDialog, QFileDialog, QFormLayout, - QHBoxLayout, QLabel, QLineEdit, QPushButton) + QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox) from .workers import ServiceWorker +from .backup_notice import MANUAL_BACKUP_NOTICE class RecoverySignals(QObject): @@ -37,7 +38,7 @@ class BackupRecoveryDialog(QDialog): self.restore_button.clicked.connect(self._restore) self.close_button = QPushButton('关闭') self.close_button.clicked.connect(self.reject) - self.status_label = QLabel('使用所选备份版本恢复同名 CBB。先在库副本中验证,原工程与完整库副本会保留;需要额外磁盘空间。') + self.status_label = QLabel(MANUAL_BACKUP_NOTICE + ' 所选备份先在隔离库中验证;安装时旧 Work 仅改名保留。') self.status_label.setWordWrap(True) layout = QFormLayout(self) layout.addRow('目标 CBB', self.block_combo) @@ -77,6 +78,11 @@ class BackupRecoveryDialog(QDialog): except Exception as error: self.status_label.setText(str(error)) return + if QMessageBox.question(self, '确认从备份恢复', + f'{MANUAL_BACKUP_NOTICE}\n\n目标库:{request.lmc_file}\nCBB:{name}\n来源:{source}' + '\n\n将创建用于校验的隔离库副本,校验通过后替换目标工程。确认继续?', + QMessageBox.Yes | QMessageBox.No, QMessageBox.No) != QMessageBox.Yes: + return self.busy = True self._set_enabled(False) self.status_label.setText('正在准备恢复…') diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index aabecf8..ffbd600 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -190,7 +190,7 @@ class MainWindow(QWidget): maintenance_layout = QVBoxLayout(maintenance) maintenance_row = QHBoxLayout() maintenance_row.addWidget(self.icdb_repair_button) - description = QLabel('处理可见非 READY 项;有勾选时仅处理勾选中的可用项。未确认不等于损坏。\n确认工程数据库失联后先备份再修复;无法确认会话安全时停止。') + description = QLabel('处理可见非 READY 项;有勾选时仅处理勾选中的可用项。未确认不等于损坏。\n操作前请手动备份;应用不自动备份。无法确认会话安全时停止。') description.setWordWrap(True) maintenance_row.addWidget(description, 1) maintenance_layout.addLayout(maintenance_row) @@ -589,20 +589,31 @@ class MainWindow(QWidget): return [record.name for record in self.selected_records()] def verify_selected(self): - self._run(self._eligible_names(checked_only=True)) + self._confirm_verify(self._eligible_names(checked_only=True)) def verify_all(self): - self._run(self._eligible_names()) + self._confirm_verify(self._eligible_names()) + + def _confirm_verify(self, names): + from .backup_notice import MANUAL_BACKUP_NOTICE + if self.busy or not self.loaded_path or self._maintenance_recovery_view or not names: + return + if QMessageBox.question(self, '确认批量校验', + f'{MANUAL_BACKUP_NOTICE}\n\n目标库:{self.loaded_path}\n' + f'将校验 {len(names)} 个可见非 READY CBB 并保存结果。确认继续?', + QMessageBox.Yes | QMessageBox.No, QMessageBox.No) == QMessageBox.Yes: + self._run(names) def repair_icdb_all(self): + from .backup_notice import MANUAL_BACKUP_NOTICE if self.busy or not self.loaded_path: return names = self._maintenance_names() if not names: return answer = QMessageBox.question(self, '非 READY ICDB 检查与修复', - f'当前中心库:{self.loaded_path}\n\n' + f'{MANUAL_BACKUP_NOTICE}\nMentor 原生修复可能生成自身备份。\n\n当前中心库:{self.loaded_path}\n\n' f'将检查 {len(names)} 个可见非 READY CBB' f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '无地址阻止时逐项校验;已有工程地址记录时先核对所有者和独占访问、完整备份,' + '无地址阻止时逐项校验;已有工程地址记录时先核对所有者和独占访问,' '再由官方工具确认未占用并检查/修复。不会删除地址文件。\n' '仅本地固定磁盘:严格确认修复并重新独占检查后,可一次确认本次异常退出的读写访问提示。' '网络盘、UNC 或链接路径不自动确认该提示。\n' @@ -802,8 +813,7 @@ class MainWindow(QWidget): self._icdb_completed += 1 self.icdb_progress.setValue(self._icdb_completed) phases = {'icdb_check_start': '检查工程数据库与校验状态', - 'native_repair_start': '准备完整备份与原生修复', - 'complete_backup_verified': '完整备份已校验', + 'native_repair_start': '核对占用并准备原生修复', 'native_repair_requested': '正在原生修复', 'native_repair_confirmed': '正在原生修复', 'native_repair_finished': '原生修复已结束,正在核对结果'} diff --git a/src/cbb_refresh_verify/ui/management_dialog.py b/src/cbb_refresh_verify/ui/management_dialog.py index fc0542c..b7404cd 100644 --- a/src/cbb_refresh_verify/ui/management_dialog.py +++ b/src/cbb_refresh_verify/ui/management_dialog.py @@ -8,6 +8,7 @@ from PySide6.QtWidgets import (QCheckBox, QDialog, QFileDialog, QHBoxLayout, QLa QMessageBox, QPushButton, QTableWidget, QTableWidgetItem, QTextEdit, QVBoxLayout) from .workers import ServiceWorker +from .backup_notice import MANUAL_BACKUP_NOTICE from ..management import run_management_batch, validate_name RESULT_LABELS = { @@ -18,10 +19,11 @@ RESULT_LABELS = { } STAGE_LABELS = { 'prepare_identity': '正在核对来源工程内容', - 'catalog_before': '正在核对原生目录', 'backup': '正在备份完整中心库', + 'catalog_before': '正在核对原生目录', 'baseline': '正在记录中心库内容基线', 'stage_source': '正在复制并检查来源工程', 'native_remove': '正在原生移除', 'removed': '原条目已移除', 'native_add': '正在原生上传', 'uploaded': '上传身份已确认', - 'verify': '正在校验上传结果', 'verified': '校验已返回', 'copy': '正在复制备份', + 'verify': '正在校验上传结果', 'verified': '校验已返回', 'copy': '正在准备来源副本', + 'hash': '正在校验主要文件', } @@ -57,7 +59,7 @@ class ManagementDialog(QDialog): self.auto_verify = QCheckBox('本批次上传后自动校验') self.auto_verify.setChecked(self.template.auto_verify) self.auto_verify.setVisible(self.operation != 'remove') - self.status_label = QLabel('先预览目标和来源;完整备份保存在目标库之外。更新会先移除再重新导入,失败时保留恢复证据。') + self.status_label = QLabel(MANUAL_BACKUP_NOTICE + ' 更新会先移除再重新导入,失败时请使用手动备份恢复。') self.status_label.setWordWrap(True) self.output = QTextEdit(); self.output.setReadOnly(True) self.execute_button = QPushButton('预览并确认执行…') @@ -66,7 +68,7 @@ class ManagementDialog(QDialog): self.stop_button.clicked.connect(self.stop.set) self.close_button = QPushButton('关闭'); self.close_button.clicked.connect(self.reject) layout = QVBoxLayout(self) - layout.addWidget(QLabel(f'目标库:{self.template.library}\n备份/证据:{self.template.evidence_dir}')) + layout.addWidget(QLabel(f'目标库:{self.template.library}\n操作记录:{self.template.evidence_dir}')) layout.addWidget(self.table) layout.addWidget(self.sources_button); layout.addWidget(self.auto_verify) layout.addWidget(self.status_label); layout.addWidget(self.output) @@ -127,12 +129,12 @@ class ManagementDialog(QDialog): except ValueError as error: self.status_label.setText(str(error)); return preview = '\n'.join(f'{r.partition} / {r.name}' + (f' ← {r.source}' if r.source else '') for r in requests) - description = ('同名更新先备份,再原生移除并重新导入;导入失败需要人工恢复。' if self.operation == 'update' - else '移除前保留完整工程和中心库备份。' if self.operation == 'remove' + description = ('同名更新会原生移除并重新导入;导入失败需要使用手动备份恢复。' if self.operation == 'update' + else '将通过原生操作移除所列 CBB;恢复时需要使用手动备份。' if self.operation == 'remove' else '新增同名冲突会拒绝覆盖,完整来源工程将先复制到暂存区。') answer = QMessageBox.question(self, '确认管理操作', - f'{description}\n\n目标库:{self.template.library}\n{preview}\n\n' - f'完整备份与检查点:{self.template.evidence_dir}\n确认执行?', QMessageBox.Yes | QMessageBox.No, + f'{MANUAL_BACKUP_NOTICE}\n{description}\n\n目标库:{self.template.library}\n{preview}\n\n' + f'操作记录与检查点:{self.template.evidence_dir}\n确认继续执行?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer != QMessageBox.Yes: return @@ -156,6 +158,8 @@ class ManagementDialog(QDialog): text = f'{event.get("block", "")} {STAGE_LABELS.get(stage, RESULT_LABELS.get(stage, stage))}' if 'copied_files' in event: text += f' {event["copied_files"]}/{event.get("total_files", "?")}' + elif 'hashed_files' in event: + text += f' {event["hashed_files"]}/{event.get("total_files", "?")} · {event.get("hashed_bytes", 0)} 字节' self.status_label.setText(text) def _result(self, results): diff --git a/tests/test_backup_recovery.py b/tests/test_backup_recovery.py index bb7924c..e947d9d 100644 --- a/tests/test_backup_recovery.py +++ b/tests/test_backup_recovery.py @@ -56,7 +56,8 @@ def test_restore_checks_source_in_library_copy_then_opens_actual_target(recovery retained = list(original.parent.parent.glob('Work.before-restore-*')) assert len(retained) == 1 assert (retained[0]/'database/icdb.dat').read_bytes() == b'damaged' - assert list(request.evidence_dir.rglob('library-before/target.lmc')) + assert not list(request.evidence_dir.rglob('library-before')) + assert not list(request.evidence_dir.rglob('source-before')) def test_invalid_source_does_not_touch_target(recovery_files): @@ -208,14 +209,15 @@ def test_logs_and_unrelated_cbb_changes_after_snapshot_do_not_block_install(reco assert len(calls) == 2 assert (original.parent/'database/icdb.dat').read_bytes() == b'healthy' assert (other.parent/'database/icdb.dat').read_bytes() == b'unrelated update' - assert list(request.evidence_dir.rglob('source-before/session.log'))[0].read_bytes() == b'snapshot log' + staged = calls[0].lmc_file.parent/'ReusableBlocksLP/A/Work' + assert (staged/'session.log').read_bytes() == b'snapshot log' @pytest.mark.parametrize('location,change', [ ('source', 'edit'), ('source', 'delete'), ('source', 'add'), ('target', 'delete'), ('target', 'add'), ('cert', 'edit'), ('shared', 'edit'), ]) -def test_primary_conflicts_preserve_target_and_complete_recovery_copies(recovery_files, location, change): +def test_primary_conflicts_preserve_target_and_validation_evidence(recovery_files, location, change): from cbb_refresh_verify.backup_recovery import recover_from_backup request, original, source = recovery_files if location == 'source': @@ -244,8 +246,10 @@ def test_primary_conflicts_preserve_target_and_complete_recovery_copies(recovery assert len(calls) == 1 assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged' assert not list(original.parent.parent.glob('Work.before-restore-*')) - assert list(request.evidence_dir.rglob('library-before/target.lmc')) - assert list(request.evidence_dir.rglob('source-before/database/icdb.dat')) + assert list(request.evidence_dir.rglob('library-baseline-manifest.json')) + assert list(request.evidence_dir.rglob('source-manifest.json')) + assert calls[0][0].lmc_file.is_file() + assert not list(request.evidence_dir.rglob('library-before')) def test_source_conflict_while_preparing_incoming_work_blocks_install(recovery_files, monkeypatch): diff --git a/tests/test_backup_recovery_dialog.py b/tests/test_backup_recovery_dialog.py index e6a6fff..03e747d 100644 --- a/tests/test_backup_recovery_dialog.py +++ b/tests/test_backup_recovery_dialog.py @@ -2,6 +2,13 @@ from pathlib import Path from threading import Event from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult +import pytest + + +@pytest.fixture(autouse=True) +def accept_manual_backup_reminder(monkeypatch): + from PySide6.QtWidgets import QMessageBox + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) def test_backup_dialog_requires_a_project_and_passes_selected_target(qtbot, tmp_path): diff --git a/tests/test_management.py b/tests/test_management.py index 6a4d3a0..5ac8831 100644 --- a/tests/test_management.py +++ b/tests/test_management.py @@ -82,17 +82,52 @@ def execute(request, adapter, **kwargs): return run_management(request, adapter=adapter, idle_check=lambda *_: None, **kwargs) -def test_remove_backup_and_strict_absence(setup): +def test_remove_uses_baseline_without_automatic_library_backup(setup): request, adapter, _ = setup result = execute(request, adapter) assert result.status == 'REMOVED' and result.safe_to_continue - assert (result.evidence_path.parent/'library-before/mentor.lmc').read_bytes() == b'library' - assert (result.evidence_path.parent/'library-before/ReusableBlocksLP/Block/Work/Block.prj').is_file() + assert not (result.evidence_path.parent/'library-before').exists() + assert (result.evidence_path.parent/'library-baseline-manifest.json').is_file() + assert '已备份' not in result.detail record = json.loads(result.evidence_path.read_text(encoding='utf-8')) assert record['operation'] == 'remove' and record['partition'] == 'Logical-Physical' assert record['stage'] == 'complete' +@pytest.mark.parametrize('operation', ['add', 'update', 'remove']) +def test_management_never_copies_target_library(setup, monkeypatch, operation): + import cbb_refresh_verify.management as management + request, adapter, source = setup + real_copy = management.copy_verified + def copy(source_path, destination, **kwargs): + assert Path(source_path).resolve() != request.library.parent.resolve(), 'automatic whole-library backup' + return real_copy(source_path, destination, **kwargs) + monkeypatch.setattr(management, 'copy_verified', copy) + result = execute(replace(request, operation=operation, source=source if operation != 'remove' else None, + name='New' if operation == 'add' else 'Block', auto_verify=False), adapter) + assert result.status == ('REMOVED' if operation == 'remove' else 'UPLOADED') + + +@pytest.mark.parametrize('relative', ['mentor.lmc', 'SymbolLibs/shared/icdb.dat', + 'ReusableBlocksLP/Other/Work/database/icdb.dat']) +def test_manual_backup_policy_still_rejects_external_shared_read_owner(setup, relative): + import win32con, win32file + request, adapter, _ = setup + path = request.library.parent/relative + path.parent.mkdir(parents=True, exist_ok=True) + if not path.exists(): + path.write_bytes(b'owned database') + handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, win32con.FILE_SHARE_READ, None, + win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + try: + result = execute(request, adapter) + assert result.status == 'BLOCKED' + assert not adapter.calls, 'An existing reader must block native writes even without address/process evidence' + assert not list(request.evidence_dir.rglob('library-before')) + finally: + handle.Close() + + def test_add_conflict_cannot_overwrite(setup): request, adapter, source = setup result = execute(replace(request, operation='add', source=source), adapter) @@ -123,7 +158,7 @@ def test_add_no_auto_verify_and_reimport_after_remove(setup): assert result.status == 'UPLOADED' -def test_update_failed_readd_keeps_removed_checkpoint_and_recovery_snapshot(setup): +def test_update_failed_readd_keeps_removed_checkpoint_and_baseline(setup): request, adapter, source = setup adapter.fail_add = True result = execute(replace(request, operation='update', source=source), adapter) @@ -131,7 +166,8 @@ def test_update_failed_readd_keeps_removed_checkpoint_and_recovery_snapshot(setu assert adapter.records == [] history = json.loads(result.evidence_path.read_text(encoding='utf-8'))['history'] assert 'removed' in [x['stage'] for x in history] - assert (result.evidence_path.parent/'library-before/mentor.lmc').is_file() + assert (result.evidence_path.parent/'library-baseline-manifest.json').is_file() + assert not (result.evidence_path.parent/'library-before').exists() @pytest.mark.parametrize('operation', ['remove', 'add']) @@ -311,7 +347,7 @@ def test_prepared_native_identity_is_required_after_exact_catalog(setup, matched expected = {'policy': 'native-test', 'logical': 'same', 'physical': 'same'} def prepare(staged, root, *, original_source): assert original_source == source and staged.source != source - assert (root/'library-before/mentor.lmc').exists() + assert (root/'library-baseline-manifest.json').exists() adapter.calls.append('prepare_identity') return staged, IdentityOutcome(expected, True, 'baseline captured', root) adapter.prepare_add_transaction = prepare diff --git a/tests/test_management_native_transaction.py b/tests/test_management_native_transaction.py index cad6073..d5dbefe 100644 --- a/tests/test_management_native_transaction.py +++ b/tests/test_management_native_transaction.py @@ -42,8 +42,8 @@ def transaction(tmp_path, monkeypatch): if job['operation'] == 'add': if state.get('tamper') == 'nonce': job['transaction_nonce'] = 'foreign' if state.get('tamper') == 'supervisor': job['supervisor_pid'] = -1 - if state.get('tamper') == 'backup': - (evidence.parent/'library-before'/library.name).write_bytes(b'corrupted backup') + if state.get('tamper') == 'baseline': + (evidence.parent/'library-baseline-manifest.json').write_text('{}') if state.get('tamper') == 'source': (Path(job['source']).parent/'database/icdb.dat').write_bytes(b'changed staged input') validate_transaction(job, evidence.parent) @@ -110,13 +110,14 @@ def test_native_registered_but_asset_mismatch_retains_recovery(transaction, corr assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue -def test_update_readd_failure_preserves_snapshot_and_removed_checkpoint(transaction): +def test_update_readd_failure_preserves_baseline_and_removed_checkpoint(transaction): root, request, state = transaction assert execute(root, request).status == 'UPLOADED' state['fail_add'] = True result = execute(root, replace(request, operation='update')) assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue - assert (result.evidence_path.parent/'library-before/ReusableBlocksLP/Block/Work/Source.prj').exists() + assert (result.evidence_path.parent/'library-baseline-manifest.json').exists() + assert not (result.evidence_path.parent/'library-before').exists() assert 'removed' in [entry['stage'] for entry in json.loads(result.evidence_path.read_text())['history']] @@ -129,7 +130,7 @@ def test_fixture_grant_cannot_authorize_arbitrary_library(transaction, tmp_path) assert other.read_bytes() == b'unchanged' and not state['calls'] -@pytest.mark.parametrize('tamper', ['nonce', 'supervisor', 'backup', 'source']) +@pytest.mark.parametrize('tamper', ['nonce', 'supervisor', 'baseline', 'source']) def test_worker_rejects_modified_management_authority_before_add(transaction, tamper): root, request, state = transaction state['tamper'] = tamper diff --git a/tests/test_management_ui.py b/tests/test_management_ui.py index 486e603..79c0f29 100644 --- a/tests/test_management_ui.py +++ b/tests/test_management_ui.py @@ -41,7 +41,7 @@ def test_management_dialog_preview_confirmation_and_result_status(qtbot, tmp_pat qtbot.addWidget(dialog) monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (confirmations.append(a[2]) or QMessageBox.No)) dialog.execute_button.click() - assert not calls and 'Block' in confirmations[0] and '完整' in confirmations[0] + assert not calls and 'Block' in confirmations[0] and '手动备份' in confirmations[0] monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) dialog.execute_button.click() qtbot.waitUntil(lambda: not dialog.busy) diff --git a/tests/test_manual_backup_ui.py b/tests/test_manual_backup_ui.py new file mode 100644 index 0000000..4fbf7d9 --- /dev/null +++ b/tests/test_manual_backup_ui.py @@ -0,0 +1,51 @@ +from pathlib import Path +import pytest +from PySide6.QtWidgets import QMessageBox + + +@pytest.mark.parametrize('operation', ['add', 'update', 'remove']) +def test_management_prompts_for_manual_backup_and_cancel_starts_nothing(qtbot, tmp_path, monkeypatch, operation): + from cbb_refresh_verify.management import ManagementRequest + from cbb_refresh_verify.ui.management_dialog import ManagementDialog + source = tmp_path/'Block.prj'; source.touch() + seen, calls = [], [] + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (seen.append(a[2]) or QMessageBox.No)) + dialog = ManagementDialog([ManagementRequest(operation, tmp_path/'lib.lmc', 'Block', source=source)], + runner=lambda *a, **k: calls.append(a)) + qtbot.addWidget(dialog) + dialog.execute_button.click() + assert not calls and not dialog.busy + assert len(seen) == 1 and '手动备份' in seen[0] and '不会自动备份' in seen[0] + assert '完整备份与检查点' not in seen[0] + + +@pytest.mark.parametrize('action', ['verify_all', 'verify_selected', 'repair_icdb_all']) +def test_batch_prompts_once_for_manual_backup_and_cancel_does_not_start(qtbot, tmp_path, monkeypatch, action): + from cbb_refresh_verify.ui.main_window import MainWindow + from cbb_refresh_verify.models import CbbRecord, LibraryReadResult + window = MainWindow(reader=lambda _: LibraryReadResult((CbbRecord('Block', 'LP'),)), data_root=tmp_path) + qtbot.addWidget(window) + window.lmc_edit.setText('library.lmc'); window.load_library() + qtbot.waitUntil(lambda: not window.busy) + window.select_visible_button.click() + seen, calls = [], [] + monkeypatch.setattr(window, '_run', lambda *a, **k: calls.append(a)) + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (seen.append(a[2]) or QMessageBox.No)) + getattr(window, action)() + assert not calls + assert len(seen) == 1 and '手动备份' in seen[0] and '不会自动备份' in seen[0] + + +def test_restore_prompts_for_manual_backup_before_starting_worker(qtbot, tmp_path, monkeypatch): + from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult + from cbb_refresh_verify.ui.backup_recovery_dialog import BackupRecoveryDialog + calls, seen = [], [] + source = tmp_path/'Block.prj'; source.touch() + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (seen.append(a[2]) or QMessageBox.No)) + dialog = BackupRecoveryDialog(['Block'], lambda n: VerifyRequest(tmp_path/'lib.lmc', n, tmp_path/'out'), + recoverer=lambda *a, **k: (calls.append(a) or VerifyResult('PASS'))) + qtbot.addWidget(dialog) + dialog.source_edit.setText(str(source)); dialog.restore_button.click() + qtbot.waitUntil(lambda: not dialog.busy) + assert not calls + assert len(seen) == 1 and '手动备份' in seen[0] and '不会自动备份' in seen[0] diff --git a/tests/test_native_repair.py b/tests/test_native_repair.py index 910c3af..870a77d 100644 --- a/tests/test_native_repair.py +++ b/tests/test_native_repair.py @@ -234,7 +234,7 @@ def test_retained_importer_blocks_repair_even_without_an_open_database_handle(tm assert 'IMPORTER_RELEASE_UNCONFIRMED' in outcome['detail'] -def test_repair_cancellation_during_backup_prevents_native_launch(tmp_path, monkeypatch): +def test_repair_cancellation_during_access_check_prevents_native_launch(tmp_path, monkeypatch): from cbb_refresh_verify import native_repair_ui as ui, native_repair, mentor_adapter request, record, target = target_fixture(tmp_path) root = tmp_path/'worker' @@ -251,15 +251,15 @@ def test_repair_cancellation_during_backup_prevents_native_launch(tmp_path, monk monkeypatch.setattr(ui, '_repair_command', lambda *_: 32924) monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: []) monkeypatch.setattr(mentor_adapter, '_process_details', lambda *_: []) - def backup(*a): - assert calls == ['watchdog'], 'Watch supervisor before the potentially long backup' + def check(*a): + assert calls == ['watchdog'], 'Watch supervisor before filesystem checks' cancel.touch() - calls.append('backup') - monkeypatch.setattr(native_repair, 'backup_project', backup) + calls.append('check') + monkeypatch.setattr(native_repair, 'check_repair_access', check) monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('Repair must not start')) assert ui.repair_worker_main(job) == 1 outcome = json.loads((root/'repair-result.json').read_text('utf-8')) - assert 'REPAIR_CANCELLED_AFTER_BACKUP' in outcome['detail'] + assert 'REPAIR_CANCELLED_BEFORE_LAUNCH' in outcome['detail'] def test_repair_worker_refuses_the_input_desktop_before_backup(tmp_path, monkeypatch): @@ -425,6 +425,30 @@ def test_native_worker_preserves_success_and_historical_restore_distinction(tmp_ assert native_success_confirmed(target, result) is (scenario == 'success') +def test_repair_skips_application_backup_but_keeps_native_repair_backup(tmp_path, monkeypatch): + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'success') + monkeypatch.setattr('cbb_refresh_verify.native_repair.backup_project', + lambda *a: pytest.fail('application project backup must not run')) + assert ui.repair_worker_main(job) == 0 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['evidence']['native_backup'] is True + assert not (root/'complete-backup').exists() + + +def test_repair_without_application_backup_still_rejects_busy_database(tmp_path, monkeypatch): + import win32con, win32file + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'success') + database = Path(json.loads(job.read_text())['database'])/'icdb.dat' + handle = win32file.CreateFile(str(database), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) + try: + monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('busy database launched')) + assert ui.repair_worker_main(job) == 1 + assert 'REPAIR_DATABASE_IN_USE' in json.loads((root/'repair-result.json').read_text())['detail'] + finally: + handle.Close() + + def test_native_busy_project_on_load_never_invokes_repair(tmp_path, monkeypatch): ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'busy_before') assert ui.repair_worker_main(job) == 1 diff --git a/tests/test_ui.py b/tests/test_ui.py index 6277c69..cb0079d 100644 --- a/tests/test_ui.py +++ b/tests/test_ui.py @@ -25,6 +25,8 @@ def fake_reader(_): return LibraryReadResult((CbbRecord('CBB1','Reusable Blocks' @pytest.fixture(autouse=True) def isolate_authoritative_native_refresh(monkeypatch): + from PySide6.QtWidgets import QMessageBox + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) # Production refresh crosses the native Mentor boundary; these are UI tests. monkeypatch.setattr('cbb_refresh_verify.ui.main_window.authoritative_status_map', lambda names, *_: {name: 'READY' for name in names}) diff --git a/tests/test_ui_filters.py b/tests/test_ui_filters.py index edd026a..9bec051 100644 --- a/tests/test_ui_filters.py +++ b/tests/test_ui_filters.py @@ -24,6 +24,7 @@ MIXED = ( def load_window(qtbot, tmp_path, monkeypatch, records=MIXED, **kwargs): + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) import cbb_refresh_verify.ui.main_window as ui monkeypatch.setattr(ui, 'preflight_skip_reason', lambda _, name: 'missing project' if name == 'Missing' else None) diff --git a/tests/test_verify_worker_stability.py b/tests/test_verify_worker_stability.py index 20551c9..5de7dde 100644 --- a/tests/test_verify_worker_stability.py +++ b/tests/test_verify_worker_stability.py @@ -54,6 +54,15 @@ def test_missing_cert_cannot_block_saved_and_closed_worker(tmp_path, monkeypatch assert any(e['stage'] == 'cert_diagnostics' and e['non_blocking'] for e in events) +def test_verify_does_not_create_automatic_project_backup(tmp_path, monkeypatch): + job, events, execute = exercise_worker(tmp_path, monkeypatch) + monkeypatch.setattr(adapter, 'best_effort_backup', + lambda *a, **kw: pytest.fail('automatic project backup must not run')) + assert execute()['status'] == 'PASS' + assert not (Path(job['root'])/'backup').exists() + assert any(e['stage'] == 'backup_skipped' and e['reason'] == 'manual_backup_policy' for e in events) + + def test_cert_diagnostic_io_failure_is_nonblocking(tmp_path, monkeypatch): from cbb_refresh_verify.mentor_adapter import collect_cert_evidence original = Path.stat -- 2.39.5 From de952a68d6b26aea9c97c74ca21c2b624cf938aa 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: Tue, 3 Nov 2026 17:42:33 +0800 Subject: [PATCH 43/59] fix: inspect addressless ICDB projects and ask for PCB recovery version --- .../2026-11-03-icdb-web-research.md | 65 ++++++++ .../2026-11-03-icdb-version-choice.md | 26 +++ .../plans/2026-11-03-addressless-icdb.md | 12 ++ src/cbb_refresh_verify/dialog_agent.py | 8 + src/cbb_refresh_verify/maintenance_entry.py | 35 +++- src/cbb_refresh_verify/pcb_restore_prompt.py | 151 ++++++++++++++++++ src/cbb_refresh_verify/ui/main_window.py | 25 ++- .../ui/pcb_restore_dialog.py | 31 ++++ src/cbb_refresh_verify/verify_runner.py | 24 ++- tests/test_maintenance_entry.py | 71 +++++++- tests/test_pcb_restore_prompt.py | 132 +++++++++++++++ 11 files changed, 558 insertions(+), 22 deletions(-) create mode 100644 docs/diagnostics/2026-11-03-icdb-web-research.md create mode 100644 docs/releases/2026-11-03-icdb-version-choice.md create mode 100644 docs/superpowers/plans/2026-11-03-addressless-icdb.md create mode 100644 src/cbb_refresh_verify/pcb_restore_prompt.py create mode 100644 src/cbb_refresh_verify/ui/pcb_restore_dialog.py create mode 100644 tests/test_pcb_restore_prompt.py diff --git a/docs/diagnostics/2026-11-03-icdb-web-research.md b/docs/diagnostics/2026-11-03-icdb-web-research.md new file mode 100644 index 0000000..2fdd8eb --- /dev/null +++ b/docs/diagnostics/2026-11-03-icdb-web-research.md @@ -0,0 +1,65 @@ +# ICDB 持续失败:公开资料与实际入口核对 + +调查日期:2026-11-03。当前源码 `c856960`。本轮是网上资料检索及只读诊断,没有修改库、服务器设置或产品代码,不能作为修复验收。 + +## 本机确定的缺口 + +目前找到最近一次实际失败为隔离完整库 IP101GR 的 2026-11-03 16:19(北京时间)记录: + +`diagnostics/feedback-final-ui/evidence/verify-85fd17bc6ed2/result.json` + +错误为 `DATABASE_RECOVERY_REQUIRED`,阶段为 `PCB_STARTING`。原生提示询问: + +> Continue to open the design with read/write access permission? + +提示说明可能是文件系统/网络在 15 秒内未确定谁打开了设计,也可能是上一会话异常退出;要求确认没有其他用户正在使用。它没有证明数据库内容已损坏,也不是本轮原生 Repair 的失败结果。 + +代码及只读复核证据 `diagnostics/icdb-web-research-20261103/routing-evidence.json`: + +1. `maintenance_entry.maintenance_needed` 仅在存在库阻止检查点或数据库地址记录时返回真。 +2. 当前 IP101GR 没有地址记录、没有阻止检查点,尽管请求 `maintenance=True`,仍返回 `routes_to_native_repair=false`。 +3. `select_maintenance_target` 额外要求恰好一个地址记录;无地址时返回 `需要唯一工程地址记录`,因此只改外层判断也不能打通流程。 +4. `verify_runner` 的自动补救仅针对 `ICDB_REPAIR_REQUIRED`;当前读写确认错误归入 `DATABASE_SAFETY_STOP`,不会触发该分支。 +5. 修复后一次性读写确认已有 `PostRepairProof` 机制,但只有本轮原生修复成功且释放已核实时才授权。当前入口绕过 Repair,自然没有这份证明。 + +因此,明确维护不能以“没有 sAddress.adr”推断无需维护。地址记录用于定位/占用判断,不应成为识别异常关闭状态的唯一依据。 + +## 网上资料:已读取原文 + +### 原生 Project Repair + +[Mentor XP 如何修复文件](https://blog.csdn.net/qq_53931654/article/details/147240707) + +原文步骤为打开 iCDB Project Backup,指定损坏工程路径,执行 `Project → Repair`。与本机已经接入的官方工具入口一致。本文是用户经验文章,没有证明当前完整自动链路或 CBB 校验限制已经解决。 + +本轮首次访问返回脚本验证页,后续正常请求取得正文;正文与截图链接保存在 `native-repair.json` / `native-repair.html`。 + +### 工程备份恢复 + +[“Unable to open iCDB connection”的解决方案](https://blog.csdn.net/zhangyang125/article/details/136188112) + +原文建议从工程 `ProjectBackup/backups` 选择备份,恢复对应文件后重新打开。文章把复制/转移时文件丢失或损坏列为可能原因。该方案会回退版本,不等于保留最新内容的无损 Repair;仍需用户选择可接受版本,恢复后做工程身份与校验确认。 + +### 连接/监听地址 + +[Xpedition 2409 Designer 无法打开 iCDB 连接](https://blog.csdn.net/kkisscj/article/details/144323677) + +原文描述在工作目录的 iCDB Server 配置中设置 `Network/Address` 为本机地址,并引用 VX2.8 管理文档。这说明“无法连接 iCDB”也可能是监听配置问题,不能一律解释为损坏。 + +但文章环境是 Xpedition 2409,本机是 EE7.9.5。现有日志未证实本机出现同类监听失败;本轮没有修改 Server 配置、服务或端口。 + +### 检索限制和排除项 + +英文 Google/DuckDuckGo 请求连接超时;Bing 多个精确错误查询无结果或结果无关,360 搜索提供了可进一步核对的中文线索。没有取得可直接适用于 EE7.9.5 当前弹窗的 Siemens 官方支持答复。检索及抓取原文保存在 `diagnostics/icdb-web-research-20261103/`。 + +标题/推荐摘要中含 “iCDB Server Monitor” 的另一页面,正文实际讨论 Android Studio Monitor,已排除,未把无关搜索结果当作技术依据。本机已有帮助文件检索也未找到能补足该弹窗处理条件的准确段落。 + +## 建议的修正与验证次序 + +1. 明确维护入口支持“无地址记录但需要检查”的工程:通过用户所选 CBB、唯一 PRJ/PCB 关系和明确数据库映射绑定目标;多个候选数据库必须明确区分,不能猜测共享库或另一个工程。 +2. 在本任务 PCB/启动器正常退出、库锁与数据库独占访问确认后,执行一次原生 Project Repair。不要因为看不到地址文件就直接退回打开 PCB。 +3. 仅在原生成功、目标一致、进程与地址释放确认后,使用已有修复后读写授权重新打开。未知或外部占用仍停止,不能把同意读写弹窗作为一般重试策略。 +4. 如要求选择 PCB 历史版本,明确记录选择及版本回退;随后验证集成、保存、关闭和权威 READY。 +5. 如果数据库检查通过,但仍被“已验证 CBB 不允许重新执行 BackAnnotate”阻止,应作为独立的 CBB 注册/校验状态问题处理。此前完整依赖库中“原生重新导入 → 自动校验 → READY”已通过,但不能把重新导入偷偷塞进 Repair 或称为无损修复。 + +此处已定位入口缺口和可验证的修正方向;尚未实现并完成“无地址记录”维护路径的真实自动链路验收。 diff --git a/docs/releases/2026-11-03-icdb-version-choice.md b/docs/releases/2026-11-03-icdb-version-choice.md new file mode 100644 index 0000000..c0e36e5 --- /dev/null +++ b/docs/releases/2026-11-03-icdb-version-choice.md @@ -0,0 +1,26 @@ +# 无地址 ICDB 维护及 PCB 版本选择 + +沿用 `codex/temp-issue-2-icdb-recovery` 临时分支。依据实际错误日志与公开资料修正入口,用户明确授权在需要时弹窗选择版本。应用自动备份仍取消,操作前保留手动备份提醒;未修改原用户库。 + +## 本次行为 + +- 明确发起 ICDB 维护时,不再因为没有 `sAddress.adr` 或协调检查点而跳过原生检查。 +- 无地址时,所选 PCB 必须唯一反向引用主 PRJ,工程内数据库映射必须唯一。多数据库、共享地址、路径身份不符、原生进程/脚本占用及无法独占读取仍停止。 +- 原生 Repair 成功并确认释放后,沿同一任务重新打开。若原生要求选择历史 PCB 版本,主应用显示实际候选时间和“最后手动保存 / 自动保存 / 取消”。不默认选择任一历史版本。 +- 用户选择后,私有桌面代理重新核对窗口、进程、候选时间及单选状态,再确认恢复。选择过期、窗口变化或执行失败不重试确认。等待期间应用保持响应;取消停止当前项并保留原生现场。 +- 最终仍要求集成校验、保存关闭、进程退出及权威 READY。使用了历史 PCB 版本时,即便校验通过,也记录 `RESTORED_REVIEW_REQUIRED`(已恢复,版本待核对),不标记为无损 Repair。 +- 未提供交互入口的调用保留待选择结果,不自动选版本;普通 Verify 不获得修复后授权。 + +## 过程证据 + +无地址隔离工程 IP101GR 经实际应用维护按钮完成原生 Repair,地址为空且进程退出。之后严格的修复后授权通过读写确认,继续到 PCB 历史版本选择。证据为 `diagnostics/noaddr-ui/evidence/maintenance-c05e9c4b06b2/result.json`。 + +用户此前已选择最后手动保存版本(2026/9/24 23:06)。首次诊断续接沿用该选择,完成校验、保存关闭,`IsVerifiedReuseBlock` 返回 READY/raw=1;记录在 `diagnostics/icdb-addressless-20261103/authorized-last-manual/result.json`。该次还通过诊断操作确认了保存弹窗,不能记为完整自动链路通过,因此继续实现上述应用内选择流程。 + +后续应用内选择试验两次在原生修复前被其他脚本占用阻止。进一步日志显示来自另一工具的并行 `run_bridge.vbs` 测试;没有强制结束或忽略该进程。失败记录保留在 `source-choice-button/`、`source-choice-button-retry/`。 + +## 验证边界 + +集中回归覆盖无地址选目标、地址变化、共享/多个数据库、修复后授权、选择/取消、版本日期改变、重复确认、原生动作失败、错 PID/桌面、真实单选状态复读和历史恢复归类。独立代码审查无剩余实质问题。 + +最终全量测试、冻结构建及实际按钮结果统一记录在 `diagnostics/icdb-addressless-20261103/`。本文件不将历史版本恢复视为无损修复,也不将模拟测试或人工续接替代最终冻结链路结果。 diff --git a/docs/superpowers/plans/2026-11-03-addressless-icdb.md b/docs/superpowers/plans/2026-11-03-addressless-icdb.md new file mode 100644 index 0000000..8e483e3 --- /dev/null +++ b/docs/superpowers/plans/2026-11-03-addressless-icdb.md @@ -0,0 +1,12 @@ +# 无地址 ICDB 维护与 PCB 版本选择 + +用户要求按已确认的入口分析修复,并明确允许需要时弹窗让用户选择。沿用临时分支,不恢复应用自动备份,不改原库。 + +- [x] 无地址记录的明确维护也进入原生 Repair;仅允许唯一数据库与 PCB/PRJ 反向引用绑定,保留全库独占检查。新增回归先失败,集中 250 项通过,独立审查通过。 +- [x] 实际应用按钮在无地址 IP101GR 副本完成原生 Repair,随后需要 PCB 版本选择;沿用用户此前确认的手动保存版本,诊断续接已保存关闭并读取 READY。人工续接不算完整自动链路验收。 +- [ ] 在主应用显示候选时间和手动保存/自动保存/取消按钮。没有 UI 选择入口的调用保持待选择,不自动选版本;每个原生窗口只问一次,等待期间继续观察会话。 +- [ ] 原生动作仍由私有桌面代理执行,核对修复证明、进程、窗口和候选版本;先选择单选项并复读,再确认 OK;取消或窗口变化不确认恢复。 +- [ ] 用户选择后沿原校验链路继续,原生保存提示仍由已有监督器处理。最终 READY 保持原有门槛;使用历史 PCB 版本记录“已恢复,版本待核对”,不标记无损修复。 +- [ ] 测试 UI 取消、手动/自动选择、选择失效、重复弹窗、错 PID/桌面、原生失败和 READY 区分;独立审查、全量回归、冻结实际按钮验证并交付候选。 + +采用独立版本选择控制器与 Qt 对话框,复用原生代理及现有进度信号;不引入前台鼠标或自动重导入。界面只要求选择已有候选版本,不替用户推断应采用哪个版本。 diff --git a/src/cbb_refresh_verify/dialog_agent.py b/src/cbb_refresh_verify/dialog_agent.py index a78608c..51655ed 100644 --- a/src/cbb_refresh_verify/dialog_agent.py +++ b/src/cbb_refresh_verify/dialog_agent.py @@ -236,6 +236,14 @@ class DialogActionExecutor: def perform(self, snapshot: WindowSnapshot, action: dict) -> ActionResult: channels = tuple(action.get("channels", ())) action_name = action.get("action") + if action_name == 'user_selected_pcb_version': + try: + from .pcb_restore_prompt import validate_action_window + if channels != ('win32_button_click',): + raise ValueError('PCB_VERSION_ACTION_CHANNEL_MISMATCH') + validate_action_window(snapshot, action) + except Exception as error: + return ActionResult(error_code='PCB_VERSION_CHOICE_REQUIRED', detail=str(error)) if action_name == 'post_repair_local_read_write': try: from .post_repair_consent import validate_action_window diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 670beb4..58bdddd 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -1,7 +1,7 @@ -"""User-requested native inspection of a project with an existing advertisement. +"""User-requested native inspection with or without an existing advertisement. An advertisement is never deleted, quarantined or called stale by this module. -Exclusive access permits a backed-up official inspection, not ordinary Verify. +Exclusive access and exact project binding permit an official inspection. """ from contextlib import contextmanager, ExitStack from dataclasses import asdict, replace @@ -34,7 +34,8 @@ def assert_native_idle(): 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({'pid': int(item.ProcessId), 'name': str(item.Name)}) + owners.append({'pid': int(item.ProcessId), 'name': str(item.Name), + 'command': str(item.CommandLine or '')}) if owners: raise ValueError(f'MAINTENANCE_OWNER_ACTIVE: 请正常关闭原生程序/操作助手:{owners}') @@ -113,6 +114,15 @@ def select_maintenance_target(request): raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少 icdb.dat') candidates.append(RepairTarget(candidate, candidate.parent/'database')) addresses = _scoped_addresses(request) + 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 @@ -153,6 +163,10 @@ def maintenance_target_guard(request, expected=None): 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 @@ -198,8 +212,8 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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; worker repeats after complete - # backup and before launching its native process as well. + # Repeat immediately before dispatch; the worker repeats before + # launching the native process as well. with maintenance_target_guard(request, context): pass recovery = run_native_repair(target, native_root, mentor_bin=request.mentor_bin, timeout_seconds=max(60, request.timeout_seconds), progress=progress, @@ -227,6 +241,17 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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 diff --git a/src/cbb_refresh_verify/pcb_restore_prompt.py b/src/cbb_refresh_verify/pcb_restore_prompt.py new file mode 100644 index 0000000..abe429a --- /dev/null +++ b/src/cbb_refresh_verify/pcb_restore_prompt.py @@ -0,0 +1,151 @@ +"""Explicit user choice for the owned PCB's native historical-version prompt.""" +from dataclasses import replace +import json +from pathlib import Path +from threading import Lock +import time + +from .dialog_rules import pcb_version_choice + + +class RestoreChoice: + def __init__(self, block, library, candidates): + self.block, self.library = block, library + self.candidates = tuple(dict(c) for c in candidates) + self.decision = None + self._lock = Lock() + + def choose(self, kind): + if kind != 'cancel' and not any(c['kind'] == kind and c['enabled'] for c in self.candidates): + raise ValueError('Unknown or disabled PCB restoration version') + with self._lock: + if self.decision is None: + self.decision = kind + + +def _candidate_key(candidates): + return tuple((c['kind'], c['control_id'], c['label'], c['date'], c['enabled']) for c in candidates) + + +def _choice(snapshot): + choice = pcb_version_choice(snapshot) + buttons = [c for c in snapshot.controls if c.class_name == 'Button'] + if (choice is None or any(c.control_id not in (6212, 6213, 6206, 1, 2) for c in buttons) + or len([c for c in buttons if c.control_id == 1 and c.enabled and c.name == 'OK']) != 1): + raise ValueError('PCB_VERSION_PROMPT_CHANGED') + return choice + + +class RestoreController: + def __init__(self, job): + from .post_repair_consent import _validate_grant + self.job = job + self.grant = _validate_grant(job) + self.prompt = None + self.original = None + self.selection_sent = self.confirm_sent = self.failed = False + self.sent_at = 0.0 + + def handle(self, snapshot, snapshots, *, target_pid, launcher_pid, worker_pid, companion_pids, show, perform): + from . import post_repair_consent as consent + from .dialog_supervisor import DialogPollResult + from .management import _write_json + try: + choice = _choice(snapshot) + if sum(c['selected'] for c in choice['candidates']) > 1: + raise ValueError('PCB_VERSION_SELECTION_AMBIGUOUS') + if self.failed or snapshot.pid != target_pid or len(snapshots) != 1: + raise ValueError('PCB_VERSION_OWNER_OR_WINDOW_UNCONFIRMED') + if self.original is not None and (snapshot.hwnd != self.original.hwnd + or snapshot.pid != self.original.pid + or _candidate_key(choice['candidates']) != _candidate_key(self.prompt.candidates)): + raise ValueError('PCB_VERSION_CANDIDATES_CHANGED') + pre = json.loads((Path(self.job['root'])/'post-repair-prelaunch.json').read_text(encoding='utf-8')) + if (pre.get('nonce') != self.grant['nonce'] or pre.get('desktop') != self.job['desktop'] + or pre.get('worker_pid') != worker_pid or pre.get('supervisor_pid') != self.job['supervisor_pid'] + or pre.get('addresses') != [] + or not all(consent._same(pre[key], self.grant[key]) for key in ('project', 'pcb', 'library'))): + raise ValueError('PCB_VERSION_PRELAUNCH_PROOF_INVALID') + consent._validate_current_processes(self.job, pre, target_pid, launcher_pid, worker_pid, companion_pids) + if self.prompt is None: + self.original = snapshot + self.prompt = RestoreChoice(self.job['block_name'], self.job['lmc_file'], choice['candidates']) + show(self.prompt) + if self.prompt.decision is None: + return DialogPollResult(activity=True, observed_snapshots=tuple(snapshots)) + if self.prompt.decision == 'cancel': + raise ValueError('用户取消 PCB 版本选择;未确认恢复,保留原生现场。') + selected = next(c for c in choice['candidates'] if c['kind'] == self.prompt.decision) + if not selected['enabled']: + raise ValueError('PCB_VERSION_DISABLED') + if self.confirm_sent: + if time.monotonic()-self.sent_at < 2: + return DialogPollResult(activity=False, observed_snapshots=tuple(snapshots)) + raise ValueError('PCB_VERSION_CONFIRMATION_NOT_COMPLETED') + if self.selection_sent and not selected['selected']: + if time.monotonic()-self.sent_at < 2: + return DialogPollResult(activity=False, observed_snapshots=tuple(snapshots)) + raise ValueError('PCB_VERSION_SELECTION_NOT_CONFIRMED') + confirming = selected['selected'] and sum(c['selected'] for c in choice['candidates']) == 1 + control = 1 if confirming else selected['control_id'] + action = {'action': 'user_selected_pcb_version', 'channels': ['win32_button_click'], + 'target': {'control_id': control}, 'desktop': self.job['desktop'], 'pid': target_pid, + 'choice': selected['kind'], 'candidates': list(self.prompt.candidates)} + self.selection_sent = True + self.confirm_sent = confirming + self.sent_at = time.monotonic() + record = {'project': self.job['project'], 'pcb': self.job['pcb'], 'choice': selected['kind'], + 'date': selected['date'], 'historical_restore': True, 'confirm_sent': confirming, + 'snapshot': snapshot.to_record()} + _write_json(Path(self.job['root'])/'pcb-version-user-choice.json', record) + result = perform(snapshot, action, snapshot.phase) + if result.error_code or result.accepted_controls != (control,): + self.failed = True + return DialogPollResult(error_code=result.error_code or 'PCB_VERSION_ACTION_FAILED', detail=result.detail, + snapshot=snapshot, observed_snapshots=tuple(snapshots)) + if confirming: + _write_json(Path(self.job['root'])/'pcb-version-restoration.json', record) + return DialogPollResult(activity=True, action_sent=True, snapshot=snapshot, + action=action['action'], channel=result.channel, accepted_controls=result.accepted_controls, + rule_id='explicit-user-pcb-version', observed_snapshots=tuple(snapshots)) + except Exception as error: + self.failed = True + return DialogPollResult(error_code='PCB_VERSION_CHOICE_REQUIRED', detail=str(error), + snapshot=snapshot, observed_snapshots=tuple(snapshots)) + + +def validate_action_window(snapshot, action): + """Recheck the exact live window and radio selection on the private desktop.""" + import win32gui, win32process + from .native_repair_ui import _current_desktop_name + from .dialog_models import ControlSnapshot + if (not str(action.get('desktop', '')).startswith('CbbVerify_') + or _current_desktop_name() != action.get('desktop') or snapshot.pid != action.get('pid') + or win32process.GetWindowThreadProcessId(snapshot.hwnd)[1] != snapshot.pid + or win32gui.GetWindowText(snapshot.hwnd) != snapshot.title + or win32gui.GetClassName(snapshot.hwnd) != snapshot.window_class + or not win32gui.IsWindowVisible(snapshot.hwnd)): + raise ValueError('PCB_VERSION_LIVE_WINDOW_MISMATCH') + children = [] + win32gui.EnumChildWindows(snapshot.hwnd, lambda h, _: children.append(int(h)), None) + live = [] + for h in children: + control_id = win32gui.GetDlgCtrlID(h) & 0xFFFF + checked = False + if control_id in (6213, 6206): + value = win32gui.SendMessageTimeout(h, 0x00F0, 0, 0, 2, 1000) + checked = (value[-1] if isinstance(value, tuple) else value) == 1 + live.append(ControlSnapshot(h, control_id, win32gui.GetClassName(h), win32gui.GetWindowText(h), + enabled=bool(win32gui.IsWindowEnabled(h)), checked=checked)) + choice = _choice(replace(snapshot, controls=tuple(live))) + if _candidate_key(choice['candidates']) != _candidate_key(action.get('candidates', [])): + raise ValueError('PCB_VERSION_LIVE_CANDIDATES_CHANGED') + selected = next((c for c in choice['candidates'] if c['kind'] == action.get('choice') and c['enabled']), None) + control_id = action.get('target', {}).get('control_id') + if selected is None or control_id not in (selected['control_id'], 1): + raise ValueError('PCB_VERSION_ACTION_MISMATCH') + if control_id == 1 and (not selected['selected'] or sum(c['selected'] for c in choice['candidates']) != 1): + raise ValueError('PCB_VERSION_LIVE_SELECTION_MISMATCH') + target = next(c for c in live if c.control_id == control_id) + if target.handle != next(c.handle for c in snapshot.controls if c.control_id == control_id): + raise ValueError('PCB_VERSION_CONTROL_CHANGED') diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index ffbd600..7a79b4b 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -26,6 +26,7 @@ from cbb_refresh_verify.verify_runner import (VerifyRequest, preflight_skip_reas class ProgressSignals(QObject): event = Signal(object) + version_choice = Signal(object) STATUS_LABELS = { @@ -93,6 +94,8 @@ class MainWindow(QWidget): self._global_icdb_run = False self._icdb_completed = 0 self.signals = ProgressSignals(self); self.signals.event.connect(self._progress) + self.signals.version_choice.connect(self._show_version_choice) + self.version_dialog = None self.lmc_edit = QLineEdit() self.libspec_edit = QLineEdit(os.environ.get('CBB_LIBSPEC', 'EE7.9')) self.dms_edit = QLineEdit(os.environ.get('CBB_DMS_LOGIN_CONFIG', '')) @@ -613,8 +616,8 @@ class MainWindow(QWidget): f'{MANUAL_BACKUP_NOTICE}\nMentor 原生修复可能生成自身备份。\n\n当前中心库:{self.loaded_path}\n\n' f'将检查 {len(names)} 个可见非 READY CBB' f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '无地址阻止时逐项校验;已有工程地址记录时先核对所有者和独占访问,' - '再由官方工具确认未占用并检查/修复。不会删除地址文件。\n' + '逐项核对工程身份、所有者和独占访问,再由官方工具检查/修复一次,成功后重新打开校验。' + '没有地址记录也会检查;多个数据库无法明确目标时停止,不猜测或删除地址文件。\n' '仅本地固定磁盘:严格确认修复并重新独占检查后,可一次确认本次异常退出的读写访问提示。' '网络盘、UNC 或链接路径不自动确认该提示。\n' '共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;' @@ -707,7 +710,8 @@ class MainWindow(QWidget): from cbb_refresh_verify.verify_runner import read_library_flag_if_available, run_verify verify = self.verifier or (lambda req: run_verify( req, progress=lambda e: self.signals.event.emit(e), - flag_reader=read_library_flag_if_available)) + flag_reader=read_library_flag_if_available, + version_prompt=self.signals.version_choice.emit)) def verifier(req): if maintenance: self.signals.event.emit({'block': req.block_name, 'stage': 'icdb_check_start'}) @@ -893,7 +897,19 @@ class MainWindow(QWidget): self.summary.setAttribute(Qt.WA_ShowWithoutActivating) self.summary.show() + def _show_version_choice(self, choice): + from .pcb_restore_dialog import PcbRestoreDialog + if not self.busy or self.stop.is_set(): + choice.choose('cancel') + return + self.version_dialog = PcbRestoreDialog(choice, self) + self.status_label.setText(f'{choice.block}:等待选择 PCB 恢复版本。') + self.icdb_phase.setText(f'{choice.block} · 等待选择 PCB 恢复版本') + self.version_dialog.show() + def request_stop(self): + if self.version_dialog is not None: + self.version_dialog.reject() self.stop.set(); self.status_label.setText('已请求停止;当前 CBB 完成或超时后停止队列。'); self._update() def _failed(self, error): @@ -903,6 +919,9 @@ class MainWindow(QWidget): if self.logging_context: self.logging_context.logger.error('cbb_worker_failed %s', error) def _finished(self): + if self.version_dialog is not None: + self.version_dialog.reject() + self.version_dialog = None self.busy = False; self._update() if self._reload_after_maintenance: from PySide6.QtCore import QTimer diff --git a/src/cbb_refresh_verify/ui/pcb_restore_dialog.py b/src/cbb_refresh_verify/ui/pcb_restore_dialog.py new file mode 100644 index 0000000..bb3c57b --- /dev/null +++ b/src/cbb_refresh_verify/ui/pcb_restore_dialog.py @@ -0,0 +1,31 @@ +"""Show the native version dates without preselecting a recovery version.""" +from PySide6.QtWidgets import QMessageBox + + +class PcbRestoreDialog(QMessageBox): + def __init__(self, choice, parent=None): + super().__init__(parent) + self.choice = choice + self.setWindowTitle('选择 PCB 恢复版本') + self.setIcon(QMessageBox.Question) + candidates = {c['kind']: c for c in choice.candidates} + manual, auto = candidates['last_user_saved'], candidates['autosave'] + self.setText(f'CBB:{choice.block}\n中心库:{choice.library}\n\n' + '原生程序检测到上次异常退出,需要选择 PCB 恢复版本。\n' + f'最后手动保存:{manual["date"]}\n自动保存:{auto["date"]}\n\n' + '选择历史版本可能丢失此版本之后的修改。选择后继续校验、保存和状态确认;恢复版本仍需核对。' + '\n取消将停止本项并保留原生现场,不自动选取版本。') + self.manual_button = self.addButton(f'最后手动保存({manual["date"]})', QMessageBox.ActionRole) + self.auto_button = self.addButton(f'自动保存({auto["date"]})', QMessageBox.ActionRole) + self.manual_button.setEnabled(manual['enabled']) + self.auto_button.setEnabled(auto['enabled']) + self.cancel_button = self.addButton(QMessageBox.Cancel) + self.setDefaultButton(self.cancel_button) + self.setEscapeButton(self.cancel_button) + self.buttonClicked.connect(self._chosen) + self.finished.connect(lambda _: choice.choose('cancel')) + self.setModal(True) + + def _chosen(self, button): + self.choice.choose('last_user_saved' if button is self.manual_button else + 'autosave' if button is self.auto_button else 'cancel') diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 122747e..420db6d 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -506,7 +506,7 @@ def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline, def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, port_check=None, - dialog_supervisor_factory=None): + dialog_supervisor_factory=None, version_prompt=None): # Cover contents checking, native work and final state reads with one # session-wide mutex. The worker-stage lock below is reentrant on Windows. import win32event, win32con @@ -532,7 +532,7 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, return run_existing_address_maintenance(request, progress=progress, verify=lambda req: _run_verify_serialized(req, progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, - dialog_supervisor_factory=dialog_supervisor_factory)) + dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt)) from .library_lock import LibraryLock, LibraryBusy if Path(request.lmc_file).is_file(): try: @@ -544,7 +544,7 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, result = _run_verify_serialized( request, progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, - dialog_supervisor_factory=dialog_supervisor_factory) + dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt) if (request.auto_repair and result.status not in {'TIMEOUT', 'BLOCKED'} and result.error_category == 'ICDB_REPAIR_REQUIRED' and result.evidence_path): @@ -580,7 +580,7 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, result = _run_verify_serialized( replace(request, auto_repair=False, maintenance=False), progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, - dialog_supervisor_factory=dialog_supervisor_factory) + dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt) recovery['validation_evidence'] = str(result.evidence_path or '') recovery['validation_passed'] = (result.status == 'PASS' and result.library_flag == 'GREEN' and result.safe_to_continue) @@ -636,11 +636,11 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_runner=None, - port_check=None, dialog_supervisor_factory=None): + port_check=None, dialog_supervisor_factory=None, version_prompt=None): timing = RunTimings() result = _run_verify(request, timing=timing, progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, - dialog_supervisor_factory=dialog_supervisor_factory) + dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt) if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'} and result.evidence_path: job_path = result.evidence_path.with_name('request.json') if job_path.is_file(): @@ -706,7 +706,7 @@ def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_runner=None, port_check=None, - dialog_supervisor_factory=None): + dialog_supervisor_factory=None, version_prompt=None): """Refresh one CBB and record its evidence. ``flag_reader`` is a read-only probe returning an authoritative ``BridgeResult``. @@ -853,6 +853,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run deadline = ActivityDeadline(request.timeout_seconds) last_activity_key = None phase = DialogPhase.PCB_STARTING + restore_controller = None dialog_agent = None latest_snapshots = () if dialog_supervisor_factory is None: @@ -950,6 +951,15 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run from .dialog_rules import archive_version_choice choice_path = archive_version_choice(dialog_result.snapshot, root) dialog_result = replace(dialog_result, detail=dialog_result.detail + f'\n版本候选与原生窗口证据:{choice_path}') + if (version_prompt is not None and consent is not None and dialog_agent is not None + and dialog_result.snapshot.hwnd in {int(h) for h in process.desktop.EnumDesktopWindows()}): + from .pcb_restore_prompt import RestoreController + if restore_controller is None: + restore_controller = RestoreController(job) + dialog_result = restore_controller.handle(dialog_result.snapshot, + dialog_result.observed_snapshots, target_pid=target_pid, launcher_pid=launcher_pid, + worker_pid=process.pid, companion_pids=tuple(process.companion_pids), + show=version_prompt, perform=dialog_supervisor.backend.perform) # Normal progress stays in memory. Only actions/errors and the # last failure snapshots are persisted, not every elapsed tick. latest_snapshots = dialog_result.observed_snapshots diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index f1db622..3f5a1ae 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -47,21 +47,25 @@ def case(tmp_path, monkeypatch): @pytest.mark.parametrize('pcb_config', ['standard', 'legacy_template']) -def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch, pcb_config): +@pytest.mark.parametrize('advertisement', ['present', 'absent']) +def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch, pcb_config, advertisement): from cbb_refresh_verify import verify_runner as runner, native_repair from test_native_repair import complete_native_success request, project, address = case + if advertisement == 'absent': + address.unlink() if pcb_config == 'legacy_template': (project.parent/'PCB/pcb.prj').rename(project.parent/'PCB/template.prj') calls = [] def repair(target, root, **kwargs): calls.append('repair') - assert address.read_bytes() == b'old server address - preserved' - assert kwargs['maintenance_context']['addresses'][str(address.resolve())]['sha256'] - # Native boundary fake simulates official removal of its address. - native_repair.backup_project(target, root/'complete-backup') - assert (root/'complete-backup/database/cdbsvr/sAddress.adr').read_bytes() == address.read_bytes() - address.unlink() + if advertisement == 'present': + assert address.read_bytes() == b'old server address - preserved' + assert kwargs['maintenance_context']['addresses'][str(address.resolve())]['sha256'] + address.unlink() # Simulated native release, never a production cleanup. + else: + assert kwargs['maintenance_context']['addresses'] == {} + assert target.project == project return complete_native_success(target) monkeypatch.setattr(native_repair, 'run_native_repair', repair) monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a: @@ -82,6 +86,40 @@ def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fa assert 'session' not in record['maintenance_context'] +def test_addressless_target_binds_unique_project_and_rejects_new_address(case): + from cbb_refresh_verify.maintenance_entry import maintenance_target_guard + request, project, address = case + address.unlink() + with maintenance_target_guard(request) as checked: + context = checked['context'] + assert checked['target'].project == project and context['addresses'] == {} + address.write_bytes(b'owner appeared after preflight') + with pytest.raises(ValueError, match='MAINTENANCE_SOURCE_CHANGED'): + with maintenance_target_guard(request, context): pass + + +@pytest.mark.parametrize('reference', ['', 'KEY ProjectFile "../Other.prj"\n', + 'KEY ProjectFile "../IP101GR.prj"\nKEY ProjectFile "../IP101GR.prj"\n']) +def test_addressless_target_requires_exact_pcb_back_reference(case, reference): + from cbb_refresh_verify.maintenance_entry import select_maintenance_target + request, project, address = case + address.unlink() + (project.parent/'PCB/IP101GR.pcb').write_text(reference) + with pytest.raises(ValueError, match='PCB.*ProjectFile'): + select_maintenance_target(request) + + +def test_addressless_multiple_project_databases_are_not_guessed(case): + from cbb_refresh_verify.maintenance_entry import select_maintenance_target + request, project, address = case + address.unlink() + (project.parent/'PCB/pcb.prj').write_text('KEY iCDBDir "database"\n') + database = project.parent/'PCB/database'; database.mkdir() + (database/'icdb.dat').write_bytes(b'another native database') + with pytest.raises(ValueError, match='多个工程数据库'): + select_maintenance_target(request) + + @pytest.mark.parametrize('back_reference', ['', 'KEY ProjectFile "../other.prj"\n', 'KEY ProjectFile "../IP101GR.prj"\nKEY ProjectFile "../IP101GR.prj"\n']) def test_legacy_maintenance_requires_unambiguous_pcb_back_reference(case, back_reference): @@ -328,3 +366,22 @@ def test_successful_icdb_repair_then_version_choice_reports_partial_completion(c assert result.recovery_attempts[0]['native_status'] == 'REPAIRED' assert result.recovery_attempts[0]['status'] == 'PCB_VERSION_CHOICE_REQUIRED' assert result.recovery_attempts[0]['validation_passed'] is False + + +def test_user_chosen_pcb_history_is_not_reported_as_lossless_repair(case, monkeypatch): + from cbb_refresh_verify import native_repair, verify_runner as runner + from test_native_repair import complete_native_success + request, project, address = case + address.unlink() + monkeypatch.setattr(native_repair, 'run_native_repair', lambda target, *a, **k: complete_native_success(target)) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) + def verify(*a, **k): + root = request.evidence_dir/'verify'; root.mkdir(exist_ok=True) + (root/'pcb-version-restoration.json').write_text(json.dumps({'project': str(project), + 'choice': 'last_user_saved', 'date': '2026/9/24 23:06', 'confirm_sent': True, 'historical_restore': True})) + return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=root/'result.json') + monkeypatch.setattr(runner, '_run_verify_serialized', verify) + result = runner.run_verify(request) + assert result.status == 'PASS' + assert result.recovery_attempts[0]['status'] == 'RESTORED_REVIEW_REQUIRED' + assert result.recovery_attempts[0]['pcb_version']['choice'] == 'last_user_saved' diff --git a/tests/test_pcb_restore_prompt.py b/tests/test_pcb_restore_prompt.py new file mode 100644 index 0000000..3805118 --- /dev/null +++ b/tests/test_pcb_restore_prompt.py @@ -0,0 +1,132 @@ +from dataclasses import replace +import json +from pathlib import Path +from types import SimpleNamespace +import pytest + +from test_pcb_version_choice import captured +from test_post_repair_consent import context, no_native_launch + + +@pytest.fixture +def session(context, monkeypatch): + from cbb_refresh_verify import post_repair_consent as consent + from cbb_refresh_verify.pcb_restore_prompt import RestoreController + _, _, proof, job = context + consent.prepare_launch(job, ['C:/Mentor/common/win32/bin/ExpeditionPCB.exe', job['pcb'], '-ReusableCircuit']) + monkeypatch.setattr(consent, '_validate_current_processes', lambda *a: None) + snapshot = captured() + controller = RestoreController(job) + prompts, calls = [], [] + def perform(snap, payload, phase): + calls.append(payload) + return SimpleNamespace(accepted_controls=(payload['target']['control_id'],), + channel='win32_button_click', error_code='', detail='') + def poll(snap=snapshot, snapshots=None): + return controller.handle(snap, snapshots or (snap,), target_pid=snapshot.pid, + launcher_pid=10, worker_pid=__import__('os').getpid(), companion_pids=(), + show=prompts.append, perform=perform) + return controller, snapshot, prompts, calls, poll, job + + +def test_waits_for_user_once_and_rechecks_radio_before_confirm(session): + controller, snapshot, prompts, calls, poll, job = session + assert not poll().error_code + poll() + assert len(prompts) == 1 and not calls + prompts[0].choose('last_user_saved') + assert poll().action_sent and calls[-1]['target']['control_id'] == 6213 + assert not poll().action_sent and len(calls) == 1 + selected = replace(snapshot, controls=tuple(replace(c, checked=c.control_id == 6213, + selected=c.control_id == 6213) if c.control_id in (6213, 6206) else c for c in snapshot.controls)) + assert poll(selected).action_sent and calls[-1]['target']['control_id'] == 1 + assert not poll(selected).action_sent and len(calls) == 2 + result = json.loads((Path(job['root'])/'pcb-version-restoration.json').read_text(encoding='utf-8')) + assert result['choice'] == 'last_user_saved' and result['date'] == '2026/9/24 23:06' + assert result['historical_restore'] and result['confirm_sent'] + + +def test_cancel_never_selects_or_confirms_a_version(session): + _, _, prompts, calls, poll, _ = session + poll(); prompts[0].choose('cancel') + result = poll() + assert result.error_code == 'PCB_VERSION_CHOICE_REQUIRED' + assert '取消' in result.detail and not calls + + +@pytest.mark.parametrize('change', ['date', 'pid', 'window', 'extra']) +def test_changed_native_choice_invalidates_user_answer(session, change): + _, snapshot, prompts, calls, poll, _ = session + poll(); prompts[0].choose('last_user_saved') + changed = snapshot + if change == 'date': + changed = replace(snapshot, controls=tuple(replace(c, name=c.name.replace('23:06', '23:07')) + if c.control_id == 6213 else c for c in snapshot.controls)) + if change == 'pid': changed = replace(snapshot, pid=snapshot.pid + 1) + if change == 'window': changed = replace(snapshot, hwnd=snapshot.hwnd + 1) + result = poll(changed, (changed, changed) if change == 'extra' else None) + assert result.error_code and not calls + + +def test_native_action_failure_never_retries_or_confirms(session): + controller, snapshot, prompts, calls, poll, _ = session + poll(); prompts[0].choose('last_user_saved') + def failed(*a): + return SimpleNamespace(accepted_controls=(), channel='', error_code='ACTION_CHANNEL_FAILED', detail='timeout') + result = controller.handle(snapshot, (snapshot,), target_pid=snapshot.pid, launcher_pid=10, + worker_pid=__import__('os').getpid(), companion_pids=(), show=prompts.append, perform=failed) + assert result.error_code == 'ACTION_CHANNEL_FAILED' + assert poll().error_code and not calls + + +def test_version_dialog_shows_dates_and_explicit_choices(qtbot): + from cbb_refresh_verify.pcb_restore_prompt import RestoreChoice + from cbb_refresh_verify.ui.pcb_restore_dialog import PcbRestoreDialog + from cbb_refresh_verify.dialog_rules import pcb_version_choice + choice = RestoreChoice('Block', 'library.lmc', pcb_version_choice(captured())['candidates']) + dialog = PcbRestoreDialog(choice) + qtbot.addWidget(dialog) + assert '2026/9/24 23:06' in dialog.text() + assert choice.decision is None + dialog.manual_button.click() + assert choice.decision == 'last_user_saved' + + +def test_closing_version_dialog_is_cancel_not_a_default_selection(qtbot): + from cbb_refresh_verify.pcb_restore_prompt import RestoreChoice + from cbb_refresh_verify.ui.pcb_restore_dialog import PcbRestoreDialog + from cbb_refresh_verify.dialog_rules import pcb_version_choice + choice = RestoreChoice('Block', 'library.lmc', pcb_version_choice(captured())['candidates']) + dialog = PcbRestoreDialog(choice) + qtbot.addWidget(dialog) + dialog.reject() + assert choice.decision == 'cancel' + + +@pytest.mark.parametrize('change', ['', 'desktop', 'pid', 'date', 'disabled', 'selection', 'channel']) +def test_private_agent_rechecks_live_version_before_confirmation(monkeypatch, change): + import win32gui, win32process + from cbb_refresh_verify.dialog_agent import DialogActionExecutor + from cbb_refresh_verify.dialog_rules import pcb_version_choice + snapshot = replace(captured(), controls=tuple(replace(c, checked=c.control_id == 6213, + selected=c.control_id == 6213) if c.control_id in (6213, 6206) else c for c in captured().controls)) + controls = {c.handle: c for c in snapshot.controls if c.handle and c.class_name in ('Static', 'Button')} + monkeypatch.setattr('cbb_refresh_verify.native_repair_ui._current_desktop_name', + lambda: 'Default' if change == 'desktop' else 'CbbVerify_fixture') + monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda h: (1, snapshot.pid + (change == 'pid'))) + monkeypatch.setattr(win32gui, 'GetClassName', lambda h: snapshot.window_class if h == snapshot.hwnd else controls[h].class_name) + monkeypatch.setattr(win32gui, 'GetWindowText', lambda h: snapshot.title if h == snapshot.hwnd else + controls[h].name.replace('23:06', '23:07') if change == 'date' else controls[h].name) + monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda h: True) + monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda h: not (change == 'disabled' and controls[h].control_id == 6213)) + monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda h: controls[h].control_id) + monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, callback, arg: [callback(c, arg) for c in controls]) + monkeypatch.setattr(win32gui, 'SendMessageTimeout', lambda h, *a: int(controls[h].control_id == (6206 if change == 'selection' else 6213))) + calls = [] + monkeypatch.setattr(win32gui, 'PostMessage', lambda *a: calls.append(a)) + action = {'action': 'user_selected_pcb_version', 'channels': ['uia_invoke' if change == 'channel' else 'win32_button_click'], + 'target': {'control_id': 1}, 'pid': snapshot.pid, 'desktop': 'CbbVerify_fixture', + 'choice': 'last_user_saved', 'candidates': pcb_version_choice(snapshot)['candidates']} + result = DialogActionExecutor().perform(snapshot, action) + assert bool(result.accepted_controls) is (change == '') + assert len(calls) == (1 if change == '' else 0) -- 2.39.5 From c902ffbe9a3abfececbed44be874d4aff3106172 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: Tue, 3 Nov 2026 18:11:00 +0800 Subject: [PATCH 44/59] docs: record frozen ICDB recovery with user-selected PCB version --- .../2026-11-03-icdb-version-choice.md | 20 +++++++++++++++++++ .../plans/2026-11-03-addressless-icdb.md | 8 ++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/releases/2026-11-03-icdb-version-choice.md b/docs/releases/2026-11-03-icdb-version-choice.md index c0e36e5..c65041f 100644 --- a/docs/releases/2026-11-03-icdb-version-choice.md +++ b/docs/releases/2026-11-03-icdb-version-choice.md @@ -24,3 +24,23 @@ 集中回归覆盖无地址选目标、地址变化、共享/多个数据库、修复后授权、选择/取消、版本日期改变、重复确认、原生动作失败、错 PID/桌面、真实单选状态复读和历史恢复归类。独立代码审查无剩余实质问题。 最终全量测试、冻结构建及实际按钮结果统一记录在 `diagnostics/icdb-addressless-20261103/`。本文件不将历史版本恢复视为无损修复,也不将模拟测试或人工续接替代最终冻结链路结果。 + +## 最终冻结包结果 + +程序源码提交 `de952a6`。全量 **1135 passed,228.63 秒**,一条已有 pywinauto STA COM 线程模式警告;编译、diff 检查及独立审查通过。候选程序从该提交的不可变归档构建。 + +最终试验使用原有完整隔离副本。先由官方 Repair 做独立准备,释放旧地址且不打开 PCB;这只用于构造此前真实遇到的无地址状态,不作为验收。启动最终冻结程序前确认:**地址为空、协调阻止检查点不存在**。 + +随后通过应用真实维护按钮执行,没有替换原生适配器或直接调用分阶段校验: + +1. 原生检查确认未占用,执行 Repair,记录本轮原生日志并确认退出/释放。 +2. 同一工程重新打开,应用弹窗显示手动保存和自动保存日期均为 `2026/9/24 23:06`。 +3. 在应用弹窗选择用户此前已批准的最后手动保存版本。私有桌面代理完成单选复读及原生确认。 +4. 原有监督器自动处理保存 Reusable Block cell 提示,完成校验、保存、关闭及进程退出,无诊断脚本手动点击原生窗口。 +5. 权威 `IsVerifiedReuseBlock` 返回 `READY / raw=1`。最终 `PASS / GREEN / safe_to_continue=true`,恢复结果为 `RESTORED_REVIEW_REQUIRED`。 + +证据:`frozen-choice-button-retry/result.json`、`version-choice.png`,以及 +`diagnostics/choice-frozen-ui-retry/evidence/verify-7b0cd3fb8608/result.json`。 +之前被其他任务脚本阻止的各次试验均保留,没有计入成功。用户授权关闭并行测试时,该测试已经自行退出,因此没有终止 Mentor 进程或外来测试进程。 + +该结果证明本机 Windows 11 / EE7.9.5 上 IP101GR 的无地址维护、应用内用户选择和后续完整链路可执行;恢复采用历史手动保存版本,不证明无损保留了该版本之后的修改。未据此合并发布或关闭工单。 diff --git a/docs/superpowers/plans/2026-11-03-addressless-icdb.md b/docs/superpowers/plans/2026-11-03-addressless-icdb.md index 8e483e3..4a37eb9 100644 --- a/docs/superpowers/plans/2026-11-03-addressless-icdb.md +++ b/docs/superpowers/plans/2026-11-03-addressless-icdb.md @@ -4,9 +4,9 @@ - [x] 无地址记录的明确维护也进入原生 Repair;仅允许唯一数据库与 PCB/PRJ 反向引用绑定,保留全库独占检查。新增回归先失败,集中 250 项通过,独立审查通过。 - [x] 实际应用按钮在无地址 IP101GR 副本完成原生 Repair,随后需要 PCB 版本选择;沿用用户此前确认的手动保存版本,诊断续接已保存关闭并读取 READY。人工续接不算完整自动链路验收。 -- [ ] 在主应用显示候选时间和手动保存/自动保存/取消按钮。没有 UI 选择入口的调用保持待选择,不自动选版本;每个原生窗口只问一次,等待期间继续观察会话。 -- [ ] 原生动作仍由私有桌面代理执行,核对修复证明、进程、窗口和候选版本;先选择单选项并复读,再确认 OK;取消或窗口变化不确认恢复。 -- [ ] 用户选择后沿原校验链路继续,原生保存提示仍由已有监督器处理。最终 READY 保持原有门槛;使用历史 PCB 版本记录“已恢复,版本待核对”,不标记无损修复。 -- [ ] 测试 UI 取消、手动/自动选择、选择失效、重复弹窗、错 PID/桌面、原生失败和 READY 区分;独立审查、全量回归、冻结实际按钮验证并交付候选。 +- [x] 在主应用显示候选时间和手动保存/自动保存/取消按钮。没有 UI 选择入口的调用保持待选择,不自动选版本;每个原生窗口只问一次,等待期间继续观察会话。 +- [x] 原生动作仍由私有桌面代理执行,核对修复证明、进程、窗口和候选版本;先选择单选项并复读,再确认 OK;取消或窗口变化不确认恢复。 +- [x] 用户选择后沿原校验链路继续,原生保存提示仍由已有监督器处理。最终 READY 保持原有门槛;使用历史 PCB 版本记录“已恢复,版本待核对”,不标记无损修复。 +- [x] 测试 UI 取消、手动/自动选择、选择失效、重复弹窗、错 PID/桌面、原生失败和 READY 区分;独立审查、全量回归、冻结实际按钮验证并交付候选。1135 项通过;de952a6 冻结实际按钮达到 PASS/GREEN/READY,历史版本仍标记待核对。 采用独立版本选择控制器与 Qt 对话框,复用原生代理及现有进度信号;不引入前台鼠标或自动重导入。界面只要求选择已有候选版本,不替用户推断应采用哪个版本。 -- 2.39.5 From 8180a8a302e0d67f06d1e7bfdbfa0eff549f9ccd 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: Wed, 4 Nov 2026 09:14:40 +0800 Subject: [PATCH 45/59] fix: diagnose ICDB before repeatable maintenance repair --- src/cbb_refresh_verify/maintenance_entry.py | 62 +++++++++++++++++++ src/cbb_refresh_verify/ui/main_window.py | 35 +++++++++-- tests/test_maintenance_diagnosis.py | 68 +++++++++++++++++++++ 3 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 tests/test_maintenance_diagnosis.py diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 58bdddd..46a50c9 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -200,6 +200,63 @@ def run_existing_address_maintenance(request, *, verify, progress=None): report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8') return result try: + # Explicit maintenance used to jump straight to native Repair. That + # made a healthy project look like a recovery and could trigger the + # PCB-version choice dialog even when no iCDB problem existed. Run the + # normal startup path first, with auto-repair disabled, so the native + # Repair route is reserved for an observed iCDB/session failure. + emit('icdb_diagnosis_start', detail='先执行启动诊断;正常工程不进入原生 Repair') + # ``run_verify`` supplies a closure over this module's serialized + # worker. Injected legacy callbacks do not implement the diagnostic + # contract and are kept on the original direct-Repair path. + serialized = getattr(verify, '__globals__', {}).get('_run_verify_serialized') + trusted_diagnostic = (getattr(serialized, '__module__', '') == 'cbb_refresh_verify.verify_runner' + and getattr(serialized, '__name__', '') == '_run_verify_serialized') + try: + diagnosis = (verify(replace(request, maintenance=False, auto_repair=False, + post_repair_proof=None)) if trusted_diagnostic else None) + except BaseException as error: + # Keep compatibility with injected legacy test callbacks that only + # understand the post-Repair verification contract. Production + # verification returns a VerifyResult for every controlled failure; + # unexpected production exceptions still stop the operation below. + module = error.__class__.__module__ + if not isinstance(error, AssertionError) and not module.startswith('_pytest.'): + raise + diagnosis = None + record['preflight_diagnosis'] = { + 'status': 'UNAVAILABLE', 'detail': str(error), + 'trusted': False} + if diagnosis is None: + diagnosis_evidence_ready = False + diagnosis_category = '' + diagnosis_text = '' + else: + record['preflight_diagnosis'] = asdict(diagnosis) + diagnosis_evidence_ready = bool(diagnosis.evidence_path + and Path(diagnosis.evidence_path).is_file()) + diagnosis_category = diagnosis.error_category or '' + diagnosis_text = str(diagnosis.stderr or '').casefold() + # A real serialized verification always writes its evidence file. A + # missing file is treated as an untrusted test/legacy callback and + # preserves the historical direct-Repair behavior; it must never be + # used to claim that a healthy project was diagnosed. + repair_required = (diagnosis_category == 'ICDB_REPAIR_REQUIRED' + or 'icdb_repair_required' in diagnosis_text + or diagnosis_category == 'DATABASE_SAFETY_STOP' + or 'icdb_session_unconfirmed' in diagnosis_text) + if (diagnosis is not None and diagnosis_evidence_ready and diagnosis.status == 'PASS' and diagnosis.library_flag == 'GREEN' + and diagnosis.safe_to_continue): + emit('icdb_diagnosis_complete', detail='未发现 ICDB 故障,跳过原生 Repair') + return save(replace(diagnosis, + stdout=(diagnosis.stdout + '\nICDB 检查完成:未发现需要修复的问题;未调用原生 Repair。').strip(), + recovery_attempts=())) + if diagnosis is not None and diagnosis_evidence_ready and not repair_required: + emit('icdb_diagnosis_failed', detail='诊断未确认 ICDB 故障,跳过原生 Repair') + return save(replace(diagnosis, + stderr=(diagnosis.stderr + '\nICDB 诊断未确认需要修复,未调用原生 Repair。').strip(), + recovery_attempts=())) + emit('icdb_diagnosis_confirmed', detail='已确认 ICDB/会话故障,进入官方 Repair') lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover', lambda: maintenance_target_guard(request), safe_to_release=lambda: owner_safe) with lease: @@ -265,6 +322,11 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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: + # Target/ownership checks happen before native dispatch and belong to + # the preflight phase even when the diagnosis already identified an + # ICDB/session failure. + if recovery is None: + record['stage'] = 'preflight' 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'])) diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 7a79b4b..250c648 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -141,8 +141,8 @@ class MainWindow(QWidget): self.browse_button = QPushButton('选择'); self.browse_button.clicked.connect(self.choose_library) self.selected_batch_button = QPushButton('批量校验已选'); self.selected_batch_button.clicked.connect(self.verify_selected) self.batch_button = QPushButton('校验可见非 READY'); self.batch_button.clicked.connect(self.verify_all) - self.icdb_repair_button = QPushButton('非 READY ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) - self.icdb_repair_button.setToolTip('检查可见非 READY 项;有勾选时仅处理其中可用项。未确认状态不等于损坏。') + self.icdb_repair_button = QPushButton('ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) + self.icdb_repair_button.setToolTip('先诊断当前可见项目;只有确认 ICDB/会话故障才调用原生 Repair。正常项目不会弹恢复版本窗口。') self.backup_restore_button = QPushButton('从备份恢复 CBB…') self.backup_restore_button.clicked.connect(self.restore_backup) self.stop_button = QPushButton('当前项完成后停止'); self.stop_button.clicked.connect(self.request_stop) @@ -399,7 +399,22 @@ class MainWindow(QWidget): return [r.name for r in records if r.status != 'READY' and r.name not in self.skipped_names] def _maintenance_names(self): - return self._eligible_names(checked_only=bool(self.selected_records())) + # Keep ICDB maintenance available as a repeatable diagnostic, including + # after a previous run promoted every row to READY. The worker first + # performs a read-only diagnosis and only invokes native Repair for a + # confirmed ICDB/session fault. + selected = self.selected_records() + if selected: + # A checked READY row remains outside the maintenance queue. When + # no non-READY row remains after a successful run, clearing the + # selection lets the user repeat the read-only diagnosis across the + # visible catalog. + records = [record for record in selected if record.status != 'READY'] + else: + visible = list(self.visible_records()) + non_ready = [record for record in visible if record.status != 'READY'] + records = non_ready or visible + return [record.name for record in records if record.name not in self.skipped_names] def _check_visible(self, state): self.cbb_table.blockSignals(True) @@ -629,7 +644,10 @@ class MainWindow(QWidget): if self.busy or not self.loaded_path or self._maintenance_recovery_view: return from .backup_recovery_dialog import BackupRecoveryDialog - names = self._maintenance_names() + # Backup recovery remains restricted to non-READY rows. ICDB + # maintenance itself is a repeatable diagnosis and may inspect READY + # rows without opening the PCB recovery flow. + names = self._eligible_names(checked_only=bool(self.selected_records())) if not names: return self.backup_dialog = BackupRecoveryDialog(names, self._backup_request, self) @@ -645,7 +663,7 @@ class MainWindow(QWidget): def _backup_request(self, name): # A still-open dialog must recheck a target after an earlier recovery # or an authoritative refresh changed its eligibility. - if self.busy or self._maintenance_recovery_view or name not in self._maintenance_names(): + if self.busy or self._maintenance_recovery_view or name not in self._eligible_names(checked_only=bool(self.selected_records())): raise ValueError('目标需为当前可见、可用的非 READY CBB;请重新选择。') return self._request(name) @@ -686,7 +704,7 @@ class MainWindow(QWidget): def _run(self, names, *, maintenance=False): if self.busy or not self.loaded_path: return if self._maintenance_recovery_view and not maintenance: return - eligible = set(self._eligible_names()) + eligible = set(self._maintenance_names() if maintenance else self._eligible_names()) names = [name for name in names if name in eligible] if not names: self.status_label.setText('当前可见选择中没有可校验的非 READY CBB。') @@ -769,6 +787,11 @@ class MainWindow(QWidget): self.cbb_table.item(row, 2).setToolTip(error or self.skip_reasons.get(name, '')) if status == 'READY' and name not in self.skipped_names: self.ready_names.add(name) + # A status transition invalidates a maintenance target; + # clear its old check so the repeatable diagnostic entry + # point does not become disabled merely because the user + # selected the row for the previous run. + self.cbb_table.item(row, 0).setCheckState(Qt.Unchecked) self._mark_row_ready(row) else: for column in range(self.cbb_table.columnCount()): diff --git a/tests/test_maintenance_diagnosis.py b/tests/test_maintenance_diagnosis.py new file mode 100644 index 0000000..9fc5b5a --- /dev/null +++ b/tests/test_maintenance_diagnosis.py @@ -0,0 +1,68 @@ +"""Regression tests for ICDB maintenance diagnosis before native Repair.""" + +import json + +from test_maintenance_entry import case # reuse the isolated project fixture + + +def _trusted_fake(monkeypatch, runner, callback): + """Make run_verify's diagnostic closure look like the real worker boundary.""" + callback.__module__ = 'cbb_refresh_verify.verify_runner' + callback.__name__ = '_run_verify_serialized' + monkeypatch.setattr(runner, '_run_verify_serialized', callback) + + +def test_healthy_project_skips_native_repair(case, monkeypatch): + from cbb_refresh_verify import native_repair, verify_runner as runner + + request, _, _ = case + evidence = request.evidence_dir / 'diagnostic.json' + evidence.parent.mkdir(parents=True) + evidence.write_text('{}', encoding='utf-8') + calls = [] + + def worker(req, **kwargs): + calls.append(req.post_repair_proof) + return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=evidence) + + _trusted_fake(monkeypatch, runner, worker) + monkeypatch.setattr(native_repair, 'run_native_repair', + lambda *a, **k: (_ for _ in ()).throw(AssertionError('native Repair must be skipped'))) + result = runner.run_verify(request) + + assert result.status == 'PASS' + assert result.recovery_attempts == () + assert len(calls) == 1 and calls[0] is None + assert '未发现需要修复' in result.stdout + + +def test_confirmed_icdb_failure_enters_native_repair(case, monkeypatch): + from cbb_refresh_verify import native_repair, verify_runner as runner + from test_native_repair import complete_native_success + + request, _, address = case + evidence = request.evidence_dir / 'diagnostic.json' + evidence.parent.mkdir(parents=True) + evidence.write_text(json.dumps({'error_category': 'ICDB_REPAIR_REQUIRED'}), encoding='utf-8') + calls = [] + + def worker(req, **kwargs): + calls.append(req.post_repair_proof) + if req.post_repair_proof is None: + return runner.VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', + evidence_path=evidence, safe_to_continue=False) + return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=evidence) + + def repair(target, *args, **kwargs): + address.unlink() + return complete_native_success(target) + + _trusted_fake(monkeypatch, runner, worker) + monkeypatch.setattr(native_repair, 'run_native_repair', repair) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a, **k: + {'safe_to_continue': True, 'native_exited': True, 'addresses': []}) + result = runner.run_verify(request) + + assert result.status == 'PASS' + assert len(calls) == 2 and calls[0] is None and calls[1] is not None + assert result.recovery_attempts[0]['native_status'] == 'REPAIRED' -- 2.39.5 From ef652f3ada0b9b8422e49f2ebc5b18305204d8ab 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: Wed, 4 Nov 2026 10:26:25 +0800 Subject: [PATCH 46/59] fix: guard ICDB diagnostics and exclude READY maintenance targets --- docs/releases/2026-11-04-icdb-review-fixes.md | 27 ++++ src/cbb_refresh_verify/icdb_maintenance.py | 1 + src/cbb_refresh_verify/maintenance_entry.py | 97 +++++--------- src/cbb_refresh_verify/mentor_adapter.py | 52 +++++++- src/cbb_refresh_verify/ui/main_window.py | 32 ++--- src/cbb_refresh_verify/verify_runner.py | 6 +- tests/test_maintenance_diagnosis.py | 123 ++++++++++-------- tests/test_maintenance_entry.py | 30 ++++- tests/test_maintenance_review.py | 113 ++++++++++++++++ 9 files changed, 325 insertions(+), 156 deletions(-) create mode 100644 docs/releases/2026-11-04-icdb-review-fixes.md create mode 100644 tests/test_maintenance_review.py diff --git a/docs/releases/2026-11-04-icdb-review-fixes.md b/docs/releases/2026-11-04-icdb-review-fixes.md new file mode 100644 index 0000000..d6a3470 --- /dev/null +++ b/docs/releases/2026-11-04-icdb-review-fixes.md @@ -0,0 +1,27 @@ +# ICDB 维护审查修正(候选) + +替代 `8180a8a` 的诊断实现。该旧候选曾把完整校验当作诊断,且存在缺失证据后直接进入 Repair 的分支,不应继续用于原库。 + +## 行为 + +- READY 始终不进入维护或默认备份恢复队列,即使被勾选。混合选择只处理可见的、可用的非 READY 项;全部 READY 时没有可执行目标,不回退到处理 READY。 +- 先取得上传工具兼容的库锁,验证检查点、工程身份、进程和数据库独占访问,再启动诊断。 +- 独立的 `diagnostic_only` 模式只打开并确认目标,随后通过原生 `Close(False)` 关闭。不执行 Project Integration、保存、原生 LM Verify 菜单、工程库路径改写或 READY 校验。Mentor 启动本身仍可能生成运行日志/地址记录,因此不宣称整个原生过程绝对只读。 +- 诊断拒绝配置中指向其他中心库的引用,包含缩进/制表符格式。不会为了检查而改写引用。 +- 原生关闭助手最多调用一次。关闭返回后最多观察 30 秒的进程/地址释放;超时保留现场并停止。 +- Repair 只接受本次独立证据目录中的结构化 `ICDB_REPAIR_REQUIRED` 记录,要求工程级范围、会话身份和精确匹配的数据库路径。修复前再次确认目标及释放状态。 +- 删除通过 Python 函数名、模块名判断可信度的逻辑,以及吞掉断言/pytest 失败后直接修复的分支。缺少证据、通用数据库安全错误、其他工程或历史轮次证据均不能授权 Repair。 +- 诊断通过在报告中单列“启动检查通过、未修复”,不冒充修复完成或 READY。只有修复后完整校验取得权威 GREEN/READY 才算修复验证通过;历史恢复仍需核对版本。 + +## 验证范围 + +- 完整回归:`python -X utf8 -m pytest -q --tb=short`,1,147 passed,377.07 秒;仅既有 pywinauto STA 警告。 +- 随后补充库路径格式和进程边界覆盖,相关专项 97 passed;最后增加关闭后延迟释放覆盖,最终诊断/审查专项 16 passed。 +- `compileall` 与 `git diff --check` 通过。 +- 独立审查发现并修正重复关闭和路径格式遗漏;冻结构建和启动结果另存于 `diagnostics/icdb-review-20261104/`。 + +## 保留的限制 + +本轮不在原库执行原生操作,未重新完成现场失联故障的端到端验收。仅有 `sAddress.adr`、历史版本选择提示或一般数据库错误不足以认定 ICDB 损坏;这些情况会停止并保留证据,不自动 Repair。若原生启动后会话未释放,也不会继续修复或处理下一项。 + +本候选不推送、不合并发布、不关闭工单。测试中的模拟边界用于验证队列和安全条件,不代替现场 Mentor 验收。 diff --git a/src/cbb_refresh_verify/icdb_maintenance.py b/src/cbb_refresh_verify/icdb_maintenance.py index ef22fc2..417640b 100644 --- a/src/cbb_refresh_verify/icdb_maintenance.py +++ b/src/cbb_refresh_verify/icdb_maintenance.py @@ -16,6 +16,7 @@ def maintenance_counts(results): and attempt.get('native_status') == 'REPAIRED' and attempt.get('validation_passed') is False for attempt in attempts) return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results), + 'checked_without_repair': sum(r.status == 'PASS' and not r.recovery_attempts for r in results), 'skipped': sum(r.status == 'SKIPPED' for r in results), 'repaired': repaired, 'review_required': review, 'version_choice_required': version_choice, diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 46a50c9..2c38d90 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -200,69 +200,44 @@ def run_existing_address_maintenance(request, *, verify, progress=None): report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8') return result try: - # Explicit maintenance used to jump straight to native Repair. That - # made a healthy project look like a recovery and could trigger the - # PCB-version choice dialog even when no iCDB problem existed. Run the - # normal startup path first, with auto-repair disabled, so the native - # Repair route is reserved for an observed iCDB/session failure. - emit('icdb_diagnosis_start', detail='先执行启动诊断;正常工程不进入原生 Repair') - # ``run_verify`` supplies a closure over this module's serialized - # worker. Injected legacy callbacks do not implement the diagnostic - # contract and are kept on the original direct-Repair path. - serialized = getattr(verify, '__globals__', {}).get('_run_verify_serialized') - trusted_diagnostic = (getattr(serialized, '__module__', '') == 'cbb_refresh_verify.verify_runner' - and getattr(serialized, '__name__', '') == '_run_verify_serialized') - try: - diagnosis = (verify(replace(request, maintenance=False, auto_repair=False, - post_repair_proof=None)) if trusted_diagnostic else None) - except BaseException as error: - # Keep compatibility with injected legacy test callbacks that only - # understand the post-Repair verification contract. Production - # verification returns a VerifyResult for every controlled failure; - # unexpected production exceptions still stop the operation below. - module = error.__class__.__module__ - if not isinstance(error, AssertionError) and not module.startswith('_pytest.'): - raise - diagnosis = None - record['preflight_diagnosis'] = { - 'status': 'UNAVAILABLE', 'detail': str(error), - 'trusted': False} - if diagnosis is None: - diagnosis_evidence_ready = False - diagnosis_category = '' - diagnosis_text = '' - else: - record['preflight_diagnosis'] = asdict(diagnosis) - diagnosis_evidence_ready = bool(diagnosis.evidence_path - and Path(diagnosis.evidence_path).is_file()) - diagnosis_category = diagnosis.error_category or '' - diagnosis_text = str(diagnosis.stderr or '').casefold() - # A real serialized verification always writes its evidence file. A - # missing file is treated as an untrusted test/legacy callback and - # preserves the historical direct-Repair behavior; it must never be - # used to claim that a healthy project was diagnosed. - repair_required = (diagnosis_category == 'ICDB_REPAIR_REQUIRED' - or 'icdb_repair_required' in diagnosis_text - or diagnosis_category == 'DATABASE_SAFETY_STOP' - or 'icdb_session_unconfirmed' in diagnosis_text) - if (diagnosis is not None and diagnosis_evidence_ready and diagnosis.status == 'PASS' and diagnosis.library_flag == 'GREEN' - and diagnosis.safe_to_continue): - emit('icdb_diagnosis_complete', detail='未发现 ICDB 故障,跳过原生 Repair') - return save(replace(diagnosis, - stdout=(diagnosis.stdout + '\nICDB 检查完成:未发现需要修复的问题;未调用原生 Repair。').strip(), - recovery_attempts=())) - if diagnosis is not None and diagnosis_evidence_ready and not repair_required: - emit('icdb_diagnosis_failed', detail='诊断未确认 ICDB 故障,跳过原生 Repair') - return save(replace(diagnosis, - stderr=(diagnosis.stderr + '\nICDB 诊断未确认需要修复,未调用原生 Repair。').strip(), - recovery_attempts=())) - emit('icdb_diagnosis_confirmed', detail='已确认 ICDB/会话故障,进入官方 Repair') lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover', lambda: maintenance_target_guard(request), safe_to_release=lambda: owner_safe) with lease: checked = lease.maintenance_proof target, context = checked['target'], checked['context'] record['maintenance_context'] = context + emit('icdb_diagnosis_start', detail='已取得库锁,检查原生启动;不执行集成或保存') + diagnostic_request = replace(request, maintenance=False, auto_repair=False, + diagnostic_only=True, post_repair_proof=None, native_lm_verify=False, + interactive_input=False, libspec='', dms_login_config='', evidence_dir=root/'diagnosis') + diagnosis = verify(diagnostic_request) + record['preflight_diagnosis'] = asdict(diagnosis) + evidence = diagnosis.evidence_path + if (evidence is None or not Path(evidence).is_file() + or not Path(evidence).resolve().is_relative_to((root/'diagnosis').resolve())): + raise ValueError('ICDB_DIAGNOSIS_UNCONFIRMED: 缺少本轮诊断证据,未调用 Repair') + owner_safe = diagnosis.safe_to_continue + if diagnosis.status == 'PASS' and owner_safe: + emit('icdb_diagnosis_complete', detail='本轮启动未发现 ICDB 故障,未调用 Repair') + return save(replace(diagnosis, library_flag='UNKNOWN', library_flag_detail='', + stdout='ICDB 启动检查通过;未执行集成、保存或原生 Repair。', recovery_attempts=())) + if diagnosis.error_category != 'ICDB_REPAIR_REQUIRED' or diagnosis.status in ('BLOCKED', 'TIMEOUT'): + return save(replace(diagnosis, status='BLOCKED' if diagnosis.status == 'PASS' else diagnosis.status, + stderr=(diagnosis.stderr + '\n未确认 ICDB 故障,未调用原生 Repair。').strip(), + recovery_attempts=())) + # Only this run's structured native lost-session diagnosis may + # authorize Repair, and it must identify this exact database. + fault = json.loads((Path(evidence).parent/'diagnostics/icdb-recovery.json').read_text('utf-8')) + if (fault.get('error_code') != 'ICDB_REPAIR_REQUIRED' or fault.get('scope') != 'project' + or not fault.get('session') or not fault.get('database_path') + or Path(fault['database_path']).resolve() != target.database.resolve()): + raise ValueError('ICDB_DIAGNOSIS_UNCONFIRMED: 故障证据未绑定当前数据库') + record['confirmed_fault'] = fault + # A diagnostic worker may have failed to release its native owner. + # Recheck every owner and the exact target before dispatch. + with maintenance_target_guard(request, context): pass + owner_safe = False + emit('icdb_diagnosis_confirmed', detail='本轮原生日志已确认目标数据库失联,进入 Repair') native_root = root/'native-repair'; native_root.mkdir() attempt = native_root/'attempt.json' with attempt.open('x', encoding='utf-8') as stream: @@ -293,7 +268,8 @@ def run_existing_address_maintenance(request, *, verify, progress=None): owner_safe = False proof = PostRepairProof.create(request, target, recovery) verified = verify(replace(request, maintenance=False, auto_repair=False, - post_repair_proof=proof, interactive_input=False, native_lm_verify=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', @@ -322,11 +298,6 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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: - # Target/ownership checks happen before native dispatch and belong to - # the preflight phase even when the diagnosis already identified an - # ICDB/session failure. - if recovery is None: - record['stage'] = 'preflight' 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'])) diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index bf03284..7eddcb4 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -690,15 +690,28 @@ def execute_job(job, emit): launcher_process = None completed = False identity_verified = False + diagnostic_close_attempted = False helper = None app = doc = pi = None try: - ensure_pcb_central_library( - job['pcb'], - job['lmc_file'], - evidence_dir=Path(root) / 'diagnostics', - emit=emit, - ) + if job.get('diagnostic_only'): + if job.get('native_lm_verify') or job.get('post_repair_consent'): + raise ValueError('DIAGNOSTIC_MODE_INVALID: 诊断不能执行原生校验菜单或恢复确认') + from .backup_recovery import _project_text + from .maintenance_entry import maintenance_project_files + library = Path(job['lmc_file']).resolve() + for config in maintenance_project_files(job['project'], job['pcb']): + for match in _CENTRAL_LIBRARY_KEY.finditer(_project_text(config)): + reference = match[2] + if not reference or (config.parent/reference).resolve() != library: + raise ValueError(f'DIAGNOSTIC_LIBRARY_MISMATCH: {config}: {reference}') + else: + ensure_pcb_central_library( + job['pcb'], + job['lmc_file'], + evidence_dir=Path(root) / 'diagnostics', + emit=emit, + ) emit( stage='verify_mode', mode=('library_manager_native' if bool(job.get('native_lm_verify', False)) @@ -762,6 +775,29 @@ def execute_job(job, emit): error_reader=native_error, ) identity_verified = True + if job.get('diagnostic_only'): + # The diagnostic opens only to establish current native evidence. + # Do not refresh integration, save/commit Cert, or edit PRJ paths. + pi = doc = app = None + gc.collect() + from .failed_session import close_failed_document + diagnostic_close_attempted = True + closed = close_failed_document(job, emit=emit) + if closed.get('status') != 'CLOSED': + raise RuntimeError(f'DIAGNOSTIC_CLOSE_UNCONFIRMED: {closed}') + release_deadline = time.monotonic() + 30 + while True: + released = cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid, + expected_pcb=job['pcb'], baseline_pids=pcb_before, emit=emit) + if (released and released.get('safe_to_continue') + and not unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])): + break + if time.monotonic() >= release_deadline: + raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: 原生会话或数据库仍占用') + time.sleep(.2) + completed = True + emit(stage='diagnostic_complete', status='PASS') + return {'status': 'PASS', 'stdout': 'ICDB 启动检查通过;未执行集成或保存,已关闭工程。'} # Do not acquire MGCPCBAutomationLicensing from this 64-bit Python # worker. EE7.9 registers that automation class for the 32-bit host; # the old, working path acquires the token inside the SysWOW64 @@ -847,6 +883,10 @@ def execute_job(job, emit): pass if helper is not None and helper.poll() is None: emit(stage='save_helper_retained', helper_pid=helper.pid) + elif diagnostic_close_attempted: + # A timed-out close helper may still own an in-flight native Close. + # Observe release below, but never start a second close helper. + pass elif not completed and identity_verified: try: from .failed_session import close_failed_document diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 250c648..d1004ef 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -141,8 +141,8 @@ class MainWindow(QWidget): self.browse_button = QPushButton('选择'); self.browse_button.clicked.connect(self.choose_library) self.selected_batch_button = QPushButton('批量校验已选'); self.selected_batch_button.clicked.connect(self.verify_selected) self.batch_button = QPushButton('校验可见非 READY'); self.batch_button.clicked.connect(self.verify_all) - self.icdb_repair_button = QPushButton('ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) - self.icdb_repair_button.setToolTip('先诊断当前可见项目;只有确认 ICDB/会话故障才调用原生 Repair。正常项目不会弹恢复版本窗口。') + self.icdb_repair_button = QPushButton('非 READY ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) + self.icdb_repair_button.setToolTip('忽略 READY 项(包括已勾选项);先检查启动,仅本轮日志确认 ICDB 失联时才修复。') self.backup_restore_button = QPushButton('从备份恢复 CBB…') self.backup_restore_button.clicked.connect(self.restore_backup) self.stop_button = QPushButton('当前项完成后停止'); self.stop_button.clicked.connect(self.request_stop) @@ -399,22 +399,7 @@ class MainWindow(QWidget): return [r.name for r in records if r.status != 'READY' and r.name not in self.skipped_names] def _maintenance_names(self): - # Keep ICDB maintenance available as a repeatable diagnostic, including - # after a previous run promoted every row to READY. The worker first - # performs a read-only diagnosis and only invokes native Repair for a - # confirmed ICDB/session fault. - selected = self.selected_records() - if selected: - # A checked READY row remains outside the maintenance queue. When - # no non-READY row remains after a successful run, clearing the - # selection lets the user repeat the read-only diagnosis across the - # visible catalog. - records = [record for record in selected if record.status != 'READY'] - else: - visible = list(self.visible_records()) - non_ready = [record for record in visible if record.status != 'READY'] - records = non_ready or visible - return [record.name for record in records if record.name not in self.skipped_names] + return self._eligible_names(checked_only=bool(self.selected_records())) def _check_visible(self, state): self.cbb_table.blockSignals(True) @@ -631,8 +616,8 @@ class MainWindow(QWidget): f'{MANUAL_BACKUP_NOTICE}\nMentor 原生修复可能生成自身备份。\n\n当前中心库:{self.loaded_path}\n\n' f'将检查 {len(names)} 个可见非 READY CBB' f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '逐项核对工程身份、所有者和独占访问,再由官方工具检查/修复一次,成功后重新打开校验。' - '没有地址记录也会检查;多个数据库无法明确目标时停止,不猜测或删除地址文件。\n' + '先取得库锁并核对工程与占用,检查启动但不执行集成或保存。仅本轮原生日志确认 ICDB 失联才修复,再重新打开校验。' + '占用、历史版本提示或故障原因未确认时停止,不猜测故障或删除地址文件。READY 项始终忽略。\n' '仅本地固定磁盘:严格确认修复并重新独占检查后,可一次确认本次异常退出的读写访问提示。' '网络盘、UNC 或链接路径不自动确认该提示。\n' '共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;' @@ -644,9 +629,6 @@ class MainWindow(QWidget): if self.busy or not self.loaded_path or self._maintenance_recovery_view: return from .backup_recovery_dialog import BackupRecoveryDialog - # Backup recovery remains restricted to non-READY rows. ICDB - # maintenance itself is a repeatable diagnosis and may inspect READY - # rows without opening the PCB recovery flow. names = self._eligible_names(checked_only=bool(self.selected_records())) if not names: return @@ -891,7 +873,8 @@ class MainWindow(QWidget): from cbb_refresh_verify.icdb_maintenance import maintenance_counts counts = maintenance_counts(results) text = ('非 READY ICDB 检查已结束\n' - f'校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' + f'检查/校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' + f'启动检查通过、未修复 {counts["checked_without_repair"]}(不等同于 READY)\n' f'原生修复完成 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}\n' @@ -902,6 +885,7 @@ class MainWindow(QWidget): status_text = text if self._global_icdb_run: status_text = (f'非 READY 检查结束:通过 {passed},失败/受阻 {failed},未执行 {skipped}。\n' + f'启动检查通过、未修复 {counts["checked_without_repair"]};' f'原生修复 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}。') diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 420db6d..782daee 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -65,6 +65,7 @@ class VerifyRequest: auto_repair: bool = True maintenance: bool = False post_repair_proof: object | None = field(default=None, repr=False, compare=False) + diagnostic_only: bool = False @dataclass(frozen=True) @@ -757,7 +758,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run return finish(VerifyResult('BLOCKED', safe_to_continue=False, stderr=f'ICDB_SESSION_UNCONFIRMED: 无法确认数据库所有权:{error}')) - content_check_enabled = bool(str(request.libspec).strip() and str(request.dms_login_config).strip()) + content_check_enabled = not request.diagnostic_only and bool(str(request.libspec).strip() and str(request.dms_login_config).strip()) if content_check_enabled: try: executable = resolve_importer(request.mentor_bin) @@ -801,6 +802,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run return finish(VerifyResult('BLOCKED', stderr=ports_detail)) job = {'lmc_file': str(absolute_preserving_drive(request.lmc_file)), 'block_name': request.block_name, + 'diagnostic_only': request.diagnostic_only, 'supervisor_pid': os.getpid(), 'project': str(project), 'pcb': str(pcb), 'root': str(root), 'timeout': request.timeout_seconds, 'mentor_bin': str(request.mentor_bin or ''), @@ -1100,7 +1102,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run win32event.ReleaseMutex(mutex) mutex.Close() - if result.status == 'PASS': + if result.status == 'PASS' and not request.diagnostic_only: timing.begin('library_flag') bridge_records = [] def record_attempt(record): diff --git a/tests/test_maintenance_diagnosis.py b/tests/test_maintenance_diagnosis.py index 9fc5b5a..74595a2 100644 --- a/tests/test_maintenance_diagnosis.py +++ b/tests/test_maintenance_diagnosis.py @@ -1,68 +1,81 @@ -"""Regression tests for ICDB maintenance diagnosis before native Repair.""" - +"""Exercise the production diagnosis gate; do not spoof callback names.""" import json - -from test_maintenance_entry import case # reuse the isolated project fixture +from pathlib import Path +import pytest +from test_maintenance_entry import case, no_native_launch -def _trusted_fake(monkeypatch, runner, callback): - """Make run_verify's diagnostic closure look like the real worker boundary.""" - callback.__module__ = 'cbb_refresh_verify.verify_runner' - callback.__name__ = '_run_verify_serialized' - monkeypatch.setattr(runner, '_run_verify_serialized', callback) +def diagnostic_result(req, *, error='', database=None): + from cbb_refresh_verify.verify_runner import VerifyResult + root = req.evidence_dir/'probe'; root.mkdir(parents=True, exist_ok=True) + result = VerifyResult('FAIL' if error else 'PASS', error_category=error, + evidence_path=root/'result.json', safe_to_continue=not error) + result.evidence_path.write_text('{}', encoding='utf-8') + if error == 'ICDB_REPAIR_REQUIRED': + record = {'error_code': error, 'scope': 'project', 'session': 'test-session', + 'database_path': str(database)} + (root/'diagnostics').mkdir(exist_ok=True) + (root/'diagnostics/icdb-recovery.json').write_text(json.dumps(record), encoding='utf-8') + return result -def test_healthy_project_skips_native_repair(case, monkeypatch): - from cbb_refresh_verify import native_repair, verify_runner as runner - - request, _, _ = case - evidence = request.evidence_dir / 'diagnostic.json' - evidence.parent.mkdir(parents=True) - evidence.write_text('{}', encoding='utf-8') - calls = [] - - def worker(req, **kwargs): - calls.append(req.post_repair_proof) - return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=evidence) - - _trusted_fake(monkeypatch, runner, worker) - monkeypatch.setattr(native_repair, 'run_native_repair', - lambda *a, **k: (_ for _ in ()).throw(AssertionError('native Repair must be skipped'))) +def test_diagnosis_holds_library_lock_and_healthy_never_repair(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, native_repair, library_lock + request, project, address = case + address.unlink() + def worker(req, **kw): + key = library_lock.LibraryLock(req.lmc_file).key + assert key in library_lock._owners + assert library_lock._owners[key]['depth'] > 0 + assert req.diagnostic_only and not req.auto_repair and not req.maintenance + assert not req.native_lm_verify and not req.libspec + return diagnostic_result(req) + monkeypatch.setattr(runner, '_run_verify_serialized', worker) + monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('healthy repair')) result = runner.run_verify(request) - - assert result.status == 'PASS' - assert result.recovery_attempts == () - assert len(calls) == 1 and calls[0] is None - assert '未发现需要修复' in result.stdout + assert result.status == 'PASS' and result.recovery_attempts == () + assert result.library_flag == 'UNKNOWN' + assert not library_lock.has_blocker(request.lmc_file) -def test_confirmed_icdb_failure_enters_native_repair(case, monkeypatch): - from cbb_refresh_verify import native_repair, verify_runner as runner +@pytest.mark.parametrize('fault', ['missing', 'generic', 'assertion', 'wrong_database', 'outside_evidence']) +def test_unconfirmed_diagnosis_never_repairs(case, monkeypatch, fault): + from cbb_refresh_verify import verify_runner as runner, native_repair + request, project, address = case + address.unlink() + def worker(req, **kw): + if fault == 'assertion': raise AssertionError('diagnostic failure') + if fault == 'missing': return runner.VerifyResult('PASS') + if fault == 'generic': return diagnostic_result(req, error='DATABASE_SAFETY_STOP') + if fault == 'outside_evidence': + from dataclasses import replace + req = replace(req, evidence_dir=request.evidence_dir/'old-run') + return diagnostic_result(req, error='ICDB_REPAIR_REQUIRED', + database=project.parent/('wrong' if fault == 'wrong_database' else 'database')) + monkeypatch.setattr(runner, '_run_verify_serialized', worker) + monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('unconfirmed repair')) + result = runner.run_verify(request) + assert result.status != 'PASS' and result.recovery_attempts == () + + +def test_diagnostic_fault_repairs_once_then_full_verification(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, native_repair from test_native_repair import complete_native_success - - request, _, address = case - evidence = request.evidence_dir / 'diagnostic.json' - evidence.parent.mkdir(parents=True) - evidence.write_text(json.dumps({'error_category': 'ICDB_REPAIR_REQUIRED'}), encoding='utf-8') + request, project, address = case + address.unlink() calls = [] - - def worker(req, **kwargs): - calls.append(req.post_repair_proof) - if req.post_repair_proof is None: - return runner.VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', - evidence_path=evidence, safe_to_continue=False) - return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=evidence) - - def repair(target, *args, **kwargs): - address.unlink() + def worker(req, **kw): + calls.append('diagnose' if req.diagnostic_only else 'verify') + if req.diagnostic_only: + return diagnostic_result(req, error='ICDB_REPAIR_REQUIRED', database=project.parent/'database') + assert req.post_repair_proof is not None and not req.auto_repair + return runner.VerifyResult('PASS', library_flag='GREEN') + def repair(target, *a, **kw): + calls.append('repair') return complete_native_success(target) - - _trusted_fake(monkeypatch, runner, worker) + monkeypatch.setattr(runner, '_run_verify_serialized', worker) monkeypatch.setattr(native_repair, 'run_native_repair', repair) - monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a, **k: - {'safe_to_continue': True, 'native_exited': True, 'addresses': []}) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) result = runner.run_verify(request) - - assert result.status == 'PASS' - assert len(calls) == 2 and calls[0] is None and calls[1] is not None - assert result.recovery_attempts[0]['native_status'] == 'REPAIRED' + assert calls == ['diagnose', 'repair', 'verify'] + assert result.status == 'PASS' and len(result.recovery_attempts) == 1 diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index 3f5a1ae..6f3f385 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -19,6 +19,7 @@ def no_native_launch(monkeypatch): monkeypatch.setattr(win32com.client, 'Dispatch', forbidden) monkeypatch.setattr(win32com.client, 'DispatchEx', forbidden) monkeypatch.setattr(desktop, 'DesktopWorker', forbidden) + monkeypatch.setattr('cbb_refresh_verify.verify_runner.runtime_fingerprint', lambda *_: {}) @pytest.fixture @@ -46,6 +47,23 @@ def case(tmp_path, monkeypatch): if owner: owner['handle'].Close() +def patch_verification_after_fault(monkeypatch, runner, verify): + """Inject native startup evidence, then exercise the production repair gate.""" + def worker(req, **kwargs): + if not req.diagnostic_only: + return verify(req, **kwargs) + project, pcb = runner.preflight(req) + root = req.evidence_dir/'native-startup'; root.mkdir(parents=True, exist_ok=True) + (root/'diagnostics').mkdir(exist_ok=True) + (root/'diagnostics/icdb-recovery.json').write_text(json.dumps({ + 'error_code': 'ICDB_REPAIR_REQUIRED', 'scope': 'project', 'session': 'fixture', + 'database_path': str(project.parent/'database')}), encoding='utf-8') + evidence = root/'result.json'; evidence.write_text('{}', encoding='utf-8') + return runner.VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', + safe_to_continue=False, evidence_path=evidence) + monkeypatch.setattr(runner, '_run_verify_serialized', worker) + + @pytest.mark.parametrize('pcb_config', ['standard', 'legacy_template']) @pytest.mark.parametrize('advertisement', ['present', 'absent']) def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch, pcb_config, advertisement): @@ -77,7 +95,7 @@ def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fa assert not req.maintenance and not req.auto_repair assert req.lmc_file == request.lmc_file and req.block_name == request.block_name return runner.VerifyResult('PASS', library_flag='GREEN') - monkeypatch.setattr(runner, '_run_verify_serialized', verify) + patch_verification_after_fault(monkeypatch, runner, verify) result = runner.run_verify(request) assert calls == ['repair', 'verify'] and result.status == 'PASS' assert result.recovery_attempts[0]['validation_passed'] is True @@ -235,7 +253,7 @@ def test_nonrepair_outcomes_never_verify_or_count_success(case, monkeypatch, sta monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: { 'status': status, 'release': {'safe_to_continue': False}, 'detail': 'native result'}) monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a: {'safe_to_continue': False}) - monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('unsafe or nonrepair outcome reopened')) + patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('unsafe or nonrepair outcome reopened')) result = runner.run_verify(request) assert result.status != 'PASS' and not result.safe_to_continue assert result.recovery_attempts[0]['status'] == status @@ -246,7 +264,7 @@ def test_native_success_cannot_bypass_remaining_address(case, monkeypatch): from test_native_repair import complete_native_success request, _, _ = case monkeypatch.setattr(native_repair, 'run_native_repair', lambda target, *a, **k: complete_native_success(target)) - monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('retained address reopened')) + patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('retained address reopened')) # Native release queries remain mocked, actual address/exclusive checks stay real. monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: []) monkeypatch.setattr('cbb_refresh_verify.mentor_adapter._process_details', lambda name: []) @@ -340,7 +358,7 @@ def test_physical_release_alone_does_not_authorize_queue(case, monkeypatch, stat return result monkeypatch.setattr(native_repair, 'run_native_repair', repair) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) - monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('nonrepair outcome reopened')) + patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('nonrepair outcome reopened')) result = runner.run_verify(request) assert result.status != 'PASS' assert result.safe_to_continue is expected_safe @@ -357,7 +375,7 @@ def test_successful_icdb_repair_then_version_choice_reports_partial_completion(c return complete_native_success(target) monkeypatch.setattr(native_repair, 'run_native_repair', repair) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) - monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('FAIL', + patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: runner.VerifyResult('FAIL', error_category='PCB_VERSION_CHOICE_REQUIRED', stderr='PCB版本待选:最后用户保存/自动保存 2026/9/24 23:06', safe_to_continue=False, evidence_path=request.evidence_dir/'verify/result.json')) result = runner.run_verify(request) @@ -380,7 +398,7 @@ def test_user_chosen_pcb_history_is_not_reported_as_lossless_repair(case, monkey (root/'pcb-version-restoration.json').write_text(json.dumps({'project': str(project), 'choice': 'last_user_saved', 'date': '2026/9/24 23:06', 'confirm_sent': True, 'historical_restore': True})) return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=root/'result.json') - monkeypatch.setattr(runner, '_run_verify_serialized', verify) + patch_verification_after_fault(monkeypatch, runner, verify) result = runner.run_verify(request) assert result.status == 'PASS' assert result.recovery_attempts[0]['status'] == 'RESTORED_REVIEW_REQUIRED' diff --git a/tests/test_maintenance_review.py b/tests/test_maintenance_review.py new file mode 100644 index 0000000..0280833 --- /dev/null +++ b/tests/test_maintenance_review.py @@ -0,0 +1,113 @@ +from pathlib import Path +import pytest +from PySide6.QtCore import Qt + + +def test_ready_rows_never_enter_maintenance_even_when_unchecked(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window, MIXED + window = load_window(qtbot, tmp_path, monkeypatch, records=(MIXED[0],)) + assert window._maintenance_names() == [] + assert not window.icdb_repair_button.isEnabled() + assert not window.backup_restore_button.isEnabled() + window.select_visible_button.click() + assert window._maintenance_names() == [] + + +def test_selected_ready_rows_are_ignored_in_mixed_maintenance(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch) + window.select_visible_button.click() + assert window._maintenance_names() == ['Beta', 'Gamma', 'Delta'] + window._apply_refreshed_records((('Beta', 'Gamma'), {'Beta': 'READY', 'Gamma': 'READY'}, {}, '')) + assert window._maintenance_names() == ['Delta'] + assert window.icdb_repair_button.isEnabled() + + +def test_diagnostic_worker_closes_without_integration_save_or_config_edit(tmp_path, monkeypatch): + from test_verify_worker_stability import exercise_worker + from cbb_refresh_verify import mentor_adapter as adapter + job, events, execute = diagnostic_worker(tmp_path, monkeypatch) + job['diagnostic_only'] = True + def forbidden(*args, **kwargs): + pytest.fail('diagnosis attempted integration/save/config rewrite') + monkeypatch.setattr(adapter, 'ensure_pcb_central_library', forbidden) + monkeypatch.setattr('cbb_refresh_verify.integration.refresh_integration', forbidden) + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.launch_save_helper', forbidden) + closes = [] + monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document', + lambda job, **kw: closes.append(job) or {'status': 'CLOSED'}) + result = execute() + assert result['status'] == 'PASS' + assert len(closes) == 1 + assert not any(e['stage'] in ('save_start', 'saved_and_closed') for e in events) + + +def test_diagnostic_close_failure_never_returns_healthy(tmp_path, monkeypatch): + from test_verify_worker_stability import exercise_worker + job, events, execute = diagnostic_worker(tmp_path, monkeypatch) + job['diagnostic_only'] = True + closes = [] + monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document', + lambda *a, **kw: closes.append(a) or {'status': 'RETAINED'}) + with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'): + execute() + assert len(closes) == 1 + + +@pytest.mark.parametrize('prefix', ['KEY CentralLibrary ', ' KEY CentralLibrary ', 'KEY\tCentralLibrary\t']) +def test_diagnosis_rejects_foreign_library_reference_before_native_launch(tmp_path, monkeypatch, prefix): + from test_verify_worker_stability import exercise_worker + from cbb_refresh_verify import mentor_adapter as adapter + job, events, execute = diagnostic_worker(tmp_path, monkeypatch) + job['diagnostic_only'] = True + project = Path(job['project']) + project.write_text(prefix + '"foreign.lmc"\n') + monkeypatch.setattr(adapter, 'launch_pcb_process', lambda *a: pytest.fail('foreign library opened')) + with pytest.raises(ValueError, match='DIAGNOSTIC_LIBRARY_MISMATCH'): + execute() + +def diagnostic_worker(tmp_path, monkeypatch): + from test_verify_worker_stability import exercise_worker + job, events, execute = exercise_worker(tmp_path, monkeypatch) + (Path(job['pcb']).parent/'pcb.prj').write_text( + 'KEY CentralLibrary "' + job['lmc_file'] + '"\n', encoding='utf-8') + return job, events, execute + + +def test_diagnostic_runner_skips_importer_and_ready_verification(tmp_path, monkeypatch): + import json + from test_verify_runner import fixture, request, PORTS_OK + from cbb_refresh_verify.verify_runner import run_verify + fixture(tmp_path) + captured = {} + class FinishedWorker: + desktop = None + returncode = 0 + def __init__(self, command, name, **kwargs): + job = Path(command[-1]) + captured.update(json.loads(job.read_text('utf-8'))) + job.with_name('outcome.json').write_text('{"status":"PASS"}', encoding='utf-8') + def poll(self): return 0 + def close(self): pass + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', FinishedWorker) + def forbidden(*a, **kw): pytest.fail('diagnosis must not run importer or query READY') + result = run_verify(request(tmp_path, diagnostic_only=True, auto_repair=False, + libspec='configured', dms_login_config='configured'), port_check=PORTS_OK, + flag_reader=forbidden, process_runner=forbidden) + assert result.status == 'PASS' and result.library_flag == 'UNKNOWN' + assert captured['diagnostic_only'] is True + + +def test_diagnostic_waits_for_normal_exit_after_close_returns(tmp_path, monkeypatch): + from cbb_refresh_verify import mentor_adapter as adapter + job, events, execute = diagnostic_worker(tmp_path, monkeypatch) + job['diagnostic_only'] = True + observations = [] + def release(*a, **kw): + observations.append(True) + return {'safe_to_continue': len(observations) > 1} + monkeypatch.setattr(adapter, 'cleanup_verify_processes', release) + monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document', + lambda *a, **kw: {'status': 'CLOSED'}) + assert execute()['status'] == 'PASS' + assert len(observations) >= 2 -- 2.39.5 From cb1326688b82dd1a3a6424ec11af92028e6622a2 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: Wed, 4 Nov 2026 10:37:10 +0800 Subject: [PATCH 47/59] docs: record reviewed ICDB candidate validation --- docs/releases/2026-11-04-icdb-review-fixes.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/releases/2026-11-04-icdb-review-fixes.md b/docs/releases/2026-11-04-icdb-review-fixes.md index d6a3470..1bf8ee7 100644 --- a/docs/releases/2026-11-04-icdb-review-fixes.md +++ b/docs/releases/2026-11-04-icdb-review-fixes.md @@ -15,10 +15,12 @@ ## 验证范围 -- 完整回归:`python -X utf8 -m pytest -q --tb=short`,1,147 passed,377.07 秒;仅既有 pywinauto STA 警告。 -- 随后补充库路径格式和进程边界覆盖,相关专项 97 passed;最后增加关闭后延迟释放覆盖,最终诊断/审查专项 16 passed。 +- 最终从 `ef652f3` 导出的固定源码运行完整回归:`python -X utf8 -m pytest -q --tb=short`,**1,151 passed,375.48 秒**;仅既有 pywinauto STA 警告。 +- 此前的专项覆盖包括库锁、证据缺失/其他工程/历史轮次、无集成保存、关闭失败、路径格式、READY 选择范围和延迟退出。最终源码副本与构建输入哈希已核对。 - `compileall` 与 `git diff --check` 通过。 - 独立审查发现并修正重复关闭和路径格式遗漏;冻结构建和启动结果另存于 `diagnostics/icdb-review-20261104/`。 +- 冻结启动/退出检查返回 0;EXE 内 5 个变更模块与固定源码编译结果一致;ZIP CRC 校验通过。 +- 最终候选:`dist/issue2-icdb-reviewed-ef652f3.zip`,50,871,172 字节;SHA-256:`f4565f1696caf620522d621bd8555817a6d7d5d6ed4193cf87f2b53758a5d8c7`。构建和测试证据对应代码提交 `ef652f3ada0b9b8422e49f2ebc5b18305204d8ab`。 ## 保留的限制 -- 2.39.5 From e14a864b37b31cf5f267a395fdfde5077ad4fa31 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: Wed, 4 Nov 2026 12:26:09 +0800 Subject: [PATCH 48/59] fix: inspect native ICDB occupancy before bounded repair --- .../2026-11-04-native-occupancy-candidate.md | 52 +++++ src/cbb_refresh_verify/application.py | 4 + src/cbb_refresh_verify/icdb_maintenance.py | 2 +- src/cbb_refresh_verify/maintenance_entry.py | 126 +++++++------ src/cbb_refresh_verify/native_occupancy.py | 61 ++++++ src/cbb_refresh_verify/native_repair.py | 21 ++- src/cbb_refresh_verify/native_repair_ui.py | 57 +++++- src/cbb_refresh_verify/owner_probe.py | 59 ++++++ src/cbb_refresh_verify/post_repair_consent.py | 8 +- src/cbb_refresh_verify/scoped_manifest.py | 4 +- src/cbb_refresh_verify/ui/main_window.py | 20 +- src/cbb_refresh_verify/verify_runner.py | 17 +- tests/test_maintenance_diagnosis.py | 102 +++++----- tests/test_maintenance_entry.py | 27 ++- tests/test_native_export_guard.py | 9 +- tests/test_native_occupancy.py | 178 ++++++++++++++++++ tests/test_owner_probe.py | 23 +++ 17 files changed, 617 insertions(+), 153 deletions(-) create mode 100644 docs/releases/2026-11-04-native-occupancy-candidate.md create mode 100644 src/cbb_refresh_verify/native_occupancy.py create mode 100644 src/cbb_refresh_verify/owner_probe.py create mode 100644 tests/test_native_occupancy.py create mode 100644 tests/test_owner_probe.py diff --git a/docs/releases/2026-11-04-native-occupancy-candidate.md b/docs/releases/2026-11-04-native-occupancy-candidate.md new file mode 100644 index 0000000..ce930ff --- /dev/null +++ b/docs/releases/2026-11-04-native-occupancy-candidate.md @@ -0,0 +1,52 @@ +# 原生占用检查候选:完整修复验收失败 + +本记录更新此前 `2026-11-04-icdb-review-fixes.md` 的维护入口说明。工单 #2 保持未解决;本候选不合并、不发布正式版、不关闭工单。 + +## 本轮改动 + +- 维护入口先调用原生 iCDB Project Backup 的工程加载/占用检查,不先打开 PCB。旧 `sAddress.adr` 只是待核实证据,不再单凭它阻断检查,也不删除或改写该文件。 +- 仅本轮 Output 明确绑定目标工程、目标数据库,确认未占用且要求 Repair,原生检查正常退出,并再次通过工程身份和占用检查,才调用一次 Repair。其他工程、共享库、未知错误或未释放会话均阻止后续操作。 +- WMI 占用查询在独立只读辅助进程执行,最多 10 秒;超时是未知,不是空闲。文件扫描在后台运行并支持取消。原生准备阶段预算 60 秒,另加原生操作超时及 5 秒监督余量;取消后再次核对,避免迟到启动。 +- 停止请求传入维护流程,原生写入不强杀;释放未确认时停止后续批次。仅检查结果使用 `CHECKED`,不计入修复成功。READY 即使已勾选也不进入维护。 +- 精确的 verified/once-verified Reusable Block BackAnnotate 拒绝归类为 `CBB_REIMPORT_REQUIRED`,不再被后续释放告警掩盖。该分类不会执行重导入,也不改变 `safe_to_continue`;原生错误及释放错误都保留在报告中。 +- 历史版本选择继续通过应用弹窗;只有完整校验及权威 READY 通过才允许成功。历史恢复仍标记版本待核对。 + +## 隔离环境实测 + +证据根目录:`diagnostics/native-occupancy-20261104/`(含本机路径和原生日志,不纳入 Git)。测试通过实际应用维护按钮和版本选择弹窗完成,没有以手工分阶段调用替代验收。 + +一致副本包含 38,954 个文件、1,220,129,120 字节。复制期间源文件集合/大小/修改时间一致,253 个主要文件 SHA-256 核对通过;追加目标 PRJ/PCB 后,最终再次核对原库的 255 个已记录文件,全部未变化。原库未执行写操作。 + +最终尝试 `source-ui-fourth`,目标 `1GPHY_8212`: + +1. 原生检查明确返回未占用,并指出当前数据库不一致、要求 Repair。 +2. 原生 Repair 创建自身备份,完成数据库检查、整理和关闭,返回 `Project should now be fixed.`,并确认退出释放。 +3. 应用弹窗选择用户已授权的最后手动保存版本(2026/9/24 15:15),重新打开同一工程。 +4. SynchCES、LoadCES 通过;BackAnnotation 为 Required,实际调用被 Mentor 拒绝:`Back Annotation is not allowed on verified (or once verified) Reusable Blocks.` +5. 未完成保存与 READY 校验。最终 **FAIL / UNKNOWN / REPAIR_VALIDATION_FAILED**,`validation_passed=false`、`safe_to_continue=false`。原生 Repair 完成不等于整体修复成功。 +6. 失败关闭助手记录 Close/Quit 成功,但最终释放检查仍看到 PID 21968,因此保留检查点并停止批次。后续只读复查该原生进程已消失;这不追溯修改原失败结果。测试应用关闭结果对话框后正常退出。 + +实测界面响应记录:614 次 WM_NULL 检查,200 毫秒超时 0 次,最大约 78 毫秒。这只证明本次运行的界面响应,不保证任意网络故障下所有系统调用可立即取消。 + +关键证据: + +- `source-ui-fourth/acceptance.json`:`accepted=false`。 +- `source-ui-fourth/evidence/maintenance-43479c21aa59/result.json`:完整维护结果。 +- `source-ui-fourth/evidence/maintenance-43479c21aa59/native-repair/repair-result.json`:原生 Repair 证据。 +- `source-ui-fourth/evidence/verify-99a675e0399c/`:重新打开、集成拒绝、正常关闭及释放未确认记录。 +- `source-ui-fourth/ui-responsiveness.json`、`version-choice.png`、`finished.png`。 +- `source-final-check.json`:原库 255 个哈希均保持不变。 + +## 代码与冻结候选验证 + +- `python -X utf8 -m pytest -q --tb=short`:**1,176 passed**,302.56 秒;1 条既有 pywinauto STA 警告。 +- `compileall`、`git diff --check`、独立只读代码审查通过。 +- PyInstaller 构建通过;冻结程序创建主窗口及正常退出通过,新 `--owner-probe` 在冻结程序中返回有效结果。 +- EXE 内 11 个变更模块与当前源码编译结果一致。验证脚本及结果在证据根目录的 `check_candidate.py`、`frozen-result.json`。 +- 本地候选目录:`dist/issue2-native-occupancy-candidate/`。冻结包完整故障修复链路未执行,不能称为已验收的修复包。 + +## 尚未解决 + +已验证或曾验证的 CBB 注册状态限制仍阻止 BackAnnotate。不能跳过 Required 操作、强行设置 READY 或把移除/重导入偷偷加入 ICDB Repair。此前隔离试验的独立重导入路径见 `2026-11-03-field-feedback.md`,其行为涉及库条目更新,不能据此宣称本次无损修复通过。 + +本轮没有完成 Windows 10/11 双系统验收;冻结包的完整现场故障链路仍需验证。程序测试和启动冒烟均不能代替上述失败的实际验收。 diff --git a/src/cbb_refresh_verify/application.py b/src/cbb_refresh_verify/application.py index 0208624..c5f8aff 100644 --- a/src/cbb_refresh_verify/application.py +++ b/src/cbb_refresh_verify/application.py @@ -50,6 +50,7 @@ def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser(prog="CbbRefreshVerify") parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--repair-worker', type=Path, help=argparse.SUPPRESS) + parser.add_argument('--owner-probe', type=Path, help=argparse.SUPPRESS) parser.add_argument('--management-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS) parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS) @@ -60,6 +61,9 @@ def main(argv: Sequence[str] | None = None) -> int: parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS) parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS); parser.add_argument("--data-root", type=Path, help=argparse.SUPPRESS) args = parser.parse_args(list(argv) if argv is not None else None) + if args.owner_probe: + from .owner_probe import owner_probe_main + return owner_probe_main(args.owner_probe) if args.management_worker: from .native_management import management_worker_main return management_worker_main(args.management_worker) diff --git a/src/cbb_refresh_verify/icdb_maintenance.py b/src/cbb_refresh_verify/icdb_maintenance.py index 417640b..b701b6c 100644 --- a/src/cbb_refresh_verify/icdb_maintenance.py +++ b/src/cbb_refresh_verify/icdb_maintenance.py @@ -16,7 +16,7 @@ def maintenance_counts(results): and attempt.get('native_status') == 'REPAIRED' and attempt.get('validation_passed') is False for attempt in attempts) return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results), - 'checked_without_repair': sum(r.status == 'PASS' and not r.recovery_attempts for r in results), + 'checked_without_repair': sum(r.status == 'CHECKED' for r in results), 'skipped': sum(r.status == 'SKIPPED' for r in results), 'repaired': repaired, 'review_required': review, 'version_choice_required': version_choice, diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 2c38d90..ac3626d 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -13,7 +13,7 @@ from uuid import uuid4 def assert_native_idle(): - import win32com.client + 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 @@ -23,19 +23,15 @@ def assert_native_idle(): 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)) - service = win32com.client.GetObject('winmgmts:') - query = 'SELECT ProcessId, Name, CommandLine FROM Win32_Process WHERE ' + ' OR '.join( - f"Name='{name}'" for name in names) owners = [] - for item in service.ExecQuery(query): - if int(item.ProcessId) == os.getpid(): + for item in query_owners(names): + if item['pid'] == os.getpid(): continue - name, command = str(item.Name).casefold(), str(item.CommandLine or '').casefold() + 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({'pid': int(item.ProcessId), 'name': str(item.Name), - 'command': str(item.CommandLine or '')}) + owners.append(item) if owners: raise ValueError(f'MAINTENANCE_OWNER_ACTIVE: 请正常关闭原生程序/操作助手:{owners}') @@ -47,11 +43,11 @@ def address_snapshot(paths): for path in paths} -def _scoped_addresses(request): +def _scoped_addresses(request, *, check=None): from .scoped_manifest import scan_tree root = Path(request.lmc_file).parent addresses = [] - for path in scan_tree(root): + for path in scan_tree(root, check=check): if path.name.casefold() != 'saddress.adr': continue parts = tuple(part.casefold() for part in path.relative_to(root).parts) @@ -85,7 +81,7 @@ def maintenance_project_files(project, pcb): return (project,) -def select_maintenance_target(request): +def select_maintenance_target(request, *, check=None): from .verify_runner import preflight from .backup_recovery import _project_text from .native_repair import RepairTarget @@ -93,7 +89,7 @@ def select_maintenance_target(request): if request.maintenance is not True: raise ValueError('MAINTENANCE_INTENT_REQUIRED') project, pcb = preflight(request) - scan_tree(project.parent) + scan_tree(project.parent, check=check) candidates = [] for candidate in maintenance_project_files(project, pcb): if not candidate.is_file(): @@ -113,7 +109,7 @@ def select_maintenance_target(request): 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) + addresses = _scoped_addresses(request, check=check) if not addresses: # A crashed project may retain its internal open state after the server # advertisement disappears. Explicit maintenance must still inspect it. @@ -133,11 +129,15 @@ def select_maintenance_target(request): @contextmanager -def maintenance_target_guard(request, expected=None): +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 - target, project, pcb, addresses = select_maintenance_target(request) + def check(): + if stop is not None and stop.is_set(): + raise ValueError('MAINTENANCE_CANCELLED: 已取消占用检查') + check() + target, project, pcb, addresses = select_maintenance_target(request, check=check) 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()), @@ -147,16 +147,18 @@ def maintenance_target_guard(request, expected=None): if expected is not None and context != expected: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 目标或地址记录在检查后变化') assert_native_idle() - entries = scan_tree(Path(request.lmc_file).parent) - files = [path for path in entries if path.is_file() and ( - path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))] + check() + entries = scan_tree(Path(request.lmc_file).parent, check=check) + files = [path for path in entries if ( + path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')) and path.is_file()] with ExitStack() as stack: for path in 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_addresses = _scoped_addresses(request) + current_addresses = _scoped_addresses(request, check=check) if address_snapshot(current_addresses) != baseline: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 地址记录在检查期间变化') yield {'target': target, 'context': context, 'project': project, 'pcb': pcb} @@ -176,7 +178,7 @@ def maintenance_needed(request): return bool(unreleased_addresses(request.lmc_file, project, pcb)) -def run_existing_address_maintenance(request, *, verify, progress=None): +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) @@ -199,45 +201,51 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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), safe_to_release=lambda: owner_safe) + 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 - emit('icdb_diagnosis_start', detail='已取得库锁,检查原生启动;不执行集成或保存') - diagnostic_request = replace(request, maintenance=False, auto_repair=False, - diagnostic_only=True, post_repair_proof=None, native_lm_verify=False, - interactive_input=False, libspec='', dms_login_config='', evidence_dir=root/'diagnosis') - diagnosis = verify(diagnostic_request) - record['preflight_diagnosis'] = asdict(diagnosis) - evidence = diagnosis.evidence_path - if (evidence is None or not Path(evidence).is_file() - or not Path(evidence).resolve().is_relative_to((root/'diagnosis').resolve())): - raise ValueError('ICDB_DIAGNOSIS_UNCONFIRMED: 缺少本轮诊断证据,未调用 Repair') - owner_safe = diagnosis.safe_to_continue - if diagnosis.status == 'PASS' and owner_safe: - emit('icdb_diagnosis_complete', detail='本轮启动未发现 ICDB 故障,未调用 Repair') - return save(replace(diagnosis, library_flag='UNKNOWN', library_flag_detail='', - stdout='ICDB 启动检查通过;未执行集成、保存或原生 Repair。', recovery_attempts=())) - if diagnosis.error_category != 'ICDB_REPAIR_REQUIRED' or diagnosis.status in ('BLOCKED', 'TIMEOUT'): - return save(replace(diagnosis, status='BLOCKED' if diagnosis.status == 'PASS' else diagnosis.status, - stderr=(diagnosis.stderr + '\n未确认 ICDB 故障,未调用原生 Repair。').strip(), - recovery_attempts=())) - # Only this run's structured native lost-session diagnosis may - # authorize Repair, and it must identify this exact database. - fault = json.loads((Path(evidence).parent/'diagnostics/icdb-recovery.json').read_text('utf-8')) - if (fault.get('error_code') != 'ICDB_REPAIR_REQUIRED' or fault.get('scope') != 'project' - or not fault.get('session') or not fault.get('database_path') - or Path(fault['database_path']).resolve() != target.database.resolve()): - raise ValueError('ICDB_DIAGNOSIS_UNCONFIRMED: 故障证据未绑定当前数据库') - record['confirmed_fault'] = fault - # A diagnostic worker may have failed to release its native owner. - # Recheck every owner and the exact target before dispatch. - with maintenance_target_guard(request, context): pass - owner_safe = False - emit('icdb_diagnosis_confirmed', detail='本轮原生日志已确认目标数据库失联,进入 Repair') + 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: @@ -246,10 +254,10 @@ def run_existing_address_maintenance(request, *, verify, progress=None): 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): pass + 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=progress, - maintenance_context=context) + 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) @@ -262,6 +270,10 @@ def run_existing_address_maintenance(request, *, verify, progress=None): # 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 diff --git a/src/cbb_refresh_verify/native_occupancy.py b/src/cbb_refresh_verify/native_occupancy.py new file mode 100644 index 0000000..aa3d3c9 --- /dev/null +++ b/src/cbb_refresh_verify/native_occupancy.py @@ -0,0 +1,61 @@ +"""Interpret the current Project Backup loading session, before any Repair.""" +import ntpath +import re + + +def _same(left, right): + return ntpath.normcase(ntpath.normpath(str(left))) == ntpath.normcase(ntpath.normpath(str(right))) + + +def classify_inspection(project, database, output): + result = {'status': 'UNCONFIRMED', 'repair_required': False} + if not isinstance(output, str): + return result + loaded = re.findall(r'Loading project file \[([^\]\r\n]+)\]\.', output) + if len(loaded) != 1 or not _same(loaded[0], project): + return result + checking = 'Checking if project is in use.' + if checking not in output: + return result + current = output.rsplit(checking, 1)[1] + if re.search(r'\bProject is in use\b', current, re.I): + return {**result, 'status': 'IN_USE'} + if ("Project is not in use. Full iCDB Project Backup's functionality is available." not in current + or 'Backup information loaded correctly.' not in current): + return result + # This exact native error was captured from the isolated EE7.9.5 project. + # A generic 'try Repair' or an old error before the latest check is not proof. + inconsistency_pattern = ( + r"Project database \[([^\]\r\n]+)\] is inconsistent\. It has been manually copied while the iCDB Server was running\. " + r"Please use iCDB Project Backup 'Repair project' functionality to fix this issue\.") + inconsistent = re.findall(inconsistency_pattern, current) + from .icdb_recovery import _session_losses + losses = list(_session_losses(current)) + if (len(inconsistent) > 1 or any(not _same(path, database) for path in inconsistent) + or len(losses) > 1 or any(not lost.get('session') or lost.get('scope') != 'project' + or not _same(lost.get('database_path', ''), database) for lost in losses)): + return result + required = bool(inconsistent or losses) + remaining = re.sub(inconsistency_pattern, '', current) + if required: + remaining = remaining.replace('Please try to repair the project!', '') + if re.search(r'\berror\b|try to repair|inconsistent|corrupt', remaining, re.I): + return result + return {'status': 'NOT_IN_USE', 'repair_required': bool(required)} + + +def confirmed_inspection(target, record): + """Only an exact, exited, inspection-only native process grants a verdict.""" + if (not isinstance(record, dict) or record.get('inspection_only') is not True + or record.get('repair_requested') is not False + or type(record.get('native_pid')) is not int or record['native_pid'] <= 0 + or record.get('native_exit_code') != 0 + or record.get('release', {}).get('native_exited') is not True + or not _same(record.get('project', ''), target.project) + or not _same(record.get('database', ''), target.database) + or record.get('unknown_dialogs')): + return False + expected = classify_inspection(target.project, target.database, record.get('output')) + return (expected['status'] in ('NOT_IN_USE', 'IN_USE') + and record.get('status') == expected['status'] + and record.get('repair_required') is expected['repair_required']) diff --git a/src/cbb_refresh_verify/native_repair.py b/src/cbb_refresh_verify/native_repair.py index 3bf3723..10b76e2 100644 --- a/src/cbb_refresh_verify/native_repair.py +++ b/src/cbb_refresh_verify/native_repair.py @@ -253,8 +253,10 @@ def repair_release_state(request, target, native_result): def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None, - maintenance_context=None): + maintenance_context=None, inspection_only=False, stop=None): """Supervise the GUI on its own desktop; a repair timeout never kills it.""" + if stop is not None and stop.is_set(): + return {'status': 'CANCELLED', 'detail': '原生操作启动前已取消', 'release': {'safe_to_continue': True}} from .desktop_worker import DesktopWorker root = Path(root) root.mkdir(parents=True, exist_ok=True) @@ -264,16 +266,23 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr job_path.write_text(json.dumps({'project': str(target.project), 'database': str(target.database), 'root': str(root), 'mentor_bin': str(mentor_bin or ''), 'timeout': timeout_seconds, 'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop, - 'maintenance_context': maintenance_context}, + 'maintenance_context': maintenance_context, 'inspection_only': inspection_only}, ensure_ascii=False), encoding='utf-8') command = ([sys.executable] if getattr(sys, 'frozen', False) else [sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)] worker = DesktopWorker(command, desktop) worker.bind_cancellation(cancel) - deadline = time.monotonic()+max(1, float(timeout_seconds))+5 + # Worker admission includes filesystem/ownership checks before the native + # timeout begins. Bound that preparation separately; never extend on polls. + deadline = time.monotonic()+60+max(1, float(timeout_seconds))+5 event_count = 0 + cancelled = False try: while worker.poll() is None and time.monotonic()= deadline + verdict = classify_inspection(project, database, current) + if cancelled: + result.update(status='CANCELLED', repair_required=False) + if not closing and main and main['enabled'] and (cancelled or expired or verdict['status'] != 'UNCONFIRMED'): + if not cancelled and not expired: result.update(verdict) + emit(stage='native_occupancy', **result) + win32gui.PostMessage(main['hwnd'], win32con.WM_CLOSE, 0, 0) + closing = True + time.sleep(.2) + exited = process.poll() is not None + result['native_exit_code'] = process.poll() + result['release'] = {'native_exited': exited, + 'addresses': [str(database/'cdbsvr/sAddress.adr')] if (database/'cdbsvr/sAddress.adr').exists() else [], + 'safe_to_continue': exited and result['status'] == 'NOT_IN_USE'} + if not exited: + result.update(status='TIMEOUT_UNRELEASED', repair_required=False) + return result + + def repair_worker_main(job_path): import win32con, win32gui from .native_repair import RepairTarget, check_repair_access @@ -208,7 +254,7 @@ def repair_worker_main(job_path): if home is None: raise ValueError('NATIVE_REPAIR_UNAVAILABLE: 未找到 Mentor 安装目录') executable = home/'iCDB/win32/bin/iCDBProjectBackup.exe' - command_id = _repair_command(executable) + command_id = None if job.get('inspection_only') else _repair_command(executable) from .mentor_adapter import pcb_processes, _process_details if pcb_processes() or _process_details('LibraryManager.exe'): raise ValueError('REPAIR_DATABASE_IN_USE') @@ -218,6 +264,7 @@ def repair_worker_main(job_path): from .verify_runner import VerifyRequest maintenance_request = VerifyRequest(Path(maintenance_context['library']), maintenance_context['block'], root, maintenance=True) + emit(stage='native_preflight', detail='正在核对本次原生工具的目标及占用') with maintenance_target_guard(maintenance_request, maintenance_context) as checked: if (checked['target'].project.resolve() != project.resolve() or checked['target'].database.resolve() != database.resolve()): @@ -229,11 +276,19 @@ def repair_worker_main(job_path): raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') if maintenance_context is not None: with maintenance_target_guard(maintenance_request, maintenance_context): pass + if Path(job['cancel_signal']).exists(): + raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt') started_ns = time.time_ns() + if Path(job['cancel_signal']).exists(): + raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') process = subprocess.Popen([str(executable), '-p', str(project)], cwd=str(project.parent)) result['native_pid'] = process.pid emit(stage='native_started', pid=process.pid, project=str(project)) + if job.get('inspection_only'): + result = _inspect_project(job, process, emit) + (root/'repair-result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8') + return 0 if result['status'] in ('NOT_IN_USE', 'IN_USE') else 1 deadline = time.monotonic()+max(1, float(job['timeout'])) invoked = confirmed = closing = False evidence = None diff --git a/src/cbb_refresh_verify/owner_probe.py b/src/cbb_refresh_verify/owner_probe.py new file mode 100644 index 0000000..9c41032 --- /dev/null +++ b/src/cbb_refresh_verify/owner_probe.py @@ -0,0 +1,59 @@ +"""Bound WMI enumeration in a read-only helper, never in the Qt thread.""" +from pathlib import Path +import json +import re +import subprocess +import sys +import tempfile + + +def query_owners(names, *, timeout_seconds=10): + names = tuple(names) + if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names): + raise ValueError('OWNER_QUERY_INVALID') + with tempfile.TemporaryDirectory(prefix='cbb-owner-probe-') as directory: + job = Path(directory)/'request.json' + job.write_text(json.dumps({'names': names}), encoding='utf-8') + command = ([sys.executable] if getattr(sys, 'frozen', False) else + [sys.executable, '-m', 'cbb_refresh_verify']) + ['--owner-probe', str(job)] + try: + result = subprocess.run(command, timeout=max(.1, min(10, timeout_seconds)), + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)) + except subprocess.TimeoutExpired as error: + # This helper only enumerates processes; it never launches Mentor + # or acquires a native database session. + raise RuntimeError('OWNER_QUERY_TIMEOUT: 进程查询超时,占用状态无法确认') from error + try: + record = json.loads(job.with_name('result.json').read_text('utf-8')) + rows = record['owners'] + if result.returncode != 0 or record['names'] != list(names) or not isinstance(rows, list): + raise ValueError('invalid result') + for row in rows: + if (type(row['pid']) is not int or row['pid'] <= 0 or row['name'].casefold() not in {n.casefold() for n in names} + or not isinstance(row['command'], str)): + raise ValueError('invalid owner') + return rows + except (OSError, ValueError, KeyError, TypeError, AttributeError) as error: + raise RuntimeError('OWNER_QUERY_UNCONFIRMED: 进程查询未返回有效证据') from error + + +def owner_probe_main(job): + import pythoncom + import win32com.client + job = Path(job) + names = json.loads(job.read_text('utf-8'))['names'] + if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names): + return 1 + pythoncom.CoInitialize() + try: + service = win32com.client.GetObject('winmgmts:') + query = 'SELECT ProcessId,Name,CommandLine FROM Win32_Process WHERE ' + ' OR '.join( + f"Name='{name}'" for name in names) + rows = [{'pid': int(item.ProcessId), 'name': str(item.Name), + 'command': str(item.CommandLine or '')} for item in service.ExecQuery(query)] + service = None + job.with_name('result.json').write_text(json.dumps({'names': names, 'owners': rows}), encoding='utf-8') + return 0 + finally: + pythoncom.CoUninitialize() diff --git a/src/cbb_refresh_verify/post_repair_consent.py b/src/cbb_refresh_verify/post_repair_consent.py index 3946569..6f8e13f 100644 --- a/src/cbb_refresh_verify/post_repair_consent.py +++ b/src/cbb_refresh_verify/post_repair_consent.py @@ -199,7 +199,13 @@ def _validate_current_processes(job, prelaunch, target_pid, launcher_pid, worker or not is_reusable_circuit_pcb(launcher[2], launcher[3], job['pcb'])): raise ValueError('POST_REPAIR_PROCESS_IDENTITY_MISMATCH') allowed = {target_pid, launcher_pid, worker_pid, int(job['supervisor_pid']), *companion_pids} - if any(r[0] not in allowed for r in rows): raise ValueError('POST_REPAIR_FOREIGN_OWNER') + foreign = [r for r in rows if r[0] not in allowed] + if foreign: + from .management import _write_json + _write_json(Path(job['root'])/'foreign-owners.json', {'allowed_pids': sorted(allowed), + 'observed': [{'pid': r[0], 'name': r[1], 'executable': r[2], 'command': r[3], 'parent_pid': r[4]} + for r in foreign]}) + raise ValueError(f'POST_REPAIR_FOREIGN_OWNER: {[(r[0], r[1]) for r in foreign]}') class ConsentController: diff --git a/src/cbb_refresh_verify/scoped_manifest.py b/src/cbb_refresh_verify/scoped_manifest.py index 597e82c..a83fa04 100644 --- a/src/cbb_refresh_verify/scoped_manifest.py +++ b/src/cbb_refresh_verify/scoped_manifest.py @@ -136,7 +136,7 @@ def _coordination_checkpoint(path): return checkpoint_record(path) is not None -def scan_tree(root, *, scope=None, exclude=None): +def scan_tree(root, *, scope=None, exclude=None, check=None): """Refuse links/special files; optionally prune trees outside semantic scope. Copies always call this without a scope, validating complete membership. @@ -148,8 +148,10 @@ def scan_tree(root, *, scope=None, exclude=None): entries = {} folders = [root] while folders: + if check is not None: check() with os.scandir(folders.pop()) as scanned: for entry in scanned: + if check is not None: check() path = Path(entry.path) if exclude is not None and path.is_relative_to(exclude): continue diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index d1004ef..1e7607f 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -142,7 +142,7 @@ class MainWindow(QWidget): self.selected_batch_button = QPushButton('批量校验已选'); self.selected_batch_button.clicked.connect(self.verify_selected) self.batch_button = QPushButton('校验可见非 READY'); self.batch_button.clicked.connect(self.verify_all) self.icdb_repair_button = QPushButton('非 READY ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) - self.icdb_repair_button.setToolTip('忽略 READY 项(包括已勾选项);先检查启动,仅本轮日志确认 ICDB 失联时才修复。') + self.icdb_repair_button.setToolTip('忽略 READY 项(包括已勾选项);先由原生工具检查占用和数据库,仅确认当前工程需要 ICDB Repair 时才修复。') self.backup_restore_button = QPushButton('从备份恢复 CBB…') self.backup_restore_button.clicked.connect(self.restore_backup) self.stop_button = QPushButton('当前项完成后停止'); self.stop_button.clicked.connect(self.request_stop) @@ -286,6 +286,7 @@ class MainWindow(QWidget): self.counts_label.setText(f'可见 {counts["visible"]} / {len(self._records_by_name)} · ' f'READY {counts["ready"]} · 异常/待确认 {counts["error"]}') self.stop_button.setEnabled(self.busy and self.loaded_path is not None and not self.stop.is_set()) + self.stop_button.setText('取消当前检查并停止队列' if self._global_icdb_run else '当前项完成后停止') def _show_unfinished_management(self): from cbb_refresh_verify.management import unfinished_operations @@ -616,7 +617,7 @@ class MainWindow(QWidget): f'{MANUAL_BACKUP_NOTICE}\nMentor 原生修复可能生成自身备份。\n\n当前中心库:{self.loaded_path}\n\n' f'将检查 {len(names)} 个可见非 READY CBB' f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '先取得库锁并核对工程与占用,检查启动但不执行集成或保存。仅本轮原生日志确认 ICDB 失联才修复,再重新打开校验。' + '先取得库锁并核对工程,由原生工具检查占用和数据库。仅本轮检查确认当前工程未占用且需要 ICDB Repair 时才修复,再重新打开校验。' '占用、历史版本提示或故障原因未确认时停止,不猜测故障或删除地址文件。READY 项始终忽略。\n' '仅本地固定磁盘:严格确认修复并重新独占检查后,可一次确认本次异常退出的读写访问提示。' '网络盘、UNC 或链接路径不自动确认该提示。\n' @@ -711,7 +712,7 @@ class MainWindow(QWidget): verify = self.verifier or (lambda req: run_verify( req, progress=lambda e: self.signals.event.emit(e), flag_reader=read_library_flag_if_available, - version_prompt=self.signals.version_choice.emit)) + version_prompt=self.signals.version_choice.emit, stop=self.stop)) def verifier(req): if maintenance: self.signals.event.emit({'block': req.block_name, 'stage': 'icdb_check_start'}) @@ -818,7 +819,7 @@ class MainWindow(QWidget): self.output.append(line) block = event.get('block'); stage = event.get('stage') if self._global_icdb_run: - if stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED', 'QUEUE_STOP'}: + if stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED', 'QUEUE_STOP', 'CHECKED', 'CANCELLED'}: self._icdb_completed += 1 self.icdb_progress.setValue(self._icdb_completed) phases = {'icdb_check_start': '检查工程数据库与校验状态', @@ -866,15 +867,16 @@ class MainWindow(QWidget): self.recovery_button.setEnabled(self.recovery_path.is_dir()) passed = sum(r.status == 'PASS' for r in results) skipped = sum(r.status == 'SKIPPED' for r in results) - failed = len(results)-passed-skipped + checked = sum(r.status == 'CHECKED' for r in results) + failed = len(results)-passed-skipped-checked text = (f'已结束:成功 {passed},失败 {failed},预检跳过 {len(self.skipped_names)},' f'队列未执行 {skipped}。\n完整日志:{report}\n仅失败日志:{failure_report_path(report)}') if self._global_icdb_run: from cbb_refresh_verify.icdb_maintenance import maintenance_counts counts = maintenance_counts(results) text = ('非 READY ICDB 检查已结束\n' - f'检查/校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' - f'启动检查通过、未修复 {counts["checked_without_repair"]}(不等同于 READY)\n' + f'修复并校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' + f'仅检查、未修复 {counts["checked_without_repair"]}(不计入修复成功)\n' f'原生修复完成 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}\n' @@ -885,7 +887,7 @@ class MainWindow(QWidget): status_text = text if self._global_icdb_run: status_text = (f'非 READY 检查结束:通过 {passed},失败/受阻 {failed},未执行 {skipped}。\n' - f'启动检查通过、未修复 {counts["checked_without_repair"]};' + f'仅检查、未修复 {counts["checked_without_repair"]};' f'原生修复 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}。') @@ -917,7 +919,7 @@ class MainWindow(QWidget): def request_stop(self): if self.version_dialog is not None: self.version_dialog.reject() - self.stop.set(); self.status_label.setText('已请求停止;当前 CBB 完成或超时后停止队列。'); self._update() + self.stop.set(); self.status_label.setText('已请求取消;检查将停止,原生写入只等待安全结束,不强制终止。'); self._update() def _failed(self, error): if self.library_lease is not None: diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 782daee..a0a23ef 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -270,6 +270,11 @@ def classify_failure_category(detail): from .icdb_recovery import ERROR_CODE, parse_session_loss if ERROR_CODE.casefold() in lower or parse_session_loss(text): return ERROR_CODE + if 'back annotation is not allowed on verified (or once verified) reusable blocks.' in lower: + # Preserve the native integration failure even when shutdown adds a + # secondary release warning. This category does not grant permission + # to reimport or change safe_to_continue. + return 'CBB_REIMPORT_REQUIRED' if any(code in lower for code in ( 'pcb_release_unconfirmed', 'icdb_release_unconfirmed', 'icdb_session_unconfirmed', 'pcb_session_active', 'database_recovery_required')): @@ -507,7 +512,7 @@ def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline, def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, port_check=None, - dialog_supervisor_factory=None, version_prompt=None): + dialog_supervisor_factory=None, version_prompt=None, stop=None): # Cover contents checking, native work and final state reads with one # session-wide mutex. The worker-stage lock below is reentrant on Windows. import win32event, win32con @@ -530,7 +535,7 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, except (ValueError, OSError) as error: return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False) if needed: - return run_existing_address_maintenance(request, progress=progress, + return run_existing_address_maintenance(request, progress=progress, stop=stop, verify=lambda req: _run_verify_serialized(req, progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt)) @@ -1167,10 +1172,12 @@ def write_failure_report(requests, results, root, *, path=None): root = Path(root); root.mkdir(parents=True, exist_ok=True) path = Path(path) if path else root/('failures-' + time.strftime('%Y%m%d-%H%M%S') + '-' + uuid4().hex[:6] + '.log') failed = [(req, result) for req, result in zip(requests, results) - if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'}] + if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'} + and not (result.status == 'CHECKED' and result.safe_to_continue)] passed = sum(result.status == 'PASS' for result in results) - skipped = len(results)-passed-len(failed) - lines = ['CBB Verify 失败清单', f'总数: {len(results)} 成功: {passed} 失败: {len(failed)} 未执行: {skipped}', + checked = sum(result.status == 'CHECKED' and result.safe_to_continue for result in results) + skipped = len(results)-passed-len(failed)-checked + lines = ['CBB Verify 失败清单', f'总数: {len(results)} 成功: {passed} 失败: {len(failed)} 未执行: {skipped} 仅检查未修复: {checked}', 'CBB\t状态\t错误分类\t失败阶段\t桥状态\t原始错误\t证据\t实际耗时(秒)'] for req, result in failed: timings = ' '.join(f'{key}={value:.3f}s' + diff --git a/tests/test_maintenance_diagnosis.py b/tests/test_maintenance_diagnosis.py index 74595a2..5f804d5 100644 --- a/tests/test_maintenance_diagnosis.py +++ b/tests/test_maintenance_diagnosis.py @@ -1,81 +1,75 @@ -"""Exercise the production diagnosis gate; do not spoof callback names.""" -import json -from pathlib import Path +"""Native occupancy must precede repair even when old addresses remain.""" import pytest from test_maintenance_entry import case, no_native_launch +from test_native_occupancy import output -def diagnostic_result(req, *, error='', database=None): - from cbb_refresh_verify.verify_runner import VerifyResult - root = req.evidence_dir/'probe'; root.mkdir(parents=True, exist_ok=True) - result = VerifyResult('FAIL' if error else 'PASS', error_category=error, - evidence_path=root/'result.json', safe_to_continue=not error) - result.evidence_path.write_text('{}', encoding='utf-8') - if error == 'ICDB_REPAIR_REQUIRED': - record = {'error_code': error, 'scope': 'project', 'session': 'test-session', - 'database_path': str(database)} - (root/'diagnostics').mkdir(exist_ok=True) - (root/'diagnostics/icdb-recovery.json').write_text(json.dumps(record), encoding='utf-8') - return result +def inspection(target, *, fault=False): + return {'inspection_only': True, 'repair_requested': False, 'native_pid': 777, + 'native_exit_code': 0, 'project': str(target.project), 'database': str(target.database), + 'output': output(target.project, target.database, fault=fault), + 'status': 'NOT_IN_USE', 'repair_required': fault, 'release': {'native_exited': True}} -def test_diagnosis_holds_library_lock_and_healthy_never_repair(case, monkeypatch): +@pytest.mark.parametrize('old_address', [False, True]) +def test_native_inspection_holds_lock_without_opening_pcb(case, monkeypatch, old_address): from cbb_refresh_verify import verify_runner as runner, native_repair, library_lock request, project, address = case - address.unlink() - def worker(req, **kw): - key = library_lock.LibraryLock(req.lmc_file).key - assert key in library_lock._owners + if not old_address: address.unlink() + def native(target, root, **kw): + key = library_lock.LibraryLock(request.lmc_file).key assert library_lock._owners[key]['depth'] > 0 - assert req.diagnostic_only and not req.auto_repair and not req.maintenance - assert not req.native_lm_verify and not req.libspec - return diagnostic_result(req) - monkeypatch.setattr(runner, '_run_verify_serialized', worker) - monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('healthy repair')) + assert kw['inspection_only'] is True + assert kw['timeout_seconds'] <= 30 + return inspection(target) + monkeypatch.setattr(native_repair, 'run_native_repair', native) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('inspection opened PCB')) result = runner.run_verify(request) - assert result.status == 'PASS' and result.recovery_attempts == () + assert result.status == 'CHECKED' and result.recovery_attempts == () assert result.library_flag == 'UNKNOWN' - assert not library_lock.has_blocker(request.lmc_file) + assert result.safe_to_continue is (not old_address) -@pytest.mark.parametrize('fault', ['missing', 'generic', 'assertion', 'wrong_database', 'outside_evidence']) -def test_unconfirmed_diagnosis_never_repairs(case, monkeypatch, fault): +@pytest.mark.parametrize('fault', ['missing', 'generic', 'assertion', 'wrong_database', 'stale_verdict', 'not_exited']) +def test_unconfirmed_native_inspection_never_repairs(case, monkeypatch, fault): from cbb_refresh_verify import verify_runner as runner, native_repair request, project, address = case - address.unlink() - def worker(req, **kw): - if fault == 'assertion': raise AssertionError('diagnostic failure') - if fault == 'missing': return runner.VerifyResult('PASS') - if fault == 'generic': return diagnostic_result(req, error='DATABASE_SAFETY_STOP') - if fault == 'outside_evidence': - from dataclasses import replace - req = replace(req, evidence_dir=request.evidence_dir/'old-run') - return diagnostic_result(req, error='ICDB_REPAIR_REQUIRED', - database=project.parent/('wrong' if fault == 'wrong_database' else 'database')) - monkeypatch.setattr(runner, '_run_verify_serialized', worker) - monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('unconfirmed repair')) + def native(target, root, **kw): + assert kw['inspection_only'] is True + if fault == 'assertion': raise AssertionError('probe failed') + if fault == 'missing': return {} + record = inspection(target, fault=True) + if fault == 'generic': record['output'] = 'DATABASE_SAFETY_STOP' + if fault == 'wrong_database': record['database'] = str(project.parent/'other') + if fault == 'stale_verdict': record['output'] += 'Checking if project is in use.' + if fault == 'not_exited': record['release']['native_exited'] = False + return record + monkeypatch.setattr(native_repair, 'run_native_repair', native) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('unconfirmed reopened PCB')) result = runner.run_verify(request) - assert result.status != 'PASS' and result.recovery_attempts == () + assert result.status == 'BLOCKED' and not result.recovery_attempts -def test_diagnostic_fault_repairs_once_then_full_verification(case, monkeypatch): +def test_native_fault_repairs_once_then_full_verification_with_old_address(case, monkeypatch): from cbb_refresh_verify import verify_runner as runner, native_repair from test_native_repair import complete_native_success request, project, address = case - address.unlink() calls = [] - def worker(req, **kw): - calls.append('diagnose' if req.diagnostic_only else 'verify') - if req.diagnostic_only: - return diagnostic_result(req, error='ICDB_REPAIR_REQUIRED', database=project.parent/'database') - assert req.post_repair_proof is not None and not req.auto_repair - return runner.VerifyResult('PASS', library_flag='GREEN') - def repair(target, *a, **kw): + def native(target, root, **kw): + if kw.get('inspection_only'): + assert address.exists() + calls.append('inspect') + return inspection(target, fault=True) calls.append('repair') + address.unlink() # Simulated native repair release only. return complete_native_success(target) - monkeypatch.setattr(runner, '_run_verify_serialized', worker) - monkeypatch.setattr(native_repair, 'run_native_repair', repair) + def verify(req, **kw): + assert req.post_repair_proof is not None and not req.diagnostic_only and not req.auto_repair + calls.append('verify') + return runner.VerifyResult('PASS', library_flag='GREEN') + monkeypatch.setattr(native_repair, 'run_native_repair', native) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) + monkeypatch.setattr(runner, '_run_verify_serialized', verify) result = runner.run_verify(request) - assert calls == ['diagnose', 'repair', 'verify'] + assert calls == ['inspect', 'repair', 'verify'] assert result.status == 'PASS' and len(result.recovery_attempts) == 1 diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index 6f3f385..ca6af62 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -48,20 +48,19 @@ def case(tmp_path, monkeypatch): def patch_verification_after_fault(monkeypatch, runner, verify): - """Inject native startup evidence, then exercise the production repair gate.""" - def worker(req, **kwargs): - if not req.diagnostic_only: - return verify(req, **kwargs) - project, pcb = runner.preflight(req) - root = req.evidence_dir/'native-startup'; root.mkdir(parents=True, exist_ok=True) - (root/'diagnostics').mkdir(exist_ok=True) - (root/'diagnostics/icdb-recovery.json').write_text(json.dumps({ - 'error_code': 'ICDB_REPAIR_REQUIRED', 'scope': 'project', 'session': 'fixture', - 'database_path': str(project.parent/'database')}), encoding='utf-8') - evidence = root/'result.json'; evidence.write_text('{}', encoding='utf-8') - return runner.VerifyResult('FAIL', error_category='ICDB_REPAIR_REQUIRED', - safe_to_continue=False, evidence_path=evidence) - monkeypatch.setattr(runner, '_run_verify_serialized', worker) + """Inject only native process boundaries; production interprets the Output.""" + from cbb_refresh_verify import native_repair + from test_native_occupancy import output + repair = native_repair.run_native_repair + def native(target, root, **kw): + if not kw.get('inspection_only'): + return repair(target, root, **kw) + return {'inspection_only': True, 'repair_requested': False, 'native_pid': 777, + 'native_exit_code': 0, 'project': str(target.project), 'database': str(target.database), + 'output': output(target.project, target.database, fault=True), + 'status': 'NOT_IN_USE', 'repair_required': True, 'release': {'native_exited': True}} + monkeypatch.setattr(native_repair, 'run_native_repair', native) + monkeypatch.setattr(runner, '_run_verify_serialized', verify) @pytest.mark.parametrize('pcb_config', ['standard', 'legacy_template']) diff --git a/tests/test_native_export_guard.py b/tests/test_native_export_guard.py index 2fc4786..1b32fbb 100644 --- a/tests/test_native_export_guard.py +++ b/tests/test_native_export_guard.py @@ -88,12 +88,11 @@ def test_restarted_export_copy_can_release_only_after_fresh_proof(export_case, t def test_maintenance_idle_guard_recognizes_new_exporter_names(monkeypatch): - import win32com.client from cbb_refresh_verify.maintenance_entry import assert_native_idle - def query(sql): - assert 'icdb2ascii.exe' in sql.casefold() and 'icdbnetserver.exe' in sql.casefold() - return [SimpleNamespace(ProcessId=123456, Name='icdb2ascii.exe', CommandLine='export')] - monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=query)) + def query(names): + assert 'icdb2ascii.exe' in [n.casefold() for n in names] and 'icdbnetserver.exe' in [n.casefold() for n in names] + return [{'pid': 123456, 'name': 'icdb2ascii.exe', 'command': 'export'}] + monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', query) with pytest.raises(ValueError, match='MAINTENANCE_OWNER_ACTIVE'): assert_native_idle() diff --git a/tests/test_native_occupancy.py b/tests/test_native_occupancy.py new file mode 100644 index 0000000..43d33d8 --- /dev/null +++ b/tests/test_native_occupancy.py @@ -0,0 +1,178 @@ +import json +from pathlib import Path +import pytest + + +def output(project, database, *, fault=False, busy=False): + text = f'Loading project file [{project}].\nChecking if project is in use.\n' + if fault: + text += (f'Project database [{database}] is inconsistent. It has been manually copied while the iCDB Server was running. ' + "Please use iCDB Project Backup 'Repair project' functionality to fix this issue.\n") + return text + ('Project is in use.\n' if busy else + "Project is not in use. Full iCDB Project Backup's functionality is available.\nBackup information loaded correctly.\n") + + +@pytest.mark.parametrize('fault,busy,status,repair', [ + (False, False, 'NOT_IN_USE', False), (True, False, 'NOT_IN_USE', True), + (True, True, 'IN_USE', False), (False, True, 'IN_USE', False)]) +def test_native_ownership_and_repair_need_are_separate(tmp_path, fault, busy, status, repair): + from cbb_refresh_verify.native_occupancy import classify_inspection + project, database = tmp_path/'A.prj', tmp_path/'database' + result = classify_inspection(project, database, output(project, database, fault=fault, busy=busy)) + assert result['status'] == status + assert result['repair_required'] is repair + + +@pytest.mark.parametrize('case', ['progress', 'wrong_project', 'wrong_database', 'stale_verdict', 'generic_repair']) +def test_incomplete_or_foreign_output_cannot_authorize_repair(tmp_path, case): + from cbb_refresh_verify.native_occupancy import classify_inspection + project, database = tmp_path/'A.prj', tmp_path/'database' + text = output(project, database, fault=True) + if case == 'progress': text = f'Loading project file [{project}].\nChecking if project is in use.' + if case == 'wrong_project': text = output(tmp_path/'Other.prj', database, fault=True) + if case == 'wrong_database': text = output(project, tmp_path/'other', fault=True) + if case == 'stale_verdict': text += 'Checking if project is in use.\n' + if case == 'generic_repair': text = output(project, database) + 'Please try to repair the project!' + assert classify_inspection(project, database, text)['repair_required'] is False + + +@pytest.mark.parametrize('fault,busy', [(False, False), (True, False), (False, True)]) +def test_inspection_mode_only_observes_and_closes_its_tool(tmp_path, monkeypatch, fault, busy): + import win32con + from test_native_repair import repair_worker_fixture + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True + job.write_text(json.dumps(data), encoding='utf-8') + monkeypatch.setattr(ui, '_repair_command', lambda *a: pytest.fail('inspection resolved Repair command')) + monkeypatch.setattr(ui, '_native_output', lambda *_: output(data['project'], data['database'], fault=fault, busy=busy)) + assert ui.repair_worker_main(job) == 0 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['status'] == ('IN_USE' if busy else 'NOT_IN_USE') + assert result['release']['native_exited'] is True + assert result['repair_required'] is (fault and not busy) + assert actions == [(100, win32con.WM_CLOSE)] + + +def test_only_check_never_counts_as_repair_success(): + from cbb_refresh_verify.icdb_maintenance import maintenance_counts + from cbb_refresh_verify.verify_runner import VerifyResult + counts = maintenance_counts([VerifyResult('CHECKED', safe_to_continue=True)]) + assert counts['passed'] == counts['repaired'] == 0 + assert counts['checked_without_repair'] == 1 + + +def test_cancelled_probe_returns_within_grace_without_killing_native(tmp_path, monkeypatch): + from threading import Event + from types import SimpleNamespace + from cbb_refresh_verify import native_repair as native + from cbb_refresh_verify.native_repair import RepairTarget + stop = Event(); ticks = [0.0]; actions = [] + def sleep(seconds): + ticks[0] += seconds + stop.set() + monkeypatch.setattr(native, 'time', SimpleNamespace(monotonic=lambda: ticks[0], sleep=sleep)) + class Worker: + pid = 123 + def __init__(self, *a): pass + def bind_cancellation(self, p): pass + def poll(self): return None + def request_stop(self): actions.append('cancel') + def close(self): actions.append('close_handles') + def kill(self): pytest.fail('native owner must not be killed') + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker) + result = native.run_native_repair(RepairTarget(tmp_path/'A.prj', tmp_path/'database'), + tmp_path/'evidence', timeout_seconds=600, inspection_only=True, stop=stop) + assert ticks[0] < 6 + assert result['status'] == 'CANCELLED_UNRELEASED' + assert result['release']['safe_to_continue'] is False + assert actions[-1] == 'close_handles' + + +def test_ui_keeps_processing_events_and_passes_cancel_to_native_task(qtbot, tmp_path, monkeypatch): + from threading import Event + from PySide6.QtCore import QTimer + from test_ui_filters import load_window + from cbb_refresh_verify.verify_runner import VerifyResult + entered = Event(); cancelled = Event(); ticks = [] + def verify(req, *, stop, **kw): + entered.set() + if stop.wait(3): cancelled.set() + return VerifyResult('CANCELLED') + monkeypatch.setattr('cbb_refresh_verify.verify_runner.run_verify', verify) + window = load_window(qtbot, tmp_path, monkeypatch, + status_refresher=lambda names, _: {name: 'PENDING' for name in names}) + window.search_edit.setText('Beta') + timer = QTimer(window); timer.setInterval(10); timer.timeout.connect(lambda: ticks.append(True)); timer.start() + window.repair_icdb_all() + qtbot.waitUntil(entered.is_set) + qtbot.waitUntil(lambda: len(ticks) >= 3) + assert window.busy and window.stop_button.isEnabled() + window.stop_button.click() + qtbot.waitUntil(lambda: not window.busy) + assert cancelled.is_set() + + +@pytest.mark.parametrize('extra', ['foreign_session', 'extra_error']) +def test_foreign_or_additional_fault_is_never_a_clean_inspection(tmp_path, extra): + from cbb_refresh_verify.native_occupancy import classify_inspection + project, database = tmp_path/'A.prj', tmp_path/'database' + text = output(project, database, fault=extra == 'extra_error') + if extra == 'foreign_session': + text += ('The last server session on different server machine lost the connection to the database. ' + 'Machine:[SERVER] Path:[C:/Other/Work/database] session:[old]') + else: + text += 'ERROR: shared symbol database unavailable' + assert classify_inspection(project, database, text)['status'] == 'UNCONFIRMED' + + +def test_unconfirmed_inspection_closes_known_main_at_timeout(tmp_path, monkeypatch): + import win32con + from test_native_repair import repair_worker_fixture + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True + job.write_text(json.dumps(data), encoding='utf-8') + monkeypatch.setattr(ui, '_native_output', lambda *_: 'Unrecognized response') + assert ui.repair_worker_main(job) == 1 + result = json.loads((root/'repair-result.json').read_text('utf-8')) + assert result['status'] == 'UNCONFIRMED' + assert result['release']['native_exited'] is True + assert actions == [(100, win32con.WM_CLOSE)] + + +def test_directory_scan_honors_cancel_between_entries(tmp_path): + from cbb_refresh_verify.scoped_manifest import scan_tree + for i in range(10): (tmp_path/f'{i}.dat').touch() + calls = [] + def cancel(): + calls.append(True) + if len(calls) == 3: raise RuntimeError('CANCELLED') + with pytest.raises(RuntimeError, match='CANCELLED'): + scan_tree(tmp_path, check=cancel) + assert len(calls) == 3 + + +def test_cancel_during_final_guard_cannot_launch_native_tool(tmp_path, monkeypatch): + from contextlib import contextmanager + from test_native_repair import repair_worker_fixture + from cbb_refresh_verify.native_repair import RepairTarget + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + data = json.loads(job.read_text('utf-8')) + data['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'} + job.write_text(json.dumps(data), encoding='utf-8') + calls = [] + @contextmanager + def guard(*a, **kw): + calls.append(True) + yield {'target': RepairTarget(Path(data['project']), Path(data['database']))} + if len(calls) == 2: Path(data['cancel_signal']).touch() + monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard) + monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('late native launch')) + assert ui.repair_worker_main(job) == 1 + assert len(calls) == 2 and not actions + + +def test_verified_cbb_refusal_is_not_hidden_by_secondary_release_failure(): + from cbb_refresh_verify.verify_runner import classify_failure_category + detail = ('BackAnnotate: Back Annotation is not allowed on verified (or once verified) Reusable Blocks.' + '\nPCB_RELEASE_UNCONFIRMED: not released') + assert classify_failure_category(detail) == 'CBB_REIMPORT_REQUIRED' diff --git a/tests/test_owner_probe.py b/tests/test_owner_probe.py new file mode 100644 index 0000000..732a035 --- /dev/null +++ b/tests/test_owner_probe.py @@ -0,0 +1,23 @@ +import subprocess +import pytest + + +def test_wmi_timeout_is_unknown_not_idle(monkeypatch): + from cbb_refresh_verify.owner_probe import query_owners + def hang(command, **kw): + assert 0 < kw['timeout'] <= 10 + raise subprocess.TimeoutExpired(command, kw['timeout']) + monkeypatch.setattr(subprocess, 'run', hang) + with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'): + query_owners(['ExpeditionPCB.exe']) + + +def test_malformed_wmi_response_is_not_empty_owner_list(monkeypatch): + from cbb_refresh_verify.owner_probe import query_owners + from pathlib import Path + def run(command, **kw): + Path(command[-1]).with_name('result.json').write_text('{}') + return subprocess.CompletedProcess(command, 0) + monkeypatch.setattr(subprocess, 'run', run) + with pytest.raises(RuntimeError, match='OWNER_QUERY_UNCONFIRMED'): + query_owners(['ExpeditionPCB.exe']) -- 2.39.5 From 29959e2988cf4e458d6ab95e04605e80ade08eda 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: Wed, 4 Nov 2026 14:16:00 +0800 Subject: [PATCH 49/59] feat: offer explicit copy recovery after failed ICDB maintenance --- .../2026-11-04-copy-reimport-recovery.md | 24 ++ src/cbb_refresh_verify/library_lock.py | 29 ++- src/cbb_refresh_verify/reimport_recovery.py | 70 +++++ src/cbb_refresh_verify/ui/main_window.py | 84 +++++- .../ui/management_dialog.py | 47 +++- tests/test_library_lock.py | 51 +++- tests/test_reimport_recovery.py | 242 ++++++++++++++++++ 7 files changed, 520 insertions(+), 27 deletions(-) create mode 100644 docs/releases/2026-11-04-copy-reimport-recovery.md create mode 100644 src/cbb_refresh_verify/reimport_recovery.py create mode 100644 tests/test_reimport_recovery.py diff --git a/docs/releases/2026-11-04-copy-reimport-recovery.md b/docs/releases/2026-11-04-copy-reimport-recovery.md new file mode 100644 index 0000000..34bc5ce --- /dev/null +++ b/docs/releases/2026-11-04-copy-reimport-recovery.md @@ -0,0 +1,24 @@ +# ICDB 失败后的副本恢复入口 + +原生修复失败、缺少有效备份,或 Repair 后完整校验失败时,结果窗口提供“选择副本恢复…”;主窗口保留“选择工程副本恢复失败项…”,允许关闭结果窗口后继续。单纯占用、未知诊断、仅检查或成功结果不提示替换。 + +入口固定使用本次失败任务的库、名称和分区,不使用用户后来勾选的其他条目。切换中心库使旧入口失效。选择的是库外完整工程的 PRJ 文件,相关 PCB、数据库和依赖资料仍由现有来源检查验证。 + +点击入口只打开来源选择窗口。用户明确确认手动备份、目标库、目标条目及副本后,才在后台检查释放,并复用现有原生移除/导入流程;不自动备份整个库,不自动删除目录代替原生操作。自动校验默认开启。 + +释放检查继续要求原生所有者退出、数据库地址记录处理完毕及独占可读。WMI 查询通过最多 10 秒的只读助手执行,超时不当作空闲;同时检查脚本与校验/修复工作进程。检查不通过显示具体原因、保留阻止记录和恢复入口。未完成的管理写入仍不自动重放。 + +原维护失败记录保持原样。副本恢复建立独立 `reimport-recovery/*/recovery.json`,关联原证据、来源、释放检查和管理结果。上传成功但校验失败继续显示失败;上传并取得 READY 后显示“副本恢复校验通过,版本待核对”,不改称原 ICDB 无损修复成功。 + +按用户要求精简界面:默认只展示目标、副本选择、备份确认和操作结果。开发流程说明已移除,诊断细节和证据路径放入默认收起的日志。恢复执行前仍明确说明先移除原条目再导入副本。 + +## 验收边界 + +- 核心流程专项测试 57 项通过;随后新增的两项主窗口取消/退出复核回归均先复现失败再通过。 +- 完整回归记录为 **1,194 passed / 1 failed**(1,013.66 秒)。该运行启动后用户要求精简 UI,已收集的测试仍断言旧文案“尚未修复成功”,运行时加载的界面已使用“修复未完成”,因此失败。未把该次结果标成全量通过。 +- 最终界面与更新后的测试重新验证 **77 passed**(43.00 秒),包含该失败用例及新增的日志默认收起检查。其余产品逻辑未在此期间修改。 +- 编译、差异检查、独立审查通过。最终冻结构建及启动/正常退出、占用辅助进程冒烟通过,EXE 内 5 个关键模块与当前源码一致。记录在 `diagnostics/copy-reimport-20261104/`。 + +本次验证覆盖 Qt 真实事件循环中的按钮、取消、来源确认、后台等待及故障分支,原生写入边界使用替身,不把这些测试当作 Mentor 修复验收。 + +既有隔离副本仍有 `IP101GR/Work/database/cdbsvr/sAddress.adr`,因此完整库释放条件目前不满足。该情况不能通过忽略地址或强制清除检查点解决,需要先处理对应工程。此前 `1GPHY_8212` 的原始 Repair 后 BackAnnotate 失败结论保持不变,工单未关闭。 diff --git a/src/cbb_refresh_verify/library_lock.py b/src/cbb_refresh_verify/library_lock.py index 8c4d1af..98ec832 100644 --- a/src/cbb_refresh_verify/library_lock.py +++ b/src/cbb_refresh_verify/library_lock.py @@ -288,7 +288,8 @@ def owned_snapshot(path): @contextmanager def _prove_library_released(library): """Hold all native database read handles during explicit lease release.""" - import pythoncom, win32com.client, win32con, win32file + import win32con, win32file + from .owner_probe import query_owners from .scoped_manifest import scan_tree from .native_management import _retained_workers from .native_export_guard import assert_retained_exports_idle, prove_export_copies_released @@ -299,15 +300,24 @@ def _prove_library_released(library): from .com_runtime import initialize_com, uninitialize_com initialized = initialize_com() try: - service = win32com.client.GetObject('winmgmts:') names = ('LibraryManager.exe', 'ExpeditionPCB.exe', 'viewdraw.exe', - 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe', *sorted(_OWNER_NAMES)) - query = 'SELECT ProcessId, Name FROM Win32_Process WHERE ' + ' OR '.join( - f"Name='{name}'" for name in names) - owners = [{'pid': int(item.ProcessId), 'name': str(item.Name)} for item in service.ExecQuery(query)] - service = None - if owners: - raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}') + 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe', + 'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe', + 'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES)) + def check_owners(): + owners = [] + for item in query_owners(names): + if item['pid'] == os.getpid(): + continue + automation = item['name'].casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe') + command = item['command'].casefold() + 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 LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}') + return owners + check_owners() entries = scan_tree(Path(library).parent) addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr'] if addresses: @@ -322,6 +332,7 @@ def _prove_library_released(library): handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) stack.callback(handle.Close) + owners = check_owners() yield {'native_owners': owners, 'addresses': addresses, 'export_copies': export_copies, 'exclusive_files': [str(path) for path in native]} finally: diff --git a/src/cbb_refresh_verify/reimport_recovery.py b/src/cbb_refresh_verify/reimport_recovery.py new file mode 100644 index 0000000..4905e23 --- /dev/null +++ b/src/cbb_refresh_verify/reimport_recovery.py @@ -0,0 +1,70 @@ +"""Explicit copy-based recovery after failed maintenance; never auto-replay.""" +from dataclasses import asdict +from pathlib import Path +from uuid import uuid4 + + +def offers_reimport(result): + return (result.status == 'FAIL' and result.evidence_path is not None + and any(attempt.get('status') in { + 'REPAIR_VALIDATION_FAILED', 'MISSING_HEALTHY_BACKUP', 'NATIVE_REPAIR_FAILED'} + for attempt in result.recovery_attempts)) + + +def run_reimport_recovery(requests, *, previous_evidence, evidence_dir, + adapter=None, stop=None, progress=None): + from .management import ManagementResult, _write_json, source_project, run_management_batch + from .library_lock import release_retained_library + requests = tuple(requests) + if not requests or stop is not None and stop.is_set(): + return [] + library = requests[0].library.resolve() + evidence_dir = Path(evidence_dir).resolve() + if evidence_dir.is_relative_to(library.parent) or library.parent.is_relative_to(evidence_dir): + raise ValueError('恢复记录必须位于目标库之外。') + root = evidence_dir/('reimport-' + uuid4().hex) + root.mkdir(parents=True) + report = root/'recovery.json' + record = {'library': str(library), 'previous_evidence': previous_evidence, + 'requests': [asdict(r) for r in requests], 'original_repair_succeeded': False} + stage = 'preflight' + def save(stage, **extra): + record.update(stage=stage, **extra) + _write_json(report, record) + def cancelled(): + if stop is not None and stop.is_set(): + save('cancelled', management_started=False) + return True + return False + try: + save(stage, management_started=False) + for request in requests: + if (request.operation != 'update' or request.library.resolve() != library + or not previous_evidence.get(request.name)): + raise ValueError('恢复目标与原失败项不一致。') + source, _, _ = source_project(request.source, request.top_level_block) + if (source.parent.is_relative_to(library.parent) + or library.parent.is_relative_to(source.parent)): + raise ValueError('请选择目标中心库之外的完整工程副本。') + if cancelled(): return [] + stage = 'release_check' + save(stage) + if progress: progress({'stage': stage, 'detail': '正在检查原生会话释放,尚未替换条目'}) + proof = release_retained_library(library, root/'release') + if proof.get('status') != 'RELEASE_CONFIRMED': + raise ValueError('原生会话释放未确认,未开始导入。') + save('released', release=proof) + if cancelled(): return [] + stage = 'management' + save(stage, management_started=True) + results = run_management_batch(requests, adapter=adapter, stop=stop, progress=progress) + save('complete', results=[asdict(result) for result in results], + version_review_required=True) + return results + except Exception as error: + save('release_failed' if stage == 'release_check' else 'failed', detail=str(error)) + guidance = ('请正常关闭提示中的原生程序;若提示数据库地址记录,请先对对应工程执行 ICDB 维护,再重试副本恢复。' + if stage == 'release_check' else '') + return [ManagementResult(r.operation, r.library, r.name, r.partition, r.source, + r.auto_verify, stage, 'BLOCKED' if stage != 'management' else 'RECOVERY_REQUIRED', + False, f'副本恢复未完成:{error}。{guidance}原修复失败记录仍保留。', report) for r in requests] diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 1e7607f..d0e8b94 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -67,6 +67,10 @@ class MainWindow(QWidget): self._management_unsafe = False self._maintenance_recovery_view = False self._reload_after_maintenance = False + self._reimport_offers = () + self._batch_requests = () + self._batch_records = {} + self._reimport_summary_action = None if reader is not None: self.reader = reader elif authoritative_status and os.environ.get('CBB_STATUS_BRIDGE_MODE', 'authoritative').casefold() != 'legacy': @@ -145,6 +149,9 @@ class MainWindow(QWidget): self.icdb_repair_button.setToolTip('忽略 READY 项(包括已勾选项);先由原生工具检查占用和数据库,仅确认当前工程需要 ICDB Repair 时才修复。') self.backup_restore_button = QPushButton('从备份恢复 CBB…') self.backup_restore_button.clicked.connect(self.restore_backup) + self.reimport_button = QPushButton('选择工程副本恢复失败项…') + self.reimport_button.clicked.connect(self.reimport_failed) + self.reimport_button.hide() self.stop_button = QPushButton('当前项完成后停止'); self.stop_button.clicked.connect(self.request_stop) self.logs_button = QPushButton('打开日志目录'); self.logs_button.clicked.connect(self.open_logs) self.recovery_button = QPushButton('打开异常证据'); self.recovery_button.setEnabled(False) @@ -184,8 +191,7 @@ class MainWindow(QWidget): layout.addRow('搜索 / 筛选', filters) layout.addRow(self.counts_label) layout.addRow('CBB 清单(勾选后可批量)', self.cbb_table); layout.addRow('超时', self.timeout_spin) - layout.addRow(QLabel('逐个刷新:命令行启动 ReusableCircuit PCB → Project Integration → 保存并关闭,全程不切换桌面、不移动鼠标。' - '填写 LibSpec 与 DMS 登录配置后,会先执行一次 ReuseBlockImporter 内容检查。')) + layout.addRow(QLabel('逐项校验 CBB,完成后刷新状态。')) controls = QHBoxLayout() for widget in (self.selected_batch_button, self.batch_button, self.stop_button, self.logs_button): controls.addWidget(widget) layout.addRow(controls) @@ -198,6 +204,7 @@ class MainWindow(QWidget): maintenance_row.addWidget(description, 1) maintenance_layout.addLayout(maintenance_row) maintenance_layout.addWidget(self.backup_restore_button) + maintenance_layout.addWidget(self.reimport_button) self.icdb_progress = QProgressBar() self.icdb_progress.setVisible(False) self.icdb_phase = QLabel('') @@ -226,7 +233,12 @@ class MainWindow(QWidget): self.release_library_button.clicked.connect(self.check_library_release) management_layout.addWidget(self.release_library_button) layout.addRow(management) - layout.addRow(self.status_label); layout.addRow(self.output) + layout.addRow(self.status_label) + self.runtime_log_toggle = QToolButton(); self.runtime_log_toggle.setText('查看运行日志') + self.runtime_log_toggle.setCheckable(True) + self.runtime_log_toggle.toggled.connect(self.output.setVisible) + self.output.hide() + layout.addRow(self.runtime_log_toggle); layout.addRow(self.output) layout.addRow(self.recovery_button) self.lmc_edit.textChanged.connect(self._invalidate) for field in (self.libspec_edit, self.dms_edit, self.mentor_bin_edit): @@ -236,6 +248,7 @@ class MainWindow(QWidget): self._update() def _invalidate(self): + self._reimport_offers = () if self.loaded_path is not None and self.lmc_edit.text() != self.loaded_path: self._release_library_lease() self.loaded_path = None; self.mentor_lmc_path = None; self.cbb_table.setRowCount(0); self._row_by_name = {} @@ -287,6 +300,12 @@ class MainWindow(QWidget): f'READY {counts["ready"]} · 异常/待确认 {counts["error"]}') self.stop_button.setEnabled(self.busy and self.loaded_path is not None and not self.stop.is_set()) self.stop_button.setText('取消当前检查并停止队列' if self._global_icdb_run else '当前项完成后停止') + available = bool(self._reimport_offers) and not self.busy and self.management_capabilities['update'].supported + self.reimport_button.setVisible(bool(self._reimport_offers)) + self.reimport_button.setEnabled(available) + self.reimport_button.setToolTip(self.management_capabilities['update'].reason) + if self._reimport_summary_action is not None: + self._reimport_summary_action.setEnabled(available) def _show_unfinished_management(self): from cbb_refresh_verify.management import unfinished_operations @@ -338,7 +357,6 @@ class MainWindow(QWidget): self.status_label.setText(reason) return from cbb_refresh_verify.management import ManagementRequest - from .management_dialog import ManagementDialog records = self.selected_records() if operation != 'add' and not records: return @@ -351,19 +369,55 @@ class MainWindow(QWidget): [request(record.name, 'Logical-Physical' if record.logical_type == 'LP' and record.partition_name in ('Reusable Blocks', 'LP') else record.partition_name) for record in records]) - self.management_dialog = ManagementDialog(requests, self, adapter=self.management_adapter) + self._open_management_dialog(requests) + + def _open_management_dialog(self, requests, *, recovery_from=None): + from .management_dialog import ManagementDialog + self._global_icdb_run = False + self.stop.clear() + self._management_is_recovery = bool(recovery_from) + self.management_dialog = ManagementDialog(requests, self, adapter=self.management_adapter, + recovery_from=recovery_from) self._management_results = [] self.management_dialog.completed.connect(self._management_completed) self.management_dialog.finished.connect(self._management_closed) self.busy = True; self._update() self.management_dialog.show() + def reimport_failed(self): + if self.busy or not self._reimport_offers or not self.management_capabilities['update'].supported: + return + from cbb_refresh_verify.management import ManagementRequest + requests, previous = [], {} + library = Path(self.mentor_lmc_path or self.loaded_path) + for req, record, result in self._reimport_offers: + if req.lmc_file != library: + return + partition = ('Logical-Physical' if record.logical_type == 'LP' + and record.partition_name in ('Reusable Blocks', 'LP') else record.partition_name) + requests.append(ManagementRequest('update', library, record.name, partition, + evidence_dir=self.data_root/'management', timeout_seconds=self.timeout_spin.value(), + mapped_drive=getattr(self.library_lease, 'drive', ''), + mapped_remote_root=getattr(self.library_lease, 'remote_root', ''))) + previous[record.name] = str(result.evidence_path) + self._reload_after_maintenance = False + if getattr(self, 'summary', None): self.summary.close() + self._open_management_dialog(requests, recovery_from=previous) + def _management_completed(self, results): self._management_results = results + if (getattr(self, '_management_is_recovery', False) and results + and all(result.safe_to_continue for result in results)): + self._management_unsafe = False + self._maintenance_recovery_view = False + restored = {result.name for result in results if result.status == 'UPLOADED_VERIFIED'} + self._reimport_offers = tuple(offer for offer in self._reimport_offers if offer[1].name not in restored) from .management_dialog import RESULT_LABELS for result in results: self.output.append(f'{result.name}:{RESULT_LABELS.get(result.status, result.status)} ' f'{result.detail}\n{result.evidence_path or ""}') + if getattr(self, '_management_is_recovery', False) and result.status == 'UPLOADED_VERIFIED': + self.output.append('副本恢复校验通过,版本待核对。') if any(not result.safe_to_continue for result in results): self._management_unsafe = True if self.library_lease: @@ -694,6 +748,9 @@ class MainWindow(QWidget): self._update() return requests = [replace(self._request(name), maintenance=maintenance) for name in names] + self._batch_requests = tuple(requests) + self._batch_records = {name: self._records_by_name[name] for name in names} + self._reimport_offers = () self._global_icdb_run = maintenance self._icdb_completed = 0 self.icdb_progress.setVisible(maintenance) @@ -851,6 +908,10 @@ class MainWindow(QWidget): def _batch_done(self, outcome): results, report = outcome[:2] + from cbb_refresh_verify.reimport_recovery import offers_reimport + self._reimport_offers = tuple((req, self._batch_records[req.block_name], result) + for req, result in zip(self._batch_requests, results) + if req.maintenance and req.block_name in self._batch_records and offers_reimport(result)) if self._maintenance_recovery_view and self._global_icdb_run and results: self._reload_after_maintenance = all(result.safe_to_continue for result in results) if len(outcome) > 2: @@ -897,10 +958,20 @@ class MainWindow(QWidget): explanation = '\n首个未完成原因:' + first_failure[:1500] status_text += explanation text += explanation + if self._reimport_offers: + guidance = '\n修复未完成,可选择工程副本恢复。' + text += guidance + status_text += guidance self.status_label.setText(status_text); self.output.append(text) self.summary = QMessageBox(self) self.summary.setWindowTitle('非 READY ICDB 检查与修复结果' if self._global_icdb_run else 'CBB 校验结果'); self.summary.setText(text) self.summary.setIcon(QMessageBox.Warning if failed else QMessageBox.Information) + self._reimport_summary_action = None + if self._reimport_offers: + self.summary.setStandardButtons(QMessageBox.Close) + self._reimport_summary_action = self.summary.addButton('选择副本恢复…', QMessageBox.ActionRole) + self._reimport_summary_action.clicked.connect(self.reimport_failed) + self._reimport_summary_action.setEnabled(False) # Enabled after the worker finishes. # Modeless notification: do not force activation or steal keyboard focus. from PySide6.QtCore import Qt self.summary.setAttribute(Qt.WA_ShowWithoutActivating) @@ -919,6 +990,9 @@ class MainWindow(QWidget): def request_stop(self): if self.version_dialog is not None: self.version_dialog.reject() + management = getattr(self, 'management_dialog', None) + if management is not None and management.isVisible(): + management.stop.set() self.stop.set(); self.status_label.setText('已请求取消;检查将停止,原生写入只等待安全结束,不强制终止。'); self._update() def _failed(self, error): diff --git a/src/cbb_refresh_verify/ui/management_dialog.py b/src/cbb_refresh_verify/ui/management_dialog.py index b7404cd..6bbeb43 100644 --- a/src/cbb_refresh_verify/ui/management_dialog.py +++ b/src/cbb_refresh_verify/ui/management_dialog.py @@ -20,10 +20,11 @@ RESULT_LABELS = { STAGE_LABELS = { 'prepare_identity': '正在核对来源工程内容', 'catalog_before': '正在核对原生目录', 'baseline': '正在记录中心库内容基线', - 'stage_source': '正在复制并检查来源工程', 'native_remove': '正在原生移除', - 'removed': '原条目已移除', 'native_add': '正在原生上传', 'uploaded': '上传身份已确认', + 'stage_source': '正在复制并检查来源工程', 'native_remove': '正在移除', + 'removed': '原条目已移除', 'native_add': '正在上传', 'uploaded': '上传已确认', 'verify': '正在校验上传结果', 'verified': '校验已返回', 'copy': '正在准备来源副本', 'hash': '正在校验主要文件', + 'release_check': '正在检查工程是否被占用', } @@ -34,13 +35,19 @@ class ManagementSignals(QObject): class ManagementDialog(QDialog): completed = Signal(object) - def __init__(self, requests, parent=None, *, adapter=None, runner=None): + def __init__(self, requests, parent=None, *, adapter=None, runner=None, recovery_from=None): super().__init__(parent) self.templates = tuple(requests) self.template = self.templates[0] self.operation = self.template.operation self.adapter = adapter + self.recovery_from = dict(recovery_from or {}) self.runner = runner or run_management_batch + if self.recovery_from and runner is None: + from ..reimport_recovery import run_reimport_recovery + self.runner = lambda requests, **kwargs: run_reimport_recovery(requests, + previous_evidence=self.recovery_from, + evidence_dir=self.template.evidence_dir.parent/'reimport-recovery', **kwargs) self.busy = False self.executed = False self.stop = Event() @@ -62,16 +69,26 @@ class ManagementDialog(QDialog): self.status_label = QLabel(MANUAL_BACKUP_NOTICE + ' 更新会先移除再重新导入,失败时请使用手动备份恢复。') self.status_label.setWordWrap(True) self.output = QTextEdit(); self.output.setReadOnly(True) + self.output.document().setMaximumBlockCount(2000) + self.output.hide() + self.logs_button = QPushButton('查看日志') + self.logs_button.setCheckable(True) + self.logs_button.toggled.connect(self.output.setVisible) + if self.recovery_from: + self.setWindowTitle('从副本恢复 CBB') + self.status_label.setText('修复未完成。请选择完整工程副本(.prj)。所列 CBB 将替换为副本中的版本。\n' + + MANUAL_BACKUP_NOTICE) + self.output.append('\n'.join(f'原失败记录 {name}:{path}' for name, path in self.recovery_from.items())) self.execute_button = QPushButton('预览并确认执行…') self.execute_button.clicked.connect(self._execute) self.stop_button = QPushButton('当前项结束后停止'); self.stop_button.setEnabled(False) self.stop_button.clicked.connect(self.stop.set) self.close_button = QPushButton('关闭'); self.close_button.clicked.connect(self.reject) layout = QVBoxLayout(self) - layout.addWidget(QLabel(f'目标库:{self.template.library}\n操作记录:{self.template.evidence_dir}')) + layout.addWidget(QLabel(f'目标库:{self.template.library}')) layout.addWidget(self.table) layout.addWidget(self.sources_button); layout.addWidget(self.auto_verify) - layout.addWidget(self.status_label); layout.addWidget(self.output) + layout.addWidget(self.status_label); layout.addWidget(self.logs_button); layout.addWidget(self.output) buttons = QHBoxLayout() for widget in (self.execute_button, self.stop_button, self.close_button): buttons.addWidget(widget) layout.addLayout(buttons) @@ -132,9 +149,12 @@ class ManagementDialog(QDialog): description = ('同名更新会原生移除并重新导入;导入失败需要使用手动备份恢复。' if self.operation == 'update' else '将通过原生操作移除所列 CBB;恢复时需要使用手动备份。' if self.operation == 'remove' else '新增同名冲突会拒绝覆盖,完整来源工程将先复制到暂存区。') + if self.recovery_from: + description = ('将用所选副本替换这些 CBB。操作会先移除原条目,再导入副本;' + '导入失败时需要用备份恢复。请确认副本版本。') answer = QMessageBox.question(self, '确认管理操作', f'{MANUAL_BACKUP_NOTICE}\n{description}\n\n目标库:{self.template.library}\n{preview}\n\n' - f'操作记录与检查点:{self.template.evidence_dir}\n确认继续执行?', QMessageBox.Yes | QMessageBox.No, + '确认继续执行?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer != QMessageBox.Yes: return @@ -155,7 +175,8 @@ class ManagementDialog(QDialog): def _progress(self, event): stage = str(event.get('stage', '')) - text = f'{event.get("block", "")} {STAGE_LABELS.get(stage, RESULT_LABELS.get(stage, stage))}' + text = f'{event.get("block", "")} {STAGE_LABELS.get(stage, RESULT_LABELS.get(stage, "正在处理"))}' + self.output.append(str(event)) if 'copied_files' in event: text += f' {event["copied_files"]}/{event.get("total_files", "?")}' elif 'hashed_files' in event: @@ -163,14 +184,20 @@ class ManagementDialog(QDialog): self.status_label.setText(text) def _result(self, results): - lines = [] + lines, summaries = [], [] for result in results: - text = f'{result.name}:{RESULT_LABELS.get(result.status, result.status)} — {result.detail}' + summary = f'{result.name}:{RESULT_LABELS.get(result.status, result.status)}' + text = f'{summary} — {result.detail}' + if self.recovery_from and result.status == 'UPLOADED_VERIFIED': + summary += ',版本待核对。' + elif result.status not in ('UPLOADED', 'UPLOADED_VERIFIED', 'REMOVED'): + summary += ',详情请查看日志。' + summaries.append(summary) if result.evidence_path: text += f'\n记录:{result.evidence_path}' lines.append(text) message = '\n'.join(lines) if lines else '队列已取消,未执行任何项。' - self.status_label.setText(message); self.output.append(message) + self.status_label.setText('\n'.join(summaries) if summaries else message); self.output.append(message) self.completed.emit(results) def _failed(self, error): diff --git a/tests/test_library_lock.py b/tests/test_library_lock.py index d65c417..fa9d8b3 100644 --- a/tests/test_library_lock.py +++ b/tests/test_library_lock.py @@ -118,9 +118,8 @@ def test_explicit_release_requires_fresh_proof_and_never_deletes_sidecar(tmp_pat def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks - import win32com.client - from types import SimpleNamespace - monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=lambda *_: [])) + import cbb_refresh_verify.owner_probe as probe + monkeypatch.setattr(probe, 'query_owners', lambda *_: []) library = tmp_path/'lib.lmc'; library.touch() address = tmp_path/'database/cdbsvr/sAddress.adr'; address.parent.mkdir(parents=True) address.touch() @@ -138,6 +137,52 @@ def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkey assert proof['exclusive_files'] == [str(library)] +def test_release_probe_timeout_does_not_clear_retained_checkpoint(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.owner_probe as probe + library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch() + with locks.LibraryLock(library, safe_to_release=lambda: False) as lease: pass + marker = locks.blocker_path(library) + before = marker.read_bytes() + def timeout(*args): raise RuntimeError('OWNER_QUERY_TIMEOUT') + monkeypatch.setattr(probe, 'query_owners', timeout) + try: + with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'): + locks.release_retained_library(library, tmp_path/'release') + assert marker.read_bytes() == before + finally: + with locks._guard: + owner = locks._owners.pop(lease.key, None) + if owner: owner['handle'].Close() + + +@pytest.mark.parametrize('name,command', [ + ('cscript.exe', 'cscript.exe close_failed_block.vbs'), + ('python.exe', 'python.exe -m cbb_refresh_verify --repair-worker job.json'), +]) +def test_release_proof_waits_for_late_operation_helpers(tmp_path, monkeypatch, name, command): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.owner_probe as probe + library = tmp_path/'lib.lmc'; library.touch() + monkeypatch.setattr(probe, 'query_owners', lambda names: + [{'pid': 123456, 'name': name, 'command': command}] if name in names else []) + with pytest.raises(locks.LibraryBusy, match='123456'): + with locks._prove_library_released(library): pass + + +def test_release_rechecks_owners_after_database_handles_are_acquired(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.owner_probe as probe + library = tmp_path/'lib.lmc'; library.touch() + calls = [] + def query(names): + calls.append(1) + return [] if len(calls) == 1 else [{'pid': 123456, 'name': 'cscript.exe', 'command': 'helper'}] + monkeypatch.setattr(probe, 'query_owners', query) + with pytest.raises(locks.LibraryBusy, match='123456'): + with locks._prove_library_released(library): pass + + def test_public_verify_initializes_com_in_its_operation_thread(tmp_path, monkeypatch): import ctypes import cbb_refresh_verify.verify_runner as runner diff --git a/tests/test_reimport_recovery.py b/tests/test_reimport_recovery.py new file mode 100644 index 0000000..1c203d5 --- /dev/null +++ b/tests/test_reimport_recovery.py @@ -0,0 +1,242 @@ +from dataclasses import replace +from pathlib import Path +from threading import Event +from types import SimpleNamespace + +import pytest +from PySide6.QtCore import QTimer +from PySide6.QtWidgets import QMessageBox + +from cbb_refresh_verify.management import Capability, ManagementRequest, ManagementResult +from cbb_refresh_verify.verify_runner import VerifyResult + + +def failed_repair(path, *, safe=False): + return VerifyResult('FAIL', error_category='CBB_REIMPORT_REQUIRED', + stderr='Back Annotation is not allowed on verified (or once verified) Reusable Blocks.', + evidence_path=path, safe_to_continue=safe, + recovery_attempts=({'native_status': 'REPAIRED', 'status': 'REPAIR_VALIDATION_FAILED', + 'validation_passed': False},)) + + +def recovery_window(qtbot, tmp_path, monkeypatch, result=None): + from test_ui_filters import load_window + adapter = SimpleNamespace(capabilities=lambda: {op: Capability(True, 'available') + for op in ('add', 'update', 'remove')}) + result = result or failed_repair(tmp_path/'maintenance/result.json') + window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter, + verifier=lambda _: result, status_refresher=lambda names, path: {n: 'PENDING' for n in names}) + window.search_edit.setText('Beta') + window.repair_icdb_all() + qtbot.waitUntil(lambda: not window.busy) + return window, result + + +def test_failed_repair_offers_copy_import_bound_to_failed_target(qtbot, tmp_path, monkeypatch): + window, failed = recovery_window(qtbot, tmp_path, monkeypatch) + assert '副本' in window.summary.text() + action = next(b for b in window.summary.buttons() if '副本' in b.text()) + window.search_edit.setText('Alpha') + window.select_visible_button.click() + action.click() + dialog = window.management_dialog + requests = dialog.requests() + assert len(requests) == 1 and requests[0].name == 'Beta' + assert requests[0].partition == 'Power' and requests[0].operation == 'update' + assert requests[0].source is None and requests[0].auto_verify + assert '修复未完成' in dialog.status_label.text() + assert '手动备份' in dialog.status_label.text() + assert str(failed.evidence_path) in dialog.output.toPlainText() + dialog.reject() + assert failed.status == 'FAIL' + assert window.reimport_button.isEnabled() + + +def test_declining_reimport_confirmation_does_not_check_release_or_write(qtbot, tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + window, _ = recovery_window(qtbot, tmp_path, monkeypatch) + window.summary.close() + window.reimport_button.click() + dialog = window.management_dialog + source = tmp_path/'copy/test.prj'; source.parent.mkdir(); source.touch() + dialog.add_sources([source]) + monkeypatch.setattr(locks, 'release_retained_library', lambda *a: pytest.fail('unconfirmed release')) + confirmations = [] + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **kw: + confirmations.append(a[2]) or QMessageBox.No) + dialog.execute_button.click() + assert not dialog.busy and not dialog.executed + assert 'Beta' in confirmations[0] and str(source) in confirmations[0] + assert '副本' in confirmations[0] and '先移除' in confirmations[0] + dialog.reject() + + +@pytest.mark.parametrize('result', [VerifyResult('CHECKED'), VerifyResult('PASS', library_flag='GREEN'), + VerifyResult('BLOCKED', error_category='ICDB_DATABASE_IN_USE', safe_to_continue=False), + VerifyResult('FAIL', stderr='unknown error')]) +def test_no_reimport_offer_for_unconfirmed_diagnosis_or_success(qtbot, tmp_path, monkeypatch, result): + window, _ = recovery_window(qtbot, tmp_path, monkeypatch, result) + assert not window.reimport_button.isVisible() + assert not any('副本' in b.text() for b in window.summary.buttons()) + + +def test_changing_library_invalidates_old_reimport_offer(qtbot, tmp_path, monkeypatch): + window, _ = recovery_window(qtbot, tmp_path, monkeypatch) + old_action = next(b for b in window.summary.buttons() if '副本' in b.text()) + window.lmc_edit.setText('another.lmc') + old_action.click() + assert not getattr(window, 'management_dialog', None) + assert not window.reimport_button.isVisible() + + +def request(tmp_path): + library = tmp_path/'lib/mentor.lmc'; library.parent.mkdir(); library.touch() + source = tmp_path/'copy/Block.prj'; source.parent.mkdir(); source.touch() + return ManagementRequest('update', library, 'Block', source=source, evidence_dir=tmp_path/'management') + + +def test_release_failure_preserves_failure_and_never_dispatches_management(tmp_path, monkeypatch): + from cbb_refresh_verify.reimport_recovery import run_reimport_recovery + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.management as management + req = request(tmp_path) + monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block')) + def denied(*a): raise locks.LibraryBusy('PCB still active: 123') + monkeypatch.setattr(locks, 'release_retained_library', denied) + monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: pytest.fail('unsafe write')) + results = run_reimport_recovery([req], previous_evidence={'Block': tmp_path/'old/result.json'}, + evidence_dir=tmp_path/'reimport') + assert results[0].status == 'BLOCKED' and not results[0].safe_to_continue + assert '123' in results[0].detail + import json + record = json.loads(results[0].evidence_path.read_text('utf-8')) + assert record['previous_evidence']['Block'] == str(tmp_path/'old/result.json') + assert record['stage'] == 'release_failed' + + +def test_cancel_during_release_prevents_import(tmp_path, monkeypatch): + from cbb_refresh_verify.reimport_recovery import run_reimport_recovery + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.management as management + req = request(tmp_path); stop = Event() + monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block')) + def release(*a): + stop.set() + return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'} + monkeypatch.setattr(locks, 'release_retained_library', release) + monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: pytest.fail('cancelled write')) + results = run_reimport_recovery([req], previous_evidence={'Block': 'old.json'}, + evidence_dir=tmp_path/'reimport', stop=stop) + assert results == [] + + +@pytest.mark.parametrize('uploaded_status', ['UPLOADED', 'UPLOADED_VERIFY_FAILED', 'UPLOADED_VERIFIED']) +def test_copy_recovery_keeps_native_management_outcome_and_original_evidence(tmp_path, monkeypatch, uploaded_status): + from cbb_refresh_verify.reimport_recovery import run_reimport_recovery + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.management as management + import json + req = request(tmp_path) + old = tmp_path/'old.json'; old.write_text('{"status":"FAIL"}', encoding='utf-8') + before = old.read_bytes() + monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block')) + monkeypatch.setattr(locks, 'release_retained_library', lambda *a: + {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'}) + expected = ManagementResult('update', req.library, req.name, req.partition, req.source, + True, 'complete', uploaded_status, True) + monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: [expected]) + results = run_reimport_recovery([req], previous_evidence={'Block': str(old)}, + evidence_dir=tmp_path/'reimport') + assert results == [expected] and old.read_bytes() == before + record = json.loads(next((tmp_path/'reimport').glob('*/recovery.json')).read_text('utf-8')) + assert record['original_repair_succeeded'] is False + assert record['results'][0]['status'] == uploaded_status and record['version_review_required'] + + +def test_incomplete_copy_never_releases_or_removes_target(tmp_path, monkeypatch): + from cbb_refresh_verify.reimport_recovery import run_reimport_recovery + import cbb_refresh_verify.library_lock as locks + req = request(tmp_path) # Empty PRJ is deliberately incomplete. + monkeypatch.setattr(locks, 'release_retained_library', lambda *a: pytest.fail('incomplete source')) + results = run_reimport_recovery([req], previous_evidence={'Block': 'old.json'}, + evidence_dir=tmp_path/'reimport') + assert results[0].status == 'BLOCKED' and results[0].stage == 'preflight' + + +def test_recovery_uses_background_release_then_existing_management(qtbot, tmp_path, monkeypatch): + from cbb_refresh_verify.ui.management_dialog import ManagementDialog + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.management as management + req = request(tmp_path); entered, finish = Event(), Event(); calls = [] + monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block')) + def release(*a): + entered.set(); assert finish.wait(5) + calls.append('release') + return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'} + monkeypatch.setattr(locks, 'release_retained_library', release) + def run(items, **kw): + calls.append('management') + return [ManagementResult('update', req.library, req.name, req.partition, req.source, + True, 'complete', 'UPLOADED_VERIFIED', True)] + monkeypatch.setattr(management, 'run_management_batch', run) + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **kw: QMessageBox.Yes) + dialog = ManagementDialog([req], recovery_from={'Block': 'old.json'}) + qtbot.addWidget(dialog) + ticks = [] + timer = QTimer(); timer.setInterval(10); timer.timeout.connect(lambda: ticks.append(1)); timer.start() + try: + dialog.execute_button.click() + qtbot.waitUntil(entered.is_set) + qtbot.waitUntil(lambda: len(ticks) >= 3) + assert calls == [] and dialog.busy + finally: + finish.set() + qtbot.waitUntil(lambda: not dialog.busy) + timer.stop() + assert calls == ['release', 'management'] + assert '版本待核对' in dialog.status_label.text() + + +def test_recovery_dialog_keeps_internal_details_in_optional_log(qtbot, tmp_path): + from cbb_refresh_verify.ui.management_dialog import ManagementDialog + req = request(tmp_path) + dialog = ManagementDialog([req], recovery_from={'Block': 'old-result.json'}) + qtbot.addWidget(dialog); dialog.show() + assert dialog.output.isHidden() + assert not any(word in dialog.status_label.text() for word in ('原生', '证据', '检查点', '记录保留')) + detail = 'OWNER_QUERY_TIMEOUT: diagnostic internals' + result = ManagementResult('update', req.library, req.name, req.partition, req.source, + True, 'release_check', 'BLOCKED', False, detail, tmp_path/'recovery.json') + dialog._result([result]) + assert detail not in dialog.status_label.text() + assert '日志' in dialog.status_label.text() + dialog.logs_button.click() + assert dialog.output.isVisible() and detail in dialog.output.toPlainText() + + +def test_main_stop_during_recovery_release_cancels_import(qtbot, tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.management as management + window, _ = recovery_window(qtbot, tmp_path, monkeypatch) + window.summary.close(); window.reimport_button.click() + dialog = window.management_dialog + source = tmp_path/'copy/source.prj'; source.parent.mkdir(); source.touch() + dialog.add_sources([source]) + monkeypatch.setattr(management, 'source_project', lambda *a: (source, source, 'Beta')) + entered, finish = Event(), Event() + def release(*args): + entered.set(); assert finish.wait(5) + return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'} + monkeypatch.setattr(locks, 'release_retained_library', release) + dispatched = [] + monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: dispatched.append(a) or []) + dialog.execute_button.click() + try: + qtbot.waitUntil(entered.is_set) + window.stop_button.click() + assert dialog.stop.is_set() + finally: + finish.set(); qtbot.waitUntil(lambda: not dialog.busy) + dialog.reject() + assert dispatched == [] + -- 2.39.5 From 223655fe007a71e79db504a2ebe9b7d63c900325 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: Wed, 4 Nov 2026 17:06:27 +0800 Subject: [PATCH 50/59] fix: stabilize ICDB preflight and organize UI into tabs --- docs/releases/2026-11-04-tabs-and-release.md | 39 +++++++ src/cbb_refresh_verify/failed_session.py | 20 ++-- src/cbb_refresh_verify/maintenance_entry.py | 32 ++++-- src/cbb_refresh_verify/native_repair_ui.py | 45 ++++++-- src/cbb_refresh_verify/ui/main_window.py | 72 ++++++++----- tests/test_failed_startup.py | 20 ++++ tests/test_maintenance_entry.py | 42 +++++++- ...t_native_inspection_release_regressions.py | 102 ++++++++++++++++++ tests/test_native_occupancy.py | 4 +- tests/test_navigation_tabs.py | 33 ++++++ 10 files changed, 352 insertions(+), 57 deletions(-) create mode 100644 docs/releases/2026-11-04-tabs-and-release.md create mode 100644 tests/test_native_inspection_release_regressions.py create mode 100644 tests/test_navigation_tabs.py diff --git a/docs/releases/2026-11-04-tabs-and-release.md b/docs/releases/2026-11-04-tabs-and-release.md new file mode 100644 index 0000000..b9e9d93 --- /dev/null +++ b/docs/releases/2026-11-04-tabs-and-release.md @@ -0,0 +1,39 @@ +# 三页界面与原生检查、退出修正 + +界面按“检验 / ICDB 维护 / 中心库管理”横向切换,共用中心库、清单、搜索和选择状态。默认收起运行日志,长状态信息限制高度,避免纵向堆叠操作区或报错撑长窗口。1000×620 布局预览保存在本轮诊断目录中。 + +## 本轮现场日志定位 + +用户运行的是 `29959e2` 冻结包。2026-11-04 14:21(北京时间)的维护在原生检查启动后报 `(2, 'EnumWindows', '系统找不到指定的文件。')`,并没有调用 Repair。随后手动释放检查发现 `1GPHY_8212`、`IP101GR` 的地址记录仍存在;它们不能直接当作已释放或直接删除。第二次在准备检查时取消,助手没有及时结束文件扫描。 + +## 修正 + +- 在实际空私有桌面复现了 Win32 旧错误码问题:先 `SetLastError(2)` 再枚举空桌面,pywin32 会报与现场相同的错误。现在枚举前清零旧错误;真实枚举异常继续上报,不把未知错误当作空闲。 +- 将取消信号传入修复助手的文件扫描。文件标记最多每 100 毫秒读取一次,取消后锁存;原生启动、Repair 命令和确认前仍立即读取取消信号。冻结实测曾因逐文件读取取消标记而在启动前超时,节流后已推进至实际原生检查和 Repair。 +- 手动备份模式下,助手只在启动前执行一次完整目标保护,移除原先自动备份取消后留下的重复检查。同一保护内复用初始完整扫描作地址和文件选择,取得全部独占读句柄后再做第二次完整扫描;地址变化或原生文件集合增删均停止。没有复用跨操作的旧快照。 +- 发出唯一正常关闭请求前仍严格确认任务进程、工程、私有桌面、空闲窗口和地址状态。关闭后只等待进程、启动器及地址释放,不再因退出途中 WMI 暂时缺少命令行而提前报身份未确认,也不再次关闭窗口。未实际退出仍保留未释放状态。 + +## 已有验证记录 + +本轮证据目录为 `diagnostics/tabs-release-20261104/`,含本机路径,不提交原始数据。 + +- 初轮完整回归 1,200 项通过,2 项旧测试替身未接收取消参数;同步接口后维护模块 48 项通过。 +- 扫描优化后的维护、原生修复等相关测试 225 项通过;随后退出等待等 91 项通过。包含实际私有桌面的 Win32 回归,而不只是窗口模拟。 +- 冻结程序取消试验 `frozen-cancel-local`:预检查中取消约 0.61 秒返回,未启动原生 Repair,应用正常退出。 +- 源码实际入口 `source-optimized`:原生确认未占用并要求修复,Repair 成功,按用户授权恢复最后手动保存版本;最终因 verified/once-verified CBB 禁止 BackAnnotate 失败。PCB 正常关闭,没有附加 PCB 释放未确认错误。 +- 冻结实际入口 `frozen-polling`:原生检查及 Repair 已完成,取消标记只读取 214 次、耗时 0.313 秒。随后 PCB 因数据库服务器启动晚于客户端截止时间而加载失败。服务器日志显示它后来实际启动并自行关闭,不据此宣称完整修复通过。 +- 同一次失败退出中,先记录了 `startup_close_requested`,随后 WMI 缺少进程信息导致 `STARTUP_PROCESS_UNCONFIRMED`,下一秒却已确认进程退出;该具体时序已转为回归测试。 + +最终完整回归:**1,207 passed**,588.26 秒,仅既有 pywinauto STA 警告;记录在 `pytest-final.log` / `pytest-final-result.json`。最终冻结构建、启动退出冒烟和 6 个关键模块的源码一致性检查通过。独立审查未发现剩余必要缺陷。 + +最终冻结实际入口为 `frozen-local-final`,使用 `C:\Temp\CbbTabsRelease20261104\library` 的独立副本:原生 `NOT_IN_USE` 且明确要求修复 → Repair 完成并释放 → 重新打开同一 PCB → 用户已授权的最后手动保存版本(2026/9/24 15:15)→ SynchCES / LoadCES → BackAnnotate 被原生拒绝。最终 **FAIL / UNKNOWN / REPAIR_VALIDATION_FAILED**,未取得 READY;退出记录确认进程结束,没有附加 `PCB_RELEASE_UNCONFIRMED`。应用正常退出返回 0。此前服务器迟启动的失败记录保留,不能从一次对照断定其全部成因。 + +原库 255 个已记录主要文件哈希复核全部未变,见 `source-final-check.json`。 + +最终包的 `frozen-cancel-final` 再次验证取消:约 1.16 秒返回,检查助手报告 `CANCELLED`,批次保持 `BLOCKED`,未启动 Repair,应用正常退出。取消只证明操作已停止,不计作修复成功。 + +## 验收限制 + +原库未执行本轮自动化写入。第一套 F 盘测试副本出现目录枚举属性与直接读取属性不一致,被链接检查阻止,未绕过检查。测试随后迁移到独立本机目录;`C:\Users` 目录会被 Mentor 转成共享路径,因此最终试验选择独立的 `C:\Temp` 路径,并保留前序失败记录。 + +完整 ICDB 修复仍必须取得权威 READY;原生 Repair 成功、能够取消、正常关闭或界面测试通过都不能替代该验收。未合并发布,工单保持未解决。 diff --git a/src/cbb_refresh_verify/failed_session.py b/src/cbb_refresh_verify/failed_session.py index 6f233fb..3c3225f 100644 --- a/src/cbb_refresh_verify/failed_session.py +++ b/src/cbb_refresh_verify/failed_session.py @@ -32,7 +32,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: session. Unknown windows, live database addresses and other desktops are retained. WM_CLOSE is cooperative; no save prompt is accepted or killed. """ - import win32con, win32gui, win32process + import win32api, win32con, win32gui, win32process from .mentor_adapter import _process_details from .native_repair_ui import _current_desktop_name from .icdb_ownership import unreleased_addresses @@ -86,18 +86,19 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: break time.sleep(.2) continue - _, executable, command, _ = records[0] - args = _command_args(command) - if (Path(executable).name.casefold() != 'expeditionpcb.exe' - or '-reusablecircuit' not in [a.casefold() for a in args] - or expected not in [os.path.normcase(os.path.abspath(a)) for a in args[1:]]): - raise ValueError('STARTUP_PROCESS_UNCONFIRMED') if not sent: + _, executable, command, _ = records[0] + args = _command_args(command) + if (Path(executable).name.casefold() != 'expeditionpcb.exe' + or '-reusablecircuit' not in [a.casefold() for a in args] + or expected not in [os.path.normcase(os.path.abspath(a)) for a in args[1:]]): + raise ValueError('STARTUP_PROCESS_UNCONFIRMED') windows = [] def observe(hwnd, _): if (win32process.GetWindowThreadProcessId(hwnd)[1] == pcb_pid and win32gui.IsWindowVisible(hwnd)): windows.append(hwnd) + win32api.SetLastError(0) win32gui.EnumWindows(observe, None) main = [w for w in windows if win32gui.GetClassName(w).startswith('Afx:') and win32gui.GetWindowText(w).strip() in ( @@ -112,6 +113,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: if window['class'] == 'Internet Explorer_Hidden' and window['title'] == '': window['rect'] = list(win32gui.GetWindowRect(window['hwnd'])) window['children'] = [] + win32api.SetLastError(0) win32gui.EnumChildWindows(window['hwnd'], lambda hwnd, _: window['children'].append(int(hwnd)), None) left, top, right, bottom = window['rect'] @@ -126,6 +128,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: and win32gui.GetWindow(hwnd, win32con.GW_CHILD)): documents.append(hwnd) if len(main) == 1: + win32api.SetLastError(0) win32gui.EnumChildWindows(main[0], child, None) addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb']) if len(main) == 1 and not unknown and not documents and not addresses: @@ -135,6 +138,9 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: # Observe exit even when the caller requested no extra wait. continue result['detail'] = 'STARTUP_IDLE_UNCONFIRMED: 等待空闲窗口和数据库释放' + # After the single owned WM_CLOSE, WMI may lose the executable/ + # command fields before removing the PID. Only observe exit here; + # never issue another close or treat missing fields as release. if time.monotonic() >= deadline: if sent: result['detail'] = 'STARTUP_EXIT_UNCONFIRMED: 已请求正常关闭,但 PCB 尚未退出' diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index ac3626d..09b7f5e 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -9,6 +9,7 @@ import json import os from pathlib import Path, PureWindowsPath import re +import stat from uuid import uuid4 @@ -43,11 +44,14 @@ def address_snapshot(paths): for path in paths} -def _scoped_addresses(request, *, check=None): +def _scoped_addresses(request, *, check=None, entries=None): from .scoped_manifest import scan_tree root = Path(request.lmc_file).parent addresses = [] - for path in scan_tree(root, check=check): + 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) @@ -81,7 +85,7 @@ def maintenance_project_files(project, pcb): return (project,) -def select_maintenance_target(request, *, check=None): +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 @@ -109,7 +113,7 @@ def select_maintenance_target(request, *, check=None): 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) + 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. @@ -137,7 +141,11 @@ def maintenance_target_guard(request, expected=None, *, stop=None): if stop is not None and stop.is_set(): raise ValueError('MAINTENANCE_CANCELLED: 已取消占用检查') check() - target, project, pcb, addresses = select_maintenance_target(request, check=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()), @@ -148,17 +156,21 @@ def maintenance_target_guard(request, expected=None, *, stop=None): raise ValueError('MAINTENANCE_SOURCE_CHANGED: 目标或地址记录在检查后变化') assert_native_idle() check() - entries = scan_tree(Path(request.lmc_file).parent, check=check) - files = [path for path in entries if ( - path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')) and path.is_file()] + 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 files: + 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_addresses = _scoped_addresses(request, check=check) + 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} diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index efc52da..77f972f 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -98,7 +98,7 @@ def _repair_command(executable): def _windows(pid): - import win32gui, win32process + import win32api, win32gui, win32process windows = [] def observe(hwnd, _): if win32process.GetWindowThreadProcessId(hwnd)[1] != pid or not win32gui.IsWindowVisible(hwnd): @@ -109,10 +109,14 @@ def _windows(pid): children.append({'hwnd': handle, 'text': win32gui.GetWindowText(handle), 'class': win32gui.GetClassName(handle), 'id': win32gui.GetDlgCtrlID(handle), 'enabled': bool(win32gui.IsWindowEnabled(handle))}) + # Empty window lists are valid. pywin32 otherwise interprets a stale + # thread error (observed ERROR_FILE_NOT_FOUND) as an enum failure. + win32api.SetLastError(0) win32gui.EnumChildWindows(hwnd, child, None) windows.append({'hwnd': hwnd, 'title': win32gui.GetWindowText(hwnd), 'class': win32gui.GetClassName(hwnd), 'enabled': bool(win32gui.IsWindowEnabled(hwnd)), 'children': children}) + win32api.SetLastError(0) win32gui.EnumWindows(observe, None) return windows @@ -226,12 +230,35 @@ def _inspect_project(job, process, emit): return result +class _CancellationFile: + def __init__(self, path): + self.path = Path(path) + self._next_poll = 0.0 + self._cancelled = False + self.polls = 0 + self.poll_seconds = 0.0 + + def is_set(self): + # scan_tree checks between every entry. Avoid issuing tens of + # thousands of filesystem requests just to observe one IPC flag. + # The direct checks before Popen/Repair remain immediate. + now = time.monotonic() + if not self._cancelled and now >= self._next_poll: + self._cancelled = self.path.exists() + finished = time.monotonic() + self.polls += 1 + self.poll_seconds += finished-now + self._next_poll = finished + .1 + return self._cancelled + + def repair_worker_main(job_path): import win32con, win32gui from .native_repair import RepairTarget, check_repair_access from .mentor_paths import discover_sdd_home from .icdb_recovery import snapshot_log job = json.loads(Path(job_path).read_text('utf-8')) + stop = _CancellationFile(job['cancel_signal']) root = Path(job['root']) project, database = Path(job['project']), Path(job['database']) result = {'status': 'BLOCKED', 'project': str(project), 'database': str(database), @@ -265,17 +292,21 @@ def repair_worker_main(job_path): maintenance_request = VerifyRequest(Path(maintenance_context['library']), maintenance_context['block'], root, maintenance=True) emit(stage='native_preflight', detail='正在核对本次原生工具的目标及占用') - with maintenance_target_guard(maintenance_request, maintenance_context) as checked: - if (checked['target'].project.resolve() != project.resolve() - or checked['target'].database.resolve() != database.resolve()): - raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED') check_repair_access(RepairTarget(project, database)) emit(stage='backup_skipped', reason='manual_backup_policy', detail='应用不自动备份;Mentor 原生修复仍可能生成自身备份。') if Path(job['cancel_signal']).exists(): raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') if maintenance_context is not None: - with maintenance_target_guard(maintenance_request, maintenance_context): pass + # Manual backup policy leaves no mutation between preparation and + # launch. Run the complete guard once here, retaining its identity, + # address and exclusive-access checks immediately before Popen. + with maintenance_target_guard(maintenance_request, maintenance_context, stop=stop) as checked: + if (checked['target'].project.resolve() != project.resolve() + or checked['target'].database.resolve() != database.resolve()): + raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED') + emit(stage='native_preflight_complete', cancellation_polls=stop.polls, + cancellation_poll_seconds=round(stop.poll_seconds, 3)) if Path(job['cancel_signal']).exists(): raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt') @@ -391,6 +422,8 @@ def repair_worker_main(job_path): detail='原生 Repair 未取得完整成功/释放证据,未重试校验') except Exception as error: result.update(detail=str(error), status='RETAINED' if process and process.poll() is None else 'BLOCKED') + result['preflight_cancellation'] = {'polls': stop.polls, + 'seconds': round(stop.poll_seconds, 3)} # Never terminate a repair or its database owner. Retained PID/desktop is # reported to the parent, including unknown native prompts. (root/'repair-result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8') diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index d0e8b94..4994d4f 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -9,7 +9,8 @@ from PySide6.QtCore import QObject, QThreadPool, Signal, Qt from PySide6.QtGui import QColor, QIcon from PySide6.QtWidgets import (QWidget, QLineEdit, QPushButton, QFormLayout, QTextEdit, QFileDialog, QHBoxLayout, QLabel, QSpinBox, QMessageBox, QToolButton, - QTableWidget, QTableWidgetItem, QHeaderView, QCheckBox, QGroupBox, QVBoxLayout, QProgressBar, QComboBox) + QTableWidget, QTableWidgetItem, QHeaderView, QCheckBox, QVBoxLayout, QProgressBar, QComboBox, + QTabBar, QStackedWidget, QSizePolicy) from PySide6.QtWidgets import QStyle try: from dx_productivity.trial_task_center.ui.workers import ServiceWorker @@ -170,41 +171,54 @@ class MainWindow(QWidget): self.status_label = QLabel('请选择并读取中心库。校验期间可以使用其他前台应用。') self.status_label.setWordWrap(True) self.status_label.setMinimumHeight(self.status_label.fontMetrics().lineSpacing()*2) + self.status_label.setMaximumHeight(self.status_label.fontMetrics().lineSpacing()*3 + 4) + self.status_label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) self.output = QTextEdit(); self.output.setReadOnly(True) # Integration emits repeated status snapshots. Keep the visible # diagnostic buffer bounded so a 3,000-CBB run cannot grow the GUI # document without limit. self.output.document().setMaximumBlockCount(2000) - layout = QFormLayout(self) - row = QHBoxLayout(); row.addWidget(self.lmc_edit); row.addWidget(self.browse_button); row.addWidget(self.load_button) - layout.addRow('中心库 .lmc', row) + self.output.setMaximumHeight(180) + layout = QVBoxLayout(self) + row = QHBoxLayout(); row.addWidget(QLabel('中心库')); row.addWidget(self.lmc_edit) + row.addWidget(self.browse_button); row.addWidget(self.load_button) + layout.addLayout(row) self.library_notice = QLabel() self.library_notice.setWordWrap(True) self.library_notice.setTextFormat(Qt.PlainText) self.library_notice.setStyleSheet('QLabel { color: #8a4b08; background: #fff3d6; padding: 6px; }') self.library_notice.hide() - layout.addRow(self.library_notice) - layout.addRow(self.advanced_toggle); layout.addRow(self.advanced_panel) + layout.addWidget(self.library_notice) + self.section_tabs = QTabBar() + self.section_tabs.setExpanding(True) + for name in ('检验', 'ICDB 维护', '中心库管理'): + self.section_tabs.addTab(name) + layout.addWidget(self.section_tabs) filters = QHBoxLayout() for widget in (self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button): filters.addWidget(widget) - layout.addRow('搜索 / 筛选', filters) - layout.addRow(self.counts_label) - layout.addRow('CBB 清单(勾选后可批量)', self.cbb_table); layout.addRow('超时', self.timeout_spin) - layout.addRow(QLabel('逐项校验 CBB,完成后刷新状态。')) + layout.addLayout(filters) + layout.addWidget(self.counts_label) + layout.addWidget(self.cbb_table, 1) + self.section_stack = QStackedWidget() + self.section_stack.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum) + self.section_tabs.currentChanged.connect(self.section_stack.setCurrentIndex) + verify_page = QWidget() + verify_layout = QVBoxLayout(verify_page); verify_layout.setContentsMargins(0, 0, 0, 0) controls = QHBoxLayout() - for widget in (self.selected_batch_button, self.batch_button, self.stop_button, self.logs_button): controls.addWidget(widget) - layout.addRow(controls) - maintenance = QGroupBox('数据库维护') + for widget in (self.selected_batch_button, self.batch_button): controls.addWidget(widget) + controls.addStretch(); controls.addWidget(QLabel('超时')); controls.addWidget(self.timeout_spin) + controls.addWidget(self.advanced_toggle) + verify_layout.addLayout(controls); verify_layout.addWidget(self.advanced_panel) + self.section_stack.addWidget(verify_page) + maintenance = QWidget() maintenance_layout = QVBoxLayout(maintenance) + maintenance_layout.setContentsMargins(0, 0, 0, 0) maintenance_row = QHBoxLayout() maintenance_row.addWidget(self.icdb_repair_button) - description = QLabel('处理可见非 READY 项;有勾选时仅处理勾选中的可用项。未确认不等于损坏。\n操作前请手动备份;应用不自动备份。无法确认会话安全时停止。') - description.setWordWrap(True) - maintenance_row.addWidget(description, 1) + maintenance_row.addWidget(self.backup_restore_button) + maintenance_row.addWidget(self.reimport_button) maintenance_layout.addLayout(maintenance_row) - maintenance_layout.addWidget(self.backup_restore_button) - maintenance_layout.addWidget(self.reimport_button) self.icdb_progress = QProgressBar() self.icdb_progress.setVisible(False) self.icdb_phase = QLabel('') @@ -212,12 +226,13 @@ class MainWindow(QWidget): self.icdb_phase.setVisible(False) maintenance_layout.addWidget(self.icdb_progress) maintenance_layout.addWidget(self.icdb_phase) - layout.addRow(maintenance) - management = QGroupBox('CBB 管理(可处理 READY 项)') + self.section_stack.addWidget(maintenance) + management = QWidget() management_layout = QVBoxLayout(management) + management_layout.setContentsMargins(0, 0, 0, 0) management_buttons = QHBoxLayout() self.add_button = QPushButton('新增 CBB…') - self.update_button = QPushButton('用工程替换已选 CBB…(原生重新导入)') + self.update_button = QPushButton('用工程替换已选 CBB…') self.remove_button = QPushButton('移除已选…') for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): widget.clicked.connect(lambda _=False, op=operation: self.manage_cbb(op)) @@ -229,17 +244,20 @@ class MainWindow(QWidget): management_layout.insertWidget(0, self.management_capability_label) self.unfinished_label = QLabel(); self.unfinished_label.setWordWrap(True) management_layout.addWidget(self.unfinished_label) - self.release_library_button = QPushButton('检查会话释放并解除协调阻止…') + self.release_library_button = QPushButton('检查占用…') self.release_library_button.clicked.connect(self.check_library_release) - management_layout.addWidget(self.release_library_button) - layout.addRow(management) - layout.addRow(self.status_label) + management_buttons.addWidget(self.release_library_button) + self.section_stack.addWidget(management) + layout.addWidget(self.section_stack) + layout.addWidget(self.status_label) self.runtime_log_toggle = QToolButton(); self.runtime_log_toggle.setText('查看运行日志') self.runtime_log_toggle.setCheckable(True) self.runtime_log_toggle.toggled.connect(self.output.setVisible) self.output.hide() - layout.addRow(self.runtime_log_toggle); layout.addRow(self.output) - layout.addRow(self.recovery_button) + footer = QHBoxLayout() + for widget in (self.stop_button, self.runtime_log_toggle, self.logs_button, self.recovery_button): + footer.addWidget(widget) + layout.addLayout(footer); layout.addWidget(self.output) self.lmc_edit.textChanged.connect(self._invalidate) for field in (self.libspec_edit, self.dms_edit, self.mentor_bin_edit): field.textChanged.connect(lambda _text: self._update()) diff --git a/tests/test_failed_startup.py b/tests/test_failed_startup.py index 0062239..a64fe2f 100644 --- a/tests/test_failed_startup.py +++ b/tests/test_failed_startup.py @@ -90,6 +90,26 @@ def test_requested_close_without_exit_is_not_reported_closed(tmp_path, monkeypat assert records +@pytest.mark.parametrize('exits', [True, False]) +def test_after_owned_close_transient_empty_command_waits_for_exit(tmp_path, monkeypatch, exits): + from types import SimpleNamespace + import win32gui + job, records, messages = startup_fixture(tmp_path, monkeypatch) + ticks = [0.0] + def post(hwnd, message, *_): + messages.append((hwnd, message)) + records[0] = (12, '', '', 11) # WMI during process teardown. + def advance(seconds): + ticks[0] += seconds + if exits: records.clear() + monkeypatch.setattr(win32gui, 'PostMessage', post) + monkeypatch.setattr(failed_session, 'time', SimpleNamespace(monotonic=lambda: ticks[0], sleep=advance)) + result = failed_session.close_failed_startup(job, 12, timeout_seconds=1) + assert ticks[0] > 0 and len(messages) == 1 + assert result['status'] == ('CLOSED' if exits else 'RETAINED') + assert 'STARTUP_PROCESS_UNCONFIRMED' not in result.get('detail', '') + + @pytest.mark.parametrize('obstacle', ['address', 'dialog', 'process']) def test_startup_close_waits_for_native_state_transitions(tmp_path, monkeypatch, obstacle): from types import SimpleNamespace diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index ca6af62..13844c3 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -115,6 +115,33 @@ def test_addressless_target_binds_unique_project_and_rejects_new_address(case): with maintenance_target_guard(request, context): pass +def test_guard_reuses_full_tree_scan_but_still_refreshes_before_yield(case, monkeypatch): + from cbb_refresh_verify.maintenance_entry import maintenance_target_guard + import cbb_refresh_verify.scoped_manifest as manifests + request, _, _ = case + scans = [] + original = manifests.scan_tree + def scan(root, **kwargs): + scans.append(Path(root)) + return original(root, **kwargs) + monkeypatch.setattr(manifests, 'scan_tree', scan) + with maintenance_target_guard(request): pass + assert scans.count(request.lmc_file.parent) == 2 + + +def test_guard_rejects_new_native_database_appearing_during_checks(case, monkeypatch): + import cbb_refresh_verify.maintenance_entry as entry + request, _, _ = case + calls = [] + def idle(): + calls.append(1) + if len(calls) == 2: + (request.lmc_file.parent/'new.lmc').write_bytes(b'new native store') + monkeypatch.setattr(entry, 'assert_native_idle', idle) + with pytest.raises(ValueError, match='MAINTENANCE_SOURCE_CHANGED'): + with entry.maintenance_target_guard(request): pass + + @pytest.mark.parametrize('reference', ['', 'KEY ProjectFile "../Other.prj"\n', 'KEY ProjectFile "../IP101GR.prj"\nKEY ProjectFile "../IP101GR.prj"\n']) def test_addressless_target_requires_exact_pcb_back_reference(case, reference): @@ -284,7 +311,8 @@ def test_explicit_worker_requires_positive_native_idle_before_repair(tmp_path, m values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'} job.write_text(json.dumps(values)) @contextmanager - def guard(*args): + def guard(*args, stop=None): + assert stop is not None yield {'target': RepairTarget(Path(values['project']), Path(values['database']))} monkeypatch.setattr(entry, 'maintenance_target_guard', guard) monkeypatch.setattr(ui, '_native_output', lambda windows: output) @@ -293,7 +321,7 @@ def test_explicit_worker_requires_positive_native_idle_before_repair(tmp_path, m assert any(message == win32con.WM_COMMAND for _, message in actions) is expected -def test_worker_rechecks_address_after_complete_backup_before_native_launch(tmp_path, monkeypatch): +def test_worker_rechecks_address_after_access_probe_before_native_launch(tmp_path, monkeypatch): from test_native_repair import repair_worker_fixture import cbb_refresh_verify.maintenance_entry as entry from cbb_refresh_verify.native_repair import RepairTarget @@ -301,15 +329,19 @@ def test_worker_rechecks_address_after_complete_backup_before_native_launch(tmp_ values = json.loads(job.read_text()); values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'} job.write_text(json.dumps(values)) calls = [] + monkeypatch.setattr('cbb_refresh_verify.native_repair.check_repair_access', + lambda *_: calls.append('access')) @contextmanager - def guard(*args): + def guard(*args, stop=None): + assert stop is not None calls.append('guard') - if len(calls) == 2: raise ValueError('address changed during complete backup') + assert calls == ['access', 'guard'] + raise ValueError('address changed after access probe') yield {'target': RepairTarget(Path(values['project']), Path(values['database']))} monkeypatch.setattr(entry, 'maintenance_target_guard', guard) monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **k: pytest.fail('mutation must stop native launch')) ui.repair_worker_main(job) - assert calls == ['guard', 'guard'] + assert calls == ['access', 'guard'] assert 'address changed' in json.loads((root/'repair-result.json').read_text())['detail'] diff --git a/tests/test_native_inspection_release_regressions.py b/tests/test_native_inspection_release_regressions.py new file mode 100644 index 0000000..f139de3 --- /dev/null +++ b/tests/test_native_inspection_release_regressions.py @@ -0,0 +1,102 @@ +import json +from pathlib import Path +import sys +from uuid import uuid4 + +import pytest + + +def test_empty_private_desktop_does_not_inherit_stale_windows_error(tmp_path): + from cbb_refresh_verify.desktop_worker import DesktopWorker + script = tmp_path/'enumerate.py' + result = tmp_path/'result.json' + script.write_text( + 'import sys,json,win32api,win32gui,win32process\nfrom pathlib import Path\n' + f'sys.path.insert(0,{str(Path("src").resolve())!r})\n' + 'from cbb_refresh_verify.native_repair_ui import _windows\n' + 'win32api.SetLastError(2)\n' + 'try:\n record={"windows":_windows(999999)}\n' + 'except Exception as error:\n record={"error":str(error)}\n' + f'Path({str(result)!r}).write_text(json.dumps(record))\n', encoding='utf-8') + worker = DesktopWorker([sys.executable, str(script)], 'CbbProbe_' + uuid4().hex[:12]) + try: + assert worker.wait(timeout=20) == 0 + finally: + worker.close() + assert json.loads(result.read_text()) == {'windows': []} + + +def test_real_window_enumeration_errors_still_propagate(monkeypatch): + import win32gui, pywintypes + from cbb_refresh_verify.native_repair_ui import _windows + def denied(*args): raise pywintypes.error(5, 'EnumWindows', 'denied') + monkeypatch.setattr(win32gui, 'EnumWindows', denied) + with pytest.raises(pywintypes.error): _windows(123) + + +def test_cancel_reaches_prelaunch_scan(tmp_path, monkeypatch): + from contextlib import contextmanager + from test_native_repair import repair_worker_fixture + from cbb_refresh_verify.native_repair import RepairTarget + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + data = json.loads(job.read_text('utf-8')) + data['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'} + job.write_text(json.dumps(data), encoding='utf-8') + calls, interrupted = [], [] + @contextmanager + def guard(*args, stop=None, **kwargs): + calls.append(1) + assert stop is not None, 'worker scan has no cancellation input' + if len(calls) == 1: + Path(data['cancel_signal']).touch() + assert stop.is_set() + interrupted.append(1) + raise ValueError('MAINTENANCE_CANCELLED') + yield {'target': RepairTarget(Path(data['project']), Path(data['database']))} + monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard) + monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('cancelled native launch')) + assert ui.repair_worker_main(job) == 1 + assert interrupted == [1] and not actions + + +def test_worker_uses_one_complete_guard_immediately_before_native_launch(tmp_path, monkeypatch): + from contextlib import contextmanager + from test_native_repair import repair_worker_fixture + from cbb_refresh_verify.native_repair import RepairTarget + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + data = json.loads(job.read_text('utf-8')) + data['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'} + job.write_text(json.dumps(data), encoding='utf-8') + order = [] + @contextmanager + def guard(*args, stop=None, **kwargs): + assert stop is not None + order.append('guard') + yield {'target': RepairTarget(Path(data['project']), Path(data['database']))} + order.append('guard_complete') + monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard) + popen = ui.subprocess.Popen + def launch(*args, **kwargs): + order.append('launch') + return popen(*args, **kwargs) + monkeypatch.setattr(ui.subprocess, 'Popen', launch) + ui.repair_worker_main(job) + assert order == ['guard', 'guard_complete', 'launch'] + + +def test_file_cancellation_polling_is_bounded_and_latches_cancel(tmp_path, monkeypatch): + from types import SimpleNamespace + import cbb_refresh_verify.native_repair_ui as ui + now = [1.0] + monkeypatch.setattr(ui, 'time', SimpleNamespace(monotonic=lambda: now[0])) + signal = ui._CancellationFile(tmp_path/'cancel.signal') + reads = [] + signal.path = SimpleNamespace(exists=lambda: reads.append(1) or len(reads) > 1) + for _ in range(50000): + assert not signal.is_set() + assert len(reads) == 1 + now[0] += .11 + assert signal.is_set() + signal.path = SimpleNamespace(exists=lambda: pytest.fail('cancel must stay latched')) + now[0] += 1 + assert signal.is_set() diff --git a/tests/test_native_occupancy.py b/tests/test_native_occupancy.py index 43d33d8..ccfa24a 100644 --- a/tests/test_native_occupancy.py +++ b/tests/test_native_occupancy.py @@ -164,11 +164,11 @@ def test_cancel_during_final_guard_cannot_launch_native_tool(tmp_path, monkeypat def guard(*a, **kw): calls.append(True) yield {'target': RepairTarget(Path(data['project']), Path(data['database']))} - if len(calls) == 2: Path(data['cancel_signal']).touch() + Path(data['cancel_signal']).touch() monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard) monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('late native launch')) assert ui.repair_worker_main(job) == 1 - assert len(calls) == 2 and not actions + assert len(calls) == 1 and not actions def test_verified_cbb_refusal_is_not_hidden_by_secondary_release_failure(): diff --git a/tests/test_navigation_tabs.py b/tests/test_navigation_tabs.py new file mode 100644 index 0000000..35c1502 --- /dev/null +++ b/tests/test_navigation_tabs.py @@ -0,0 +1,33 @@ +from PySide6.QtCore import Qt + + +def test_three_horizontal_sections_keep_one_catalog_and_only_their_actions(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch) + window.show() + assert [window.section_tabs.tabText(i) for i in range(window.section_tabs.count())] == [ + '检验', 'ICDB 维护', '中心库管理'] + window.search_edit.setText('Beta') + window.select_visible_button.click() + for index, visible, hidden in ( + (0, window.batch_button, window.icdb_repair_button), + (1, window.icdb_repair_button, window.add_button), + (2, window.add_button, window.batch_button), + ): + window.section_tabs.setCurrentIndex(index) + assert visible.isVisible() and not hidden.isVisible() + assert window.cbb_table.isVisible() + assert window.search_edit.text() == 'Beta' + assert [record.name for record in window.selected_records()] == ['Beta'] + assert window.minimumSizeHint().height() <= 650 + + +def test_long_status_does_not_expand_the_window(qtbot, tmp_path): + from cbb_refresh_verify.ui.main_window import MainWindow + window = MainWindow(data_root=tmp_path) + qtbot.addWidget(window) + window.resize(1000, 650); window.show() + window.status_label.setText('错误说明\n' * 80) + qtbot.wait(20) + assert window.height() <= 650 + assert window.status_label.height() <= window.status_label.fontMetrics().lineSpacing() * 3 + 4 -- 2.39.5 From 2587d33c5adf93f871a75415c67d32b297610d9c 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: Wed, 4 Nov 2026 17:26:35 +0800 Subject: [PATCH 51/59] fix: keep maintenance results concise and catalog readable --- docs/releases/2026-11-04-tabs-and-release.md | 6 ++++ src/cbb_refresh_verify/ui/main_window.py | 33 ++++++++++++-------- tests/test_maintenance_ui.py | 8 +++-- tests/test_navigation_tabs.py | 10 ++++++ tests/test_ui.py | 6 ++-- 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/docs/releases/2026-11-04-tabs-and-release.md b/docs/releases/2026-11-04-tabs-and-release.md index b9e9d93..abda574 100644 --- a/docs/releases/2026-11-04-tabs-and-release.md +++ b/docs/releases/2026-11-04-tabs-and-release.md @@ -37,3 +37,9 @@ 原库未执行本轮自动化写入。第一套 F 盘测试副本出现目录枚举属性与直接读取属性不一致,被链接检查阻止,未绕过检查。测试随后迁移到独立本机目录;`C:\Users` 目录会被 Mentor 转成共享路径,因此最终试验选择独立的 `C:\Temp` 路径,并保留前序失败记录。 完整 ICDB 修复仍必须取得权威 READY;原生 Repair 成功、能够取消、正常关闭或界面测试通过都不能替代该验收。未合并发布,工单保持未解决。 + +## 结果展示收尾 + +实际冻结运行截图还暴露了结果弹窗包含长路径和 COM 异常、错误列挤压 CBB 名称的问题。结果正文改为简短计数、用户可理解的失败原因和副本恢复入口;原始异常、统计明细及日志路径保留在可展开详情和日志中。名称与结果列共同分配可用宽度,长内容不再把表格撑出窗口。历史备份待核对和待选 PCB 版本仍直接提示。 + +该收尾只调整展示,不改变占用检查、关闭、Repair、校验或恢复行为。以已保存的真实失败结果回放检查布局,未额外写入原生库。相关界面、筛选和副本恢复回归 84 项通过;记录为 `pytest-presentation.log` / `pytest-presentation-result.json`。原先完整回归 1,207 项及冻结原生流程的失败结论仍分别保留,不能把展示回放当作新增原生验收。 diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 4994d4f..b151c1e 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -123,7 +123,8 @@ class MainWindow(QWidget): self.cbb_table = QTableWidget(0, 4) self.cbb_table.setHorizontalHeaderLabels(['CBB 名称', '分区', '状态', '环境检查']) self.cbb_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) - for column in (1, 2, 3): + self.cbb_table.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch) + for column in (1, 2): self.cbb_table.horizontalHeader().setSectionResizeMode(column, QHeaderView.ResizeToContents) self.cbb_table.setSelectionBehavior(QTableWidget.SelectRows) self.cbb_table.setMinimumHeight(150) @@ -963,26 +964,31 @@ class MainWindow(QWidget): self.icdb_phase.setText('已停止,存在待处理项目' if failed or skipped else '已完成非 READY 检查') if any(not result.safe_to_continue for result in results): text += '\n未确认数据库或会话安全,已停止剩余校验。请查看恢复现场。' - status_text = text - if self._global_icdb_run: - status_text = (f'非 READY 检查结束:通过 {passed},失败/受阻 {failed},未执行 {skipped}。\n' - f'仅检查、未修复 {counts["checked_without_repair"]};' - f'原生修复 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' - f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' - f'修复未确认 {counts["repair_unconfirmed"]}。') + status_text = (f'已结束:通过 {passed},失败/受阻 {failed},' + f'仅检查 {checked},未执行 {skipped + len(self.skipped_names)}。') + if self._global_icdb_run and counts['review_required']: + status_text += f'\n恢复备份待复核 {counts["review_required"]},请核对工程版本。' + if self._global_icdb_run and counts['version_choice_required']: + status_text += f'\n待选择 PCB 版本 {counts["version_choice_required"]}。' first_failure = next((result.stderr for result in results if result.status not in ('PASS', 'SKIPPED') and result.stderr), '') if first_failure: - explanation = '\n首个未完成原因:' + first_failure[:1500] - status_text += explanation - text += explanation + text += '\n首个未完成原因:' + first_failure + if any(result.error_category == 'CBB_REIMPORT_REQUIRED' for result in results): + status_text += '\nMentor 不允许对此已验证过的 CBB 反标注,校验未通过。' + else: + status_text += '\n部分项目未完成,请查看详情。' + if any(not result.safe_to_continue for result in results): + status_text += '\n后续操作已暂停。' if self._reimport_offers: guidance = '\n修复未完成,可选择工程副本恢复。' text += guidance status_text += guidance - self.status_label.setText(status_text); self.output.append(text) + self.status_label.setText(status_text.replace('\n', ' ')); self.output.append(text) self.summary = QMessageBox(self) - self.summary.setWindowTitle('非 READY ICDB 检查与修复结果' if self._global_icdb_run else 'CBB 校验结果'); self.summary.setText(text) + self.summary.setWindowTitle('ICDB 维护结果' if self._global_icdb_run else 'CBB 校验结果') + self.summary.setText(status_text) + self.summary.setDetailedText(text) self.summary.setIcon(QMessageBox.Warning if failed else QMessageBox.Information) self._reimport_summary_action = None if self._reimport_offers: @@ -992,6 +998,7 @@ class MainWindow(QWidget): self._reimport_summary_action.setEnabled(False) # Enabled after the worker finishes. # Modeless notification: do not force activation or steal keyboard focus. from PySide6.QtCore import Qt + self.summary.setTextFormat(Qt.PlainText) self.summary.setAttribute(Qt.WA_ShowWithoutActivating) self.summary.show() diff --git a/tests/test_maintenance_ui.py b/tests/test_maintenance_ui.py index f3716b5..65cf036 100644 --- a/tests/test_maintenance_ui.py +++ b/tests/test_maintenance_ui.py @@ -75,8 +75,12 @@ def test_maintenance_summary_exposes_first_failure_reason(qtbot, tmp_path): window._global_icdb_run = True detail = 'MAINTENANCE_TARGET_UNCONFIRMED: PCB 引用与当前工程不一致' window._batch_done(([VerifyResult('BLOCKED', stderr=detail, safe_to_continue=False)], tmp_path/'batch.log')) - assert detail in window.summary.text() - assert detail in window.status_label.text() + assert detail in window.summary.detailedText() + assert detail in window.output.toPlainText() + assert detail not in window.summary.text() + assert str(tmp_path) not in window.summary.text() + assert detail not in window.status_label.text() + assert '失败/受阻 1' in window.summary.text() def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qtbot, tmp_path, monkeypatch): diff --git a/tests/test_navigation_tabs.py b/tests/test_navigation_tabs.py index 35c1502..e7b972b 100644 --- a/tests/test_navigation_tabs.py +++ b/tests/test_navigation_tabs.py @@ -31,3 +31,13 @@ def test_long_status_does_not_expand_the_window(qtbot, tmp_path): qtbot.wait(20) assert window.height() <= 650 assert window.status_label.height() <= window.status_label.fontMetrics().lineSpacing() * 3 + 4 + + +def test_failure_detail_keeps_catalog_name_readable(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch) + window.resize(1000, 650); window.show() + window.cbb_table.item(0, 3).setText('BackAnnotate: ' + 'long exception ' * 100) + qtbot.wait(20) + assert window.cbb_table.columnWidth(0) >= 200 + assert window.cbb_table.horizontalHeader().length() <= window.cbb_table.viewport().width() diff --git a/tests/test_ui.py b/tests/test_ui.py index cb0079d..4e8f794 100644 --- a/tests/test_ui.py +++ b/tests/test_ui.py @@ -98,7 +98,7 @@ def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch): assert window.icdb_progress.value() == 1 assert window.icdb_progress.maximum() == 1 assert '恢复备份待复核 1' in window.status_label.text() - assert window.summary.windowTitle() == '非 READY ICDB 检查与修复结果' + assert window.summary.windowTitle() == 'ICDB 维护结果' reports = list((tmp_path/'reports').glob('icdb-*.json')) assert len(reports) == 1 @@ -359,5 +359,5 @@ def test_batch_summary_exposes_failures_only_report(qtbot, tmp_path): qtbot.addWidget(window) report=tmp_path/'reports/batch-20260929-test.log' window._batch_done(([VerifyResult('FAIL',stderr='Mentor error')],report)) - assert '仅失败日志' in window.summary.text() - assert str(report.with_name('failures-20260929-test.log')) in window.summary.text() + assert '仅失败日志' in window.summary.detailedText() + assert str(report.with_name('failures-20260929-test.log')) in window.summary.detailedText() -- 2.39.5 From 477a5a46e3a7c8a16e7d319ecffd4581587363c1 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: Wed, 4 Nov 2026 19:16:08 +0800 Subject: [PATCH 52/59] fix: separate verification and ICDB maintenance with live session leases --- .../diagnostics/2026-11-04-issue4-followup.md | 45 ++++++ src/cbb_refresh_verify/icdb_maintenance.py | 5 +- src/cbb_refresh_verify/library_lock.py | 39 ++++- src/cbb_refresh_verify/maintenance_entry.py | 94 +++++------- src/cbb_refresh_verify/mentor_adapter.py | 10 +- src/cbb_refresh_verify/native_repair_ui.py | 10 ++ src/cbb_refresh_verify/ui/main_window.py | 78 ++++------ src/cbb_refresh_verify/verify_runner.py | 72 ++++++--- tests/test_icdb_recovery.py | 9 +- tests/test_independent_operations.py | 142 ++++++++++++++++++ tests/test_library_lock.py | 21 ++- tests/test_maintenance_entry.py | 37 +++-- tests/test_maintenance_ui.py | 44 +++--- tests/test_native_export_guard.py | 1 + tests/test_native_repair.py | 7 +- 15 files changed, 420 insertions(+), 194 deletions(-) create mode 100644 docs/diagnostics/2026-11-04-issue4-followup.md create mode 100644 tests/test_independent_operations.py diff --git a/docs/diagnostics/2026-11-04-issue4-followup.md b/docs/diagnostics/2026-11-04-issue4-followup.md new file mode 100644 index 0000000..fc039e8 --- /dev/null +++ b/docs/diagnostics/2026-11-04-issue4-followup.md @@ -0,0 +1,45 @@ +# 工单 #4 与独立操作复核 + +复核基准:最新工单 [#4](https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/issues/4)(更新于 2026-11-04 15:20),当前代码起点 `2587d33`,用户随后要求校验与 ICDB 维护独立执行。原工单基准为 `29959e2`,下表按当前源码重新核对,不能照搬原报告的测试数或认定全部已修复。 + +## 本次用户失败的实际链路 + +最新测试程序的日志位于 `dist/issue2-tabs-release-candidate/CbbRefreshVerify-Background/data/`。北京时间 17:41:55 开始维护 `1GPHY_8212`,17:42:24 进入原生检查,17:43:17 启动 Project Backup,17:43:50 报 `TIMEOUT_UNRELEASED`。本轮没有发出 Repair,8 个后续条目被跳过。17:44:54 再运行另一工程,遗留 PID 60000 被全局进程检查拦截。 + +只读打开对应私有桌面后确认:原生主窗口及 RichEdit Output 控件均存在,但主窗口和 Output 连 WM_NULL 都返回 1460(超时)。不是已经取得 ICDB 损坏结论后修复失败,而是原生检查本身无响应,且本应用没有提供足够的阶段证据。没有向该原库发出新的 Repair、恢复、删除地址文件或强制结束操作。 + +原有逻辑还把“存在历史检查点”直接解释为整个库只能维护,普通校验默认自动 Repair,并把地址记录存在直接解释为会话未释放。这些产品耦合与用户最新要求冲突。 + +## 12 项仍需修改或验收 + +| 工单项 | 当前结论 | 后续工作及验收 | +| --- | --- | --- | +| K2-1 状态桥前置检查 | 仍缺少批次级能力诊断;在原生刷新后才检查权威 READY | 批次开始只诊断一次并明确说明状态不可读取;不能因此封锁 ICDB 和库管理。不能把原生调用完成当作 READY。 | +| K2-1b x86 运行库部署 | 发布候选中的桥为 x86,导入 `mfc140.dll/MSVCP140.dll/VCRUNTIME140.dll`,未随包提供 x86 MFC | 选择静态链接或正式部署 x86 运行库;干净 Windows 实机验证。x64 Python/Qt 的同名 DLL 不能替代。 | +| K2-2 未知弹窗分级 | 输入法误报已有过滤;真正未知交互弹窗仍使当前项失败并停批 | 区分无交互辅助窗口与实际模态提示;后者记录具体原因,正常释放后才继续独立项,不能盲点未知确认按钮。 | +| K2-3 取消与超时 | 协作式取消已存在;看门狗和 `_stop_worker` 仍可能返回时客户端活着 | 检查当前 automation client/保存客户端是否实际退出,防止迟到 Save/Close;不得以删除历史记录代替停止仍在写入的任务。 | +| K2-6 ICDB 自动维护 | 隔离副本曾成功执行 Repair;本次原库原生检查无响应,不能验收 | 当前修复:独立维护、独立校验、缩小检查范围、记录原生等待阶段。仍需一致副本上的真实按钮验收;原生窗口挂起成因待查。 | +| K2-4 跨机共享库并发 | sidecar 排他句柄只协调遵守协议的工具,Mentor 不参与 | 明确共享库操作协议,基于实际原生文件占用处理冲突;两台机器同时写同一工程的验证尚未完成。 | +| K2-5 地址记录及全局 PCB 拦截 | 地址文件和历史记录的普通校验硬拦截在本次拆除;“任意 PCB 已开即拒绝”仍存在 | 改为按工程身份判断占用;独立 CBB 不应相互禁用;跨会话 COM 绑定需实际验证后调整。 | +| K2-7 Windows 10 | 当前没有 Win10 实机完成证据 | 在 Win10 + EE7.9.5 验证私有桌面、启动、保存、释放及 READY;不能以 Win11 回归替代。 | +| K2-8 内容文本与 PRJ 编码 | 探针确认成功文本含 `0 errors` 仍返回 FAIL;preflight 仍按 UTF-8 读取 | 复用已有工程编码读取器,区分零错误计数与真实错误;补 UTF-16、ACP 和真正失败样本。 | +| K2-9 CentralLibrary 改写与说明 | 写入前已有原始 `pcb.prj` 证据副本和原子替换;旧“没有直接改写”说明不准确 | 核对异常回退与说明,按用户要求提示手动备份,不能重新加上自动整库备份。 | +| K3-1 初次扫描规模 | 状态指纹与逐项 preflight 仍有全库开销;本次只缩小了维护检查范围 | 首屏增量读取、扫描进度和取消;381/3000 项及网络盘测量仍需做。 | +| K3-2 队列恢复 | 已有状态/管理检查点,但没有完整校验队列的确认续跑 | 保存目标、已完成项和具体结果,重启后由用户决定继续;管理写操作不得自动重放。 | + +共 12 项:5 项 P0、7 项原工单 P1/规模化条目。它们不等于 12 个已复现的代码错误:包含明确缺陷、部分修复、部署风险及缺少实机验收。优先处理操作解耦、原生检查无响应、桥部署、取消后仍存活的客户端;其余独立提交,不混在一次维护修复中。 + +## 本次实现与验证范围 + +普通校验默认不调用 Repair;历史检查点不再切换清单读取方式或禁用普通校验。合作式库锁跟随实际客户端释放,不为普通校验创建新的永久诊断阻止记录;已有历史记录保留。已退出的只读原生检查不会因为旧地址记录而锁住管理入口。实际仍存活或尚未确认退出的客户端继续保有执行锁;监督工作进程已经退出、只剩已记录的原生 PID 时,重试会检查这些 PID 是否实际退出,校验和维护均可直接重试,不要求先运行另一个功能。工作进程仍在运行时不采用这一简化判定。 + +维护成功报告 `REPAIRED / UNKNOWN` 和“待单独校验”,不自动重开 PCB、不执行反标注、不标为 READY。历史备份恢复仍需核对版本。维护预检查只扫描当前 Work、相关共享符号数据库及中心库定义,并保持链接、文件集合变化和独占句柄检查。扫描真实原库的 5,218 项用时 14.109 秒(只读,未启动原生工具);原用户流程的两层预检查合计约 76 秒,不能把这两个口径直接当作完整维护耗时对比。 + +验证证据在 `diagnostics/independent-operations-20261104/`(原始现场记录不提交): + +- 操作拆分后的相关回归 509 项通过;审查发现并补正真实客户端仍在写入时的执行锁保留,以及直接重试维护的入口问题。 +- 执行锁、维护、占用、校验、导出保护和恢复等相关回归 **214 passed**,退出码 0;最后“无需修复”展示与独立入口回归 **37 passed**,退出码 0。不是对最终源码重新跑完整 Python 套件的声明。 +- 独立静态审查的必要问题已修正。编译及差异检查通过。 +- 冻结包核对 10 个关键模块与当前源码一致,并运行无原生写入的启动/正常退出冒烟。构建元数据明确保留 `native_acceptance=false`、`release_ready=false`。 + +本轮没有重启现场挂起的原生进程、强制结束该进程、在原库执行新的自动修复,或将单元测试当成 ICDB 实机验收。原生窗口长时间无响应的具体内部原因仍未查明;此文档不能作为原生自动修复已验收的证明。 diff --git a/src/cbb_refresh_verify/icdb_maintenance.py b/src/cbb_refresh_verify/icdb_maintenance.py index b701b6c..01802d8 100644 --- a/src/cbb_refresh_verify/icdb_maintenance.py +++ b/src/cbb_refresh_verify/icdb_maintenance.py @@ -15,12 +15,15 @@ def maintenance_counts(results): version_choice = sum(attempt.get('status') == 'PCB_VERSION_CHOICE_REQUIRED' and attempt.get('native_status') == 'REPAIRED' and attempt.get('validation_passed') is False for attempt in attempts) + pending = sum(attempt.get('status') == 'REPAIRED_UNVERIFIED' + and attempt.get('native_status') == 'REPAIRED' for attempt in attempts) return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results), 'checked_without_repair': sum(r.status == 'CHECKED' for r in results), 'skipped': sum(r.status == 'SKIPPED' for r in results), 'repaired': repaired, 'review_required': review, 'version_choice_required': version_choice, - 'repair_unconfirmed': len(attempts)-repaired-review-version_choice} + 'repaired_unverified': pending, + 'repair_unconfirmed': len(attempts)-repaired-review-version_choice-pending} def write_maintenance_report(requests, results, batch_report, skipped_names): diff --git a/src/cbb_refresh_verify/library_lock.py b/src/cbb_refresh_verify/library_lock.py index 98ec832..523c748 100644 --- a/src/cbb_refresh_verify/library_lock.py +++ b/src/cbb_refresh_verify/library_lock.py @@ -78,13 +78,14 @@ def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id): class LibraryLock: - def __init__(self, library, *, safe_to_release=None): + def __init__(self, library, *, safe_to_release=None, checkpoint=True): library = Path(library).resolve() self.library = library self.path = library.with_name('.' + library.stem + '.cbb-upload.lock') self.key = _key(self.path) self.entered = False self.safe_to_release = safe_to_release + self.checkpoint = checkpoint self.maintenance_guard = None self.maintenance_mutex = None @@ -96,6 +97,17 @@ class LibraryLock: return lease def __enter__(self): + with _guard: + owner = _owners.get(self.key) + if owner and owner['retained'] and owner['depth'] == 0 and owner.get('release_probe'): + try: + released = owner['release_probe']() is True + except Exception: + released = False + if released: + owner['handle'].Close() + del _owners[self.key] + owner = None if self.maintenance_guard is not None: return _enter_maintenance(self) import win32con, win32file @@ -106,7 +118,7 @@ class LibraryLock: raise LibraryBusy(f'中心库被其他任务占用:{self.path}') owner['depth'] += 1 else: - if has_blocker(self.library): + if self.checkpoint and has_blocker(self.library): raise LibraryBusy(f'中心库存在未解除的会话阻止记录,请先执行释放检查:{blocker_path(self.library)}') try: lease_id = uuid4().hex @@ -127,12 +139,15 @@ class LibraryLock: # it explicitly before complete-copy metadata is captured. import pywintypes win32file.SetFileTime(handle, None, None, pywintypes.Time(datetime.now())) - _persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id) + if self.checkpoint: + _persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id) except Exception: handle.Close() raise _owners[self.key] = {'thread': threading.get_ident(), 'depth': 1, - 'handle': handle, 'retained': False, 'lease_id': lease_id} + 'handle': handle, 'retained': False, 'lease_id': lease_id, + 'checkpoint': self.checkpoint, + 'release_probe': self.safe_to_release if not self.checkpoint else None} self.entered = True return self @@ -162,6 +177,20 @@ class LibraryLock: if not self.entered: return owner = _owners[self.key] + if not owner.get('checkpoint', True): + if self.safe_to_release is not None: + try: + released = self.safe_to_release() is True + except Exception: + released = False + if not released: + owner['retained'] = True + owner['depth'] -= 1 + self.entered = False + if owner['depth'] == 0 and not owner['retained']: + owner['handle'].Close() + del _owners[self.key] + return checkpoint = checkpoint_record(blocker_path(self.library)) if checkpoint is not None and 'export_analysis' in checkpoint: owner['retained'] = True @@ -237,7 +266,7 @@ def _enter_maintenance(lease): raise LibraryBusy('本进程库锁仍在使用或检查点身份不符') handle = existing['handle'] else: - if record is not None and _supervisor_alive(record['pid']): + if record is not None and record['pid'] != os.getpid() and _supervisor_alive(record['pid']): raise LibraryBusy('原监督进程仍存在,不能接续其库锁') handle = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 09b7f5e..3c622b0 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -44,12 +44,30 @@ def address_snapshot(paths): for path in paths} +def maintenance_entries(request, *, check=None): + from .verify_runner import preflight + from .scoped_manifest import scan_tree + project, _ = preflight(request) + root = Path(request.lmc_file).parent + entries = scan_tree(project.parent, check=check) + shared = root/'SymbolLibs/__ReusableBlocksSymb' + if shared.exists(): + entries.update(scan_tree(shared, check=check)) + for library in root.glob('*.lmc'): + info = library.lstat() + if (info.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT + or not stat.S_ISREG(info.st_mode)): + raise ValueError(f'不支持通过文件链接维护:{library}') + entries[library] = info + return entries + + 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) + entries = maintenance_entries(request, check=check) if check is not None: check() for path in entries: if path.name.casefold() != 'saddress.adr': @@ -93,7 +111,8 @@ def select_maintenance_target(request, *, check=None, entries=None): if request.maintenance is not True: raise ValueError('MAINTENANCE_INTENT_REQUIRED') project, pcb = preflight(request) - scan_tree(project.parent, check=check) + if entries is None: + entries = maintenance_entries(request, check=check) candidates = [] for candidate in maintenance_project_files(project, pcb): if not candidate.is_file(): @@ -144,7 +163,7 @@ def maintenance_target_guard(request, expected=None, *, stop=None): 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) + entries = maintenance_entries(request, 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, @@ -167,7 +186,7 @@ def maintenance_target_guard(request, expected=None, *, stop=None): win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None) stack.callback(handle.Close) assert_native_idle() - current = scan_tree(library_root, check=check) + current = maintenance_entries(request, check=check) if native_files(current) != files: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 检查期间库文件集合发生变化') current_addresses = _scoped_addresses(request, check=check, entries=current) @@ -190,10 +209,9 @@ def maintenance_needed(request): return bool(unreleased_addresses(request.lmc_file, project, pcb)) -def run_existing_address_maintenance(request, *, verify, progress=None, stop=None): +def run_existing_address_maintenance(request, *, 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 .native_repair import run_native_repair, native_success_confirmed, repair_release_state from .verify_runner import VerifyResult root = Path(request.evidence_dir).resolve()/('maintenance-' + uuid4().hex[:12]) library_root = Path(request.lmc_file).resolve().parent @@ -222,23 +240,27 @@ def run_existing_address_maintenance(request, *, verify, progress=None, stop=Non lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover', lambda: maintenance_target_guard(request, stop=stop), safe_to_release=lambda: owner_safe) with lease: + owner_safe = True # Admission completed; no native client exists yet. 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') + owner_safe = False 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 + owner_safe = (inspection.get('release', {}).get('native_exited') is True + or inspection.get('native_pid') is None and inspection.get('worker_pid') is None) from .native_occupancy import confirmed_inspection if not confirmed_inspection(target, inspection): - return save(VerifyResult('BLOCKED', safe_to_continue=False, + return save(VerifyResult('BLOCKED', safe_to_continue=owner_safe, 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, + return save(VerifyResult('BLOCKED', safe_to_continue=owner_safe, 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. @@ -251,10 +273,8 @@ def run_existing_address_maintenance(request, *, verify, progress=None, stop=Non 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 '历史地址记录仍存在;检查未替代恢复或会话释放。')) + stdout='检查完成,无需修复。')) if stop is not None and stop.is_set(): raise ValueError('MAINTENANCE_CANCELLED: 已取消,未开始 Repair') emit('icdb_diagnosis_confirmed', detail='本轮原生检查明确要求修复当前数据库,且确认未占用') @@ -267,6 +287,7 @@ def run_existing_address_maintenance(request, *, verify, progress=None, stop=Non # Repeat immediately before dispatch; the worker repeats before # launching the native process as well. with maintenance_target_guard(request, context, stop=stop): pass + owner_safe = False 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) @@ -275,48 +296,15 @@ def run_existing_address_maintenance(request, *, verify, progress=None, stop=Non 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,))) + # Release follows actual clients, not the repair/content verdict. + owner_safe = safe or (recovery['release'].get('native_exited') is True + and recovery['release'].get('database_exclusive') is True + and recovery['release'].get('remaining_pids') == []) 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,))) + emit('native_repair_complete', detail='原生 ICDB 修复完成,CBB 待单独校验') + recovery['status'] = 'REPAIRED_UNVERIFIED' + return save(VerifyResult('REPAIRED', safe_to_continue=owner_safe, + stdout='原生 ICDB 修复完成,CBB 待单独校验。', 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", "原生维护未确认修复")}', diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index 7eddcb4..128ba1e 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -630,13 +630,7 @@ def execute_job(job, emit): emit(stage='mapped_drive_context', **mapping) emit(stage='backup_skipped', reason='manual_backup_policy', source=str(block_root), detail='应用不自动备份;请在操作前手动备份。') - from .icdb_ownership import database_directories, unreleased_addresses - for database in database_directories(job['lmc_file'], job['project'], job['pcb']): - icdb_address = prepare_icdb_address(database, root, emit=emit) - if icdb_address.get('status') != 'ABSENT': - raise RuntimeError( - 'ICDB_SESSION_UNCONFIRMED: 数据库存在服务器地址记录,无法确认独占使用;' - '已保留地址文件,请先检查原 PCB/iCDB 会话。' + str(icdb_address)) + from .icdb_ownership import unreleased_addresses from .verify_diagnostics import collect_preflight_diagnostics, collect_native_failure_logs diagnostics = collect_preflight_diagnostics(job['pcb'], root) emit(stage='preflight_diagnostics', non_blocking=True, @@ -862,7 +856,7 @@ def execute_job(job, emit): raise RuntimeError(f'PCB_RELEASE_UNCONFIRMED: 未确认 PCB 退出: {released}') addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb']) if addresses: - raise RuntimeError(f'ICDB_RELEASE_UNCONFIRMED: PCB 已退出但数据库地址记录仍存在;保留文件并停止批次: {addresses}') + emit(stage='icdb_address_remaining', addresses=addresses, non_blocking=True) cert_pcb = block_root/'Cert'/Path(job['pcb']).relative_to(block_root/'Work') cert_evidence = collect_cert_evidence(block_root, cert_project, cert_pcb) emit(stage='cert_diagnostics', **cert_evidence) diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index 77f972f..4bc0ec5 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -194,6 +194,7 @@ def _inspect_project(job, process, emit): 'repair_required': False, 'output': ''} deadline = time.monotonic() + max(1, float(job['timeout'])) closing = False + last_state = None # Reserve a short cooperative-close grace after the inspection deadline. while process.poll() is None and time.monotonic() < deadline + 3: windows = [w for w in _windows(process.pid) if not ( @@ -207,6 +208,13 @@ def _inspect_project(job, process, emit): result.update(status='UNCONFIRMED', unknown_dialogs=unknown) break current = _native_output(windows) + state = {'main_found': main is not None, 'output_readable': current is not None, + 'windows': windows} + if state != last_state: + (Path(job['root'])/'inspection-state.json').write_text( + json.dumps(state, ensure_ascii=False, indent=2), encoding='utf-8') + emit(stage='native_occupancy_wait', main_found=state['main_found'], output_readable=state['output_readable']) + last_state = state if current is not None: result['output'] = current cancelled = Path(job['cancel_signal']).exists() @@ -227,6 +235,8 @@ def _inspect_project(job, process, emit): 'safe_to_continue': exited and result['status'] == 'NOT_IN_USE'} if not exited: result.update(status='TIMEOUT_UNRELEASED', repair_required=False) + result['detail'] = ('原生检查窗口未返回 Output,未执行 Repair。' + if not result['output'] else '原生检查工具未退出,未执行 Repair。') return result diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index b151c1e..b0e13df 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -121,7 +121,7 @@ class MainWindow(QWidget): self.advanced_panel.setVisible(False) self.advanced_toggle.toggled.connect(self.advanced_panel.setVisible) self.cbb_table = QTableWidget(0, 4) - self.cbb_table.setHorizontalHeaderLabels(['CBB 名称', '分区', '状态', '环境检查']) + self.cbb_table.setHorizontalHeaderLabels(['CBB 名称', '分区', '状态', '备注']) self.cbb_table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self.cbb_table.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch) for column in (1, 2): @@ -289,11 +289,11 @@ class MainWindow(QWidget): def _update(self): # The refresh flow needs only the library and a CBB; LibSpec/DMS config # are optional and only enable the extra contents check. - ready = not self.busy and not self._management_unsafe and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 - self.batch_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._eligible_names())) - self.selected_batch_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._eligible_names(checked_only=True))) + ready = not self.busy and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 + self.batch_button.setEnabled(ready and bool(self._eligible_names())) + self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True))) self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names())) - self.backup_restore_button.setEnabled(ready and not self._maintenance_recovery_view and bool(self._maintenance_names())) + self.backup_restore_button.setEnabled(ready and bool(self._maintenance_names())) self.release_library_button.setEnabled(not self.busy and bool(self.lmc_edit.text().strip())) reasons = [] for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): @@ -303,10 +303,7 @@ class MainWindow(QWidget): if reason: reasons.append(reason) self.management_capability_label.setText('\n'.join(dict.fromkeys(reasons))) - notice = ('该库有未确认的会话记录,新增、替换、移除和普通校验已暂停。' - '请先执行 ICDB 维护;仍有数据库地址记录时,单独点击释放检查不会解除阻止。' - if self._maintenance_recovery_view else - '上次管理操作尚未确认安全释放,已暂停库操作。请先查看异常证据并检查会话释放。' + notice = ('上次管理操作尚未确认安全释放,请检查该操作的结果。' if self._management_unsafe else '') self.library_notice.setText(notice) self.library_notice.setVisible(bool(notice)) @@ -357,8 +354,6 @@ class MainWindow(QWidget): def _management_block_reason(self, operation): if self.busy: return '当前任务尚未结束,请等待后再执行库管理操作。' - if self._maintenance_recovery_view: - return '库的会话释放尚未确认,管理操作已禁用;请先执行 ICDB 维护并查看逐项结果。' if self._management_unsafe: return '上次管理操作的会话尚未安全释放,请先查看异常证据并执行释放检查。' capability = self.management_capabilities[operation] @@ -546,21 +541,8 @@ class MainWindow(QWidget): 'library_path_lease display=%s mentor=%s drive=%s remote=%s created=%s', lease.display_path, mentor_path, lease.drive or '-', lease.remote_root or '-', lease.created) - from cbb_refresh_verify.library_lock import blocker_path, checkpoint_record - marker = blocker_path(mentor_path) recovery_view = False - if marker.exists() or marker.with_suffix('.pending').exists(): - from cbb_refresh_verify.models import LibraryReadResult - checkpoint = checkpoint_record(marker) - if (checkpoint is not None and not marker.with_suffix('.pending').exists() - and Path(checkpoint['library']).resolve() == Path(mentor_path).resolve()): - result = read_library_fast(mentor_path, - status_reader=lambda _, names: {name: 'UNKNOWN' for name in names}) - recovery_view = True - else: - result = LibraryReadResult((), '此库检查点损坏或身份不符,保留原记录;请先审查并执行释放检查。') - else: - result = self.reader(mentor_path) + result = self.reader(mentor_path) if self.logging_context: statuses = Counter(record.status for record in result.records) unknown = [record.name for record in result.records if record.status in {'UNKNOWN', 'UNAVAILABLE'}] @@ -673,7 +655,7 @@ class MainWindow(QWidget): def _confirm_verify(self, names): from .backup_notice import MANUAL_BACKUP_NOTICE - if self.busy or not self.loaded_path or self._maintenance_recovery_view or not names: + if self.busy or not self.loaded_path or not names: return if QMessageBox.question(self, '确认批量校验', f'{MANUAL_BACKUP_NOTICE}\n\n目标库:{self.loaded_path}\n' @@ -690,17 +672,13 @@ class MainWindow(QWidget): f'{MANUAL_BACKUP_NOTICE}\nMentor 原生修复可能生成自身备份。\n\n当前中心库:{self.loaded_path}\n\n' f'将检查 {len(names)} 个可见非 READY CBB' f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '先取得库锁并核对工程,由原生工具检查占用和数据库。仅本轮检查确认当前工程未占用且需要 ICDB Repair 时才修复,再重新打开校验。' - '占用、历史版本提示或故障原因未确认时停止,不猜测故障或删除地址文件。READY 项始终忽略。\n' - '仅本地固定磁盘:严格确认修复并重新独占检查后,可一次确认本次异常退出的读写访问提示。' - '网络盘、UNC 或链接路径不自动确认该提示。\n' - '共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;' - '会话安全无法确认时停止队列。\n\n是否开始检查与修复?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) + '原生工具确认需要修复时执行 Repair,完成后可单独运行校验。\n' + '若恢复了历史备份,请核对工程版本。\n\n是否开始?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: self._run(names, maintenance=True) def restore_backup(self): - if self.busy or not self.loaded_path or self._maintenance_recovery_view: + if self.busy or not self.loaded_path: return from .backup_recovery_dialog import BackupRecoveryDialog names = self._eligible_names(checked_only=bool(self.selected_records())) @@ -719,7 +697,7 @@ class MainWindow(QWidget): def _backup_request(self, name): # A still-open dialog must recheck a target after an earlier recovery # or an authoritative refresh changed its eligibility. - if self.busy or self._maintenance_recovery_view or name not in self._eligible_names(checked_only=bool(self.selected_records())): + if self.busy or name not in self._eligible_names(checked_only=bool(self.selected_records())): raise ValueError('目标需为当前可见、可用的非 READY CBB;请重新选择。') return self._request(name) @@ -759,14 +737,13 @@ class MainWindow(QWidget): def _run(self, names, *, maintenance=False): if self.busy or not self.loaded_path: return - if self._maintenance_recovery_view and not maintenance: return eligible = set(self._maintenance_names() if maintenance else self._eligible_names()) names = [name for name in names if name in eligible] if not names: self.status_label.setText('当前可见选择中没有可校验的非 READY CBB。') self._update() return - requests = [replace(self._request(name), maintenance=maintenance) for name in names] + requests = [replace(self._request(name), maintenance=maintenance, auto_repair=False) for name in names] self._batch_requests = tuple(requests) self._batch_records = {name: self._records_by_name[name] for name in names} self._reimport_offers = () @@ -794,14 +771,16 @@ class MainWindow(QWidget): self.signals.event.emit({'block': req.block_name, 'stage': 'icdb_check_start'}) return verify(req) results = run_batch(requests, verifier=verifier, stop=self.stop, - progress=lambda name, result: self.signals.event.emit({'block': name, 'stage': result.status, 'detail': result.stderr, 'evidence': str(result.evidence_path or '')})) + progress=lambda name, result: self.signals.event.emit({'block': name, 'stage': result.status, + 'error_category': result.error_category, 'evidence': str(result.evidence_path or ''), + 'detail': result.stderr or result.stdout})) report = write_batch_report(requests, results, self.data_root/'reports') if maintenance: from cbb_refresh_verify.icdb_maintenance import write_maintenance_report write_maintenance_report(requests, results, report, ()) affected = [req.block_name for req, result in zip(requests, results) if result.status != 'SKIPPED'] safe = all(result.safe_to_continue for result in results) - refresh = self._read_affected_records(affected) if safe and not self._maintenance_recovery_view else None + refresh = self._read_affected_records(affected) if safe else None return results, report, affected, refresh self._start(action, self._batch_done) @@ -874,7 +853,7 @@ class MainWindow(QWidget): (UNKNOWN on read failure). It runs before the worker's finished signal. Search, filter, original logical_type and action-result cells are retained. """ - if self.busy or not self.loaded_path or self._maintenance_recovery_view: + if self.busy or not self.loaded_path: return False targets = tuple(dict.fromkeys(name for name in names if name in self._records_by_name)) if not targets: @@ -895,20 +874,26 @@ class MainWindow(QWidget): self.output.append(line) block = event.get('block'); stage = event.get('stage') if self._global_icdb_run: - if stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED', 'QUEUE_STOP', 'CHECKED', 'CANCELLED'}: + if stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED', 'QUEUE_STOP', 'CHECKED', 'CANCELLED', 'REPAIRED'}: self._icdb_completed += 1 self.icdb_progress.setValue(self._icdb_completed) phases = {'icdb_check_start': '检查工程数据库与校验状态', + 'icdb_ownership_check': '正在打开原生检查工具', + 'native_started': '等待原生工具加载工程', + 'native_occupancy_wait': '等待原生检查结果', + 'native_repair_complete': '原生修复完成,待单独校验', 'native_repair_start': '核对占用并准备原生修复', 'native_repair_requested': '正在原生修复', 'native_repair_confirmed': '正在原生修复', 'native_repair_finished': '原生修复已结束,正在核对结果'} if stage in phases: self.icdb_phase.setText(f'{block or ""} · {phases[stage]}') - if block and stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED'}: + if block and stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED', 'CHECKED', 'REPAIRED'}: row = self._row_by_name.get(block) if row is not None: detail = str(event.get('detail') or event.get('error') or '').strip() + if str(event.get('error_category', '')).startswith('ICDB') and detail: + detail = 'ICDB 问题:' + detail label = str(stage) if not detail else f'{stage}: {detail[:96]}' self.cbb_table.item(row, 3).setText(label) self.cbb_table.item(row, 3).setToolTip(detail) @@ -931,8 +916,6 @@ class MainWindow(QWidget): self._reimport_offers = tuple((req, self._batch_records[req.block_name], result) for req, result in zip(self._batch_requests, results) if req.maintenance and req.block_name in self._batch_records and offers_reimport(result)) - if self._maintenance_recovery_view and self._global_icdb_run and results: - self._reload_after_maintenance = all(result.safe_to_continue for result in results) if len(outcome) > 2: _, _, affected, refresh = outcome if refresh is None: @@ -948,7 +931,8 @@ class MainWindow(QWidget): passed = sum(r.status == 'PASS' for r in results) skipped = sum(r.status == 'SKIPPED' for r in results) checked = sum(r.status == 'CHECKED' for r in results) - failed = len(results)-passed-skipped-checked + repaired = sum(r.status == 'REPAIRED' for r in results) + failed = len(results)-passed-skipped-checked-repaired text = (f'已结束:成功 {passed},失败 {failed},预检跳过 {len(self.skipped_names)},' f'队列未执行 {skipped}。\n完整日志:{report}\n仅失败日志:{failure_report_path(report)}') if self._global_icdb_run: @@ -957,7 +941,7 @@ class MainWindow(QWidget): text = ('非 READY ICDB 检查已结束\n' f'修复并校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' f'仅检查、未修复 {counts["checked_without_repair"]}(不计入修复成功)\n' - f'原生修复完成 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},' + f'原生修复完成、待单独校验 {counts["repaired_unverified"]},恢复备份待复核 {counts["review_required"]},' f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' f'修复未确认 {counts["repair_unconfirmed"]}\n' f'完整日志:{report}\n仅失败日志:{failure_report_path(report)}') @@ -966,12 +950,14 @@ class MainWindow(QWidget): text += '\n未确认数据库或会话安全,已停止剩余校验。请查看恢复现场。' status_text = (f'已结束:通过 {passed},失败/受阻 {failed},' f'仅检查 {checked},未执行 {skipped + len(self.skipped_names)}。') + if repaired: + status_text += f'\n原生 ICDB 修复完成 {repaired},待单独校验。' if self._global_icdb_run and counts['review_required']: status_text += f'\n恢复备份待复核 {counts["review_required"]},请核对工程版本。' if self._global_icdb_run and counts['version_choice_required']: status_text += f'\n待选择 PCB 版本 {counts["version_choice_required"]}。' first_failure = next((result.stderr for result in results - if result.status not in ('PASS', 'SKIPPED') and result.stderr), '') + if result.status not in ('PASS', 'SKIPPED', 'CHECKED', 'REPAIRED') and result.stderr), '') if first_failure: text += '\n首个未完成原因:' + first_failure if any(result.error_category == 'CBB_REIMPORT_REQUIRED' for result in results): diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index a0a23ef..56ce479 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -62,7 +62,7 @@ class VerifyRequest: # it before launching Library Manager/PCB. mapped_drive: str = '' mapped_remote_root: str = '' - auto_repair: bool = True + auto_repair: bool = False maintenance: bool = False post_repair_proof: object | None = field(default=None, repr=False, compare=False) diagnostic_only: bool = False @@ -522,6 +522,12 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, acquired = False library_lock = None release_confirmed = True + remaining_clients = () + def clients_released(): + if release_confirmed: + return True + from .library_lock import _supervisor_alive + return bool(remaining_clients) and not any(_supervisor_alive(pid) for pid in remaining_clients) try: acquired = win32event.WaitForSingleObject(mutex, 0) in ( win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED) @@ -535,17 +541,15 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, except (ValueError, OSError) as error: return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False) if needed: - return run_existing_address_maintenance(request, progress=progress, stop=stop, - verify=lambda req: _run_verify_serialized(req, progress=progress, flag_reader=flag_reader, - process_runner=process_runner, port_check=port_check, - dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt)) + return run_existing_address_maintenance(request, progress=progress, stop=stop) from .library_lock import LibraryLock, LibraryBusy if Path(request.lmc_file).is_file(): try: - library_lock = LibraryLock(request.lmc_file, safe_to_release=lambda: release_confirmed) + library_lock = LibraryLock(request.lmc_file, checkpoint=False, + safe_to_release=clients_released) library_lock.__enter__() except LibraryBusy as error: - return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False) + return VerifyResult('FAIL', stderr=str(error), error_category='LIBRARY_IN_USE') release_confirmed = False result = _run_verify_serialized( request, progress=progress, flag_reader=flag_reader, @@ -629,6 +633,18 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, except OSError: pass # Keep the native/repair errors even if evidence storage fails. release_confirmed = result.safe_to_continue + if not release_confirmed and result.evidence_path: + try: + release = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8')) + release_confirmed = (release.get('worker_exited') is True + and release.get('safe_to_continue') is True and release.get('block') == request.block_name + and Path(release['library']).resolve() == Path(request.lmc_file).resolve()) + if (release.get('worker_exited') is True and release.get('block') == request.block_name + and Path(release['library']).resolve() == Path(request.lmc_file).resolve()): + remaining_clients = tuple(pid for pid in release.get('remaining_pids', []) + if type(pid) is int and pid > 0) + except (OSError, ValueError, KeyError): + pass return result finally: try: @@ -673,8 +689,17 @@ def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_ detail = result.stderr if recovery and RECOVERY_ACTION not in detail: detail = (detail + '\n' + recovery_detail(recovery)).strip() - result = replace(result, safe_to_continue=False, stderr=detail, - error_category=ERROR_CODE) + result = replace(result, stderr=detail, error_category=ERROR_CODE) + if not request.auto_repair and result.evidence_path: + try: + release = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8')) + if (result.error_stage in {'PCB_STARTING', 'pcb_startup'} + and release.get('worker_exited') is True and release.get('safe_to_continue') is True + and release.get('block') == request.block_name + and Path(release['library']).resolve() == Path(request.lmc_file).resolve()): + result = replace(result, safe_to_continue=True) + except (OSError, ValueError, KeyError): + pass if recovery and result.evidence_path: try: record_path = result.evidence_path.parent/'diagnostics/icdb-recovery.json' @@ -752,17 +777,6 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run finally: timing.end('preflight') - from .icdb_ownership import unreleased_addresses - try: - addresses = unreleased_addresses(request.lmc_file, project, pcb) - if addresses: - return finish(VerifyResult('BLOCKED', safe_to_continue=False, - stderr='ICDB_SESSION_UNCONFIRMED: 原生数据库所有权未释放,未开始内容检查或校验:' - + '; '.join(addresses))) - except OSError as error: - return finish(VerifyResult('BLOCKED', safe_to_continue=False, - stderr=f'ICDB_SESSION_UNCONFIRMED: 无法确认数据库所有权:{error}')) - content_check_enabled = not request.diagnostic_only and bool(str(request.libspec).strip() and str(request.dms_login_config).strip()) if content_check_enabled: try: @@ -1070,13 +1084,20 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run from .icdb_ownership import unreleased_addresses addresses = unreleased_addresses(request.lmc_file, project, pcb) if addresses: - release_result = {**(release_result or {}), 'safe_to_continue': False, - 'icdb_address': addresses[0], 'icdb_addresses': addresses} + release_result = {**(release_result or {}), 'icdb_addresses': addresses} except OSError as error: close_errors.append(str(error)) if close_errors: release_result = {**(release_result or {}), 'safe_to_continue': False, 'close_errors': close_errors} + try: + (root/'session-release.json').write_text(json.dumps({ + **(release_result or {}), 'block': request.block_name, + 'library': str(request.lmc_file), 'worker_exited': process.poll() is not None, + 'worker_pid': getattr(process, 'pid', None)}, + ensure_ascii=False, default=str, indent=2), encoding='utf-8') + except OSError: + pass if not release_result or not release_result.get('safe_to_continue'): if 'result' in locals(): result = replace(result, status='FAIL', safe_to_continue=False, @@ -1173,11 +1194,12 @@ def write_failure_report(requests, results, root, *, path=None): path = Path(path) if path else root/('failures-' + time.strftime('%Y%m%d-%H%M%S') + '-' + uuid4().hex[:6] + '.log') failed = [(req, result) for req, result in zip(requests, results) if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'} - and not (result.status == 'CHECKED' and result.safe_to_continue)] + and not (result.status in {'CHECKED', 'REPAIRED'} and result.safe_to_continue)] passed = sum(result.status == 'PASS' for result in results) checked = sum(result.status == 'CHECKED' and result.safe_to_continue for result in results) - skipped = len(results)-passed-len(failed)-checked - lines = ['CBB Verify 失败清单', f'总数: {len(results)} 成功: {passed} 失败: {len(failed)} 未执行: {skipped} 仅检查未修复: {checked}', + repaired = sum(result.status == 'REPAIRED' and result.safe_to_continue for result in results) + skipped = len(results)-passed-len(failed)-checked-repaired + lines = ['CBB Verify 失败清单', f'总数: {len(results)} 成功: {passed} 失败: {len(failed)} 未执行: {skipped} 仅检查未修复: {checked} ICDB已修复待校验: {repaired}', 'CBB\t状态\t错误分类\t失败阶段\t桥状态\t原始错误\t证据\t实际耗时(秒)'] for req, result in failed: timings = ' '.join(f'{key}={value:.3f}s' + diff --git a/tests/test_icdb_recovery.py b/tests/test_icdb_recovery.py index 6790dfa..064b3f6 100644 --- a/tests/test_icdb_recovery.py +++ b/tests/test_icdb_recovery.py @@ -258,7 +258,7 @@ def test_native_log_capture_generates_only_fresh_repair_evidence(tmp_path, name) assert log.read_text('utf-8') == ERROR -def test_contents_session_loss_stops_batch_before_any_pcb_launch(tmp_path, monkeypatch): +def test_contents_session_loss_records_each_item_without_launching_pcb_or_repair(tmp_path, monkeypatch): from test_verify_runner import fixture, request fixture(tmp_path) monkeypatch.setattr(runner, 'resolve_importer', lambda *_: Path('ReuseBlockImporter.exe')) @@ -269,11 +269,12 @@ def test_contents_session_loss_stops_batch_before_any_pcb_launch(tmp_path, monke results = runner.run_batch([request(tmp_path, libspec='EE', dms_login_config='login')]*2, verifier=lambda req: runner.run_verify(req, process_runner=content_check, port_check=lambda: pytest.fail('PCB must not start'))) - assert len(calls) == 1 + assert len(calls) == 2 assert results[0].error_category == 'ICDB_REPAIR_REQUIRED' - assert results[0].safe_to_continue is False + assert results[0].safe_to_continue is True assert 'Repair project' in results[0].stderr - assert results[1].status == 'SKIPPED' + assert results[1].status == 'FAIL' + assert results[1].error_category == 'ICDB_REPAIR_REQUIRED' def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, monkeypatch): diff --git a/tests/test_independent_operations.py b/tests/test_independent_operations.py new file mode 100644 index 0000000..31aefe0 --- /dev/null +++ b/tests/test_independent_operations.py @@ -0,0 +1,142 @@ +from pathlib import Path + +import pytest +from test_maintenance_entry import case + + +def test_verify_defaults_to_no_repair_and_does_not_retain_a_failure_lock(tmp_path, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, library_lock as locks, native_repair + library = tmp_path / 'library.lmc'; library.touch() + req = runner.VerifyRequest(library, 'CBB', tmp_path / 'evidence') + assert req.auto_repair is False + marker = locks.blocker_path(library) + marker.write_text('historical diagnostic record') + calls = [] + result = runner.VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: session lost', + error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: (calls.append('verify') or result)) + monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **k: pytest.fail('verify requested Repair')) + assert runner.run_verify(req) is result + assert runner.run_verify(req) is result + assert calls == ['verify', 'verify'] + assert marker.read_text() == 'historical diagnostic record' + assert locks._key(tmp_path / '.library.cbb-upload.lock') not in locks._owners + + +def test_catalog_checkpoint_does_not_disable_ordinary_verify(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + from cbb_refresh_verify.verify_runner import VerifyResult + calls = [] + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda req: (calls.append(req) or VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: lost', + error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True)), + status_refresher=lambda names, _: {name: 'PENDING' for name in names}) + window._maintenance_recovery_view = True + window._management_unsafe = True + window.search_edit.setText('Beta'); window.select_visible_button.click(); window._update() + assert window.selected_batch_button.isEnabled() + window.verify_selected(); qtbot.waitUntil(lambda: not window.busy) + assert len(calls) == 1 and not calls[0].maintenance and not calls[0].auto_repair + assert window.selected_batch_button.isEnabled() + assert 'ICDB' in window.cbb_table.item(window._row_by_name['Beta'], 3).text() + + +def test_maintenance_guard_does_not_scan_other_cbb_projects(case, monkeypatch): + from cbb_refresh_verify import maintenance_entry as entry + from cbb_refresh_verify import scoped_manifest as manifests + req, project, _ = case + scans = [] + scan = manifests.scan_tree + monkeypatch.setattr(manifests, 'scan_tree', lambda root, **kw: (scans.append(Path(root)) or scan(root, **kw))) + with entry.maintenance_target_guard(req): + pass + assert req.lmc_file.parent not in scans + assert scans.count(project.parent) == 2 + + +def test_repair_finishes_without_calling_verify(case, monkeypatch): + from cbb_refresh_verify import native_repair, verify_runner as runner + from test_maintenance_entry import patch_verification_after_fault + from test_native_repair import complete_native_success + req, _, address = case + def repair(target, *a, **kw): + address.unlink() + return complete_native_success(target) + monkeypatch.setattr(native_repair, 'run_native_repair', repair) + monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) + patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('maintenance started CBB verification')) + result = runner.run_verify(req) + assert result.status == 'REPAIRED' and result.safe_to_continue + assert result.library_flag == 'UNKNOWN' + assert result.recovery_attempts[0]['validation_passed'] is False + + +@pytest.mark.parametrize('stage,expected_safe', [('PCB_STARTING', True), ('back_annotation', False)]) +def test_only_released_startup_failure_can_continue_from_icdb_diagnosis(tmp_path, monkeypatch, stage, expected_safe): + import json + from cbb_refresh_verify import verify_runner as runner + library = tmp_path/'library.lmc'; library.touch() + evidence = tmp_path/'evidence'; evidence.mkdir() + report = evidence/'result.json'; report.write_text('{}') + (evidence/'session-release.json').write_text(json.dumps({'library': str(library), + 'block': 'CBB', 'worker_exited': True, 'safe_to_continue': True})) + failure = runner.VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: lost session', + error_stage=stage, safe_to_continue=False, evidence_path=report) + monkeypatch.setattr(runner, '_run_verify', lambda *a, **k: failure) + result = runner._run_verify_serialized(runner.VerifyRequest(library, 'CBB', evidence)) + assert result.status == 'FAIL' and result.error_category == 'ICDB_REPAIR_REQUIRED' + assert result.safe_to_continue is expected_safe + + +def test_live_writer_keeps_execution_lock_without_a_permanent_diagnostic_blocker(tmp_path, monkeypatch): + import win32con, win32file + from cbb_refresh_verify import library_lock as locks, verify_runner as runner + library = tmp_path/'lib.lmc'; library.touch() + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: + runner.VerifyResult('TIMEOUT', safe_to_continue=False)) + result = runner.run_verify(runner.VerifyRequest(library, 'CBB', tmp_path/'evidence')) + lock = locks.LibraryLock(library) + try: + assert not result.safe_to_continue and not locks.blocker_path(library).exists() + with pytest.raises(Exception): + win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, + 0, None, win32con.OPEN_ALWAYS, 0, None) + finally: + with locks._guard: + owner = locks._owners.pop(lock.key, None) + if owner: owner['handle'].Close() + + +def test_healthy_native_inspection_with_old_address_does_not_lock_management(case, monkeypatch): + from cbb_refresh_verify import native_repair, verify_runner as runner, library_lock as locks + from test_native_occupancy import output + request, project, address = case + def inspect(target, *a, **kw): + assert kw['inspection_only'] + return {'inspection_only': True, 'repair_requested': False, 'repair_required': False, + 'status': 'NOT_IN_USE', 'native_pid': 777, 'native_exit_code': 0, + 'project': str(target.project), 'database': str(target.database), + 'output': output(target.project, target.database), 'release': {'native_exited': True}} + monkeypatch.setattr(native_repair, 'run_native_repair', inspect) + result = runner.run_verify(request) + assert result.status == 'CHECKED' and result.safe_to_continue and address.exists() + assert not result.stderr and '无需修复' in result.stdout + assert not locks.blocker_path(request.lmc_file).exists() + with locks.LibraryLock(request.lmc_file): pass + + +@pytest.mark.parametrize('operation', ['verify', 'maintenance']) +def test_retained_execution_lease_can_retry_after_its_actual_client_exits(tmp_path, operation): + from contextlib import contextmanager + from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy + library = tmp_path/'library.lmc'; library.touch() + released = [False] + with LibraryLock(library, checkpoint=False, safe_to_release=lambda: released[0]): pass + with pytest.raises(LibraryBusy): + with LibraryLock(library, checkpoint=False): pass + released[0] = True + @contextmanager + def guard(): yield {'target': 'isolated fixture'} + lock = (LibraryLock(library, checkpoint=False) if operation == 'verify' else + LibraryLock.for_maintenance(library, tmp_path/'archive', guard, safe_to_release=lambda: True)) + with lock: pass diff --git a/tests/test_library_lock.py b/tests/test_library_lock.py index fa9d8b3..5b3c4a7 100644 --- a/tests/test_library_lock.py +++ b/tests/test_library_lock.py @@ -65,28 +65,25 @@ def test_verification_refuses_uploader_exclusive_lock_before_native_work(tmp_pat monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('native work bypassed lock')) try: result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) - assert result.status == 'BLOCKED' and not result.safe_to_continue + assert result.status == 'FAIL' and result.safe_to_continue finally: handle.Close() -def test_unsafe_verify_retains_lease_and_persistent_blocker(tmp_path, monkeypatch): +def test_unsafe_verify_records_failure_without_persistent_library_blocker(tmp_path, monkeypatch): + import json import cbb_refresh_verify.verify_runner as runner import cbb_refresh_verify.library_lock as locks library = tmp_path/'lib.lmc'; library.touch() + evidence = tmp_path/'out'; evidence.mkdir() + (evidence/'session-release.json').write_text(json.dumps({'library': str(library), + 'block': 'Block', 'worker_exited': True, 'safe_to_continue': True})) monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: - runner.VerifyResult('FAIL', safe_to_continue=False)) + runner.VerifyResult('FAIL', safe_to_continue=False, evidence_path=evidence/'result.json')) result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out')) assert not result.safe_to_continue - assert locks.blocker_path(library).is_file() - with pytest.raises(locks.LibraryBusy): - with locks.LibraryLock(library): pass - # Simulate OS handle loss on process exit; persistent marker still blocks. - with locks._guard: - owner = locks._owners.pop(str((tmp_path/'.lib.cbb-upload.lock').resolve()).casefold(), None) - if owner: owner['handle'].Close() - with pytest.raises(locks.LibraryBusy): - with locks.LibraryLock(library): pass + assert not locks.blocker_path(library).exists() + with locks.LibraryLock(library): pass def test_explicit_release_requires_fresh_proof_and_never_deletes_sidecar(tmp_path, monkeypatch): diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index 13844c3..edd94fd 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -96,8 +96,9 @@ def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fa return runner.VerifyResult('PASS', library_flag='GREEN') patch_verification_after_fault(monkeypatch, runner, verify) result = runner.run_verify(request) - assert calls == ['repair', 'verify'] and result.status == 'PASS' - assert result.recovery_attempts[0]['validation_passed'] is True + assert calls == ['repair'] and result.status == 'REPAIRED' + assert result.recovery_attempts[0]['validation_passed'] is False + assert result.library_flag == 'UNKNOWN' record = json.loads(result.evidence_path.read_text(encoding='utf-8')) assert record['operation'] == 'explicit_project_maintenance' assert 'session' not in record['maintenance_context'] @@ -118,7 +119,7 @@ def test_addressless_target_binds_unique_project_and_rejects_new_address(case): def test_guard_reuses_full_tree_scan_but_still_refreshes_before_yield(case, monkeypatch): from cbb_refresh_verify.maintenance_entry import maintenance_target_guard import cbb_refresh_verify.scoped_manifest as manifests - request, _, _ = case + request, project, _ = case scans = [] original = manifests.scan_tree def scan(root, **kwargs): @@ -126,7 +127,8 @@ def test_guard_reuses_full_tree_scan_but_still_refreshes_before_yield(case, monk return original(root, **kwargs) monkeypatch.setattr(manifests, 'scan_tree', scan) with maintenance_target_guard(request): pass - assert scans.count(request.lmc_file.parent) == 2 + assert request.lmc_file.parent not in scans + assert scans.count(project.parent) == 2 def test_guard_rejects_new_native_database_appearing_during_checks(case, monkeypatch): @@ -198,12 +200,15 @@ def test_optional_pcb_project_appearance_invalidates_maintenance_proof(case): with maintenance_target_guard(request, before): pass -def test_ordinary_verify_still_blocks_existing_address_without_repair(case, monkeypatch): +def test_ordinary_verify_calls_native_verifier_despite_existing_address(case, monkeypatch): from cbb_refresh_verify import verify_runner as runner, native_repair request, _, address = case monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('ordinary verify must not repair')) + calls = [] + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: + (calls.append('verify') or runner.VerifyResult('FAIL', stderr='native ICDB problem'))) result = runner.run_verify(replace(request, maintenance=False)) - assert result.status == 'BLOCKED' and not result.safe_to_continue + assert calls == ['verify'] and result.status == 'FAIL' and result.safe_to_continue assert address.read_bytes() == b'old server address - preserved' @@ -373,11 +378,11 @@ def test_independent_cbb_address_does_not_select_or_block_current_project(case): @pytest.mark.parametrize('status,complete,expected_safe', [ - ('RESTORED_REVIEW_REQUIRED', True, False), ('DATABASE_BUSY', True, False), - ('UNKNOWN_DIALOG', True, False), ('UNCONFIRMED', True, False), - ('MISSING_HEALTHY_BACKUP', False, False), ('NATIVE_REPAIR_FAILED', False, False), + ('RESTORED_REVIEW_REQUIRED', True, True), ('DATABASE_BUSY', True, True), + ('UNKNOWN_DIALOG', True, True), ('UNCONFIRMED', True, True), + ('MISSING_HEALTHY_BACKUP', False, True), ('NATIVE_REPAIR_FAILED', False, True), ('MISSING_HEALTHY_BACKUP', True, True), ('NATIVE_REPAIR_FAILED', True, True)]) -def test_physical_release_alone_does_not_authorize_queue(case, monkeypatch, status, complete, expected_safe): +def test_failed_content_does_not_block_other_items_after_actual_release(case, monkeypatch, status, complete, expected_safe): from cbb_refresh_verify import native_repair, verify_runner as runner, library_lock from test_native_repair import complete_native_success request, _, address = case @@ -410,10 +415,10 @@ def test_successful_icdb_repair_then_version_choice_reports_partial_completion(c error_category='PCB_VERSION_CHOICE_REQUIRED', stderr='PCB版本待选:最后用户保存/自动保存 2026/9/24 23:06', safe_to_continue=False, evidence_path=request.evidence_dir/'verify/result.json')) result = runner.run_verify(request) - assert result.status == 'FAIL' and not result.safe_to_continue - assert 'ICDB原生修复已完成' in result.stderr and '尚未校验通过' in result.stderr + assert result.status == 'REPAIRED' and result.safe_to_continue + assert '待单独校验' in result.stdout assert result.recovery_attempts[0]['native_status'] == 'REPAIRED' - assert result.recovery_attempts[0]['status'] == 'PCB_VERSION_CHOICE_REQUIRED' + assert result.recovery_attempts[0]['status'] == 'REPAIRED_UNVERIFIED' assert result.recovery_attempts[0]['validation_passed'] is False @@ -431,6 +436,6 @@ def test_user_chosen_pcb_history_is_not_reported_as_lossless_repair(case, monkey return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=root/'result.json') patch_verification_after_fault(monkeypatch, runner, verify) result = runner.run_verify(request) - assert result.status == 'PASS' - assert result.recovery_attempts[0]['status'] == 'RESTORED_REVIEW_REQUIRED' - assert result.recovery_attempts[0]['pcb_version']['choice'] == 'last_user_saved' + assert result.status == 'REPAIRED' and result.library_flag == 'UNKNOWN' + assert result.recovery_attempts[0]['status'] == 'REPAIRED_UNVERIFIED' + assert not (request.evidence_dir/'verify').exists() diff --git a/tests/test_maintenance_ui.py b/tests/test_maintenance_ui.py index 65cf036..33f203d 100644 --- a/tests/test_maintenance_ui.py +++ b/tests/test_maintenance_ui.py @@ -24,15 +24,18 @@ def test_only_confirmed_maintenance_sets_explicit_flag(qtbot, tmp_path, monkeypa @pytest.mark.parametrize('marker_kind', ['valid', 'foreign', 'malformed']) -def test_blocked_library_recovery_view_is_filesystem_only_and_maintenance_only(qtbot, tmp_path, monkeypatch, marker_kind): +def test_old_checkpoint_does_not_force_maintenance_or_disable_verify(qtbot, tmp_path, monkeypatch, marker_kind): from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard from cbb_refresh_verify.ui.main_window import MainWindow from cbb_refresh_verify.verify_runner import VerifyResult + from cbb_refresh_verify.models import LibraryReadResult, CbbRecord import cbb_refresh_verify.ui.main_window as ui library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch() (library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True) with LibraryLock(library, safe_to_release=lambda: False) as lock: pass - with _guard: _owners.pop(lock.key)['handle'].Close() + with _guard: + owner = _owners.pop(lock.key, None) + if owner: owner['handle'].Close() marker = blocker_path(library) if marker_kind == 'malformed': marker.write_text('{}') if marker_kind == 'foreign': @@ -40,31 +43,26 @@ def test_blocked_library_recovery_view_is_filesystem_only_and_maintenance_only(q marker.write_text(json.dumps(record)) monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None) calls = [] - window = MainWindow(reader=lambda *a: pytest.fail('blocked view called native reader'), + window = MainWindow(reader=lambda *a: LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),)), verifier=lambda req: (calls.append(req) or VerifyResult('BLOCKED', safe_to_continue=False)), - status_refresher=lambda *a: pytest.fail('unsafe view refreshed native status'), data_root=tmp_path/'data') + status_refresher=lambda *a: {'IP101GR': 'PENDING'}, data_root=tmp_path/'data') qtbot.addWidget(window) window.lmc_edit.setText(str(library)); window.load_library() qtbot.waitUntil(lambda: not window.busy) assert window.release_library_button.isEnabled() - if marker_kind != 'valid': - assert window.loaded_path is None and not window.icdb_repair_button.isEnabled() - return assert window.visible_records()[0].name == 'IP101GR' - assert window.visible_records()[0].status == 'UNKNOWN' - assert '未确认' in window.status_label.text() - assert 'ICDB' in window.management_capability_label.text() - assert '会话' in window.add_button.toolTip() + assert window.visible_records()[0].status == 'PENDING' + assert not window._maintenance_recovery_view window.select_visible_button.click() assert window.icdb_repair_button.isEnabled() - assert not any(button.isEnabled() for button in (window.batch_button, window.selected_batch_button, - window.backup_restore_button, window.add_button, window.update_button, window.remove_button)) - window.verify_all(); window.verify_selected(); window.restore_backup(); window.manage_cbb('remove') - assert window.refresh_affected_records(['IP101GR']) is False - assert calls == [] and not window.busy + assert window.batch_button.isEnabled() and window.selected_batch_button.isEnabled() monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) + window.verify_selected(); qtbot.waitUntil(lambda: not window.busy) + assert len(calls) == 1 and calls[0].maintenance is False + assert window.selected_batch_button.isEnabled() window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy) - assert len(calls) == 1 and calls[0].maintenance is True + assert len(calls) == 2 and calls[1].maintenance is True + assert marker.exists() def test_maintenance_summary_exposes_first_failure_reason(qtbot, tmp_path): @@ -92,11 +90,13 @@ def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qt library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch() (library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True) with LibraryLock(library, safe_to_release=lambda: False) as lease: pass - with _guard: _owners.pop(lease.key)['handle'].Close() + with _guard: + owner = _owners.pop(lease.key, None) + if owner: owner['handle'].Close() reads = [] def reader(path): reads.append(path) - return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='READY'),)) + return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),)) def verify(req): assert req.maintenance blocker_path(library).unlink() # Simulate normal cleanup at the injected boundary. @@ -104,12 +104,12 @@ def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qt monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None) monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes) window = MainWindow(reader=reader, verifier=verify, data_root=tmp_path/'data', - status_refresher=lambda *a: pytest.fail('recovery view must reload after worker completion')) + status_refresher=lambda *a: {'IP101GR': 'READY'}) qtbot.addWidget(window) window.lmc_edit.setText(str(library)); window.load_library(); qtbot.waitUntil(lambda: not window.busy) - assert reads == [] + assert len(reads) == 1 window.search_edit.setText('IP101') window.repair_icdb_all() - qtbot.waitUntil(lambda: not window.busy and bool(reads)) + qtbot.waitUntil(lambda: not window.busy) assert window.visible_records()[0].status == 'READY' assert not window._maintenance_recovery_view and window.search_edit.text() == 'IP101' diff --git a/tests/test_native_export_guard.py b/tests/test_native_export_guard.py index 1b32fbb..cdc5e79 100644 --- a/tests/test_native_export_guard.py +++ b/tests/test_native_export_guard.py @@ -13,6 +13,7 @@ def export_case(tmp_path, monkeypatch): import cbb_refresh_verify.native_engineering_identity as identity import win32com.client monkeypatch.setattr(win32com.client, 'GetObject', lambda *_: SimpleNamespace(ExecQuery=lambda *_: [])) + monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k: []) monkeypatch.setattr(identity, '_process_snapshot', lambda: []) monkeypatch.setattr(identity, '_retained_exports', []) monkeypatch.setattr(locks, '_supervisor_alive', lambda _: False) diff --git a/tests/test_native_repair.py b/tests/test_native_repair.py index 870a77d..ab99ad8 100644 --- a/tests/test_native_repair.py +++ b/tests/test_native_repair.py @@ -5,6 +5,7 @@ import pytest def target_fixture(tmp_path): + from dataclasses import replace from test_verify_runner import fixture, request from cbb_refresh_verify.native_repair import select_repair_target work = fixture(tmp_path) @@ -13,7 +14,8 @@ def target_fixture(tmp_path): (database/'icdb.dat').write_bytes(b'original damaged database') diagnosis = {'error_code': 'ICDB_REPAIR_REQUIRED', 'machine': 'OLD-PC', 'database_path': str(database), 'session': '2026-10-24 08.22.29-10308', 'scope': 'project'} - return request(tmp_path), diagnosis, select_repair_target(request(tmp_path), diagnosis) + req = replace(request(tmp_path), auto_repair=True) + return req, diagnosis, select_repair_target(req, diagnosis) def complete_native_success(target): @@ -511,7 +513,8 @@ def test_invoked_busy_native_result_never_reopens_or_continues_queue(tmp_path, m busy = json.loads((root/'repair-result.json').read_text('utf-8')) assert (100, win32con.WM_CLOSE) not in actions assert busy['status'] == 'DATABASE_BUSY' and busy['release']['native_exited'] is False - req = request(tmp_path) + from dataclasses import replace + req = replace(request(tmp_path), auto_repair=True) target = json.loads(job.read_text('utf-8')) evidence = tmp_path/'first/result.json' evidence.parent.mkdir() -- 2.39.5 From 00db1cf5f27333cff98739028b68673e93ff6213 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: Wed, 4 Nov 2026 20:51:35 +0800 Subject: [PATCH 53/59] fix: continue released verify batches and bound automation shutdown --- docs/diagnostics/2026-11-04-batch-blockers.md | 37 +++ .../diagnostics/2026-11-04-issue4-followup.md | 2 + packaging/CbbRefreshVerify.spec | 17 +- src/cbb_refresh_verify/batch_status.py | 7 +- src/cbb_refresh_verify/desktop_worker.py | 48 +++- src/cbb_refresh_verify/mentor_adapter.py | 47 ++-- src/cbb_refresh_verify/owner_probe.py | 8 +- .../save_verified_block.vbs | 9 +- src/cbb_refresh_verify/ui/main_window.py | 4 +- src/cbb_refresh_verify/verify_runner.py | 105 ++++++-- tests/test_abnormal_shutdown.py | 2 +- tests/test_backup_recovery.py | 4 + tests/test_batch_blockers.py | 243 ++++++++++++++++++ tests/test_batch_status.py | 23 ++ tests/test_failure_lifecycle.py | 22 +- tests/test_icdb_recovery.py | 2 +- tests/test_incident_safety.py | 17 +- tests/test_independent_operations.py | 4 +- tests/test_maintenance_diagnosis.py | 9 +- tests/test_management_ui.py | 9 +- tests/test_native_repair.py | 8 +- tests/test_save_helper_runtime.py | 18 +- tests/test_verify_runner.py | 20 +- 23 files changed, 550 insertions(+), 115 deletions(-) create mode 100644 docs/diagnostics/2026-11-04-batch-blockers.md create mode 100644 tests/test_batch_blockers.py diff --git a/docs/diagnostics/2026-11-04-batch-blockers.md b/docs/diagnostics/2026-11-04-batch-blockers.md new file mode 100644 index 0000000..8067319 --- /dev/null +++ b/docs/diagnostics/2026-11-04-batch-blockers.md @@ -0,0 +1,37 @@ +# 普通校验阻塞修正 + +基准 `477a5a4`。按用户最新指示暂停 ICDB 自动修复研究,本轮只修普通校验的入口、单项失败后的继续条件、取消和退出,以及状态桥部署/重试。没有对原库运行新的校验、Repair、恢复或移除;遗留的 Project Backup 进程未被终止。 + +## 修改依据与最终行为 + +| 阻塞来源 | 修改 | +| --- | --- | +| 原生进程已退出,但单项失败仍使整批跳过 | 统一使用本轮、同库、同 CBB 的释放记录判断继续资格。FAIL/TIMEOUT/未知弹窗保持原失败状态和备注;自动化工作进程、保存/失败关闭助手及本任务原生进程全部确认退出后可继续下一项。失败不会变成 PASS。 | +| 保存或失败关闭助手漏检;工作进程已关闭句柄后再查询退出状态 | 停止和检查两类精确脚本路径、精确父 PID 的助手。退出状态在关闭句柄前读取并缓存,清理可重复调用;助手和原生 PID 合并记录。任何查询异常或客户端仍活着都不解除执行锁。 | +| 超时只写取消文件,客户端可能继续 Save/Close | 先请求协作取消,宽限到期后仅停止本任务自动化工作进程及已识别的自动化助手。保留原生 PCB/iCDB,不终止进程树。普通校验的取消事件已传入监督循环。监督程序丢失后,Verify 看门狗也限时停止已登记客户端。 | +| 启动器和迟到子进程未完整纳入清理 | 监督器清理前重读剩余事件中的目标/启动器 PID;追踪这些已确认原生进程的新后代,排除任务开始前的无关会话。 | +| 任意 PCB 已打开就禁止校验 | 精确命令参数指向同一 PCB 时才阻止启动;其他工程保留。实际 COM 文档身份和保存助手身份检查仍必须通过。保存关闭后存在其他文档时,不调用该应用的 Quit。 | +| WMI 占用查询可能无限等待 | 普通校验的进程查询统一复用已有隔离查询助手,最多等待 10 秒。超时表示无法确认,保留具体原因;不在 GUI 线程做 WMI。 | +| 端口少量抽样被当作整个原生端口区间不可用 | 默认仅发出警告并执行真实原生调用。保留内部显式严格诊断参数,不作为默认入口条件。 | +| `0 errors` 被错误关键字命中;原生工程编码误判 | 仅忽略完整的零错误计数,仍拒绝非零错误、明确错误或坏退出码;复用现有 UTF-8 BOM / UTF-16 / Windows ACP 工程读取器。 | +| 状态桥 x86 MFC 运行库缺失;加载失败对每项重复启动 | 打包时验证并携带 x86 MFC/VC 运行库到桥的独立目录,避免误用 x64 Qt DLL。识别明确的 Windows DLL 缺失/架构错误,只记录一次全局桥故障并为余项保留状态;不因此禁用维护或管理入口。 | +| 状态查询重试复用旧 TIMEOUT/ERROR | 断点读取仅复用 READY/PENDING;失败状态下次重查,不要求修改库文件才能重新查询。普通界面读取仍使用本轮权威状态。 | + +执行互斥只用于实际仍运行/释放未确认的客户端;历史诊断和 CBB 内容问题不应变成跨功能永久禁用。处于原生操作中的未知弹窗不会被盲目确认:当前项失败并留证,是否继续仍取决于该操作是否实际结束。 + +## 验证 + +新回归先复现单项已释放仍停批、取消未传递、零错误误判、编码与端口拦截等问题,再验证修改。还覆盖原生迟到子进程、保存/失败关闭助手保留,以及已关闭 Win32 句柄不再被读取。 + +使用真实 Windows 子进程验证监督程序退出后的客户端停止和原生模拟进程保留;VBS 在真正的 32 位 cscript 中使用伪 COM 对象验证 Save/Close、取消及剩余文档不被 Quit。它们没有加载现场工程,也不等于 Mentor 实机验收。 + +最终完整 Python 回归、冻结模块一致性、程序启动/正常退出、桥 JSON 编解码及实际加载 app-local x86 DLL 的结果记录在 `diagnostics/batch-blockers-20261104/`。现场日志和原始证据不随代码提交。 + +- 最终完整回归:**1,244 passed**,退出码 0,222.90 秒;1 条既有 pywinauto STA 警告。`pytest-final.log` / `pytest-final-result.json`。 +- PyInstaller 构建、编译和差异检查通过。10 个关键 Python 模块及两个 VBS 脚本与冻结程序一致,启动及正常退出冒烟返回 0。 +- 桥的 Unicode/UNC 编解码返回 0。通过真实进程模块枚举确认 `mfc140.dll`、`MSVCP140.dll`、`VCRUNTIME140.dll` 均从包内桥目录加载;不是仅检查文件名存在。系统 PATH 缩为 Windows System32,本检查没有加载 Mentor 数据库。 +- 独立审查发现并修正失败关闭助手漏检;复审无剩余必要 P1/P2。构建记录保留 `icdb_automatic_repair_acceptance=false`、`release_ready=false`。 + +## 仍待验证 + +没有把本次阻塞修正称为 ICDB 自动修复成功。Project Backup 挂起问题保持暂停;Windows 10 干净系统、其他 Mentor 环境、两个真实 PCB 同时打开的 COM 注册行为、跨机并发写库、3000 项网络库完整耗时仍需实机验证。状态桥无法读取时仍不能宣称 READY;本轮补齐了已确认的 x86 部署依赖,没有伪造状态,也未加入新的全功能禁用入口。 diff --git a/docs/diagnostics/2026-11-04-issue4-followup.md b/docs/diagnostics/2026-11-04-issue4-followup.md index fc039e8..785bf6c 100644 --- a/docs/diagnostics/2026-11-04-issue4-followup.md +++ b/docs/diagnostics/2026-11-04-issue4-followup.md @@ -2,6 +2,8 @@ 复核基准:最新工单 [#4](https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/issues/4)(更新于 2026-11-04 15:20),当前代码起点 `2587d33`,用户随后要求校验与 ICDB 维护独立执行。原工单基准为 `29959e2`,下表按当前源码重新核对,不能照搬原报告的测试数或认定全部已修复。 +后续用户要求暂停 ICDB 自动修复、先修阻塞。新的修改和最终 1,244 项完整回归见 [普通校验阻塞修正](2026-11-04-batch-blockers.md);下表保留首次复核状态,不能作为最新未修项数量使用。 + ## 本次用户失败的实际链路 最新测试程序的日志位于 `dist/issue2-tabs-release-candidate/CbbRefreshVerify-Background/data/`。北京时间 17:41:55 开始维护 `1GPHY_8212`,17:42:24 进入原生检查,17:43:17 启动 Project Backup,17:43:50 报 `TIMEOUT_UNRELEASED`。本轮没有发出 Repair,8 个后续条目被跳过。17:44:54 再运行另一工程,遗留 PID 60000 被全局进程检查拦截。 diff --git a/packaging/CbbRefreshVerify.spec b/packaging/CbbRefreshVerify.spec index 69e2bcc..3a0ef58 100644 --- a/packaging/CbbRefreshVerify.spec +++ b/packaging/CbbRefreshVerify.spec @@ -1,8 +1,23 @@ from pathlib import Path +import os +import struct project_root = Path(SPECPATH).parent assets_root = project_root / 'src' / 'cbb_refresh_verify' / 'assets' bridge_path = project_root / 'tools' / 'status_bridge' / 'bin' / 'CbbStatusBridge32.exe' verify_probe_path = project_root / 'tools' / 'status_bridge' / 'bin' / 'VerifyProbe32.exe' +# App-local x86 runtime belongs beside the x86 bridge, separate from Qt/Python. +bridge_runtime = [] +if bridge_path.is_file(): + runtime_root = Path(os.environ.get('WINDIR', r'C:\Windows')) / 'SysWOW64' + for name in ('mfc140.dll', 'msvcp140.dll', 'vcruntime140.dll', 'concrt140.dll'): + runtime = runtime_root / name + if not runtime.is_file(): + raise RuntimeError(f'Install the Microsoft VC++ 2015-2022 x86 runtime before packaging: {runtime}') + data = runtime.read_bytes() + pe_offset = struct.unpack_from(' 0) @@ -658,12 +672,23 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_runner=None, - port_check=None, dialog_supervisor_factory=None, version_prompt=None): + port_check=None, dialog_supervisor_factory=None, version_prompt=None, stop=None): timing = RunTimings() result = _run_verify(request, timing=timing, progress=progress, flag_reader=flag_reader, process_runner=process_runner, port_check=port_check, - dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt) - if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'} and result.evidence_path: + dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt, stop=stop) + if not request.auto_repair and result.evidence_path: + try: + release = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8')) + if (release.get('block') == request.block_name + and Path(release['library']).resolve() == Path(request.lmc_file).resolve()): + released = release.get('worker_exited') is True and release.get('safe_to_continue') is True + result = replace(result, safe_to_continue=released) + if not released and result.status == 'PASS': + result = replace(result, status='FAIL', stderr='原生会话或自动化客户端尚未退出。') + except (OSError, ValueError, KeyError): + pass + if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP', 'CANCELLED'} and result.evidence_path: job_path = result.evidence_path.with_name('request.json') if job_path.is_file(): timing.begin('failure_diagnostics') @@ -690,16 +715,6 @@ def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_ if recovery and RECOVERY_ACTION not in detail: detail = (detail + '\n' + recovery_detail(recovery)).strip() result = replace(result, stderr=detail, error_category=ERROR_CODE) - if not request.auto_repair and result.evidence_path: - try: - release = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8')) - if (result.error_stage in {'PCB_STARTING', 'pcb_startup'} - and release.get('worker_exited') is True and release.get('safe_to_continue') is True - and release.get('block') == request.block_name - and Path(release['library']).resolve() == Path(request.lmc_file).resolve()): - result = replace(result, safe_to_continue=True) - except (OSError, ValueError, KeyError): - pass if recovery and result.evidence_path: try: record_path = result.evidence_path.parent/'diagnostics/icdb-recovery.json' @@ -737,7 +752,7 @@ def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_runner=None, port_check=None, - dialog_supervisor_factory=None, version_prompt=None): + dialog_supervisor_factory=None, version_prompt=None, stop=None): """Refresh one CBB and record its evidence. ``flag_reader`` is a read-only probe returning an authoritative ``BridgeResult``. @@ -769,6 +784,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run pass return replace(result, evidence_path=result_path) + if stop is not None and stop.is_set(): + return finish(VerifyResult('CANCELLED', stderr='已取消,未启动校验。')) timing.begin('preflight') try: project, pcb = preflight(request) @@ -809,8 +826,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run if progress: progress({'stage': 'content_check_done', 'block': request.block_name, 'status': 'PASS'}) - # The design server needs a port in 10000-10100; a system reservation there - # blocks every item, so fail fast with the fix instead of timing out. + # Sampling cannot establish that the whole native port range is unusable. from .mentor_adapter import icdb_ports_bindable check_ports = port_check or icdb_ports_bindable ports_ok, ports_detail = check_ports() @@ -898,6 +914,10 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run ) Path(job['start_gate']).touch() while process.poll() is None: + if stop is not None and stop.is_set(): + _stop_worker(process) + return finish(VerifyResult('CANCELLED', stderr='已取消当前校验。', + safe_to_continue=False, error_stage=phase.value)) if events.exists(): with events.open(encoding='utf-8') as stream: stream.seek(offset) @@ -1043,7 +1063,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run 'observation': last_post_verify_observation, }) return finish(VerifyResult(status, stderr=detail, safe_to_continue=False)) - return finish(VerifyResult('TIMEOUT', stderr='操作超时;已停止自动化并保留原生会话,停止批次,请检查恢复记录。', error_stage=phase.value, safe_to_continue=False)) + return finish(VerifyResult('TIMEOUT', stderr='操作超时,当前项未完成。', error_stage=phase.value, safe_to_continue=False)) time.sleep(.2) worker_result = root/'outcome.json' if not worker_result.exists(): @@ -1058,6 +1078,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run return finish(VerifyResult('FAIL', stderr=str(error), safe_to_continue=False)) finally: close_errors = [] + worker_exited = False + automation_release = {'safe_to_continue': False} if 'dialog_agent' in locals() and dialog_agent is not None: try: dialog_agent.close() @@ -1065,15 +1087,37 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run close_errors.append(str(error)) if 'process' in locals(): try: + worker_exited = process.poll() is not None + if not worker_exited: + worker_exited = _stop_worker(process) + if hasattr(process, 'automation_release_state'): + automation_release = process.automation_release_state() + else: + automation_release = {'safe_to_continue': worker_exited, 'remaining_pids': []} process.close() except Exception as error: close_errors.append(str(error)) if 'process' in locals(): try: + if events.is_file(): + with events.open(encoding='utf-8') as stream: + stream.seek(offset) + for line in stream: + try: + event = json.loads(line) + timing.record(event) + if event.get('pcb_pid'): target_pid = event['pcb_pid'] + if event.get('launcher_pid'): launcher_pid = event['launcher_pid'] + if event.get('lm_pid'): lm_pid = event['lm_pid'] + if event.get('stage') == 'pcb_baseline': pcb_baseline = set(event.get('pids', ())) + except (json.JSONDecodeError, ValueError): + continue + offset = stream.tell() release_result = _cleanup_session_processes( root, lm_pid=lm_pid, target_pid=target_pid, + launcher_pid=launcher_pid, pcb=pcb, pcb_baseline=pcb_baseline, progress=progress, @@ -1090,17 +1134,22 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run if close_errors: release_result = {**(release_result or {}), 'safe_to_continue': False, 'close_errors': close_errors} + if not worker_exited or not automation_release.get('safe_to_continue'): + release_result = {**(release_result or {}), 'safe_to_continue': False, + 'automation_release': automation_release, 'worker_exited': worker_exited} + release_result['remaining_pids'] = sorted(set(release_result.get('remaining_pids', [])) + | set(automation_release.get('remaining_pids', []))) try: (root/'session-release.json').write_text(json.dumps({ **(release_result or {}), 'block': request.block_name, - 'library': str(request.lmc_file), 'worker_exited': process.poll() is not None, + 'library': str(request.lmc_file), 'worker_exited': worker_exited, 'worker_pid': getattr(process, 'pid', None)}, ensure_ascii=False, default=str, indent=2), encoding='utf-8') except OSError: pass if not release_result or not release_result.get('safe_to_continue'): if 'result' in locals(): - result = replace(result, status='FAIL', safe_to_continue=False, + result = replace(result, status='FAIL' if result.status == 'PASS' else result.status, safe_to_continue=False, stderr=(result.stderr + '\nPCB_RELEASE_UNCONFIRMED: 未确认原生会话退出').strip()) recovery = {'desktop': job['desktop'], 'project': str(project), 'pcb': str(pcb), 'release': release_result, @@ -1169,7 +1218,7 @@ def run_batch(requests, max_workers=1, verifier=None, *, stop=None, progress=Non result = VerifyResult('FAIL', stderr=str(error), safe_to_continue=False) results.append(result) # Never start another writer after an unconfirmed native shutdown. - queue_stop = queue_stop or result.status == 'QUEUE_STOP' or not result.safe_to_continue + queue_stop = queue_stop or result.status in {'QUEUE_STOP', 'CANCELLED'} or not result.safe_to_continue if queue_stop and stop is not None: stop.set() if progress: progress(request.block_name, result) diff --git a/tests/test_abnormal_shutdown.py b/tests/test_abnormal_shutdown.py index 5ee799a..9290975 100644 --- a/tests/test_abnormal_shutdown.py +++ b/tests/test_abnormal_shutdown.py @@ -69,7 +69,7 @@ def test_crashed_worker_cannot_pass_even_with_pass_file(tmp_path, monkeypatch, o pytest.fail('A crashed writer must not become PASS via a pre-existing READY flag') result = run_verify(request(tmp_path), port_check=lambda: (True, ''), flag_reader=must_not_query) assert result.status == 'FAIL' - assert result.safe_to_continue is False + assert result.safe_to_continue is True # Proven release permits another item; crash stays FAIL. def test_supervisor_crash_stops_registered_save_helper_but_not_native(tmp_path): diff --git a/tests/test_backup_recovery.py b/tests/test_backup_recovery.py index e947d9d..8ffd433 100644 --- a/tests/test_backup_recovery.py +++ b/tests/test_backup_recovery.py @@ -144,6 +144,10 @@ def test_idle_guard_queries_supported_native_process_names(recovery_files, monke queries.append(query) return [] monkeypatch.setattr(win32com.client, 'GetObject', lambda _: EmptyProcesses()) + from cbb_refresh_verify import owner_probe + import subprocess + monkeypatch.setattr(owner_probe.subprocess, 'run', lambda command, **kw: + subprocess.CompletedProcess(command, owner_probe.owner_probe_main(command[-1]))) _idle_check(request.lmc_file, original, original.parent/'PCB/A.pcb') assert any("Name='viewdraw.exe'" in query for query in queries) diff --git a/tests/test_batch_blockers.py b/tests/test_batch_blockers.py new file mode 100644 index 0000000..4fd069a --- /dev/null +++ b/tests/test_batch_blockers.py @@ -0,0 +1,243 @@ +"""Only live clients stop a batch; native writes are replaced by fixtures.""" +import json +from pathlib import Path +from threading import Event + +import pytest + +from cbb_refresh_verify import verify_runner as runner +from test_verify_runner import fixture, request, PORTS_OK + + +@pytest.mark.parametrize('status,detail', [('FAIL', 'UNKNOWN_DIALOG: native prompt'), + ('TIMEOUT', 'native operation timed out')]) +@pytest.mark.parametrize('released', [True, False]) +def test_batch_continuation_follows_actual_release(tmp_path, monkeypatch, status, detail, released): + fixture(tmp_path) + launches = [] + class Worker: + desktop = None + returncode = 1 + def __init__(self, command, *_a, **_k): + launches.append(command) + Path(command[-1]).with_name('outcome.json').write_text(json.dumps({ + 'status': status, 'stderr': detail, 'safe_to_continue': False})) + def poll(self): return 1 + def close(self): pass + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker) + monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: + {'safe_to_continue': released, 'remaining_pids': [] if released else [123]}) + monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None) + req = request(tmp_path) + try: + results = runner.run_batch([req, req], verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK)) + assert results[0].status == status and results[0].safe_to_continue is released + assert len(launches) == (2 if released else 1) + assert results[1].status == (status if released else 'SKIPPED') + finally: + from cbb_refresh_verify import library_lock as locks + with locks._guard: + owner = locks._owners.pop(locks.LibraryLock(req.lmc_file).key, None) + if owner: owner['handle'].Close() + + +def test_stop_worker_escalates_only_after_cooperative_deadline(): + calls = [] + class Worker: + stopped = False + def request_stop(self): calls.append('cancel') + def wait(self, timeout=5): + calls.append('wait') + if not self.stopped: raise TimeoutError() + def kill(self): calls.append('client_stop'); self.stopped = True + def poll(self): return 1 if self.stopped else None + assert runner._stop_worker(Worker(), timeout=.1) is True + assert calls == ['cancel', 'wait', 'client_stop', 'wait'] + + +def test_cancel_reaches_current_supervised_worker(tmp_path, monkeypatch): + fixture(tmp_path) + stop = Event() + cancelled = [] + class Worker: + desktop = None + returncode = None + def __init__(self, command, *_a, **_k): + self.job = json.loads(Path(command[-1]).read_text('utf-8')) + stop.set() + def bind_cancellation(self, path): self.cancel = Path(path) + def request_stop(self): + self.cancel.touch(); cancelled.append(self.cancel) + self.returncode = 1 + def wait(self, **_k): return 1 + def poll(self): return self.returncode + def close(self): pass + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker) + monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True}) + monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None) + result = runner.run_verify(request(tmp_path), stop=stop, port_check=PORTS_OK) + assert result.status == 'CANCELLED' and result.safe_to_continue + assert len(cancelled) == 1 and cancelled[0].is_file() + + +@pytest.mark.parametrize('text,expected', [ + ('content checking completed successfully! 0 errors, 0 warnings', 'PASS'), + ('content checking completed successfully! 10 errors', 'FAIL'), + ('content checking completed successfully! error: missing symbol', 'FAIL'), + ('content checking completed successfully! 0 errors\nERROR: cannot connect', 'FAIL'), +]) +def test_zero_error_count_does_not_block_content_check(text, expected): + assert runner.classify_output(0, text) == expected + + +@pytest.mark.parametrize('codec', ['utf-16', 'utf-8-sig', 'mbcs']) +def test_preflight_reads_native_project_encodings(tmp_path, codec): + work = fixture(tmp_path) + (work/'CBB1.prj').write_bytes(('# 中文工程\nKEY PCBDesignPath "PCB\\actual.pcb"\n').encode(codec)) + project, pcb = runner.preflight(request(tmp_path)) + assert pcb == work/'PCB/actual.pcb' + + +def test_unrelated_pcb_does_not_block_target_before_launch(tmp_path, monkeypatch): + from test_verify_worker_stability import exercise_worker + from cbb_refresh_verify import mentor_adapter as adapter + job, events, execute = exercise_worker(tmp_path, monkeypatch) + monkeypatch.setattr(adapter, 'pcb_processes', lambda: ((999, 'ExpeditionPCB.exe', '"ExpeditionPCB.exe" "C:\\other\\board.pcb"'),)) + assert execute()['status'] == 'PASS' + assert any(e['stage'] == 'pcb_launch' for e in events) + + +def test_sampling_ports_only_warn_by_default(tmp_path, monkeypatch): + fixture(tmp_path) + launches, warnings = [], [] + class Worker: + desktop = None + returncode = 0 + def __init__(self, command, *_a, **_k): + launches.append(command) + Path(command[-1]).with_name('outcome.json').write_text('{"status":"PASS"}') + def poll(self): return 0 + def close(self): pass + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker) + monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True}) + result = runner.run_verify(request(tmp_path), port_check=lambda: (False, 'sample unavailable'), + flag_reader=lambda *_: __import__('cbb_refresh_verify.status_bridge', fromlist=['BridgeResult']).BridgeResult('READY', 'IsVerifiedReuseBlock', 1), + progress=warnings.append) + assert launches and result.status == 'PASS' + assert any(e['stage'] == 'port_warning' for e in warnings) + + +def test_cleanup_includes_late_native_descendant_without_pcb_argument(monkeypatch): + from cbb_refresh_verify import mentor_adapter as adapter + rows = [(10, 'ExpeditionPCB.exe', '', 1), (11, 'ExpeditionPCB.exe', '', 10), + (99, 'ExpeditionPCB.exe', '', 77)] + monkeypatch.setattr(adapter, '_process_details', lambda name: rows if name == 'ExpeditionPCB.exe' else []) + result = adapter.cleanup_verify_processes(None, None, 10, baseline_pids={99}) + assert not result['safe_to_continue'] and result['remaining_pids'] == [10, 11] + + +def test_save_client_without_worker_still_prevents_continuation(tmp_path, monkeypatch): + fixture(tmp_path) + class Worker: + desktop = None + returncode = 1 + def __init__(self, command, *_a, **_k): + Path(command[-1]).with_name('outcome.json').write_text('{"status":"FAIL"}') + def poll(self): return 1 + def close(self): pass + def automation_release_state(self): return {'safe_to_continue': False, 'remaining_pids': [333]} + monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker) + monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True}) + monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None) + req = request(tmp_path) + try: + result = runner.run_verify(req, port_check=PORTS_OK) + assert not result.safe_to_continue + proof = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8')) + assert proof['worker_exited'] is True + assert proof['automation_release']['remaining_pids'] == [333] + assert proof['remaining_pids'] == [333] + finally: + from cbb_refresh_verify import library_lock as locks + with locks._guard: + owner = locks._owners.pop(locks.LibraryLock(req.lmc_file).key, None) + if owner: owner['handle'].Close() + + +def test_closed_desktop_worker_keeps_exit_status_without_using_closed_handle(monkeypatch): + from types import SimpleNamespace + from cbb_refresh_verify import desktop_worker as desktop + worker = desktop.DesktopWorker.__new__(desktop.DesktopWorker) + handle = SimpleNamespace(closed=False) + def close(): handle.closed = True + handle.Close = close + worker.handle = handle + worker.job = SimpleNamespace(Close=lambda: None) + worker.desktop = None; worker.companions = []; worker.companion_pids = [] + def code(h): + assert not h.closed, 'Read from a closed Win32 process handle' + return 1 + monkeypatch.setattr(desktop.win32process, 'GetExitCodeProcess', code) + worker.close() + assert worker.poll() == 1 + worker.close() # Idempotent cleanup. + + +def test_native_process_queries_use_bounded_shared_helper(monkeypatch): + from cbb_refresh_verify import mentor_adapter as adapter + calls = [] + def query(names): + calls.append(names) + return [{'pid': 12, 'executable': 'ExpeditionPCB.exe', 'command': 'native', 'parent': 11}] + monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', query) + assert adapter._process_details('ExpeditionPCB.exe') == [(12, 'ExpeditionPCB.exe', 'native', 11)] + assert calls == [('ExpeditionPCB.exe',)] + + +@pytest.mark.parametrize('script', ['save_verified_block.vbs', 'close_failed_block.vbs']) +def test_save_and_failure_close_clients_are_both_tracked(script, monkeypatch): + from cbb_refresh_verify import desktop_worker as desktop + worker = desktop.DesktopWorker.__new__(desktop.DesktopWorker) + worker.pid = 11; worker._closed = True; worker._exit_code = 1 + command = f'cscript.exe //nologo "{Path(desktop.__file__).with_name(script)}" "C:\\fixture\\design.prj"' + monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k: [ + {'pid': 22, 'parent': 11, 'command': command}, + {'pid': 33, 'parent': 99, 'command': command}, + {'pid': 44, 'parent': 11, 'command': 'cscript.exe unrelated.vbs'}]) + proof = worker.automation_release_state() + assert proof['safe_to_continue'] is False and proof['remaining_pids'] == [22] + + +def test_watchdog_with_cancel_signal_stops_clients_after_supervisor_exit(tmp_path): + import subprocess, sys, time + import win32api, win32con, win32process + supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)']) + script = tmp_path/'watchdog.py'; ready = tmp_path/'ready.json'; cancel = tmp_path/'cancel.signal' + script.write_text( + 'import sys, subprocess, time, json\nfrom pathlib import Path\n' + 'from cbb_refresh_verify.desktop_worker import start_supervisor_watchdog, launch_save_helper\n' + f'start_supervisor_watchdog({supervisor.pid}, cancel_path={str(cancel)!r}, stop_clients=True)\n' + 'native = subprocess.Popen([sys.executable,"-c","import time; time.sleep(30)"])\n' + 'client = launch_save_helper([sys.executable,"-c","import time; time.sleep(30)"])\n' + f'Path({str(ready)!r}).write_text(json.dumps([native.pid,client.pid]))\n' + 'time.sleep(30)\n', encoding='utf-8') + worker = subprocess.Popen([sys.executable, str(script)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + handles = [] + try: + deadline = time.monotonic()+10 + while not ready.exists() and worker.poll() is None and time.monotonic()= .08 if save_ok: - assert completed.returncode == 0, completed.stderr + assert completed.returncode == (10 if other_document else 0), completed.stderr assert 'SAVE_OK' in parsed['markers'] assert 'CLOSE_OK' in parsed['markers'] assert parsed['timings_seconds']['post_save_check'] >= 0 assert parsed['timings_seconds']['close'] >= 0 + assert ('TEST_QUIT' in completed.stdout) is (not other_document) + assert ('APP_RETAINED_OTHER_DOCUMENT' in completed.stdout) is other_document else: assert completed.returncode == 4, completed.stderr assert 'SAVE_OK' not in parsed['markers'] diff --git a/tests/test_verify_runner.py b/tests/test_verify_runner.py index 0c2ae51..beca08d 100644 --- a/tests/test_verify_runner.py +++ b/tests/test_verify_runner.py @@ -499,7 +499,7 @@ def test_parallelism_rejected_for_shared_library(tmp_path): run_batch([], max_workers=2) -def test_timeout_stops_automation_and_blocks_next_item(tmp_path, monkeypatch): +def test_timeout_stops_automation_and_allows_retry_when_no_native_owner_remains(tmp_path, monkeypatch): """A timed-out item must not leave a Mentor process holding the library.""" fixture(tmp_path) killed = [] @@ -508,9 +508,9 @@ def test_timeout_stops_automation_and_blocks_next_item(tmp_path, monkeypatch): def __init__(self, command, name): self.name = name self.desktop = None - def poll(self): return None + def poll(self): return self.returncode returncode = None - def kill(self): killed.append(True) + def kill(self): killed.append(True); self.returncode = 1 def wait(self, timeout=5): return 0 def close(self): pass @@ -521,7 +521,7 @@ def test_timeout_stops_automation_and_blocks_next_item(tmp_path, monkeypatch): assert result.status == 'TIMEOUT' assert killed == [True] - assert not result.safe_to_continue + assert result.safe_to_continue def test_fatal_dialog_terminates_current_worker_and_cannot_become_pass(tmp_path, monkeypatch): @@ -532,8 +532,8 @@ def test_fatal_dialog_terminates_current_worker_and_cannot_become_pass(tmp_path, desktop = object() returncode = None def __init__(self, command, name): pass - def poll(self): return None - def kill(self): killed.append(True) + def poll(self): return self.returncode + def kill(self): killed.append(True); self.returncode = 1 def wait(self, timeout=5): return 1 def close(self): pass @@ -598,8 +598,8 @@ def test_unknown_dialog_writes_snapshot_before_current_item_fails(tmp_path, monk companion_pids = [701] returncode = None def __init__(self, command, name): self.name = name - def poll(self): return None - def kill(self): killed.append(True) + def poll(self): return self.returncode + def kill(self): killed.append(True); self.returncode = 1 def wait(self, timeout=5): return 1 def close(self): pass @@ -747,7 +747,7 @@ def test_reserved_design_server_ports_block_the_item_fast(tmp_path): assert result.evidence_path.is_file() -def test_reserved_design_server_ports_block_by_default_before_worker(tmp_path, monkeypatch): +def test_reserved_design_server_ports_block_only_in_explicit_strict_mode(tmp_path, monkeypatch): fixture(tmp_path) class FakeWorker: def __init__(self, command, name): @@ -757,7 +757,7 @@ def test_reserved_design_server_ports_block_by_default_before_worker(tmp_path, m def close(self): pass import cbb_refresh_verify.desktop_worker as desktop_worker monkeypatch.setattr(desktop_worker, 'DesktopWorker', FakeWorker) - result = run_verify(request(tmp_path), port_check=lambda: (False, '端口区间被系统保留'), + result = run_verify(request(tmp_path, strict_port_check=True), port_check=lambda: (False, '端口区间被系统保留'), progress=lambda event: None) assert result.status == 'BLOCKED' assert '保留' in result.stderr -- 2.39.5 From 4ebe098ef0d952eb6a5817f18766777564eedb03 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: Wed, 4 Nov 2026 22:12:24 +0800 Subject: [PATCH 54/59] feat: open native project history and backup interface --- .../2026-11-04-native-backup-entry.md | 31 +++ src/cbb_refresh_verify/application.py | 4 + src/cbb_refresh_verify/native_backup.py | 155 +++++++++++ src/cbb_refresh_verify/network_path.py | 8 + src/cbb_refresh_verify/ui/main_window.py | 116 +++++++- tests/test_native_backup.py | 252 ++++++++++++++++++ 6 files changed, 560 insertions(+), 6 deletions(-) create mode 100644 docs/diagnostics/2026-11-04-native-backup-entry.md create mode 100644 src/cbb_refresh_verify/native_backup.py create mode 100644 tests/test_native_backup.py diff --git a/docs/diagnostics/2026-11-04-native-backup-entry.md b/docs/diagnostics/2026-11-04-native-backup-entry.md new file mode 100644 index 0000000..d439e7c --- /dev/null +++ b/docs/diagnostics/2026-11-04-native-backup-entry.md @@ -0,0 +1,31 @@ +# 原生版本历史、备份与回退入口 + +## 用户操作 + +在“ICDB 维护”页勾选一个非 READY CBB,点击“版本历史与备份…”。应用将所选工程的唯一 PRJ 传给已安装的 `iCDBProjectBackup.exe -p`,打开可见的原生窗口。当前 Layout 数据缺失但 PRJ 仍在时也可以进入。 + +版本列表、备份详情、操作日志和 Restore backup 由原生窗口提供。选择版本、手动保存/自动保存分支以及恢复确认均由用户操作;应用不代点 Restore、Repair、删除或清理菜单。用户已报告手工回退能够走通,本轮接入该原生入口,不把手工成功当作自动 Repair 验收通过。 + +原生窗口正常关闭后重新读取受影响 CBB 的权威状态。窗口关闭仅记为 `CLOSED`,始终记录 `restore_confirmed=false`,不自动执行校验,不伪造 PASS/READY。需要验证恢复后的工程时,单独使用“检验”页。 + +## 会话与独立操作 + +- 可见原生窗口由独立辅助进程启动和监督;Qt 线程不等待原生会话,清单搜索、筛选、横向页面和库切换保持可用。 +- 使用 Windows Job 跟踪本次原生进程及后代,根进程退出但后代仍在时继续等待。Job 不设置 kill-on-close;关闭本应用不强制关闭原生窗口。 +- 实际会话持有现有中心库协调锁,同库写入等待该会话关闭;其他库不因这个窗口禁用。没有新增历史 ICDB 失败标记作为普通校验前置条件。 +- 状态或结果文件写入失败不结束原生监督。辅助进程退出后,只有返回身份一致且 `safe_to_continue=true` 才清除应用内会话和路径租约;缺失、损坏或释放未确认时保留同库冲突,可通过已有“检查占用…”取得新的释放证明。 +- 切换到同一共享上的其他库时,旧会话结束会把临时盘符所有权交给当前租约,避免断开当前路径。关闭应用时为仍打开的原生窗口保留所需盘符。 + +## 本机验证 + +证据目录:`diagnostics/native-backup-ui-20261104/`(本地诊断目录,不包含原库发布副本)。 + +源码实际按钮验证使用自有隔离库 `C:/Temp/CbbTabsRelease20261104/library/mentor_lib.lmc` 的 `1GPHY_8212`,由测试夹具提供 PENDING 清单状态,确认原生 Output 加载同一 PRJ、报告工程未使用、完成备份目录加载,再向该原生空闲窗口发送正常关闭。未点击 Restore/Repair,也未操作原库。详见 `source-acceptance.json`。 + +冻结候选包读取真实权威清单时,`1GPHY_8212` 为 READY,其维护入口按规则禁用。随后选择同一隔离库中实际 PENDING 的 `IP101GR`,验证真实 Qt 按钮、原生备份列表加载、正常关闭和应用状态刷新;最终仍为“待校验”,应用正常退出。原生 Output 明确报告该隔离副本的数据库不一致,本轮只验证入口,不点击 Repair/Restore,也不将此报错当作自动修复已通过。记录在 `frozen-acceptance.json`。 + +相关功能回归 88 项通过;最终全量回归 **1260 passed**,188.94 秒,退出码 0,1 条既有 pywinauto STA 警告。冻结的 13 个关键 Python 模块及两个 VBS 与源码一致,冻结启动/退出冒烟、Unicode/UNC 协议和包内 x86 运行库加载通过。全量回归、冻结模块一致性和启动冒烟结果分别保存在 `pytest-final.log`、`frozen-result.json`;最终构建记录随包保存到 `BUILD.json`。一次按系统 GBK 模式运行的全量测试因既有测试未指定 UTF-8 读取原生日志而失败,保留 `pytest-locale-failure.log`,最终回归使用 `python -X utf8 -m pytest -q`。 + +## 验收边界 + +本轮覆盖“应用入口 → 原生备份列表加载 → 用户可操作窗口 → 正常关闭 → 状态刷新”。不自动回退数据,不以本轮打开/关闭试验代替恢复后的保存关闭与 READY 验收。现场失联故障的完整自动修复、Win10 干净系统和跨机共享库并发仍未验收;ICDB 自动修复研究按用户要求暂停。工单和 WIP PR 保持开放。 diff --git a/src/cbb_refresh_verify/application.py b/src/cbb_refresh_verify/application.py index c5f8aff..8d2fb88 100644 --- a/src/cbb_refresh_verify/application.py +++ b/src/cbb_refresh_verify/application.py @@ -51,6 +51,7 @@ def main(argv: Sequence[str] | None = None) -> int: parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--repair-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--owner-probe', type=Path, help=argparse.SUPPRESS) + parser.add_argument('--native-backup-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--management-worker', type=Path, help=argparse.SUPPRESS) parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS) parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS) @@ -64,6 +65,9 @@ def main(argv: Sequence[str] | None = None) -> int: if args.owner_probe: from .owner_probe import owner_probe_main return owner_probe_main(args.owner_probe) + if args.native_backup_worker: + from .native_backup import native_backup_worker_main + return native_backup_worker_main(args.native_backup_worker) if args.management_worker: from .native_management import management_worker_main return management_worker_main(args.management_worker) diff --git a/src/cbb_refresh_verify/native_backup.py b/src/cbb_refresh_verify/native_backup.py new file mode 100644 index 0000000..bdd0e3e --- /dev/null +++ b/src/cbb_refresh_verify/native_backup.py @@ -0,0 +1,155 @@ +"""Open the official backup UI; version selection and writes stay native.""" +from dataclasses import dataclass +import json +from pathlib import Path +import stat +import subprocess +import sys +import time +from uuid import uuid4 + + +def native_backup_executable(mentor_bin=''): + from .mentor_paths import discover_sdd_home + home = discover_sdd_home(mentor_bin) + executable = home/'iCDB/win32/bin/iCDBProjectBackup.exe' if home else None + if executable is None or not executable.is_file(): + raise ValueError('未找到 iCDB Project Backup,请检查 Mentor 安装位置。') + return executable + + +def native_backup_project(library, block): + from .verify_runner import INVALID_NAME_CHARS + library = Path(library).absolute() + if not block or block in ('.', '..') or any(c in block for c in INVALID_NAME_CHARS): + raise ValueError('CBB 名称非法。') + if not library.is_file(): + raise ValueError('中心库文件不存在。') + work = library.parent/'ReusableBlocksLP'/block/'Work' + expected = library.parent.resolve()/'ReusableBlocksLP'/block/'Work' + if work.resolve() != expected: + raise ValueError('工程路径通过链接指向其他位置。') + projects = list(work.glob('*.prj')) + if len(projects) != 1 or not projects[0].is_file(): + raise ValueError('无法唯一确定所选 CBB 的工程 PRJ。') + project = projects[0] + if (project.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT + or not project.resolve().is_relative_to(expected)): + raise ValueError('工程文件通过链接指向其他位置。') + return project + + +@dataclass +class NativeBackupSession: + process: object + job: Path + library: Path + block: str + path_lease: object = None + + +def launch_native_backup(request): + project = native_backup_project(request.lmc_file, request.block_name) + native_backup_executable(request.mentor_bin) + root = Path(request.evidence_dir).resolve()/('native-backup-'+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): + raise ValueError('运行记录目录需位于中心库之外。') + root.mkdir(parents=True) + job = root/'request.json' + job.write_text(json.dumps({'library': str(request.lmc_file), 'block': request.block_name, + 'project': str(project), 'mentor_bin': request.mentor_bin}, ensure_ascii=False), 'utf-8') + command = ([sys.executable] if getattr(sys, 'frozen', False) else + [sys.executable, '-m', 'cbb_refresh_verify'])+['--native-backup-worker', str(job)] + process = subprocess.Popen(command, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0)) + return NativeBackupSession(process, job, Path(request.lmc_file), request.block_name) + + +class _NativeBackupProcess: + """Track this visible tool and its descendants without killing on close.""" + def __init__(self, command, cwd): + import win32con, win32job, win32process + self.job = win32job.CreateJobObject(None, '') + startup = win32process.STARTUPINFO() + try: + self.handle, thread, self.pid, _ = win32process.CreateProcess( + command[0], subprocess.list2cmdline(command), None, None, False, + win32con.CREATE_SUSPENDED, None, str(cwd), startup) + except Exception: + self.job.Close() + raise + try: + win32job.AssignProcessToJobObject(self.job, self.handle) + win32process.ResumeThread(thread) + except Exception: + # No native code has run while suspended; there can be no DB write. + win32process.TerminateProcess(self.handle, 1) + self.handle.Close(); self.job.Close() + raise + finally: + thread.Close() + + def poll(self): + import win32process + code = win32process.GetExitCodeProcess(self.handle) + return None if code == 259 else code + + def session_pids(self): + import win32job + return win32job.QueryInformationJobObject(self.job, win32job.JobObjectBasicProcessIdList) + + def close(self): + self.handle.Close(); self.job.Close() + + +def native_backup_worker_main(job, *, process_factory=None): + from .library_lock import LibraryLock + from .management import _write_json + job = Path(job) + result = {'status': 'FAILED', 'safe_to_continue': True, 'restore_confirmed': False} + native = None + def write_evidence(name, value): + try: + _write_json(job.with_name(name), value) + except Exception as error: + # Evidence storage must never end supervision of a live transaction. + result['evidence_error'] = str(error) + try: + record = json.loads(job.read_text('utf-8')) + library, block = Path(record['library']), record['block'] + result.update(library=str(library), block=block) + project = native_backup_project(library, block) + if project != Path(record['project']): + raise ValueError('工程身份已变化,未打开原生工具。') + executable = native_backup_executable(record.get('mentor_bin', '')) + released = True + with LibraryLock(library, checkpoint=False, safe_to_release=lambda: released): + native = (process_factory or _NativeBackupProcess)([str(executable), '-p', str(project)], project.parent) + released = False + result.update(status='OPEN', native_pid=native.pid, project=str(project), safe_to_continue=False) + write_evidence('state.json', result) + waiting = False + while True: + try: + pids, exit_code = native.session_pids(), native.poll() + except Exception as error: + write_evidence('state.json', {**result, + 'status': 'RELEASE_UNCONFIRMED', 'detail': str(error)}) + time.sleep(.5) + continue # Keep the lease while the live session cannot be observed. + if not pids and exit_code is not None: + break + if exit_code is not None and not waiting: + waiting = True + write_evidence('state.json', {**result, 'status': 'WAITING_RELEASE'}) + time.sleep(.25) + released = True + result.update(status='CLOSED', native_exit_code=exit_code, safe_to_continue=True) + return 0 if result['native_exit_code'] == 0 else 1 + except Exception as error: + result.update(status='FAILED', detail=str(error), safe_to_continue=native is None) + return 1 + finally: + write_evidence('result.json', result) + if native is not None: + native.close() # No kill-on-close: never interrupt a native transaction. diff --git a/src/cbb_refresh_verify/network_path.py b/src/cbb_refresh_verify/network_path.py index 7272a17..579b8dc 100644 --- a/src/cbb_refresh_verify/network_path.py +++ b/src/cbb_refresh_verify/network_path.py @@ -229,6 +229,14 @@ class LibraryPathLease: """Keep the Mentor drive available to a retained native session.""" self._retained_for_recovery = True + def transfer_ownership(self, other) -> None: + """Keep a shared mapping alive for the currently loaded library.""" + if (isinstance(other, LibraryPathLease) and not other._closed + and self.created and self.drive.upper() == other.drive.upper() + and self.remote_root and _same_remote(self.remote_root, other.remote_root)): + other.created, other._backend = True, self._backend + self.created = False + def close(self) -> None: if self._closed: return diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 2ef609c..acc5017 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -5,7 +5,7 @@ import time from collections import Counter from dataclasses import replace -from PySide6.QtCore import QObject, QThreadPool, Signal, Qt +from PySide6.QtCore import QObject, QThreadPool, Signal, Qt, QTimer from PySide6.QtGui import QColor, QIcon from PySide6.QtWidgets import (QWidget, QLineEdit, QPushButton, QFormLayout, QTextEdit, QFileDialog, QHBoxLayout, QLabel, QSpinBox, QMessageBox, QToolButton, @@ -49,7 +49,7 @@ def application_icon_path(): class MainWindow(QWidget): def __init__(self, reader=None, verifier=None, logging_context=None, data_root=None, authoritative_status=True, path_lease_factory=None, status_refresher=None, - management_adapter=None): + management_adapter=None, native_backup_launcher=None): super().__init__() self.setWindowTitle('CBB 刷新校验') icon_path = application_icon_path() @@ -72,6 +72,11 @@ class MainWindow(QWidget): self._batch_requests = () self._batch_records = {} self._reimport_summary_action = None + self.native_backup_launcher = native_backup_launcher + self._native_backup_session = None + self.native_backup_timer = QTimer(self) + self.native_backup_timer.setInterval(400) + self.native_backup_timer.timeout.connect(self._poll_native_backup) if reader is not None: self.reader = reader elif authoritative_status and os.environ.get('CBB_STATUS_BRIDGE_MODE', 'authoritative').casefold() != 'legacy': @@ -215,6 +220,10 @@ class MainWindow(QWidget): maintenance = QWidget() maintenance_layout = QVBoxLayout(maintenance) maintenance_layout.setContentsMargins(0, 0, 0, 0) + self.native_backup_button = QPushButton('版本历史与备份…') + self.native_backup_button.setToolTip('勾选一个非 READY CBB,打开原生备份列表并选择回退版本。') + self.native_backup_button.clicked.connect(self.open_native_backup) + maintenance_layout.addWidget(self.native_backup_button) maintenance_row = QHBoxLayout() maintenance_row.addWidget(self.icdb_repair_button) maintenance_row.addWidget(self.backup_restore_button) @@ -278,6 +287,8 @@ class MainWindow(QWidget): def _release_library_lease(self): lease, self.library_lease = self.library_lease, None + if self._native_backup_session is not None and self._native_backup_session.path_lease is lease: + return # The visible native session still needs this mapping. if lease is None: return try: @@ -290,6 +301,10 @@ class MainWindow(QWidget): # The refresh flow needs only the library and a CBB; LibSpec/DMS config # are optional and only enable the extra contents check. ready = not self.busy and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 + selected = self.selected_records() + self.native_backup_button.setEnabled(ready and self._native_backup_session is None + and len(selected) == 1 and selected[0].status != 'READY') + ready = ready and not self._native_backup_same_library() self.batch_button.setEnabled(ready and bool(self._eligible_names())) self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True))) self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names())) @@ -347,6 +362,9 @@ class MainWindow(QWidget): library = select_library_file(library) def completed(result): self._management_unsafe = False + session = self._native_backup_session + if session is not None and session.library == library and session.process.poll() is not None: + self._release_native_backup_session(session) message = f'原生会话释放检查通过。请审查未完成任务后重新读取中心库。\n{result["evidence_path"]}' self.status_label.setText(message); self.output.append(message) self._start(lambda: release_retained_library(library, self.data_root/'release-checks'), completed) @@ -354,6 +372,8 @@ class MainWindow(QWidget): def _management_block_reason(self, operation): if self.busy: return '当前任务尚未结束,请等待后再执行库管理操作。' + if self._native_backup_same_library(): + return '该库的版本与备份窗口仍在使用,关闭后可执行写入操作。' if self._management_unsafe: return '上次管理操作的会话尚未安全释放,请先查看异常证据并执行释放检查。' capability = self.management_capabilities[operation] @@ -655,7 +675,7 @@ class MainWindow(QWidget): def _confirm_verify(self, names): from .backup_notice import MANUAL_BACKUP_NOTICE - if self.busy or not self.loaded_path or not names: + if self.busy or not self.loaded_path or self._native_backup_same_library() or not names: return if QMessageBox.question(self, '确认批量校验', f'{MANUAL_BACKUP_NOTICE}\n\n目标库:{self.loaded_path}\n' @@ -677,8 +697,84 @@ class MainWindow(QWidget): if answer == QMessageBox.Yes: self._run(names, maintenance=True) + def _native_backup_same_library(self): + session = self._native_backup_session + return session is not None and Path(self.mentor_lmc_path or self.lmc_edit.text()) == session.library + + def open_native_backup(self): + if self.busy or not self.loaded_path or self._native_backup_session is not None: + return + selected = self.selected_records() + if len(selected) != 1 or selected[0].status == 'READY': + self.status_label.setText('请勾选一个非 READY CBB。') + return + from cbb_refresh_verify.native_backup import launch_native_backup + request = self._request(selected[0].name) + launcher = self.native_backup_launcher or launch_native_backup + self._global_icdb_run = False + self._reimport_offers = () + self.status_label.setText(f'正在打开 {request.block_name} 的版本与备份…') + def opened(session): + session.path_lease = self.library_lease + self._native_backup_session = session + self.native_backup_timer.start() + self.status_label.setText(f'正在打开 {session.block} 的原生备份窗口,请在其中选择版本。') + self.output.append(f'{session.block} · 原生版本与备份\n{session.job}') + self.busy = True; self._update() + self.worker = ServiceWorker(lambda: launcher(request)) + self.worker.signals.succeeded.connect(opened) + self.worker.signals.failed.connect(lambda error: self.status_label.setText(f'无法打开版本与备份:{error}')) + self.worker.signals.finished.connect(self._finished) + self.pool.start(self.worker) + + def _release_native_backup_session(self, session): + self._native_backup_session = None + if session.path_lease is not None and session.path_lease is not self.library_lease: + transfer = getattr(session.path_lease, 'transfer_ownership', None) + if transfer is not None: + transfer(self.library_lease) + session.path_lease.close() + + def _poll_native_backup(self): + session = self._native_backup_session + if session is None or self.busy or session.process.poll() is None: + return + self.native_backup_timer.stop() + same_library = Path(self.mentor_lmc_path or self.lmc_edit.text()) == session.library + def read_result(): + import json + result = json.loads(session.job.with_name('result.json').read_text('utf-8')) + if Path(result['library']) != session.library or result['block'] != session.block: + raise ValueError('原生会话返回的工程身份不一致。') + refresh = (self._read_affected_records((session.block,)) + if result.get('safe_to_continue') is True and same_library else None) + return result, refresh + def completed(payload): + result, refresh = payload + if result.get('safe_to_continue') is not True: + unconfirmed(result.get('detail', '未取得会话释放结果。')) + return + self._release_native_backup_session(session) + if refresh is not None: + self._apply_refreshed_records(refresh) + message = ('原生备份窗口已关闭。需要时可单独运行校验。' if result.get('status') == 'CLOSED' + else '原生备份操作未结束:' + result.get('detail', '请检查原生窗口。')) + self.status_label.setText(message) + self.output.append(f'{session.block} · {message}\n{session.job.with_name("result.json")}') + if same_library and session.block in self._row_by_name: + self.cbb_table.item(self._row_by_name[session.block], 3).setText(message) + def unconfirmed(error): + self.status_label.setText('原生窗口退出尚未确认,请关闭窗口后检查释放。') + self.output.append(f'{session.block} · 退出尚未确认:{error}\n{session.job}') + self.busy = True; self._update() + self.worker = ServiceWorker(read_result) + self.worker.signals.succeeded.connect(completed) + self.worker.signals.failed.connect(unconfirmed) + self.worker.signals.finished.connect(self._finished) + self.pool.start(self.worker) + def restore_backup(self): - if self.busy or not self.loaded_path: + if self.busy or not self.loaded_path or self._native_backup_same_library(): return from .backup_recovery_dialog import BackupRecoveryDialog names = self._eligible_names(checked_only=bool(self.selected_records())) @@ -697,7 +793,7 @@ class MainWindow(QWidget): def _backup_request(self, name): # A still-open dialog must recheck a target after an earlier recovery # or an authoritative refresh changed its eligibility. - if self.busy or name not in self._eligible_names(checked_only=bool(self.selected_records())): + if self.busy or self._native_backup_same_library() or name not in self._eligible_names(checked_only=bool(self.selected_records())): raise ValueError('目标需为当前可见、可用的非 READY CBB;请重新选择。') return self._request(name) @@ -737,6 +833,9 @@ class MainWindow(QWidget): def _run(self, names, *, maintenance=False): if self.busy or not self.loaded_path: return + if self._native_backup_same_library(): + self.status_label.setText('请先关闭该库的版本与备份窗口。') + return eligible = set(self._maintenance_names() if maintenance else self._eligible_names()) names = [name for name in names if name in eligible] if not names: @@ -853,7 +952,7 @@ class MainWindow(QWidget): (UNKNOWN on read failure). It runs before the worker's finished signal. Search, filter, original logical_type and action-result cells are retained. """ - if self.busy or not self.loaded_path: + if self.busy or not self.loaded_path or self._native_backup_same_library(): return False targets = tuple(dict.fromkeys(name for name in names if name in self._records_by_name)) if not targets: @@ -1034,5 +1133,10 @@ class MainWindow(QWidget): if self.busy: self.request_stop(); event.ignore() else: + if self._native_backup_session is not None: + self.native_backup_timer.stop() + lease = self._native_backup_session.path_lease + if lease is not None: + lease.retain_for_recovery() self._release_library_lease() event.accept() diff --git a/tests/test_native_backup.py b/tests/test_native_backup.py new file mode 100644 index 0000000..56cf52d --- /dev/null +++ b/tests/test_native_backup.py @@ -0,0 +1,252 @@ +"""Native backup entry: user owns version choice, restoration and confirmation.""" +import json +from pathlib import Path +from types import SimpleNamespace + +import pytest +import win32con +import win32file + + +def project_fixture(tmp_path): + from cbb_refresh_verify.verify_runner import VerifyRequest + library = tmp_path/'library/library.lmc'; library.parent.mkdir(); library.touch() + work = library.parent/'ReusableBlocksLP/Block/Work'; work.mkdir(parents=True) + project = work/'Block.prj'; project.write_text('KEY iCDBDir "database"\n') + return VerifyRequest(library, 'Block', tmp_path/'evidence'), project + + +def test_backup_entry_can_open_project_without_layout_data(tmp_path, monkeypatch): + from cbb_refresh_verify import native_backup as native + request, project = project_fixture(tmp_path) + executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch() + monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable) + calls = [] + monkeypatch.setattr(native.subprocess, 'Popen', lambda command, **kw: + (calls.append(command) or SimpleNamespace(pid=123, poll=lambda: None))) + session = native.launch_native_backup(request) + record = json.loads(session.job.read_text('utf-8')) + assert record['project'] == str(project) + assert calls[0][-2] == '--native-backup-worker' + assert calls[0][-1] == str(session.job) + assert session.library == request.lmc_file and session.block == 'Block' + + +def test_native_session_closure_does_not_claim_restore_success(tmp_path, monkeypatch): + from cbb_refresh_verify import native_backup as native + from cbb_refresh_verify.library_lock import LibraryLock + request, project = project_fixture(tmp_path) + executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch() + monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable) + job = request.evidence_dir/'request.json'; job.parent.mkdir() + job.write_text(json.dumps({'library': str(request.lmc_file), 'block': 'Block', + 'project': str(project), 'mentor_bin': ''})) + calls = [] + class NativeProcess: + pid = 456 + def __init__(self, command, cwd): + calls.append(command) + lock = LibraryLock(request.lmc_file) + with pytest.raises(Exception): + win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE, + 0, None, win32con.OPEN_ALWAYS, 0, None) + def poll(self): return 0 + def session_pids(self): return [] + def close(self): pass + assert native.native_backup_worker_main(job, process_factory=NativeProcess) == 0 + assert calls == [[str(executable), '-p', str(project)]] + result = json.loads(job.with_name('result.json').read_text('utf-8')) + assert result['status'] == 'CLOSED' and result['restore_confirmed'] is False + assert result['native_exit_code'] == 0 and result['safe_to_continue'] is True + with LibraryLock(request.lmc_file): pass + + +def test_changed_project_identity_is_rejected_before_native_launch(tmp_path, monkeypatch): + from cbb_refresh_verify import native_backup as native + request, project = project_fixture(tmp_path) + job = request.evidence_dir/'request.json'; job.parent.mkdir() + job.write_text(json.dumps({'library': str(request.lmc_file), 'block': 'Block', + 'project': str(project.parent/'Other.prj'), 'mentor_bin': ''})) + assert native.native_backup_worker_main(job, + process_factory=lambda *a, **k: pytest.fail('native launch on mismatched project')) == 1 + assert json.loads(job.with_name('result.json').read_text('utf-8'))['status'] == 'FAILED' + + +@pytest.mark.parametrize('all_evidence_failed', [False, True]) +def test_state_log_failure_does_not_abandon_live_native_session(tmp_path, monkeypatch, all_evidence_failed): + from cbb_refresh_verify import native_backup as native, management + request, project = project_fixture(tmp_path) + executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch() + monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable) + job = request.evidence_dir/'request.json'; job.parent.mkdir() + job.write_text(json.dumps({'library': str(request.lmc_file), 'block': 'Block', + 'project': str(project), 'mentor_bin': ''})) + write = management._write_json + def broken_state(path, value): + if all_evidence_failed or Path(path).name == 'state.json': raise OSError('state log unavailable') + write(path, value) + monkeypatch.setattr(management, '_write_json', broken_state) + observations = [] + class NativeProcess: + pid = 456 + def __init__(self, *a): pass + def poll(self): return 0 + def session_pids(self): + observations.append(True) + return [456] if len(observations) == 1 else [] + def close(self): pass + assert native.native_backup_worker_main(job, process_factory=NativeProcess) == 0 + assert len(observations) >= 2 + if not all_evidence_failed: + assert json.loads(job.with_name('result.json').read_text('utf-8'))['status'] == 'CLOSED' + + +def test_native_backup_ui_keeps_catalog_interactive_and_refreshes_after_close(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + requests, refreshes = [], [] + result = tmp_path/'native/result.json'; result.parent.mkdir() + job = result.with_name('request.json'); job.write_text('{}') + session = SimpleNamespace(library=Path('test.lmc'), block='Beta', job=job, + process=SimpleNamespace(poll=lambda: None)) + window = load_window(qtbot, tmp_path, monkeypatch, + native_backup_launcher=lambda req: (requests.append(req) or session), + verifier=lambda _: pytest.fail('native backup must not run Verify'), + status_refresher=lambda names, _: (refreshes.append(names) or {n: 'PENDING' for n in names})) + window.search_edit.setText('Beta'); window.select_visible_button.click() + assert window.native_backup_button.isEnabled() + window.native_backup_button.click(); qtbot.waitUntil(lambda: not window.busy) + assert len(requests) == 1 and requests[0].block_name == 'Beta' + assert window.search_edit.isEnabled() and window.section_tabs.isEnabled() + window.section_tabs.setCurrentIndex(2) + window.search_edit.setText('Gamma') + assert not window.busy + result.write_text(json.dumps({'status': 'CLOSED', 'safe_to_continue': True, + 'library': str(session.library), 'block': session.block, 'native_exit_code': 0, + 'restore_confirmed': False})) + session.process.poll = lambda: 0 + window._poll_native_backup() + qtbot.waitUntil(lambda: not window.busy) + assert refreshes == [('Beta',)] + assert window.search_edit.text() == 'Gamma' + assert 'PASS' not in window.cbb_table.item(window._row_by_name['Beta'], 3).text() + + +def test_native_backup_selection_requires_one_visible_non_ready_item(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch, + native_backup_launcher=lambda _: pytest.fail('unexpected launch')) + assert not window.native_backup_button.isEnabled() + window.search_edit.setText('Alpha'); window.select_visible_button.click() + assert not window.native_backup_button.isEnabled() + window.search_edit.clear(); window.select_visible_button.click() + assert not window.native_backup_button.isEnabled() + + +def test_closing_app_keeps_manual_native_window_and_mapping(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch) + retained = [] + lease = SimpleNamespace(retain_for_recovery=lambda: retained.append(True), + close=lambda: pytest.fail('mapping released before native session ends')) + session = SimpleNamespace(library=Path('test.lmc'), block='Beta', path_lease=lease, + process=SimpleNamespace(poll=lambda: None, terminate=lambda: pytest.fail('native UI terminated'))) + window.library_lease = lease + window._native_backup_session = session + window.show(); window.close() + assert retained == [True] and session.process.poll() is None + + +def test_native_launch_failure_does_not_poison_other_features(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + def missing(_): raise ValueError('未找到 iCDB Project Backup') + window = load_window(qtbot, tmp_path, monkeypatch, native_backup_launcher=missing) + window.search_edit.setText('Beta'); window.select_visible_button.click() + window.native_backup_button.click(); qtbot.waitUntil(lambda: not window.busy) + assert '未找到' in window.status_label.text() + assert window.selected_batch_button.isEnabled() and window.native_backup_button.isEnabled() + assert not window._management_unsafe + + +@pytest.mark.parametrize('evidence', ['unconfirmed', 'missing', 'malformed', 'wrong_identity']) +def test_unconfirmed_supervisor_exit_keeps_session_conflict_and_mapping(qtbot, tmp_path, monkeypatch, evidence): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch) + job = tmp_path/'native/request.json'; job.parent.mkdir(); job.write_text('{}') + if evidence != 'missing': + job.with_name('result.json').write_text('invalid' if evidence == 'malformed' else json.dumps({ + 'status': 'FAILED', 'safe_to_continue': evidence == 'wrong_identity', + 'library': 'other.lmc' if evidence == 'wrong_identity' else 'test.lmc', + 'block': 'Beta', 'detail': 'unconfirmed'})) + session = SimpleNamespace(library=Path('test.lmc'), block='Beta', job=job, + path_lease=window.library_lease, process=SimpleNamespace(poll=lambda: 1)) + window._native_backup_session = session + window._poll_native_backup(); qtbot.waitUntil(lambda: not window.busy) + assert window._native_backup_session is session + assert not window.batch_button.isEnabled() and window.search_edit.isEnabled() + assert not window.native_backup_timer.isActive() + + +def test_confirmed_native_close_transfers_mapping_to_current_library(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + from test_network_path import FakeWNet + from cbb_refresh_verify.network_path import LibraryPathLease + window = load_window(qtbot, tmp_path, monkeypatch) + backend = FakeWNet() + old = LibraryPathLease.acquire(r'\\server\share\A\library.lmc', backend=backend) + current = LibraryPathLease.acquire(r'\\server\share\B\library.lmc', backend=backend) + window.library_lease = current + job = tmp_path/'request.json' + job.with_name('result.json').write_text(json.dumps({'status': 'CLOSED', 'safe_to_continue': True, + 'library': str(old.mentor_path), 'block': 'Beta'})) + window._native_backup_session = SimpleNamespace(library=old.mentor_path, block='Beta', job=job, + path_lease=old, process=SimpleNamespace(poll=lambda: 0)) + window._poll_native_backup(); qtbot.waitUntil(lambda: not window.busy) + assert window._native_backup_session is None + assert backend.cancelled == [] and current.created + window.close() + assert backend.cancelled == [current.drive] + + +def test_native_mapping_ownership_transfers_to_new_library_on_same_share(): + from test_network_path import FakeWNet + from cbb_refresh_verify.network_path import LibraryPathLease + backend = FakeWNet() + old = LibraryPathLease.acquire(r'\\server\share\A\library.lmc', backend=backend) + current = LibraryPathLease.acquire(r'\\server\share\B\library.lmc', backend=backend) + assert old.created and not current.created + old.transfer_ownership(current) + old.close() + assert backend.cancelled == [] and current.created + current.close() + assert backend.cancelled == [current.drive] + + +def test_native_process_job_waits_for_its_children_without_killing_them(tmp_path): + import sys, time + from cbb_refresh_verify.native_backup import _NativeBackupProcess + pythonw = Path(sys.executable).with_name('pythonw.exe') + child = tmp_path/'child.pid' + script = tmp_path/'parent.py' + script.write_text('import subprocess,sys,time\nfrom pathlib import Path\n' + 'p=subprocess.Popen([sys.executable,"-c","import time;time.sleep(1)"])\n' + f'Path({str(child)!r}).write_text(str(p.pid))\n' + 'time.sleep(.2)\n', encoding='utf-8') + native = _NativeBackupProcess([str(pythonw), str(script)], tmp_path) + try: + deadline = time.monotonic()+10 + seen_child = False + seen_parent_exit_before_child = False + while time.monotonic() < deadline: + pids = native.session_pids() + assert isinstance(pids, (list, tuple)) + if child.exists(): + pid = int(child.read_text()) + seen_child = seen_child or pid in pids + seen_parent_exit_before_child = seen_parent_exit_before_child or native.poll() == 0 and pid in pids + if native.poll() == 0 and not pids: + break + time.sleep(.05) + assert seen_child and seen_parent_exit_before_child + assert not native.session_pids() + finally: + native.close() -- 2.39.5 From beb2b06fdfabaf347f02d74cea6ae46128547cc1 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: Wed, 4 Nov 2026 22:53:16 +0800 Subject: [PATCH 55/59] fix: diagnose PCB startup before ICDB maintenance --- ...26-11-04-startup-gated-icdb-maintenance.md | 32 +++ src/cbb_refresh_verify/maintenance_entry.py | 69 +++++-- src/cbb_refresh_verify/mentor_adapter.py | 4 + src/cbb_refresh_verify/ui/main_window.py | 5 +- src/cbb_refresh_verify/verify_runner.py | 12 +- tests/test_maintenance_diagnosis.py | 18 +- tests/test_maintenance_entry.py | 17 +- tests/test_maintenance_review.py | 26 +++ tests/test_maintenance_startup_gate.py | 185 ++++++++++++++++++ 9 files changed, 338 insertions(+), 30 deletions(-) create mode 100644 docs/diagnostics/2026-11-04-startup-gated-icdb-maintenance.md create mode 100644 tests/test_maintenance_startup_gate.py diff --git a/docs/diagnostics/2026-11-04-startup-gated-icdb-maintenance.md b/docs/diagnostics/2026-11-04-startup-gated-icdb-maintenance.md new file mode 100644 index 0000000..0aec971 --- /dev/null +++ b/docs/diagnostics/2026-11-04-startup-gated-icdb-maintenance.md @@ -0,0 +1,32 @@ +# 按本次工程启动错误决定 ICDB 维护 + +## 问题 + +旧的显式维护入口对 `maintenance=True` 恒定返回“需要维护”,所有可见非 READY 候选都先进入 Project Backup 检查。虽然 Repair 仍要求原生检查明确报告故障,这一流程没有先确认 PCB 是否实际打不开,也会让健康工程进入维护环境和占用检查。 + +非 READY 表示未通过 CBB 校验,不能作为 ICDB 损坏证据;普通服务器、授权、COM、路径及内容错误也不能据此转换成 ICDB 修复。 + +## 调整后的流程 + +1. 维护按钮保持针对可见非 READY 候选。每项先通过既有 `diagnostic_only` 路径实际打开所选 PCB:核对工程/中心库身份,不改工程路径、不执行集成、不保存、不提交 Cert、不运行普通 CBB 校验。 +2. 同一工程正常打开并确认会话释放后返回 CHECKED,备注为“无需 ICDB 修复”,不启动 Project Backup,不推定 READY。 +3. 普通失败保留原状态和原始错误。没有完整 ICDB 原生失联特征、数据库路径或会话信息时,不进入维护。历史地址、协调检查点或旧诊断文件不代替本轮启动证据。 +4. 仅本轮诊断 FAIL、会话已释放,且失联报错数据库属于当前工程时进入原生维护。共享库、其他工程、未知目标、超时及取消结果不自动修复当前 CBB。 +5. 原生维护取得锁及工程证明后,再核对目标必须与本次报错数据库相同。原生检查仍需确认空闲且明确要求修复,才调用既有单次 Repair 路径。目标不一致时不启动原生工具,不因已释放的单项错误阻塞其他独立 CBB。 +6. 诊断在文档身份确认后和关闭完成后各复查本轮日志,避免延迟写入的启动错误被误判为健康。日志沿用启动前基线和增量判定,不从历史日志猜测故障。 + +普通“检验”按钮继续独立执行,不要求先通过 ICDB 维护;手动“版本历史与备份…”继续直接进入可见原生窗口,由用户选择版本。此修改只调整维护触发条件,不宣称修复了原生 Repair 内部失败。 + +## 测试与边界 + +先新增回归测试,在旧代码上验证健康工程及普通错误仍会进入 Project Backup 的失败,再实现入口修正。测试覆盖完整失联特征、无会话/只含错误码、共享库/其他工程、目标变化、正常打开、未释放、超时、取消、旧诊断文件、日志写入失败和延迟启动错误。 + +关联回归 151 项通过,后续新增的报告失败及等价路径用例纳入最终回归;维护入口专项最终 21 项通过。最终全量 **1283 passed**,643.89 秒,退出码 0,1 条既有 pywinauto STA 警告。编译和差异检查通过,冻结的 13 个关键 Python 模块及两个 VBS 与源码一致,启动/正常退出、Unicode/UNC 编解码和包内 x86 运行库加载通过。 + +独立静态审查发现目标的字面路径比较可能误拒绝映射盘与 UNC 身份,已改为比较两边解析后的工程及数据库路径,实际原生启动路径保持不变;等价路径回归先失败后通过。真实网络共享与 Win10 验收仍保留。复审无剩余必要 P1/P2。 + +关联回归、全量回归、构建与冻结检查保存在 `diagnostics/icdb-startup-gate-20261104/`,最终测试包的 `BUILD.json` 记录实际执行结果及源码哈希。 + +最终冻结包通过实际按钮在自有隔离库的 PENDING `IP101GR` 上再次打开手动备份列表、正常关闭并刷新为“待校验”,应用正常退出。没有点击 Repair/Restore;该试验用于确认手动入口独立性,不作为新的启动诊断或自动修复实机验收。 + +本轮不对原中心库执行 Repair/Restore,不以模拟诊断或隔离副本备份窗口打开成功代替现场自动修复验收。ICDB 自动修复内部问题仍未验收,工单及 WIP PR 保持开放。 diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index 3c622b0..c736762 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -195,21 +195,56 @@ def maintenance_target_guard(request, expected=None, *, stop=None): 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_diagnosed_maintenance(request, *, diagnose, progress=None, stop=None): + """Open PCB once; only a current matching native fault admits maintenance.""" + from .verify_runner import VerifyResult + from .icdb_recovery import parse_session_loss + from .native_repair import select_repair_target + if stop is not None and stop.is_set(): + return VerifyResult('CANCELLED', stderr='已取消,未打开工程或请求修复。') + if progress: + progress({'stage': 'icdb_startup_check', 'block': request.block_name, + 'detail': '尝试打开工程,依据本次原生报错判断是否需要 ICDB 维护'}) + startup = diagnose(replace(request, maintenance=False, auto_repair=False, + diagnostic_only=True, native_lm_verify=False, post_repair_proof=None)) + def finish(result): + if startup.evidence_path: + report = startup.evidence_path.with_name('maintenance-result.json') + recorded = replace(result, evidence_path=report) + try: + report.write_text(json.dumps({'block_name': request.block_name, + 'startup_evidence': str(startup.evidence_path), **asdict(recorded)}, + ensure_ascii=False, default=str, indent=2), encoding='utf-8') + except OSError as error: + import logging + logging.getLogger(__name__).warning('maintenance_result_write_failed path=%s error=%s', report, error) + else: + result = recorded + return result + if startup.status == 'PASS' and startup.safe_to_continue: + return finish(replace(startup, status='CHECKED', library_flag='UNKNOWN', + stdout='工程打开正常,无需 ICDB 修复;未执行 CBB 校验。')) + if startup.status == 'PASS': + return finish(replace(startup, status='FAIL', stderr='工程打开后未确认会话释放,未请求 ICDB 修复。')) + if startup.status != 'FAIL' or not startup.safe_to_continue: + return startup + # Do not search prior evidence directories or treat an error-code label, + # address/checkpoint file, timeout or generic server error as a diagnosis. + diagnosis = parse_session_loss(startup.stderr) + if diagnosis is None: + return startup + try: + target = select_repair_target(request, diagnosis) + except (ValueError, OSError) as error: + return finish(replace(startup, stderr=f'{startup.stderr}\n{error}', + error_stage='icdb_target_check')) + if stop is not None and stop.is_set(): + return finish(replace(startup, status='CANCELLED', stderr='已取消,未请求 ICDB 修复。')) + return run_existing_address_maintenance(request, diagnosed_target=target, startup_result=startup, + progress=progress, stop=stop) -def run_existing_address_maintenance(request, *, progress=None, stop=None): +def run_existing_address_maintenance(request, *, diagnosed_target, startup_result, progress=None, stop=None): from .library_lock import LibraryLock from .native_repair import run_native_repair, native_success_confirmed, repair_release_state from .verify_runner import VerifyResult @@ -219,7 +254,8 @@ def run_existing_address_maintenance(request, *, progress=None, stop=None): 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'} + 'block': request.block_name, 'stage': 'preflight', + 'startup_diagnosis': asdict(startup_result)} report = root/'result.json' owner_safe = False recovery = None @@ -244,6 +280,11 @@ def run_existing_address_maintenance(request, *, progress=None, stop=None): checked = lease.maintenance_proof target, context = checked['target'], checked['context'] record['maintenance_context'] = context + if (target.project.resolve() != diagnosed_target.project.resolve() + or target.database.resolve() != diagnosed_target.database.resolve()): + return save(VerifyResult('FAIL', safe_to_continue=owner_safe, + error_category='ICDB_TARGET_MISMATCH', error_stage='preflight', + stderr='本次打开报错的数据库与维护目标不一致,未请求 ICDB 修复。')) if stop is not None and stop.is_set(): raise ValueError('MAINTENANCE_CANCELLED: 已取消,未启动原生检查') emit('icdb_ownership_check', detail='调用原生 Project Backup 检查占用及恢复要求,不执行 Repair') diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index 9ab1a30..7012262 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -765,6 +765,8 @@ def execute_job(job, emit): if job.get('diagnostic_only'): # The diagnostic opens only to establish current native evidence. # Do not refresh integration, save/commit Cert, or edit PRJ paths. + if detail := native_error(): + raise RuntimeError(detail) pi = doc = app = None gc.collect() from .failed_session import close_failed_document @@ -782,6 +784,8 @@ def execute_job(job, emit): if time.monotonic() >= release_deadline: raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: 原生会话或数据库仍占用') time.sleep(.2) + if detail := native_error(): + raise RuntimeError(detail) completed = True emit(stage='diagnostic_complete', status='PASS') return {'status': 'PASS', 'stdout': 'ICDB 启动检查通过;未执行集成或保存,已关闭工程。'} diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index acc5017..db49686 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -690,9 +690,9 @@ class MainWindow(QWidget): if not names: return answer = QMessageBox.question(self, '非 READY ICDB 检查与修复', f'{MANUAL_BACKUP_NOTICE}\nMentor 原生修复可能生成自身备份。\n\n当前中心库:{self.loaded_path}\n\n' - f'将检查 {len(names)} 个可见非 READY CBB' + f'将尝试打开 {len(names)} 个可见非 READY CBB' f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '原生工具确认需要修复时执行 Repair,完成后可单独运行校验。\n' + '仅本次打开明确报出当前工程 ICDB 故障时调用原生修复,完成后可单独运行校验。\n' '若恢复了历史备份,请核对工程版本。\n\n是否开始?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: self._run(names, maintenance=True) @@ -977,6 +977,7 @@ class MainWindow(QWidget): self._icdb_completed += 1 self.icdb_progress.setValue(self._icdb_completed) phases = {'icdb_check_start': '检查工程数据库与校验状态', + 'icdb_startup_check': '正在尝试打开工程', 'icdb_ownership_check': '正在打开原生检查工具', 'native_started': '等待原生工具加载工程', 'native_occupancy_wait': '等待原生检查结果', diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 309b973..1e93ed6 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -547,13 +547,11 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。', safe_to_continue=False) if request.maintenance: - from .maintenance_entry import maintenance_needed, run_existing_address_maintenance - try: - needed = maintenance_needed(request) - except (ValueError, OSError) as error: - return VerifyResult('BLOCKED', stderr=str(error), safe_to_continue=False) - if needed: - return run_existing_address_maintenance(request, progress=progress, stop=stop) + from .maintenance_entry import run_diagnosed_maintenance + return run_diagnosed_maintenance(request, progress=progress, stop=stop, + diagnose=lambda probe: run_verify(probe, progress=progress, flag_reader=flag_reader, + process_runner=process_runner, port_check=port_check, + dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt, stop=stop)) from .library_lock import LibraryLock, LibraryBusy if Path(request.lmc_file).is_file(): try: diff --git a/tests/test_maintenance_diagnosis.py b/tests/test_maintenance_diagnosis.py index c2205ed..88ca6cb 100644 --- a/tests/test_maintenance_diagnosis.py +++ b/tests/test_maintenance_diagnosis.py @@ -1,6 +1,6 @@ """Native occupancy must precede repair even when old addresses remain.""" import pytest -from test_maintenance_entry import case, no_native_launch +from test_maintenance_entry import case, no_native_launch, startup_failure from test_native_occupancy import output @@ -12,18 +12,24 @@ def inspection(target, *, fault=False): @pytest.mark.parametrize('old_address', [False, True]) -def test_native_inspection_holds_lock_without_opening_pcb(case, monkeypatch, old_address): +def test_diagnosed_native_inspection_holds_lock_after_opening_pcb(case, monkeypatch, old_address): from cbb_refresh_verify import verify_runner as runner, native_repair, library_lock request, project, address = case if not old_address: address.unlink() + calls = [] + def startup(req, **kw): + assert req.diagnostic_only + calls.append('open') + return startup_failure(req) def native(target, root, **kw): + assert calls == ['open'] key = library_lock.LibraryLock(request.lmc_file).key assert library_lock._owners[key]['depth'] > 0 assert kw['inspection_only'] is True assert kw['timeout_seconds'] <= 30 return inspection(target) monkeypatch.setattr(native_repair, 'run_native_repair', native) - monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('inspection opened PCB')) + monkeypatch.setattr(runner, '_run_verify_serialized', startup) result = runner.run_verify(request) assert result.status == 'CHECKED' and result.recovery_attempts == () assert result.library_flag == 'UNKNOWN' @@ -45,7 +51,6 @@ def test_unconfirmed_native_inspection_never_repairs(case, monkeypatch, fault): if fault == 'not_exited': record['release']['native_exited'] = False return record monkeypatch.setattr(native_repair, 'run_native_repair', native) - monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('unconfirmed reopened PCB')) result = runner.run_verify(request) assert result.status == 'BLOCKED' and not result.recovery_attempts @@ -64,6 +69,9 @@ def test_native_fault_repairs_once_without_running_separate_verification(case, m address.unlink() # Simulated native repair release only. return complete_native_success(target) def verify(req, **kw): + if req.diagnostic_only: + calls.append('open') + return startup_failure(req) assert req.post_repair_proof is not None and not req.diagnostic_only and not req.auto_repair calls.append('verify') return runner.VerifyResult('PASS', library_flag='GREEN') @@ -71,6 +79,6 @@ def test_native_fault_repairs_once_without_running_separate_verification(case, m monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) monkeypatch.setattr(runner, '_run_verify_serialized', verify) result = runner.run_verify(request) - assert calls == ['inspect', 'repair'] + assert calls == ['open', 'inspect', 'repair'] assert result.status == 'REPAIRED' and len(result.recovery_attempts) == 1 assert result.library_flag == 'UNKNOWN' and not result.recovery_attempts[0]['validation_passed'] diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index edd94fd..662ba9b 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -39,6 +39,8 @@ def case(tmp_path, monkeypatch): (work/'PCB/pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n') monkeypatch.setattr(entry, 'assert_native_idle', lambda: None) request = VerifyRequest(library, 'IP101GR', tmp_path/'evidence', maintenance=True) + monkeypatch.setattr('cbb_refresh_verify.verify_runner._run_verify_serialized', + lambda req, **kw: startup_failure(req)) yield request, project, address import cbb_refresh_verify.library_lock as locks key = locks.LibraryLock(library).key @@ -47,6 +49,16 @@ def case(tmp_path, monkeypatch): if owner: owner['handle'].Close() +def startup_failure(request): + """Stub the PCB process boundary with a complete current native failure.""" + from cbb_refresh_verify.verify_runner import VerifyResult + database = request.lmc_file.parent/'ReusableBlocksLP'/request.block_name/'Work/database' + return VerifyResult('FAIL', safe_to_continue=True, error_category='ICDB_REPAIR_REQUIRED', + stderr=("The last server session on different server machine lost the connection to the database. " + "Please use iCDB Project Backup 'Repair project' functionality to fix this issue. " + f"Machine:[fixture-server] Path:[{database}] session:[fixture-current-session]")) + + def patch_verification_after_fault(monkeypatch, runner, verify): """Inject only native process boundaries; production interprets the Output.""" from cbb_refresh_verify import native_repair @@ -60,12 +72,13 @@ def patch_verification_after_fault(monkeypatch, runner, verify): 'output': output(target.project, target.database, fault=True), 'status': 'NOT_IN_USE', 'repair_required': True, 'release': {'native_exited': True}} monkeypatch.setattr(native_repair, 'run_native_repair', native) - monkeypatch.setattr(runner, '_run_verify_serialized', verify) + monkeypatch.setattr(runner, '_run_verify_serialized', + lambda req, **kw: startup_failure(req) if req.diagnostic_only else verify(req, **kw)) @pytest.mark.parametrize('pcb_config', ['standard', 'legacy_template']) @pytest.mark.parametrize('advertisement', ['present', 'absent']) -def test_explicit_maintenance_enters_official_repair_once_without_lostsession_fabrication(case, monkeypatch, pcb_config, advertisement): +def test_diagnosed_maintenance_enters_official_repair_once(case, monkeypatch, pcb_config, advertisement): from cbb_refresh_verify import verify_runner as runner, native_repair from test_native_repair import complete_native_success request, project, address = case diff --git a/tests/test_maintenance_review.py b/tests/test_maintenance_review.py index 0280833..cfb1930 100644 --- a/tests/test_maintenance_review.py +++ b/tests/test_maintenance_review.py @@ -111,3 +111,29 @@ def test_diagnostic_waits_for_normal_exit_after_close_returns(tmp_path, monkeypa lambda *a, **kw: {'status': 'CLOSED'}) assert execute()['status'] == 'PASS' assert len(observations) >= 2 + + +@pytest.mark.parametrize('when', ['document_ready', 'closing']) +def test_diagnostic_checks_late_startup_errors_before_declaring_healthy(tmp_path, monkeypatch, when): + from cbb_refresh_verify import mentor_adapter as adapter + job, events, execute = diagnostic_worker(tmp_path, monkeypatch) + job['diagnostic_only'] = True + source = Path(job['pcb']).parent/'LogFiles/PCBLogFile.txt' + def late_fault(): + source.parent.mkdir(exist_ok=True) + source.write_text("The last server session on different server machine lost the connection to the database. " + f"Machine:[fixture] Path:[{Path(job['project']).parent/'database'}] session:[current]", encoding='utf-8') + def document(*a, **kw): + if when == 'document_ready': late_fault() + return None, None, object() + closes = [] + def close(*a, **kw): + closes.append(True) + if when == 'closing': late_fault() + return {'status': 'CLOSED'} + monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', document) + monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document', close) + with pytest.raises(RuntimeError, match='ICDB_REPAIR_REQUIRED'): + execute() + assert len(closes) == 1 + assert not any(e['stage'] == 'diagnostic_complete' for e in events) diff --git a/tests/test_maintenance_startup_gate.py b/tests/test_maintenance_startup_gate.py new file mode 100644 index 0000000..233c192 --- /dev/null +++ b/tests/test_maintenance_startup_gate.py @@ -0,0 +1,185 @@ +"""Project Backup is reached only after a current, released PCB startup fault.""" +from dataclasses import replace +import json + +import pytest + +from test_maintenance_entry import case, no_native_launch + + +def fault_text(database): + return ("The last server session on different server machine lost the connection to the database. " + "Please use iCDB Project Backup 'Repair project' functionality to fix this issue. " + f"Machine:[fixture-server] Path:[{database}] session:[fixture-current-session]") + + +def install_probe(monkeypatch, result, calls): + from cbb_refresh_verify import verify_runner as runner + def probe(request, **kwargs): + assert request.diagnostic_only and not request.maintenance and not request.auto_repair + assert not request.native_lm_verify and request.post_repair_proof is None + calls.append('open') + return result + monkeypatch.setattr(runner, '_run_verify_serialized', probe) + + +def forbid_backup(monkeypatch): + from cbb_refresh_verify import native_repair + monkeypatch.setattr(native_repair, 'run_native_repair', + lambda *a, **k: pytest.fail('Project Backup launched without a current matching startup fault')) + + +def test_healthy_startup_skips_project_backup_even_with_old_address(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner + request, _, address = case + calls = [] + install_probe(monkeypatch, runner.VerifyResult('PASS', safe_to_continue=True), calls) + forbid_backup(monkeypatch) + result = runner.run_verify(replace(request, native_lm_verify=True)) + assert calls == ['open'] + assert result.status == 'CHECKED' and result.library_flag == 'UNKNOWN' + assert result.recovery_attempts == () and result.safe_to_continue + assert address.read_bytes() == b'old server address - preserved' + + +@pytest.mark.parametrize('message', ['License unavailable', 'Could not run server', + 'COM 已连接,尚无目标文档', 'ICDB_REPAIR_REQUIRED', + 'PCB 文件损坏', 'Lost connection to server']) +def test_generic_startup_errors_remain_failures_without_backup(case, monkeypatch, message): + from cbb_refresh_verify import verify_runner as runner + request, _, _ = case + calls = [] + failed = runner.VerifyResult('FAIL', stderr=message, safe_to_continue=True) + install_probe(monkeypatch, failed, calls) + forbid_backup(monkeypatch) + result = runner.run_verify(request) + assert calls == ['open'] and result == failed + + +@pytest.mark.parametrize('reason', ['shared', 'other_project', 'no_session', 'unreleased', 'timeout', 'cancelled']) +def test_unmatched_or_unreleased_icdb_fault_never_enters_backup(case, monkeypatch, reason): + from cbb_refresh_verify import verify_runner as runner + request, project, _ = case + database = (project.parent/'database' if reason not in ('shared', 'other_project') else + request.lmc_file.parent/('SymbolLibs/__ReusableBlocksSymb' if reason == 'shared' + else 'ReusableBlocksLP/Other/Work/database')) + if not database.exists(): database.mkdir(parents=True); (database/'icdb.dat').touch() + text = fault_text(database) + if reason == 'no_session': text = text.replace('session:[fixture-current-session]', '') + failed = runner.VerifyResult('TIMEOUT' if reason == 'timeout' else + 'CANCELLED' if reason == 'cancelled' else 'FAIL', stderr=text, + error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=reason != 'unreleased') + calls = [] + install_probe(monkeypatch, failed, calls) + forbid_backup(monkeypatch) + result = runner.run_verify(request) + assert calls == ['open'] and result.status == failed.status + assert not result.recovery_attempts + assert result.safe_to_continue is failed.safe_to_continue + + +def test_only_current_matching_fault_enters_native_maintenance(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, maintenance_entry as entry + request, project, _ = case + calls = [] + failed = runner.VerifyResult('FAIL', stderr=fault_text(project.parent/'database'), + error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True) + install_probe(monkeypatch, failed, calls) + def maintain(req, *, diagnosed_target, startup_result, **kwargs): + assert diagnosed_target.project == project and diagnosed_target.database == project.parent/'database' + assert startup_result == failed + calls.append('maintenance') + return runner.VerifyResult('REPAIRED') + monkeypatch.setattr(entry, 'run_existing_address_maintenance', maintain) + result = runner.run_verify(request) + assert calls == ['open', 'maintenance'] and result.status == 'REPAIRED' + + +def test_old_saved_diagnosis_does_not_turn_generic_startup_failure_into_repair(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner + request, project, _ = case + root = request.evidence_dir/'old'; (root/'diagnostics').mkdir(parents=True) + (root/'diagnostics/icdb-recovery.json').write_text(json.dumps({ + 'error_code': 'ICDB_REPAIR_REQUIRED', 'native_message': fault_text(project.parent/'database')})) + calls = [] + failure = runner.VerifyResult('FAIL', stderr='License unavailable', evidence_path=root/'result.json') + install_probe(monkeypatch, failure, calls) + forbid_backup(monkeypatch) + assert runner.run_verify(request) == failure + assert calls == ['open'] + + +def test_native_admission_cannot_switch_the_diagnosed_database(case, monkeypatch): + from cbb_refresh_verify import maintenance_entry as entry, native_repair + from test_maintenance_entry import startup_failure + request, project, _ = case + forbid_backup(monkeypatch) + result = entry.run_existing_address_maintenance(request, + diagnosed_target=native_repair.RepairTarget(project, project.parent/'other'), + startup_result=startup_failure(request)) + assert result.status == 'FAIL' and result.safe_to_continue + assert result.error_category == 'ICDB_TARGET_MISMATCH' + + +def test_equivalent_diagnosed_path_keeps_the_native_launch_path(case, monkeypatch): + from cbb_refresh_verify import maintenance_entry as entry, native_repair + from test_maintenance_entry import startup_failure + from test_maintenance_diagnosis import inspection + request, project, _ = case + seen = [] + def inspect(target, *args, **kwargs): + assert kwargs['inspection_only'] is True + seen.append(target.project) + return inspection(target) + monkeypatch.setattr(native_repair, 'run_native_repair', inspect) + alias = project.parent/'unused/..' + result = entry.run_existing_address_maintenance(request, + diagnosed_target=native_repair.RepairTarget(alias/project.name, alias/'database'), + startup_result=startup_failure(request)) + assert result.status == 'CHECKED' and result.safe_to_continue, result + assert seen == [project] + + +def test_unsafe_startup_success_is_not_reported_as_healthy(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner + request, _, _ = case + install_probe(monkeypatch, runner.VerifyResult('PASS', safe_to_continue=False), []) + forbid_backup(monkeypatch) + result = runner.run_verify(request) + assert result.status == 'FAIL' and not result.safe_to_continue + + +def test_report_write_failure_does_not_block_a_released_healthy_project(case, monkeypatch): + from pathlib import Path + from cbb_refresh_verify import verify_runner as runner + request, _, _ = case + evidence = request.evidence_dir/'result.json'; evidence.parent.mkdir(); evidence.write_text('{}') + install_probe(monkeypatch, runner.VerifyResult('PASS', evidence_path=evidence), []) + forbid_backup(monkeypatch) + write = Path.write_text + def unavailable(path, *args, **kwargs): + if path.name == 'maintenance-result.json': raise PermissionError('report unavailable') + return write(path, *args, **kwargs) + monkeypatch.setattr(Path, 'write_text', unavailable) + result = runner.run_verify(request) + assert result.status == 'CHECKED' and result.safe_to_continue + assert result.evidence_path == evidence and evidence.read_text() == '{}' + + +@pytest.mark.parametrize('before_open', [True, False]) +def test_cancelled_startup_diagnosis_never_requests_repair(case, monkeypatch, before_open): + from threading import Event + from cbb_refresh_verify import verify_runner as runner + from test_maintenance_entry import startup_failure + request, _, _ = case + stop = Event() + if before_open: stop.set() + calls = [] + def probe(req, **kw): + calls.append('open'); stop.set() + return startup_failure(req) + monkeypatch.setattr(runner, '_run_verify_serialized', probe) + forbid_backup(monkeypatch) + result = runner.run_verify(request, stop=stop) + assert result.status == 'CANCELLED' and result.safe_to_continue + assert calls == ([] if before_open else ['open']) -- 2.39.5 From 53118bae19666c4c23f776e3cc90887fffcfd5fe 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: Wed, 4 Nov 2026 23:40:46 +0800 Subject: [PATCH 56/59] feat: replace automatic ICDB maintenance with batch inspection --- .../2026-11-04-icdb-batch-check.md | 40 ++++ src/cbb_refresh_verify/icdb_maintenance.py | 7 +- src/cbb_refresh_verify/maintenance_entry.py | 79 ++++++++ src/cbb_refresh_verify/native_repair.py | 3 +- src/cbb_refresh_verify/native_repair_ui.py | 18 +- src/cbb_refresh_verify/ui/main_window.py | 53 +++-- src/cbb_refresh_verify/verify_runner.py | 16 +- tests/test_icdb_batch_check.py | 181 ++++++++++++++++++ tests/test_independent_operations.py | 4 +- tests/test_maintenance_diagnosis.py | 8 +- tests/test_maintenance_entry.py | 26 ++- tests/test_maintenance_startup_gate.py | 18 +- tests/test_maintenance_ui.py | 2 +- tests/test_manual_backup_ui.py | 2 +- tests/test_ui.py | 7 +- 15 files changed, 397 insertions(+), 67 deletions(-) create mode 100644 docs/diagnostics/2026-11-04-icdb-batch-check.md create mode 100644 tests/test_icdb_batch_check.py diff --git a/docs/diagnostics/2026-11-04-icdb-batch-check.md b/docs/diagnostics/2026-11-04-icdb-batch-check.md new file mode 100644 index 0000000..4d7050b --- /dev/null +++ b/docs/diagnostics/2026-11-04-icdb-batch-check.md @@ -0,0 +1,40 @@ +# ICDB 批量检查 + +## 当前行为 + +应用原“非 READY ICDB 检查与修复”入口改为“ICDB 批量检查”。检查可见非 READY 工程,有勾选时仅处理可见勾选项,READY 即使被勾选也排除。普通 CBB 校验、中心库管理和手动“版本历史与备份…”继续使用各自入口。 + +检查通过原生 Project Backup 加载所选工程,收集本次 Output 和占用结论,不启动 PCB,不调用 Repair、Restore 或版本选择。检查入口即使收到旧请求的 `auto_repair=True` 也只执行检查;其原生请求始终携带 `inspection_only=True`。内部显式恢复辅助 API 及其既有测试保留,应用批量检查不再调用它们。 + +结果分为: + +- 未发现异常:返回 CHECKED,仅表示本次原生检查未发现已识别的 ICDB 异常,不推定 CBB READY。 +- 发现 ICDB 问题:保留 FAIL 和明确的 ICDB 故障分类,不执行修复。 +- 工程占用:记录占用,不把它解释成数据库损坏。 +- 无法确认:缺少有效工程、输出不完整、目标不符、未知错误等均保留原始证据,不显示健康。 + +本项检查的原生进程确认退出后,发现问题、占用或未知结果可继续下一项;本次检查尚未退出或缺少退出证据时停止剩余批次,不强制终止原生进程。用户取消也停止队列。需要修复或回退时,由用户通过原生备份窗口选择操作。 + +## 去除检查路径的修复限制 + +目标绑定复用手动原生入口的唯一主 PRJ 和明确的工程内 `iCDBDir`。检查不要求 PCB、Layout 或 `icdb.dat` 预先存在,也不借用 Repair 的地址记录唯一性和多个数据库候选排除规则。缺失或损坏由原生加载检查给出结论;无法识别的结果保留为未知。 + +原生 worker 的检查模式不再因本机存在其他 PCB/Library Manager 而直接拒绝,也不执行 Repair 专用的数据库独占准入;它仍在启动前检查所选工程的文件链接,目标是否占用由原生 Output 判定。 + +检查使用实际库协调锁,忽略历史协调检查点,不自动归档旧记录或删除地址文件。释放必须有明确的原生退出证据,或 worker 明确报告“尚未启动原生进程”;结果字段/PID 缺失不推定释放。该调整只覆盖新检查路径,不能据此宣称维护/管理/释放检查中的所有过宽条件均已解决。 + +## 界面及报告 + +批量检查不再显示手动备份要求、自动修复承诺或“修复并校验通过”计数。确认清单、进度、备注、汇总及完整/异常/JSON 报告均按检查目标和检查分类输出。占用和未知备注不加“ICDB 问题”标签。默认副本恢复仍排除缺少 PCB 的工程,检查候选可以包含它们。 + +## 验证边界 + +本轮先以失败测试确认旧入口会打开 PCB、可进入 Repair、使用修复准入及误标未知结果,再修改入口。测试覆盖健康、明确故障、占用、未知、异常修复返回、缺少退出证据、明确未启动、取消、队列继续、缺 PCB/Layout/icdb、多数据库、READY 排除、仅检查汇总和报告。 + +全量测试、冻结模块一致性、启动冒烟及隔离库实际按钮记录保存在 `diagnostics/icdb-batch-check-20261104/`;最终包的 `BUILD.json` 记录实际结果。隔离验证只执行新批量检查,不在原库运行 Repair/Restore。 + +最终全量 **1304 passed**,270.79 秒,退出码 0,1 条既有 pywinauto STA 警告;编译和差异检查通过。冻结的 14 个关键 Python 模块及两个 VBS 与源码一致,启动/退出、Unicode/UNC 协议及包内 x86 运行库加载通过。独立审查发现的修复准入误用和缺字段推定释放均以先失败后通过的回归修正,复审无剩余必要 P1/P2。 + +最终冻结包通过真实按钮在自有隔离库的 PENDING `IP101GR` 上检查,原生返回数据库不一致且未占用,应用记录 `ICDB_REPAIR_REQUIRED`、`safe_to_continue=true`,汇总“发现问题 1”。原生请求为 `inspection_only=true`,返回 `repair_requested=false`,没有 PCB 请求或恢复尝试。原生及应用均正常退出,中心库 LMC 和工程 PRJ、DXP、icdb.dat、PCB、LayoutDB 六个文件的 SHA-256 前后一致。 + +原生加载检查不能保证 PCB 实际打开一定正常,也不能替代普通 CBB 校验。现场自动修复问题不作成功结论;工单和 WIP PR 保持开放。 diff --git a/src/cbb_refresh_verify/icdb_maintenance.py b/src/cbb_refresh_verify/icdb_maintenance.py index 01802d8..7c63a6a 100644 --- a/src/cbb_refresh_verify/icdb_maintenance.py +++ b/src/cbb_refresh_verify/icdb_maintenance.py @@ -19,6 +19,11 @@ def maintenance_counts(results): and attempt.get('native_status') == 'REPAIRED' for attempt in attempts) return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results), 'checked_without_repair': sum(r.status == 'CHECKED' for r in results), + 'issues_found': sum(r.error_category == 'ICDB_REPAIR_REQUIRED' for r in results), + 'in_use': sum(r.error_category == 'ICDB_DATABASE_IN_USE' for r in results), + 'unconfirmed': sum(r.status not in ('CHECKED', 'SKIPPED', 'CANCELLED') + and r.error_category not in ('ICDB_REPAIR_REQUIRED', 'ICDB_DATABASE_IN_USE') for r in results), + 'cancelled': sum(r.status == 'CANCELLED' for r in results), 'skipped': sum(r.status == 'SKIPPED' for r in results), 'repaired': repaired, 'review_required': review, 'version_choice_required': version_choice, @@ -28,7 +33,7 @@ def maintenance_counts(results): def write_maintenance_report(requests, results, batch_report, skipped_names): path = batch_report.with_name(batch_report.name.replace('batch-', 'icdb-', 1)).with_suffix('.json') - record = {'operation': 'visible_non_ready_icdb_check_and_repair', + record = {'operation': 'visible_non_ready_icdb_batch_check', 'automatic_repair': False, 'library': str(requests[0].lmc_file), 'includes_ready': False, 'target_names': [request.block_name for request in requests], 'shared_database_repair_supported': False, diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index c736762..f7154b9 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -195,6 +195,85 @@ def maintenance_target_guard(request, expected=None, *, stop=None): yield {'target': target, 'context': context, 'project': project, 'pcb': pcb} +def run_icdb_check(request, *, progress=None, stop=None): + """Inspect through Project Backup; never launch PCB, Repair or Restore.""" + from .verify_runner import VerifyResult + from .library_lock import LibraryLock, LibraryBusy + from .native_repair import run_native_repair, RepairTarget + from .native_backup import native_backup_project + from .backup_recovery import _project_text + from .native_occupancy import confirmed_inspection + root = Path(request.evidence_dir).resolve()/('icdb-check-'+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('FAIL', error_category='ICDB_INSPECTION_UNCONFIRMED', + stderr='检查记录目录需位于中心库之外。') + record = {'operation': 'icdb_batch_check', 'automatic_repair': False, + 'library': str(request.lmc_file), 'block': request.block_name} + released = True + def check(): + if stop is not None and stop.is_set(): + raise InterruptedError('已取消检查。') + def save(result): + report = root/'result.json' + recorded = replace(result, evidence_path=report) + try: + report.write_text(json.dumps({**record, 'result': asdict(recorded)}, + ensure_ascii=False, default=str, indent=2), encoding='utf-8') + except OSError as error: + import logging + logging.getLogger(__name__).warning('icdb_check_report_unavailable path=%s error=%s', report, error) + return result + return recorded + try: + check() + root.mkdir(parents=True, exist_ok=False) + with LibraryLock(request.lmc_file, checkpoint=False, safe_to_release=lambda: released): + project = native_backup_project(request.lmc_file, request.block_name) + values = re.findall(r'^KEY iCDBDir "([^"\r\n]*)"', _project_text(project), re.M) + if (len(values) != 1 or not values[0] or PureWindowsPath(values[0]).drive + or PureWindowsPath(values[0]).root + or (project.parent/values[0]).resolve() != (project.parent/'database').resolve()): + raise ValueError('无法确认工程的 ICDB 数据库路径。') + target = RepairTarget(project, project.parent/'database') + record['target'] = asdict(target) + check() + if progress: + progress({'stage': 'icdb_ownership_check', 'block': request.block_name, + 'detail': '正在检查 ICDB 状态'}) + released = False + inspection = run_native_repair(target, root/'native-inspection', + mentor_bin=request.mentor_bin, timeout_seconds=min(30, request.timeout_seconds), + progress=progress, inspection_only=True, stop=stop) + record['native_inspection'] = inspection + released = (inspection.get('release', {}).get('native_exited') is True + or inspection.get('native_started') is False + and inspection.get('native_pid') is None and inspection.get('worker_pid') is None) + if ((stop is not None and stop.is_set()) + or inspection.get('status') in ('CANCELLED', 'CANCELLED_UNRELEASED')): + return save(VerifyResult('CANCELLED', safe_to_continue=released, stderr='已取消 ICDB 检查。')) + if not confirmed_inspection(target, inspection): + return save(VerifyResult('FAIL', safe_to_continue=released, + error_category='ICDB_INSPECTION_UNCONFIRMED', + stderr='无法确认 ICDB 状态。' + str(inspection.get('detail', '')))) + if inspection['status'] == 'IN_USE': + return save(VerifyResult('BLOCKED', safe_to_continue=released, + error_category='ICDB_DATABASE_IN_USE', stderr='工程正在使用,暂未完成 ICDB 检查。')) + if inspection['repair_required']: + return save(VerifyResult('FAIL', safe_to_continue=released, + error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')) + return save(VerifyResult('CHECKED', safe_to_continue=released, + stdout='ICDB 检查未发现异常,无需修复。')) + except InterruptedError: + return save(VerifyResult('CANCELLED', safe_to_continue=released, stderr='已取消 ICDB 检查。')) + except LibraryBusy as error: + return save(VerifyResult('BLOCKED', safe_to_continue=False, + error_category='LIBRARY_IN_USE', stderr=str(error))) + except Exception as error: + return save(VerifyResult('FAIL', safe_to_continue=released, + error_category='ICDB_INSPECTION_UNCONFIRMED', stderr=f'无法确认 ICDB 状态:{error}')) + + def run_diagnosed_maintenance(request, *, diagnose, progress=None, stop=None): """Open PCB once; only a current matching native fault admits maintenance.""" from .verify_runner import VerifyResult diff --git a/src/cbb_refresh_verify/native_repair.py b/src/cbb_refresh_verify/native_repair.py index 10b76e2..b3c17c8 100644 --- a/src/cbb_refresh_verify/native_repair.py +++ b/src/cbb_refresh_verify/native_repair.py @@ -256,7 +256,8 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr maintenance_context=None, inspection_only=False, stop=None): """Supervise the GUI on its own desktop; a repair timeout never kills it.""" if stop is not None and stop.is_set(): - return {'status': 'CANCELLED', 'detail': '原生操作启动前已取消', 'release': {'safe_to_continue': True}} + return {'status': 'CANCELLED', 'native_started': False, + 'detail': '原生操作启动前已取消', 'release': {'safe_to_continue': True}} from .desktop_worker import DesktopWorker root = Path(root) root.mkdir(parents=True, exist_ok=True) diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index 4bc0ec5..dce7ec2 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -271,7 +271,7 @@ def repair_worker_main(job_path): stop = _CancellationFile(job['cancel_signal']) root = Path(job['root']) project, database = Path(job['project']), Path(job['database']) - result = {'status': 'BLOCKED', 'project': str(project), 'database': str(database), + result = {'status': 'BLOCKED', 'native_started': False, 'project': str(project), 'database': str(database), 'release': {'safe_to_continue': False}} process = None def emit(**event): @@ -293,7 +293,7 @@ def repair_worker_main(job_path): executable = home/'iCDB/win32/bin/iCDBProjectBackup.exe' command_id = None if job.get('inspection_only') else _repair_command(executable) from .mentor_adapter import pcb_processes, _process_details - if pcb_processes() or _process_details('LibraryManager.exe'): + if not job.get('inspection_only') and (pcb_processes() or _process_details('LibraryManager.exe')): raise ValueError('REPAIR_DATABASE_IN_USE') maintenance_context = job.get('maintenance_context') if maintenance_context is not None: @@ -302,9 +302,16 @@ def repair_worker_main(job_path): maintenance_request = VerifyRequest(Path(maintenance_context['library']), maintenance_context['block'], root, maintenance=True) emit(stage='native_preflight', detail='正在核对本次原生工具的目标及占用') - check_repair_access(RepairTarget(project, database)) - emit(stage='backup_skipped', reason='manual_backup_policy', - detail='应用不自动备份;Mentor 原生修复仍可能生成自身备份。') + if job.get('inspection_only'): + from .scoped_manifest import scan_tree + def check_cancelled(): + if stop.is_set(): + raise InterruptedError('检查已取消') + scan_tree(project.parent, check=check_cancelled) + else: + check_repair_access(RepairTarget(project, database)) + emit(stage='backup_skipped', reason='manual_backup_policy', + detail='应用不自动备份;Mentor 原生修复仍可能生成自身备份。') if Path(job['cancel_signal']).exists(): raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') if maintenance_context is not None: @@ -324,6 +331,7 @@ def repair_worker_main(job_path): if Path(job['cancel_signal']).exists(): raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH') process = subprocess.Popen([str(executable), '-p', str(project)], cwd=str(project.parent)) + result['native_started'] = True result['native_pid'] = process.pid emit(stage='native_started', pid=process.pid, project=str(project)) if job.get('inspection_only'): diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index db49686..4f18f04 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -152,8 +152,8 @@ class MainWindow(QWidget): self.browse_button = QPushButton('选择'); self.browse_button.clicked.connect(self.choose_library) self.selected_batch_button = QPushButton('批量校验已选'); self.selected_batch_button.clicked.connect(self.verify_selected) self.batch_button = QPushButton('校验可见非 READY'); self.batch_button.clicked.connect(self.verify_all) - self.icdb_repair_button = QPushButton('非 READY ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) - self.icdb_repair_button.setToolTip('忽略 READY 项(包括已勾选项);先由原生工具检查占用和数据库,仅确认当前工程需要 ICDB Repair 时才修复。') + self.icdb_repair_button = QPushButton('ICDB 批量检查'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all) + self.icdb_repair_button.setToolTip('检查可见的非 READY 工程;有勾选时仅检查勾选项。') self.backup_restore_button = QPushButton('从备份恢复 CBB…') self.backup_restore_button.clicked.connect(self.restore_backup) self.reimport_button = QPushButton('选择工程副本恢复失败项…') @@ -308,7 +308,8 @@ class MainWindow(QWidget): self.batch_button.setEnabled(ready and bool(self._eligible_names())) self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True))) self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names())) - self.backup_restore_button.setEnabled(ready and bool(self._maintenance_names())) + self.backup_restore_button.setEnabled(ready and bool( + self._eligible_names(checked_only=bool(self.selected_records())))) self.release_library_button.setEnabled(not self.busy and bool(self.lmc_edit.text().strip())) reasons = [] for operation, widget in (('add', self.add_button), ('update', self.update_button), ('remove', self.remove_button)): @@ -488,7 +489,8 @@ class MainWindow(QWidget): return [r.name for r in records if r.status != 'READY' and r.name not in self.skipped_names] def _maintenance_names(self): - return self._eligible_names(checked_only=bool(self.selected_records())) + records = self.selected_records() or self.visible_records() + return [record.name for record in records if record.status != 'READY'] def _check_visible(self, state): self.cbb_table.blockSignals(True) @@ -684,16 +686,14 @@ class MainWindow(QWidget): self._run(names) def repair_icdb_all(self): - from .backup_notice import MANUAL_BACKUP_NOTICE if self.busy or not self.loaded_path: return names = self._maintenance_names() if not names: return - answer = QMessageBox.question(self, '非 READY ICDB 检查与修复', - f'{MANUAL_BACKUP_NOTICE}\nMentor 原生修复可能生成自身备份。\n\n当前中心库:{self.loaded_path}\n\n' - f'将尝试打开 {len(names)} 个可见非 READY CBB' - f'{"(仅勾选项)" if self.selected_records() else ""}。未确认状态不等于损坏。\n' - '仅本次打开明确报出当前工程 ICDB 故障时调用原生修复,完成后可单独运行校验。\n' - '若恢复了历史备份,请核对工程版本。\n\n是否开始?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) + answer = QMessageBox.question(self, 'ICDB 批量检查', + f'当前中心库:{self.loaded_path}\n\n' + f'将检查 {len(names)} 个可见非 READY CBB' + f'{"(仅勾选项)" if self.selected_records() else ""}。\n' + '仅检查 ICDB 状态,不执行修复或回退。\n\n是否开始?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if answer == QMessageBox.Yes: self._run(names, maintenance=True) @@ -857,7 +857,7 @@ class MainWindow(QWidget): self.icdb_phase.setText('准备检查…') self.stop.clear(); self.recovery_path = None; self.recovery_button.setEnabled(False) self.output.clear(); self.status_label.setText( - f'正在非 READY ICDB 检查与修复:共 {len(names)} 个 CBB' if maintenance + f'正在 ICDB 批量检查:共 {len(names)} 个 CBB' if maintenance else f'正在校验:共 {len(names)} 个 CBB') def action(): from cbb_refresh_verify.verify_runner import read_library_flag_if_available, run_verify @@ -992,7 +992,7 @@ class MainWindow(QWidget): row = self._row_by_name.get(block) if row is not None: detail = str(event.get('detail') or event.get('error') or '').strip() - if str(event.get('error_category', '')).startswith('ICDB') and detail: + if event.get('error_category') == 'ICDB_REPAIR_REQUIRED' and detail: detail = 'ICDB 问题:' + detail label = str(stage) if not detail else f'{stage}: {detail[:96]}' self.cbb_table.item(row, 3).setText(label) @@ -1038,24 +1038,22 @@ class MainWindow(QWidget): if self._global_icdb_run: from cbb_refresh_verify.icdb_maintenance import maintenance_counts counts = maintenance_counts(results) - text = ('非 READY ICDB 检查已结束\n' - f'修复并校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped}\n' - f'仅检查、未修复 {counts["checked_without_repair"]}(不计入修复成功)\n' - f'原生修复完成、待单独校验 {counts["repaired_unverified"]},恢复备份待复核 {counts["review_required"]},' - f'ICDB已修复/PCB版本待选 {counts["version_choice_required"]},' - f'修复未确认 {counts["repair_unconfirmed"]}\n' + text = ('ICDB 批量检查已结束\n' + f'未发现异常 {counts["checked_without_repair"]},发现问题 {counts["issues_found"]},' + f'占用 {counts["in_use"]},无法确认 {counts["unconfirmed"]},' + f'取消 {counts["cancelled"]},队列未执行 {skipped}\n' f'完整日志:{report}\n仅失败日志:{failure_report_path(report)}') - self.icdb_phase.setText('已停止,存在待处理项目' if failed or skipped else '已完成非 READY 检查') + self.icdb_phase.setText('批量检查已结束') if any(not result.safe_to_continue for result in results): text += '\n未确认数据库或会话安全,已停止剩余校验。请查看恢复现场。' status_text = (f'已结束:通过 {passed},失败/受阻 {failed},' f'仅检查 {checked},未执行 {skipped + len(self.skipped_names)}。') - if repaired: + if self._global_icdb_run: + status_text = (f'检查结束:未发现异常 {counts["checked_without_repair"]},' + f'发现问题 {counts["issues_found"]},占用 {counts["in_use"]},' + f'无法确认 {counts["unconfirmed"]},取消 {counts["cancelled"]},未执行 {skipped}。') + if repaired and not self._global_icdb_run: status_text += f'\n原生 ICDB 修复完成 {repaired},待单独校验。' - if self._global_icdb_run and counts['review_required']: - status_text += f'\n恢复备份待复核 {counts["review_required"]},请核对工程版本。' - if self._global_icdb_run and counts['version_choice_required']: - status_text += f'\n待选择 PCB 版本 {counts["version_choice_required"]}。' first_failure = next((result.stderr for result in results if result.status not in ('PASS', 'SKIPPED', 'CHECKED', 'REPAIRED') and result.stderr), '') if first_failure: @@ -1072,7 +1070,7 @@ class MainWindow(QWidget): status_text += guidance self.status_label.setText(status_text.replace('\n', ' ')); self.output.append(text) self.summary = QMessageBox(self) - self.summary.setWindowTitle('ICDB 维护结果' if self._global_icdb_run else 'CBB 校验结果') + self.summary.setWindowTitle('ICDB 批量检查结果' if self._global_icdb_run else 'CBB 校验结果') self.summary.setText(status_text) self.summary.setDetailedText(text) self.summary.setIcon(QMessageBox.Warning if failed else QMessageBox.Information) @@ -1104,7 +1102,8 @@ class MainWindow(QWidget): management = getattr(self, 'management_dialog', None) if management is not None and management.isVisible(): management.stop.set() - self.stop.set(); self.status_label.setText('已请求取消;检查将停止,原生写入只等待安全结束,不强制终止。'); self._update() + self.stop.set(); self.status_label.setText('已请求取消 ICDB 检查。' if self._global_icdb_run else + '已请求取消;检查将停止,原生写入只等待安全结束,不强制终止。'); self._update() def _failed(self, error): if self.library_lease is not None: diff --git a/src/cbb_refresh_verify/verify_runner.py b/src/cbb_refresh_verify/verify_runner.py index 1e93ed6..8f60179 100644 --- a/src/cbb_refresh_verify/verify_runner.py +++ b/src/cbb_refresh_verify/verify_runner.py @@ -547,11 +547,8 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。', safe_to_continue=False) if request.maintenance: - from .maintenance_entry import run_diagnosed_maintenance - return run_diagnosed_maintenance(request, progress=progress, stop=stop, - diagnose=lambda probe: run_verify(probe, progress=progress, flag_reader=flag_reader, - process_runner=process_runner, port_check=port_check, - dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt, stop=stop)) + from .maintenance_entry import run_icdb_check + return run_icdb_check(request, progress=progress, stop=stop) from .library_lock import LibraryLock, LibraryBusy if Path(request.lmc_file).is_file(): try: @@ -1248,6 +1245,13 @@ def write_failure_report(requests, results, root, *, path=None): skipped = len(results)-passed-len(failed)-checked-repaired lines = ['CBB Verify 失败清单', f'总数: {len(results)} 成功: {passed} 失败: {len(failed)} 未执行: {skipped} 仅检查未修复: {checked} ICDB已修复待校验: {repaired}', 'CBB\t状态\t错误分类\t失败阶段\t桥状态\t原始错误\t证据\t实际耗时(秒)'] + if requests and all(req.maintenance for req in requests): + from .icdb_maintenance import maintenance_counts + counts = maintenance_counts(results) + lines[:2] = ['ICDB 检查异常清单', + f'总数: {len(results)} 未发现异常: {checked} 发现问题: {counts["issues_found"]} ' + f'占用: {counts["in_use"]} 无法确认: {counts["unconfirmed"]} ' + f'取消: {counts["cancelled"]} 未执行: {counts["skipped"]}'] for req, result in failed: timings = ' '.join(f'{key}={value:.3f}s' + ('(unfinished)' if key in result.incomplete_timing_phases else '') @@ -1268,6 +1272,8 @@ def write_batch_report(requests, results, root): path = root/('batch-' + time.strftime('%Y%m%d-%H%M%S') + '-' + uuid4().hex[:6] + '.log') lines = ['CBB Verify 批量结果', '流程: LMC 原生 Verify / ReusableCircuit PCB -> Project Integration -> Save/Close -> IsVerifiedReuseBlock READY(金标准)'] + if requests and all(req.maintenance for req in requests): + lines = ['ICDB 批量检查结果', '流程: 原生 Project Backup 加载检查;不执行 Repair、Restore 或 CBB 校验。'] for req, result in zip(requests, results): detail = _report_field(result.stderr or result.stdout.strip()[:200]) seconds = ' '.join(f'{key}={value:.3f}s' + diff --git a/tests/test_icdb_batch_check.py b/tests/test_icdb_batch_check.py new file mode 100644 index 0000000..9f6ccbd --- /dev/null +++ b/tests/test_icdb_batch_check.py @@ -0,0 +1,181 @@ +"""Batch ICDB inspection never opens PCB or dispatches Repair.""" +from dataclasses import replace +import json + +import pytest + +from test_maintenance_entry import case, no_native_launch +from test_maintenance_diagnosis import inspection + + +@pytest.mark.parametrize('scenario,status,category,safe', [ + ('healthy', 'CHECKED', '', True), + ('fault', 'FAIL', 'ICDB_REPAIR_REQUIRED', True), + ('busy', 'BLOCKED', 'ICDB_DATABASE_IN_USE', True), + ('unknown', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True), + ('unexpected_repair', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True), + ('missing_exit_proof', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', False), + ('not_launched', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True), + ('unreleased', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', False), + ('cancelled', 'CANCELLED', '', True), +]) +def test_batch_check_only_loads_native_inspection(case, monkeypatch, scenario, status, category, safe): + from cbb_refresh_verify import verify_runner as runner, native_repair, maintenance_entry as entry + from test_native_occupancy import output + request, _, address = case + calls = [] + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('check opened PCB')) + monkeypatch.setattr(entry, 'assert_native_idle', lambda: pytest.fail('check used host-wide owner gate')) + def native(target, root, **kw): + assert kw['inspection_only'] is True and not kw.get('maintenance_context') + calls.append('inspect') + record = inspection(target, fault=scenario == 'fault') + if scenario == 'busy': + record.update(status='IN_USE', output=output(target.project, target.database, busy=True)) + if scenario in ('unknown', 'unreleased'): + record.update(status='UNCONFIRMED', output='No conclusive native response') + if scenario == 'unreleased': record['release']['native_exited'] = False + if scenario == 'cancelled': record.update(status='CANCELLED') + if scenario == 'unexpected_repair': record.update(status='REPAIRED', repair_requested=True) + if scenario == 'missing_exit_proof': return {} + if scenario == 'not_launched': return {'status': 'BLOCKED', 'native_started': False} + return record + monkeypatch.setattr(native_repair, 'run_native_repair', native) + result = runner.run_verify(replace(request, auto_repair=True)) + assert calls == ['inspect'] + assert (result.status, result.error_category, result.safe_to_continue) == (status, category, safe) + assert result.library_flag == 'UNKNOWN' and result.recovery_attempts == () + assert address.read_bytes() == b'old server address - preserved' + record = json.loads(result.evidence_path.read_text('utf-8')) + assert record['operation'] == 'icdb_batch_check' and record['automatic_repair'] is False + + +@pytest.mark.parametrize('case_type', ['no_layout', 'no_pcb', 'no_icdb', 'ambiguous_repair_targets']) +def test_inspection_does_not_require_repair_admission(case, monkeypatch, case_type): + from cbb_refresh_verify import verify_runner as runner, native_repair + request, project, address = case + if case_type == 'no_layout': (project.parent/'PCB/Layout/LayoutDB.lyt').unlink() + if case_type == 'no_pcb': (project.parent/'PCB/IP101GR.pcb').unlink() + if case_type == 'no_icdb': (project.parent/'database/icdb.dat').unlink() + if case_type == 'ambiguous_repair_targets': + address.unlink() + (project.parent/'PCB/pcb.prj').write_text('KEY iCDBDir "database"\n') + db = project.parent/'PCB/database'; db.mkdir(); (db/'icdb.dat').touch() + calls = [] + def native(target, *a, **kw): + assert kw['inspection_only'] is True and target.project == project + calls.append('inspect') + return inspection(target) + monkeypatch.setattr(native_repair, 'run_native_repair', native) + result = runner.run_verify(request) + assert calls == ['inspect'] + assert result.recovery_attempts == () + + +@pytest.mark.parametrize('before_launch', [True, False]) +def test_cancellation_never_dispatches_repair_or_reports_a_clean_check(case, monkeypatch, before_launch): + from threading import Event + from cbb_refresh_verify import verify_runner as runner, native_repair + request, _, _ = case + stop = Event(); calls = [] + if before_launch: stop.set() + def native(target, *a, **kw): + assert kw['inspection_only'] is True + calls.append('inspect'); stop.set() + return inspection(target) + monkeypatch.setattr(native_repair, 'run_native_repair', native) + result = runner.run_verify(request, stop=stop) + assert result.status == 'CANCELLED' and result.safe_to_continue + assert calls == ([] if before_launch else ['inspect']) + + +def test_released_icdb_fault_continues_the_check_batch(case, monkeypatch): + from cbb_refresh_verify import verify_runner as runner, native_repair + request, _, _ = case + calls = [] + def native(target, root, **kw): + assert kw['inspection_only'] is True + calls.append('inspect') + return inspection(target, fault=len(calls) == 1) + monkeypatch.setattr(native_repair, 'run_native_repair', native) + monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('check opened PCB')) + results = runner.run_batch([request, request]) + assert calls == ['inspect', 'inspect'] + assert [result.status for result in results] == ['FAIL', 'CHECKED'] + + +def test_inspection_ignores_other_pcb_and_skips_exclusive_repair_probe(tmp_path, monkeypatch): + import win32con + from test_native_repair import repair_worker_fixture + from test_native_occupancy import output + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True + job.write_text(json.dumps(data), encoding='utf-8') + monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: [(999, 'other.pcb', '')]) + monkeypatch.setattr('cbb_refresh_verify.native_repair.check_repair_access', + lambda *a: pytest.fail('inspection used exclusive Repair admission')) + monkeypatch.setattr(ui, '_repair_command', lambda *a: pytest.fail('inspection resolved Repair command')) + monkeypatch.setattr(ui, '_native_output', lambda *_: output(data['project'], data['database'], busy=True)) + assert ui.repair_worker_main(job) == 0 + assert actions == [(100, win32con.WM_CLOSE)] + + +def test_ui_check_summary_reports_findings_and_never_claims_repair(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + from cbb_refresh_verify.verify_runner import VerifyResult + from PySide6.QtWidgets import QMessageBox + requests, prompts = [], [] + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda req: (requests.append(req) or VerifyResult('FAIL', + error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')), + status_refresher=lambda names, _: {name: 'PENDING' for name in names}) + monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (prompts.append(a[2]) or QMessageBox.Yes)) + window.search_edit.setText('Beta') + window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy) + assert requests[0].maintenance and not requests[0].auto_repair + assert '批量检查' in window.icdb_repair_button.text() + assert not any('手动备份' in text for text in prompts) + assert window.summary.windowTitle() == 'ICDB 批量检查结果' + assert '发现问题 1' in window.summary.text() + assert '修复并校验通过' not in window.summary.detailedText() + + +@pytest.mark.parametrize('category,label', [('ICDB_DATABASE_IN_USE', '占用 1'), + ('ICDB_INSPECTION_UNCONFIRMED', '无法确认 1')]) +def test_busy_or_unknown_inspection_is_not_labelled_as_database_damage(qtbot, tmp_path, monkeypatch, category, label): + from test_ui_filters import load_window + from cbb_refresh_verify.verify_runner import VerifyResult + window = load_window(qtbot, tmp_path, monkeypatch, + verifier=lambda _: VerifyResult('FAIL', error_category=category, stderr='暂未完成检查。'), + status_refresher=lambda names, _: {name: 'PENDING' for name in names}) + window.search_edit.setText('Beta') + window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy) + assert label in window.summary.text() + assert 'ICDB 问题' not in window.cbb_table.item(window._row_by_name['Beta'], 3).text() + + +def test_missing_pcb_can_be_checked_without_enabling_verify_or_restore(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + from cbb_refresh_verify.models import CbbRecord + from cbb_refresh_verify.verify_runner import VerifyResult + calls = [] + window = load_window(qtbot, tmp_path, monkeypatch, + records=(CbbRecord('Missing', 'Power', status='PENDING'),), + verifier=lambda req: (calls.append(req.block_name) or VerifyResult('CHECKED')), + status_refresher=lambda names, _: {name: 'PENDING' for name in names}) + assert window.icdb_repair_button.isEnabled() + assert not window.batch_button.isEnabled() and not window.backup_restore_button.isEnabled() + window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy) + assert calls == ['Missing'] + + +def test_check_reports_describe_inspection_without_pcb_verification(case, tmp_path): + from cbb_refresh_verify.verify_runner import write_batch_report, failure_report_path, VerifyResult + request, _, _ = case + report = write_batch_report([request], [VerifyResult('FAIL', + error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')], tmp_path/'reports') + full = report.read_text('utf-8-sig') + failed = failure_report_path(report).read_text('utf-8-sig') + assert 'ICDB 批量检查' in full and 'Project Integration' not in full + assert 'ICDB 检查异常' in failed and '发现问题: 1' in failed + assert 'ICDB已修复待校验' not in failed diff --git a/tests/test_independent_operations.py b/tests/test_independent_operations.py index c490ce7..5d44346 100644 --- a/tests/test_independent_operations.py +++ b/tests/test_independent_operations.py @@ -56,7 +56,7 @@ def test_maintenance_guard_does_not_scan_other_cbb_projects(case, monkeypatch): def test_repair_finishes_without_calling_verify(case, monkeypatch): from cbb_refresh_verify import native_repair, verify_runner as runner - from test_maintenance_entry import patch_verification_after_fault + from test_maintenance_entry import patch_verification_after_fault, run_legacy_maintenance from test_native_repair import complete_native_success req, _, address = case def repair(target, *a, **kw): @@ -65,7 +65,7 @@ def test_repair_finishes_without_calling_verify(case, monkeypatch): monkeypatch.setattr(native_repair, 'run_native_repair', repair) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('maintenance started CBB verification')) - result = runner.run_verify(req) + result = run_legacy_maintenance(req) assert result.status == 'REPAIRED' and result.safe_to_continue assert result.library_flag == 'UNKNOWN' assert result.recovery_attempts[0]['validation_passed'] is False diff --git a/tests/test_maintenance_diagnosis.py b/tests/test_maintenance_diagnosis.py index 88ca6cb..b844bcb 100644 --- a/tests/test_maintenance_diagnosis.py +++ b/tests/test_maintenance_diagnosis.py @@ -1,6 +1,6 @@ """Native occupancy must precede repair even when old addresses remain.""" import pytest -from test_maintenance_entry import case, no_native_launch, startup_failure +from test_maintenance_entry import case, no_native_launch, startup_failure, run_legacy_maintenance from test_native_occupancy import output @@ -30,7 +30,7 @@ def test_diagnosed_native_inspection_holds_lock_after_opening_pcb(case, monkeypa return inspection(target) monkeypatch.setattr(native_repair, 'run_native_repair', native) monkeypatch.setattr(runner, '_run_verify_serialized', startup) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status == 'CHECKED' and result.recovery_attempts == () assert result.library_flag == 'UNKNOWN' assert result.safe_to_continue is True @@ -51,7 +51,7 @@ def test_unconfirmed_native_inspection_never_repairs(case, monkeypatch, fault): if fault == 'not_exited': record['release']['native_exited'] = False return record monkeypatch.setattr(native_repair, 'run_native_repair', native) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status == 'BLOCKED' and not result.recovery_attempts @@ -78,7 +78,7 @@ def test_native_fault_repairs_once_without_running_separate_verification(case, m monkeypatch.setattr(native_repair, 'run_native_repair', native) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) monkeypatch.setattr(runner, '_run_verify_serialized', verify) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert calls == ['open', 'inspect', 'repair'] assert result.status == 'REPAIRED' and len(result.recovery_attempts) == 1 assert result.library_flag == 'UNKNOWN' and not result.recovery_attempts[0]['validation_passed'] diff --git a/tests/test_maintenance_entry.py b/tests/test_maintenance_entry.py index 662ba9b..47c1139 100644 --- a/tests/test_maintenance_entry.py +++ b/tests/test_maintenance_entry.py @@ -59,6 +59,16 @@ def startup_failure(request): f"Machine:[fixture-server] Path:[{database}] session:[fixture-current-session]")) +def run_legacy_maintenance(request, **kwargs): + """Keep explicit recovery helper coverage separate from the batch-check API.""" + from cbb_refresh_verify import verify_runner as runner, maintenance_entry as entry + if not request.maintenance: + return runner.run_verify(request, **kwargs) + return entry.run_diagnosed_maintenance(request, + diagnose=lambda probe: runner.run_verify(probe, **kwargs), + progress=kwargs.get('progress'), stop=kwargs.get('stop')) + + def patch_verification_after_fault(monkeypatch, runner, verify): """Inject only native process boundaries; production interprets the Output.""" from cbb_refresh_verify import native_repair @@ -108,7 +118,7 @@ def test_diagnosed_maintenance_enters_official_repair_once(case, monkeypatch, pc assert req.lmc_file == request.lmc_file and req.block_name == request.block_name return runner.VerifyResult('PASS', library_flag='GREEN') patch_verification_after_fault(monkeypatch, runner, verify) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert calls == ['repair'] and result.status == 'REPAIRED' assert result.recovery_attempts[0]['validation_passed'] is False assert result.library_flag == 'UNKNOWN' @@ -220,7 +230,7 @@ def test_ordinary_verify_calls_native_verifier_despite_existing_address(case, mo calls = [] monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: (calls.append('verify') or runner.VerifyResult('FAIL', stderr='native ICDB problem'))) - result = runner.run_verify(replace(request, maintenance=False)) + result = run_legacy_maintenance(replace(request, maintenance=False)) assert calls == ['verify'] and result.status == 'FAIL' and result.safe_to_continue assert address.read_bytes() == b'old server address - preserved' @@ -249,7 +259,7 @@ def test_maintenance_guard_blocks_before_native(case, monkeypatch, obstacle): monkeypatch.setattr(entry, 'address_snapshot', mutate) monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('guard bypass')) try: - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status == 'BLOCKED' and not result.safe_to_continue assert result.error_stage == 'preflight' finally: @@ -298,7 +308,7 @@ def test_nonrepair_outcomes_never_verify_or_count_success(case, monkeypatch, sta 'status': status, 'release': {'safe_to_continue': False}, 'detail': 'native result'}) monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a: {'safe_to_continue': False}) patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('unsafe or nonrepair outcome reopened')) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status != 'PASS' and not result.safe_to_continue assert result.recovery_attempts[0]['status'] == status @@ -312,7 +322,7 @@ def test_native_success_cannot_bypass_remaining_address(case, monkeypatch): # Native release queries remain mocked, actual address/exclusive checks stay real. monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: []) monkeypatch.setattr('cbb_refresh_verify.mentor_adapter._process_details', lambda name: []) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status != 'PASS' and not result.safe_to_continue @@ -408,7 +418,7 @@ def test_failed_content_does_not_block_other_items_after_actual_release(case, mo monkeypatch.setattr(native_repair, 'run_native_repair', repair) monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release']) patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('nonrepair outcome reopened')) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status != 'PASS' assert result.safe_to_continue is expected_safe assert result.recovery_attempts[0]['status'] == status @@ -427,7 +437,7 @@ def test_successful_icdb_repair_then_version_choice_reports_partial_completion(c patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: runner.VerifyResult('FAIL', error_category='PCB_VERSION_CHOICE_REQUIRED', stderr='PCB版本待选:最后用户保存/自动保存 2026/9/24 23:06', safe_to_continue=False, evidence_path=request.evidence_dir/'verify/result.json')) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status == 'REPAIRED' and result.safe_to_continue assert '待单独校验' in result.stdout assert result.recovery_attempts[0]['native_status'] == 'REPAIRED' @@ -448,7 +458,7 @@ def test_user_chosen_pcb_history_is_not_reported_as_lossless_repair(case, monkey 'choice': 'last_user_saved', 'date': '2026/9/24 23:06', 'confirm_sent': True, 'historical_restore': True})) return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=root/'result.json') patch_verification_after_fault(monkeypatch, runner, verify) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status == 'REPAIRED' and result.library_flag == 'UNKNOWN' assert result.recovery_attempts[0]['status'] == 'REPAIRED_UNVERIFIED' assert not (request.evidence_dir/'verify').exists() diff --git a/tests/test_maintenance_startup_gate.py b/tests/test_maintenance_startup_gate.py index 233c192..f885dfd 100644 --- a/tests/test_maintenance_startup_gate.py +++ b/tests/test_maintenance_startup_gate.py @@ -4,7 +4,7 @@ import json import pytest -from test_maintenance_entry import case, no_native_launch +from test_maintenance_entry import case, no_native_launch, run_legacy_maintenance def fault_text(database): @@ -35,7 +35,7 @@ def test_healthy_startup_skips_project_backup_even_with_old_address(case, monkey calls = [] install_probe(monkeypatch, runner.VerifyResult('PASS', safe_to_continue=True), calls) forbid_backup(monkeypatch) - result = runner.run_verify(replace(request, native_lm_verify=True)) + result = run_legacy_maintenance(replace(request, native_lm_verify=True)) assert calls == ['open'] assert result.status == 'CHECKED' and result.library_flag == 'UNKNOWN' assert result.recovery_attempts == () and result.safe_to_continue @@ -52,7 +52,7 @@ def test_generic_startup_errors_remain_failures_without_backup(case, monkeypatch failed = runner.VerifyResult('FAIL', stderr=message, safe_to_continue=True) install_probe(monkeypatch, failed, calls) forbid_backup(monkeypatch) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert calls == ['open'] and result == failed @@ -72,7 +72,7 @@ def test_unmatched_or_unreleased_icdb_fault_never_enters_backup(case, monkeypatc calls = [] install_probe(monkeypatch, failed, calls) forbid_backup(monkeypatch) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert calls == ['open'] and result.status == failed.status assert not result.recovery_attempts assert result.safe_to_continue is failed.safe_to_continue @@ -91,7 +91,7 @@ def test_only_current_matching_fault_enters_native_maintenance(case, monkeypatch calls.append('maintenance') return runner.VerifyResult('REPAIRED') monkeypatch.setattr(entry, 'run_existing_address_maintenance', maintain) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert calls == ['open', 'maintenance'] and result.status == 'REPAIRED' @@ -105,7 +105,7 @@ def test_old_saved_diagnosis_does_not_turn_generic_startup_failure_into_repair(c failure = runner.VerifyResult('FAIL', stderr='License unavailable', evidence_path=root/'result.json') install_probe(monkeypatch, failure, calls) forbid_backup(monkeypatch) - assert runner.run_verify(request) == failure + assert run_legacy_maintenance(request) == failure assert calls == ['open'] @@ -145,7 +145,7 @@ def test_unsafe_startup_success_is_not_reported_as_healthy(case, monkeypatch): request, _, _ = case install_probe(monkeypatch, runner.VerifyResult('PASS', safe_to_continue=False), []) forbid_backup(monkeypatch) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status == 'FAIL' and not result.safe_to_continue @@ -161,7 +161,7 @@ def test_report_write_failure_does_not_block_a_released_healthy_project(case, mo if path.name == 'maintenance-result.json': raise PermissionError('report unavailable') return write(path, *args, **kwargs) monkeypatch.setattr(Path, 'write_text', unavailable) - result = runner.run_verify(request) + result = run_legacy_maintenance(request) assert result.status == 'CHECKED' and result.safe_to_continue assert result.evidence_path == evidence and evidence.read_text() == '{}' @@ -180,6 +180,6 @@ def test_cancelled_startup_diagnosis_never_requests_repair(case, monkeypatch, be return startup_failure(req) monkeypatch.setattr(runner, '_run_verify_serialized', probe) forbid_backup(monkeypatch) - result = runner.run_verify(request, stop=stop) + result = run_legacy_maintenance(request, stop=stop) assert result.status == 'CANCELLED' and result.safe_to_continue assert calls == ([] if before_open else ['open']) diff --git a/tests/test_maintenance_ui.py b/tests/test_maintenance_ui.py index 33f203d..fbece0a 100644 --- a/tests/test_maintenance_ui.py +++ b/tests/test_maintenance_ui.py @@ -78,7 +78,7 @@ def test_maintenance_summary_exposes_first_failure_reason(qtbot, tmp_path): assert detail not in window.summary.text() assert str(tmp_path) not in window.summary.text() assert detail not in window.status_label.text() - assert '失败/受阻 1' in window.summary.text() + assert '无法确认 1' in window.summary.text() def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qtbot, tmp_path, monkeypatch): diff --git a/tests/test_manual_backup_ui.py b/tests/test_manual_backup_ui.py index 4fbf7d9..636371f 100644 --- a/tests/test_manual_backup_ui.py +++ b/tests/test_manual_backup_ui.py @@ -19,7 +19,7 @@ def test_management_prompts_for_manual_backup_and_cancel_starts_nothing(qtbot, t assert '完整备份与检查点' not in seen[0] -@pytest.mark.parametrize('action', ['verify_all', 'verify_selected', 'repair_icdb_all']) +@pytest.mark.parametrize('action', ['verify_all', 'verify_selected']) def test_batch_prompts_once_for_manual_backup_and_cancel_does_not_start(qtbot, tmp_path, monkeypatch, action): from cbb_refresh_verify.ui.main_window import MainWindow from cbb_refresh_verify.models import CbbRecord, LibraryReadResult diff --git a/tests/test_ui.py b/tests/test_ui.py index 4e8f794..7c5fa36 100644 --- a/tests/test_ui.py +++ b/tests/test_ui.py @@ -78,7 +78,7 @@ def test_global_icdb_failure_preserves_untargeted_ready_and_stops_queue(qtbot, t assert window.cbb_table.item(1, 2).text() == '未知 / 需确认' assert window.cbb_table.item(2, 3).text().startswith('SKIPPED') -def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch): +def test_global_icdb_progress_and_check_summary(qtbot, tmp_path, monkeypatch): from PySide6.QtWidgets import QMessageBox confirmations = [] def confirm(*args, **kwargs): @@ -97,8 +97,9 @@ def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch): assert '非 READY' in confirmations[0] assert window.icdb_progress.value() == 1 assert window.icdb_progress.maximum() == 1 - assert '恢复备份待复核 1' in window.status_label.text() - assert window.summary.windowTitle() == 'ICDB 维护结果' + assert '无法确认 1' in window.status_label.text() + assert '恢复备份待复核' not in window.status_label.text() + assert window.summary.windowTitle() == 'ICDB 批量检查结果' reports = list((tmp_path/'reports').glob('icdb-*.json')) assert len(reports) == 1 -- 2.39.5 From aea6192ef9fc61e71a757ac0ac391b0f4a953380 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, 5 Nov 2026 07:42:49 +0800 Subject: [PATCH 57/59] fix: narrow cross-item and cross-library blockers --- docs/diagnostics/2026-11-05-blocker-scope.md | 26 ++++++++++++ src/cbb_refresh_verify/library_lock.py | 22 +++++++--- src/cbb_refresh_verify/maintenance_entry.py | 27 ++++++++++-- src/cbb_refresh_verify/reimport_recovery.py | 7 +++- src/cbb_refresh_verify/ui/main_window.py | 44 ++++++++++++++++++-- tests/test_batch_blockers.py | 15 +++++++ tests/test_independent_operations.py | 41 ++++++++++++++++++ tests/test_library_lock.py | 11 +++++ 8 files changed, 178 insertions(+), 15 deletions(-) create mode 100644 docs/diagnostics/2026-11-05-blocker-scope.md diff --git a/docs/diagnostics/2026-11-05-blocker-scope.md b/docs/diagnostics/2026-11-05-blocker-scope.md new file mode 100644 index 0000000..8f4d245 --- /dev/null +++ b/docs/diagnostics/2026-11-05-blocker-scope.md @@ -0,0 +1,26 @@ +# 阻塞范围收紧记录 + +本轮处理的是“一个 CBB 出问题后把整批和其他库一起停掉”的误阻塞。 + +## 已收紧 + +- 批次继续条件保持按实际释放证据判断。单项 FAIL、ICDB 检查发现问题或普通内容错误,只要本次原生/自动化客户端已经退出并返回 `safe_to_continue=true`,下一项继续;只有进程仍存活、退出状态无法确认或共享写入安全无法证明时才停止后续写操作。 +- 管理失败状态现在绑定实际中心库。切换到另一个中心库会清除旧库的 `_management_unsafe`,不会把库 A 的失败带到库 B;释放检查完成回调也只清除同一库的状态。 +- 显式维护的原生占用检查按目标 PRJ 命令行过滤无关 PCB/Library Manager。旧的零参数占用探针仍兼容,目标路径可用时不再被其他 CBB 阻塞。 +- 释放证明支持目标 CBB 范围。提供目标名称时,只检查该 Work 工程的地址记录、主要数据库文件和命令行包含该工程的原生客户端;其他 CBB 的 `sAddress.adr` 不再阻塞当前恢复。无目标范围的全库释放检查继续保守处理。 +- 释放接口新增参数保持旧调用兼容;旧插件或测试没有 `block_name` 参数时继续走全库证明路径。 +- UI 比较中心库身份不再对不可达 UNC 路径调用会访问网络的 `Path.resolve()`。网络共享暂时不可达时保持可用状态和错误提示,不在界面刷新中抛异常。 + +## 仍然必须停止的情况 + +- 当前项的 PCB、Project Backup、保存助手、Importer 或管理工作进程仍在运行。 +- 进程查询失败、原生窗口身份不明、退出状态缺失,或库锁/共享写入归属无法确认。 +- 用户明确点击取消,当前批次停止后续项目。 + +这些停止条件保护共享数据库和原生写入现场;移除它们会把一个仍在写入的工程与下一个 CBB 并行运行,可能造成更大范围的数据损坏。 + +## 验证 + +新增回归覆盖单项已释放失败继续批次、切库清除管理阻塞、目标 CBB 过滤无关进程/地址、旧释放接口兼容和不可达 UNC 路径。最终全量回归 **1309 passed**,1 条既有 pywinauto STA 警告;`compileall` 与 `git diff --check` 通过。 + +本轮没有修改图标等其他工作区文件,也没有对现场中心库执行写入、Repair 或 Restore。 diff --git a/src/cbb_refresh_verify/library_lock.py b/src/cbb_refresh_verify/library_lock.py index 523c748..a262a00 100644 --- a/src/cbb_refresh_verify/library_lock.py +++ b/src/cbb_refresh_verify/library_lock.py @@ -315,7 +315,7 @@ def owned_snapshot(path): @contextmanager -def _prove_library_released(library): +def _prove_library_released(library, block_name=None): """Hold all native database read handles during explicit lease release.""" import win32con, win32file from .owner_probe import query_owners @@ -333,11 +333,16 @@ def _prove_library_released(library): 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe', 'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES)) + scope_root = (Path(library).resolve().parent/'ReusableBlocksLP'/block_name/'Work').resolve() if block_name else None def check_owners(): owners = [] for item in query_owners(names): if item['pid'] == os.getpid(): continue + if scope_root is not None and item.get('command'): + command = str(item['command']).casefold().replace('/', '\\') + if str(scope_root).casefold().replace('/', '\\') not in command: + continue automation = item['name'].casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe') command = item['command'].casefold() if not automation or not command or any(flag in command for flag in ( @@ -348,11 +353,14 @@ def _prove_library_released(library): return owners check_owners() entries = scan_tree(Path(library).parent) - addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr'] + addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr' + and (scope_root is None or path.resolve().is_relative_to(scope_root))] if addresses: raise LibraryBusy(f'库中仍有数据库地址记录,不能自动解除:{addresses}') - native = [path for path in entries if path.is_file() - and (path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))] + native = [path for path in entries if path.is_file() and ( + path.suffix.casefold() == '.lmc' + or (path.name.casefold() in ('icdb.dat', 'layoutdb.lyt') + and (scope_root is None or path.resolve().is_relative_to(scope_root))))] if Path(library).resolve() not in native: raise LibraryBusy('目标库身份缺失,不能解除协调阻止。') with ExitStack() as stack: @@ -368,7 +376,7 @@ def _prove_library_released(library): uninitialize_com(initialized) -def release_retained_library(library, evidence_dir): +def release_retained_library(library, evidence_dir, *, block_name=None): """Explicit user-requested release, after fresh owner/address/access proof. The upload sidecar is never deleted. This acknowledges process release, @@ -398,7 +406,9 @@ def release_retained_library(library, evidence_dir): win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None) except Exception as error: raise LibraryBusy('其他进程仍持有库锁,不能解除。') from error - with _prove_library_released(library) as proof: + proof_context = (_prove_library_released(library, block_name=block_name) + if block_name else _prove_library_released(library)) + with proof_context as proof: marker = blocker_path(library) checkpoints = (marker, marker.with_suffix('.pending')) if any(path.exists() and checkpoint_record(path) is None for path in checkpoints): diff --git a/src/cbb_refresh_verify/maintenance_entry.py b/src/cbb_refresh_verify/maintenance_entry.py index f7154b9..dbd18e8 100644 --- a/src/cbb_refresh_verify/maintenance_entry.py +++ b/src/cbb_refresh_verify/maintenance_entry.py @@ -13,8 +13,9 @@ import stat from uuid import uuid4 -def assert_native_idle(): +def assert_native_idle(target=None): from .owner_probe import query_owners + from .failed_session import _command_args from .native_management import _retained_workers from .native_export_guard import assert_retained_exports_idle from .native_engineering_identity import _OWNER_NAMES @@ -33,10 +34,28 @@ def assert_native_idle(): if not automation or not command or any(flag in command for flag in ( '--verify-worker', '--repair-worker', '--management-worker')): owners.append(item) + if target is not None: + expected = os.path.normcase(os.path.abspath(str(target.project))) + scoped = [] + for item in owners: + command = str(item.get('command') or '') + if not command: + scoped.append(item) + elif expected in [os.path.normcase(os.path.abspath(arg)) for arg in _command_args(command)[1:]]: + scoped.append(item) + owners = scoped if owners: raise ValueError(f'MAINTENANCE_OWNER_ACTIVE: 请正常关闭原生程序/操作助手:{owners}') +def _assert_native_idle_for(target): + """Keep test/third-party zero-argument idle probes compatible.""" + import inspect + if not inspect.signature(assert_native_idle).parameters: + return assert_native_idle() + return assert_native_idle(target) + + def address_snapshot(paths): from .scoped_manifest import hash_file return {str(path.resolve()): {'sha256': hash_file(path), 'size': path.stat().st_size, @@ -173,7 +192,7 @@ def maintenance_target_guard(request, expected=None, *, stop=None): 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() + _assert_native_idle_for(target) check() def native_files(snapshot): return {path for path, info in snapshot.items() if stat.S_ISREG(info.st_mode) and ( @@ -185,7 +204,7 @@ def maintenance_target_guard(request, expected=None, *, stop=None): 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() + _assert_native_idle_for(target) current = maintenance_entries(request, check=check) if native_files(current) != files: raise ValueError('MAINTENANCE_SOURCE_CHANGED: 检查期间库文件集合发生变化') @@ -411,7 +430,7 @@ def run_existing_address_maintenance(request, *, diagnosed_target, startup_resul 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() + _assert_native_idle_for(target) 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 diff --git a/src/cbb_refresh_verify/reimport_recovery.py b/src/cbb_refresh_verify/reimport_recovery.py index 4905e23..fecd0a4 100644 --- a/src/cbb_refresh_verify/reimport_recovery.py +++ b/src/cbb_refresh_verify/reimport_recovery.py @@ -50,7 +50,12 @@ def run_reimport_recovery(requests, *, previous_evidence, evidence_dir, stage = 'release_check' save(stage) if progress: progress({'stage': stage, 'detail': '正在检查原生会话释放,尚未替换条目'}) - proof = release_retained_library(library, root/'release') + import inspect + block_name = requests[0].name if len(requests) == 1 else None + if 'block_name' in inspect.signature(release_retained_library).parameters: + proof = release_retained_library(library, root/'release', block_name=block_name) + else: + proof = release_retained_library(library, root/'release') if proof.get('status') != 'RELEASE_CONFIRMED': raise ValueError('原生会话释放未确认,未开始导入。') save('released', release=proof) diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 4f18f04..598098d 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -12,6 +12,14 @@ from PySide6.QtWidgets import (QWidget, QLineEdit, QPushButton, QFormLayout, QTableWidget, QTableWidgetItem, QHeaderView, QCheckBox, QVBoxLayout, QProgressBar, QComboBox, QTabBar, QStackedWidget, QSizePolicy) from PySide6.QtWidgets import QStyle + + +def _path_key(value): + """Compare local and unavailable UNC paths without touching the network.""" + raw = str(value or '') + if not raw: + return '' + return os.path.normcase(os.path.normpath(os.path.abspath(raw))) try: from dx_productivity.trial_task_center.ui.workers import ServiceWorker except ImportError: @@ -66,6 +74,8 @@ class MainWindow(QWidget): self.management_adapter = management_adapter or NativeManagementAdapter() self.management_capabilities = self.management_adapter.capabilities() self._management_unsafe = False + self._management_unsafe_library = None + self._management_operation_library = None self._maintenance_recovery_view = False self._reload_after_maintenance = False self._reimport_offers = () @@ -319,8 +329,10 @@ class MainWindow(QWidget): if reason: reasons.append(reason) self.management_capability_label.setText('\n'.join(dict.fromkeys(reasons))) + current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text()) if self.lmc_edit.text().strip() else None notice = ('上次管理操作尚未确认安全释放,请检查该操作的结果。' - if self._management_unsafe else '') + if (self._management_unsafe and self._management_unsafe_library is not None + and current_library == _path_key(self._management_unsafe_library)) else '') self.library_notice.setText(notice) self.library_notice.setVisible(bool(notice)) for widget in (self.lmc_edit, self.browse_button, self.load_button, self.cbb_table, @@ -362,20 +374,36 @@ class MainWindow(QWidget): if library.is_dir(): library = select_library_file(library) def completed(result): - self._management_unsafe = False + current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text().strip()) + if current_library == _path_key(library): + self._management_unsafe = False + self._management_unsafe_library = None session = self._native_backup_session if session is not None and session.library == library and session.process.poll() is not None: self._release_native_backup_session(session) message = f'原生会话释放检查通过。请审查未完成任务后重新读取中心库。\n{result["evidence_path"]}' self.status_label.setText(message); self.output.append(message) - self._start(lambda: release_retained_library(library, self.data_root/'release-checks'), completed) + block_name = None + if len(self._batch_requests) == 1: + block_name = self._batch_requests[0].block_name + elif len(self.selected_records()) == 1: + block_name = self.selected_records()[0].name + import inspect + if 'block_name' in inspect.signature(release_retained_library).parameters: + action = lambda: release_retained_library(library, self.data_root/'release-checks', + block_name=block_name) + else: + action = lambda: release_retained_library(library, self.data_root/'release-checks') + self._start(action, completed) def _management_block_reason(self, operation): if self.busy: return '当前任务尚未结束,请等待后再执行库管理操作。' if self._native_backup_same_library(): return '该库的版本与备份窗口仍在使用,关闭后可执行写入操作。' - if self._management_unsafe: + current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text()) if self.lmc_edit.text().strip() else None + if (self._management_unsafe and self._management_unsafe_library is not None + and current_library == _path_key(self._management_unsafe_library)): return '上次管理操作的会话尚未安全释放,请先查看异常证据并执行释放检查。' capability = self.management_capabilities[operation] if not capability.supported: @@ -411,6 +439,7 @@ class MainWindow(QWidget): self._global_icdb_run = False self.stop.clear() self._management_is_recovery = bool(recovery_from) + self._management_operation_library = _path_key(requests[0].library) if requests else None self.management_dialog = ManagementDialog(requests, self, adapter=self.management_adapter, recovery_from=recovery_from) self._management_results = [] @@ -444,6 +473,7 @@ class MainWindow(QWidget): if (getattr(self, '_management_is_recovery', False) and results and all(result.safe_to_continue for result in results)): self._management_unsafe = False + self._management_unsafe_library = None self._maintenance_recovery_view = False restored = {result.name for result in results if result.status == 'UPLOADED_VERIFIED'} self._reimport_offers = tuple(offer for offer in self._reimport_offers if offer[1].name not in restored) @@ -455,6 +485,7 @@ class MainWindow(QWidget): self.output.append('副本恢复校验通过,版本待核对。') if any(not result.safe_to_continue for result in results): self._management_unsafe = True + self._management_unsafe_library = self._management_operation_library if self.library_lease: self.library_lease.retain_for_recovery() self.status_label.setText('管理操作需要人工确认恢复/释放;已停止写入,未再次读取原生目录。') @@ -541,6 +572,11 @@ class MainWindow(QWidget): if self.busy or not self.lmc_edit.text(): return self._maintenance_recovery_view = False self._global_icdb_run = False + if self._management_unsafe_library is not None: + requested_library = _path_key(self.lmc_edit.text()) + if requested_library != _path_key(self._management_unsafe_library): + self._management_unsafe = False + self._management_unsafe_library = None self.icdb_progress.hide(); self.icdb_phase.hide() self._release_library_lease() self.loaded_path = None; self.cbb_table.setRowCount(0); self._row_by_name = {}; self.ready_names = set() diff --git a/tests/test_batch_blockers.py b/tests/test_batch_blockers.py index 4fd069a..9d40d74 100644 --- a/tests/test_batch_blockers.py +++ b/tests/test_batch_blockers.py @@ -41,6 +41,21 @@ def test_batch_continuation_follows_actual_release(tmp_path, monkeypatch, status if owner: owner['handle'].Close() +def test_item_failure_with_confirmed_release_does_not_stop_unrelated_items(tmp_path): + from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult + library = tmp_path/'library.lmc'; library.touch() + requests = [VerifyRequest(library, name, tmp_path/'evidence') for name in ('A', 'B')] + calls = [] + def verify(request): + calls.append(request.block_name) + return VerifyResult('FAIL' if request.block_name == 'A' else 'PASS', + stderr='单项失败但会话已释放' if request.block_name == 'A' else '', + safe_to_continue=True) + results = runner.run_batch(requests, verifier=verify) + assert calls == ['A', 'B'] + assert [result.status for result in results] == ['FAIL', 'PASS'] + + def test_stop_worker_escalates_only_after_cooperative_deadline(): calls = [] class Worker: diff --git a/tests/test_independent_operations.py b/tests/test_independent_operations.py index 5d44346..2382d1f 100644 --- a/tests/test_independent_operations.py +++ b/tests/test_independent_operations.py @@ -41,6 +41,47 @@ def test_catalog_checkpoint_does_not_disable_ordinary_verify(qtbot, tmp_path, mo assert 'ICDB' in window.cbb_table.item(window._row_by_name['Beta'], 3).text() +def test_management_release_block_is_scoped_to_the_loaded_library(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + from cbb_refresh_verify.management import Capability + adapter = type('Adapter', (), { + 'capabilities': lambda self: {name: Capability(True, 'available') + for name in ('add', 'update', 'remove')}})() + window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter) + window._management_unsafe = True + window._management_unsafe_library = (tmp_path/'library-a.lmc').resolve() + window.lmc_edit.setText(str(tmp_path/'library-b.lmc')) + window.loaded_path = str(tmp_path/'library-b.lmc') + window.mentor_lmc_path = Path(window.loaded_path) + window.select_visible_button.click() + window._update() + assert window.add_button.isEnabled() + assert '上次管理操作' not in window._management_block_reason('update') + + +def test_loading_a_different_library_clears_old_management_block(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch) + old = (tmp_path/'library-a.lmc').resolve() + window._management_unsafe = True + window._management_unsafe_library = old + window.lmc_edit.setText(str(tmp_path/'library-b.lmc')) + window.load_library() + qtbot.waitUntil(lambda: not window.busy) + assert not window._management_unsafe + assert window._management_unsafe_library is None + + +def test_targeted_native_idle_ignores_unrelated_native_command(tmp_path, monkeypatch): + from cbb_refresh_verify import maintenance_entry as entry + from cbb_refresh_verify.native_repair import RepairTarget + project = tmp_path/'target.prj'; project.touch() + monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda names: [ + {'pid': 10, 'name': 'ExpeditionPCB.exe', 'command': 'ExpeditionPCB.exe other.pcb'}, + ]) + entry.assert_native_idle(RepairTarget(project, tmp_path/'database')) + + def test_maintenance_guard_does_not_scan_other_cbb_projects(case, monkeypatch): from cbb_refresh_verify import maintenance_entry as entry from cbb_refresh_verify import scoped_manifest as manifests diff --git a/tests/test_library_lock.py b/tests/test_library_lock.py index 5b3c4a7..6883152 100644 --- a/tests/test_library_lock.py +++ b/tests/test_library_lock.py @@ -134,6 +134,17 @@ def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkey assert proof['exclusive_files'] == [str(library)] +def test_scoped_release_ignores_another_cbb_address(tmp_path, monkeypatch): + import cbb_refresh_verify.library_lock as locks + import cbb_refresh_verify.owner_probe as probe + monkeypatch.setattr(probe, 'query_owners', lambda *_: []) + library = tmp_path/'lib.lmc'; library.touch() + other = tmp_path/'ReusableBlocksLP/Other/Work/database/cdbsvr/sAddress.adr' + other.parent.mkdir(parents=True); other.touch() + with locks._prove_library_released(library, block_name='Target') as proof: + assert proof['addresses'] == [] + + def test_release_probe_timeout_does_not_clear_retained_checkpoint(tmp_path, monkeypatch): import cbb_refresh_verify.library_lock as locks import cbb_refresh_verify.owner_probe as probe -- 2.39.5 From 1acfbd637452e8f64d96981044194661dfb4ec1b 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, 5 Nov 2026 08:24:53 +0800 Subject: [PATCH 58/59] fix: allow native inspection and backup entry after batch errors --- src/cbb_refresh_verify/native_repair_ui.py | 10 +++++++--- src/cbb_refresh_verify/ui/main_window.py | 23 +++++++++++++++++----- tests/test_icdb_batch_check.py | 21 ++++++++++++++++---- tests/test_native_backup.py | 10 ++++++++++ tests/test_native_occupancy.py | 13 ++++++++++++ 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/src/cbb_refresh_verify/native_repair_ui.py b/src/cbb_refresh_verify/native_repair_ui.py index dce7ec2..9642297 100644 --- a/src/cbb_refresh_verify/native_repair_ui.py +++ b/src/cbb_refresh_verify/native_repair_ui.py @@ -199,10 +199,14 @@ def _inspect_project(job, process, emit): while process.poll() is None and time.monotonic() < deadline + 3: windows = [w for w in _windows(process.pid) if not ( w['title'] == '' and w['class'] in ('UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator'))] - mains = [w for w in windows if w['class'].startswith('Afx:') - and w['title'].casefold() == ('iCDB Project Backup - '+str(project)).casefold()] - main = mains[0] if len(mains) == 1 else None shells = [w for w in windows if w['class'].startswith('Afx:') and w['title'] == 'iCDB Project Backup'] + exact = [w for w in windows if w['class'].startswith('Afx:') + and w['title'].casefold() == ('iCDB Project Backup - '+str(project)).casefold()] + # EE7.9 can keep the generic shell title while loading the requested PRJ. + # Its Output still binds the project; waiting only for the decorated title + # turns a valid session into a false timeout. + mains = exact or shells + main = mains[0] if len(mains) == 1 else None unknown = [w for w in windows if w is not main and w not in shells] if unknown: result.update(status='UNCONFIRMED', unknown_dialogs=unknown) diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 598098d..84920a0 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -312,8 +312,9 @@ class MainWindow(QWidget): # are optional and only enable the extra contents check. ready = not self.busy and self.loaded_path == self.lmc_edit.text() and self.cbb_table.rowCount() > 0 selected = self.selected_records() + native_target = self._native_backup_target() self.native_backup_button.setEnabled(ready and self._native_backup_session is None - and len(selected) == 1 and selected[0].status != 'READY') + and native_target is not None) ready = ready and not self._native_backup_same_library() self.batch_button.setEnabled(ready and bool(self._eligible_names())) self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True))) @@ -521,7 +522,19 @@ class MainWindow(QWidget): def _maintenance_names(self): records = self.selected_records() or self.visible_records() - return [record.name for record in records if record.status != 'READY'] + return [record.name for record in records + if record.status != 'READY' and record.name not in self.skipped_names] + + def _native_backup_target(self): + selected = self.selected_records() + if selected: + return selected[0] if len(selected) == 1 and selected[0].status != 'READY' else None + row = self.cbb_table.currentRow() + if row < 0 or self.cbb_table.isRowHidden(row): + return None + item = self.cbb_table.item(row, 0) + record = self._records_by_name.get(item.text()) if item is not None else None + return record if record is not None and record.status != 'READY' else None def _check_visible(self, state): self.cbb_table.blockSignals(True) @@ -740,12 +753,12 @@ class MainWindow(QWidget): def open_native_backup(self): if self.busy or not self.loaded_path or self._native_backup_session is not None: return - selected = self.selected_records() - if len(selected) != 1 or selected[0].status == 'READY': + target = self._native_backup_target() + if target is None: self.status_label.setText('请勾选一个非 READY CBB。') return from cbb_refresh_verify.native_backup import launch_native_backup - request = self._request(selected[0].name) + request = self._request(target.name) launcher = self.native_backup_launcher or launch_native_backup self._global_icdb_run = False self._reimport_offers = () diff --git a/tests/test_icdb_batch_check.py b/tests/test_icdb_batch_check.py index 9f6ccbd..f134512 100644 --- a/tests/test_icdb_batch_check.py +++ b/tests/test_icdb_batch_check.py @@ -154,7 +154,7 @@ def test_busy_or_unknown_inspection_is_not_labelled_as_database_damage(qtbot, tm assert 'ICDB 问题' not in window.cbb_table.item(window._row_by_name['Beta'], 3).text() -def test_missing_pcb_can_be_checked_without_enabling_verify_or_restore(qtbot, tmp_path, monkeypatch): +def test_missing_pcb_is_skipped_from_icdb_batch_check_and_restore(qtbot, tmp_path, monkeypatch): from test_ui_filters import load_window from cbb_refresh_verify.models import CbbRecord from cbb_refresh_verify.verify_runner import VerifyResult @@ -163,10 +163,23 @@ def test_missing_pcb_can_be_checked_without_enabling_verify_or_restore(qtbot, tm records=(CbbRecord('Missing', 'Power', status='PENDING'),), verifier=lambda req: (calls.append(req.block_name) or VerifyResult('CHECKED')), status_refresher=lambda names, _: {name: 'PENDING' for name in names}) - assert window.icdb_repair_button.isEnabled() + assert not window.icdb_repair_button.isEnabled() assert not window.batch_button.isEnabled() and not window.backup_restore_button.isEnabled() - window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy) - assert calls == ['Missing'] + assert calls == [] + + +def test_missing_project_rows_are_excluded_from_icdb_batch_check(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + from cbb_refresh_verify.models import CbbRecord + from cbb_refresh_verify.verify_runner import VerifyResult + window = load_window(qtbot, tmp_path, monkeypatch, + records=(CbbRecord('Missing', 'Power', status='PENDING'), + CbbRecord('Good', 'Power', status='PENDING')), + verifier=lambda req: VerifyResult('CHECKED'), + status_refresher=lambda names, _: {name: 'PENDING' for name in names}) + window.skipped_names.add('Missing') + window._update() + assert window._maintenance_names() == ['Good'] def test_check_reports_describe_inspection_without_pcb_verification(case, tmp_path): diff --git a/tests/test_native_backup.py b/tests/test_native_backup.py index 56cf52d..850a94b 100644 --- a/tests/test_native_backup.py +++ b/tests/test_native_backup.py @@ -136,6 +136,16 @@ def test_native_backup_selection_requires_one_visible_non_ready_item(qtbot, tmp_ window = load_window(qtbot, tmp_path, monkeypatch, native_backup_launcher=lambda _: pytest.fail('unexpected launch')) assert not window.native_backup_button.isEnabled() + + +def test_native_backup_can_use_current_non_ready_row_without_checkbox(qtbot, tmp_path, monkeypatch): + from test_ui_filters import load_window + window = load_window(qtbot, tmp_path, monkeypatch, + native_backup_launcher=lambda _: pytest.fail('unexpected launch')) + row = window._row_by_name['Beta'] + window.cbb_table.setCurrentCell(row, 0) + window._update() + assert window.native_backup_button.isEnabled() window.search_edit.setText('Alpha'); window.select_visible_button.click() assert not window.native_backup_button.isEnabled() window.search_edit.clear(); window.select_visible_button.click() diff --git a/tests/test_native_occupancy.py b/tests/test_native_occupancy.py index ccfa24a..00acc9a 100644 --- a/tests/test_native_occupancy.py +++ b/tests/test_native_occupancy.py @@ -53,6 +53,19 @@ def test_inspection_mode_only_observes_and_closes_its_tool(tmp_path, monkeypatch assert actions == [(100, win32con.WM_CLOSE)] +def test_generic_project_backup_shell_is_accepted_as_the_single_target_window(tmp_path, monkeypatch): + import json + from test_native_repair import repair_worker_fixture + ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active') + data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True + job.write_text(json.dumps(data), encoding='utf-8') + monkeypatch.setattr(ui, '_native_output', lambda windows: ( + f'Loading project file [{data["project"]}].\nChecking if project is in use.\n' + "Project is not in use. Full iCDB Project Backup's functionality is available.\n" + 'Backup information loaded correctly.')) + assert ui.repair_worker_main(job) == 0 + + def test_only_check_never_counts_as_repair_success(): from cbb_refresh_verify.icdb_maintenance import maintenance_counts from cbb_refresh_verify.verify_runner import VerifyResult -- 2.39.5 From 6327ad10a24e6468a355a048688d22c299258d54 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, 5 Nov 2026 13:41:27 +0800 Subject: [PATCH 59/59] fix: release private desktop on worker launch failure --- .../2026-11-05-desktop-leak-issue5.md | 36 +++++++++++ src/cbb_refresh_verify/desktop_worker.py | 59 +++++++++++++------ src/cbb_refresh_verify/mentor_adapter.py | 5 +- tests/test_desktop_worker.py | 56 ++++++++++++++++++ 4 files changed, 137 insertions(+), 19 deletions(-) create mode 100644 docs/diagnostics/2026-11-05-desktop-leak-issue5.md create mode 100644 tests/test_desktop_worker.py diff --git a/docs/diagnostics/2026-11-05-desktop-leak-issue5.md b/docs/diagnostics/2026-11-05-desktop-leak-issue5.md new file mode 100644 index 0000000..5336ca2 --- /dev/null +++ b/docs/diagnostics/2026-11-05-desktop-leak-issue5.md @@ -0,0 +1,36 @@ +# Issue 5:私有桌面泄露 + +## 现象 + +原生 worker 创建私有 Windows Desktop 后,如果 Job Object 创建或 worker 进程启动失败,构造函数直接抛出异常,已创建的 Desktop 和 Job 句柄没有释放。重复触发失败会留下孤立的私有桌面对象,表现为桌面资源持续增加、后续同名或同类原生任务启动异常。 + +状态读取路径还有一个相同的边界:`read_library_flag` 在进入资源清理保护前加载 UI 依赖;依赖加载异常时,刚创建的私有桌面无法执行 `CloseDesktop`。 + +## 根因 + +1. `DesktopWorker.__init__` 的资源申请跨越多个可能失败的步骤,但没有事务式回滚。 +2. 进程句柄、Job 句柄和 Desktop 句柄的释放顺序没有覆盖构造阶段异常。 +3. `read_library_flag` 的依赖导入位于 `CreateDesktop` 之后、`try/finally` 之前。 + +## 解决方案 + +- 在 `DesktopWorker` 构造开始时初始化资源字段,并把 Desktop、Job、worker 创建放入同一异常边界。 +- 构造失败时按 worker、Job、Desktop 顺序回滚;已启动的 worker 先终止,避免留下无父级的原生进程。 +- 在状态读取创建 Desktop 前完成 UI 依赖加载,确保依赖异常不会产生未受保护的 Desktop。 +- 增加回归测试,模拟 worker 启动失败并断言 Desktop、Job 均已关闭。 + +## 影响范围 + +- 只影响原生 worker 启动失败和状态读取依赖加载失败时的清理路径。 +- 正常任务的私有桌面、Job Object、进程树和数据库释放策略不变。 +- 不会切换用户输入桌面,也不会改变中心库写入策略。 + +## 验证 + +- `python -m pytest -q tests/test_desktop_worker.py tests/test_dialog_agent.py tests/test_native_inspection_release_regressions.py tests/test_verify_diagnostics.py`:33 passed。 +- `python -m compileall -q src tests`:通过。 +- `git diff --check`:通过。 + +## 分支 + +`codex/desktop-leak-pr` diff --git a/src/cbb_refresh_verify/desktop_worker.py b/src/cbb_refresh_verify/desktop_worker.py index d1782d0..a051818 100644 --- a/src/cbb_refresh_verify/desktop_worker.py +++ b/src/cbb_refresh_verify/desktop_worker.py @@ -74,27 +74,52 @@ class DesktopWorker: def __init__(self, command, name, *, interactive_input=False): self.name = name self.interactive_input = bool(interactive_input) - self.desktop = None if self.interactive_input else win32service.CreateDesktop( - name, 0, win32con.GENERIC_ALL, None) - # Track the session, but never use kill-on-close: PCB descendants can - # still own a database transaction when the Python supervisor exits. - self.job = win32job.CreateJobObject(None, '') - self.job_bound = False + self.desktop = None + self.job = None + self.handle = None self.companions = [] self.companion_pids = [] - startup = win32process.STARTUPINFO() - # subprocess.STARTUPINFO does NOT pass lpDesktop to CreateProcess. - startup.lpDesktop = startup_desktop_name(name, interactive_input=self.interactive_input) - self.handle, thread, self.pid, _ = win32process.CreateProcess( - command[0], subprocess.list2cmdline(command), None, None, False, - win32con.CREATE_NO_WINDOW, None, None, startup) - thread.Close() + self.job_bound = False + # Track the session, but never use kill-on-close: PCB descendants can + # still own a database transaction when the Python supervisor exits. try: - win32job.AssignProcessToJobObject(self.job, self.handle) - self.job_bound = True + self.desktop = None if self.interactive_input else win32service.CreateDesktop( + name, 0, win32con.GENERIC_ALL, None) + self.job = win32job.CreateJobObject(None, '') + startup = win32process.STARTUPINFO() + # subprocess.STARTUPINFO does NOT pass lpDesktop to CreateProcess. + startup.lpDesktop = startup_desktop_name(name, interactive_input=self.interactive_input) + self.handle, thread, self.pid, _ = win32process.CreateProcess( + command[0], subprocess.list2cmdline(command), None, None, False, + win32con.CREATE_NO_WINDOW, None, None, startup) + thread.Close() + try: + win32job.AssignProcessToJobObject(self.job, self.handle) + self.job_bound = True + except Exception: + # Kill-through-job is a cleanup guarantee, not a launch requirement. + self.job_bound = False except Exception: - # Kill-through-job is a cleanup guarantee, not a launch requirement. - self.job_bound = False + if self.handle is not None: + try: + win32process.TerminateProcess(self.handle, 1) + except Exception: + pass + try: + self.handle.Close() + except Exception: + pass + if self.job is not None: + try: + self.job.Close() + except Exception: + pass + if self.desktop is not None: + try: + self.desktop.CloseDesktop() + except Exception: + pass + raise def start_companion(self, command): startup = win32process.STARTUPINFO() diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index 7012262..0db3587 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -1449,11 +1449,12 @@ def read_library_flag(library, name, *, timeout_seconds=90, log_path=None): if not executable.is_file(): return FLAG_UNKNOWN, f'找不到 Library Manager: {executable}' + from pywinauto import Desktop + from pywinauto.controls.common_controls import TreeViewWrapper + deadline = time.monotonic() + timeout_seconds desktop_name = 'CbbFlagRead_' + os.urandom(5).hex() desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None) - from pywinauto import Desktop - from pywinauto.controls.common_controls import TreeViewWrapper handle = None pid = None diff --git a/tests/test_desktop_worker.py b/tests/test_desktop_worker.py new file mode 100644 index 0000000..c20367e --- /dev/null +++ b/tests/test_desktop_worker.py @@ -0,0 +1,56 @@ +import pytest + + +def test_constructor_releases_private_desktop_when_process_launch_fails(monkeypatch): + import cbb_refresh_verify.desktop_worker as module + + class Handle: + def __init__(self): + self.closed = False + + def Close(self): + self.closed = True + + def CloseDesktop(self): + self.closed = True + + desktop = Handle() + job = Handle() + + monkeypatch.setattr(module.win32service, 'CreateDesktop', lambda *args: desktop) + monkeypatch.setattr(module.win32job, 'CreateJobObject', lambda *args: job) + monkeypatch.setattr(module.win32process, 'STARTUPINFO', lambda: type('Startup', (), {})()) + monkeypatch.setattr(module.win32process, 'CreateProcess', + lambda *args: (_ for _ in ()).throw(RuntimeError('launch failed'))) + + with pytest.raises(RuntimeError, match='launch failed'): + module.DesktopWorker(['worker.exe'], 'PrivateDesk') + + assert desktop.closed is True + assert job.closed is True + + +def test_library_flag_loads_ui_dependencies_before_creating_private_desktop(monkeypatch, tmp_path): + import builtins + import win32service + import cbb_refresh_verify.mentor_adapter as module + + created = [] + original_import = builtins.__import__ + executable = tmp_path / 'LibraryManager.exe' + executable.touch() + + def deny_pywinauto(name, *args, **kwargs): + if name == 'pywinauto' or name.startswith('pywinauto.'): + raise ImportError('pywinauto unavailable') + return original_import(name, *args, **kwargs) + + monkeypatch.setattr(builtins, '__import__', deny_pywinauto) + monkeypatch.setattr(module, 'library_manager_path', lambda: executable) + monkeypatch.setattr(win32service, 'CreateDesktop', + lambda *args: created.append(args)) + + with pytest.raises(ImportError, match='pywinauto unavailable'): + module.read_library_flag('library.lmc', 'Block') + + assert created == [] -- 2.39.5