From 131b6dbbb31b654bc9e8e28fd424988323ab6e4f 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: Fri, 30 Oct 2026 16:33:10 +0800 Subject: [PATCH] fix: harden supervised replacement and missing uid handling --- .../plans/2026-10-30-reliable-supervision.md | 75 +++++++ .../2026-10-30-reliable-supervision-design.md | 30 +++ .../2026-10-30-environment-compatibility.md | 82 +++++++ .../2026-10-30-field-collection-policy.md | 21 ++ .../2026-10-30-final-robustness-regression.md | 43 ++++ docs/testing/2026-10-30-missing-uid.md | 30 +++ .../2026-10-30-reliable-supervision.md | 57 +++++ .../2026-10-30-environment-compatibility.json | 138 ++++++++++++ docs/user/safe-stop-and-recovery.md | 18 ++ .../dxdesigner795/NativeSnapshotReader.vbs | 2 +- .../dxdesigner795/QuickPartReplace.vbs | 203 +++++++++++++++--- .../trial_task_center/batch_supervisor.py | 123 +++++++++-- .../trial_task_center/batch_worker.py | 22 +- .../trial_task_center/diagnostics_bundle.py | 3 +- .../trial_task_center/isolated_desktop.py | 13 ++ .../trial_task_center/native_helper.py | 93 +++++--- .../trial_task_center/supervisor_evidence.py | 106 +++++++++ .../test_automatic_replacement_regression.py | 2 +- .../test_quick_part_replace_contract.py | 2 +- tests/ui/test_ui_execution_feedback.py | 19 ++ tests/v1_1/test_accuracy_vbs_guards.py | 98 ++++++++- tests/v1_1/test_batch_supervisor.py | 109 +++++++++- tests/v1_1/test_isolated_desktop.py | 92 ++++++++ tests/v1_1/test_missing_uid_inventory.py | 135 ++++++++++++ tests/v1_1/test_native_readback_efficiency.py | 9 + tests/v1_1/test_project_long_paths.py | 2 + tests/v1_1/test_safe_user_stop.py | 51 ++++- tests/v1_1/test_supervisor_evidence.py | 107 +++++++++ tests/v1_1/test_target_cell_execution.py | 2 +- tests/v1_1/test_target_execution.py | 60 ++++++ 30 files changed, 1655 insertions(+), 92 deletions(-) create mode 100644 docs/superpowers/plans/2026-10-30-reliable-supervision.md create mode 100644 docs/superpowers/specs/2026-10-30-reliable-supervision-design.md create mode 100644 docs/testing/2026-10-30-environment-compatibility.md create mode 100644 docs/testing/2026-10-30-field-collection-policy.md create mode 100644 docs/testing/2026-10-30-final-robustness-regression.md create mode 100644 docs/testing/2026-10-30-missing-uid.md create mode 100644 docs/testing/2026-10-30-reliable-supervision.md create mode 100644 docs/testing/evidence/2026-10-30-environment-compatibility.json create mode 100644 src/dx_productivity/trial_task_center/supervisor_evidence.py create mode 100644 tests/v1_1/test_missing_uid_inventory.py create mode 100644 tests/v1_1/test_supervisor_evidence.py diff --git a/docs/superpowers/plans/2026-10-30-reliable-supervision.md b/docs/superpowers/plans/2026-10-30-reliable-supervision.md new file mode 100644 index 0000000..6bf756f --- /dev/null +++ b/docs/superpowers/plans/2026-10-30-reliable-supervision.md @@ -0,0 +1,75 @@ +# Reliable Supervision Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Make batch execution produce durable diagnostics and deterministic forced-stop outcomes when Windows workers or private desktop cleanup fail. + +**Architecture:** Add a small lifecycle/evidence writer shared by worker and supervisor. Keep business result validation separate from transport and resource cleanup; classify missing results from recorded process evidence. Make forced stop durable and idempotent before terminating the owned Job Object. + +**Tech Stack:** Python 3.12, PySide6, pywin32, Windows Job Objects, pytest. + +**Spec:** `docs/superpowers/specs/2026-10-30-reliable-supervision-design.md` + +## Global Constraints + +- Never automatically replay a write operation after an uncertain or missing result. +- Preserve existing task/business result formats and UI recovery actions. +- Use atomic file replacement for durable JSON records. +- Windows-specific tests must remain isolated from real DxDesigner projects. + +--- + +### Task 1: Durable lifecycle and process evidence + +**Files:** +- Create: `src/dx_productivity/trial_task_center/supervisor_evidence.py` +- Modify: `src/dx_productivity/trial_task_center/batch_worker.py` +- Modify: `src/dx_productivity/trial_task_center/batch_supervisor.py` +- Test: `tests/v1_1/test_supervisor_evidence.py` + +- [x] Write tests for durable lifecycle append, bounded stdout/stderr capture, and typed missing-result diagnostics. +- [x] Run `..\\..\\.venv\\Scripts\\python.exe -m pytest -q tests/v1_1/test_supervisor_evidence.py`; observe the new tests fail because the evidence API is absent. +- [x] Implement `LifecycleEvidence.append(event, **fields)`, `try_append`, `write_process_evidence`, and typed errors with `from_directory`. JSON uses existing flush/fsync/replace; JSONL tolerates incomplete final lines. Stream tails are bounded in UTF-8 bytes when persisted. +- [x] Make the worker append startup, business completion, result commit, and exception events; preserve the existing result files. +- [x] Make the supervisor record process exit code and bounded output and use evidence when result files are missing. +- [x] Run the focused evidence tests and then the existing supervisor protocol tests. + +### Task 2: Deterministic forced-stop precedence + +**Files:** +- Modify: `src/dx_productivity/trial_task_center/batch_supervisor.py` +- Reuse unchanged: `src/dx_productivity/trial_task_center/stop_control.py` +- Reuse unchanged: `src/dx_productivity/trial_task_center/supervisor_result.py` +- Test: `tests/v1_1/test_safe_user_stop.py` + +- [x] Add failing tests proving protection-write failure preserves a forced-stop state, transport callback failure does not downgrade a confirmed force, and repeated force requests are idempotent. +- [x] Observe missing forced-state evidence, duplicate termination, and raw late-write `PermissionError` before implementing fixes. +- [x] Persist forced intent before protection and termination, classify protection/termination confirmation separately, and return `user_forced_stop` only after Job termination is confirmed. +- [x] Preserve `primary_error`, existing BOM diagnostics, and cleanup evidence, including simultaneous cleanup and confirmation persistence errors. +- [x] Run all safe-stop and supervisor-result tests. + +### Task 3: Windows process membership and cleanup evidence + +**Files:** +- Modify: `src/dx_productivity/trial_task_center/isolated_desktop.py` +- Reuse unchanged: `src/dx_productivity/trial_task_center/batch_resources.py` +- Test: `tests/v1_1/test_isolated_desktop.py` +- Test: `tests/v1_1/test_desktop_diagnostics.py` + +- [x] Add suspended-start membership and real worker PID/venv identity tests; retain existing PID plus creation-time and desktop-release mismatch tests. +- [x] Reproduce launcher PID differing from actual worker PID; captured stderr identifies the admission rejection. +- [x] Normalize only the current non-frozen venv interpreter launch; verify Job assignment before resume and persist existing cleanup/accounting snapshots. +- [x] Verify and reuse existing strict post-release enumeration and durable release proof; no desktop-release rewrite is needed after the identity fix. +- [x] Run the focused Windows resource tests. + +### Task 4: Regression and operator documentation + +**Files:** +- Modify: `docs/user/safe-stop-and-recovery.md` +- Create: `docs/testing/2026-10-30-reliable-supervision.md` +- Test: `tests/ui/test_ui_execution_feedback.py` + +- [x] Add UI assertions for typed missing-result, forced-stop, and resource-pending messages. +- [x] Run the complete current-version UI/V1.1 suite, final affected-path regression, and required release safety subset. The extra full historical execution was stopped and is not reported as passed; see the testing report for exact scope. +- [x] Document evidence locations, recovery order, and the no-replay rule. +- [x] Record unperformed real CAD/frozen EXE validation and incomplete historical execution explicitly; no skipped or incomplete run is reported as passed. diff --git a/docs/superpowers/specs/2026-10-30-reliable-supervision-design.md b/docs/superpowers/specs/2026-10-30-reliable-supervision-design.md new file mode 100644 index 0000000..45138a6 --- /dev/null +++ b/docs/superpowers/specs/2026-10-30-reliable-supervision-design.md @@ -0,0 +1,30 @@ +# Reliable Supervision Design + +## Goal + +Make batch execution diagnosable and recoverable when the worker crashes, exits without a result, loses its transport channel, or is forcibly stopped. + +## Scope + +This change covers the supervisor/worker protocol, durable lifecycle evidence, forced-stop precedence, and Windows process/resource diagnostics. It does not change replacement business rules or UI layout. + +## Design + +The worker writes an append-only lifecycle journal and atomically commits its final result. The supervisor records child exit status and bounded stdout/stderr evidence before interpreting the result. Missing result files become a typed failure that preserves the original lifecycle state and never authorizes automatic replay. + +Lifecycle entries and output tails are supplemental diagnostics. Failure to write them must not erase a committed business result or forced-stop outcome. The existing strict supervisor ownership journal and uncertainty markers continue to authorize/block operations; supplemental logs never do. + +Forced termination is an explicit state machine. The supervisor persists the forced intent and uncertainty protection before terminating the owned Job Object. Protection or termination errors remain visible as blocked states; they cannot be replaced by a generic transport error. + +The Windows launcher continues to use suspended creation, Job Object assignment, and resume, but records assignment and membership evidence. Cleanup reports success only after process identity, Job accounting, desktop handles, and release records all agree. + +Investigation identified a current-interpreter venv redirector rather than a broken desktop release algorithm. Only non-frozen launches of the current venv interpreter use the base interpreter with its child-local venv identity. Retain existing strict identity admission and desktop release checks. Atomic Job assignment at process creation is deferred as a separate change; externally killing the creator between suspended creation and Job assignment remains outside this iteration's guarantee. + +## Acceptance criteria + +1. Every worker attempt has a durable lifecycle record even when it exits before producing a result. +2. A missing or malformed result reports a typed diagnostic containing exit code and captured stderr/stdout evidence. +3. Forced stop reports `user_forced_stop` after confirmed termination and remains blocked with a retryable diagnostic when confirmation fails. +4. No failure path automatically reruns a write operation. +5. Windows tests cover worker crash, missing result, callback failure, forced-stop retry, orphan descendants, and resource-release mismatch. +6. Existing UI and business tests remain green. diff --git a/docs/testing/2026-10-30-environment-compatibility.md b/docs/testing/2026-10-30-environment-compatibility.md new file mode 100644 index 0000000..43f4f2a --- /dev/null +++ b/docs/testing/2026-10-30-environment-compatibility.md @@ -0,0 +1,82 @@ +# 2026-10-30 Windows 环境兼容性验收 + +## 目标和范围 + +按用户确认,以 **Windows 10/11 x64、DxDesigner EE 7.9.5** 为兼容目标。此次验证把源码测试、模拟异常、真实冻结 EXE 和真实 CAD 工程替换分别记录,不能互相替代。 + +本机是 Windows 11 Pro 10.0.26200 x64,当前会话不是管理员,并处于宿主已有的 Windows Job 中。源码解释器为 CPython 3.12.14,使用项目虚拟环境。没有可用的 Windows 10 实机证据,因此不能把本机测试解释为 Windows 10 已通过。 + +## 源码与启动边界 + +| 场景 | 验证方式 | 结果 | +| --- | --- | --- | +| 普通 Python 虚拟环境 | 真实子进程 PID、虚拟环境前缀、win32api 依赖导入 | 通过 | +| `-E`、`-I` 隔离启动参数 | 真实子进程,分别测试重定向和不重定向标准流 | 通过 | +| 中文、空格工作目录 | 真实子进程回报 CWD 与预期一致 | 通过 | +| 子进程环境隔离 | 验证传入环境字典不因虚拟环境适配而改变 | 通过 | +| 冻结/其他程序命令 | 在原生启动边界的受控测试中验证不替换为当前 Python | 通过;此项不是冻结 EXE 实测 | +| 非管理员与嵌套 Job | 本机实际会话及其子进程;Job 归属、后代退出和桌面释放 | 通过 | +| Job 分配失败/归属未确认 | 故障注入,确认不恢复未受管控的子进程并收尾 | 通过;不等同于所有企业策略均可运行 | +| 中文、长路径及 MAX_PATH 受限路径访问 | 路径专项、工程索引/证据、深目录运行测试 | 通过;真实 CAD 路径限制仍需副本验证 | + +命令: + +```powershell +..\..\.venv\Scripts\python.exe -m pytest -q tests/v1_1/test_isolated_desktop.py tests/v1_1/test_path_access.py tests/v1_1/test_project_long_paths.py tests/v1_1/test_deep_runtime_paths.py tests/v1_1/test_deep_scan_archive_paths.py tests/v1_1/test_com_project_path.py tests/packaging/test_dx_part_replace_build.py --tb=short --junitxml=build/compatibility-source-results.xml +``` + +结果:**61 passed,34.12 秒**。其中新增 8 项启动环境测试。它们验证现有修复的边界,此轮尚未因这些测试修改生产行为。 + +## 冻结包验收 + +使用 `packaging/Build-DxPartReplace.ps1` 从当前源码构建,不用旧 EXE 代替。脚本先运行当前 V1.1 与发布安全子集,再构建、检查 `win32timezone`、验证真实冻结监督器、比较打包 VBS 与源码的哈希并执行应用启动/关闭检查。 + +构建前置回归:**957 passed,1 个已有 pywinauto STA 提示,426.70 秒**。 + +标准构建脚本成功结束。实际冻结监督器、`win32timezone` 归档、进程创建时间读取、桌面释放和 VBS 源码/包内哈希检查全部通过。 + +实际 EXE 环境矩阵 **7/7 通过**: + +| 场景 | 实测结果 | +| --- | --- | +| 清理 Python 环境、从不同工作目录启动 | 通过 | +| PATH 仅保留 System32,不包含 Python | 通过 | +| 设置其他/无效 PYTHONHOME、PYTHONPATH、Qt 插件路径及数据目录环境变量 | 通过;显式数据目录生效,错误目录未创建 | +| 中文、空格数据目录 | 通过 | +| 原生 Windows Qt 平台插件 | 通过;不是 offscreen 替代 | +| 339 字符的数据目录 | 通过;仅证明应用数据/证据路径,不表示 CAD 工程路径上限相同 | +| 把完整 EXE 与 _internal 搬到中文/空格便携目录 | 通过 | + +每次均检查真实冻结监督器退出码为 0、Job 归属验证成功、Job 活跃进程数为 0、桌面句柄关闭且确认桌面消失,以及应用干净退出日志。测试数据均在新建目录,`--smoke-test` 只运行后台进程/桌面检查,不打开或替换 CAD 工程。 + +便携测试包附带 `Run-CompatibilityCheck.ps1`。它不需要目标机安装 Python,已在本机 **Windows PowerShell 5.1.26100.9711** 实际运行通过;输出 JSON 记录退出、日志和资源检查结果。脚本作为本次测试包附件,不会被错误地计为已完成 Windows 10 实机验证。 + +原始矩阵和源码/EXE SHA256 见 [JSON 证据](evidence/2026-10-30-environment-compatibility.json)。本次生产代码没有因兼容专项再修改;增加了启动环境回归,并构建当前修复的独立测试包。 + +测试包:`dist/DxPartReplace-compatibility-20261030.zip`,包含完整便携目录与自检脚本,是本地兼容性测试候选,未发布到远程。 + +## Windows 10/其他机器的复测方法 + +复制或解压完整 `DxPartReplace` 目录,包括 EXE 与 `_internal`,不要只复制 EXE。可先运行包内 `Run-CompatibilityCheck.ps1` 自动检查并生成报告;也可在可写目录运行以下命令,每次使用新的数据目录: + +```powershell +$compatDataRoot = Join-Path $env:LOCALAPPDATA ('DxPartReplace-compat-' + [Guid]::NewGuid().ToString('N')) +$compatExe = (Resolve-Path -LiteralPath '.\DxPartReplace.exe').Path +$compatProcess = Start-Process -FilePath $compatExe -ArgumentList @('--smoke-test', '--data-root', ('"' + $compatDataRoot + '"')) -WindowStyle Hidden -Wait -PassThru +$compatProcess.ExitCode +Get-Content -LiteralPath (Join-Path $compatDataRoot 'logs\application.log') -Tail 30 +Get-ChildItem -LiteralPath (Join-Path $compatDataRoot 'batch-resources') -Filter 'session.json' -Recurse | ForEach-Object { Get-Content -LiteralPath $_.FullName -Raw | ConvertFrom-Json } | Select-Object state,cleanup +``` + +通过条件包括退出码 0、日志同时包含 `packaged_runtime_smoke_passed` 和 `application_clean_shutdown exit_code=0`,以及资源记录 `state=released`、桌面句柄关闭并确认桌面消失。仅看到窗口出现或退出码为 0 不足以证明后台收尾成功。 + +随后需在工程副本上单独验证:工程采集、BOM 导入、替换保存重开回读、暂停/恢复和资源清理。此步骤才覆盖本机中央库、许可证、CAD COM 注册、工程和文件服务器条件。 + +## 尚未证明的环境 + +- Windows 10 实机、其他 Windows 11 构建及不同补丁状态。 +- 其他电脑的 DxDesigner 7.9.5 安装、中心库、许可证与 COM 注册。 +- 网络共享工程/数据目录、多用户并发访问 SQLite、只读安装目录和企业杀毒/应用控制策略。 +- 其他 CAD 版本和 32 位 Windows;本次目标为 Windows x64、DxDesigner 7.9.5。 + +不以放宽身份校验、忽略 Job 分配失败或自动重放替换来换取表面兼容。环境不允许安全执行时,必须停止并保留明确诊断。 diff --git a/docs/testing/2026-10-30-field-collection-policy.md b/docs/testing/2026-10-30-field-collection-policy.md new file mode 100644 index 0000000..153d136 --- /dev/null +++ b/docs/testing/2026-10-30-field-collection-policy.md @@ -0,0 +1,21 @@ +# 字段采集鲁棒性与回退策略 + +器件采集不能假设 COM 属性、用户属性或显示属性都存在且可读。当前按字段用途分层: + +| 字段 | 作用 | 读取失败时 | +|---|---|---| +| UID | 组件唯一身份、选择、回读和恢复关联 | 优先使用真实 UID;缺失时使用图页内唯一 Refdes 的本地键 `@REFDES|`,重复或 Refdes 缺失则失败;不伪造 CAD UID | +| Refdes | 人工确认和目标定位 | 采集目标行立即失败;不能生成无位号计划 | +| Part Number | 源料号匹配、替换范围 | 对候选组件要求可读;COM 异常立即失败,属性缺失则该组件不匹配 | +| Value | 精确目标校验和 NC/Value 策略 | 非精确展示场景可回退为空;若快照选择器或恢复校验依赖它,校验失败并停止 | +| Part Name | 替换后库身份校验 | 替换回读阶段必需;缺失/异常失败,不继续提交为成功 | +| Part Label | 仅在 Value 策略为 Part Label 或库回读证明要求时必需 | 不需要时可为空;需要时缺失/异常失败 | +| Cell Name、NC、用户属性 | 规则或可选策略 | 未启用对应策略时不读取或回退默认;启用时变为必需并校验 | +| 坐标、字体、颜色、可见性等显示字段 | 未选择器件的完整状态和展示证据 | 使用安全默认值并写 WARN;若后续完整回读证明不一致,停止恢复/报告失败 | +| 引脚/网络/坐标几何 | 连接和引脚号晋级证明 | 需要该证明时失败;不把缺失网络伪装成可验证的连接 | + +Python native helper 现在对可选属性捕获 COM 异常并返回空值,对身份和源料号使用 required 模式;真实 UID 缺失时,当前页枚举和选择/回读统一使用 `@REFDES|...` fallback identity,并检测重复位号。VBS 增加了可选属性安全读取、显示字段默认值和警告日志;必需字段仍通过显式 required 读取。所有失败都回到有限错误出口,监督器可收集状态,不进入等待循环。 + +安全原则是“缺字段可降级,身份不可猜测”:如果降级会影响替换对象、保存回读或恢复判断,就停止而不是继续。诊断中会保留阶段、字段名和错误,但不会把错误的默认值当作成功证据。 + +验证:字段读取与身份专项覆盖可选显示字段读取失败、可选 Part Label 读取失败、必需 Part Number/Part Name 失败、无 UID 的 Refdes fallback、重复 Refdes 阻断和无位号场景。兼容性回归持续要求无歧义时才允许继续。 diff --git a/docs/testing/2026-10-30-final-robustness-regression.md b/docs/testing/2026-10-30-final-robustness-regression.md new file mode 100644 index 0000000..06db654 --- /dev/null +++ b/docs/testing/2026-10-30-final-robustness-regression.md @@ -0,0 +1,43 @@ +# 库存入口与独立脚本回归 + +工作分支:`codex/project-optimization`。本轮延续无 UID、可选字段回退和监督进程可靠性修复。 + +## 复现并修复的问题 + +- 独立原生读取脚本只抽取固定函数列表,遗漏 `QPSnapshotAttribute` 新增依赖 `QPReadOptionalAttributeValue`,真实 `cscript.exe` 执行返回 `Variable is undefined`。已把依赖加入脚本生成列表,沿用实际生成和执行脚本的回归用例。 +- 库存入口生成 fallback identity 后,导出前仍检查 CAD 原始 UID 非空,导致无 UID 工程仍被阻断。已检查本地 identity 和非空位号;导出后关联键为 `@REFDES|`。 +- 库存导出未直接拒绝同页重复位号。现在按去除首尾空白、忽略大小写的位号检查当前图页,包括具有真实 UID 的器件;冲突时不发布库存文件。 +- 库存读取可选 Value 时仍走必需读取路径。现在使用现有可选字段读取函数,COM 读取失败降级为空并记录警告;身份和源料号仍按必需字段处理。 +- 扩展恢复测试发现 `QPSnapshotAttribute` 将基础 Value 和 InstanceValue 均写成最终有效值,破坏恢复前的原始属性比对。现分别读取、保存两个原始字段,保留空覆盖值;读取异常仍使用有限降级出口。新增空覆盖值和非空覆盖值回归,字段与完整 Value 恢复专项 121 passed。 +- 两组旧测试替身没有接收 `required` 参数,已对齐生产接口。长路径样例改为动态增加目录直到超过 260 字符,不再依赖 pytest 根目录长度。旧契约测试按已批准的可选显示字段回退策略检查告警。 + +## 测试环境 + +系统盘空间不足曾引起 SQLite 和文件写入级联失败,此类结果不能当作代码回归结论。后续进程将 `TEMP` 和 `TMP` 指向 F 盘,且 pytest `--basetemp` 必须位于该 TEMP 下,满足旧安装器测试的隔离要求。未修改安装器的目录保护。 + +```powershell +$env:TEMP='F:\dxpart-test-temp' +$env:TMP=$env:TEMP +..\..\.venv\Scripts\python.exe -m pytest -q tests --basetemp F:\dxpart-test-temp\full-final --junitxml build\regression-final-20261030.xml --tb=short +``` + +## 已完成的专项验证 + +- 最初 7 项失败对应的完整相关测试文件:84 passed。 +- 旧原生 helper 集成组:17 passed。 +- 新增库存入口、字段策略和 VBS 契约专项:75 passed。库存入口用例实际调用 `cscript.exe`,检查发布内容中的 UID/fallback 和位号,并覆盖空位号、大小写重复位号及可选 Value COM 异常。 + +发布前回归(`tests/v1_1`、应用入口、执行安全、替换结果验证):**986 passed,1 warning,280.28 秒**。提示来自已有的 pywinauto COM 线程模式切换。报告:`build/release-regression-final-20261030.xml`。 + +最终冻结包从最新源码重建成功,`tools/verify_packaged_runtime.py` 真实执行通过:冻结监督器、`win32timezone`、进程创建时间读取、资源释放均通过。两个 VBS 文件源码/包内 SHA256 一致: + +| 文件 | SHA256 | +|---|---| +| QuickPartReplace.vbs | `841411B889EC994D2B3C0B88D744BB896E4787601DC5AB522FCE7CCFBFD0BFB3` | +| NativeSnapshotReader.vbs | `419DD800C1F4649736F87009D64EAE666B9D574ECD170FC8BC662E35B9C83747` | + +全目录扩展运行(运行过程中包含代码修订,不能作为固定版本全绿证明):2219 passed、9 failed,1532.09 秒。3 项与本轮中途变更的库存/快照实现有关,已在独立专项通过;5 项隔离 Python 启动失败伴随基础运行时标准库缺失;1 项停止握手在 1 秒限时内超时。保留报告 `build/regression-final-20261030.xml`,不覆盖为通过。 + +后续核对发现,原虚拟环境指向的 Codex Python 运行时已经缺少 `Lib/os.py`、`Lib/encodings/__init__.py`,运行时报告 `Could not find platform independent libraries `;工具自带 PowerShell 部分模块也不可用。未修改原虚拟环境或这些共享运行时,改用本机完整 CPython 3.12.10 建立临时验证环境,在 `.pth` 中引用原有依赖与 Win32 DLL 目录。隔离启动实际导入 Win32、Qt 成功;发布前集合和 Value 恢复集合共 **1085 passed、2 warnings、285.51 秒**。剩余 2 项是在原依赖目录缺少 `win32ui` 的环境中导入 pywinauto 失败,属于验证环境依赖损坏,不是业务断言;需在目标机安装完整 pywin32 后复测。 + +Windows 10 实机、真实无 UID DxDesigner 7.9.5 工程保存重开回读、网络共享和其他机器许可证/中央库条件,仍需现场证据。 diff --git a/docs/testing/2026-10-30-missing-uid.md b/docs/testing/2026-10-30-missing-uid.md new file mode 100644 index 0000000..d3e62e8 --- /dev/null +++ b/docs/testing/2026-10-30-missing-uid.md @@ -0,0 +1,30 @@ +# 无 UID 器件鲁棒性修复 + +## 根因 + +目标计划的 `ComponentTarget` 本身已经拒绝空 UID,但来自原生快照和实时 CAD 的路径仍可能在进入计划之前把空 UID 当作普通字符串: + +- `native_helper.read_snapshot()` 只检查行列数和 UID 集合重复,没有支持本地 fallback 身份; +- `check_context()` 以 `str(component.UID)` 构造字典,单个空 UID 可以成为合法字典键,多个空 UID 还可能互相覆盖; +- `QuickPartReplace.vbs` 的库存循环在后续分支才读取 UID,错误可能发生在 CAD 打开、等待或选择之后,导致界面表现为长时间无进展。 + +## 修复 + +- 快照协议仍拒绝空 UID 文本,但 VBS 库存和实时 CAD 会生成一致的本地键 `@REFDES|`; +- CAD 当前页枚举统一通过 `_component_uid()`,UID 缺失时使用 Refdes fallback,重复 fallback 键立即阻断; +- VBS 库存循环在读取器件身份时允许 UID 缺失,但要求 Refdes 可读;无 Refdes 或无法读取身份仍立即失败; +- 选择、快照、回读和恢复统一使用 fallback 键,底层 CAD 查找使用 Refdes/坐标回退索引;原生 CLI 无法提供稳定 UID 时继续退回 CAD 采集; +- 不生成伪造的 CAD UID;fallback 只用于本批次本地关联,位号不唯一时不能继续。 + +## 验证 + +```text +tests/v1_1/test_target_execution.py +tests/v1_1/test_accuracy_vbs_guards.py +tests/v1_1/test_native_inventory_reader.py +tests/v1_1/test_native_ascii_inventory.py +tests/trial_task_center/test_quick_run.py +160 passed in 47.39s +``` + +新增场景覆盖:无 UID 的唯一 Refdes fallback、重复 Refdes 阻断、VBS 无 UID、VBS 空 Refdes、可选属性失败和必需属性失败。真实 DxDesigner 工程仍需用副本验证 fallback 选择、保存回读和窗口收尾。 diff --git a/docs/testing/2026-10-30-reliable-supervision.md b/docs/testing/2026-10-30-reliable-supervision.md new file mode 100644 index 0000000..982021d --- /dev/null +++ b/docs/testing/2026-10-30-reliable-supervision.md @@ -0,0 +1,57 @@ +# 2026-10-30 监督执行可靠性改进 + +分支:`codex/project-optimization`。本轮在其独立工作树执行,未修改主工作树 DSH 分支,未运行真实 CAD 工程写入。 + +## 原因与结论修正 + +此前 UI 与 V1.1 回归为 904 通过、15 失败。仅凭缺少 `result.json` 不能确定是业务执行、Job 清理或资源协议的根因。 + +本轮捕获原先被丢弃的 stderr 后,复现了 `Supervisor request belongs to another process`:Windows 虚拟环境的 `Scripts/python.exe` 启动转发器拥有一个 PID,而实际解释器是另一个 PID。监督记录保存前者,worker 读取自身身份为后者,严格准入校验遂拒绝正常请求。私有桌面的窗口/线程诊断同样受此 PID 差异影响。 + +修复在原生启动边界只针对当前、非冻结的虚拟环境解释器,直接启动 `sys._base_executable`,在该子进程的环境字典中设置 `__PYVENV_LAUNCHER__` 保留虚拟环境身份和依赖。未放宽 PID/创建时间校验,也未修改 Windows 用户环境变量。冻结 EXE 和非当前 Python/CAD 命令沿用原启动路径。 + +## 改动 + +1. `supervisor_evidence.py` 管理辅助阶段日志和有界进程诊断;进程 JSON 复用现有带 flush/fsync/原子替换的 `write_record`。JSONL 读取保留完整事件并容忍硬退出留下的尾部残行。 +2. worker 在准入成功后记录阶段,监督器在启动前与退出后记录阶段。进程退出码、stdout/stderr 尾部与 Job 清理证据保存在 `process.json`,stderr 不再送入 DEVNULL。 +3. 启动失败、结果缺失、结果无效分别使用稳定错误代码。业务结果缺失不触发自动重放;中断保护写入失败返回 `interruption_protection_failed`,保存原业务错误和保护写入错误。 +4. 强制结束先持久化请求和任务保护,再请求终止;重复请求不会重复终止。Job/资源或确认记录未能确认时返回结构化的 `batch_resources_pending`,其中保留 `user_forced_stop` 主错误,不把未确认状态报告为干净结束。 +5. 原生启动在挂起状态下加入 Job 后调用 `IsProcessInJob` 验证;未确认归属时不恢复主线程。现有 Job 为空、后代进程退出、桌面关闭和消失确认的严格检查继续保留。 +6. 诊断包增加 `process.json` 和 `lifecycle.jsonl` 白名单,沿用已有文件数量/大小限制和脱敏。 + +## 故障注入与边界 + +- 真实 Python worker 正常启动与虚拟环境身份一致性;深路径启动和准入请求防重放。 +- worker 硬退出、没有结果、损坏 JSON、退出码 0 但没有有效业务结果。 +- 100000 字节无换行 stderr 后硬退出,尾部保留并限制为 64 KiB。 +- 中断保护写入拒绝;诊断写入拒绝不覆盖已提交业务结果。 +- 正常强制结束、首次保护失败重试、UI 回调异常后强制结束、Job 收尾确认异常、最后一次确认记录写入失败,以及同时发生收尾与写入失败。 +- 根进程退出后仍存活的后代进程;桌面仍被外部引用;无窗口线程的桌面诊断。全部使用测试副本、进程桩和临时桌面,不访问生产工程。 + +独立审查发现并复现了“终止后最后一次记录写入失败覆盖强制结束结果”的问题;增加真实进程故障注入后修复,复核无剩余可操作发现。 + +## 外部经验与本项目取舍 + +- [Microsoft Job Objects](https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects):Job 管理后代进程;只判断根进程退出不足以证明整个执行已收尾。 +- [Microsoft Process Creation Flags](https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags):挂起创建后完成归属,再恢复线程。 +- [CPython 3.12 的 Windows launcher 实现](https://github.com/python/cpython/blob/3.12/PC/launcher.c):`VENV_REDIRECT` 定位原始解释器并创建新进程,`__PYVENV_LAUNCHER__` 用于解释器身份和虚拟环境前缀。这与本机真实 PID 和依赖探针的复现结果相符。 +- [win-safesubprocess](https://github.com/mokusatsu/win_safesubprocess):参考挂起创建、加入 Job、恢复及失败关闭的顺序,不引入其运行时依赖。 +- [pytest-warden](https://github.com/alexneo2003/pytest-warden):参考监督器与可能卡住的 worker 分进程以及真实子进程故障测试;本项目对可能写入的 CAD 阶段不照搬自动超时硬杀策略。 +- [etcdrs issue 64](https://github.com/tgockel/etcdrs/issues/64)、[fidget issue 1690](https://github.com/omesser/fidget/issues/1690):用于检查 Job 归属和挂起顺序的竞态经验;issue 是参考材料,不是本机缺陷已复现的证明。 + +本轮不改用新的任务框架,也不因缺失结果就重新替换。`PROC_THREAD_ATTRIBUTE_JOB_LIST` 原子创建归属属于后续可单独验证的增强;当前继续使用既有挂起创建顺序并增加归属验证。不能宣称覆盖父进程在创建与归属之间被外部强杀的全部情况。 + +## 验证记录 + +| 验证 | 结果 | +| --- | --- | +| `python -m pytest -q tests/ui tests/v1_1 --tb=short --junitxml=build/reliable-supervision-results.xml` | 945 passed,1 个已有 pywinauto STA 提示,346.31 秒;此前 15 个失败均通过 | +| 最后修改后重跑:UI、safe_user_stop、batch_supervisor、supervisor_evidence、isolated_desktop、desktop_diagnostics、supervisor_result | 128 passed,174.94 秒;包含审查后新增的最后一次确认写入失败用例 | +| 发布安全子集:`tests/part_replace/test_verification.py tests/trial_task_center/test_execution_safety.py tests/trial_task_center/test_application.py` | 41 passed,13.72 秒 | +| `git diff --check` | 无空白错误 | +| 全仓测试收集(含历史用例) | 当时共收集 2204 项,可正常收集;后续新增测试使最终数量可能变化 | +| 额外全仓历史执行:排除 `tests/ui` 与 `tests/v1_1` | 已启动,因旧 VBS 路径耗时而主动停止;不计作完成或通过,不替代当前版本与发布安全子集结果 | + +上述三组通过计数存在重叠,不能相加为独立用例数量。945 项回归之后有强制结束确认持久化和阶段日志的最后修改,使用 128 项专项回归验证最终相关代码,没有把前次完整运行冒称为最终全仓完整运行。 + +真实 DxDesigner 替换、中央库/许可证兼容与冻结包实机仍须单独验收;模拟进程通过不能代替这些现场结论。未构建或发布新版本;修改保留在目标分支的工作树中。 diff --git a/docs/testing/evidence/2026-10-30-environment-compatibility.json b/docs/testing/evidence/2026-10-30-environment-compatibility.json new file mode 100644 index 0000000..008af92 --- /dev/null +++ b/docs/testing/evidence/2026-10-30-environment-compatibility.json @@ -0,0 +1,138 @@ +{ + "windows": [ + 10, + 0, + 26200, + 2, + "" + ], + "architecture": "x64", + "is_admin": false, + "parent_in_job": true, + "exe_sha256": "4feda79db29f6a176ee7b3c4772d701c3658e6dc3212eb9c5f88045610ca88d9", + "cad_project_opened": false, + "cases": [ + { + "name": "clean-foreign-working-directory", + "data_root": "F:\\Sync Tool Skyworth\\Work Maps\\dxdesigner-part-replacement\\.worktrees\\project-optimization\\build\\compatibility-20261030-114333\\data-clean", + "path_length": 138, + "native_ui": false, + "passed": true, + "exit_code": 0, + "stderr_tail": "", + "frozen_supervisor": true, + "desktop_released": true, + "job_empty": true, + "seconds": 8.453 + }, + { + "name": "path-without-python", + "data_root": "F:\\Sync Tool Skyworth\\Work Maps\\dxdesigner-part-replacement\\.worktrees\\project-optimization\\build\\compatibility-20261030-114333\\data-no-python-path", + "path_length": 147, + "native_ui": false, + "passed": true, + "exit_code": 0, + "stderr_tail": "", + "frozen_supervisor": true, + "desktop_released": true, + "job_empty": true, + "seconds": 6.781 + }, + { + "name": "foreign-python-and-qt-environment", + "data_root": "F:\\Sync Tool Skyworth\\Work Maps\\dxdesigner-part-replacement\\.worktrees\\project-optimization\\build\\compatibility-20261030-114333\\data-poisoned", + "path_length": 141, + "native_ui": false, + "passed": true, + "exit_code": 0, + "stderr_tail": "", + "frozen_supervisor": true, + "desktop_released": true, + "job_empty": true, + "seconds": 8.203 + }, + { + "name": "unicode-and-spaces-data", + "data_root": "F:\\Sync Tool Skyworth\\Work Maps\\dxdesigner-part-replacement\\.worktrees\\project-optimization\\build\\compatibility-20261030-114333\\中文 数据目录 with spaces", + "path_length": 147, + "native_ui": false, + "passed": true, + "exit_code": 0, + "stderr_tail": "", + "frozen_supervisor": true, + "desktop_released": true, + "job_empty": true, + "seconds": 8.875 + }, + { + "name": "native-windows-platform", + "data_root": "F:\\Sync Tool Skyworth\\Work Maps\\dxdesigner-part-replacement\\.worktrees\\project-optimization\\build\\compatibility-20261030-114333\\data-windows-ui", + "path_length": 143, + "native_ui": true, + "passed": true, + "exit_code": 0, + "stderr_tail": "", + "frozen_supervisor": true, + "desktop_released": true, + "job_empty": true, + "seconds": 10.719 + }, + { + "name": "long-data-path", + "data_root": "F:\\Sync Tool Skyworth\\Work Maps\\dxdesigner-part-replacement\\.worktrees\\project-optimization\\build\\compatibility-20261030-114333\\level-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\\revision-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\\data", + "path_length": 339, + "native_ui": false, + "passed": true, + "exit_code": 0, + "stderr_tail": "", + "frozen_supervisor": true, + "desktop_released": true, + "job_empty": true, + "seconds": 8.531 + }, + { + "name": "relocated-unicode-portable-folder", + "data_root": "F:\\Sync Tool Skyworth\\Work Maps\\dxdesigner-part-replacement\\.worktrees\\project-optimization\\build\\compatibility-20261030-114333\\data-relocated", + "path_length": 142, + "native_ui": false, + "passed": true, + "exit_code": 0, + "stderr_tail": "", + "frozen_supervisor": true, + "desktop_released": true, + "job_empty": true, + "seconds": 19.61 + } + ], + "passed": true, + "powershell_sidecar": { + "passed": true, + "windows": "10.0.26200.0", + "powershell": "5.1.26100.9711", + "exit_code": 0, + "cad_project_opened": false + }, + "source_suite": { + "passed": 61, + "seconds": 34.12 + }, + "build_gate": { + "passed": 957, + "seconds": 426.7, + "warnings": 1 + }, + "unverified": [ + "Windows 10 physical machine", + "Other Windows builds", + "Actual CAD project replacement and library/license differences", + "Enterprise app-control/antivirus policy", + "Network-share data root" + ], + "build_type": "unpublished compatibility candidate; onedir; x64", + "source_sha256": { + "batch_supervisor.py": "5ba630615eea788981c87e86883ef5ffa7f837c313870e3dd71a0671892bafed", + "batch_worker.py": "805e880d1f25139435733698fdf8525daa9be7c496b5b7e113638ee42361f823", + "isolated_desktop.py": "9e60b3820a5132a727f5492b73f678ccc2913c9bb966edc6636088347582dafe", + "supervisor_evidence.py": "ebd1d046e851dd04b7d6f5ec5cba916825707955884fdc8462b384053e91cc95" + } +} \ No newline at end of file diff --git a/docs/user/safe-stop-and-recovery.md b/docs/user/safe-stop-and-recovery.md index da1e96a..cf34355 100644 --- a/docs/user/safe-stop-and-recovery.md +++ b/docs/user/safe-stop-and-recovery.md @@ -28,3 +28,21 @@ - 如果应用没有正常退出或后代进程需要强制清理,会单独报告收尾异常,不能把“资源已释放”理解为“保存和退出均正常”。 任务管理器强杀、系统关机或掉电可能不给程序写最终日志的机会。这些情况依赖修改前已经落盘的证据及下次启动的中断核验;程序不能保证被外部强杀时完成当前保存。 + +## 后台进程启动或结果异常 + +`worker_start_failed` 表示后台进程未成功启动。`worker_result_missing` 表示进程已退出但没有可信的最终结果;`worker_result_invalid` 表示结果损坏或身份校验未通过。退出码为 0 也不能代替业务结果和保存核验。 + +出现结果缺失或无效时,先导出诊断包;若显示资源待处理,先点“重新清理资源”,再对结果不确定的任务使用“核验并恢复”。清理不会重复替换,也不会清除工程保护。`interruption_protection_failed` 表示连中断保护也未能写入,请保留现场、日志和备份,不要删除批次记录绕过阻断。 + +如果采集到的器件 UID 为空、位号为空或 UID 重复,工具会在读取快照和当前 CAD 页时立即停止,报告目标身份不完整,并保留诊断记录。此类器件不会进入替换选择或等待回读循环;修正工程身份后应重新采集和生成计划。不要把空 UID 当作可重复使用的匹配键。 + +每个批次的 `data/batch-resources/<批次ID>/` 增加以下诊断文件: + +- `lifecycle.jsonl`:启动、业务开始/结束、资源收尾和最终结果提交的阶段记录。 +- `process.json`:监督进程退出码、stdout/stderr 各自最多 64 KiB 的 UTF-8 尾部、最后协议消息和自有 Job 的释放记录。 +- `supervisor.json` 中的 `force_state`:记录强制结束是否完成保护、是否已请求终止、退出确认是否完成。 + +阶段日志是辅助诊断;最终 `result.json`、保存回读和保护记录仍是执行/恢复的依据。辅助日志写入失败不会覆盖已经提交的业务结果。若强制结束后的确认记录写入失败,仍显示资源/记录待处理并保留强制结束事实,不能直接开始下一次替换。 + +诊断包会包含上述文件,并沿用账号、令牌与用户目录脱敏规则。原始工程和库文件不会随诊断包导出。 diff --git a/integration/dxdesigner795/NativeSnapshotReader.vbs b/integration/dxdesigner795/NativeSnapshotReader.vbs index b1fa02b..0713801 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 QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ") + Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines 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]*" diff --git a/integration/dxdesigner795/QuickPartReplace.vbs b/integration/dxdesigner795/QuickPartReplace.vbs index b3b493d..1f0e632 100644 --- a/integration/dxdesigner795/QuickPartReplace.vbs +++ b/integration/dxdesigner795/QuickPartReplace.vbs @@ -25,6 +25,7 @@ Dim QP_RestoreTransactionOpen Dim QP_PinIndex Dim QP_PinContext Dim QP_ComponentUidIndex, QP_ComponentRefdesIndex, QP_ComponentLocationIndex +Dim QP_FallbackIdentityUsed Dim QP_ComponentIndexContext Dim QP_NativeCallActive Dim QP_BatchMode, QP_OwnedProject, QP_BatchState, QP_BatchCode, QP_BatchMessage @@ -69,6 +70,7 @@ Set QP_ComponentUidIndex = Nothing Set QP_ComponentRefdesIndex = Nothing Set QP_ComponentLocationIndex = Nothing QP_ComponentIndexContext = "" +QP_FallbackIdentityUsed = False QP_CurrentJobId = "" QP_ExitCode = 0 QP_Stage = "session_startup" @@ -714,8 +716,8 @@ End Sub Sub QPDiscoverInventory(ByRef Documents) Dim Requested, Schematics, Schematic, Sheets, Sheet, Document, View - Dim Components, Comp, PartAttr, ValueAttr, PartNumber, PartKey, HitKey, Seen, Text, Count - Dim ComponentText, ValueText, ComponentMode + Dim Components, Comp, PartAttr, ValueAttr, PartNumber, PartKey, HitKey, Seen, Text, Count, Uid + Dim ComponentText, ValueText, ComponentMode, SheetRefdes, RefdesKey Set Requested = QPReadInventoryRequest() Set Seen = CreateObject("Scripting.Dictionary") Text = "project_path" & vbTab & "schematic_name" & vbTab & "sheet_number" & vbTab & "old_part" & vbCrLf @@ -739,22 +741,31 @@ Sub QPDiscoverInventory(ByRef Documents) Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "inventory sheet identity changed" QP_Stage = "inventory_query " & CStr(Schematic) & ":" & CStr(Sheet) Set Components = View.Query(VDM_COMP, VD_ALL) + Set SheetRefdes = CreateObject("Scripting.Dictionary") + SheetRefdes.CompareMode = vbTextCompare For Each Comp In Components QP_Stage = "inventory_read_component " & CStr(Schematic) & ":" & CStr(Sheet) + Uid = QPComponentIdentityKey(Comp) + If ComponentMode Then + RefdesKey = Trim(CStr(Comp.Refdes)) + If Len(RefdesKey) > 0 Then + If SheetRefdes.Exists(RefdesKey) Then Err.Raise vbObjectError + 872, _ + "QuickPartReplace.Discovery", "duplicate component Refdes: " & RefdesKey + SheetRefdes.Add RefdesKey, True + End If + End If PartNumber = "" Set PartAttr = QPFindCurrentAttribute(Comp, "Part Number") If Not PartAttr Is Nothing Then PartNumber = QPEffectiveAttributeValue(PartAttr) PartKey = LCase(CStr(PartNumber)) If Requested.Exists(PartKey) Then If ComponentMode Then - ValueText = "" - Set ValueAttr = QPFindCurrentAttribute(Comp, "Value") - If Not ValueAttr Is Nothing Then ValueText = QPEffectiveAttributeValue(ValueAttr) - If Len(CStr(Comp.UID)) = 0 Or Len(CStr(Comp.Refdes)) = 0 Then _ + ValueText = QPEffectiveComponentAttribute(Comp, "Value") + If Len(Trim(CStr(Uid))) = 0 Or Len(Trim(CStr(Comp.Refdes))) = 0 Then _ Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "component identity is incomplete" ComponentText = ComponentText & QPEncode(QPProjectPath()) & vbTab & _ QPEncode(CStr(Schematic)) & vbTab & QPEncode(CStr(Sheet)) & vbTab & _ - QPEncode(CStr(Comp.UID)) & vbTab & QPEncode(CStr(Comp.Refdes)) & vbTab & _ + QPEncode(Uid) & vbTab & QPEncode(CStr(Comp.Refdes)) & vbTab & _ QPEncode(CStr(PartNumber)) & vbTab & QPEncode(CStr(ValueText)) & vbCrLf End If HitKey = QPSheetKey(Schematic, Sheet) & Chr(30) & PartKey @@ -776,6 +787,49 @@ Sub QPDiscoverInventory(ByRef Documents) If ComponentMode Then QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "components.tsv"), ComponentText End Sub +Function QPComponentIdentityKey(ByRef Comp) + Dim Uid, Refdes, Number, Description + On Error Resume Next + Err.Clear + Uid = CStr(Comp.UID) + Number = Err.Number + Description = Err.Description + Err.Clear + On Error GoTo 0 + If Number <> 0 Then Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", _ + "component UID could not be read: " & Description + On Error Resume Next + Err.Clear + Refdes = CStr(Comp.Refdes) + Number = Err.Number + Description = Err.Description + Err.Clear + On Error GoTo 0 + If Number <> 0 Then Err.Raise vbObjectError + 872, _ + "QuickPartReplace.Discovery", "component Refdes is unreadable; operation stopped before any replacement" + If Len(Trim(CStr(Uid))) > 0 Then + QPComponentIdentityKey = CStr(Uid) + Else + QP_FallbackIdentityUsed = True + If Len(Trim(CStr(Refdes))) > 0 Then + QPComponentIdentityKey = "@REFDES|" & Trim(CStr(Refdes)) + QPLog QP_Root, "WARN", "component_uid_missing_using_refdes_fallback refdes=" & CStr(Refdes) + Else + Dim Point + On Error Resume Next + Set Point = Comp.GetLocation() + If Err.Number <> 0 Or Point Is Nothing Then + Err.Clear + On Error GoTo 0 + Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "component UID and Refdes are empty" + End If + QPComponentIdentityKey = "@ANON|" & CStr(Point.X) & "|" & CStr(Point.Y) + Err.Clear + On Error GoTo 0 + End If + End If +End Function + Function QPReadInventoryRequest() Dim Path, Lines, Header, Result, Index, Fields, OldPart, Key Path = QP_Fso.BuildPath(QP_Root, "inventory-request.tsv") @@ -1185,13 +1239,13 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath) ' when restoration needs them, from fresh proxies after native mutation. Set Selectors = QPCommandTargets(Command) For Each Comp In Components - Uid = CStr(Comp.UID) + Uid = QPComponentIdentityKey(Comp) If Selectors Is Nothing Then - PartNumber = QPEffectiveComponentAttribute(Comp, "Part Number") + PartNumber = QPReadableAttributeValue(Comp, "Part Number") If StrComp(PartNumber, CStr(Command("old_part_number")), _ vbTextCompare) = 0 Then Matches.Add CStr(Matches.Count), Comp ElseIf Selectors.Exists(Uid) Then - PartNumber = QPEffectiveComponentAttribute(Comp, "Part Number") + PartNumber = QPRequiredAttributeValue(Comp, "Part Number") Selector = Selectors.Item(Uid) ValueText = "" Set ValueAttr = QPFindCurrentAttribute(Comp, "Value") @@ -1235,10 +1289,10 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath) Set MatchedUids = CreateObject("Scripting.Dictionary") For Index = 0 To Matches.Count - 1 Set Comp = Matches.Item(CStr(Index)) - MatchedUids.Add CStr(Comp.UID), True + MatchedUids.Add QPComponentIdentityKey(Comp), True Next Set NativeRead = Nothing - If QPUseNativeSnapshot() Then + If QPUseNativeSnapshot() And Not QP_FallbackIdentityUsed Then QPRequireProject Command Set NativeRead = QPNativeCapture(MatchedUids) If Not NativeRead Is Nothing Then @@ -1248,7 +1302,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath) End If For Index = 0 To Matches.Count - 1 Set Comp = Matches.Item(CStr(Index)) - Uid = CStr(Comp.UID) + Uid = QPComponentIdentityKey(Comp) QPLog QP_Root, "INFO", "stage=snapshot_component uid=" & QPLogField(Uid) If NativeRead Is Nothing Then Snapshot = Snapshot & QPSnapshotComponent(Comp) @@ -1318,6 +1372,24 @@ Function QPUseNativeSnapshot() QPUseNativeSnapshot = True End Function +Function QPReadableAttributeValue(ByRef Comp, ByVal Name) + Dim Attr, ErrorNumber, ErrorSource, ErrorDescription + On Error Resume Next + Err.Clear + Set Attr = Comp.FindAttribute(CStr(Name)) + ErrorNumber = Err.Number + ErrorSource = Err.Source + ErrorDescription = Err.Description + Err.Clear + On Error GoTo 0 + If ErrorNumber <> 0 Then Err.Raise ErrorNumber, ErrorSource, ErrorDescription + If Attr Is Nothing Then + QPReadableAttributeValue = "" + Else + QPReadableAttributeValue = QPEffectiveAttributeValue(Attr) + End If +End Function + Function QPUntouchedComponent(ByRef Comp) Dim State, Identity, Fields State = QPUntouchedFullState(Comp) @@ -1360,8 +1432,10 @@ Function QPUntouchedIdentity(ByVal State) End Function Function QPUntouchedFullState(ByRef Comp) - Dim Point, Pins, Connections, Attr, AttributeText, Result, Uid + Dim Point, Pins, Connections, Attr, AttributeText, Result, Uid, Identity Uid = CStr(Comp.UID) + Identity = Uid + If Len(Trim(Uid)) = 0 Then Identity = "@REFDES|" & Trim(CStr(Comp.Refdes)) Set Point = Comp.GetLocation() Set Pins = QPComponentPins(Uid) If Pins.Count = 0 Then @@ -1371,22 +1445,54 @@ Function QPUntouchedFullState(ByRef Comp) Else Connections = QPConnectionSignatureFromPins(Uid, Pins) End If - Result = "C" & vbTab & QPEncode(Uid) & vbTab & QPEncode(CStr(Comp.Refdes)) & _ + Result = "C" & vbTab & QPEncode(Identity) & vbTab & QPEncode(CStr(Comp.Refdes)) & _ vbTab & QPEncode(CStr(Point.X)) & vbTab & QPEncode(CStr(Point.Y)) & _ vbTab & QPEncode(CStr(Comp.Orientation)) & vbTab & QPEncode(CStr(Comp.Scale)) & _ vbTab & QPEncode(Connections) & vbCrLf AttributeText = "" For Each Attr In Comp.Attributes - AttributeText = AttributeText & QPSnapshotAttribute(Uid, Attr) + AttributeText = AttributeText & QPSnapshotAttribute(Identity, Attr) Next QPUntouchedFullState = Result & QPSortSnapshotLines(AttributeText) End Function Function QPEffectiveComponentAttribute(ByRef Comp, ByVal Name) - Dim Attr + Dim Attr, ErrorNumber, ErrorDescription QPEffectiveComponentAttribute = "" + On Error Resume Next + Err.Clear Set Attr = QPFindCurrentAttribute(Comp, Name) - If Not Attr Is Nothing Then QPEffectiveComponentAttribute = QPEffectiveAttributeValue(Attr) + ErrorNumber = Err.Number + ErrorDescription = Err.Description + Err.Clear + If ErrorNumber = 0 And Not Attr Is Nothing Then + QPEffectiveComponentAttribute = QPReadOptionalAttributeValue(Attr) + ElseIf ErrorNumber <> 0 Then + QPLog QP_Root, "WARN", "optional_attribute_unavailable name=" & CStr(Name) & _ + " description=" & CStr(ErrorDescription) + End If + On Error GoTo 0 +End Function + +Function QPReadOptionalAttributeValue(ByRef Attr) + Dim Instance, Value, Number + QPReadOptionalAttributeValue = "" + On Error Resume Next + Err.Clear + Instance = CStr(Attr.InstanceValue) + Number = Err.Number + Err.Clear + If Number = 0 And Len(CStr(Instance)) > 0 Then + QPReadOptionalAttributeValue = Instance + On Error GoTo 0 + Exit Function + End If + Err.Clear + Value = CStr(Attr.Value) + Number = Err.Number + Err.Clear + If Number = 0 Then QPReadOptionalAttributeValue = CStr(Value) + On Error GoTo 0 End Function Sub QPVerifyUntouched(ByRef Records) @@ -1409,7 +1515,7 @@ Sub QPVerifyUntouched(ByRef Records) Set Seen = CreateObject("Scripting.Dictionary") For Each Comp In Components Total = Total + 1 - Uid = CStr(Comp.UID) + Uid = QPComponentIdentityKey(Comp) If Seen.Exists(Uid) Then Err.Raise vbObjectError + 874, _ "QuickPartReplace.Target", "duplicate component UID" Seen.Add Uid, True @@ -1471,13 +1577,15 @@ Function QPSnapshotComponent(ByRef Comp) Dim ErrorNumber Dim ErrorSource Dim ErrorDescription - Dim Uid, Pins + Dim Uid, Identity, Pins Uid = CStr(Comp.UID) + Identity = Uid + If Len(Trim(Uid)) = 0 Then Identity = "@REFDES|" & Trim(CStr(Comp.Refdes)) Set Point = Comp.GetLocation() Set Pins = QPComponentPins(Uid) Connections = QPConnectionSignatureFromPins(Uid, Pins) - Result = "C" & vbTab & QPEncode(Uid) & vbTab & _ + Result = "C" & vbTab & QPEncode(Identity) & vbTab & _ QPEncode(CStr(Comp.Refdes)) & vbTab & QPEncode(CStr(Point.X)) & _ vbTab & QPEncode(CStr(Point.Y)) & vbTab & _ QPEncode(CStr(Comp.Orientation)) & vbTab & QPEncode(CStr(Comp.Scale)) & _ @@ -1488,7 +1596,7 @@ Function QPSnapshotComponent(ByRef Comp) AttributeText = "" AttributeCount = 0 For Each Attr In Comp.Attributes - AttributeText = AttributeText & QPSnapshotAttribute(Uid, Attr) + AttributeText = AttributeText & QPSnapshotAttribute(Identity, Attr) If Err.Number <> 0 Then Exit For AttributeCount = AttributeCount + 1 QPProgress "snapshot_attributes", AttributeCount, 0 @@ -1530,7 +1638,7 @@ Function QPSortSnapshotLines(ByVal Text) End Function Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr) - Dim Instance, Point, ErrorNumber, ErrorDescription + Dim Instance, Value, Point, ErrorNumber, ErrorDescription Dim LocationX Dim LocationY Dim Color @@ -1539,24 +1647,40 @@ Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr) Dim Orientation Dim Visible - Instance = Attr.InstanceValue + Instance = "" + Value = "" On Error Resume Next + Err.Clear + Instance = CStr(Attr.InstanceValue) + If Err.Number <> 0 Then + Instance = "" + QPLog QP_Root, "WARN", "optional_attribute_unavailable field=instance_value" + End If + Err.Clear + Value = CStr(Attr.Value) + If Err.Number <> 0 Then + Value = "" + QPLog QP_Root, "WARN", "optional_attribute_unavailable field=value" + End If + Err.Clear Set Point = Attr.GetLocation() - LocationX = CStr(Point.X) - LocationY = CStr(Point.Y) - ErrorNumber = Err.Number - ErrorDescription = Err.Description + If Err.Number = 0 And Not Point Is Nothing Then + LocationX = CStr(Point.X) + LocationY = CStr(Point.Y) + Else + LocationX = "0" + LocationY = "0" + QPLog QP_Root, "WARN", "optional_display_field_unavailable field=location" + End If Err.Clear On Error GoTo 0 - If ErrorNumber <> 0 Then Err.Raise ErrorNumber, "QuickPartReplace.Snapshot", _ - "Cannot capture " & CStr(Attr.Name) & ".location: " & ErrorDescription Color = QPOptionalAttributeField(Attr, "color") Font = QPOptionalAttributeField(Attr, "font") Size = QPOptionalAttributeField(Attr, "size") Orientation = QPOptionalAttributeField(Attr, "orientation") Visible = QPOptionalAttributeField(Attr, "visible") QPSnapshotAttribute = "A" & vbTab & QPEncode(ComponentUid) & vbTab & _ - QPEncode(CStr(Attr.Name)) & vbTab & QPEncode(CStr(Attr.Value)) & _ + QPEncode(CStr(Attr.Name)) & vbTab & QPEncode(Value) & _ vbTab & QPEncode(CStr(Instance)) & vbTab & QPEncode(LocationX) & _ vbTab & QPEncode(LocationY) & vbTab & QPEncode(Color) & _ vbTab & QPEncode(Font) & vbTab & QPEncode(Size) & vbTab & _ @@ -1598,8 +1722,9 @@ Function QPOptionalAttributeField(ByRef Attr, ByVal FieldName) Err.Clear On Error GoTo 0 If ErrorNumber <> 0 Then - Err.Raise ErrorNumber, "QuickPartReplace.Snapshot", _ - "Cannot capture " & CStr(Attr.Name) & "." & FieldName & ": " & ErrorDescription + QPOptionalAttributeField = "0" + QPLog QP_Root, "WARN", "optional_display_field_unavailable field=" & CStr(FieldName) & _ + " description=" & CStr(ErrorDescription) Else QPOptionalAttributeField = Value End If @@ -2597,8 +2722,16 @@ Function QPFindCurrentAttribute(ByRef Comp, ByVal Name) End Function Function QPRequiredAttributeValue(ByRef Comp, ByVal Name) - Dim Attr - Set Attr = QPFindCurrentAttribute(Comp, Name) + Dim Attr, ErrorNumber, ErrorSource, ErrorDescription + On Error Resume Next + Err.Clear + Set Attr = Comp.FindAttribute(CStr(Name)) + ErrorNumber = Err.Number + ErrorSource = Err.Source + ErrorDescription = Err.Description + Err.Clear + On Error GoTo 0 + If ErrorNumber <> 0 Then Err.Raise ErrorNumber, ErrorSource, ErrorDescription If Attr Is Nothing Then Err.Raise vbObjectError + 806, "QuickPartReplace", _ "required attribute missing: " & CStr(Name) diff --git a/src/dx_productivity/trial_task_center/batch_supervisor.py b/src/dx_productivity/trial_task_center/batch_supervisor.py index 1649aac..0a042a6 100644 --- a/src/dx_productivity/trial_task_center/batch_supervisor.py +++ b/src/dx_productivity/trial_task_center/batch_supervisor.py @@ -11,6 +11,7 @@ import subprocess import sys import threading import time +from collections import deque from uuid import uuid4 from .errors import TaskCenterError @@ -59,23 +60,37 @@ def _worker_context(root): def _force_owned_supervisor(root, journal, process): """Persist uncertainty before terminating the exact process/Job we own.""" + from .batch_resources import write_record + path = Path(root) / 'batch-resources' / journal['batch_id'] / 'supervisor.json' + if journal.get('force_state') in {'termination_requested', 'confirmed'}: + return journal['stop_mode'] = 'forced' - journal['force_requested_at'] = time.time() + journal.setdefault('force_requested_at', time.time()) + journal['force_state'] = 'requested' + write_record(path, journal) # The running worker can cross a task boundary while evidence is written. # Protect all possible targets before force; keep their saved results intact. journal['protect_all_targets'] = True - _mark_interrupted(root, journal, '用户明确强制结束;写入与保存结果须核验,禁止自动重放。') + try: + _mark_interrupted(root, journal, '用户明确强制结束;写入与保存结果须核验,禁止自动重放。') + except Exception as error: + journal.update(state='blocked', force_state='protection_failed', force_error=str(error)) + try: + write_record(path, journal) + except OSError: + pass # The durable requested state still blocks a later replay. + raise try: process.terminate() except Exception as error: if not getattr(process, 'cleanup_record', {}).get('forced_termination'): raise journal['force_cleanup_error'] = str(error) - try: - from .batch_resources import write_record - write_record(Path(root) / 'batch-resources' / journal['batch_id'] / 'supervisor.json', journal) - except OSError: - pass # The forced intent and uncertainty were already durably saved. + journal['force_state'] = 'termination_requested' + try: + write_record(path, journal) + except OSError: + pass # Intent and uncertainty already exist; exit still requires confirmation. def _retain_supervisor_after_control_error(root, journal, process, control, error): @@ -290,29 +305,53 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres 'interactive_stop': isinstance(stop_event, StopControl)}) journal['request_sha256'] = hashlib.sha256(internal_path(request).read_bytes()).hexdigest() write_record(directory / 'supervisor.json', journal) + from .supervisor_evidence import LifecycleEvidence + lifecycle = LifecycleEvidence(directory) + lifecycle.try_append('supervisor_starting', operation=operation) gate, messages = MessageGate(batch_id, attempt_id), queue.Queue() api, process, reader = _Win32Api(), None, None + stderr_reader = None + stdout_tail, stderr_tail = deque(maxlen=65536), deque(maxlen=65536) sent_cancel, last_heartbeat = False, time.monotonic() transport_error = None + ownership_cleanup_error = None force_accepted = False + last_message = None def receive(): try: while line := process.stdout.readline(1024 * 1024): + if isinstance(line, bytes): + stdout_tail.extend(line.decode('utf-8', errors='replace')) + else: + stdout_tail.extend(str(line)) try: messages.put(json.loads(line)) except (ValueError, UnicodeError): messages.put({'type': 'invalid'}) finally: messages.put(None) + def receive_stderr(): + if process is None or process.stderr is None: + return + while True: + line = process.stderr.readline(1024 * 1024) + if not line: + return + if isinstance(line, bytes): + stderr_tail.extend(line.decode('utf-8', errors='replace')) + else: + stderr_tail.extend(str(line)) try: # A normal input-desktop supervisor owns its private-desktop children. # Only the supervisor, never the GUI, creates a private desktop. process = api.create_process(_command(internal_path(request)), None, legacy_path(root), - _environment(), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + _environment(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) journal.update(state='running', process=process_identity(process)) write_record(directory / 'supervisor.json', journal) reader = threading.Thread(target=receive, daemon=True) reader.start() + stderr_reader = threading.Thread(target=receive_stderr, daemon=True) + stderr_reader.start() while process.poll() is None or not messages.empty(): force = getattr(stop_event, 'force_event', None) if force is not None and force.is_set() and process.poll() is None: @@ -328,7 +367,8 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres if stop_event is not None and stop_event.is_set() and not sent_cancel: try: write_record(directory / 'cancel.json', {'version': VERSION, 'batch_id': batch_id, 'attempt_id': attempt_id}) - journal.update(stop_mode='safe', stop_requested_at=time.time()) + journal.setdefault('stop_mode', 'safe') + journal.setdefault('stop_requested_at', time.time()) write_record(directory / 'supervisor.json', journal) sent_cancel = True except OSError as error: @@ -343,6 +383,7 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres continue if value is None or not gate.accept(value): continue + last_message = value last_heartbeat = time.monotonic() if value['type'] == 'progress' and on_progress is not None: on_progress(value['payload']) @@ -372,20 +413,57 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres process.close() # Waits, and confines termination to the owned Job. api.close_pending() except Exception as error: + ownership_cleanup_error = error if process is not None: _LIVE_SUPERVISORS[directory / 'supervisor.json'] = (api, process) transport_error = error if transport_error is None else TaskCenterError( 'batch_resources_pending', f'监督进程错误:{transport_error};收尾错误:{error}') if reader is not None: reader.join(3) + if stderr_reader is not None: + stderr_reader.join(3) + from .supervisor_evidence import write_process_evidence + try: + write_process_evidence(directory, exit_code=getattr(process, 'returncode', None), + stdout=''.join(stdout_tail), stderr=''.join(stderr_tail), + cleanup=getattr(process, 'cleanup_record', None), last_message=last_message) + except OSError as evidence_error: + journal['diagnostic_write_error'] = str(evidence_error) + service._logging.logger.error('supervisor_evidence_write_failed batch_id=%s error=%s', + batch_id, evidence_error) + lifecycle.try_append('supervisor_exit', + exit_code=getattr(process, 'returncode', None), + stdout_chars=len(stdout_tail), stderr_chars=len(stderr_tail)) if force_accepted: from .batch_resources import cleanup_pending_resources try: - cleanup_pending_resources(root, logging_context=service._logging) + pending = cleanup_pending_resources(root, logging_context=service._logging) + if pending: + ownership_cleanup_error = TaskCenterError('batch_resources_pending', '强制结束后的资源仍待清理。') except Exception as error: + ownership_cleanup_error = error service._logging.logger.error('forced_stop_cleanup_pending batch_id=%s error=%s', batch_id, error) - raise TaskCenterError('user_forced_stop', + failure = TaskCenterError('user_forced_stop', '已按确认强制结束;已保存的执行证据保留,当前结果待核验。请先核验恢复,不要重复替换。') + failure.primary_error = transport_error + if ownership_cleanup_error is not None: + journal.update(force_state='confirmation_failed', force_cleanup_error=str(ownership_cleanup_error)) + else: + journal.update(force_state='confirmed', force_confirmed_at=time.time()) + try: + write_record(directory / 'supervisor.json', journal) + except OSError as persistence_error: + if ownership_cleanup_error is not None: + combined = TaskCenterError('batch_resources_pending', + f'退出确认异常:{ownership_cleanup_error};确认记录写入失败:{persistence_error}') + combined.primary_error, combined.cleanup_error = ownership_cleanup_error, persistence_error + ownership_cleanup_error = combined + else: + ownership_cleanup_error = persistence_error + if ownership_cleanup_error is not None: + from .supervisor_result import resource_pending_error + raise resource_pending_error(operation, None, ownership_cleanup_error, failure) + raise failure try: try: response = json.loads(internal_path(directory / 'result.json').read_text(encoding='utf-8')) @@ -404,13 +482,30 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres from .supervisor_result import validated_outcome, resource_pending_error decoded_result, decoded_error, decoded_primary = validated_outcome(operation, response['payload']) except (OSError, ValueError, KeyError, TypeError, AttributeError) as error: - _mark_interrupted(root, journal, transport_error or error) + from .supervisor_evidence import MissingSupervisorResult, InvalidSupervisorResult + failure_type = MissingSupervisorResult if isinstance(error, FileNotFoundError) else InvalidSupervisorResult + cause = transport_error or failure_type(exit_code=getattr(process, 'returncode', None), + stdout_tail=''.join(stdout_tail), stderr_tail=''.join(stderr_tail)) + if process is None and transport_error is not None: + cause = TaskCenterError('worker_start_failed', '后台进程启动失败,未开始业务执行:' + str(transport_error)) + try: + _mark_interrupted(root, journal, cause) + except OSError as protection_error: + journal.update(state='blocked', outcome='interrupted', protection_error=str(protection_error)) + try: + write_record(directory / 'supervisor.json', journal) + except OSError: + service._logging.logger.exception('supervisor_protection_record_failed batch_id=%s', batch_id) + failure = TaskCenterError('interruption_protection_failed', + '后台进程结果不确定,且中断保护写入失败;已阻止继续执行,请保留诊断并核验工程。') + failure.primary_error, failure.cleanup_error = cause, protection_error + raise failure from protection_error # Recheck physical resources even when final business results are # missing; this never authorizes repeating the interrupted job. from .batch_resources import cleanup_pending_resources cleanup_pending_resources(root, logging_context=service._logging) - failure = TaskCenterError('supervisor_interrupted', - '批次监督进程未留下可信结果;保留保护记录,不自动重试。' + + failure = cause if isinstance(cause, TaskCenterError) else TaskCenterError( + 'supervisor_interrupted', '批次监督进程未留下可信结果;保留保护记录,不自动重试。' + (f' 首发原因:{transport_error}' if transport_error else '')) failure.primary_error = transport_error or error raise failure from (transport_error or error) diff --git a/src/dx_productivity/trial_task_center/batch_worker.py b/src/dx_productivity/trial_task_center/batch_worker.py index 0468406..a5db39c 100644 --- a/src/dx_productivity/trial_task_center/batch_worker.py +++ b/src/dx_productivity/trial_task_center/batch_worker.py @@ -51,7 +51,7 @@ def _admit_request(directory, request, request_bytes): time.sleep(.02) own = process_identity(type('WorkerIdentity', (), {'pid': os.getpid(), 'handle': win32api.GetCurrentProcess()})()) if journal['process'] != own: - raise ValueError('Supervisor request belongs to another process') + raise ValueError(f'Supervisor request belongs to another process: expected={journal["process"]!r} actual={own!r}') # Atomic exclusive publication makes requests one-shot, including retained # requests invoked manually or racing a delayed/duplicate launch. with internal_path(directory / 'claim.json').open('x', encoding='utf-8') as stream: @@ -63,11 +63,6 @@ def _admit_request(directory, request, request_bytes): def run(request_path): from .batch_resources import write_record - from .batch_runtime import BatchCadRuntime - from .batch_supervisor import _worker_context - from .logging_setup import configure_logging - from .service import TaskCenterService - from .store import TaskStore output, owns_output = _output_stream() request_path = canonical_path(request_path, strict=True) @@ -79,9 +74,17 @@ def run(request_path): raise ValueError('Invalid batch supervisor request') root = canonical_path(request['root']) directory = root / 'batch-resources' / batch_id + from .supervisor_evidence import LifecycleEvidence + evidence = LifecycleEvidence(directory) if request_path != directory / 'request.json': raise ValueError('Supervisor request ownership path mismatch') _admit_request(directory, request, request_bytes) + evidence.try_append('worker_started', pid=os.getpid()) + from .batch_runtime import BatchCadRuntime + from .batch_supervisor import _worker_context + from .logging_setup import configure_logging + from .service import TaskCenterService + from .store import TaskStore _worker_context(root) from .stop_control import StopControl stop = StopControl() if request.get('interactive_stop', False) else threading.Event() @@ -128,6 +131,7 @@ def run(request_path): thread.start() response = {'business_succeeded': False, 'status': 'error'} try: + evidence.try_append('business_started', operation=request['operation']) arguments = decode(request['arguments']) operation = request['operation'] def progress(message): @@ -166,6 +170,7 @@ def run(request_path): else: result = getattr(service, operation)(**arguments, runtime=runtime) runtime.result = result + evidence.try_append('business_finished', operation=operation, succeeded=True) response.update(status='ok', business_succeeded=True, result=encode(result)) write_record(directory / 'business-result.json', {'version': VERSION, 'batch_id': batch_id, 'attempt_id': attempt_id, 'generation': max(1, runtime.generation), 'payload': response}) @@ -173,16 +178,21 @@ def run(request_path): from .bom_import_state import BomImportPhase phase(BomImportPhase.RELEASING, '本批位号核验结束,正在释放后台资源…') except Exception as error: + evidence.try_append('business_failed', operation=operation, error=type(error).__name__) response['operation_error'] = encode_error(error) raise + finally: + evidence.try_append('resources_releasing') except Exception as error: context.report_exception('batch_supervisor', error) response.update(status='error', error=encode_error(error)) finally: + evidence.try_append('worker_closing', status=response.get('status'), business_succeeded=response.get('business_succeeded')) finished.set() thread.join(3) store.close() emit('result', response, persist=True) + evidence.try_append('result_committed', status=response.get('status'), business_succeeded=response.get('business_succeeded')) if owns_output: output.close() return 0 diff --git a/src/dx_productivity/trial_task_center/diagnostics_bundle.py b/src/dx_productivity/trial_task_center/diagnostics_bundle.py index 12d60da..1828380 100644 --- a/src/dx_productivity/trial_task_center/diagnostics_bundle.py +++ b/src/dx_productivity/trial_task_center/diagnostics_bundle.py @@ -14,7 +14,8 @@ MAX_FILE_BYTES = 4 * 1024 * 1024 MAX_BUNDLE_BYTES = 32 * 1024 * 1024 MAX_FILES = 512 _BATCH_NAMES = {'session.json', 'supervisor.json', 'request.json', 'result.json', - 'business-result.json', 'supervisor.log', 'resource-release.log', 'cancel.json'} + 'business-result.json', 'supervisor.log', 'resource-release.log', 'cancel.json', + 'process.json', 'lifecycle.jsonl'} _QUICK_NAMES = {'private-desktop.json', 'checkpoint.json', 'status.json', 'uncertain.flag', 'resolved-uncertain.flag', 'protection.json', 'cleanup.json', 'project-session.json', 'project-checkpoint.json', 'status.tsv'} diff --git a/src/dx_productivity/trial_task_center/isolated_desktop.py b/src/dx_productivity/trial_task_center/isolated_desktop.py index 978119f..db4fcb4 100644 --- a/src/dx_productivity/trial_task_center/isolated_desktop.py +++ b/src/dx_productivity/trial_task_center/isolated_desktop.py @@ -15,6 +15,7 @@ import math import os from pathlib import Path import subprocess +import sys import time from threading import get_native_id from typing import Mapping, Sequence @@ -125,6 +126,15 @@ class _Win32Api: def create_process(self, command: Sequence[str], desktop: str, cwd: Path | None, env: Mapping[str, str] | None, *, stdout=None, stderr=None): + # Windows venv python.exe redirects to a second process. Launch the + # interpreter directly with its venv identity, so the handle we own is + # also the process that admits the request and creates desktop windows. + if (not getattr(sys, 'frozen', False) and sys.prefix != sys.base_prefix + and os.path.normcase(os.path.abspath(str(command[0]))) == os.path.normcase(sys.executable) + and getattr(sys, '_base_executable', None)): + command = (sys._base_executable, *command[1:]) + env = dict(os.environ if env is None else env) + env['__PYVENV_LAUNCHER__'] = sys.executable startup = self.process.STARTUPINFO() startup.lpDesktop = desktop flags = self.con.CREATE_NO_WINDOW | self.con.CREATE_SUSPENDED @@ -148,6 +158,9 @@ class _Win32Api: process.thread = thread self.job.AssignProcessToJobObject(job, handle) process.job_assigned = True + if not self.job.IsProcessInJob(handle, job): + raise TaskCenterError('private_desktop', 'Owned process Job membership was not confirmed') + process.cleanup_record['job_membership_verified'] = True self.process.ResumeThread(thread) thread.Close() process.thread = None diff --git a/src/dx_productivity/trial_task_center/native_helper.py b/src/dx_productivity/trial_task_center/native_helper.py index 86e7fef..14292b0 100644 --- a/src/dx_productivity/trial_task_center/native_helper.py +++ b/src/dx_productivity/trial_task_center/native_helper.py @@ -87,6 +87,8 @@ def read_snapshot(path): raise ValueError("Invalid snapshot Value or NC option") if any(len(row) != 8 for row in components): raise ValueError("Invalid component snapshot") + if any(not row[1].strip() or not row[2].strip() for row in components): + raise ValueError("Snapshot component UID or Refdes is empty") if len({row[1] for row in components}) != len(components): raise ValueError("Duplicate snapshot UID") if metadata[0][2].casefold() == metadata[0][3].casefold(): @@ -346,11 +348,32 @@ def attach(): return app, api -def attribute_value(component, name): - attribute = component.FindAttribute(name) - if attribute is None: - return "" - return str(attribute.InstanceValue or attribute.Value) +def attribute_value(component, name, *, required=False, fallback=""): + """Read a CAD attribute without allowing optional COM failures to hang a run. + + Identity and source Part Number callers pass ``required=True``. Display, + baseline and optional policy fields degrade to the supplied fallback; a + later comparison can still reject a changed or incomplete result. + """ + try: + attribute = component.FindAttribute(name) + if attribute is None: + value = fallback + else: + try: + instance = attribute.InstanceValue + except Exception: + instance = None + try: + base = attribute.Value + except Exception: + base = None + value = instance or base or fallback + return str(value) + except Exception as error: + if required: + raise ValueError(f"required attribute {name!r} is unavailable") from error + return fallback def selection(component, value): @@ -360,7 +383,22 @@ def selection(component, value): def components_on_sheet(app, api): - return [api.IVdComp(component._oleobj_) for component in app.ActiveView.Query(128, 0)] + components = [api.IVdComp(component._oleobj_) for component in app.ActiveView.Query(128, 0)] + uids = [_component_uid(component) for component in components] + if len(set(uids)) != len(uids): + raise ValueError("Duplicate component UID on sheet") + return components + + +def _component_uid(component): + value = getattr(component, 'UID', None) + uid = '' if value is None else str(value).strip() + if uid: + return uid + refdes = str(getattr(component, 'Refdes', '') or '').strip() + if not refdes: + raise ValueError("Component UID and Refdes are empty; cannot safely identify the target") + return '@REFDES|' + refdes def check_active_context(app, metadata): @@ -376,7 +414,7 @@ def check_context(app, api, metadata, records, require_selection=True): check_active_context(app, metadata) components = components_on_sheet(app, api) wanted = {row[1] for row in records} - by_uid = {str(component.UID): component for component in components} + by_uid = {_component_uid(component): component for component in components} if len(by_uid) != len(components): raise ValueError("Duplicate component UID on sheet") if len(metadata) == 10: @@ -386,15 +424,16 @@ def check_context(app, api, metadata, records, require_selection=True): for uid, (refdes, old_value) in selectors.items(): component = by_uid.get(uid) if (component is None or str(component.Refdes) != refdes or - attribute_value(component, "Part Number").casefold() != metadata[2].casefold() or + attribute_value(component, "Part Number", required=True).casefold() != metadata[2].casefold() or attribute_value(component, "Value") != old_value): raise ValueError("Selected target changed since inventory") else: - actual = {str(component.UID) for component in components if attribute_value(component, "Part Number").casefold() == metadata[2].casefold()} + actual = {_component_uid(component) for component in components if attribute_value(component, "Part Number", required=True).casefold() == metadata[2].casefold()} if actual != wanted: raise ValueError(f"Source batch changed since snapshot: expected={sorted(wanted)!r} actual={sorted(actual)!r}") if require_selection: - selected = {str(api.IVdComp(component._oleobj_).UID) for component in app.ActiveView.Query(128, 1)} + selected = {_component_uid(api.IVdComp(component._oleobj_)) + for component in app.ActiveView.Query(128, 1)} if selected != wanted: raise ValueError(f"Selected component set differs from snapshot: expected={sorted(wanted)!r} actual={sorted(selected)!r}") return components @@ -424,7 +463,7 @@ def verify_unselected_snapshot(snapshot, components, selected_uids): raise ValueError("Duplicate unselected component UID") current = {} for component in components: - uid = str(component.UID) + uid = _component_uid(component) if uid in selected_uids: continue current[uid] = (str(component.Refdes), *(attribute_value(component, name) @@ -438,7 +477,7 @@ def select_snapshot_batch(app, api, metadata, records): components = check_context(app, api, metadata, records, require_selection=False) wanted = {row[1] for row in records} for component in components: - selection(component, str(component.UID) in wanted) + selection(component, _component_uid(component) in wanted) return check_context(app, api, metadata, records) @@ -460,7 +499,7 @@ def promote_pin_numbers(app, api, replacement_uids, slot_count=1, on_progress=No try: progress('select_replacements') for component in components: - selection(component, str(component.UID) in replacement_uids) + selection(component, _component_uid(component) in replacement_uids) for slot in range(1, int(slot_count) + 1): progress(f'promote_slot_{slot}') app.ActiveView.Block.PromoteSymbolNumbers(1, slot) @@ -468,10 +507,10 @@ def promote_pin_numbers(app, api, replacement_uids, slot_count=1, on_progress=No promoted = [] for item in app.ActiveView.Query(4096, 0): pin = api.IVdCmpPin(item._oleobj_) - if str(pin.Component.UID) in replacement_uids: + if _component_uid(pin.Component) in replacement_uids: if not str(pin.Number).strip(): raise ValueError("Native target-library pin number promotion failed") - promoted.append({"uid": str(pin.Component.UID), "number": str(pin.Number)}) + promoted.append({"uid": _component_uid(pin.Component), "number": str(pin.Number)}) if not promoted: raise ValueError("Replacement produced no pins") return promoted @@ -488,7 +527,7 @@ def _promotion_phase(snapshot, report, phase): def replacement_matches(components, records, target, require_part_label, *, library_row=None): - current_by_uid = {str(component.UID): component for component in components} + current_by_uid = {_component_uid(component): component for component in components} if len(current_by_uid) != len(components): raise ValueError('Duplicate component UID on sheet') expected_name = expected_label = None @@ -502,15 +541,15 @@ def replacement_matches(components, records, target, require_part_label, *, libr candidate = current_by_uid.get(record[1]) if candidate is None: raise ValueError('Native replacement lost component UID identity') - if attribute_value(candidate, "Part Number").casefold() != target.casefold(): + if attribute_value(candidate, "Part Number", required=True).casefold() != target.casefold(): return None identity = str(candidate.Refdes).strip() or f"UID={candidate.UID}" - part_name = attribute_value(candidate, 'Part Name') + part_name = attribute_value(candidate, 'Part Name', required=True) if not part_name: raise ValueError( f"Native replacement Part Name readback failed; refdes={identity}; uid={candidate.UID}" ) - part_label = attribute_value(candidate, 'Part Label') if require_part_label or expected_label else '' + part_label = attribute_value(candidate, 'Part Label', required=require_part_label or bool(expected_label)) if require_part_label or expected_label else '' if require_part_label and not part_label: raise ValueError( f"Native replacement Part Label readback failed; refdes={identity}; uid={candidate.UID}; " @@ -533,13 +572,13 @@ def verify_replacement_readback(snapshot, components, metadata, records, before_ # The persisted U baseline already covers exactly these identity fields. verify_unselected_snapshot(snapshot, components, {row[1] for row in records}) else: - current = {str(component.UID): component for component in components} + current = {_component_uid(component): component for component in components} for uid, identity in untouched.items(): component = current.get(uid) actual = None if component is None else ( str(component.Refdes), *(attribute_value(component, name) for name in ('Part Number', 'Value', 'Part Name', 'Part Label'))) - if actual != identity: + if actual != identity: raise ValueError('An unselected component was changed') verified = replacement_matches(components, records, metadata[3], require_part_label, library_row=library_row) @@ -566,9 +605,9 @@ def run_native(snapshot, report): if len(metadata) == 10: verify_unselected_snapshot(snapshot, components, wanted) source_uids = { - str(component.UID) + _component_uid(component) for component in components - if str(component.UID) in wanted and attribute_value(component, "Part Number").casefold() == metadata[2].casefold() + if _component_uid(component) in wanted and attribute_value(component, "Part Number", required=True).casefold() == metadata[2].casefold() } if source_uids != wanted: binding = require_resumable_transaction(snapshot, report, metadata, records) @@ -592,11 +631,11 @@ def run_native(snapshot, report): report["stage"] = "native_replace_verified" return components = select_snapshot_batch(app, api, metadata, records) - before_uids = {str(component.UID) for component in components} - untouched = None if len(metadata) == 10 else {str(component.UID): ( + before_uids = {_component_uid(component) for component in components} + untouched = None if len(metadata) == 10 else {_component_uid(component): ( str(component.Refdes), *(attribute_value(component, name) for name in ("Part Number", "Value", "Part Name", "Part Label")) - ) for component in components if str(component.UID) not in wanted} + ) for component in components if _component_uid(component) not in wanted} view_handle = int(app.ActiveViewHandle) process_id = win32process.GetWindowThreadProcessId(view_handle)[1] main_handle = win32gui.GetAncestor(view_handle, 2) @@ -733,7 +772,7 @@ def run_native(snapshot, report): ready = wait_for_native_target(read_target, metadata[3], columns[2], slot_count) components = check_context(app, api, metadata, records, require_selection=False) for component in components: - selection(component, str(component.UID) in wanted) + selection(component, _component_uid(component) in wanted) for identifier in (1704, 1105, 1697, 1700): check_radio(dialog, identifier) check_context(app, api, metadata, records) diff --git a/src/dx_productivity/trial_task_center/supervisor_evidence.py b/src/dx_productivity/trial_task_center/supervisor_evidence.py new file mode 100644 index 0000000..872fe8c --- /dev/null +++ b/src/dx_productivity/trial_task_center/supervisor_evidence.py @@ -0,0 +1,106 @@ +"""Durable, bounded evidence for one supervised batch attempt.""" +from __future__ import annotations + +from datetime import datetime, timezone +import json +import os +from pathlib import Path +import threading + +from .errors import TaskCenterError +from .path_access import internal_path + + +def _atomic_json(path: Path, value: dict) -> None: + from .batch_resources import write_record + write_record(path, value) + + +def _tail(value: str, limit: int) -> str: + value = str(value or '') + limit = max(0, int(limit)) + return value.encode('utf-8')[-limit:].decode('utf-8', errors='ignore') if limit else '' + + +class LifecycleEvidence: + def __init__(self, directory: Path): + self.directory = internal_path(directory) + self.path = self.directory / 'lifecycle.jsonl' + self._lock = threading.Lock() + + def append(self, event: str, **fields) -> dict: + row = {'at': datetime.now(timezone.utc).isoformat(), 'event': str(event), **fields} + with self._lock: + self.directory.mkdir(parents=True, exist_ok=True) + with self.path.open('a', encoding='utf-8', newline='') as stream: + stream.write(json.dumps(row, ensure_ascii=True, allow_nan=False, sort_keys=True) + '\n') + stream.flush() + os.fsync(stream.fileno()) + return row + + def events(self) -> tuple[dict, ...]: + try: + lines = self.path.read_text(encoding='utf-8').splitlines() + except FileNotFoundError: + return () + rows = [] + for line in lines: + try: + row = json.loads(line) + if isinstance(row, dict): + rows.append(row) + except ValueError: + continue # A hard exit may leave an incomplete last append. + return tuple(rows) + + def try_append(self, event: str, **fields) -> str: + """Supplemental tracing must not replace business or protection evidence.""" + try: + self.append(event, **fields) + except OSError as error: + return str(error) + return '' + + +def write_process_evidence(directory: Path, *, exit_code, stdout='', stderr='', limit=65536, + cleanup=None, last_message=None) -> dict: + value = {'exit_code': exit_code, 'stdout_tail': _tail(stdout, limit), 'stderr_tail': _tail(stderr, limit), + 'cleanup': cleanup, 'last_message': last_message, + 'recorded_at': datetime.now(timezone.utc).isoformat()} + _atomic_json(internal_path(directory) / 'process.json', value) + return value + + +class SupervisorResultEvidenceError(TaskCenterError): + code_name = 'worker_result_missing' + description = '后台进程未留下可信结果' + def __init__(self, *, exit_code=None, stdout_tail='', stderr_tail=''): + stdout_tail = _tail(stdout_tail if isinstance(stdout_tail, str) else '', 65536) + stderr_tail = _tail(stderr_tail if isinstance(stderr_tail, str) else '', 65536) + self.exit_code = exit_code + self.stdout_tail = stdout_tail + self.stderr_tail = stderr_tail + detail = f'{self.description};退出码={exit_code!r}。保留保护记录,请先核验恢复,禁止自动重放。' + if stderr_tail: + detail += f'; stderr={stderr_tail[-512:]}' + super().__init__(self.code_name, detail) + + @classmethod + def from_directory(cls, directory: Path): + try: + value = json.loads((internal_path(directory) / 'process.json').read_text(encoding='utf-8')) + except (FileNotFoundError, OSError, ValueError, TypeError): + value = {} + if not isinstance(value, dict): + value = {} + return cls(exit_code=value.get('exit_code'), stdout_tail=value.get('stdout_tail', ''), + stderr_tail=value.get('stderr_tail', '')) + + +class MissingSupervisorResult(SupervisorResultEvidenceError): + """No committed result survived the worker exit.""" + + +class InvalidSupervisorResult(SupervisorResultEvidenceError): + code_name = 'worker_result_invalid' + description = '后台进程结果损坏或身份校验不通过' diff --git a/tests/integration/test_automatic_replacement_regression.py b/tests/integration/test_automatic_replacement_regression.py index a35c32b..70fb0f7 100644 --- a/tests/integration/test_automatic_replacement_regression.py +++ b/tests/integration/test_automatic_replacement_regression.py @@ -180,7 +180,7 @@ class NativeTests(unittest.TestCase): component = type("Component", (), {"UID": "uid", "Refdes": "R1"})() records = [["C", "uid", "R1"]] - def value(_component, name): + def value(_component, name, *, required=False, fallback=""): return {"Part Number": "NEW", "Part Name": "RES", "Part Label": ""}[name] with patch.object(native, "attribute_value", side_effect=value): diff --git a/tests/integration/test_quick_part_replace_contract.py b/tests/integration/test_quick_part_replace_contract.py index 9b96ed0..525aa8c 100644 --- a/tests/integration/test_quick_part_replace_contract.py +++ b/tests/integration/test_quick_part_replace_contract.py @@ -117,7 +117,7 @@ def test_quick_part_replace_prioritizes_recovery_and_preflights_before_writes() assert '"hard_failure"' in text assert "multiple awaiting_native_replace rules" in text assert text.index("QPRestorePreflight") < text.index("Comp.Refdes =") - assert "Cannot capture " in text + assert "optional_display_field_unavailable field=" in text assert "QPOptionalAttributeField" in text assert "value_style_defaulted" in text assert 'QP_App.ExecuteCommand("undo")' in text diff --git a/tests/ui/test_ui_execution_feedback.py b/tests/ui/test_ui_execution_feedback.py index 2ce3a5c..bd38162 100644 --- a/tests/ui/test_ui_execution_feedback.py +++ b/tests/ui/test_ui_execution_feedback.py @@ -1,5 +1,6 @@ """UI log and feedback coverage, collectable alongside the service feedback tests.""" import json +import pytest from dx_productivity.trial_task_center.errors import TaskCenterError from dx_productivity.trial_task_center.ui.messages import ( _aligned_log_line, _format_task_log_lines, _nc_sync_log_lines, @@ -54,6 +55,24 @@ def test_preflight_error_keeps_reason_and_stable_id_in_operator_feedback(ui): assert error.error_id in ui.window._log_presenter.log_text +@pytest.mark.parametrize('kind', ['missing', 'invalid', 'forced', 'resources']) +def test_supervisor_failure_feedback_keeps_support_id_and_recovery_instruction(ui, kind): + from dx_productivity.trial_task_center.supervisor_evidence import MissingSupervisorResult, InvalidSupervisorResult + from dx_productivity.trial_task_center.batch_resources import BatchResourceCleanupError + errors = { + 'missing': MissingSupervisorResult(exit_code=79, stderr_tail='worker failed'), + 'invalid': InvalidSupervisorResult(exit_code=0), + 'forced': TaskCenterError('user_forced_stop', '已强制结束,请先核验恢复。'), + 'resources': BatchResourceCleanupError('资源确认失败,请重新清理资源。'), + } + error = errors[kind] + ui.window._show_error(error) + text = ui.window.run_panel.operator_band.text() + assert error.error_id in text + assert error.message in text + assert error.error_id in ui.window._log_presenter.log_text + + def test_running_feedback_names_owner_and_keeps_navigation_available(ui,qtbot): from threading import Event release,started=Event(),Event() diff --git a/tests/v1_1/test_accuracy_vbs_guards.py b/tests/v1_1/test_accuracy_vbs_guards.py index 2c1a40b..c6e84fb 100644 --- a/tests/v1_1/test_accuracy_vbs_guards.py +++ b/tests/v1_1/test_accuracy_vbs_guards.py @@ -22,7 +22,11 @@ def routine(name): def run_vbs(tmp_path, names, body, prelude=''): script = tmp_path / 'guard.vbs' - script.write_text(prelude + '\n' + '\n'.join(routine(name) for name in names) + '\n' + body, + names = tuple(names) + support = tuple(name for name in ('QPReadOptionalAttributeValue', 'QPRequiredAttributeValue', + 'QPReadableAttributeValue', 'QPEffectiveAttributeValue', + 'QPComponentIdentityKey') if name not in names) + script.write_text(prelude + '\n' + '\n'.join(routine(name) for name in names + support) + '\n' + body, encoding='ascii') result = subprocess.run(['cscript.exe', '//nologo', str(script)], capture_output=True, text=True, timeout=15) @@ -69,6 +73,41 @@ End Class ''') == 'True' +@pytest.mark.parametrize(('uid', 'refdes', 'expected'), [ + ('', 'R1', '@REFDES|R1'), + ('u1', 'R1', 'u1'), +]) +def test_missing_component_uid_uses_stable_refdes_fallback(tmp_path, uid, refdes, expected): + output = run_vbs(tmp_path, ['QPComponentIdentityKey'], f''' +Dim Comp +Set Comp = New IdentityComponent +Comp.UID = "{uid}": Comp.Refdes = "{refdes}" +On Error Resume Next +WScript.Echo QPComponentIdentityKey(Comp) +''', LOG_STUB + f''' +Class IdentityComponent + Public UID, Refdes +End Class +''') + assert output == expected + + +def test_missing_component_refdes_still_fails_fast(tmp_path): + output = run_vbs(tmp_path, ['QPComponentIdentityKey'], ''' +Dim Comp +Set Comp = New IdentityComponent +Comp.UID = "": Comp.Refdes = "" +On Error Resume Next +WScript.Echo QPComponentIdentityKey(Comp) +WScript.Echo Err.Number +''', LOG_STUB + ''' +Class IdentityComponent + Public UID, Refdes +End Class +''') + assert output.splitlines()[-1] != '0' + + def test_instance_value_com_failure_cannot_fallback_to_base_value(tmp_path): assert run_vbs(tmp_path, ['QPEffectiveAttributeValue'], ''' Dim Attr, Value @@ -89,6 +128,51 @@ End Class ''') == '1234' +def test_optional_attribute_fields_degrade_to_safe_defaults(tmp_path): + output = run_vbs(tmp_path, ['QPOptionalAttributeField', 'QPReadOptionalAttributeValue'], ''' +Dim Attr +Set Attr = New BrokenOptionalAttribute +WScript.Echo QPOptionalAttributeField(Attr, "color") +WScript.Echo QPOptionalAttributeField(Attr, "location_x") + WScript.Echo QPReadOptionalAttributeValue(Attr) +''', LOG_STUB + ''' +Class BrokenOptionalAttribute + Public Name + Private Sub Class_Initialize + Name = "Value" + End Sub + Public Property Get Color + Err.Raise 1234, "FakeCOM", "color unavailable" + End Property + Public Function GetLocation() + Err.Raise 1235, "FakeCOM", "location unavailable" + End Function + Public Property Get InstanceValue + Err.Raise 1236, "FakeCOM", "instance unavailable" + End Property + Public Property Get Value + Err.Raise 1237, "FakeCOM", "value unavailable" + End Property +End Class +''') + assert output.splitlines() == ['0', '0'] + + +def test_optional_component_label_read_failure_is_not_identity_failure(tmp_path): + output = run_vbs(tmp_path, ['QPEffectiveComponentAttribute', 'QPFindCurrentAttribute', 'QPLogField'], ''' +Dim Comp +Set Comp = New BrokenComponent +WScript.Echo QPEffectiveComponentAttribute(Comp, "Part Label") +''', LOG_STUB + ''' +Class BrokenComponent + Function FindAttribute(ByVal Name) + Err.Raise 1234, "FakeCOM", "optional label unavailable" + End Function +End Class +''') + assert output == '' + + CONNECTION_ROUTINES = ['QPConnectionsMatch', 'QPConnectionLineValid', 'QPConnectionCoordinate', 'QPConnectionNumber', 'QPConnectionNetIdentity', 'QPConnectionsMatchOffset'] @@ -331,6 +415,18 @@ WScript.Echo Replace(Text,vbCrLf,"") assert output.splitlines() == ['1', 'A\tu1\tValue\tSAME\tSAME\t0\t0\t1\t2\t3\t0\t1'] +@pytest.mark.parametrize('instance', ['', 'OVERRIDE']) +def test_snapshot_preserves_base_value_separately_from_instance_override(tmp_path, instance): + output = run_vbs(tmp_path, ['QPSnapshotAttribute', 'QPOptionalAttributeField', 'QPEncode'], f''' +Dim Attr, Text +Set Attr=New FullAttribute +Attr.Value="BASE":Attr.InstanceValue="{instance}" +Text=QPSnapshotAttribute("u1",Attr) +WScript.Echo Replace(Text,vbCrLf,"") +''', LOG_STUB + FULL_FAKE) + assert output.split('\t')[3:5] == ['BASE', instance] + + def test_unselected_identity_comes_from_the_same_complete_attribute_read(tmp_path): output = run_vbs(tmp_path, UNTOUCHED_ROUTINES, ''' Dim Comp, Attr, Text, Lines, Names, Values, Item diff --git a/tests/v1_1/test_batch_supervisor.py b/tests/v1_1/test_batch_supervisor.py index c687387..f4f4357 100644 --- a/tests/v1_1/test_batch_supervisor.py +++ b/tests/v1_1/test_batch_supervisor.py @@ -17,6 +17,16 @@ def test_supervisor_runs_in_other_process_and_releases_gate(tmp_path): assert result['pid'] != os.getpid() assert result['resource_state'] == 'not_acquired' assert_supervisor_idle(tmp_path) + directory = next((tmp_path / 'batch-resources').iterdir()) + from dx_productivity.trial_task_center.supervisor_evidence import LifecycleEvidence + stages = [row['event'] for row in LifecycleEvidence(directory).events()] + assert stages == ['supervisor_starting', 'worker_started', 'business_started', + 'business_finished', 'resources_releasing', 'worker_closing', + 'result_committed', 'supervisor_exit'] + process = json.loads((directory / 'process.json').read_text(encoding='utf-8')) + assert process['exit_code'] == 0 + assert process['cleanup']['job_active_processes'] == 0 + assert process['cleanup']['job_membership_verified'] is True finally: store.close() @@ -29,6 +39,102 @@ def test_mutation_guard_refuses_owned_launch_gate(tmp_path): assert_supervisor_idle(tmp_path) +def test_diagnostic_write_failure_does_not_erase_committed_business_result(tmp_path, monkeypatch): + from dx_productivity.trial_task_center import supervisor_evidence + from dx_productivity.trial_task_center.batch_supervisor import run_supervised + 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 + def denied(*args, **kwargs): + raise PermissionError('diagnostic write denied') + monkeypatch.setattr(supervisor_evidence, 'write_process_evidence', denied) + store = TaskStore(tmp_path / 'tasks.db') + try: + service = TaskCenterService(store, configure_logging(tmp_path)) + result = run_supervised(service, 'probe', {}) + assert result['resource_state'] == 'not_acquired' + assert list((tmp_path / 'batch-resources').glob('*/result.json')) + finally: + store.close() + + +def test_launch_failure_keeps_original_cause_and_blocks_attempt(tmp_path, monkeypatch): + from dx_productivity.trial_task_center.batch_supervisor import run_supervised + from dx_productivity.trial_task_center.isolated_desktop import _Win32Api + 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.errors import TaskCenterError + def failed(*args, **kwargs): + raise OSError('native launch denied') + monkeypatch.setattr(_Win32Api, 'create_process', failed) + store = TaskStore(tmp_path / 'tasks.db') + try: + service = TaskCenterService(store, configure_logging(tmp_path)) + with pytest.raises(TaskCenterError) as caught: + run_supervised(service, 'probe', {}) + assert caught.value.code == 'worker_start_failed' + assert 'native launch denied' in str(caught.value.primary_error) + finally: + store.close() + + +def test_corrupt_committed_result_is_not_accepted_as_success(tmp_path, monkeypatch): + import sys + from dx_productivity.trial_task_center import batch_supervisor + 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.supervisor_evidence import InvalidSupervisorResult + monkeypatch.setattr(batch_supervisor, '_command', lambda request: ( + sys.executable, '-c', "import pathlib,sys; pathlib.Path(sys.argv[1]).with_name('result.json').write_text('{')", str(request))) + store = TaskStore(tmp_path / 'tasks.db') + try: + service = TaskCenterService(store, configure_logging(tmp_path)) + with pytest.raises(InvalidSupervisorResult) as caught: + batch_supervisor.run_supervised(service, 'probe', {}) + assert caught.value.exit_code == 0 + journal = json.loads(next((tmp_path / 'batch-resources').glob('*/supervisor.json')).read_text(encoding='utf-8')) + assert journal['state'] == 'blocked' + finally: + store.close() + + +@pytest.mark.parametrize('protection_failure', [False, True]) +def test_crash_captures_stderr_without_newline_and_keeps_exit_code(tmp_path, monkeypatch, protection_failure): + import sys + from dx_productivity.trial_task_center.batch_supervisor import run_supervised + from dx_productivity.trial_task_center import batch_supervisor + from dx_productivity.trial_task_center.supervisor_evidence import MissingSupervisorResult + 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.errors import TaskCenterError + monkeypatch.setattr(batch_supervisor, '_command', lambda request: ( + sys.executable, '-c', "import os,sys; sys.stderr.write('x'*100000+'crash detail'); sys.stderr.flush(); os._exit(79)")) + store = TaskStore(tmp_path / 'tasks.db') + try: + service = TaskCenterService(store, configure_logging(tmp_path)) + task = store.create_task('never opened in CAD') + if protection_failure: + def denied(*args, **kwargs): + raise PermissionError('protection denied') + monkeypatch.setattr(batch_supervisor, '_mark_interrupted', denied) + with pytest.raises(TaskCenterError) as caught: + run_supervised(service, 'execute_quick_run', {'task_id': task.task_id}) + error = caught.value.primary_error if protection_failure else caught.value + if protection_failure: + assert caught.value.code == 'interruption_protection_failed' + assert isinstance(error, MissingSupervisorResult) + assert error.exit_code == 79 + assert error.stderr_tail.endswith('crash detail') + assert len(error.stderr_tail.encode('utf-8')) <= 65536 + journal = json.loads(next((tmp_path / 'batch-resources').glob('*/supervisor.json')).read_text(encoding='utf-8')) + assert journal['state'] == 'blocked' + finally: + store.close() + + def test_supervisor_deep_root_uses_compatible_process_working_directory(tmp_path, monkeypatch): from dx_productivity.trial_task_center.batch_supervisor import run_supervised from dx_productivity.trial_task_center.isolated_desktop import _Win32Api @@ -259,7 +365,8 @@ def test_real_supervisor_process_crash_reconciles_without_replaying_mock_busines else: with pytest.raises(TaskCenterError) as caught: supervisor.run_supervised(service, 'execute_quick_run', {'task_id': task.task_id}) - assert caught.value.code == 'supervisor_interrupted' + assert caught.value.code == 'worker_result_missing' + assert caught.value.exit_code == 73 executions = witness.read_text().splitlines() assert len(executions) == 1 and int(executions[0]) not in {os.getpid(), previous['pid']} marker = evidence / 'quick-run/uncertain.flag' diff --git a/tests/v1_1/test_isolated_desktop.py b/tests/v1_1/test_isolated_desktop.py index eab707c..b31b4ed 100644 --- a/tests/v1_1/test_isolated_desktop.py +++ b/tests/v1_1/test_isolated_desktop.py @@ -68,6 +68,87 @@ def test_startup_info_always_targets_private_desktop(): assert info.lpDesktop == 'winsta0\\dx-private-test' +@pytest.mark.skipif(os.name != 'nt', reason='native Windows process identity') +def test_redirected_process_pid_matches_owned_handle(): + import win32process + api = _Win32Api() + process = api.create_process((sys.executable, '-c', 'import time; time.sleep(1)'), + None, None, None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + try: + assert process.pid == win32process.GetProcessId(process.handle) + finally: + process.close() + api.close_pending() + + +@pytest.mark.skipif(os.name != 'nt', reason='native Windows venv launcher') +def test_python_worker_identity_and_environment_match_native_process(): + api = _Win32Api() + script = 'import os,sys,json,win32api; print(json.dumps([os.getpid(),sys.prefix]),flush=True)' + process = api.create_process((sys.executable, '-c', script), None, None, None, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + try: + import json + pid, prefix = json.loads(process.stdout.readline()) + assert pid == process.pid + assert prefix == sys.prefix + assert process.wait(timeout=5) == 0 + finally: + process.close() + api.close_pending() + + +@pytest.mark.skipif(os.name != 'nt', reason='native Windows launch environment') +@pytest.mark.parametrize('flags', [(), ('-E',), ('-I',)]) +@pytest.mark.parametrize('redirected', [False, True]) +def test_python_launch_preserves_venv_in_isolated_modes_and_unicode_cwd(tmp_path, flags, redirected): + import json + working = tmp_path / '中文工作目录 with spaces' + working.mkdir() + witness = working / 'identity.json' + script = ('import json,os,sys,win32api; from pathlib import Path; ' + 'Path(sys.argv[1]).write_text(json.dumps([os.getpid(),sys.prefix,os.getcwd()]),encoding="utf-8")') + environment = dict(os.environ) + environment['COMPATIBILITY_SENTINEL'] = 'retained' + original = dict(environment) + api = _Win32Api() + kwargs = {'stdout': subprocess.PIPE, 'stderr': subprocess.PIPE} if redirected else {} + process = api.create_process((sys.executable, *flags, '-c', script, str(witness)), + None, working, environment, **kwargs) + try: + assert process.wait(timeout=15) == 0 + pid, prefix, cwd = json.loads(witness.read_text(encoding='utf-8')) + assert pid == process.pid + assert prefix == sys.prefix + assert Path(cwd) == working + assert environment == original # child-only venv setup must not alter caller environment + finally: + process.close() + api.close_pending() + + +@pytest.mark.parametrize('frozen,current_python', [(True, True), (False, False)]) +def test_frozen_and_foreign_executables_never_use_current_venv_launcher(monkeypatch, frozen, current_python): + from dx_productivity.trial_task_center import isolated_desktop as module + api, _, _, *_ = native_api() + command = (sys.executable if current_python else r'C:\other-runtime\python.exe', '--batch-worker', 'request.json') + observed = [] + create = api.process.CreateProcess + def record(*args): + observed.append(args) + return create(*args) + api.process.CreateProcess = record + monkeypatch.setattr(module.sys, 'frozen', frozen, raising=False) + environment = {'SystemRoot': os.environ['SystemRoot'], 'PYTHONHOME': 'unrelated'} + process = api.create_process(command, None, None, environment) + try: + assert observed[0][0] == command[0] + assert observed[0][6] == environment + assert '__PYVENV_LAUNCHER__' not in environment + finally: + process.close() + + def test_desktop_records_native_handle_before_close_and_failed_retry(): class Desktop: fail = True @@ -190,6 +271,7 @@ def native_api(*, assign_error=False): {'BasicLimitInformation': {'LimitFlags': 0}} if kind == 9 else {'ActiveProcesses': int(state['running'])}), SetInformationJobObject=lambda *_args: events.append(('limits',)), AssignProcessToJobObject=assign, TerminateJobObject=terminate, + IsProcessInJob=lambda *args: True, JobObjectExtendedLimitInformation=9, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE=0x2000, JobObjectBasicAccountingInformation=1, ) @@ -203,6 +285,16 @@ def test_process_is_suspended_until_job_assignment(): assert process.pid == 42 assert ('create', 0x8000004) in events assert events.index(('assign',)) < events.index(('resume',)) + assert process.cleanup_record['job_membership_verified'] is True + + +def test_unverified_job_membership_never_resumes_child(): + api, events, state, *_ = native_api() + api.job.IsProcessInJob = lambda *args: False + with pytest.raises(TaskCenterError, match='membership'): + api.create_process(('python.exe',), None, None, None) + assert ('resume',) not in events + assert state['running'] is False def test_failed_job_assignment_terminates_and_reaps_owned_process(): diff --git a/tests/v1_1/test_missing_uid_inventory.py b/tests/v1_1/test_missing_uid_inventory.py new file mode 100644 index 0000000..023072c --- /dev/null +++ b/tests/v1_1/test_missing_uid_inventory.py @@ -0,0 +1,135 @@ +"""Exercise the inventory entry point, rather than only its identity helper.""" +import pytest + +from test_accuracy_vbs_guards import run_vbs + + +@pytest.mark.parametrize('broken_value', [False, True]) +@pytest.mark.parametrize(('uid', 'refdes', 'duplicate', 'expected'), [ + ('', 'R1', False, 'OK'), + ('u1', 'R1', False, 'OK'), + ('', '', False, 'BLOCKED'), + ('u1', '', False, 'BLOCKED'), + ('', 'R1', True, 'BLOCKED'), + ('u1', 'R1', True, 'BLOCKED'), +]) +def test_inventory_accepts_missing_uid_only_with_unique_refdes(tmp_path, uid, refdes, duplicate, expected, broken_value): + (tmp_path / 'component-inventory.request').touch() + output = run_vbs(tmp_path, ['QPDiscoverInventory', 'QPFindCurrentAttribute', + 'QPEffectiveComponentAttribute'], f''' +Dim Documents, Comp, Other, Failure +QP_Root="{tmp_path}" +BrokenValue={str(broken_value)} +Set QP_Fso=CreateObject("Scripting.FileSystemObject") +Set QP_App=New FakeApp +Set Documents=New FakeDocuments +Set Comp=New FakeComponent +Comp.UID="{uid}":Comp.Refdes="{refdes}" +QP_App.ActiveView.Components.Add Comp,True +If {str(duplicate)} Then + Set Other=New FakeComponent + Other.UID="other":Other.Refdes="{refdes.lower()}" + QP_App.ActiveView.Components.Add Other,True +End If +On Error Resume Next +QPDiscoverInventory Documents +Failure=Err.Number +On Error GoTo 0 +If Failure<>0 Then + WScript.Echo "BLOCKED" +Else + WScript.Echo "OK" + WScript.Echo ExportedText +End If +''', ''' +Dim QP_Root, QP_App, QP_Fso, QP_Stage, QP_FallbackIdentityUsed, BrokenValue, ExportedText +Const VDM_COMP=128 +Const VD_ALL=0 +Sub QPLog(ByVal Root,ByVal Level,ByVal Message) +End Sub +Sub QPProgress(ByVal Stage,ByVal Done,ByVal Total) +End Sub +Function QPReadInventoryRequest() + Dim Requested + Set Requested=CreateObject("Scripting.Dictionary") + Requested.Add "old","OLD" + Set QPReadInventoryRequest=Requested +End Function +Function QPProjectPath() + QPProjectPath="project.prj" +End Function +Function QPSheetKey(ByVal Schematic,ByVal Sheet) + QPSheetKey=Schematic & ":" & Sheet +End Function +Function QPEncode(ByVal Value) + QPEncode=CStr(Value) +End Function +Sub QPWriteUtf8(ByVal FilePath,ByVal Text) + If Right(FilePath,14)="components.tsv" Then ExportedText=Text +End Sub +Class FakeApp + Public ActiveView + Private Sub Class_Initialize + Set ActiveView=New FakeView + End Sub +End Class +Class FakeView + Public Components,Block + Private Sub Class_Initialize + Set Components=CreateObject("Scripting.Dictionary") + Set Block=New FakeBlock + End Sub + Function Query(ByVal Kind,ByVal Scope) + Set Query=Components + End Function + Function GetName(ByVal Mode) + GetName="MAIN" + End Function +End Class +Class FakeBlock + Public SheetNum + Private Sub Class_Initialize + SheetNum="1" + End Sub +End Class +Class FakeDocuments + Function GetAvailableSchematics() + Set GetAvailableSchematics=CreateObject("Scripting.Dictionary") + GetAvailableSchematics.Add "MAIN",True + End Function + Function GetAvailableSheets(ByVal Schematic) + Set GetAvailableSheets=CreateObject("Scripting.Dictionary") + GetAvailableSheets.Add "1",True + End Function + Function Open(ByVal Schematic,ByVal Sheet) + Set Open=New FakeDocument + End Function +End Class +Class FakeDocument + Sub Activate() + End Sub + Sub Close(ByVal Save,ByVal Name) + End Sub +End Class +Class FakeComponent + Public UID,Refdes + Function FindAttribute(ByVal Name) + If Name="Value" And BrokenValue Then Err.Raise 1234,"FakeCOM","optional value unavailable" + If Name="Part Number" Then + Set FindAttribute=New FakeAttribute + Else + Set FindAttribute=Nothing + End If + End Function +End Class +Class FakeAttribute + Public Value,InstanceValue + Private Sub Class_Initialize + Value="OLD":InstanceValue="" + End Sub +End Class +''') + assert output.splitlines()[0] == expected + if expected == 'OK': + identity = uid or '@REFDES|' + refdes + assert output.splitlines()[-1].split('\t')[3:6] == [identity, refdes, 'OLD'] diff --git a/tests/v1_1/test_native_readback_efficiency.py b/tests/v1_1/test_native_readback_efficiency.py index 6d6ea54..7380312 100644 --- a/tests/v1_1/test_native_readback_efficiency.py +++ b/tests/v1_1/test_native_readback_efficiency.py @@ -66,3 +66,12 @@ def test_preselection_context_does_not_query_an_unused_selection(monkeypatch): assert view.selection_reads == 0 with pytest.raises(ValueError, match='Selected component set'): native.check_context(app, api, metadata, records) + + +def test_optional_attribute_read_failure_degrades_but_required_identity_fails_fast(): + class Broken: + def FindAttribute(self, name): + raise RuntimeError('COM attribute unavailable') + assert native.attribute_value(Broken(), 'Value') == '' + with pytest.raises(ValueError, match='required attribute'): + native.attribute_value(Broken(), 'Part Number', required=True) diff --git a/tests/v1_1/test_project_long_paths.py b/tests/v1_1/test_project_long_paths.py index 7e89d2e..c0b254f 100644 --- a/tests/v1_1/test_project_long_paths.py +++ b/tests/v1_1/test_project_long_paths.py @@ -10,6 +10,8 @@ from dx_productivity.trial_task_center import legacy_paths, native_helper def long_project(tmp_path): folder = tmp_path / ('design-' + 'x' * 78) / ('revision-' + 'y' * 78) + while len(str(folder / 'board.prj')) <= 260: + folder /= 'extra-' + 'z' * 40 folder.mkdir(parents=True) project = folder / 'board.prj' project.write_text('project') diff --git a/tests/v1_1/test_safe_user_stop.py b/tests/v1_1/test_safe_user_stop.py index 629f696..b65f48a 100644 --- a/tests/v1_1/test_safe_user_stop.py +++ b/tests/v1_1/test_safe_user_stop.py @@ -125,6 +125,23 @@ def test_force_cannot_terminate_before_interruption_evidence_is_durable(tmp_path with pytest.raises(PermissionError): module._force_owned_supervisor(tmp_path, journal, process) assert calls == [] + saved = json.loads((tmp_path / 'batch-resources' / ('a' * 32) / 'supervisor.json').read_text()) + assert saved['stop_mode'] == 'forced' + assert saved['force_state'] == 'protection_failed' + assert saved['state'] == 'blocked' + + +def test_repeated_force_does_not_terminate_twice(tmp_path): + from types import SimpleNamespace + from dx_productivity.trial_task_center.batch_supervisor import _force_owned_supervisor + calls = [] + process = SimpleNamespace(terminate=lambda: calls.append('terminate')) + journal = {'operation': 'execute_task_queue', 'batch_id': 'a' * 32, 'attempt_id': 'b' * 32, + 'task_ids': ['task'], 'state': 'running'} + _force_owned_supervisor(tmp_path, journal, process) + _force_owned_supervisor(tmp_path, journal, process) + assert calls == ['terminate'] + assert journal['force_state'] == 'termination_requested' def test_force_already_performed_remains_forced_when_exit_diagnostics_fail(tmp_path): @@ -263,8 +280,11 @@ def test_failed_protection_fsync_is_retried_before_force_is_allowed(tmp_path, mo thread.join(3) -@pytest.mark.parametrize('deny_first,transport_failure', [(False, False), (True, False), (False, True)]) -def test_real_supervisor_force_preserves_protection_and_never_replays(tmp_path, monkeypatch, deny_first, transport_failure): +@pytest.mark.parametrize('deny_first,transport_failure,cleanup_failure,late_write_failure', [ + (False, False, False, False), (True, False, False, False), (False, True, False, False), + (False, False, True, False), (False, False, False, True), (False, False, True, True)]) +def test_real_supervisor_force_preserves_protection_and_never_replays( + tmp_path, monkeypatch, deny_first, transport_failure, cleanup_failure, late_write_failure): import os import textwrap import time @@ -310,6 +330,27 @@ def test_real_supervisor_force_preserves_protection_and_never_replays(tmp_path, raise PermissionError('force evidence denied once') return original_mark(*args) monkeypatch.setattr(module, '_mark_interrupted', mark) + if late_write_failure: + from dx_productivity.trial_task_center import batch_resources + original_write = batch_resources.write_record + late_failures = [] + def write(path, value): + if (Path(path).name == 'supervisor.json' and not late_failures + and value.get('force_state') in {'confirmed', 'confirmation_failed'}): + late_failures.append(True) + raise PermissionError('late confirmation write denied') + return original_write(path, value) + monkeypatch.setattr(batch_resources, 'write_record', write) + if cleanup_failure: + from dx_productivity.trial_task_center.isolated_desktop import _Win32Process + original_close = _Win32Process.close + failures = [] + def close(process): + original_close(process) + if process.cleanup_record['forced_termination'] and not failures: + failures.append(True) + raise OSError('exit confirmation diagnostic failed') + monkeypatch.setattr(_Win32Process, 'close', close) control = StopControl() driver_errors, survived = [], [] def request(): @@ -348,13 +389,17 @@ def test_real_supervisor_force_preserves_protection_and_never_replays(tmp_path, module.run_supervised(service, 'execute_quick_run', {'task_id': task.task_id}, stop_event=control, on_progress=(lambda _: (_ for _ in ()).throw(RuntimeError('UI feedback failed'))) if transport_failure else None) assert not driver_errors - assert caught.value.code == 'user_forced_stop' + assert caught.value.code == ('batch_resources_pending' if cleanup_failure or late_write_failure else 'user_forced_stop') + if cleanup_failure or late_write_failure: + assert caught.value.primary_error.code == 'user_forced_stop' assert (evidence / 'uncertain.flag').exists() marker = (evidence / 'uncertain.flag').read_bytes() assert not (evidence / 'unexpected_completed').exists() journals = [json.loads(p.read_text(encoding='utf-8')) for p in (tmp_path / 'batch-resources').glob('*/supervisor.json')] forced = next(r for r in journals if r['operation'] == 'execute_quick_run') assert forced['stop_mode'] == 'forced' and forced['business_uncertain'] is True + assert forced['force_state'] == ('termination_requested' if late_write_failure else + 'confirmation_failed' if cleanup_failure else 'confirmed') assert service.cleanup_batch_resources() == () assert (evidence / 'uncertain.flag').read_bytes() == marker assert not (evidence / 'unexpected_completed').exists() diff --git a/tests/v1_1/test_supervisor_evidence.py b/tests/v1_1/test_supervisor_evidence.py new file mode 100644 index 0000000..386778d --- /dev/null +++ b/tests/v1_1/test_supervisor_evidence.py @@ -0,0 +1,107 @@ +import json + +import pytest + + +def test_lifecycle_evidence_appends_atomically_and_is_readable(tmp_path): + from dx_productivity.trial_task_center.supervisor_evidence import LifecycleEvidence + + evidence = LifecycleEvidence(tmp_path / 'attempt') + evidence.append('started', pid=123) + evidence.append('result_committed', sequence=4) + + rows = evidence.events() + assert [row['event'] for row in rows] == ['started', 'result_committed'] + assert rows[0]['pid'] == 123 + assert all((tmp_path / 'attempt' / name).exists() for name in ('lifecycle.jsonl',)) + + +def test_missing_result_error_includes_process_and_stream_evidence(tmp_path): + from dx_productivity.trial_task_center.supervisor_evidence import ( + MissingSupervisorResult, + write_process_evidence, + ) + + path = tmp_path / 'attempt' + write_process_evidence(path, exit_code=17, stdout='last progress', stderr='import failed') + + error = MissingSupervisorResult.from_directory(path) + assert error.code == 'worker_result_missing' + assert error.exit_code == 17 + assert error.stdout_tail == 'last progress' + assert error.stderr_tail == 'import failed' + assert 'import failed' in str(error) + + +def test_stream_tail_is_bounded(tmp_path): + from dx_productivity.trial_task_center.supervisor_evidence import write_process_evidence + + write_process_evidence(tmp_path, exit_code=0, stdout='x' * 100, stderr='y' * 100, limit=8) + data = json.loads((tmp_path / 'process.json').read_text(encoding='utf-8')) + assert data['stdout_tail'] == 'x' * 8 + assert data['stderr_tail'] == 'y' * 8 + + +def test_invalid_result_has_distinct_code_and_preserves_exit_evidence(tmp_path): + from dx_productivity.trial_task_center.supervisor_evidence import InvalidSupervisorResult, write_process_evidence + write_process_evidence(tmp_path, exit_code=0, stderr='diagnostic') + error = InvalidSupervisorResult.from_directory(tmp_path) + assert error.code == 'worker_result_invalid' + assert error.exit_code == 0 + assert error.stderr_tail == 'diagnostic' + + +def test_process_evidence_preserves_cleanup_and_last_protocol_message(tmp_path): + from dx_productivity.trial_task_center.supervisor_evidence import write_process_evidence + value = write_process_evidence(tmp_path, exit_code=0, + cleanup={'job_active_processes': 0}, last_message={'type': 'heartbeat', 'sequence': 3}) + assert value['cleanup']['job_active_processes'] == 0 + assert value['last_message']['sequence'] == 3 + + +def test_stream_tail_bound_is_utf8_bytes(tmp_path): + from dx_productivity.trial_task_center.supervisor_evidence import write_process_evidence + data = write_process_evidence(tmp_path, exit_code=0, stdout='中' * 100, limit=8) + assert len(data['stdout_tail'].encode('utf-8')) <= 8 + + +@pytest.mark.parametrize('value', [None, [], {'stdout_tail': None, 'stderr_tail': 3}]) +def test_malformed_process_evidence_cannot_mask_missing_result(tmp_path, value): + from dx_productivity.trial_task_center.supervisor_evidence import MissingSupervisorResult + (tmp_path / 'process.json').write_text(json.dumps(value), encoding='utf-8') + error = MissingSupervisorResult.from_directory(tmp_path) + assert error.code == 'worker_result_missing' + + +def test_torn_lifecycle_keeps_complete_events(tmp_path): + from dx_productivity.trial_task_center.supervisor_evidence import LifecycleEvidence + evidence = LifecycleEvidence(tmp_path) + evidence.append('started') + with evidence.path.open('ab') as stream: + stream.write(b'{"event":') + assert [row['event'] for row in evidence.events()] == ['started'] + + +def test_optional_lifecycle_reports_write_failure_without_disrupting_business(tmp_path, monkeypatch): + from dx_productivity.trial_task_center.supervisor_evidence import LifecycleEvidence + evidence = LifecycleEvidence(tmp_path) + def denied(*args, **kwargs): + raise PermissionError('disk denied') + monkeypatch.setattr(evidence, 'append', denied) + assert 'disk denied' in evidence.try_append('result_committed') + + +def test_diagnostic_bundle_includes_redacted_attempt_evidence(tmp_path): + import zipfile + from dx_productivity.trial_task_center.diagnostics_bundle import export_diagnostic_bundle + from dx_productivity.trial_task_center.supervisor_evidence import LifecycleEvidence, write_process_evidence + directory = tmp_path / 'data' / 'batch-resources' / ('a' * 32) + LifecycleEvidence(directory).append('worker_started', pid=123) + write_process_evidence(directory, exit_code=7, stderr='password=secretValue') + bundle = export_diagnostic_bundle(tmp_path / 'data', tmp_path / 'diagnostic.zip') + with zipfile.ZipFile(bundle) as archive: + prefix = 'batch-resources/' + 'a' * 32 + '/' + assert prefix + 'lifecycle.jsonl' in archive.namelist() + process = archive.read(prefix + 'process.json').decode() + assert 'secretValue' not in process + assert 'REDACTED' in process diff --git a/tests/v1_1/test_target_cell_execution.py b/tests/v1_1/test_target_cell_execution.py index e8cf09d..10307d9 100644 --- a/tests/v1_1/test_target_cell_execution.py +++ b/tests/v1_1/test_target_cell_execution.py @@ -175,7 +175,7 @@ def test_native_catalog_checks_cannot_cold_read_or_submit_on_miss(tmp_path, monk monkeypatch.setattr(native_helper, 'check_active_context', lambda *args: None) for name in ('components_on_sheet', 'select_snapshot_batch', 'check_context'): monkeypatch.setattr(native_helper, name, lambda *args, **kwargs: components) - monkeypatch.setattr(native_helper, 'attribute_value', lambda *args: 'OLD') + monkeypatch.setattr(native_helper, 'attribute_value', lambda *args, **kwargs: 'OLD') monkeypatch.setattr(native_helper, 'selection', lambda *args: None) monkeypatch.setattr(native_helper, 'replacement_command_from_resources', lambda *args: 33977) monkeypatch.setattr(native_helper, 'wait_for', lambda callback, *args, **kwargs: callback()) diff --git a/tests/v1_1/test_target_execution.py b/tests/v1_1/test_target_execution.py index f4989c8..7e8d491 100644 --- a/tests/v1_1/test_target_execution.py +++ b/tests/v1_1/test_target_execution.py @@ -166,6 +166,66 @@ def test_native_context_rejects_missing_or_changed_target_without_no_match(): check_context(App(components), Api, metadata, [record]) +def test_native_snapshot_rejects_empty_uid_snapshot_only_until_inventory_fallback(tmp_path): + from dx_productivity.trial_task_center.native_helper import read_snapshot + snapshot = tmp_path / 'snapshot.tsv' + snapshot.write_text( + 'M\tC:/board.prj\tOLD\tNEW\tMAIN:1\tpreserve\t\t1\ttargets\t\x1fR1\x1fA\n' + 'C\t\tR1\t0\t0\t0\t0\t\n', encoding='utf-8') + with pytest.raises(ValueError, match='UID'): + read_snapshot(snapshot) + + +def test_native_context_uses_refdes_fallback_for_empty_component_uid(): + from dx_productivity.trial_task_center.native_helper import check_context + class Component: + UID = '' + Refdes = 'R1' + _oleobj_ = None + def FindAttribute(self, name): + value = 'OLD' if name == 'Part Number' else 'A' + return type('Attr', (), {'InstanceValue': value, 'Value': value})() + Component._oleobj_ = Component() + class View: + Block = type('Block', (), {'SheetNum': '1'})() + def GetName(self, index): return 'MAIN' + def Query(self, kind, selected): return [Component()] + class App: + ActiveView = View() + def GetProjectData(self): + return type('Project', (), {'GetProjectFilePath': lambda self: 'C:/board.prj'})() + class Api: + IVdComp = staticmethod(lambda value: value) + metadata = ['M', 'C:/board.prj', 'OLD', 'NEW', 'MAIN:1', 'preserve', '', '1', 'targets', 'x\x1fR1\x1fA'] + metadata[9] = '@REFDES|R1\x1fR1\x1fA' + assert len(check_context(App(), Api(), metadata, + [['C', '@REFDES|R1', 'R1', '0', '0', '0', '0', '']], require_selection=False)) == 1 + + +def test_native_context_rejects_duplicate_refdes_fallback(): + from dx_productivity.trial_task_center.native_helper import check_context + class Component: + UID = '' + Refdes = 'R1' + _oleobj_ = None + def FindAttribute(self, name): + return type('Attr', (), {'InstanceValue': 'OLD', 'Value': 'OLD'})() + Component._oleobj_ = Component() + class View: + Block = type('Block', (), {'SheetNum': '1'})() + def GetName(self, index): return 'MAIN' + def Query(self, kind, selected): return [Component(), Component()] + class App: + ActiveView = View() + def GetProjectData(self): + return type('Project', (), {'GetProjectFilePath': lambda self: 'C:/board.prj'})() + class Api: + IVdComp = staticmethod(lambda value: value) + with pytest.raises(ValueError, match='Duplicate component UID'): + check_context(App(), Api(), ['M', 'C:/board.prj', 'OLD', 'NEW', 'MAIN:1'], + [['C', '@REFDES|R1', 'R1']]) + + def test_native_rejects_changed_unselected_component_since_snapshot(tmp_path): class Component: UID = 'u2'