fix: preserve catalog state and verify PCB write access

This commit is contained in:
阿白叔 committed 2026-11-20 23:24:47 +08:00
1 parent 3efeaed656
commit 86837155ea
11 files changed
+651 -92

No files matched your search

+51 -1
View File
@@ -99,6 +99,42 @@ def _database_log_declined(root, pcb_pid):
return False
def _read_write_declined(job, root, pcb_pid):
from .dialog_models import WindowSnapshot
from .session_record import IDENTITY_KEYS
choice_path = root/'pcb-open-user-choice.json'
dialogs_path = root/'dialogs.jsonl'
try:
if (choice_path.stat().st_size > 1024*1024 or dialogs_path.stat().st_size > 1024*1024
or not job.get('operation_id') or (root/'save.log').exists()):
return False
choice = json.loads(choice_path.read_text(encoding='utf-8'))
binding = {key: job[key] for key in ('operation_id', 'desktop', 'project', 'pcb')}
if (any(choice.get(key) != value for key, value in binding.items())
or choice.get('library') != job['lmc_file'] or choice.get('choice') not in ('skip', 'cancel')):
return False
owner = choice['owner']['target']
if (owner.get('pid') != pcb_pid or not owner.get('owner_sid')
or any(owner.get(key) is None for key in IDENTITY_KEYS)):
return False
original = choice['snapshot']
snapshot = WindowSnapshot.from_record(original)
target = _cancel_target(snapshot)
if snapshot.pid != pcb_pid or target is None or target[0] != 'read_write_access':
return False
expected = 'PCB_OPEN_SKIPPED' if choice['choice'] == 'skip' else 'USER_CANCELLED'
for line in dialogs_path.read_text(encoding='utf-8').splitlines():
record = json.loads(line)
if (record.get('action') == 'user_confirmed_pcb_open' and record.get('accepted_control') == [7]
and record.get('error_code') == expected
and all(record.get(key) == value for key, value in original.items())):
return owner
except (OSError, ValueError, TypeError, KeyError, AttributeError):
pass
return False
def cancelled_prewrite_startup(job, pcb_pid=None):
"""Positive ordinary pre-document cancellation proof; absence of Save is insufficient."""
from .session_record import IDENTITY_KEYS, read_session_record
@@ -188,6 +224,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
declined_hwnd = None
declined_kind = None
cancelled_open = _database_log_declined(root, pcb_pid)
decline_owner = None
dismissed_tips = set()
close_handle = None
close_identity = None
@@ -200,8 +237,19 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
or residual_sessions._handle_identity(close_handle) != close_identity
or win32process.GetWindowThreadProcessId(hwnd)[1] != pcb_pid):
raise ValueError('STARTUP_ACTION_UNCONFIRMED')
if decline_owner:
actual = {'pid': pcb_pid, **close_identity,
**residual_sessions._force_ownership(close_handle, pcb_pid)}
if any(actual.get(key) != value for key, value in decline_owner.items()):
raise ValueError('STARTUP_ACTION_UNCONFIRMED: 原进程身份变化')
win32gui.PostMessage(hwnd, message, 0, 0)
while True:
# PCB can dismiss the prompt before the supervisor records its action.
if not cancelled_open:
decline_owner = _read_write_declined(job, root, pcb_pid)
if decline_owner:
cancelled_open = True
result['cancelled_prompt'] = 'read_write_access'
records = [r for r in _process_details('ExpeditionPCB.exe') if r[0] == pcb_pid]
if not records:
if (close_handle is not None
@@ -219,6 +267,8 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
result['release'] = {'pcb_exited': True, 'launcher_exited': launcher_exited,
'addresses': addresses,
'safe_to_continue': launcher_exited and (cancelled_open or not addresses)}
if addresses:
result['release']['resource_release_confirmed'] = False
if result['release']['safe_to_continue']:
result['status'] = 'CLOSED'
result.pop('detail', None)
@@ -354,7 +404,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
close_identity = residual_sessions._handle_identity(close_handle)
if Path(close_identity['image']).name.casefold() != 'expeditionpcb.exe':
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
if cancelled_diagnostic or cancelled_prewrite:
if cancelled_diagnostic or cancelled_prewrite or decline_owner:
cleanup_post(main[0],win32con.WM_CLOSE)
else:
win32gui.PostMessage(main[0], win32con.WM_CLOSE, 0, 0)