fix: reconcile reused identities and protect every native dispatch

This commit is contained in:
阿白叔 committed 2026-11-06 16:21:08 +08:00
1 parent cb11624b68
commit d9a79b6535
8 files changed
+159 -13

No files matched your search

+41 -5
View File
@@ -52,7 +52,8 @@ def test_add_timeout_publishes_cancellation_without_killing_native_owner(tmp_pat
assert len(native._retained_workers)==1
def test_cancellation_during_log_evidence_prevents_native_submit(tmp_path, monkeypatch):
@pytest.mark.parametrize('cancel', [True, False])
def test_cancellation_and_phase_at_native_add_submit(tmp_path, monkeypatch, cancel):
import os,sys
from types import SimpleNamespace
import win32gui,win32process
@@ -78,19 +79,54 @@ def test_cancellation_during_log_evidence_prevents_native_submit(tmp_path, monke
def post(h,*args):
state['posted'].append(h)
if h==110:state['new']=True
if h==210:
from cbb_refresh_verify.session_record import read_session_record
assert read_session_record(root/'worker-state.json', evidence_root=root)['phase'] == 'NATIVE_CALL'
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()
if path.name=='native-add-log-baseline.json' and cancel:(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'):
'name':'Block','timeout_seconds':5,'supervisor_pid':os.getpid(),
'operation_id':'test-add-phase','session_record_path':str(root/'session.json')}
from cbb_refresh_verify.session_record import write_session_record
write_session_record(root/'worker-state.json', {'operation_id': 'test-add-phase', 'phase':'POSTCALL', 'processes':[]})
with pytest.raises(RuntimeError,match='NATIVE_ADD_CANCELLED' if cancel else 'unsafe OK posted'):
native._probe_add_dialog_impl(SimpleNamespace(OnReusableBlocksEditor=lambda:None),job,desktop,{99},root)
assert state['posted']==[110]
assert state['posted']==([110] if cancel else [110,210])
def test_unknown_pending_add_does_not_send_cleanup_dialog_actions(tmp_path, monkeypatch):
from cbb_refresh_verify import native_management as native
def pending(app, job, *a, **kw):
job['_native_add_pending'] = True
raise TimeoutError('native Add pending')
monkeypatch.setattr(native, '_probe_add_dialog_impl', pending)
monkeypatch.setattr(native, '_close_probe_dialogs', lambda *a: pytest.fail('closing a pending Add'))
with pytest.raises(TimeoutError):
native._probe_add_dialog(None, {}, None, set(), tmp_path)
@pytest.mark.parametrize('acknowledged', [False, True])
def test_add_failure_only_allows_cleanup_after_confirmed_native_ack(tmp_path, monkeypatch, acknowledged):
import json
from cbb_refresh_verify import native_management as native
job = {}
def failed(app, job, *a, **kw):
job['_native_add_pending'] = True
(tmp_path/'native-add-output.json').write_text(json.dumps({'failure': {'acknowledged': acknowledged}}))
raise RuntimeError('NATIVE_ADD_FAILED: simulated complete error')
closed = []
monkeypatch.setattr(native, '_probe_add_dialog_impl', failed)
monkeypatch.setattr(native, '_close_probe_dialogs', lambda *a: closed.append(True))
with pytest.raises(RuntimeError, match='NATIVE_ADD_FAILED'):
native._probe_add_dialog(None, job, None, set(), tmp_path)
assert bool(closed) is acknowledged
assert job['_native_add_pending'] is not acknowledged
def test_copy_failure_log_must_be_new_and_match_current_block(tmp_path):