From ca39fb35179cca397dc7ade69a59a0665c17ec62 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, 9 Nov 2026 10:11:36 +0800 Subject: [PATCH] fix: clear verified queue blockers and exit idle orphan workers --- .../2026-11-09-review-319-validation.json | 53 +++++ .../2026-11-09-review-319-validation.md | 33 +++ .../plans/2026-11-09-review-319.md | 32 +++ src/cbb_refresh_verify/desktop_worker.py | 76 +++++-- src/cbb_refresh_verify/library_lock.py | 3 + src/cbb_refresh_verify/mentor_adapter.py | 15 +- src/cbb_refresh_verify/native_management.py | 2 + src/cbb_refresh_verify/residual_sessions.py | 4 + src/cbb_refresh_verify/session_record.py | 150 ++++++++++--- src/cbb_refresh_verify/ui/main_window.py | 1 + .../ui/residual_release_dialog.py | 15 +- tests/test_incident_safety.py | 4 +- tests/test_integration.py | 8 +- tests/test_management_supervision.py | 2 + tests/test_residual_release_dialog.py | 3 +- tests/test_review_319.py | 206 ++++++++++++++++++ 16 files changed, 537 insertions(+), 70 deletions(-) create mode 100644 docs/releases/2026-11-09-review-319-validation.json create mode 100644 docs/releases/2026-11-09-review-319-validation.md create mode 100644 docs/superpowers/plans/2026-11-09-review-319.md create mode 100644 tests/test_review_319.py diff --git a/docs/releases/2026-11-09-review-319-validation.json b/docs/releases/2026-11-09-review-319-validation.json new file mode 100644 index 0000000..95b543e --- /dev/null +++ b/docs/releases/2026-11-09-review-319-validation.json @@ -0,0 +1,53 @@ +{ + "date": "2026-11-09", + "review_url": "https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/pulls/13#issuecomment-319", + "base_commit": "96e179ba6529eefd1401a012155cd7b7cbc0ffc3", + "environment": { + "python": "3.12.10", + "windows": "10.0.26200" + }, + "full_suite": { + "tests": 1732, + "failures": 0, + "errors": 0, + "skipped": 0, + "duration_seconds": 501.166, + "xml": "F:\\Sync Tool Skyworth\\Work Maps\\cbb-refresh-verify-795\\diagnostics\\cbb-refresh-verify-795-review-stack-fixes-full.xml", + "xml_sha256": "578dd4b01db5c18ce657430ce3b2cd2dba8516d8191bdd31bf4e0df78187b3ba", + "warnings": [ + "Existing pywinauto Revert to STA COM threading mode warning" + ] + }, + "focused_suite": { + "passed": 79 + }, + "compileall": "passed", + "diff_check": "passed", + "independent_review": "No remaining important findings in the scoped diff; durable completion feedback addressed", + "tested_files_sha256": { + "src/cbb_refresh_verify/desktop_worker.py": "173a951d510e78e088c439768ecfe31ab198723e7717b314b4a74a9d590a1fe0", + "src/cbb_refresh_verify/library_lock.py": "391fb2f0b25f400676ba230472fb3bbd4a0d994ed5a8dfee46f8f32c7f198219", + "src/cbb_refresh_verify/mentor_adapter.py": "1fc8c98d5508120e03bf001a42dd3e0da7b165eb18c90f1aed83147d17db8638", + "src/cbb_refresh_verify/native_management.py": "96c2fe613a657d110302c8f7cc033b91ef256e168036f4794eb39be475e69de8", + "src/cbb_refresh_verify/residual_sessions.py": "a19622be3e959a7e64bf9de953450203499fb82618da8537307e5f61ef4d793b", + "src/cbb_refresh_verify/session_record.py": "c4c5771d11d7469980c02f2fb264b26e5ee26634a37daae008dbb65097ab4e0c", + "src/cbb_refresh_verify/ui/main_window.py": "e46096ce784933f3ab47e4c316f3c84e3e5cf07242489323f6234b61aeb22f80", + "src/cbb_refresh_verify/ui/residual_release_dialog.py": "ec7566bac6d92845f98933bffd4c06f31eeec42946e69699aff94e31f9a8baef", + "tests/test_incident_safety.py": "52f3ccb04db8971bf38628c2bd334d4fd79795e97de6538f6f66cb35f7123ece", + "tests/test_integration.py": "9e9d30692607c889e1e3744a63c79c68e3af7d4c86804e0bcebe48e68765086c", + "tests/test_management_supervision.py": "107196ce8a00dd76e7e3e83fb47ae2e67c857fa234492ba593bf71a7d7e1c5e5", + "tests/test_residual_release_dialog.py": "7d2bf95afc2f9112b9433c7d2fefde3b35b5e85a70a6bd506471c039e2505509", + "tests/test_review_319.py": "831fd34d426abb4436b9132bfb7ed754994155a34ac60a59378dee0964b54d28" + }, + "not_verified": [ + "Frozen execution of this revision", + "Real Mentor native writes/save/close during supervisor loss", + "Windows 10", + "Shared network/cross-machine release", + "ICDB field recovery" + ], + "automatic_icdb_repair": false, + "candidate_rebuilt": false, + "merged": false, + "issue_closed": false +} diff --git a/docs/releases/2026-11-09-review-319-validation.md b/docs/releases/2026-11-09-review-319-validation.md new file mode 100644 index 0000000..2c6cb1a --- /dev/null +++ b/docs/releases/2026-11-09-review-319-validation.md @@ -0,0 +1,33 @@ +# PR #13 review 319 validation + +Review: https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/pulls/13#issuecomment-319 + +Baseline: `96e179ba6529eefd1401a012155cd7b7cbc0ffc3`. + +## Changes + +- Successful explicit resource proof acknowledges the retained queue entries captured before proof. The alias-set identity is a generation token: unrelated libraries and newer handoffs remain retained. Scoped proof acknowledges only the matching block. +- Final observer outcomes remain available in the residual dialog after its observer ends. The existing refresh/confirm actions provide a running-session retry path. Successful release records a matching operation's RELEASED observation, so history refresh does not resurrect it as unconfirmed. Journal failure is reported separately from the established release fact. +- Discovery observes process identities only. It never opens production databases exclusively, deletes coordination checkpoints, closes clients, or replays operations. Startup observers use the same read-only mode; only explicit release or already-authorized task reconciliation can establish resource release. +- Supervisor loss sets cancellation independently of evidence-write success. An idle worker exits with code 75 only when its matching journal and synchronized in-memory phase both confirm no pending native write and every registered helper has exited. Missing/unknown/stale-active state retains the worker. The watchdog rechecks returned calls and cannot authorize a subsequent native write/helper. Native children are never terminated by this exit; worker exit is not database-release or Save-success evidence. +- Worker phase persistence and watchdog journal reads share a gate to avoid a Windows sharing violation between reading the JSON and atomically replacing it. The dead `_terminate_pid` function is removed; cleanup tests observe actual process-termination boundaries. + +## Verification + +Commands run through the existing test wrapper with a dedicated mutex namespace and Qt offscreen; no user/native Mentor process is part of these fixtures. + +``` +python -u -X utf8 diagnostics/run_review_fixes.py "F:/Sync Tool Skyworth/Work Maps/cbb-refresh-verify-795-review-stack" +python -X utf8 -m compileall -q src tests +git diff --check +``` + +The focused session/watchdog/management/dialog/lease regression run passed 79 tests. The initial regressions were observed failing before the corresponding fixes: explicit proof left a library unavailable, history scan attempted release, orphan idle workers stayed alive, residual refresh omitted runtime records, and successful release reappeared as unconfirmed. Real harmless Windows subprocess fixtures cover PRECALL/POSTCALL, NATIVE_CALL/CLOSING, unknown/missing/stale journals, failed cancel/evidence writes, and rechecking after return. + +Full-suite result is recorded in `2026-11-09-review-319-validation.json`. Independent review found no remaining P1/P2 in this scoped diff; its observation about durable completion was addressed and regression-tested. + +## Limits + +This is source/PR validation. The earlier `0.2.4b2-iconfix-44bb734` executable has not been rebuilt with these changes and must not be used as evidence for this revision. Its icon assets are preserved. + +Not validated here: frozen execution of this revision, real Mentor Save/Close or library writes during supervisor loss, Windows 10, shared/network or cross-machine release. ICDB automatic Repair remains disabled. No field-recovery acceptance, merge, formal publication or issue closure is implied. diff --git a/docs/superpowers/plans/2026-11-09-review-319.md b/docs/superpowers/plans/2026-11-09-review-319.md new file mode 100644 index 0000000..ffb8817 --- /dev/null +++ b/docs/superpowers/plans/2026-11-09-review-319.md @@ -0,0 +1,32 @@ +# PR #13 comment 319: residual lifecycle fixes + +**Goal:** Fix the P2 and three P3 findings against head `96e179b` without interrupting an in-flight native writer. + +**Scope:** https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/pulls/13#issuecomment-319 + +**Constraints:** Python 3.12/PySide6/pywin32, no new dependencies; existing isolated worktree and icon retained; no automatic ICDB Repair, native process termination, merge, release or issue closure. Real Mentor and shared-network acceptance remains outstanding. + +## 1. Release proof and queue state + +- [x] Reproduce an ended UNCONFIRMED observer leaving its library unavailable after explicit successful release. +- [x] Snapshot retained entries before proof; acknowledge only those entries, for the matching library, while proof handles are still held. Failed proof and newer handoffs remain blocked. Test independent libraries and queue resumption. +- [x] Retain final observer states for the existing residual panel, including unreadable journals; expose fresh recheck through that panel. + +## 2. Passive discovery + +- [x] Test that discovery of an exited session never calls release proof or modifies library checkpoints. +- [x] Separate process observation from explicit reconciliation; startup observation stays read-only, reports ACTIVE/UNCONFIRMED and cannot claim RELEASED without proof. +- [x] Refresh journal/status information from the running residual dialog, keeping all I/O off the Qt thread. + +## 3. Supervisor loss + +- [x] Use harmless real subprocesses to test PRECALL/POSTCALL exit, NATIVE_CALL/CLOSING retention, missing/unreadable journals and failed cancellation/journal writes. +- [x] Synchronize phase transitions and helper registration with watchdog shutdown. Only matching durable and in-memory non-pending state allows worker-only exit; preserve active helpers and unknown states. Prevent subsequent calls after supervisor loss. +- [x] Recheck after retained calls return; do not kill a native child or interpret worker exit as release proof. + +## 4. Dead code and validation + +- [x] Delete unused `_terminate_pid`; replace obsolete test hooks with assertions at actual process termination boundaries. +- [x] Run focused regressions, full Python suite, compileall and diff checks. Record results and field-validation limits, then commit scoped changes. Do not rebuild or publish a candidate without documenting its source and validation. + +Execution: implement inline in the existing isolated worktree; red/green tests per task. The user has authorized these fixes, so no further plan confirmation is needed. diff --git a/src/cbb_refresh_verify/desktop_worker.py b/src/cbb_refresh_verify/desktop_worker.py index 0013897..89aaf13 100644 --- a/src/cbb_refresh_verify/desktop_worker.py +++ b/src/cbb_refresh_verify/desktop_worker.py @@ -1,6 +1,8 @@ """Launch automation on a private Windows desktop, never the input desktop.""" import subprocess import os +import logging +from contextlib import contextmanager from pathlib import Path import threading import win32con @@ -13,6 +15,39 @@ import win32service _automation_clients = [] _automation_clients_lock = threading.Lock() _supervisor_lost = threading.Event() +_native_phases = {} + + +def _phase_pending(phase): + if phase not in ('PRECALL', 'POSTCALL', 'RELEASE_CHECK', 'FINISHED', 'NATIVE_CALL', 'CLOSING'): + return None + from .operation_lifecycle import stop_disposition + return stop_disposition(call_started=phase != 'PRECALL', + call_returned=phase in ('POSTCALL', 'RELEASE_CHECK', 'FINISHED'), + identity_confirmed=True) == 'WAIT_FOR_RETURN' + + +@contextmanager +def worker_phase(operation_id, phase): + with _automation_clients_lock: + if _supervisor_lost.is_set() and phase in ('NATIVE_CALL', 'CLOSING'): + raise RuntimeError('WORKER_CANCELLED: 监督器已退出,未提交新的原生调用') + _native_phases[operation_id] = phase + yield + + +def _record_write_pending(record_path, operation_id=None): + if record_path is None: + return None + from .session_record import read_session_record + path = Path(record_path).with_name('worker-state.json') + try: + state = read_session_record(path, evidence_root=path.parent) + if operation_id is not None and state['operation_id'] != operation_id: + return None + return _phase_pending(state.get('phase')) + except (OSError, ValueError): + return None def launch_save_helper(command, *, start_gate=None, identity_path=None, operation_id=None, **kwargs): @@ -58,13 +93,29 @@ def start_supervisor_watchdog(supervisor_pid, *, cancel_path=None, stop_clients= def watch(): try: win32event.WaitForSingleObject(parent, win32event.INFINITE) - _supervisor_lost.set() + with _automation_clients_lock: + _supervisor_lost.set() if cancel_path is not None: - Path(cancel_path).touch() + try: + Path(cancel_path).touch() + except OSError: + logging.getLogger(__name__).warning('Unable to record orphan cancellation', exc_info=True) if session_record_path is not None: from .session_record import write_session_record - write_session_record(Path(session_record_path).with_name('supervisor-lost.json'), - {'operation_id': operation_id, 'phase': 'SUPERVISOR_LOST'}) + try: + write_session_record(Path(session_record_path).with_name('supervisor-lost.json'), + {'operation_id': operation_id, 'phase': 'SUPERVISOR_LOST'}) + except (OSError, ValueError): + logging.getLogger(__name__).warning('Unable to record supervisor loss', exc_info=True) + while True: + with _automation_clients_lock: + pending = _record_write_pending(session_record_path, operation_id) + # The same gate blocks new calls/helpers after loss. A stale + # POSTCALL on disk cannot override an active in-memory call. + if (pending is False and _phase_pending(_native_phases.get(operation_id)) is False + and all(helper.poll() is not None for helper in _automation_clients)): + os._exit(75) # This Python worker only; native children remain untouched. + threading.Event().wait(.2) finally: parent.Close() @@ -232,22 +283,7 @@ class DesktopWorker: register_worker_session(self, job) def native_write_pending(self): - if not getattr(self, 'session_record_path', None): - return None - from .session_record import read_session_record - path = self.session_record_path.with_name('worker-state.json') - try: - state = read_session_record(path, evidence_root=path.parent) - from .operation_lifecycle import stop_disposition - phase = state.get('phase') - started = (False if phase == 'PRECALL' else True if phase in - ('NATIVE_CALL', 'CLOSING', 'POSTCALL', 'RELEASE_CHECK', 'FINISHED') else None) - returned = (False if phase in ('PRECALL', 'NATIVE_CALL', 'CLOSING') else True if phase in - ('POSTCALL', 'RELEASE_CHECK', 'FINISHED') else None) - return stop_disposition(call_started=started, call_returned=returned, - identity_confirmed=True) in ('WAIT_FOR_RETURN', 'OBSERVE_UNCONFIRMED') - except (OSError, ValueError): - return None + return _record_write_pending(getattr(self, 'session_record_path', None)) def request_stop(self): if not hasattr(self, 'cancel_path'): diff --git a/src/cbb_refresh_verify/library_lock.py b/src/cbb_refresh_verify/library_lock.py index 7ea8712..d987077 100644 --- a/src/cbb_refresh_verify/library_lock.py +++ b/src/cbb_refresh_verify/library_lock.py @@ -528,6 +528,8 @@ def release_retained_library(library, evidence_dir, *, block_name=None): """ import win32con, win32event, win32file library = Path(library).resolve(strict=True) + from .session_record import retained_session_snapshot, acknowledge_library_release + retained = retained_session_snapshot(library) evidence = Path(evidence_dir).resolve() if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence): raise ValueError('释放检查证据必须位于目标库之外。') @@ -584,6 +586,7 @@ def release_retained_library(library, evidence_dir, *, block_name=None): if owner: owner['handle'].Close() del _owners[lease.key] + acknowledge_library_release(record, retained, block_name=scoped) return record finally: if external is not None: diff --git a/src/cbb_refresh_verify/mentor_adapter.py b/src/cbb_refresh_verify/mentor_adapter.py index 890ac48..b2e127d 100644 --- a/src/cbb_refresh_verify/mentor_adapter.py +++ b/src/cbb_refresh_verify/mentor_adapter.py @@ -928,19 +928,6 @@ def execute_job(job, emit): lm_session.close() -def _terminate_pid(pid, *, timeout=20): - """Terminate a validated automation helper PID, never its native children.""" - try: - subprocess.run( - [str(Path(os.environ.get('WINDIR', r'C:\Windows'))/'System32'/'taskkill.exe'), '/PID', str(int(pid)), '/F'], - capture_output=True, - timeout=timeout, - creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0), - ) - except Exception: - pass - - def cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid=None, *, expected_pcb=None, baseline_pids=(), emit=None): """Observe native process release without interrupting database writes. @@ -1025,6 +1012,8 @@ def worker_main(request_path): try: if job.get('supervisor_pid'): + from .session_record import update_worker_state + update_worker_state(job, 'PRECALL') from .desktop_worker import start_supervisor_watchdog start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job.get('cancel_signal'), stop_clients=True, supervisor_identity=job.get('supervisor_identity'), diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 84d2fbb..47a2c4c 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -1224,6 +1224,8 @@ def management_worker_main(request_path): raise RuntimeError('原生管理操作仅允许在本次私有桌面执行。') _require_management_continuation(job, root) if job.get('cancel_signal'): + from .session_record import update_worker_state + update_worker_state(job, 'PRECALL') from .desktop_worker import start_supervisor_watchdog start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'], supervisor_identity=job.get('supervisor_identity'), diff --git a/src/cbb_refresh_verify/residual_sessions.py b/src/cbb_refresh_verify/residual_sessions.py index 96e9e50..d5dfcd4 100644 --- a/src/cbb_refresh_verify/residual_sessions.py +++ b/src/cbb_refresh_verify/residual_sessions.py @@ -512,6 +512,10 @@ def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False, ca or Path(proof.get('library', '')).resolve() != library): raise ValueError('释放证明与当前库不一致') result['status'] = 'RELEASE_CONFIRMED' + from .session_record import record_session_release + errors = record_session_release(proof, plan.get('runtime_sessions', [])) + if errors: + result['evidence_error'] = ';'.join(errors) except BaseException as error: result.update(status='PARTIAL', detail=f'{type(error).__name__}: {error}') if not isinstance(error, Exception): diff --git a/src/cbb_refresh_verify/session_record.py b/src/cbb_refresh_verify/session_record.py index 5bb453e..a2e08ba 100644 --- a/src/cbb_refresh_verify/session_record.py +++ b/src/cbb_refresh_verify/session_record.py @@ -15,6 +15,7 @@ IDENTITY_KEYS = ('pid', 'created_handle', 'image', 'owner_sid', 'session_id') _observers = {} _observer_guard = threading.Lock() _retained_libraries = {} +_retained_results = {} def _library_key(library): @@ -28,6 +29,61 @@ def library_available(library): return not any(key in aliases for aliases in _retained_libraries.values()) +def retained_session_snapshot(library): + key = _library_key(library) + with _observer_guard: + return {path: (aliases, _retained_results.get(path, {}).get('block')) + for path, aliases in _retained_libraries.items() if key in aliases} + + +def acknowledge_library_release(proof, snapshot, *, block_name=None): + """Called under fresh proof handles; never clear a newer handoff.""" + if proof.get('status') != 'RELEASE_CONFIRMED': + return + key = _library_key(proof['library']) + with _observer_guard: + records = [dict(_retained_results.get(path, {}), path=path) + for path, (aliases, block) in snapshot.items() + if key in aliases and _retained_libraries.get(path) is aliases + and (block_name is None or block == block_name)] + record_session_release(proof, records) + with _observer_guard: + for path, (aliases, block) in snapshot.items(): + if (key in aliases and _retained_libraries.get(path) is aliases + and (block_name is None or block == block_name)): + _retained_libraries.pop(path, None) + _retained_results.pop(path, None) + + +def record_session_release(proof, records): + """Persist a past proof only for the pre-proof operation, never replay it.""" + errors = [] + if proof.get('status') != 'RELEASE_CONFIRMED': + return errors + library = Path(proof['library']).resolve() + for record in records: + try: + path = Path(record['path']) + session = read_session_record(path, evidence_root=path.parent) + if (session['operation_id'] != record.get('operation_id') + or Path(session['library']).resolve() != library): + continue + write_session_record(path.with_name('observation-release-'+uuid4().hex+'.json'), { + 'operation_id': session['operation_id'], 'library': str(library), + 'status': 'RELEASED', 'release_proof': proof}) + except (OSError, ValueError, KeyError) as error: + errors.append(str(error)) + logging.getLogger(__name__).warning('Unable to record session release', exc_info=True) + return errors + + +def retained_session_states(library): + key = _library_key(library) + with _observer_guard: + return [dict(_retained_results[path], path=path) for path, aliases in _retained_libraries.items() + if key in aliases and path in _retained_results] + + def register_worker_session(worker, job): """The launcher must persist identity before opening the worker's gate.""" path = Path(job['session_record_path']) @@ -43,26 +99,28 @@ def update_worker_state(job, phase, *, identities=(), write_outcome=None, requir if required: raise ValueError('原生写入缺少会话记录位置,未提交新的调用') return False - path = Path(job['session_record_path']).with_name('worker-state.json') - try: - state = ({'operation_id': job['operation_id'], 'processes': []} if not path.exists() - else read_session_record(path, evidence_root=path.parent)) - if state['operation_id'] != job['operation_id']: - raise ValueError('工作状态操作身份不一致') - for role, pid in identities: - if pid and not any(p['pid'] == pid for p in state['processes']): - state['processes'].append({'role': role, **process_identity(pid)}) - state['phase'] = phase - if write_outcome is not None: - state['write_outcome'] = write_outcome - write_session_record(path, state) - return True - except Exception: - if required: - raise - # An unavailable journal cannot interrupt an already executing COM call. - logging.getLogger(__name__).warning('Worker session journal unavailable', exc_info=True) - return False + from .desktop_worker import worker_phase + with worker_phase(job['operation_id'], phase): + path = Path(job['session_record_path']).with_name('worker-state.json') + try: + state = ({'operation_id': job['operation_id'], 'processes': []} if not path.exists() + else read_session_record(path, evidence_root=path.parent)) + if state['operation_id'] != job['operation_id']: + raise ValueError('工作状态操作身份不一致') + for role, pid in identities: + if pid and not any(p['pid'] == pid for p in state['processes']): + state['processes'].append({'role': role, **process_identity(pid)}) + state['phase'] = phase + if write_outcome is not None: + state['write_outcome'] = write_outcome + write_session_record(path, state) + return True + except Exception: + if required: + raise + # An unavailable journal cannot interrupt an already executing COM call. + logging.getLogger(__name__).warning('Worker session journal unavailable', exc_info=True) + return False def process_identity(pid): @@ -135,11 +193,12 @@ def _session(path, root): def _result(status, detail='', *, session=None, state=None, proof=None): return {'status': status, 'detail': detail, 'library': (session or {}).get('library'), + 'block': (session or {}).get('block'), 'operation_id': (session or {}).get('operation_id'), 'phase': (state or {}).get('phase', 'UNCONFIRMED'), 'release_proof': proof} -def _reconcile(path, root, handles, stack): +def _reconcile(path, root, handles, stack, *, release=True): import pywintypes, win32api, win32con, win32event from .residual_sessions import _handle_identity, _force_ownership session, state, processes = _session(path, root) @@ -176,6 +235,8 @@ def _reconcile(path, root, handles, stack): active = True if active: return _result('ACTIVE', '原生调用尚未确认返回', session=session, state=state) + if not release: + return _result('UNCONFIRMED', '记录中的进程已退出,需确认库占用已释放', session=session, state=state) from .library_lock import release_retained_library try: proof = release_retained_library(session['library'], Path(path).parent/'release-proof', @@ -197,17 +258,28 @@ def reconcile_session_record(path, *, evidence_root): return _result('UNCONFIRMED', str(error)) -def observe_retained_session(path, *, evidence_root, stop=None, progress=None, process=None): +def inspect_session_record(path, *, evidence_root): + """Observe process identities without touching the production library.""" + with ExitStack() as stack: + try: + return _reconcile(Path(path), Path(evidence_root), {}, stack, release=False) + except Exception as error: + return _result('UNCONFIRMED', str(error)) + + +def observe_retained_session(path, *, evidence_root, stop=None, progress=None, process=None, + observe_only=False): stop = stop if stop is not None else threading.Event() observation = Path(path).with_name('observation-' + uuid4().hex + '.json') with ExitStack() as stack: handles = {} while True: try: - result = _reconcile(Path(path), Path(evidence_root), handles, stack) + result = _reconcile(Path(path), Path(evidence_root), handles, stack, release=not observe_only) except Exception as error: result = _result('UNCONFIRMED', str(error)) - if process is not None and process.poll() is not None and getattr(process, 'cbb_library', None): + if (not observe_only and process is not None and process.poll() is not None + and getattr(process, 'cbb_library', None)): try: from .library_lock import release_retained_library proof = release_retained_library(process.cbb_library, Path(path).parent/'release-proof') @@ -216,7 +288,7 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p session={'library': str(process.cbb_library)}, proof=proof) except Exception: pass - if result['status'] == 'IDENTITY_CHANGED': + if result['status'] == 'IDENTITY_CHANGED' and not observe_only: try: from .library_lock import release_retained_library proof = release_retained_library(result['library'], Path(path).parent/'release-proof') @@ -233,7 +305,8 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p progress(result) except Exception: logging.getLogger(__name__).warning('Observation callback failed', exc_info=True) - if result['status'] == 'RELEASED' or stop.is_set(): + if (result['status'] == 'RELEASED' or stop.is_set() + or observe_only and result['status'] != 'ACTIVE'): return result if not handles and process is not None and process.poll() is None: stop.wait(1) @@ -243,24 +316,38 @@ def observe_retained_session(path, *, evidence_root, stop=None, progress=None, p stop.wait(5 if result['status'] in ('UNCONFIRMED', 'IDENTITY_CHANGED') else 1) -def handoff_session(path, *, evidence_root, process=None, progress=None, stop=None): +def handoff_session(path, *, evidence_root, process=None, progress=None, stop=None, observe_only=False): """Retain exact handles in memory until the observer independently releases.""" key = str(Path(path).absolute()).casefold() try: session = read_session_record(path, evidence_root=evidence_root) library = session['library'] except (OSError, ValueError, KeyError): + session = {} library = getattr(process, 'cbb_library', None) aliases = {_library_key(library), _library_key(Path(library).resolve())} if library else set() with _observer_guard: if key in _observers: return _retained_libraries[key] = aliases + _retained_results[key] = {'status': 'UNCONFIRMED', 'phase': 'UNCONFIRMED', + 'library': str(library) if library else None, 'block': session.get('block'), + 'detail': '正在核对残留会话', 'operation_id': session.get('operation_id')} + def notify(value): + with _observer_guard: + if _retained_libraries.get(key) is aliases: + previous = _retained_results[key] + _retained_results[key] = {**previous, **value, + 'library': value.get('library') or previous['library'], + 'operation_id': value.get('operation_id') or previous['operation_id'], + 'block': value.get('block') or previous['block']} + if progress is not None: + progress(value) def observe(): result = None try: - result = observe_retained_session(path, evidence_root=evidence_root, progress=progress, - stop=stop, process=process) + result = observe_retained_session(path, evidence_root=evidence_root, progress=notify, + stop=stop, process=process, observe_only=observe_only) if process is not None and process.poll() is not None: process.close() return result @@ -269,6 +356,7 @@ def handoff_session(path, *, evidence_root, process=None, progress=None, stop=No _observers.pop(key, None) if result and result['status'] == 'RELEASED': _retained_libraries.pop(key, None) + _retained_results.pop(key, None) thread = threading.Thread(target=observe, name='retained-native-session', daemon=True) _observers[key] = (thread, process) thread.start() @@ -308,7 +396,9 @@ def discover_sessions(evidence_root, *, library=None): continue if library is not None and _library_key(value.get('library', '')) != _library_key(library): continue - value = {**value, 'path': str(path), **reconcile_session_record(path, evidence_root=root)} + observed = inspect_session_record(path, evidence_root=root) + value = {**value, **observed, 'library': value.get('library'), + 'block': value.get('block'), 'operation_id': value['operation_id'], 'path': str(path)} except (OSError, ValueError): continue found.append(value) diff --git a/src/cbb_refresh_verify/ui/main_window.py b/src/cbb_refresh_verify/ui/main_window.py index 5c5b686..c4bf6b3 100644 --- a/src/cbb_refresh_verify/ui/main_window.py +++ b/src/cbb_refresh_verify/ui/main_window.py @@ -457,6 +457,7 @@ class MainWindow(QWidget): for session in sessions: if session['status'] == 'ACTIVE': handoff_session(session['path'], evidence_root=root, + observe_only=True, progress=lambda value: notify({'stage': 'session_observation', **value})) return unfinished, sessions worker = ServiceWorker(inspect) diff --git a/src/cbb_refresh_verify/ui/residual_release_dialog.py b/src/cbb_refresh_verify/ui/residual_release_dialog.py index 039ce23..32daa96 100644 --- a/src/cbb_refresh_verify/ui/residual_release_dialog.py +++ b/src/cbb_refresh_verify/ui/residual_release_dialog.py @@ -30,6 +30,9 @@ class ResidualReleaseDialog(QDialog): self.message = QLabel('正在检查当前库关联的会话…') self.message.setWordWrap(True) layout.addWidget(self.message) + self.runtime_label = QLabel() + self.runtime_label.setWordWrap(True) + layout.addWidget(self.runtime_label) self.table = QTableWidget(0, 6) self.table.setHorizontalHeaderLabels(['工程', '程序', 'PID', '状态', '窗口位置', '程序路径']) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) @@ -155,6 +158,7 @@ class ResidualReleaseDialog(QDialog): or any(r.get('release_status') != 'RELEASED' for r in result['sessions'])): return False self.plan = None + self.runtime_label.clear() self.released.emit(proof) return True @@ -191,10 +195,19 @@ class ResidualReleaseDialog(QDialog): elif not plan['sessions']: text += '确认后将复查库占用状态。' self.message.setText(text) + states = {'ACTIVE': '进行中', 'UNCONFIRMED': '待核对', 'IDENTITY_CHANGED': '待核对'} + self.runtime_label.setText('\n'.join( + f'{states.get(row["status"], "待核对")} · {row.get("block") or "当前库"} · ' + f'{row.get("detail") or "操作状态需重新核对"}' for row in plan.get('runtime_sessions', []))) def inspect(): from cbb_refresh_verify.network_path import select_library_file + from cbb_refresh_verify.session_record import discover_sessions, retained_session_states library = self.library if self.library.suffix.casefold() == '.lmc' else select_library_file(self.library) - return inspect_residual_sessions(library) + plan = inspect_residual_sessions(library) + states = {row['path']: row for row in retained_session_states(library)} + states.update({str(Path(row['path']).absolute()).casefold(): row for row in + discover_sessions(self.evidence_dir.parent, library=library)}) + return {**plan, 'runtime_sessions': list(states.values())} self._start(inspect, ready) def force_release(self): diff --git a/tests/test_incident_safety.py b/tests/test_incident_safety.py index c25a967..3427641 100644 --- a/tests/test_incident_safety.py +++ b/tests/test_incident_safety.py @@ -57,7 +57,9 @@ def test_unsafe_result_stops_batch_even_when_reported_pass(): def test_cleanup_does_not_force_terminate_native_database_owner(monkeypatch): killed = [] - monkeypatch.setattr(adapter, '_terminate_pid', lambda pid, **k: killed.append(pid)) + import win32api + monkeypatch.setattr(adapter.subprocess, 'run', lambda *args, **kwargs: killed.append(args)) + monkeypatch.setattr(win32api, 'TerminateProcess', lambda *args: killed.append(args)) monkeypatch.setattr(adapter, '_process_details', lambda name: [(22, 'ExpeditionPCB.exe', 'pcb', 11)] if name == 'ExpeditionPCB.exe' else []) events = [] diff --git a/tests/test_integration.py b/tests/test_integration.py index 64d0ba9..8e68f8e 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -96,8 +96,8 @@ def test_cleanup_verify_processes_confirms_absent_worker_parent_and_lm(monkeypat from cbb_refresh_verify import mentor_adapter terminated = [] - monkeypatch.setattr(mentor_adapter, '_terminate_pid', - lambda pid, **_kwargs: terminated.append(int(pid))) + monkeypatch.setattr(mentor_adapter.subprocess, 'run', + lambda *args, **kwargs: terminated.append(args)) monkeypatch.setattr(mentor_adapter, '_process_details', lambda _name: ()) result = cleanup_verify_processes(101, 202, 303, emit=lambda **event: None) assert terminated == [] @@ -120,8 +120,8 @@ def test_cleanup_verify_processes_discovers_unbound_target_pcb_processes(monkeyp (999, r'C:\Mentor\wg\ExpeditionPCB.exe', command, 0), ], ) - monkeypatch.setattr(mentor_adapter, '_terminate_pid', - lambda pid, **_kwargs: terminated.append(int(pid))) + monkeypatch.setattr(mentor_adapter.subprocess, 'run', + lambda *args, **kwargs: terminated.append(args)) result = mentor_adapter.cleanup_verify_processes( 101, diff --git a/tests/test_management_supervision.py b/tests/test_management_supervision.py index f2a0c70..7afedcc 100644 --- a/tests/test_management_supervision.py +++ b/tests/test_management_supervision.py @@ -79,6 +79,8 @@ def test_real_management_worker_parent_loss_waits_for_native_boundary(tmp_path): 'from cbb_refresh_verify import native_management as n\n' 'n._current_desktop=lambda:(None,"CbbManagement_test")\n' 'def native_call(job,root):\n' + ' from cbb_refresh_verify.session_record import update_worker_state\n' + ' update_worker_state(job,"NATIVE_CALL")\n' f' Path({str(started)!r}).touch()\n' ' time.sleep(1.5)\n' ' try: n._require_management_continuation(job,root)\n' diff --git a/tests/test_residual_release_dialog.py b/tests/test_residual_release_dialog.py index d0301b6..839c57b 100644 --- a/tests/test_residual_release_dialog.py +++ b/tests/test_residual_release_dialog.py @@ -56,7 +56,8 @@ def test_confirm_releases_full_plan_and_only_emits_actual_library_proof(qtbot, t qtbot.waitUntil(lambda: not dialog.busy) qtbot.mouseClick(dialog.confirm_button, Qt.LeftButton) qtbot.waitUntil(lambda: not dialog.busy) - assert calls == [plan] and released == [proof] + assert len(calls) == 1 and calls[0]['sessions'] == plan['sessions'] + assert calls[0]['library'] == plan['library'] and released == [proof] assert '已释放 2' in dialog.message.text() assert not dialog.confirm_button.isEnabled() diff --git a/tests/test_review_319.py b/tests/test_review_319.py new file mode 100644 index 0000000..889d6ab --- /dev/null +++ b/tests/test_review_319.py @@ -0,0 +1,206 @@ +"""Review 319 regressions; native databases are replaced by isolated files.""" +from contextlib import contextmanager +from pathlib import Path +from threading import Event +import json +import subprocess +import sys +import time + +import pytest + +from cbb_refresh_verify import session_record as sessions +from test_session_record import child, record + + +def retain_unconfirmed(root, library): + root.mkdir(parents=True) + path = root/'session.json' + sessions.write_session_record(path, dict(operation_id=root.name, library=str(library), processes=[])) + done = Event() + sessions.handoff_session(path, evidence_root=root, progress=lambda _: done.set()) + assert done.wait(3) + with sessions._observer_guard: + observer = sessions._observers.get(str(path.absolute()).casefold()) + if observer: + observer[0].join(3) + assert not observer[0].is_alive() + assert not sessions.library_available(library) + return path + + +def test_explicit_proof_resumes_queue_after_unconfirmed_observer_ended(tmp_path, monkeypatch): + from cbb_refresh_verify import library_lock as locks + from cbb_refresh_verify.operation_queue import NativeQueue + monkeypatch.setattr(sessions, '_retained_libraries', {}) + library = tmp_path/'library/lib.lmc' + library.parent.mkdir(); library.touch() + other = tmp_path/'other.lmc' + retain_unconfirmed(tmp_path/'old', library) + retain_unconfirmed(tmp_path/'independent', other) + @contextmanager + def proof(*args, **kwargs): + yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} + monkeypatch.setattr(locks, '_prove_library_released', proof) + result = locks.release_retained_library(library, tmp_path/'proof') + assert result['status'] == 'RELEASE_CONFIRMED' + assert sessions.library_available(library), 'successful explicit proof left the queue blocked' + assert sessions.discover_sessions(tmp_path/'old') == [], 'released history reappeared as unconfirmed' + assert not sessions.library_available(other) + assert NativeQueue().run(lambda: 'resumed', ready=lambda: sessions.library_available(library)) == 'resumed' + + +def test_failed_proof_and_newer_handoff_cannot_clear_queue_retention(tmp_path, monkeypatch): + from cbb_refresh_verify import library_lock as locks + monkeypatch.setattr(sessions, '_retained_libraries', {}) + library = tmp_path/'library/lib.lmc' + library.parent.mkdir(); library.touch() + old = retain_unconfirmed(tmp_path/'old', library) + def denied(*args, **kwargs): + raise OSError('external owner remains') + monkeypatch.setattr(locks, '_prove_library_released', denied) + with pytest.raises(OSError, match='external owner'): + locks.release_retained_library(library, tmp_path/'proof') + assert not sessions.library_available(library) + @contextmanager + def proof(*args, **kwargs): + retain_unconfirmed(tmp_path/'new', library) + yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} + monkeypatch.setattr(locks, '_prove_library_released', proof) + locks.release_retained_library(library, tmp_path/'proof') + assert not sessions.library_available(library) + assert str(old.absolute()).casefold() not in sessions._retained_libraries + + +def test_discovery_of_exited_session_never_reconciles_library(tmp_path, child, monkeypatch): + path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))]) + child.terminate(); child.wait(5) + def forbidden(*args, **kwargs): + pytest.fail('passive history scan attempted library release') + monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library', forbidden) + found = sessions.discover_sessions(tmp_path) + assert len(found) == 1 and found[0]['path'] == str(path) + assert found[0]['status'] == 'UNCONFIRMED' + assert found[0]['release_proof'] is None + + +@pytest.mark.parametrize('phase', ['PRECALL', 'POSTCALL']) +@pytest.mark.parametrize('broken_evidence', [False, True]) +def test_parent_loss_exits_idle_worker_even_when_stop_evidence_fails(tmp_path, phase, broken_evidence): + run_watchdog_case(tmp_path, phase, broken_evidence=broken_evidence, exits=True) + + +@pytest.mark.parametrize('phase', ['NATIVE_CALL', 'CLOSING', 'UNCONFIRMED']) +def test_parent_loss_preserves_pending_or_unknown_worker(tmp_path, phase): + run_watchdog_case(tmp_path, phase, exits=False) + + +def test_stale_postcall_journal_cannot_override_in_memory_active_call(tmp_path): + run_watchdog_case(tmp_path, 'NATIVE_CALL', stale=True, exits=False) + + +def test_missing_journal_preserves_worker_until_state_can_be_confirmed(tmp_path): + run_watchdog_case(tmp_path, 'POSTCALL', missing=True, exits=False) + + +def run_watchdog_case(root, phase, *, exits, broken_evidence=False, stale=False, missing=False): + supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)']) + script, ready, returned = root/'worker.py', root/'ready', root/'returned' + cancel = root/'missing/cancel' if broken_evidence else root/'cancel' + script.write_text( + 'import time\nfrom pathlib import Path\n' + 'from cbb_refresh_verify import session_record as s, desktop_worker as d\n' + f'job=dict(operation_id="watchdog-test", session_record_path={str(root/"session.json")!r})\n' + f's.update_worker_state(job, {"POSTCALL" if stale else phase!r})\n' + 'original=s.write_session_record\n' + 'def write(path,value):\n' + f' if {broken_evidence!r} and Path(path).name=="supervisor-lost.json": raise OSError("disk full")\n' + f' if {stale!r} and Path(path).name=="worker-state.json": raise OSError("stale journal")\n' + ' return original(path,value)\n' + 's.write_session_record=write\n' + + (f's.update_worker_state(job,{phase!r})\n' if stale else '') + + (f'Path({str(root/"worker-state.json")!r}).unlink()\n' if missing else '') + + f'd.start_supervisor_watchdog({supervisor.pid},cancel_path={str(cancel)!r},' + 'session_record_path=job["session_record_path"],operation_id=job["operation_id"])\n' + f'Path({str(ready)!r}).touch()\n' + f'while not Path({str(returned)!r}).exists(): time.sleep(.02)\n' + 's.write_session_record=original\n' + 's.update_worker_state(job,"POSTCALL")\n' + 'time.sleep(30)\n', encoding='utf-8') + log = (root/'worker.log').open('wb') + worker = subprocess.Popen([sys.executable, str(script)], stdout=log, stderr=log) + try: + deadline = time.monotonic()+6 + while not ready.exists() and worker.poll() is None and time.monotonic()