diff --git a/docs/testing/2026-10-23-issue-3-frozen-startup-recovery.md b/docs/testing/2026-10-23-issue-3-frozen-startup-recovery.md new file mode 100644 index 0000000..d04e74a --- /dev/null +++ b/docs/testing/2026-10-23-issue-3-frozen-startup-recovery.md @@ -0,0 +1,32 @@ +# Issue 3:冻结监督进程中的首次 CAD 启动失败恢复 + +2026-10-23,运行 `tools/probe_frozen_startup_recovery.py --failures 1`,实机退出码 0。使用独立全新 `Audio_D2128` 副本,与正常 UI 验收及 marker 写保护验收目录隔离。 + +本轮冻结 EXE SHA256 为 `4e251627d1ed6f22786de6febec971f889b6b1f2f69c86f19ccdeebd0d41ed95`。该版本首次启动故障等待约 120 秒的行为如实保留;后续检测 CAD 提前退出的源码/EXE 优化不得套用本轮耗时。 + +本轮是**源码非 GUI parent + 真实冻结 worker/helper/VBS**。parent 创建 `TaskCenterService(supervised=True)`,测试中仅替换 `batch_supervisor._command`,使 BOM 与队列请求均启动该冻结 EXE 的 `--batch-worker` 模式。不修改生产源码、生产 VBS 或增加注入开关。完整图读取为独立源码只读 inspector,在独立私有 CAD 中运行。该混合入口不等同于完整 GUI 故障注入。 + +真实证据目录为 `build/issue-3-frozen-startup-recovery/run-b266b219d8374db3b926d8a24a7dcc2f`。 + +故障线程仅监视本轮 `execute_task_queue` 的 session/私有 CAD 记录。终止前交叉核对 batch/session/generation、桌面名、记录中的 CAD PID、进程创建时间、`state=starting`、无 GUID、无 bootstrap 绑定、无 cad-ready checkpoint,以及对应 `prepared` attempt。所有条件满足后,只终止该明确自有 CAD;不附着外部 COM,不终止未知或外部进程。 + +| 检查 | 真实结果 | +| --- | --- | +| 第一次自有 CAD | PID 40836,创建时间 `2026-10-23 08:32:26.507000+00:00`;确认尚未授权工程 worker 后终止 | +| 第一次 attempt | `prepared` 保持不变;SHA256 `79c88c6b81d3488952aa154e50ef878cca1d68462cadb91e8ceb0bad5ea08737` 未变化 | +| 第一次桌面 | `dx-3e9ef3b9fd75476d9e7b8825bc6172be` 释放确认成功 | +| 安全换代顺序 | 第一代 `released_at` 早于第二代桌面 handle-create `time_ns`;观察第二代时桌面枚举中旧桌面已经消失 | +| 第二代 CAD | PID 33116,创建时间 `2026-10-23 08:34:27.099000+00:00`,新桌面 `dx-89dbf9636ccd461db04a95ef1df03dfa` | +| 重试预算 | execution session generation=2,项目 retries 只有一条且 `count=1` | +| 正式业务结果 | 一次 `saved_ok`,任务 completed;没有重复完成或再次替换 | +| 独立图对照 | 57 器件、103 引脚;56 个非目标器件完全一致;RV05 原 UID `$2I2014`、位置/方向/缩放与连接保持不变 | +| 目标结果 | 料号 `4100-CA0000-T0`,Part Label `0R`,保留 native target 的 Value 属性不存在状态 | +| 清理 | session `released`;所有自有 CAD 退出、桌面消失;无 pending resource/supervisor | +| 用户桌面 | 输入桌面未变化;自有 CAD 未成为前台或出现在输入桌面 | +| 原始输入 | 原始工程目录与中央库 LMC 哈希保持不变 | + +本轮总耗时 349.859 秒,包含独立前后图读取。第一代 `project_worker` 耗时 120.187 秒,记录的重试原因为 `private CAD bootstrap timed out`;第二代 CAD 启动 18.188 秒,第二代 `project_worker` 89.750 秒。未降低生产超时,也未把等待失败降为成功。 + +主要证据:`report.json`(`verified=true`)、`fault-observations.json`、`before.json`、`after.json`、`desktop-observation.json`、两代私有 CAD 记录,以及 `data/batch-resources/*/{request.json,supervisor.json,session.json,result.json,business-result.json}`。 + +本轮只验证首次 prewrite 启动失败后安全恢复一次。连续两次启动失败、优化后提前退出检测的完整冻结重测,以及完整 GUI 故障注入不在本轮覆盖范围;postwrite 监督进程崩溃使用另外的新副本单独验收。 diff --git a/tools/probe_frozen_startup_recovery.py b/tools/probe_frozen_startup_recovery.py new file mode 100644 index 0000000..4f74b73 --- /dev/null +++ b/tools/probe_frozen_startup_recovery.py @@ -0,0 +1,271 @@ +"""Hybrid source parent / frozen supervisor startup-failure CAD acceptance. + +Prepare without CAD with --help. Run only after obtaining the shared CAD test +window. Production source/VBS are unchanged; parent _command alone is replaced. +""" +from __future__ import annotations + +import argparse +from dataclasses import asdict +import hashlib +import json +from pathlib import Path +import re +import shutil +import subprocess +import sys +from threading import Event, Thread +import time +import traceback +from uuid import uuid4 + +REPO = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(REPO / 'src')) +sys.path.insert(0, str(REPO / 'tools')) +from probe_batch_runtime_live import hashes, compare_saved, save + + +def read_json(path): + try: + return json.loads(path.read_text(encoding='utf-8')) + except (OSError, ValueError): + return None + + +class StartupFault: + """Stop only certified own viewdraw PIDs while their session is starting.""" + def __init__(self, root, output, failures, cancel): + self.root, self.output, self.failures, self.cancel = root, output, failures, cancel + self.stop = Event() + self.faults, self.observations, self.errors = [], [], [] + self.seen = set() + + def scan(self): + import win32api + import win32event + import win32process + from dx_productivity.trial_task_center.isolated_desktop import _Win32Api + from dx_productivity.trial_task_center.path_access import internal_path + for request in (self.root / 'batch-resources').glob('*/request.json'): + value = read_json(request) + if not value or value['operation'] != 'execute_task_queue': + continue + directory = request.parent + session = read_json(directory / 'session.json') + journal = read_json(directory / 'supervisor.json') + if not session or not journal or not journal.get('process'): + continue + assert session['session_id'] == directory.name == value['batch_id'] + for record_path in directory.glob('generation-*/private-desktop.json'): + record = read_json(record_path) + if not record or not record.get('cad_pid'): + continue + pid = record['cad_pid'] + key = (directory.name, record['token']) + if key in self.seen: + continue + generation = int(record_path.parent.name.removeprefix('generation-')) + if generation != session['generation'] or record['desktop'] != session['desktop']: + continue + identities = [item for item in session['processes'] if item['pid'] == pid] + if not identities: + continue + assert len(identities) == 1 and identities[0]['creation_time'] + identity = identities[0] + handle = win32api.OpenProcess(0x1000 | 0x100000 | 1, False, pid) + try: + actual_created = str(win32process.GetProcessTimes(handle)['CreationTime']) + assert actual_created == identity['creation_time'], 'PID creation time mismatch' + listed = _Win32Api().service.GetProcessWindowStation().EnumDesktops() + observation = {'generation': generation, 'pid': pid, + 'creation_time': actual_created, 'desktop': record['desktop'], + 'listed_desktops': [name for name in listed if name.startswith('dx-')], + 'state': record['state'], 'time_ns': time.time_ns()} + self.observations.append(observation) + if generation <= self.failures: + # Recheck the same record immediately before the one + # authorized termination. No COM attachment is needed. + current = read_json(record_path) + live_session = read_json(directory / 'session.json') + assert current and live_session + assert current['state'] == 'starting' and 'guid' not in current + assert current['cad_pid'] == pid and current['token'] == record['token'] + assert current['desktop'] == live_session['desktop'] + assert live_session['generation'] == generation + assert not list(record_path.parent.glob('cad-binding-*.json')) + assert not list(directory.parent.parent.rglob('cad-ready-*.json')) + prepared = list(directory.glob(f'attempts/g{generation}/**/*.json')) + assert len(prepared) == 1 + marker = read_json(prepared[0]) + assert marker and marker['state'] == 'prepared' + # The initial record is written immediately after + # launch, before bootstrap/project worker authorization. + assert win32event.WaitForSingleObject(handle, 0) == 258 + win32process.TerminateProcess(handle, 71) + assert win32event.WaitForSingleObject(handle, 5000) == 0 + self.faults.append({**observation, 'terminated_owned_startup_only': True, + 'marker': str(prepared[0]), 'marker_before': marker, + 'marker_sha256_before': hashlib.sha256(prepared[0].read_bytes()).hexdigest()}) + self.seen.add(key) + save(self.output / 'fault-observations.json', {'faults': self.faults, + 'observations': self.observations, 'errors': self.errors}) + finally: + handle.Close() + + def _watch(self): + while not self.stop.wait(.02): + try: + self.scan() + except Exception as error: + self.errors.append(repr(error)) + self.cancel.set() + save(self.output / 'fault-observations.json', {'faults': self.faults, + 'observations': self.observations, 'errors': self.errors}) + return + + def __enter__(self): + self.thread = Thread(target=self._watch, daemon=True) + self.thread.start() + return self + + def __exit__(self, *_): + self.stop.set() + self.thread.join(5) + assert not self.thread.is_alive() + + +def run(source, library, executable, base, failures): + from dx_productivity.trial_task_center import batch_supervisor + from dx_productivity.trial_task_center.batch_resources import pending_resources + from dx_productivity.trial_task_center.bom_plan import write_bom_template + from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession + from dx_productivity.trial_task_center.logging_setup import configure_logging + from dx_productivity.trial_task_center.service import TaskCenterService + from dx_productivity.trial_task_center.store import TaskStore + from dx_productivity.trial_task_center.task_queue import execute_task_queue + from private_desktop_observer import PrivateDesktopObserver + + source, library, executable = [path.resolve(strict=True) for path in (source, library, executable)] + assert executable.name == 'DxPartReplace.exe' + output = base.resolve() / ('run-' + uuid4().hex) + assert output.is_relative_to(REPO / 'build' / 'issue-3-frozen-startup-recovery') + output.mkdir(parents=True, exist_ok=False) + source_hashes, library_hash = hashes(source), hashlib.sha256(library.read_bytes()).hexdigest() + folder = output / 'projects' / source.name + shutil.copytree(source, folder) + project = folder / (source.name + '.prj') + text, count = re.subn(r'(?m)^KEY CentralLibrary .*$', lambda _: f'KEY CentralLibrary "{library.as_posix()}"', + project.read_text(encoding='utf-8-sig')) + assert count == 1 + project.write_text(text, encoding='utf-8') + report = {'mode': 'source_supervised_parent_frozen_worker', 'gui_parent_covered': False, + 'frozen_executable': str(executable), 'exe_sha256': hashlib.sha256(executable.read_bytes()).hexdigest(), + 'project': str(project), 'requested_startup_failures': failures, 'launches': []} + original_command = batch_supervisor._command + def frozen_command(request): + command = (str(executable), '--batch-worker', str(request)) + report['launches'].append({'command': command, 'request': read_json(request)}) + save(output / 'report.json', report) + return command + batch_supervisor._command = frozen_command + store = TaskStore(output / 'tasks.db') + service = TaskCenterService(store, configure_logging(output / 'data'), supervised=True) + def independent(label): + evidence = output / 'independent-read' / 'quick-run-history' / ('sheet-discovery-' + label) + evidence.mkdir(parents=True) + destination, spec = output / (label + '.json'), evidence / 'spec.json' + save(spec, {'project': str(project), 'destination': str(destination)}) + with IsolatedCadSession(evidence) as cad: + process = cad.start_worker((sys.executable, str(REPO / 'tools/probe_marker_write_failure_live.py'), + '--inspect-file', str(spec)), stdout=subprocess.PIPE, stderr=subprocess.PIPE) + try: + code = process.wait(timeout=240) + (evidence / 'worker-output.txt').write_bytes(process.stdout.read() + b'\n' + process.stderr.read()) + assert code == 0, (evidence / 'worker-output.txt').read_text(errors='replace') + finally: + process.close() + return read_json(destination) + started = time.monotonic() + try: + with PrivateDesktopObserver(output): + print('Independent BEFORE (source read-only inspector)', flush=True) + before = independent('before') + tasks = service.create_project_tasks(output / 'projects', selected_paths=(project,)) + assert len(tasks) == 1 + bom = write_bom_template(output / 'input.xlsx', ({'project_path': str(project), + 'old_part_number': '4100-CA0000-T7', 'new_part_number': '4100-CA0000-T0', + 'refdes': 'RV05', 'enabled': True, 'sync_nc': True},)) + print('BOM import in frozen supervisor', flush=True) + imported = service.inspect_bom_batch(tasks[0].task_id, bom, + on_progress=lambda message: print(message, flush=True)) + assert imported.passed_rows == 1 and imported.preview is not None + service.save_bom_batch(imported.preview, {item.task_id: item.rows for item in imported.preview.projects}) + cancel = Event() + print('Frozen queue with owned startup CAD failure', flush=True) + with StartupFault(output / 'data', output, failures, cancel) as injector: + result = execute_task_queue(service, (tasks[0].task_id,), stop_event=cancel) + report['queue_result'] = asdict(result) + report['faults'], report['fault_errors'] = injector.faults, injector.errors + assert not injector.errors and len(injector.faults) == failures + execution = [item for item in report['launches'] if item['request']['operation'] == 'execute_task_queue'] + assert len(execution) == 1 + request = Path(execution[0]['command'][-1]) + session = read_json(request.parent / 'session.json') + report['execution_session'] = session + assert session['state'] == 'released' and session['generation'] == 2 + assert len(session['retries']) == 1 and next(iter(session['retries'].values()))['count'] == 1 + for fault in injector.faults: + path = Path(fault['marker']) + assert read_json(path)['state'] == 'prepared' + assert hashlib.sha256(path.read_bytes()).hexdigest() == fault['marker_sha256_before'] + assert not pending_resources(output / 'data') + assert not batch_supervisor.pending_supervisors(output / 'data') + first_release, last_release = session['generation_releases'][0], session['generation_releases'][-1] + assert first_release['cleanup']['desktop_confirmation']['released'] + second_create = last_release['cleanup']['desktop_handle_history'][0]['time_ns'] + assert first_release['released_at'] * 1e9 <= second_create + report['old_desktop_released_before_second_create'] = True + status_files = {str(path.relative_to(output)): path.read_text(encoding='utf-8-sig') + for path in (output / 'data').rglob('status.tsv')} + report['status_files'] = status_files + saved_count = sum(sum(line.split('\t')[1:2] == ['saved_ok'] for line in text.splitlines()) + for text in status_files.values()) + assert saved_count == (1 if failures == 1 else 0) + assert all(entry.state == ('completed' if failures == 1 else 'blocked') for entry in result.entries) + print('Independent AFTER (source read-only inspector)', flush=True) + after = independent('after') + report['graph_comparison'] = compare_saved(before, after) if failures == 1 else {'identical': before == after} + if failures == 2: + assert before == after + observation = read_json(output / 'desktop-observation.json') + assert not observation['observer_errors'] + assert observation['input_desktop_before'] == observation['input_desktop_after'] + assert not observation['owned_cad_became_foreground'] and not observation['owned_cad_on_input_desktop'] + assert all(observation['cad_exit_confirmed'].values()) and all(observation['desktop_absent'].values()) + report['verified'] = True + except BaseException as error: + report.update(verified=False, error=repr(error), traceback=traceback.format_exc()) + traceback.print_exc() + finally: + batch_supervisor._command = original_command + store.close() + from dx_productivity.trial_task_center.legacy_paths import cleanup_worker_aliases + report['alias_cleanup_errors'] = list(map(str, cleanup_worker_aliases())) + report['source_unchanged'] = hashes(source) == source_hashes + report['library_unchanged'] = hashlib.sha256(library.read_bytes()).hexdigest() == library_hash + report['elapsed_seconds'] = time.monotonic() - started + save(output / 'report.json', report) + print(json.dumps({key: report.get(key) for key in ('verified', 'error', 'elapsed_seconds')}, ensure_ascii=False), flush=True) + return 0 if (report.get('verified') and report['source_unchanged'] and report['library_unchanged'] + and not report['alias_cleanup_errors']) else 1 + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--source', type=Path, required=True) + parser.add_argument('--library', type=Path, required=True) + parser.add_argument('--executable', type=Path, default=REPO / 'dist/DxPartReplace/DxPartReplace.exe') + parser.add_argument('--output', type=Path, default=REPO / 'build/issue-3-frozen-startup-recovery') + parser.add_argument('--failures', type=int, choices=(1, 2), default=1) + args = parser.parse_args() + raise SystemExit(run(args.source, args.library, args.executable, args.output, args.failures))