From e5e628de4ab681a0ba6e7c613af171fa360ec963 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: Sat, 31 Oct 2026 10:24:56 +0800 Subject: [PATCH] fix: persist active task before worker launch --- ...026-10-30-phase1-lifecycle-optimization.md | 2 +- .../2026-10-31-pr9-latest-review-fix.md | 34 +++++++++++++++++++ .../dxdesigner795/NativeSnapshotReader.vbs | 5 ++- .../trial_task_center/batch_runtime.py | 4 +++ .../trial_task_center/batch_supervisor.py | 19 ++++++++--- .../trial_task_center/project_session.py | 11 +++++- .../test_quick_part_replace_contract.py | 2 ++ tests/v1_1/test_batch_runtime.py | 22 ++++++++++++ 8 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 docs/testing/2026-10-31-pr9-latest-review-fix.md diff --git a/docs/testing/2026-10-30-phase1-lifecycle-optimization.md b/docs/testing/2026-10-30-phase1-lifecycle-optimization.md index 42fb27b..5e8f26e 100644 --- a/docs/testing/2026-10-30-phase1-lifecycle-optimization.md +++ b/docs/testing/2026-10-30-phase1-lifecycle-optimization.md @@ -1,6 +1,6 @@ # Phase 1:生命周期优化验证 -日期:2026-10-30(北京时间) +日期:2026-10-30(北京时间) 分支:`codex/project-optimization` ## 本轮修复 diff --git a/docs/testing/2026-10-31-pr9-latest-review-fix.md b/docs/testing/2026-10-31-pr9-latest-review-fix.md new file mode 100644 index 0000000..806872d --- /dev/null +++ b/docs/testing/2026-10-31-pr9-latest-review-fix.md @@ -0,0 +1,34 @@ +# PR #9 最新复审修复(评论 95) + +审查基线:`5ed8444`。本轮新增 1 项 P1 和 3 项 P2,另有编码与诊断建议。 + +## P1:当前任务保护证据先落盘 + +此前 `_prepare_attempts()` 只更新内存中的 `active_task`/`active_attempt`,磁盘 `session.json` 仍可能记录上一工程。新任务已经进入 CAD/worker 写入后崩溃时,监督器会读取陈旧任务并把 `uncertain.flag` 写到已完成工程。 + +修复:`run_worker()` 在 `_prepare_attempts()` 和私有桌面准备完成后、启动 CAD worker 前立即原子保存当前 `active_task` 和 `active_attempt`。新增回归模拟连续两个工程,第二个 worker 启动时读取磁盘 session,确认任务与 attempt 已切换。 + +## P2:生成式 VBS 脚本依赖闭合 + +`QPBuildNativeReadScript()` 的内嵌清单现在包含 `QPComponentIdentityKey`、`QPLog` 和 `QPAppendAscii`。原始日志函数随脚本内嵌,避免 fallback 身份首次出现时依赖末尾临时 stub 的隐式行为。原生读取脚本和契约测试重新通过。 + +## P2:工程会话关闭可观察且可强制结束 + +交互关闭等待现在每 15 秒记录 `project_session_awaiting_close`;如果绑定的 `StopControl.force_event` 被置位,会设置 worker abort 并退出等待,交由上层 Job 收尾。普通关闭仍等待已授权的安全边界,不自动强杀 CAD。 + +## 相关收口 + +- supervisor 接收 stdout/stderr 对 UTF-8 失败时按 Windows `mbcs` 回退,保留中文 DxDesigner/VBS 诊断内容。 +- Python 超时诊断继续统一使用 `_component_uid()`。 +- UID 非空时 VBS 身份函数提前返回并 trim,fallback 才读取 Refdes;fallback 日志按位号去重。 +- 进度写入失败日志按路径节流。 + +## 验证 + +- 生命周期、停止、监督结果、身份、VBS guards、无 UID库存:**152 passed**。 +- 原生读取、VBS 契约、项目会话和当前任务落盘回归:**34 passed**。 +- `python -m compileall -q src` 通过。 +- GBK 输出回退断言通过:`'中文'.encode('gbk')` 正确还原。 +- 冻结 EXE smoke 通过;最新源码与包内 VBS SHA256 一致。 + +仍需 Windows 10、真实 DxDesigner 7.9.5 无 UID 工程、中央库/许可证/COM 注册、网络共享和企业策略现场验收。 diff --git a/integration/dxdesigner795/NativeSnapshotReader.vbs b/integration/dxdesigner795/NativeSnapshotReader.vbs index 5f76571..ec6b0ee 100644 --- a/integration/dxdesigner795/NativeSnapshotReader.vbs +++ b/integration/dxdesigner795/NativeSnapshotReader.vbs @@ -281,7 +281,7 @@ Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token, "If QP_NR_Code<>0 Then QP_NR_Text=""ERROR"" & vbTab & " & QPNativeExpression(Token) & _ " & vbTab & CStr(QP_NR_Code) & vbTab & QPEncode(QP_NR_Source) & vbTab & QPEncode(QP_NR_Description) & vbCrLf" & vbCrLf & _ "QPWriteUtf8 " & QPNativeExpression(ResultPath) & ",QP_NR_Text" & vbCrLf & "On Error GoTo 0" & vbCrLf - Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPComponentIdentityKey QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ") + Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPComponentIdentityKey QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines QPLog QPAppendAscii QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ") Pattern.Global = False For Each Name In Names Pattern.Pattern = "^(Function|Sub) " & Name & "\([^\n]*\n[\s\S]*?^End \1[ \t]*" @@ -289,8 +289,7 @@ Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token, If Matches.Count <> 1 Then QPNativeReadInvalid "missing native read routine: " & Name Body = Body & vbCrLf & Matches(0).Value & vbCrLf Next - Body = Body & vbCrLf & "Sub QPLog(ByVal Root,ByVal Level,ByVal Message)" & vbCrLf & "End Sub" & vbCrLf & _ - "Sub QPProgress(ByVal Stage,ByVal Done,ByVal Total)" & vbCrLf & _ + Body = Body & vbCrLf & "Sub QPProgress(ByVal Stage,ByVal Done,ByVal Total)" & vbCrLf & _ " Dim StopPath,Elapsed,CurrentTime,ProgressPath,Stream,SafeFlag" & vbCrLf & _ " If QP_NR_Safe And Len(QP_AttemptToken)>0 Then" & vbCrLf & _ " StopPath=QP_Fso.BuildPath(QP_Root,""monitor-"" & QP_AttemptToken & "".stop"")" & vbCrLf & _ diff --git a/src/dx_productivity/trial_task_center/batch_runtime.py b/src/dx_productivity/trial_task_center/batch_runtime.py index 2424a2a..4a25f0c 100644 --- a/src/dx_productivity/trial_task_center/batch_runtime.py +++ b/src/dx_productivity/trial_task_center/batch_runtime.py @@ -279,6 +279,10 @@ class BatchCadRuntime: project, directory, expected = self._prepare_attempts(root, command, attempt_id, generation) self._active_attempts = list(expected) self._ensure_desktop(abort_event) + # Make the current task/attempt durable before any worker or CAD + # mutation can begin. A crash in the following window must protect + # this task rather than a stale completed task from session.json. + self._save(active_task=self.record['active_task'], active_attempt=attempt_id) before = project_fingerprint(project, self.root) status = internal_path(root / 'status.tsv') status_before = status.read_bytes() if status.exists() else None diff --git a/src/dx_productivity/trial_task_center/batch_supervisor.py b/src/dx_productivity/trial_task_center/batch_supervisor.py index 6bd12c7..002e0dd 100644 --- a/src/dx_productivity/trial_task_center/batch_supervisor.py +++ b/src/dx_productivity/trial_task_center/batch_supervisor.py @@ -24,6 +24,15 @@ _LIVE_SUPERVISORS = {} _WRITE_OPERATIONS = frozenset({'execute_task_queue', 'execute_quick_run_queue', 'execute_quick_run', 'recover_quick_run'}) +def _decode_process_output(value): + if not isinstance(value, bytes): + return str(value) + try: + return value.decode('utf-8') + except UnicodeDecodeError: + return value.decode('mbcs' if os.name == 'nt' else 'utf-8', errors='replace') + + def _mark_interrupted(root, journal, reason): from .batch_resources import write_record, _valid_record from .stop_control import protect_interruption @@ -343,11 +352,13 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres try: while line := process.stdout.readline(1024 * 1024): if isinstance(line, bytes): - stdout_tail.extend(line.decode('utf-8', errors='replace')) + decoded = _decode_process_output(line) + stdout_tail.extend(decoded) else: - stdout_tail.extend(str(line)) + decoded = str(line) + stdout_tail.extend(decoded) try: - messages.put(json.loads(line)) + messages.put(json.loads(decoded)) except (ValueError, UnicodeError): messages.put({'type': 'invalid'}) finally: @@ -360,7 +371,7 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres if not line: return if isinstance(line, bytes): - stderr_tail.extend(line.decode('utf-8', errors='replace')) + stderr_tail.extend(_decode_process_output(line)) else: stderr_tail.extend(str(line)) try: diff --git a/src/dx_productivity/trial_task_center/project_session.py b/src/dx_productivity/trial_task_center/project_session.py index 879d7d6..c8358d1 100644 --- a/src/dx_productivity/trial_task_center/project_session.py +++ b/src/dx_productivity/trial_task_center/project_session.py @@ -116,8 +116,17 @@ class ProjectSessionWorker: if interactive: # The monitor owns the safe/unsafe distinction. Do not # turn a GUI close or ordinary pause into a timed kill. + force_event = getattr(getattr(self.runtime, 'stop_event', None), 'force_event', None) + last_notice = time.monotonic() while not self._done.wait(.1): - pass + if force_event is not None and force_event.is_set(): + self._abort.set() + break + if time.monotonic() - last_notice >= 15: + import logging + logging.getLogger(__name__).warning( + 'project_session_awaiting_close token=%s', self.token) + last_notice = time.monotonic() close_grace = 60 if self.private_desktop else min(30, max(10, self._timeout)) if not self._done.wait(close_grace): self._abort.set() diff --git a/tests/integration/test_quick_part_replace_contract.py b/tests/integration/test_quick_part_replace_contract.py index 408b2ae..e2a9e4e 100644 --- a/tests/integration/test_quick_part_replace_contract.py +++ b/tests/integration/test_quick_part_replace_contract.py @@ -47,6 +47,8 @@ def test_quick_part_replace_is_an_ascii_current_sheet_automatic_contract() -> No 'AddAttribute("Value=" & LabelValue', "QPNativeReplace Command, SnapshotPath", '" --native-helper --snapshot "', + 'QPLog QP_Root', + 'QPAppendAscii', ): assert required in text diff --git a/tests/v1_1/test_batch_runtime.py b/tests/v1_1/test_batch_runtime.py index 85cb6cf..ca1fac0 100644 --- a/tests/v1_1/test_batch_runtime.py +++ b/tests/v1_1/test_batch_runtime.py @@ -183,6 +183,28 @@ def test_completed_project_does_not_hold_resolution_wait_for_next_project_failur assert not (Path(command[3]) / 'quick-run' / 'uncertain.flag').exists() +def test_current_task_and_attempt_are_durable_before_worker_launch(tmp_path, monkeypatch): + import subprocess + one, _ = command_for(tmp_path) + two, _ = command_for(tmp_path, 'next') + next_root = Path(two[3]) + command_path = next_root / 'command.tsv' + command_path.write_text(command_path.read_text().replace('task\tjob\t', 'task2\tjob2\t'), encoding='utf-8') + calls = [] + def monitor(command, *args, **kwargs): + calls.append(command) + record = json.loads(runtime.path.read_text(encoding='utf-8')) + expected_task = 'task' if len(calls) == 1 else 'task2' + assert record.get('active_task') == expected_task + assert record.get('active_attempt') + return subprocess.CompletedProcess(command, 0, '', '') + runtime = worker_runtime(tmp_path, monkeypatch, monitor) + two = (*two[:-1], 'job2') + with runtime: + runtime.run_worker(one, 5) + runtime.run_worker(two, 5) + + @pytest.mark.parametrize('second_fails', [False, True]) def test_prewrite_failure_releases_old_desktop_and_retries_only_once(tmp_path, monkeypatch, second_fails): import subprocess