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] 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'