diff --git a/docs/reviews/2026-10-30-project-optimization-review.md b/docs/reviews/2026-10-30-project-optimization-review.md new file mode 100644 index 0000000..fcb2e8b --- /dev/null +++ b/docs/reviews/2026-10-30-project-optimization-review.md @@ -0,0 +1,50 @@ +# 2026-10-30 优化分支第二轮审查 + +本轮在 `.worktrees/project-optimization` 的 `codex/project-optimization` 分支执行。主工作区继续使用 DSH 分支,两个工作目录分别维护各自提交。 + +## 已复现并修复的问题 + +| 问题 | 触发条件和原因 | 修复 | +| --- | --- | --- | +| P1:历史资源记录使界面卡顿 | `_update_gates()` 同步调用资源扫描,搜索、选择规则和执行状态更新都会读取全部历史 JSON。500 条已释放记录的一次扫描约需 1.57–1.65 秒 | 新增 `ui/resource_status.py`,后台读取并保留状态快照;初始化、操作收尾和定时复查更新快照,按钮刷新直接读取快照 | +| P1:异常退出丢失 BOM 逐行诊断 | `_finish_response()` 在资源复核通过后发现 CAD 退出异常,创建新的 `application_shutdown_failed` 错误,却没有携带原业务错误和 BOM 报告 | 在 `supervisor_result.py` 统一构造退出错误,保留原错误、清理错误和逐行报告;移除报告中的可执行预览 | +| P2:规则窗口销毁后访问已释放控件 | 封装读取仍在运行时销毁规则窗口,成功和失败回调均只判断逻辑关闭标记,未判断 Qt 对象是否有效 | 回调先检查 `isValid(self)`,丢弃销毁后的结果;两条路径均有实际 Qt 生命周期回归 | +| P2:已完成批次仍显示可执行按钮 | 按钮只判断批次存在;处理函数重新筛选任务后发现没有剩余任务,直接返回 | 抽取 `pending_batch_tasks()`,按钮门控、执行和确认范围共用待执行任务列表 | + +资源快照采用保守处理:首次读取未完成或读取失败时阻断执行;操作结束触发的刷新会合并,操作前的旧快照不能解除门禁。周期检查不会取消慢读取。窗口关闭会停止周期检查并丢弃迟到结果。服务层原有执行前资源检查继续作为最终校验。 + +## 测量范围 + +使用独立临时目录创建合法的已释放资源记录,无 CAD 进程和工程写入。原同步扫描测得: + +- 0 条:三次分别为 2.68、1.83、1.41 毫秒。 +- 100 条:293.55、277.36、313.80 毫秒。 +- 500 条:1650.06、1626.25、1569.35 毫秒。 + +接入后台快照后,在真实 `TrialTaskCenterWindow`、`TaskCenterService` 和 `TaskStore` 的空任务窗口中,500 条历史记录下连续调用按钮门控 100 次,总计 6.63 毫秒,期间文件扫描次数为 0。另有已选中任务的 Qt 回归验证后台慢读取期间仍能搜索和响应事件。该结果证明 GUI 热路径移除了扫描,不表示磁盘扫描本身变快,也不代表实际 CAD 替换速度。 + +## 前次结论修正 + +前次把停止事件轮询从 0.1 秒改成 0.01 秒,并将一次时序测试失败解释为阻塞根因,证据不足。`Event.wait()` 会在事件设置后立即唤醒;初次保护失败后的进度文件写入可能延后重试,固定等待 0.25 秒不能证明保护重试没有发生。 + +本轮恢复 0.1 秒等待,回归测试等待明确的强制结束拒绝状态,再检查线程仍存活和保护写入确已重试。不会把这项时序调整计作资源泄漏修复。 + +## 测试入口修正 + +全仓收集发现 `tests/ui/test_execution_feedback.py` 与 `tests/v1_1/test_execution_feedback.py` 的模块名冲突。UI 文件更名为 `test_ui_execution_feedback.py` 并更新文档引用。 + +默认 `python -m pytest -q` 仅覆盖 `tests/v1_1`,不能称为整个仓库测试。全仓历史测试曾启动,随后为实施新确认的 GUI 阻塞修复而主动停止,未将该次运行记作完整通过。当前验证覆盖新版测试、UI 和项目发布要求的安全子集。 + +本轮未进行真实 DxDesigner 工程替换或冻结包验收;现场桌面引用残留仍需实际故障机器的进程与句柄证据。 + +## 最终验证 + +```powershell +python -m pytest -q tests/v1_1 tests/ui tests/part_replace/test_verification.py tests/trial_task_center/test_execution_safety.py tests/trial_task_center/test_application.py --durations=5 +``` + +结果:960 passed,1 项既有 pywinauto STA 警告,462.49 秒。此命令覆盖新版测试、UI 和发布安全子集。 + +安全停止的真实子进程测试曾在收到首条阶段进度前触发 0.6 秒启动超时,未进入其要验证的保存/强制结束路径。测试现将启动预算与阶段无进度时限区分,并断言目标阶段确实被接收;保存和只读阶段的原 0.3 秒时限继续有效。生产监控超时参数未修改。 + +`python -m compileall -q src` 和 `git diff --check` 均通过。 diff --git a/docs/superpowers/plans/2026-10-30-lifecycle-optimization.md b/docs/superpowers/plans/2026-10-30-lifecycle-optimization.md new file mode 100644 index 0000000..d9e5931 --- /dev/null +++ b/docs/superpowers/plans/2026-10-30-lifecycle-optimization.md @@ -0,0 +1,85 @@ +# Lifecycle Optimization 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 CAD save, close, exit, and resource-release outcomes explicit and recoverable across normal, failed, cancelled, and forced-stop paths. + +**Architecture:** Keep business completion and infrastructure cleanup as separate outcomes. Extend the existing resource journal and runtime result objects with phase evidence, give owned helper processes a bounded natural-exit window, and preserve cleanup failures instead of converting them into a successful-looking result. + +**Tech Stack:** Python 3.12, PySide6, pywin32, pytest, existing `BatchCadRuntime`, `IsolatedCadSession`, supervisor protocol, and VBS worker evidence files. + +**Spec:** Approved in chat on 2026-10-30 as Phase 1 of the project optimization. + +## Global Constraints + +- Preserve the existing no-replay protection for uncertain project mutations. +- Never terminate an unowned process or clear evidence that cannot be verified. +- Keep normal successful replacement behavior and public service method signatures compatible. +- Every production change must have a regression test that fails before the change. + +--- + +### Task 1: Model and persist separate save/close evidence + +**Files:** +- Modify: `src/dx_productivity/trial_task_center/batch_runtime.py` +- Modify: `src/dx_productivity/trial_task_center/isolated_cad.py` +- Modify: `src/dx_productivity/trial_task_center/batch_resources.py` +- Test: `tests/v1_1/test_batch_runtime.py` +- Test: `tests/v1_1/test_isolated_cad.py` + +**Interfaces:** +- Consumes existing CAD shutdown records and project checkpoints. +- Produces journal fields for save requested, save returned, readback verified, project closed, and cleanup outcome. + +- [ ] **Step 1: Write failing tests** for a close failure after a save request and for a cleanup record that reports business completion with infrastructure failure. +- [ ] **Step 2: Run the focused tests** and verify they fail because the current record has no separate save evidence and the completed result hides cleanup failure. +- [ ] **Step 3: Add the smallest evidence fields** with backward-compatible defaults and preserve the existing uncertain/recovery guard. +- [ ] **Step 4: Run the focused tests** and verify both pass. +- [ ] **Step 5: Run the related supervisor and recovery tests** before moving to the next task. + +### Task 2: Allow owned helper processes a bounded natural exit window + +**Files:** +- Modify: `src/dx_productivity/trial_task_center/isolated_desktop.py` +- Test: `tests/v1_1/test_isolated_desktop.py` +- Test: `tests/v1_1/test_batch_runtime_windows.py` + +**Interfaces:** +- Consumes owned process identities and existing Win32 job cleanup records. +- Produces cleanup records that distinguish naturally exited descendants from forced termination. + +- [ ] **Step 1: Write a failing test** where the root CAD process has exited and an owned helper exits shortly afterward; assert no forced termination is requested. +- [ ] **Step 2: Run the test** and verify the current implementation calls the force cleanup immediately. +- [ ] **Step 3: Add a bounded wait** only for identities whose owned root has already exited; retain force cleanup after the deadline. +- [ ] **Step 4: Run the focused Windows tests** and verify the cleanup record includes the wait and final state. +- [ ] **Step 5: Run the resource release test group**. + +### Task 3: Separate diagnostic journal failures from the CAD Quit protocol + +**Files:** +- Modify: `src/dx_productivity/trial_task_center/isolated_cad.py` +- Modify: `src/dx_productivity/trial_task_center/batch_release_log.py` +- Test: `tests/v1_1/test_isolated_cad.py` +- Test: `tests/v1_1/test_batch_runtime.py` + +**Interfaces:** +- Consumes the existing owned CAD identity and project-close state. +- Produces an explicit diagnostic-write warning while still attempting a valid Quit sequence. + +- [ ] **Step 1: Write a failing test** that injects a closing-record write failure and asserts Quit is still attempted when project state is safe. +- [ ] **Step 2: Run the test** and verify Quit is currently skipped. +- [ ] **Step 3: Isolate diagnostic persistence errors** from the normal Quit decision and record both outcomes. +- [ ] **Step 4: Run the focused tests** and verify both normal and failed-record paths. +- [ ] **Step 5: Run all lifecycle and supervisor tests**. + +### Task 4: Verify the phase and document field validation + +**Files:** +- Modify: `docs/testing/2026-10-26-application-lifecycle-investigation.md` +- Create: `docs/testing/2026-10-30-phase1-lifecycle-optimization.md` + +- [ ] **Step 1: Run the complete test suite** and capture the count and warnings. +- [ ] **Step 2: Run `python -m compileall -q src` and `git diff --check`.** +- [ ] **Step 3: Record the exact scenarios covered and the remaining real DxDesigner field checks.** +- [ ] **Step 4: Review the final diff for accidental changes outside Phase 1.** 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-19-release-preparation.md b/docs/testing/2026-10-19-release-preparation.md index a2805a9..51552ca 100644 --- a/docs/testing/2026-10-19-release-preparation.md +++ b/docs/testing/2026-10-19-release-preparation.md @@ -30,7 +30,7 @@ | 已完成任务状态、重试、重复执行新轮次 | tests/v1_1/test_ui_batch_retry.py;test_new_round.py | | 任务切换丢弃旧查询、执行期间只读浏览、归属不丢失 | tests/v1_1/test_ui_responsiveness.py | | 常规 mutation 禁止重入,退出等待安全收尾,强制销毁回调 | tests/ui/test_trial_task_center_workflow.py;tests/v1_1/test_ui_remediation.py | -| 错误编号及日志格式、按需创建/关闭/重新打开日志 | tests/ui/test_execution_feedback.py;tests/v1_1/test_lazy_components.py;test_execution_results_view.py | +| 错误编号及日志格式、按需创建/关闭/重新打开日志 | tests/ui/test_ui_execution_feedback.py;tests/v1_1/test_lazy_components.py;test_execution_results_view.py | | 小窗口和布局持久化 | tests/ui/test_value_options_layout.py;tests/v1_1/test_ui_workspace.py | ## 验收进度 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..f837ecb --- /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;需要保留该状态时在快照阶段阻断替换,恢复预检拒绝缺失字段;不将默认 0 写回工程 | +| 引脚/网络/坐标几何 | 连接和引脚号晋级证明 | 需要该证明时失败;不把缺失网络伪装成可验证的连接 | + +Python native helper 对可选属性捕获 COM 异常并返回空值,对身份和源料号使用 required 模式;真实 UID 缺失时,当前页枚举、引脚、选择、回读和恢复统一使用 `@REFDES|...` fallback identity,并检测重复位号。VBS 对可选库存字段允许空值降级;作为原始快照写回依据的 Value、InstanceValue、显示字段必须成功采集。不可读字段不能用默认值代替原始状态,必须在修改前停止。所有失败都回到有限错误出口,监督器可收集状态,不进入等待循环。 + +安全原则是“缺字段可降级,身份不可猜测”:如果降级会影响替换对象、保存回读或恢复判断,就停止而不是继续。诊断中会保留阶段、字段名和错误,但不会把错误的默认值当作成功证据。 + +验证:字段读取与身份专项覆盖可选显示字段读取失败、可选 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..42fac1e --- /dev/null +++ b/docs/testing/2026-10-30-final-robustness-regression.md @@ -0,0 +1,45 @@ +# 库存入口与独立脚本回归 + +工作分支:`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 后复测。 + +后续 PR 审查复测确认:临时验证环境遗漏 `Pythonwin` 搜索路径导致上述 2 项 `win32ui` 导入失败,并非依赖文件不存在。补齐临时 `.pth` 后真实导入成功。该历史运行结果保留;当前代码、审查修复与精确测试范围见 [PR #9 审查修复记录](2026-10-30-pr9-review-fixes.md)。 + +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-phase1-lifecycle-optimization.md b/docs/testing/2026-10-30-phase1-lifecycle-optimization.md new file mode 100644 index 0000000..5e8f26e --- /dev/null +++ b/docs/testing/2026-10-30-phase1-lifecycle-optimization.md @@ -0,0 +1,26 @@ +# Phase 1:生命周期优化验证 + +日期:2026-10-30(北京时间) +分支:`codex/project-optimization` + +## 本轮修复 + +批次执行发生一次可重试的预写失败时,运行时会先释放旧代资源,再重建 CAD 会话。此前如果旧代 CAD 的 `__exit__` 抛出异常,`BatchCadRuntime._close_resources(rebuilding=True)` 只写入 `shutdown_warning`,没有把异常加入资源错误集合,随后可能继续创建下一代桌面和 CAD。 + +现在该异常同时保留在诊断字段和资源错误集合中,批次状态保持 `blocked`,不会继续重建。外层上下文再次退出时也不会立即重复 close 并把 blocked 改写为 released。已有 `manual_retry` 清理入口仍可在资源状态修复后重新尝试释放。 + +后续审查撤回了上一轮把等待间隔从 0.1 秒缩短到 0.01 秒的调整:事件设置会立即唤醒等待线程,原先的固定时长测试不足以证明该参数是阻塞根因。强制结束前必须成功写入保护证据的约束保留。详见 [第二轮审查](../reviews/2026-10-30-project-optimization-review.md)。 + +## 回归证据 + +- 新增 `test_rebuild_does_not_hide_cad_shutdown_failure`,先验证旧行为会吞掉 CAD 收尾异常,再验证修复后保持 blocked。 +- `tests/v1_1/test_batch_runtime.py`、`test_batch_runtime_windows.py`、`test_batch_supervisor.py`、`test_supervisor_result.py`:53 passed。 +- `test_isolated_cad.py`、`test_isolated_desktop.py`、`test_safe_user_stop.py`、`test_failed_native_recovery_guard.py`:93 passed。 +- `python -m compileall -q src`:通过。 +- `git diff --check`:通过。 + +## 当前边界 + +当前代码已经包含辅助进程自然退出宽限,以及诊断记录失败时仍尝试身份校验后的 CAD Quit。本轮没有重复修改这些路径,也没有声称真实 DxDesigner 现场桌面持有者问题已经闭环。 + +仍需在工程副本和实际 CAD 环境验证:CAD 收尾异常后的人工清理、跨重启资源复核、保存中断后的恢复入口,以及冻结包中的同一行为。 diff --git a/docs/testing/2026-10-30-pr9-review-fixes.md b/docs/testing/2026-10-30-pr9-review-fixes.md new file mode 100644 index 0000000..60f220f --- /dev/null +++ b/docs/testing/2026-10-30-pr9-review-fixes.md @@ -0,0 +1,49 @@ +# PR #9 审查修复记录 + +审查来源:[评论 88](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/pulls/9#issuecomment-88)、[评论 89](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/pulls/9#issuecomment-89)。被审查提交为 `131b6db`。 + +## 阻断项 + +1. 旧格式原生回读把未选器件比较移回循环;新增零个未选器件、两个未选器件、首个未选器件被修改的回归。 +2. `replacement_matches` 返回本地身份键;一/多个无 UID 目标的匹配结果直接流入 `promote_pin_numbers`,验证引脚归属不丢失。 +3. VBS 组件索引、引脚索引、连接签名、捕获集合、保存重开回读和恢复预检统一使用 `QPComponentIdentityKey`。独立原生读取脚本携带同一身份函数。无 UID 且无位号不再生成 `@ANON`,与 Python 一致地阻断;fallback 使用标志在每次捕获前重置。 +4. 不可读的显示字段返回空值状态,不再返回伪造的 `0`。快照在缺少需要保留的显示状态时立即失败;恢复预检和直接显示恢复入口在任何写入前拒绝缺失字段。基础 Value、InstanceValue 作为原始快照证据也必须可读,可选库存 Value 仍可降级为空。六项故障注入覆盖颜色、字体、字号、方向、可见性、坐标,确认 getter 恢复后仍不会写入默认值。 + +新增 VBS 流程回归使用生产函数、真实 `cscript.exe` 和带引脚 COM 替身,覆盖无 UID 捕获、目标/非目标集合区分、恢复、保存、重开和 `saved_ok` 回读。其 CAD 打开与命令传输使用替身,不能解释为真实 DxDesigner 验收通过。 + +## 其他意见的处理 + +- 损坏/不可读 `session.json`、错误 `cad_shutdown` 或 `cleanup_errors` 类型转为 `application_shutdown_failed`,保留业务结果,并将监督记录改为 blocked。 +- 进度发布失败写 WARN,避免磁盘/权限错误静默丢失等待核验状态。 +- 强制终止状态已存在时仍检查进程存活;存活则重试终止,已退出则避免重复调用。 +- 已存在但为空的中断保护文件补写原因、flush、fsync;非空证据保持原样。 +- 控制异常等待通过 `StopControl.waiting_for_resolution`、`resolution_error` 暴露状态,并每 15 秒写等待日志。保留进程所有权直至确认退出或明确强制结束;任意超时自动杀进程会破坏已有的安全停止约定,未采用该做法。 +- worker 的等待允许由 GUI supervisor 终止其自有 Job 结束,见 `stop_control.wait_for_resolution` 的说明;普通取消不等于强制终止。现有真实监督器/Job 测试继续覆盖此边界。 +- CAD 从未完成身份绑定、或者进程创建失败的边界继续保留保守保护;本轮不把“调用创建函数抛错”直接当作“绝无子进程/绝无写入”的证据。资源收尾失败与原始启动失败仍应结合诊断记录判断,尚未取得这些场景的现场 CAD 证据。 + +## 运行环境和数字口径 + +本轮使用 Windows 11 x64、本机完整 CPython 3.12.10、临时虚拟环境,复用项目依赖。临时 `.pth` 必须包含 `win32`、`win32/lib`、`Pythonwin` 和 Win32 DLL 路径。此前 1085 passed / 2 import errors 中的 `win32ui` 问题是临时环境遗漏 `Pythonwin`,不是原依赖文件不存在;现已验证真实导入 pywinauto 和 win32ui 成功,原虚拟环境未改写。 + +历史报告中的 957、960、986、923、1147 对应不同日期、提交和测试范围,不能相加或当作同一版本全量验收。本轮结果只引用下面的具体命令;旧的 Beta 4 发布日期保留其 2026-10-26 历史日期。兼容性 JSON 的本机路径作为内部环境证据保留,对外分发需另行脱敏。 + +```powershell +$env:TEMP='F:\dxpart-test-temp' +$env:TMP=$env:TEMP +& F:\dxpart-test-temp\verification-venv\Scripts\python.exe -m pytest -q tests/v1_1 tests/trial_task_center/test_execution_safety.py tests/trial_task_center/test_worker_monitor.py tests/integration/test_quick_value_modes.py tests/integration/test_automatic_replacement_regression.py tests/integration/test_quick_part_replace_contract.py --basetemp F:\dxpart-test-temp\pr9-review-final --junitxml build\pr9-review-final.xml --tb=short +``` + +专项初验:144 passed / 1 个测试替身缺失 Selected 属性;替身已修正。显示字段故障注入:6 passed。收尾记录、强制重试、保护文件与日志专项:8 passed。独立原生读取与收尾子集:77 passed、8 deselected。最终审查关键专项:127 passed。 + +上述扩大集合最终以 `--basetemp F:\dxpart-test-temp\pr9-review-green --junitxml build\pr9-review-green.xml` 重跑,结果:**1115 passed,1 warning,624.93 秒**。唯一提示是既有 pywinauto STA COM 模式提示;这包含修复后的 `tests/v1_1`、执行安全、工作进程监视、Value 全恢复、原生替换和 VBS 契约测试。首轮同范围结果 1109 passed / 1 failed,是旧契约要求直接访问 `CmpPin.Component.UID`;断言改为统一身份函数后重新验证,历史报告不覆盖。 + +冻结包由上述完整 CPython 3.12.10 构建成功;`tools/verify_packaged_runtime.py` 实际运行 EXE 验证监督器、Job/进程身份、`win32timezone` 和资源释放通过。源码/包内文件 SHA256 一致: + +| 文件 | SHA256 | +|---|---| +| QuickPartReplace.vbs | `4380E5DFD5421F2B2BF104EFAC43128FAFEE4B281AB12F972AFE37CE46D7352E` | +| NativeSnapshotReader.vbs | `46519AA1191DAC97A778D0F4074F2CF3AAF23667E2863A6C7E2057045FCAF099` | + +`git diff --check` 和 `python -m compileall -q src` 通过。 + +Windows 10 实机、真实 DxDesigner 7.9.5 无 UID 工程、中央库/许可证和网络共享条件仍待现场验收。 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/2026-10-31-pr9-latest-review-fix.md b/docs/testing/2026-10-31-pr9-latest-review-fix.md new file mode 100644 index 0000000..9fef909 --- /dev/null +++ b/docs/testing/2026-10-31-pr9-latest-review-fix.md @@ -0,0 +1,41 @@ +# PR #9 最新复审修复(评论 95) + +审查基线:`5ed8444`。本轮新增 1 项 P1 和 3 项 P2,另有编码与诊断建议。 + +## P1:当前任务保护证据先落盘 + +此前 `_prepare_attempts()` 只更新内存中的 `active_task`/`active_attempt`,磁盘 `session.json` 仍可能记录上一工程。新任务已经进入 CAD/worker 写入后崩溃时,监督器会读取陈旧任务并把 `uncertain.flag` 写到已完成工程。 + +修复:`run_worker()` 在 `_prepare_attempts()` 和私有桌面准备完成后、启动 CAD worker 前立即原子保存当前 `active_task` 和 `active_attempt`。新增回归模拟连续两个工程,第二个 worker 启动时读取磁盘 session,确认任务与 attempt 已切换。 + +## P2:生成式 VBS 脚本依赖闭合 + +`QPBuildNativeReadScript()` 的内嵌清单现在包含 `QPComponentIdentityKey`、`QPLog` 和 `QPAppendAscii`。原始日志函数随脚本内嵌,避免 fallback 身份首次出现时依赖末尾临时 stub 的隐式行为。原生读取脚本和契约测试重新通过。 + +## P2:工程会话关闭可观察且可强制结束 + +交互关闭等待现在每 15 秒记录 `project_session_awaiting_close`;如果绑定的 `StopControl.force_event` 被置位,会设置 worker abort 并退出等待,交由上层 Job 收尾。普通关闭仍等待已授权的安全边界,不自动强杀 CAD。 + +## 相关收口 + +- supervisor 接收 stdout/stderr 对 UTF-8 失败时按 Windows `mbcs` 回退,保留中文 DxDesigner/VBS 诊断内容。 +- Python 超时诊断继续统一使用 `_component_uid()`。 +- UID 非空时 VBS 身份函数提前返回并 trim,fallback 才读取 Refdes;fallback 日志按位号去重。 +- 进度写入失败日志按路径节流。 + +## 验证 + +- 生命周期、停止、监督结果、身份、VBS guards、无 UID库存:**152 passed**。 +- 原生读取、VBS 契约、项目会话和当前任务落盘回归:**34 passed**。 +- `python -m compileall -q src` 通过。 +- GBK 输出回退断言通过:`'中文'.encode('gbk')` 正确还原。 +- 冻结 EXE smoke 通过;最新源码与包内 VBS SHA256 一致。 + +## 非阻断建议采纳情况 + +- 收尾记录写失败改为 best-effort,保留原始 `application_shutdown_failed`,不让二次磁盘错误覆盖主因。 +- Python fallback 完整回读新增回归;VBS 捕获/恢复链路已有生产脚本加 COM 替身端到端覆盖。 +- 恢复预检缺失字段错误现在包含字段名和 fallback UID,便于现场诊断。 +- 引脚 owner 的二次身份校验保留:它是连接归属的安全证明,去掉虽然可能减少 COM 调用,但会降低错误 pin 被混入连接签名的可检测性;待真实大工程基准后再评估。 + +仍需 Windows 10、真实 DxDesigner 7.9.5 无 UID 工程、中央库/许可证/COM 注册、网络共享和企业策略现场验收。 diff --git a/docs/testing/2026-10-31-pr9-lifecycle-fix.md b/docs/testing/2026-10-31-pr9-lifecycle-fix.md new file mode 100644 index 0000000..9538240 --- /dev/null +++ b/docs/testing/2026-10-31-pr9-lifecycle-fix.md @@ -0,0 +1,33 @@ +# PR #9 生命周期复审修复 + +复审评论:PR #9 评论 91、92、93。阻断问题是:已完成工程的历史 attempt 仍保持 `mutation_possible`,下一工程启动前的普通异常会把整个批次送入 `wait_for_resolution`,保留 CAD/桌面并无限等待强制结束。 + +## 根因与修复 + +`BatchCadRuntime.__exit__` 原先把 `_project_attempts` 中所有历史 attempt 传给不确定性判断。成功工程在 `_checkpoint()` 后已经写入 `project-checkpoint.json` 并证明 CAD/工程关闭,但历史 attempt 文件仍然是 `mutation_possible`,因此 `can_retry_attempt()` 永远返回 False。 + +现在运行时只保留 `_active_attempts` 作为在途写入证据: + +- `_prepare_attempts()` 成功后设置当前 generation 的 attempt 集合; +- `_checkpoint()` 完成 CAD 关闭和 checkpoint 发布后清空 `_active_attempts`; +- `__exit__` 只对当前未完成 attempt 调用 `_retain_uncertain`; +- 历史 attempt 文件、禁止重放证据和 checkpoint 审计仍保留,不会被清除或重新授权重放。 + +这样,已完成工程之后的下一工程备份/启动异常会正常释放自有 CAD/桌面并传播原始异常;当前 attempt 尚未获得关闭 checkpoint 时,仍然保留原有安全等待。 + +## 连带收口 + +- VBS 身份函数在真实 UID 非空时提前返回并去除首尾空格,只有 fallback 才读取 Refdes;fallback WARN 按位号去重,每次捕获按 job 重置。 +- Python 超时诊断使用 `_component_uid()`,无 UID 结果显示 `@REFDES|...`。 +- 进度发布失败告警按路径 15 秒节流,避免磁盘满时每秒刷写日志。 +- 独立 VBS 函数入口在 fallback 告警字典未初始化时惰性创建。 + +## 验证 + +- `tests/v1_1/test_batch_runtime.py tests/v1_1/test_safe_user_stop.py tests/v1_1/test_supervisor_result.py`:**67 passed**。 +- `tests/v1_1/test_accuracy_vbs_guards.py::test_missing_component_uid_uses_stable_refdes_fallback tests/v1_1/test_missing_uid_inventory.py`:**14 passed**。 +- 新增回归 `test_completed_project_does_not_hold_resolution_wait_for_next_project_failure` 覆盖成功工程后下一工程启动异常,断言不进入 `waiting_for_resolution` 且不污染已完成工程的 `uncertain.flag`。 + +Windows 10、真实 DxDesigner 7.9.5 无 UID 工程、中央库/许可证和网络共享仍需现场验证。 + +冻结包已从本次代码重建,`verify_packaged_runtime.py` 通过;`QuickPartReplace.vbs` SHA256 为 `7752FD937EF1513744F49B3A529DC075D4BD81AA743460254306FAA1BC385C92`,`NativeSnapshotReader.vbs` SHA256 为 `46519AA1191DAC97A778D0F4074F2CF3AAF23667E2863A6C7E2057045FCAF099`。 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/testing/evidence/2026-10-30-resource-scan.json b/docs/testing/evidence/2026-10-30-resource-scan.json new file mode 100644 index 0000000..703e0ae --- /dev/null +++ b/docs/testing/evidence/2026-10-30-resource-scan.json @@ -0,0 +1,15 @@ +{ + "date": "2026-10-30", + "scenario": "temporary directories containing valid released resource records; no CAD", + "synchronous_scan_samples": [ + {"released_sessions": 0, "milliseconds": [2.68, 1.83, 1.41]}, + {"released_sessions": 100, "milliseconds": [293.55, 277.36, 313.8]}, + {"released_sessions": 500, "milliseconds": [1650.06, 1626.25, 1569.35]} + ], + "gui_after_background_snapshot": { + "released_sessions": 500, + "gate_refreshes": 100, + "total_milliseconds": 6.63, + "filesystem_scans_during_gate_refreshes": 0 + } +} 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..ec6b0ee 100644 --- a/integration/dxdesigner795/NativeSnapshotReader.vbs +++ b/integration/dxdesigner795/NativeSnapshotReader.vbs @@ -222,7 +222,7 @@ Function QPNativeCollectMode(ByRef SelectedUids, ByVal Token, ByVal Project, ByV Count = 0 QPProgress "native_snapshot_read", 0, Components.Count For Each Comp In Components - Uid = CStr(Comp.UID) + Uid = QPComponentIdentityKey(Comp) If Len(Uid) = 0 Or Seen.Exists(Uid) Then QPNativeReadInvalid "duplicate or empty component UID" Seen.Add Uid, True If SelectedUids.Exists(Uid) Then @@ -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 QPComponentIdentityKey QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines QPLog QPAppendAscii QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ") Pattern.Global = False For Each Name In Names Pattern.Pattern = "^(Function|Sub) " & Name & "\([^\n]*\n[\s\S]*?^End \1[ \t]*" @@ -289,8 +289,7 @@ Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token, If Matches.Count <> 1 Then QPNativeReadInvalid "missing native read routine: " & Name Body = Body & vbCrLf & Matches(0).Value & vbCrLf Next - Body = Body & vbCrLf & "Sub QPLog(ByVal Root,ByVal Level,ByVal Message)" & vbCrLf & "End Sub" & vbCrLf & _ - "Sub QPProgress(ByVal Stage,ByVal Done,ByVal Total)" & vbCrLf & _ + Body = Body & vbCrLf & "Sub QPProgress(ByVal Stage,ByVal Done,ByVal Total)" & vbCrLf & _ " Dim StopPath,Elapsed,CurrentTime,ProgressPath,Stream,SafeFlag" & vbCrLf & _ " If QP_NR_Safe And Len(QP_AttemptToken)>0 Then" & vbCrLf & _ " StopPath=QP_Fso.BuildPath(QP_Root,""monitor-"" & QP_AttemptToken & "".stop"")" & vbCrLf & _ diff --git a/integration/dxdesigner795/QuickPartReplace.vbs b/integration/dxdesigner795/QuickPartReplace.vbs index b3b493d..efffc55 100644 --- a/integration/dxdesigner795/QuickPartReplace.vbs +++ b/integration/dxdesigner795/QuickPartReplace.vbs @@ -25,6 +25,8 @@ Dim QP_RestoreTransactionOpen Dim QP_PinIndex Dim QP_PinContext Dim QP_ComponentUidIndex, QP_ComponentRefdesIndex, QP_ComponentLocationIndex +Dim QP_FallbackIdentityUsed +Dim QP_FallbackIdentityWarnings Dim QP_ComponentIndexContext Dim QP_NativeCallActive Dim QP_BatchMode, QP_OwnedProject, QP_BatchState, QP_BatchCode, QP_BatchMessage @@ -69,6 +71,8 @@ Set QP_ComponentUidIndex = Nothing Set QP_ComponentRefdesIndex = Nothing Set QP_ComponentLocationIndex = Nothing QP_ComponentIndexContext = "" +QP_FallbackIdentityUsed = False +Set QP_FallbackIdentityWarnings = CreateObject("Scripting.Dictionary") QP_CurrentJobId = "" QP_ExitCode = 0 QP_Stage = "session_startup" @@ -412,7 +416,7 @@ Sub QPBuildComponentIndex(ByRef Components) Set Uids = CreateObject("Scripting.Dictionary") Count = 0 For Each Comp In Components - QPAddComponentIndexEntry Uids, CStr(Comp.UID), Comp + QPAddComponentIndexEntry Uids, QPComponentIdentityKey(Comp), Comp Count = Count + 1 If Count Mod 25 = 0 Then QPProgress "index_components", Count, 0 Next @@ -478,8 +482,7 @@ Function QPComponentPins(ByVal ComponentUid) Set AllPins = QP_App.ActiveView.Query(VDM_COMPPIN, VD_ALL) Count = 0 For Each CmpPin In AllPins - Owner = CStr(CmpPin.Component.UID) - If Len(Owner) = 0 Then Err.Raise vbObjectError + 862, "QuickPartReplace", "pin owner UID is empty" + Owner = QPComponentIdentityKey(CmpPin.Component) If Not LocalIndex.Exists(Owner) Then Set Bucket = CreateObject("Scripting.Dictionary") LocalIndex.Add Owner, Bucket @@ -714,8 +717,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 +742,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 +788,45 @@ 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 = Trim(CStr(Uid)) + Exit Function + Else + QP_FallbackIdentityUsed = True + If Len(Trim(CStr(Refdes))) > 0 Then + QPComponentIdentityKey = "@REFDES|" & Trim(CStr(Refdes)) + If Not IsObject(QP_FallbackIdentityWarnings) Then _ + Set QP_FallbackIdentityWarnings = CreateObject("Scripting.Dictionary") + If Not QP_FallbackIdentityWarnings.Exists(LCase(Trim(CStr(Refdes)))) Then + QP_FallbackIdentityWarnings.Add LCase(Trim(CStr(Refdes))), True + QPLog QP_Root, "WARN", "component_uid_missing_using_refdes_fallback refdes=" & CStr(Refdes) + End If + Else + Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "component UID and Refdes are empty" + 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") @@ -963,7 +1014,7 @@ Sub QPFinishBatchItem() For Index = 0 To Records.Item("components").Count - 1 Set Record = Records.Item("components").Item(CStr(Index)) Set Candidate = QPFindUniqueByOldLocation(Record) - If CStr(Candidate.UID) <> CStr(Record("uid")) Then _ + If QPComponentIdentityKey(Candidate) <> CStr(Record("uid")) Then _ Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "saved component UID differs" Plans.Add CStr(Index), Candidate Next @@ -1172,6 +1223,8 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath) Dim Index Dim Selectors, Selector, Uid, ValueAttr, ValueText, MatchedUids, UntouchedCount, NativeRead + QP_FallbackIdentityUsed = False + Set QP_FallbackIdentityWarnings = CreateObject("Scripting.Dictionary") If StrComp(CStr(Command("old_part_number")), CStr(Command("new_part_number")), vbTextCompare) = 0 Then _ Err.Raise vbObjectError + 829, "QuickPartReplace", "source and target must differ" QPInvalidatePinIndex @@ -1185,13 +1238,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 +1288,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 +1301,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) @@ -1264,7 +1317,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath) UntouchedCount = 0 If NativeRead Is Nothing Then For Each Comp In Components - If Not MatchedUids.Exists(CStr(Comp.UID)) Then + If Not MatchedUids.Exists(QPComponentIdentityKey(Comp)) Then QPProgress "capture_untouched", UntouchedCount, Components.Count - Matches.Count Snapshot = Snapshot & QPUntouchedComponent(Comp) UntouchedCount = UntouchedCount + 1 @@ -1318,6 +1371,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,33 +1431,65 @@ Function QPUntouchedIdentity(ByVal State) End Function Function QPUntouchedFullState(ByRef Comp) - Dim Point, Pins, Connections, Attr, AttributeText, Result, Uid - Uid = CStr(Comp.UID) + Dim Point, Pins, Connections, Attr, AttributeText, Result, Uid, Identity + Identity = QPComponentIdentityKey(Comp) Set Point = Comp.GetLocation() - Set Pins = QPComponentPins(Uid) + Set Pins = QPComponentPins(Identity) If Pins.Count = 0 Then ' A genuinely empty query is valid for annotation symbols; COM errors ' still propagate. Selected replacement symbols continue to require pins. Connections = "NO_PINS" Else - Connections = QPConnectionSignatureFromPins(Uid, Pins) + Connections = QPConnectionSignatureFromPins(Identity, 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) @@ -1400,7 +1503,7 @@ Sub QPVerifyUntouched(ByRef Records) ' Read new component/pin proxies after each native mutation, restore or reopen. QPInvalidatePinIndex QPInvalidateComponentIndex - If QPUseNativeSnapshot() Then + If QPUseNativeSnapshot() And Not QP_FallbackIdentityUsed Then If QPTryNativeVerifyUntouched(Records) Then Exit Sub End If Set Components = QP_App.ActiveView.Query(VDM_COMP, VD_ALL) @@ -1409,7 +1512,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 +1574,13 @@ Function QPSnapshotComponent(ByRef Comp) Dim ErrorNumber Dim ErrorSource Dim ErrorDescription - Dim Uid, Pins + Dim Uid, Identity, Pins - Uid = CStr(Comp.UID) + Identity = QPComponentIdentityKey(Comp) Set Point = Comp.GetLocation() - Set Pins = QPComponentPins(Uid) - Connections = QPConnectionSignatureFromPins(Uid, Pins) - Result = "C" & vbTab & QPEncode(Uid) & vbTab & _ + Set Pins = QPComponentPins(Identity) + Connections = QPConnectionSignatureFromPins(Identity, Pins) + 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 +1591,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 +1633,7 @@ Function QPSortSnapshotLines(ByVal Text) End Function Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr) - Dim Instance, Point, ErrorNumber, ErrorDescription + Dim Instance, Value, Point, ErrorNumber, ErrorDescription, Field Dim LocationX Dim LocationY Dim Color @@ -1539,24 +1642,52 @@ 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 + Err.Clear + On Error GoTo 0 + Err.Raise vbObjectError + 836, "QuickPartReplace.Snapshot", "attribute InstanceValue unreadable; replacement blocked before mutation" + End If + Err.Clear + Value = CStr(Attr.Value) + If Err.Number <> 0 Then + Err.Clear + On Error GoTo 0 + Err.Raise vbObjectError + 836, "QuickPartReplace.Snapshot", "attribute Value unreadable; replacement blocked before mutation" + End If + Err.Clear + Set Point = Nothing Set Point = Attr.GetLocation() - LocationX = CStr(Point.X) - LocationY = CStr(Point.Y) ErrorNumber = Err.Number - ErrorDescription = Err.Description + Err.Clear + LocationX = "" + LocationY = "" + If ErrorNumber = 0 Then + If Not Point Is Nothing Then + LocationX = CStr(Point.X) + LocationY = CStr(Point.Y) + End If + End If + If Len(LocationX) = 0 Or Len(LocationY) = 0 Then + 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") + For Each Field In Array(LocationX, LocationY, Color, Font, Size, Orientation, Visible) + If Len(CStr(Field)) = 0 Then Err.Raise vbObjectError + 836, _ + "QuickPartReplace.Snapshot", "attribute display state unreadable; replacement blocked before mutation" + Next 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 +1729,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 = "" + QPLog QP_Root, "WARN", "optional_display_field_unavailable field=" & CStr(FieldName) & _ + " description=" & CStr(ErrorDescription) Else QPOptionalAttributeField = Value End If @@ -1636,9 +1768,9 @@ Sub QPRestoreAfterOperatorReplace(ByRef Command, ByVal SnapshotPath) For Index = 0 To Records.Item("components").Count - 1 Set ComponentRecord = Records.Item("components").Item(CStr(Index)) Set Candidate = QPFindUniqueByOldLocation(ComponentRecord) - If Used.Exists(CStr(Candidate.UID)) Then _ + If Used.Exists(QPComponentIdentityKey(Candidate)) Then _ Err.Raise vbObjectError + 831, "QuickPartReplace", "duplicate replacement association" - Used.Add CStr(Candidate.UID), True + Used.Add QPComponentIdentityKey(Candidate), True QPRestorePreflight Candidate, ComponentRecord, Records.Item("attributes"), _ CStr(Command("new_part_number")), QPValueMode(Command) Plans.Add CStr(Index), Candidate @@ -1795,9 +1927,9 @@ Sub QPRecoverSavedJob(ByRef Command, ByVal SnapshotPath) QPRequireSnapshotContext SnapshotPath Set Record = Records.Item("components").Item(CStr(Index)) Set Candidate = QPFindUniqueByOldLocation(Record) - If CStr(Candidate.UID) <> CStr(Record("uid")) Or Used.Exists(CStr(Candidate.UID)) Then _ + If QPComponentIdentityKey(Candidate) <> CStr(Record("uid")) Or Used.Exists(QPComponentIdentityKey(Candidate)) Then _ Err.Raise vbObjectError + 865, "QuickPartReplace.Recovery", "saved component UID changed or duplicated" - Used.Add CStr(Candidate.UID), True + Used.Add QPComponentIdentityKey(Candidate), True PartNumber = QPRequiredAttributeValue(Candidate, "Part Number") If StrComp(PartNumber, CStr(Command("old_part_number")), vbTextCompare) = 0 Then OldCount = OldCount + 1 @@ -1904,7 +2036,7 @@ End Function Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _ ByRef Attributes, ByVal NewPartNumber, ByVal ValueMode) - Dim Index + Dim Index, Field Dim Record Dim LabelAttr Dim ValueAttr @@ -1914,7 +2046,7 @@ Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _ Dim CurrentConnections Dim SavedConnections - If CStr(Comp.UID) <> CStr(ComponentRecord("uid")) Or _ + If QPComponentIdentityKey(Comp) <> CStr(ComponentRecord("uid")) Or _ CStr(Comp.Refdes) <> CStr(ComponentRecord("refdes")) Then _ Err.Raise vbObjectError + 831, "QuickPartReplace", "replacement component UID or Refdes differs from snapshot; no restoration writes allowed" If StrComp(QPRequiredAttributeValue(Comp, "Part Number"), _ @@ -1932,6 +2064,16 @@ Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _ Err.Raise vbObjectError + 804, "QuickPartReplace", _ "connection validation failed" End If + For Index = 0 To Attributes.Count - 1 + Set Record = Attributes.Item(CStr(Index)) + If CStr(Record("uid")) = CStr(ComponentRecord("uid")) Then + For Each Field In Array("x", "y", "color", "font", "size", "orientation", "visible") + If Len(CStr(Record(Field))) = 0 Then Err.Raise vbObjectError + 836, _ + "QuickPartReplace.Snapshot", "attribute display state unavailable field=" & CStr(Field) & _ + "; uid=" & CStr(ComponentRecord("uid")) & "; no restoration writes allowed" + Next + End If + Next If ValueMode = "preserve" Or ValueMode = "custom" Then Exit Sub HasOldValue = False Set ValueRecord = Nothing @@ -2597,8 +2739,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) @@ -2644,7 +2794,7 @@ End Function Function QPConnectionSignature(ByRef Comp) Dim Pins, Uid - Uid = CStr(Comp.UID) + Uid = QPComponentIdentityKey(Comp) Set Pins = QPComponentPins(Uid) QPConnectionSignature = QPConnectionSignatureFromPins(Uid, Pins) End Function @@ -2673,7 +2823,7 @@ Function QPConnectionSignatureFromPins(ByVal ComponentUid, ByRef Pins) PinReadCount = 0 For Each PinIndexKey In Pins.Keys Set CmpPin = Pins.Item(CStr(PinIndexKey)) - If StrComp(CStr(CmpPin.Component.UID), ComponentUid, _ + If StrComp(QPComponentIdentityKey(CmpPin.Component), ComponentUid, _ vbBinaryCompare) = 0 Then Set Point = CmpPin.GetLocation() PinKey = CStr(CmpPin.Number) & "|" & CStr(Point.X) & "|" & _ diff --git a/src/dx_productivity/trial_task_center/batch_runtime.py b/src/dx_productivity/trial_task_center/batch_runtime.py index 953cfc2..4a25f0c 100644 --- a/src/dx_productivity/trial_task_center/batch_runtime.py +++ b/src/dx_productivity/trial_task_center/batch_runtime.py @@ -101,6 +101,9 @@ class BatchCadRuntime: self._project_attempts = {} self._logging = None self._active_evidence = None + self._active_attempts = [] + self._close_failure = None + self._close_failure_rebuild = False def bind_logs(self, context, task_ids): """Keep every participating task's final release outcome visible.""" @@ -119,7 +122,10 @@ class BatchCadRuntime: def __exit__(self, error_type, error, traceback): if error is not None and self._active_evidence is not None: - attempts = [item for values in self._project_attempts.values() for item in values] + # Only the current generation can still own an in-flight CAD write. + # Earlier projects retain audit evidence but their closed checkpoint + # proves that they no longer need the live CAD/desktop wait. + attempts = list(self._active_attempts) try: self._retain_uncertain(self._active_evidence, None, attempts, error) except WorkerMonitorError: @@ -252,6 +258,7 @@ class BatchCadRuntime: 'session_id': self.session_id, 'generation': self.generation, 'attempt_id': attempt_id, 'evidence_root': str(root), 'project_path': str(project), 'project_closed': True, 'worker_closed': True, 'cad_pid': self.record['cad_pid'], 'cad_guid': self.record['cad_guid']}) + self._active_attempts = [] def run_worker(self, command, timeout, *, abort_event=None, cooperative_abort=False): from .discovery_cache import project_fingerprint @@ -270,7 +277,12 @@ class BatchCadRuntime: self._save() generation = self.generation + (self.desktop is None) project, directory, expected = self._prepare_attempts(root, command, attempt_id, generation) + self._active_attempts = list(expected) self._ensure_desktop(abort_event) + # Make the current task/attempt durable before any worker or CAD + # mutation can begin. A crash in the following window must protect + # this task rather than a stale completed task from session.json. + self._save(active_task=self.record['active_task'], active_attempt=attempt_id) before = project_fingerprint(project, self.root) status = internal_path(root / 'status.tsv') status_before = status.read_bytes() if status.exists() else None @@ -381,13 +393,22 @@ class BatchCadRuntime: if self.record['state'] == 'released': return reason = reason or ('rebuild' if rebuilding else 'batch_end') + if self._close_failure is not None and self._close_failure_rebuild and reason != 'manual_retry': + raise BatchResourceCleanupError(str(self._close_failure), completed_result=self.result, + cleanup_error=self._close_failure) + if reason == 'manual_retry': + self._close_failure = None started = time.monotonic() self._release_log('started', reason=reason) try: self._close_resources(rebuilding=rebuilding) except Exception as error: + self._close_failure = error + self._close_failure_rebuild = rebuilding self._release_log('failed', reason=reason, elapsed=time.monotonic() - started, error=error) raise + self._close_failure = None + self._close_failure_rebuild = False self._release_log('succeeded', reason=reason, elapsed=time.monotonic() - started) def _close_resources(self, *, rebuilding=False): @@ -404,8 +425,10 @@ class BatchCadRuntime: except Exception as error: if rebuilding: self.record['shutdown_warning'] = str(error) - else: - errors.append(str(error)) + # A rebuild must not proceed while the previous CAD owner has + # an unconfirmed shutdown. Keep the warning for diagnostics, + # but retain the failure in the resource gate as well. + errors.append(str(error)) finally: cad_record = getattr(cad, 'record', {}) cad_record = cad_record if isinstance(cad_record, dict) else {} diff --git a/src/dx_productivity/trial_task_center/batch_supervisor.py b/src/dx_productivity/trial_task_center/batch_supervisor.py index 45c4a56..c8d5073 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 @@ -23,6 +24,15 @@ _LIVE_SUPERVISORS = {} _WRITE_OPERATIONS = frozenset({'execute_task_queue', 'execute_quick_run_queue', 'execute_quick_run', 'recover_quick_run'}) +def _decode_process_output(value): + if not isinstance(value, bytes): + return str(value) + try: + return value.decode('utf-8') + except UnicodeDecodeError: + return value.decode('mbcs' if os.name == 'nt' else 'utf-8', errors='replace') + + def _mark_interrupted(root, journal, reason): from .batch_resources import write_record, _valid_record from .stop_control import protect_interruption @@ -59,29 +69,46 @@ 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'} + and process.poll() is not None): + 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): """Do not turn a GUI/transport exception into an unconfirmed Job kill.""" from .batch_resources import write_record control.set() + control.waiting_for_resolution = True + control.resolution_error = str(error) control.control_error = '控制通信异常,已请求安全停止并保留应用;可导出诊断或明确强制结束:' + str(error) journal['protect_all_targets'] = True try: @@ -93,7 +120,14 @@ def _retain_supervisor_after_control_error(root, journal, process, control, erro {'version': VERSION, 'batch_id': journal['batch_id'], 'attempt_id': journal['attempt_id']}) except OSError: pass # Keep ownership; failure to request stop is not permission to kill. + last_notice = time.monotonic() while True: + if time.monotonic() - last_notice >= 15: + import logging + logging.getLogger(__name__).warning( + 'supervisor_retained_waiting_for_resolution batch_id=%s error=%s', + journal['batch_id'], control.control_error) + last_notice = time.monotonic() if control.force_event.is_set(): try: _force_owned_supervisor(root, journal, process) @@ -218,7 +252,7 @@ def _environment(): def _finish_response(root, journal, response, context): """A supervisor exit may release its last USER reference. Never repeat work.""" from .batch_resources import cleanup_pending_resources, write_record - from .supervisor_result import validated_outcome, resource_pending_error + from .supervisor_result import validated_outcome, resource_pending_error, application_shutdown_error result, error, primary = validated_outcome(journal['operation'], response) pending = cleanup_pending_resources(root, logging_context=context) related = tuple(item for item in pending if item.get('session_id') == journal['batch_id'] or not item.get('session_id')) @@ -230,18 +264,34 @@ def _finish_response(root, journal, response, context): if related: raise resource_pending_error(journal['operation'], result, error, primary) resource_path = internal_path(Path(root) / 'batch-resources' / journal['batch_id'] / 'session.json') + def preserve_shutdown_failure(detail): + try: + write_record(Path(root) / 'batch-resources' / journal['batch_id'] / 'supervisor.json', journal) + except OSError as record_error: + context.logger.error('supervisor_shutdown_record_failed batch_id=%s error=%s', + journal['batch_id'], record_error) + journal['shutdown_record_error'] = str(record_error) try: resource = json.loads(resource_path.read_text(encoding='utf-8')) except FileNotFoundError: resource = {} - shutdown = resource.get('cad_shutdown') or {} + except (OSError, ValueError, TypeError) as failure: + journal.update(state='blocked', shutdown_record_error=str(failure)) + preserve_shutdown_failure(failure) + raise application_shutdown_error(result, error, primary) from failure + shutdown = resource.get('cad_shutdown') if isinstance(resource, dict) else None + if shutdown is None and isinstance(resource, dict): + shutdown = {} + if (not isinstance(shutdown, dict) + or not isinstance(shutdown.get('cleanup_errors', ()), (list, tuple)) + or any(not isinstance(item, dict) for item in shutdown.get('cleanup_errors', ()))): + journal.update(state='blocked', shutdown_record_error='invalid shutdown record shape') + preserve_shutdown_failure('invalid shutdown record shape') + raise application_shutdown_error(result, error, primary) if (shutdown.get('orderly_shutdown') is False or shutdown.get('forced_termination') or shutdown.get('diagnostic_errors') or any(item.get('stage') != 'desktop_release' for item in shutdown.get('cleanup_errors', ()))): - failure = TaskCenterError('application_shutdown_failed', - '已核验业务结果保留,但应用未正常收尾;请查看退出诊断,不能将资源消失视为正常退出。') - failure.completed_result, failure.cleanup_error = result, error - raise failure + raise application_shutdown_error(result, error, primary) if response.get('business_succeeded'): if response.get('error'): context.logger.info('supervisor_resource_recovery_passed batch_id=%s business_replayed=false', journal['batch_id']) @@ -293,29 +343,55 @@ 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): + decoded = _decode_process_output(line) + stdout_tail.extend(decoded) + else: + decoded = str(line) + stdout_tail.extend(decoded) try: - messages.put(json.loads(line)) + messages.put(json.loads(decoded)) except (ValueError, UnicodeError): messages.put({'type': 'invalid'}) finally: 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(_decode_process_output(line)) + 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: @@ -331,7 +407,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: @@ -346,6 +423,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']) @@ -375,20 +453,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')) @@ -407,13 +522,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..8988903 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}; " @@ -519,7 +558,7 @@ def replacement_matches(components, records, target, require_part_label, *, libr if expected_name is not None and (part_name != expected_name or (expected_label and part_label != expected_label)): raise ValueError(f'Native replacement does not match the selected target library identity; uid={candidate.UID}') - matches.append(str(candidate.UID)) + matches.append(_component_uid(candidate)) if len(set(matches)) != len(records): raise ValueError("Duplicate replacement association") return matches @@ -533,7 +572,7 @@ 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 ( @@ -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) @@ -781,10 +820,10 @@ def run_native(snapshot, report): refs = {record[2] for record in records} report['replacement_timeout'].update( component_count=len(current), - components=[dict(uid=str(c.UID), refdes=str(c.Refdes), + components=[dict(uid=_component_uid(c), refdes=str(c.Refdes), attributes={name: attribute_value(c, name) for name in ('Part Number', 'Part Name', 'Part Label', 'Value')}) - for c in current if str(c.UID) in wanted or str(c.Refdes) in refs]) + for c in current if _component_uid(c) in wanted or str(c.Refdes) in refs]) except Exception as error: report['replacement_timeout']['component_readback_error'] = str(error) try: diff --git a/src/dx_productivity/trial_task_center/project_session.py b/src/dx_productivity/trial_task_center/project_session.py index 879d7d6..c8358d1 100644 --- a/src/dx_productivity/trial_task_center/project_session.py +++ b/src/dx_productivity/trial_task_center/project_session.py @@ -116,8 +116,17 @@ class ProjectSessionWorker: if interactive: # The monitor owns the safe/unsafe distinction. Do not # turn a GUI close or ordinary pause into a timed kill. + force_event = getattr(getattr(self.runtime, 'stop_event', None), 'force_event', None) + last_notice = time.monotonic() while not self._done.wait(.1): - pass + if force_event is not None and force_event.is_set(): + self._abort.set() + break + if time.monotonic() - last_notice >= 15: + import logging + logging.getLogger(__name__).warning( + 'project_session_awaiting_close token=%s', self.token) + last_notice = time.monotonic() close_grace = 60 if self.private_desktop else min(30, max(10, self._timeout)) if not self._done.wait(close_grace): self._abort.set() diff --git a/src/dx_productivity/trial_task_center/stop_control.py b/src/dx_productivity/trial_task_center/stop_control.py index 7f016b9..98427d9 100644 --- a/src/dx_productivity/trial_task_center/stop_control.py +++ b/src/dx_productivity/trial_task_center/stop_control.py @@ -31,6 +31,9 @@ def protect_interruption(root, reason): # A previous create/write/fsync may have failed. Existence alone does # not establish durable evidence; preserve its bytes but sync again. with marker.open('r+b') as existing: + if not existing.read(1): + existing.write(str(reason).encode('utf-8')) + existing.flush() os.fsync(existing.fileno()) else: with stream: 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/src/dx_productivity/trial_task_center/supervisor_result.py b/src/dx_productivity/trial_task_center/supervisor_result.py index 976ea62..905c2a4 100644 --- a/src/dx_productivity/trial_task_center/supervisor_result.py +++ b/src/dx_productivity/trial_task_center/supervisor_result.py @@ -48,3 +48,24 @@ def resource_pending_error(operation, result, error, primary_error=None): elif getattr(error, 'bom_report', None) is not None: failure.bom_report = replace(error.bom_report, preview=None) return failure + + +def application_shutdown_error(result, error, primary_error=None): + """Retain diagnostics, but never expose a plan after abnormal shutdown.""" + from dataclasses import replace + from .bom_import_report import BomImportReport + from .errors import TaskCenterError + + if isinstance(result, BomImportReport): + result = replace(result, preview=None) + failure = TaskCenterError('application_shutdown_failed', + '应用未正常收尾,已有报告和核验结果保留;请查看退出诊断。') + failure.completed_result = result + failure.cleanup_error = error + failure.primary_error = primary_error + for candidate in (result, getattr(error, 'bom_report', None), + getattr(primary_error, 'bom_report', None)): + if isinstance(candidate, BomImportReport): + failure.bom_report = replace(candidate, preview=None) + break + return failure diff --git a/src/dx_productivity/trial_task_center/ui/dialogs.py b/src/dx_productivity/trial_task_center/ui/dialogs.py index 6d705b9..39a64fd 100644 --- a/src/dx_productivity/trial_task_center/ui/dialogs.py +++ b/src/dx_productivity/trial_task_center/ui/dialogs.py @@ -1,5 +1,6 @@ """Task metadata and replacement-rule editing dialogs.""" from PySide6.QtCore import QSignalBlocker, QThreadPool +from shiboken6 import isValid from PySide6.QtWidgets import (QWidget, QDialog, QLineEdit, QTextEdit, QFormLayout, QLabel, QDialogButtonBox, QCheckBox, QComboBox, QPushButton, QHBoxLayout) from .workers import ServiceWorker @@ -151,7 +152,8 @@ class _MappingDialog(QDialog): worker = ServiceWorker(lambda: self._cell_loader(pn)) self._cell_workers.add(worker) def current(): - if self._cell_closed or generation != self._cell_generation or pn != self.new_edit.text().strip(): + if (not isValid(self) or self._cell_closed or generation != self._cell_generation + or pn != self.new_edit.text().strip()): return False try: same_context = context == self._cell_context() diff --git a/src/dx_productivity/trial_task_center/ui/execution_state.py b/src/dx_productivity/trial_task_center/ui/execution_state.py index 9d91cf1..cb3dbd5 100644 --- a/src/dx_productivity/trial_task_center/ui/execution_state.py +++ b/src/dx_productivity/trial_task_center/ui/execution_state.py @@ -3,6 +3,15 @@ from dataclasses import dataclass from ..models import TaskStatus +def pending_batch_tasks(tasks, group_id): + """Use the persisted batch order for both execution and its UI controls.""" + if not group_id: + return () + return tuple(sorted((task for task in tasks if task.group_id == group_id + and task.status not in {TaskStatus.DELETED, TaskStatus.COMPLETED}), + key=lambda task: task.group_position)) + + @dataclass(frozen=True, slots=True) class ExecutionState: editable: bool = False diff --git a/src/dx_productivity/trial_task_center/ui/main_window.py b/src/dx_productivity/trial_task_center/ui/main_window.py index cb5a9de..1b388bc 100644 --- a/src/dx_productivity/trial_task_center/ui/main_window.py +++ b/src/dx_productivity/trial_task_center/ui/main_window.py @@ -12,7 +12,7 @@ from ..errors import TaskCenterError from ..models import TaskStatus from ..version import VERSION from .dialogs import _TaskDialog, _MappingDialog -from .execution_state import execution_state +from .execution_state import execution_state, pending_batch_tasks from .mapping_table import MappingTable from .messages import ERROR_TEXT, _format_live_log_line, format_operation_error from .project_plan import DirectoryTaskDialog, ProjectPlanPanel @@ -24,6 +24,7 @@ from .icons import apply_icons, icon from .task_list import TaskList from .workers import ServiceWorker from .queries import LatestQuery +from .resource_status import ResourceStatus from .task_log import TaskLogPresenter from .execution_feedback import format_feedback from .operation_state import ContextReadPhase, OperationPhase, OperationState @@ -100,6 +101,7 @@ class TrialTaskCenterWindow(MaterialMainWindow): self._context_query = LatestQuery(self) self._task_query = LatestQuery(self) self._feedback_query = LatestQuery(self) + self._resource_status = ResourceStatus(lambda: service.pending_batch_resources(), self) self._run_started_at = 0.0 self._quick_run_elapsed = QElapsedTimer() self._quick_run_timer = QTimer(self) @@ -169,6 +171,8 @@ class TrialTaskCenterWindow(MaterialMainWindow): saved = self._layout_settings.value(f'single_project/{name}') if saved is not None: widget.restoreState(saved) + self._resource_status.changed.connect(self._update_gates) + self._resource_status.refresh() self._refresh_task_list() self._update_gates() if (self.service.layout_settings_path.parent / 'dsh' / 'incidents').is_dir(): @@ -337,6 +341,7 @@ class TrialTaskCenterWindow(MaterialMainWindow): self._context_query.close() self._task_query.close() self._feedback_query.close() + self._resource_status.close() self._log_presenter.query.close() self._quick_run_timer.stop() if self._dsh_view is not None: @@ -413,9 +418,9 @@ class TrialTaskCenterWindow(MaterialMainWindow): def _update_gates(self): busy = bool(self._workers) or self._loading - resources_pending = bool(self.service.pending_batch_resources()) - self.run_panel.cleanup_resources_button.setVisible(resources_pending) - self.run_panel.cleanup_resources_button.setEnabled(resources_pending and not busy) + resources_pending = self._resource_status.blocked + self.run_panel.cleanup_resources_button.setVisible(resources_pending and self._resource_status.known) + self.run_panel.cleanup_resources_button.setEnabled(resources_pending and self._resource_status.known and not busy) state = execution_state(self._context, busy=busy, dirty=self.plan_panel.table.dirty or self.plan_panel.table.has_pending_editor()) group_unsaved = self._group_has_unsaved_plans() if state.editable or state.new_round else False @@ -473,7 +478,9 @@ class TrialTaskCenterWindow(MaterialMainWindow): and t.status is not TaskStatus.DELETED} group_dirty = self.plan_panel.has_unsaved_plan(group_ids) group_active = any(t.status in {TaskStatus.RUNNING, TaskStatus.PAUSED} for t in self._tasks if t.task_id in group_ids) - self.task_list.queue_button.setEnabled(grouped and not busy and not group_dirty and not bool(context.interrupted) and not resources_pending) + batch_pending = pending_batch_tasks(self._tasks, context.task.group_id if grouped else None) + self.task_list.queue_button.setEnabled(bool(batch_pending) and not busy and not group_dirty + and not context.interrupted and not resources_pending) for button in (self.task_list.up_button, self.task_list.down_button): button.setEnabled(grouped and not busy and not group_active and self.task_list.sort.currentData() == 'execution' @@ -484,7 +491,9 @@ class TrialTaskCenterWindow(MaterialMainWindow): if self._loading and not self._message: self.run_panel.operator_band.setText('正在读取任务,可继续选择其他任务。') if not busy: - self.run_panel.execution_status.setText('资源待清理' if resources_pending else '任务读取失败' if read_failed else + self.run_panel.execution_status.setText('正在检查后台资源' if not self._resource_status.known else + '资源状态读取失败' if self._resource_status.error is not None else + '资源待清理' if resources_pending else '任务读取失败' if read_failed else '结果待核验' if context and context.interrupted else '空闲') def _group_has_unsaved_plans(self): @@ -654,8 +663,7 @@ class TrialTaskCenterWindow(MaterialMainWindow): if not self.task_list.queue_button.isEnabled(): return group = self._context.task.group_id - tasks = sorted((t for t in self._tasks if t.group_id == group and t.status not in {TaskStatus.DELETED, TaskStatus.COMPLETED}), - key=lambda t: t.group_position) + tasks = pending_batch_tasks(self._tasks, group) if not tasks: return token, ids = self._project_queues.get(group, (None, tuple(t.task_id for t in tasks))) @@ -760,10 +768,10 @@ class TrialTaskCenterWindow(MaterialMainWindow): def _startup_resource_recheck(self): if self._closed: return - if self._workers or self._loading: + if self._workers or self._loading or not self._resource_status.known or self._resource_status.busy: QTimer.singleShot(250, self._startup_resource_recheck) return - if self.service.pending_batch_resources(): + if self._resource_status.blocked: self._update_gates() self._cleanup_batch_resources() # Resource-only; never resumes a queue. @@ -862,8 +870,7 @@ class TrialTaskCenterWindow(MaterialMainWindow): def _confirm_quick_run(self, scope, selected_projects, enabled_rules): context = self._context names = context.project.path if scope == 'current' else '\n'.join( - f'{t.group_position+1}. {t.name}' for t in sorted(self._tasks, key=lambda t: t.group_position) - if t.group_id == context.task.group_id and t.status not in {TaskStatus.DELETED, TaskStatus.COMPLETED}) + f'{t.group_position+1}. {t.name}' for t in pending_batch_tasks(self._tasks, context.task.group_id)) text = (f'执行范围:{names}\n\n' + (f'勾选目标/规则:{enabled_rules}\n' if scope == 'current' else f'按顺序处理 {selected_projects} 个工程任务。\n') + '逐项执行原生 Replace Part + Library Wins,保留每行 Value/NC 策略;创建备份并自动保存、关闭、重开核验。' + '\n默认在受监督的私有桌面执行,结束后释放自有 CAD 会话;主桌面可继续查看其他文件。请确保待处理工程未在其他 CAD 会话中打开。' @@ -1041,6 +1048,11 @@ class TrialTaskCenterWindow(MaterialMainWindow): except Exception as error: operation_error = error self._close_pending = False + if execution: + # A result renderer is part of the worker lifecycle. + # If it fails, release the execution controls just as + # the service failure path does. + self._finish_run() self.operation_state = self.operation_state.finish(error) self._show_error(error) def failed(error): @@ -1062,6 +1074,7 @@ class TrialTaskCenterWindow(MaterialMainWindow): self._workers.discard(worker) if not isValid(self) or self._closed: return + self._resource_status.refresh() self.plan_panel.finish_operation(error=operation_error) self._close_saving = False if self._close_pending and not self._closed: diff --git a/src/dx_productivity/trial_task_center/ui/resource_status.py b/src/dx_productivity/trial_task_center/ui/resource_status.py new file mode 100644 index 0000000..50f7ce0 --- /dev/null +++ b/src/dx_productivity/trial_task_center/ui/resource_status.py @@ -0,0 +1,69 @@ +"""Background resource checks with a conservative snapshot for UI controls.""" +from PySide6.QtCore import QObject, QTimer, Signal + +from .queries import LatestQuery + + +class ResourceStatus(QObject): + changed = Signal() + + def __init__(self, read, parent=None): + super().__init__(parent) + self._read = read + self._query = LatestQuery(self) + self._query.settled.connect(self._settled) + self._refresh_again = False + self._closed = False + self.known = False + self.records = () + self.error = None + self._timer = QTimer(self) + self._timer.setInterval(2000) + self._timer.timeout.connect(self._poll) + self._timer.start() + + @property + def busy(self): + return self._query.busy + + @property + def blocked(self): + return not self.known or self.error is not None or bool(self.records) + + def refresh(self): + if self._closed: + return + if self.busy: + # An operation finished during this read. Recheck afterward rather + # than letting an older result clear the resource gate. + self._refresh_again = True + return + self._query.request(self._read, self._received, self._failed) + + def _poll(self): + # Slow reads must finish; periodic polling must not invalidate them. + if not self.busy: + self.refresh() + + def _received(self, records): + if self._refresh_again: + return + self.records = tuple(records) + self.known = True + self.error = None + self.changed.emit() + + def _failed(self, error): + self.known = True + self.error = error + self.changed.emit() + + def _settled(self): + if self._refresh_again and not self._closed: + self._refresh_again = False + self.refresh() + + def close(self): + self._closed = True + self._timer.stop() + self._query.close() diff --git a/src/dx_productivity/trial_task_center/worker_monitor.py b/src/dx_productivity/trial_task_center/worker_monitor.py index c6ac3fd..ebf59a9 100644 --- a/src/dx_productivity/trial_task_center/worker_monitor.py +++ b/src/dx_productivity/trial_task_center/worker_monitor.py @@ -9,8 +9,8 @@ from pathlib import Path import queue import re import subprocess -import threading import time +import threading from uuid import uuid4 @@ -18,6 +18,9 @@ class WorkerMonitorError(subprocess.SubprocessError): pass +_PROGRESS_FAILURE_LOGGED: dict[str, float] = {} + + class ProgressWatch: def __init__(self, token: str, job_id: str, idle_timeout: float, started: float) -> None: if idle_timeout <= 0: @@ -131,8 +134,15 @@ def _atomic_text(path: Path, text: str) -> None: def _publish_progress(path: Path, snapshot: dict) -> None: try: _atomic_text(path, json.dumps(snapshot, ensure_ascii=False)) - except OSError: - pass # Display telemetry failure must not terminate a possible CAD write. + except OSError as error: + # Telemetry cannot interrupt a CAD write, but the lost state must remain diagnosable. + import logging + now = time.monotonic() + key = str(path) + last = _PROGRESS_FAILURE_LOGGED.get(key, 0.0) + if now - last >= 15: + logging.getLogger(__name__).warning('worker_progress_publish_failed path=%s error=%s', path, error) + _PROGRESS_FAILURE_LOGGED[key] = now def _decode(data: bytes) -> str: 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..e2a9e4e 100644 --- a/tests/integration/test_quick_part_replace_contract.py +++ b/tests/integration/test_quick_part_replace_contract.py @@ -26,7 +26,7 @@ def test_quick_part_replace_is_an_ascii_current_sheet_automatic_contract() -> No "Comp.Orientation", "Comp.Scale", "QP_App.ActiveView.Query(VDM_COMPPIN, VD_ALL)", - "CmpPin.Component.UID", + "QPComponentIdentityKey(CmpPin.Component)", "Set Connection = CmpPin.Connection", "Set Net = Connection.Net", "Net.UID", @@ -47,6 +47,8 @@ def test_quick_part_replace_is_an_ascii_current_sheet_automatic_contract() -> No 'AddAttribute("Value=" & LabelValue', "QPNativeReplace Command, SnapshotPath", '" --native-helper --snapshot "', + 'QPLog QP_Root', + 'QPAppendAscii', ): assert required in text @@ -117,7 +119,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/integration/test_quick_value_modes.py b/tests/integration/test_quick_value_modes.py index 27648cc..cd098c8 100644 --- a/tests/integration/test_quick_value_modes.py +++ b/tests/integration/test_quick_value_modes.py @@ -14,7 +14,7 @@ FAKES = next(ast.literal_eval(statement.value) for statement in TREE.body for target in statement.targets)) EXTRA = r''' Class ValueComponent - Public UID, Refdes, Orientation, Scale, Attributes, AddCount + Public UID, Refdes, Orientation, Scale, Attributes, AddCount, Selected Private Point Private Sub Class_Initialize() Set Point=New TestPoint @@ -371,6 +371,27 @@ Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"status.tsv")),"saved_ok")>0," ''', routines_transform=_batch_readback_fixture) +def test_missing_uid_capture_restore_save_and_reopen(tmp_path): + run_vbs(tmp_path, 'part_number', ''' +Dim FullChecks, Captured +FullChecks=0 +Command.Add "project_path",QPProjectPath() +Comp.FindAttribute("Part Number").Value="OLD" +QPCaptureAndSelect Command,SnapshotPath +Set Captured=QPReadSnapshot(SnapshotPath) +Require Captured.Item("components").Item("0")("uid")="@REFDES|R1","fallback lost in snapshot" +Require Captured.Item("untouched").Count=0,"target incorrectly captured as untouched" +Comp.FindAttribute("Part Number").Value="NEW" +QP_BatchMode=True:QP_SessionMode=False +QP_OwnedProject=True:QP_OwnedProjectPath=QPProjectPath() +QPRestoreAfterOperatorReplace Command,SnapshotPath +QPFinishBatchItem +Require QP_App.SaveCount=1,"replacement was not saved exactly once" +Require Len(QP_App.ProjectPath)=0,"reopened project was not closed" +Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"status.tsv")),"saved_ok")>0,"missing saved readback proof" +''', before='Comp.UID=""', routines_transform=_batch_readback_fixture) + + def test_non_session_no_match_closes_without_any_save(tmp_path): run_vbs(tmp_path, 'part_number', ''' Dim FullChecks diff --git a/tests/ui/conftest.py b/tests/ui/conftest.py index 11e21f3..82f7a56 100644 --- a/tests/ui/conftest.py +++ b/tests/ui/conftest.py @@ -36,7 +36,7 @@ def ui(qtbot, tmp_path): def settle(): qtbot.waitUntil(lambda:not window._workers and not window._task_query.busy and not window._context_query.busy and not window._feedback_query.busy - and not window._log_presenter.query.busy,timeout=5000) + and not window._log_presenter.query.busy and not window._resource_status.busy,timeout=5000) def open_task(task): window.open_task(task.task_id) qtbot.waitUntil(lambda:not window._context_query.busy and not window._loading,timeout=5000) diff --git a/tests/ui/test_trial_task_center_workflow.py b/tests/ui/test_trial_task_center_workflow.py index 5de50fe..a5ce00f 100644 --- a/tests/ui/test_trial_task_center_workflow.py +++ b/tests/ui/test_trial_task_center_workflow.py @@ -23,6 +23,20 @@ def test_current_task_executes_only_after_confirmation_and_no_match_is_completed assert w.run_panel.new_round_button.isEnabled() +def test_batch_execute_button_tracks_remaining_tasks(ui): + w = ui.window + w._quick_run_confirmer = lambda *_args: True + w._prepare_quick_run() + ui.settle() + # A completed selection must still allow execution of its pending sibling. + assert w.task_list.queue_button.isEnabled() + w.task_list.queue_button.click() + ui.settle() + assert len(ui.calls) == 2 + assert not w.task_list.queue_button.isEnabled(), 'a completed batch must not offer a no-op execution button' + assert w.run_panel.new_round_button.isEnabled() + + @pytest.mark.parametrize('value',[False,True]) def test_changed_value_policy_stops_stale_confirmation_before_worker(ui,value): w=ui.window;task=ui.tasks[0] @@ -60,6 +74,17 @@ def test_regular_mutation_locks_navigation_until_completion(ui,qtbot): ui.open(ui.tasks[1]) +def test_execution_callback_failure_releases_run_controls(ui,qtbot): + w=ui.window + w._begin_run(ui.tasks[0].task_id) + w._run_async(lambda: 'result', lambda _result: (_ for _ in ()).throw(RuntimeError('render failed')), + expected_task_id=ui.tasks[0].task_id) + ui.settle() + assert w._batch_stop is None + assert not w.run_panel.busy_indicator.isVisible() + assert not w.run_panel.force_stop_button.isVisible() + + def test_close_during_execution_requests_boundary_pause_and_waits(ui,qtbot): w=ui.window;started,release=Event(),Event() def work(): diff --git a/tests/ui/test_execution_feedback.py b/tests/ui/test_ui_execution_feedback.py similarity index 75% rename from tests/ui/test_execution_feedback.py rename to tests/ui/test_ui_execution_feedback.py index 686984e..bd38162 100644 --- a/tests/ui/test_execution_feedback.py +++ b/tests/ui/test_ui_execution_feedback.py @@ -1,5 +1,6 @@ -"""Readable logs and execution feedback on the current project-task interface.""" +"""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..bdd0d40 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_unreadable_display_fields_retain_missing_state(tmp_path): + output = run_vbs(tmp_path, ['QPOptionalAttributeField', 'QPReadOptionalAttributeValue'], ''' +Dim Attr +Set Attr = New BrokenOptionalAttribute +WScript.Echo "color=" & QPOptionalAttributeField(Attr, "color") +WScript.Echo "x=" & 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() == ['color=', 'x='] + + +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,70 @@ WScript.Echo Replace(Text,vbCrLf,"") assert output.splitlines() == ['1', 'A\tu1\tValue\tSAME\tSAME\t0\t0\t1\t2\t3\t0\t1'] +@pytest.mark.parametrize('field', ['Color', 'Font', 'Size', 'Orientation', 'Visible', 'location']) +def test_unreadable_style_blocks_snapshot_and_never_writes_default(tmp_path, field): + fake = FULL_FAKE + if field == 'location': + fake = fake.replace('Function GetLocation()\n Dim Point:', + 'Function GetLocation()\n If FailRead Then Err.Raise 1234,"FakeCOM","unavailable"\n Dim Point:', 1) + record_field = 'x' + else: + fake = fake.replace('Color, Font, Size, Orientation, Visible', + ', '.join(x for x in ['Color', 'Font', 'Size', 'Orientation', 'Visible'] if x != field), 1) + marker = ' Private Sub Class_Initialize\n Name = "Value"' + fake = fake.replace(marker, f''' Private Stored + Property Get {field} + If FailRead Then Err.Raise 1234,"FakeCOM","unavailable" + {field}=Stored + End Property + Property Let {field}(ByVal NewValue) + Stored=NewValue:Writes=Writes+1 + End Property + Private Sub Class_Initialize + Name = "Value"''', 1) + record_field = field.lower() + output = run_vbs(tmp_path, ['QPSnapshotAttribute', 'QPOptionalAttributeField', 'QPEncode', + 'QPRestoreDisplay'], f''' +Dim Attr,Text,Failure,Record,Key,FailRead,Writes +FailRead=False +Set Attr=New FullAttribute +Writes=0:FailRead=True +On Error Resume Next +Text=QPSnapshotAttribute("u1",Attr) +Failure=Err.Number:Err.Clear +On Error GoTo 0 +WScript.Echo CStr(Failure<>0) +FailRead=False +Set Record=CreateObject("Scripting.Dictionary") +For Each Key In Array("x","y","color","font","size","orientation","visible") + Record.Add Key,"1" +Next +Record("{record_field}")="" +On Error Resume Next +QPRestoreDisplay Attr,Record,"R1" +Failure=Err.Number:Err.Clear +On Error GoTo 0 +WScript.Echo CStr(Failure<>0) +WScript.Echo Writes +''', LOG_STUB + fake + ''' +Sub QPVerifyDisplay(ByRef Attr,ByRef Record,ByVal Refdes) +End Sub +''') + assert output.splitlines() == ['True', 'True', '0'] + + +@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_runtime.py b/tests/v1_1/test_batch_runtime.py index 20d1361..ca1fac0 100644 --- a/tests/v1_1/test_batch_runtime.py +++ b/tests/v1_1/test_batch_runtime.py @@ -165,6 +165,46 @@ def test_two_projects_reuse_cad_and_produce_distinct_close_checkpoints(tmp_path, assert Cad.created[0].closed and Desktop.created[0].released +def test_completed_project_does_not_hold_resolution_wait_for_next_project_failure(tmp_path, monkeypatch): + import subprocess + command, _ = command_for(tmp_path) + next_command, _ = command_for(tmp_path, 'next') + runtime = worker_runtime(tmp_path, monkeypatch, lambda command, *a, **k: + subprocess.CompletedProcess(command, 0, '', '')) + with runtime: + runtime.run_worker(command, 5) + assert runtime._active_attempts == [] + def fail_prepare(*args, **kwargs): + raise OSError('next project backup failed') + monkeypatch.setattr(runtime, '_prepare_attempts', fail_prepare) + with pytest.raises(OSError, match='next project backup failed'): + runtime.run_worker(next_command, 5) + assert not getattr(runtime.stop_event, 'waiting_for_resolution', False) + assert not (Path(command[3]) / 'quick-run' / 'uncertain.flag').exists() + + +def test_current_task_and_attempt_are_durable_before_worker_launch(tmp_path, monkeypatch): + import subprocess + one, _ = command_for(tmp_path) + two, _ = command_for(tmp_path, 'next') + next_root = Path(two[3]) + command_path = next_root / 'command.tsv' + command_path.write_text(command_path.read_text().replace('task\tjob\t', 'task2\tjob2\t'), encoding='utf-8') + calls = [] + def monitor(command, *args, **kwargs): + calls.append(command) + record = json.loads(runtime.path.read_text(encoding='utf-8')) + expected_task = 'task' if len(calls) == 1 else 'task2' + assert record.get('active_task') == expected_task + assert record.get('active_attempt') + return subprocess.CompletedProcess(command, 0, '', '') + runtime = worker_runtime(tmp_path, monkeypatch, monitor) + two = (*two[:-1], 'job2') + with runtime: + runtime.run_worker(one, 5) + runtime.run_worker(two, 5) + + @pytest.mark.parametrize('second_fails', [False, True]) def test_prewrite_failure_releases_old_desktop_and_retries_only_once(tmp_path, monkeypatch, second_fails): import subprocess @@ -193,6 +233,29 @@ def test_prewrite_failure_releases_old_desktop_and_retries_only_once(tmp_path, m and release['released_at'] > 0 for release in releases) +def test_rebuild_does_not_hide_cad_shutdown_failure(tmp_path): + from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime + from dx_productivity.trial_task_center.errors import TaskCenterError + + class FailingCad(Cad): + def __exit__(self, *_exc): + self.closed = True + self.desktop.close() + raise OSError('CAD quit confirmation failed') + + runtime = BatchCadRuntime(tmp_path / 'data', 'execution', + desktop_factory=Desktop, cad_factory=FailingCad) + runtime.__enter__() + runtime._ensure_desktop() + runtime._ensure_cad() + with pytest.raises(TaskCenterError, match='CAD quit confirmation failed'): + runtime.close(rebuilding=True) + with pytest.raises(TaskCenterError, match='CAD quit confirmation failed'): + runtime.__exit__(None, None, None) + record = json.loads(runtime.path.read_text()) + assert record['state'] == 'blocked' + + def test_possible_mutation_prevents_automatic_replay(tmp_path, monkeypatch): from dx_productivity.trial_task_center.worker_monitor import WorkerMonitorError command, _ = command_for(tmp_path) 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..bcb43e5 100644 --- a/tests/v1_1/test_native_readback_efficiency.py +++ b/tests/v1_1/test_native_readback_efficiency.py @@ -1,4 +1,5 @@ from collections import Counter +from pathlib import Path from types import SimpleNamespace import pytest @@ -43,6 +44,55 @@ def test_native_readback_checks_each_unselected_attribute_once(tmp_path, target_ assert other.reads == {'Part Number': 1, 'Value': 1, 'Part Name': 1, 'Part Label': 1} +@pytest.mark.parametrize('others,changed', [(0, False), (2, False), (2, True)]) +def test_legacy_readback_checks_every_unselected_component(tmp_path, others, changed): + target = Component('u1', 'R1', **{'Part Number': 'NEW', 'Part Name': 'TARGET'}) + components, baseline = [target], {} + for index in range(others): + uid, refdes = f'u{index + 2}', f'R{index + 2}' + baseline[uid] = (refdes, 'OLD', 'V0', 'NAME', 'LABEL') + components.append(Component(uid, refdes, **{'Part Number': 'OLD', + 'Value': 'CHANGED' if changed and index == 0 else 'V0', + 'Part Name': 'NAME', 'Part Label': 'LABEL'})) + def verify(): + return native.verify_replacement_readback(tmp_path / 'snapshot.tsv', components, + ['M', 'project', 'OLD', 'NEW', 'sheet', 'preserve'], [['C', 'u1', 'R1']], + {c.UID for c in components}, baseline, False, None, ['u1']) + if changed: + with pytest.raises(ValueError, match='unselected component'): + verify() + else: + assert verify() == ['u1'] + + +@pytest.mark.parametrize('count', [1, 2]) +def test_missing_uid_matches_flow_into_pin_promotion(monkeypatch, count): + components = [Component('', f'R{i+1}', **{'Part Number': 'NEW', 'Part Name': 'TARGET'}) + for i in range(count)] + records = [['C', f'@REFDES|R{i+1}', f'R{i+1}'] for i in range(count)] + matches = native.replacement_matches(components, records, 'NEW', False) + assert matches == [row[1] for row in records] + pins = [SimpleNamespace(Component=c, Number='1') for c in components] + for pin in pins: + pin._oleobj_ = pin + app = SimpleNamespace(ActiveView=SimpleNamespace( + Block=SimpleNamespace(PromoteSymbolNumbers=lambda *args: None), + Query=lambda *args: pins)) + monkeypatch.setattr(native, 'components_on_sheet', lambda *args: components) + monkeypatch.setattr(native, 'selection', lambda *args: None) + promoted = native.promote_pin_numbers(app, SimpleNamespace(IVdCmpPin=lambda p: p), matches) + assert [pin['uid'] for pin in promoted] == matches + + +def test_missing_uid_fallback_survives_complete_readback(): + target = Component('', 'R1', **{'Part Number': 'NEW', 'Part Name': 'TARGET', 'Part Label': 'LABEL'}) + snapshot = Path('fallback-readback.tsv') + metadata = ['M', 'project', 'OLD', 'NEW', 'sheet', 'part_label', '', '1'] + assert native.verify_replacement_readback(snapshot, [target], metadata, + [['C', '@REFDES|R1', 'R1']], {'@REFDES|R1'}, {}, True, + ['lib', 'NEW', 'symbol', 'gate', 'TARGET', 'LABEL'], ['@REFDES|R1']) == ['@REFDES|R1'] + + def test_preselection_context_does_not_query_an_unused_selection(monkeypatch): target = Component('u1', 'R1', **{'Part Number': 'OLD', 'Value': 'V0'}) target._oleobj_ = target @@ -66,3 +116,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_resource_status.py b/tests/v1_1/test_resource_status.py new file mode 100644 index 0000000..db46f8c --- /dev/null +++ b/tests/v1_1/test_resource_status.py @@ -0,0 +1,103 @@ +"""Resource snapshots stay conservative during slow, failed and repeated reads.""" +import os +os.environ.setdefault('QT_QPA_PLATFORM', 'offscreen') + +from threading import Event + +from dx_productivity.trial_task_center.ui.resource_status import ResourceStatus + + +def test_resource_read_failure_blocks_until_a_successful_recheck(qtbot): + outcome = [()] + def read(): + if isinstance(outcome[0], Exception): + raise outcome[0] + return outcome[0] + status = ResourceStatus(read) + try: + assert status.blocked and not status.known + status.refresh() + qtbot.waitUntil(lambda: not status.busy) + assert status.known and not status.blocked + outcome[0] = OSError('resource directory unavailable') + status.refresh() + qtbot.waitUntil(lambda: not status.busy) + assert status.blocked and isinstance(status.error, OSError) + outcome[0] = () + status.refresh() + qtbot.waitUntil(lambda: not status.busy) + assert not status.blocked and status.error is None + finally: + status.close() + + +def test_operation_completion_coalesces_recheck_and_ignores_the_stale_clear_snapshot(qtbot): + started, release = Event(), Event() + calls, states = [], [] + def read(): + calls.append(1) + if len(calls) == 1: + started.set() + assert release.wait(5) + return () + return ({'state': 'blocked'},) + status = ResourceStatus(read) + status.changed.connect(lambda: states.append(status.blocked)) + try: + status.refresh() + qtbot.waitUntil(started.is_set) + for _ in range(10): + status.refresh() + release.set() + qtbot.waitUntil(lambda: not status.busy) + assert len(calls) == 2 + assert status.records == ({'state': 'blocked'},) + assert states == [True], 'a pre-operation snapshot must not briefly unlock CAD actions' + finally: + release.set() + status.close() + + +def test_polling_does_not_restart_a_slow_resource_read(qtbot): + started, release = Event(), Event() + calls = [] + def read(): + calls.append(1) + started.set() + assert release.wait(5) + return () + status = ResourceStatus(read) + try: + status.refresh() + qtbot.waitUntil(started.is_set) + for _ in range(10): + status._poll() + release.set() + qtbot.waitUntil(lambda: not status.busy) + assert status.known and not status.blocked + assert len(calls) == 1 + finally: + release.set() + status.close() + + +def test_close_cancels_polling_and_drops_late_resource_results(qtbot): + started, release = Event(), Event() + changes = [] + def read(): + started.set() + assert release.wait(5) + return () + status = ResourceStatus(read) + status.changed.connect(lambda: changes.append(True)) + try: + status.refresh() + qtbot.waitUntil(started.is_set) + status.close() + assert not status._timer.isActive() + release.set() + qtbot.waitUntil(lambda: not status.busy) + assert status.blocked and changes == [] + finally: + release.set() + status.close() diff --git a/tests/v1_1/test_safe_user_stop.py b/tests/v1_1/test_safe_user_stop.py index 50f0f3f..0d97dfa 100644 --- a/tests/v1_1/test_safe_user_stop.py +++ b/tests/v1_1/test_safe_user_stop.py @@ -9,6 +9,21 @@ import pytest from dx_productivity.trial_task_center.worker_monitor import ProgressWatch, run_monitored_process +@pytest.fixture +def started_worker_watch(monkeypatch): + """Test phase timeouts after real child startup, with a bounded startup grace.""" + stages = set() + action = ProgressWatch.action + def after_start(watch, now): + if not watch.sequence and now - watch.started < 5: + return None + if watch.sequence: + stages.add(watch.stage) + return action(watch, now) + monkeypatch.setattr(ProgressWatch, 'action', after_start) + return stages + + def test_unsafe_save_timeout_waits_for_completion_and_recovers_on_progress(): watch = ProgressWatch('token', 'job', 10, 0) assert watch.accept(b'DX_PROGRESS\ttoken\tjob\t1\tsave_and_close\t0\t1\t0\n', 0) @@ -20,7 +35,7 @@ def test_unsafe_save_timeout_waits_for_completion_and_recovers_on_progress(): assert watch.snapshot(now=201)['waiting_for_safe_completion'] is False -def test_real_slow_save_is_not_killed_by_inactivity(tmp_path): +def test_real_slow_save_is_not_killed_by_inactivity(tmp_path, started_worker_watch): script = tmp_path / 'save.py' script.write_text('''import sys,time,pathlib root=pathlib.Path(sys.argv[1]);job=sys.argv[4];token=sys.argv[5] @@ -32,10 +47,11 @@ time.sleep(1.5) command = (sys.executable, '-u', str(script), str(tmp_path), sys.executable, '', 'job') result = run_monitored_process(command, .3) assert result.returncode == 0 + assert 'save_and_close' in started_worker_watch assert (tmp_path / 'saved.txt').read_text() == 'saved and independently verified' -def test_readonly_native_wait_keeps_bounded_cancellation_without_force_ui(tmp_path): +def test_readonly_native_wait_keeps_bounded_cancellation_without_force_ui(tmp_path, started_worker_watch): script = tmp_path / 'read.py' script.write_text('''import sys,time,pathlib root=pathlib.Path(sys.argv[1]);job=sys.argv[4];token=sys.argv[5] @@ -46,9 +62,10 @@ time.sleep(1.5) command = (sys.executable, '-u', str(script), str(tmp_path), sys.executable, '', 'job') with pytest.raises(subprocess.TimeoutExpired): run_monitored_process(command, .3, cooperative_abort=True) + assert 'native_snapshot_read' in started_worker_watch -def test_explicit_force_stops_unsafe_worker_with_protection(tmp_path): +def test_explicit_force_stops_unsafe_worker_with_protection(tmp_path, monkeypatch): script = tmp_path / 'save.py' script.write_text('''import sys,time,pathlib root=pathlib.Path(sys.argv[1]);job=sys.argv[4];token=sys.argv[5] @@ -59,22 +76,20 @@ time.sleep(30) (root/'unexpected_completed').touch() ''', encoding='utf-8') force = Event() - def request(): - for _ in range(200): - if (tmp_path / 'started').exists(): - force.set() - return - Event().wait(.01) - requester = Thread(target=request) - requester.start() + accept = ProgressWatch.accept + def force_after_unsafe_progress(watch, line, now): + accepted = accept(watch, line, now) + if accepted and watch.stage == 'save_and_close' and not watch.safe_stop: + force.set() + return accepted + monkeypatch.setattr(ProgressWatch, 'accept', force_after_unsafe_progress) command = (sys.executable, '-u', str(script), str(tmp_path), sys.executable, '', 'job') - try: - with pytest.raises(Exception, match='forced'): - run_monitored_process(command, .3, force_event=force) - assert (tmp_path / 'uncertain.flag').exists() - assert not (tmp_path / 'unexpected_completed').exists() - finally: - requester.join(3) + # This checks force at an accepted unsafe boundary, not startup speed. + from dx_productivity.trial_task_center.worker_monitor import WorkerMonitorError + with pytest.raises(WorkerMonitorError, match='forced'): + run_monitored_process(command, 5, force_event=force) + assert (tmp_path / 'uncertain.flag').exists() + assert not (tmp_path / 'unexpected_completed').exists() def test_abnormal_cad_shutdown_is_not_hidden_by_physical_release(tmp_path): @@ -110,6 +125,43 @@ 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'), + poll=lambda: 0 if calls else None) + 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_retry_terminates_again_if_process_is_still_alive(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'), poll=lambda: None) + journal = {'operation': 'execute_task_queue', 'batch_id': 'a' * 32, 'attempt_id': 'b' * 32, + 'task_ids': ['task'], 'state': 'running', 'force_state': 'termination_requested'} + _force_owned_supervisor(tmp_path, journal, process) + assert calls == ['terminate'] + + +def test_empty_interruption_marker_is_repaired_before_force(tmp_path): + from dx_productivity.trial_task_center.stop_control import protect_interruption + marker = tmp_path / 'uncertain.flag' + marker.touch() + protect_interruption(tmp_path, 'reason=interrupted') + assert marker.read_text(encoding='utf-8') == 'reason=interrupted' def test_force_already_performed_remains_forced_when_exit_diagnostics_fail(tmp_path): @@ -228,7 +280,14 @@ def test_failed_protection_fsync_is_retried_before_force_is_allowed(tmp_path, mo Event().wait(.01) assert control.control_error control.request_force() - thread.join(.25) + # Initial progress publication can still be doing filesystem I/O. + # Observe the rejected force request instead of assuming it reaches + # the retry within a fixed quarter-second scheduling window. + for _ in range(300): + if control.control_error: + break + Event().wait(.01) + assert control.control_error assert thread.is_alive(), 'a file left by failed fsync must not authorize force' assert len(attempts) >= 2 monkeypatch.setattr(module.os, 'fsync', original) @@ -241,8 +300,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 @@ -288,6 +350,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(): @@ -326,13 +409,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_supervisor_result.py b/tests/v1_1/test_supervisor_result.py index f7a584f..3536129 100644 --- a/tests/v1_1/test_supervisor_result.py +++ b/tests/v1_1/test_supervisor_result.py @@ -1,6 +1,36 @@ import pytest +@pytest.mark.parametrize('content', ['{bad', '[]', '{"cad_shutdown": 5}', + '{"cad_shutdown": {"cleanup_errors": [1]}}']) +def test_corrupt_shutdown_evidence_keeps_result_and_blocks(tmp_path, monkeypatch, content): + from dx_productivity.trial_task_center import batch_resources + from dx_productivity.trial_task_center.batch_supervisor import _finish_response + from dx_productivity.trial_task_center.errors import TaskCenterError + from dx_productivity.trial_task_center.logging_setup import configure_logging + from dx_productivity.trial_task_center.supervisor_protocol import encode + journal = {'operation': 'probe', 'batch_id': 'a' * 32} + root = tmp_path / 'batch-resources' / journal['batch_id'] + root.mkdir(parents=True) + (root / 'session.json').write_text(content, encoding='utf-8') + monkeypatch.setattr(batch_resources, 'cleanup_pending_resources', lambda *args, **kwargs: ()) + response = {'business_succeeded': True, 'status': 'ok', 'result': encode({'verified': True})} + with pytest.raises(TaskCenterError) as caught: + _finish_response(tmp_path, journal, response, configure_logging(tmp_path)) + assert caught.value.code == 'application_shutdown_failed' + assert caught.value.completed_result == {'verified': True} + assert journal['state'] == 'blocked' + + +def test_failed_progress_publish_emits_warning(tmp_path, monkeypatch, caplog): + from dx_productivity.trial_task_center import worker_monitor + def fail(*args): + raise OSError('disk full') + monkeypatch.setattr(worker_monitor, '_atomic_text', fail) + worker_monitor._publish_progress(tmp_path / 'progress.json', {'stage': 'awaiting_reconciliation'}) + assert 'worker_progress_publish_failed' in caplog.text + + @pytest.mark.parametrize('body', [None, {}, {'status':'ok','business_succeeded':'yes','result':'bad'}, {'status':'ok','business_succeeded':True,'result':'wrong type'}]) def test_invalid_result_cannot_finish_resource_gate(tmp_path, body): @@ -21,3 +51,47 @@ def test_pending_cleanup_cannot_expose_bom_preview(): error=resource_pending_error('inspect_bom_batch',report,OSError('held desktop')) assert error.completed_result.preview is None assert error.bom_report.preview is None + + +@pytest.mark.parametrize('business_succeeded', [True, False]) +def test_abnormal_shutdown_retains_bom_rows_without_an_executable_preview(tmp_path, business_succeeded): + from dx_productivity.trial_task_center.batch_resources import write_record + from dx_productivity.trial_task_center.batch_supervisor import _finish_response + from dx_productivity.trial_task_center.bom_batch import BomBatchPreview + from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult + from dx_productivity.trial_task_center.bom_import_state import BomImportMachine + from dx_productivity.trial_task_center.errors import TaskCenterError + from dx_productivity.trial_task_center.logging_setup import configure_logging + from dx_productivity.trial_task_center.supervisor_protocol import encode, encode_error + + report = BomImportReport((BomRowResult(4, 'board.prj', 'Board', 'failed', 'R99 missing'),), (), + BomBatchPreview('task', (), ()) if business_succeeded else None) + primary = TaskCenterError('inventory_cleanup', 'inventory reader failed') + primary.bom_report = report + journal = {'operation': 'inspect_bom_batch', 'batch_id': 'a' * 32, 'state': 'blocked'} + write_record(tmp_path / 'batch-resources' / journal['batch_id'] / 'session.json', { + 'version': 1, 'session_id': journal['batch_id'], 'kind': 'bom_import', 'state': 'released', + 'generation': 1, 'desktop': 'dx-' + 'b' * 32, 'processes': [], 'cad_pid': None, + 'cad_guid': None, 'checkpoints': {}, 'retries': {}, 'timings': [], + 'cad_shutdown': {'clean': False, 'orderly_shutdown': False}}) + response = {'business_succeeded': business_succeeded, 'status': 'error', + 'error': encode_error(TaskCenterError('batch_resources_pending', 'Quit failed'))} + if business_succeeded: + response['result'] = encode(report) + else: + response['operation_error'] = encode_error(primary) + machine = BomImportMachine() + attempt = machine.start() + with pytest.raises(TaskCenterError) as caught: + _finish_response(tmp_path, journal, response, configure_logging(tmp_path)) + failure = caught.value + assert failure.code == 'application_shutdown_failed' + machine.finish(attempt, error=failure) + assert machine.state.report is not None, 'shutdown errors must preserve Excel row diagnostics' + assert machine.state.report.rows[0].excel_row == 4 + assert machine.state.report.rows[0].reason == 'R99 missing' + assert machine.state.report.preview is None + if business_succeeded: + assert failure.completed_result.preview is None + else: + assert failure.primary_error.code == 'inventory_cleanup' 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_cell_rules.py b/tests/v1_1/test_target_cell_rules.py index 4b9bec3..5c03920 100644 --- a/tests/v1_1/test_target_cell_rules.py +++ b/tests/v1_1/test_target_cell_rules.py @@ -265,6 +265,33 @@ def test_dialog_discards_choices_when_project_binding_changes_before_delivery(qt assert '工程' in dialog.cell_status.text() +@pytest.mark.parametrize('reader_fails', [False, True]) +def test_destroyed_mapping_dialog_ignores_pending_cell_reader(qtbot, reader_fails): + from PySide6.QtCore import QCoreApplication, QEvent, QThreadPool + from shiboken6 import isValid + + release, started = Event(), Event() + def load(_part): + started.set() + assert release.wait(5) + if reader_fails: + raise OSError('late library failure') + return ('CellA',) + dialog = _MappingDialog(None, 'OLD', 'NEW', cell_loader=load) + dialog.cell_load_button.click() + try: + qtbot.waitUntil(started.is_set) + dialog.deleteLater() + QCoreApplication.sendPostedEvents(dialog, QEvent.Type.DeferredDelete) + assert not isValid(dialog) + finally: + release.set() + with qtbot.captureExceptions() as exceptions: + assert QThreadPool.globalInstance().waitForDone(5000) + QCoreApplication.processEvents() + assert not exceptions, 'completed readers must not access destroyed dialog widgets' + + def test_update_rejects_non_text_cell_instead_of_clearing(batch): app, store, tasks = batch rule = app.add_rule(tasks[0].task_id, 'OLD', 'NEW', target_cell='Existing') 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' diff --git a/tests/v1_1/test_ui_operation_lifecycle.py b/tests/v1_1/test_ui_operation_lifecycle.py index 04ea9a0..d250b24 100644 --- a/tests/v1_1/test_ui_operation_lifecycle.py +++ b/tests/v1_1/test_ui_operation_lifecycle.py @@ -20,13 +20,14 @@ def test_pending_batch_resources_block_new_work_and_cleanup_does_not_replace(win pending.clear() return () monkeypatch.setattr(window.service, 'cleanup_batch_resources', cleanup) - window._update_gates() + window._resource_status.refresh() + qtbot.waitUntil(lambda: not window._resource_status.busy) assert not window.run_panel.quick_run_button.isEnabled() assert not window.plan_panel.collect_button.isEnabled() assert not window.plan_panel.import_bom_button.isEnabled() assert window.run_panel.cleanup_resources_button.isEnabled() window.run_panel.cleanup_resources_button.click() - qtbot.waitUntil(lambda: not window._workers) + qtbot.waitUntil(lambda: not window._workers and not window._resource_status.busy) assert calls == ['cleanup'] assert not window.run_panel.cleanup_resources_button.isEnabled() assert '已释放' in window.run_panel.operator_band.text() diff --git a/tests/v1_1/test_ui_responsiveness.py b/tests/v1_1/test_ui_responsiveness.py index d73d3ea..1346df7 100644 --- a/tests/v1_1/test_ui_responsiveness.py +++ b/tests/v1_1/test_ui_responsiveness.py @@ -40,6 +40,35 @@ def loaded(qtbot, window, task): and window._context.task.task_id == task.task_id, timeout=3000) +def test_resource_scan_never_runs_in_gate_refresh_and_keeps_event_loop_responsive(browser, qtbot, monkeypatch): + window, service, (task, _) = browser + window.open_task(task.task_id) + loaded(qtbot, window, task) + entered, release = Event(), Event() + gui_thread = get_ident() + def slow_resources(): + assert get_ident() != gui_thread, 'resource files must not be read on the GUI thread' + entered.set() + assert release.wait(5) + return ({'state': 'blocked'},) + monkeypatch.setattr(service, 'pending_batch_resources', slow_resources) + try: + window._update_gates() + window._resource_status.refresh() + qtbot.waitUntil(entered.is_set) + tick = [] + QTimer.singleShot(0, lambda: tick.append(True)) + window.task_list.search.setText('board') + window._update_gates() + qtbot.waitUntil(lambda: bool(tick)) + assert not release.is_set() + finally: + release.set() + qtbot.waitUntil(lambda: not window._resource_status.busy) + assert window.run_panel.cleanup_resources_button.isEnabled() + assert not window.run_panel.quick_run_button.isEnabled() + + def test_slow_task_read_is_background_and_stale_result_cannot_change_selection(browser, qtbot, monkeypatch): window, service, (a, b) = browser real = service.task_context