fix: harden supervised replacement and missing UID handling #9

Merged
abaishu0730 merged 10 commits from codex/project-optimization into main 2026-10-31 11:25:10 +08:00
78 changed files with 4612 additions and 224 deletions

No files matched your search

+7 -5
View File
@@ -4,11 +4,11 @@
面向 **Windows + DxDesigner / Expedition Enterprise 7.9.5** 的器件批量替换工具。一个工程对应一个任务,可以只替换同料号下的部分位号;支持直接编辑和 Excel BOM 导入,同批工程按顺序执行。
**最新版本:V1.1.4 Beta 3(预发布)** · [下载便携包](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/download/v1.1.4-beta.3/DxPartReplace-1.1.4-beta.3.zip) · [版本说明与附件](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/tag/v1.1.4-beta.3) · [完整使用说明](docs/user/v1.1.0-project-tasks.md)
**最新版本:V1.1.4 Beta 4(预发布)** · [下载便携包](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/download/v1.1.4-beta.4/DxPartReplace-1.1.4-beta.4.zip) · [版本说明与附件](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/tag/v1.1.4-beta.4) · [完整使用说明](docs/user/v1.1.0-project-tasks.md)
所有发布均标记为 **Beta 预发布**。V1.1.4 Beta 1 因 BOM 导入缺少 `win32timezone` 已撤回,请使用 Beta 3;旧 Git 标签保留供回溯。升级前备份 `data`,完整更新 EXE 与 `_internal`,不要只替换 EXE。
所有发布均标记为 **Beta 预发布**。V1.1.4 Beta 1 因 BOM 导入缺少 `win32timezone` 已撤回,请使用 Beta 4;旧 Git 标签保留供回溯。升级前备份 `data`,完整更新 EXE 与 `_internal`,不要只替换 EXE。
Beta 3 面向工单 #1/#3 复测:修复内部长路径访问,将后台资源移交独立批次监督进程,补齐安全重建、释放诊断与可选关闭归档。[本版变更和复测说明](docs/releases/v1.1.4-beta.3.md)。
Beta 4 面向停止与资源收尾复测:普通暂停等待当前项保存核验,写入中无进度不再自动杀进程;新增明确确认的强制结束,保留中断证据与恢复入口。[本版变更和复测说明](docs/releases/v1.1.4-beta.4.md)。
本版提供[目标封装选择](docs/user/target-cell-selection.md):目标封装可留空,此时工具不主动覆写封装,可后续与中心库同步;需要指定时,在料号规则中读取并选择合法封装。BOM 保持原 11 列。
@@ -20,7 +20,7 @@ Beta 3 面向工单 #1/#3 复测:修复内部长路径访问,将后台资
### 第 1 步:解压并打开应用
[下载 V1.1.4 Beta 3 便携包](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/download/v1.1.4-beta.3/DxPartReplace-1.1.4-beta.3.zip),**完整解压**,双击 `DxPartReplace.exe`。保留它与 `_internal` 的相对位置,无需安装 Python;本机需要可用的 DxDesigner、许可证和中央库。
[下载 V1.1.4 Beta 4 便携包](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/download/v1.1.4-beta.4/DxPartReplace-1.1.4-beta.4.zip),**完整解压**,双击 `DxPartReplace.exe`。保留它与 `_internal` 的相对位置,无需安装 Python;本机需要可用的 DxDesigner、许可证和中央库。
打开后看到下图主窗口。首次使用请准备完整的工程副本,执行前保存并关闭待处理工程。
@@ -133,6 +133,8 @@ Beta 3 面向工单 #1/#3 复测:修复内部长路径访问,将后台资
运行期间可以在主桌面查看其他文件,请勿打开或修改本次正在处理的同一工程。需要暂停时,点击 **完成当前项后暂停**,等待当前步骤安全收尾。
Beta 4 在保存或核验尚未确认时保留应用等待,可先 **导出诊断包**。只有明确接受中断后需要核验时,才选择 **强制结束…**;之后先核验恢复,不能直接重复替换。详见[停止、强制结束与恢复](docs/user/safe-stop-and-recovery.md)。
**完成标志:** 任务显示已完成,底部提示本批执行结束。还要打开结果核对,不能只看进度条。
### 第 12 步:查看实际修改结果与日志
@@ -196,7 +198,7 @@ DxPartReplace/
## 验证范围与已知限制
V1.1.4 Beta 3 新增批次后台资源管理:一次 BOM 导入或顺序执行共用一个私有桌面,CAD 按需启动并在同批工程间复用,批次结束统一退出。若提示 **资源释放待处理**,已保存核验的结果保留,请点击 **重新清理资源**;该操作只清理后台资源,不会再次替换。旧的工程保护锁仍按“核验并恢复”处理。设计、异常边界与实机结果见[批次运行时验收记录](docs/testing/2026-10-21-batch-private-runtime.md)。
V1.1.4 Beta 4 沿用批次后台资源管理:一次 BOM 导入或顺序执行共用一个私有桌面,CAD 按需启动并在同批工程间复用,批次结束统一退出。若提示 **资源释放待处理**,已保存核验的结果保留,请点击 **重新清理资源**;该操作只清理后台资源,不会再次替换。旧的工程保护锁仍按“核验并恢复”处理。设计、异常边界与实机结果见[批次运行时验收记录](docs/testing/2026-10-21-batch-private-runtime.md)。
资源释放会追加日志:在 **替换结果 / 日志 → 完整日志** 查看本批开始释放、释放成功/失败及重新清理的结果。独立文件位于 `data/batch-resources/<批次ID>/resource-release.log`,记录批次、桌面、CAD PID、退出码、释放耗时、句柄关闭和桌面消失的分别确认,以及失败原因;同时保留 `session.json` 的原始释放证明。重启后重新检查也会追加记录,不覆盖原失败日志。
+37
View File
@@ -0,0 +1,37 @@
# V1.1.4 Beta 4 构建与发布验证
2026-10-26,用户授权发布下一版 Beta。版本 `1.1.4-beta.4`,Python 元数据 `1.1.4b4`,Windows 文件版本 `1.1.4.4`,预发布标志开启。业务修改基于 `9c03cd5`;本轮产品代码只调整版本标识。
## 构建门禁
- `packaging/Build-DxPartReplace.ps1`:退出码 0,源码门禁 **923 passed,232.69 秒**,一条既有 pywinauto STA 警告。
- 打包配置及外部验收脚本检查:**17 passed,11.08 秒**。
- 构建产物在完整冻结 EXE 主进程中启动独立监督进程、读取真实进程创建时间并确认自有进程/Job/桌面释放,再完成 GUI 启动与正常关闭检查。
- 冻结的 92 个 `dx_productivity` 模块及应用入口逐项对照源码,递归标准化 code filename 后 code object 一致。包含新的 `stop_control` 模块;两份 VBS 在包装阶段另做哈希比对。
- EXE SHA256:`ee64d983f6f0457bac2bb16225ee8a8044753b5bf7779e498549c61d8ed4d13b`。
构建日志:`build/release-1.1.4-beta.4-build.log`。源码比对:`build/release-1.1.4-beta.4/frozen-source-check.json`。实现阶段扩大测试 1147 项通过的范围见[安全停止验收](../testing/2026-10-26-safe-stop-acceptance.md),不与本轮构建门禁数量混算。
## 冻结监督进程实机验证
本轮用源码控制器驱动上述正式 EXE 的 `--batch-worker`,执行 BOM 导入与真实 CAD 队列;批内 helper 和 VBS 使用冻结产物。复制两个全新工程,不重放之前完成的替换。
在第一个工程的生产 `save_requested` 日志出现时只请求普通停止,结果为第一任务 completed、第二任务 pending、队列 paused。新 CAD 实例独立读回确认:第一工程 57 器件、103 引脚连接正确,56 个非目标器件保持;第二工程 57 器件完整属性/连接图与执行前一致。
执行批次 `orderly_shutdown=true`、`clean=true`、`forced_termination=false`,CAD 与 Quit helper 退出码均为 0,资源状态 released。源工程目录哈希和中央库 `.lmc` 哈希不变。
证据:`build/release-1.1.4-beta.4/frozen-save-stop.json` 与 `build/safe-stop-live-9894f95db1/`。后者由通用 source 探针生成,保留了其原始 scope 文本;本轮实际冻结进程路由、调用请求和 EXE 哈希以 companion `frozen-save-stop.json` 为准。前后独立读取使用源码读取器,不将其说成全部入口均冻结,也不将该测试说成完整 GUI 交互停止。
## 包装与公开附件
便携包只从 EXE、完整 `_internal`、仓库教程及文档生成,附空白 11 列 BOM 模板和历史窗口 GIF;不带 `data`、任务数据库、用户工程、工单附件或现场日志。
包装脚本要求本轮冻结停止验证的 EXE 哈希与被打包文件一致,检查 ZIP CRC、解压路径、所有程序文件 SHA256,并在清除源码 Python 环境变量后执行解压出来的完整 EXE smoke。ZIP 大小、SHA256、程序文件清单与解压验证结果由公开 `release-manifest-1.1.4-beta.4.json` 记录。
附件:`DxPartReplace-1.1.4-beta.4.zip`、`SHA256SUMS-1.1.4-beta.4.txt`、`release-manifest-1.1.4-beta.4.json`、`BOM-template.xlsx`、`window-tutorial-1.1.4-beta.4.gif`。GIF 沿用 1.1.2 基础教程,新停止功能以包内说明和界面为准。
发布流程为先创建草稿,上传后逐项回下载核对大小和 SHA256,全部一致才公开为 Beta 预发布。远端响应与附件校验证据保存在 `build/release-1.1.4-beta.4/`;认证信息不写入文件或日志。本轮没有授权给工单发消息,因此不发布工单评论、不关闭工单,也不修改旧版本附件。
## 现场边界
仍未完成原工单 102 个工程的全量现场复测,也没有完整冻结 GUI 的真实保存中强杀或掉电无损验收。工单现场桌面残留持有者尚未查明。本版本提供已验证的停止与收尾改进,不能据此宣布现场事故根因已经根治。
+41
View File
@@ -0,0 +1,41 @@
# DxPartReplace V1.1.4 Beta 4
2026-10-26,**Beta 预发布/安全停止与应用收尾复测版**。关联工单 [#1](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/issues/1)、[#3](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/issues/3)。
## 下载与升级
完整解压 `DxPartReplace-1.1.4-beta.4.zip`,运行 `DxPartReplace.exe`。无需安装 Python;保留 EXE 和 `_internal` 的相对位置,本机需要可用的 DxDesigner、许可证及中央库。
请在新目录解压,使用工程副本复测。沿用旧任务前,先安全退出旧程序并备份完整 `data`;不要同时用两个版本打开同一数据目录。不要删除原有保护记录来绕过核验。
便携包包含中文教程、空白 11 列 BOM 模板、历史窗口操作 GIF、停止与恢复说明及文件校验清单。GIF 沿用 1.1.2 基础操作,新版停止与强制结束功能以实际界面及[停止说明](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/src/tag/v1.1.4-beta.4/docs/user/safe-stop-and-recovery.md)为准。
## 本版修改
- 普通“完成当前项后暂停”只停止后续调度,当前替换项完成必要校验、保存、回读和关闭后再停止;关闭窗口也等待收尾。
- 有写入可能的阶段长时间没有进度时保留应用等待,不再仅凭超时自动杀进程;纯只读采集仍采用有界取消。
- 新增独立的“强制结束…”确认。先可靠保存中断保护和批次证据,再终止本程序拥有的进程;保护写入失败时拒绝本次强制结束并显示原因。
- 当前修改或保存结果不确定、应用拒绝退出时保留现场,禁止盲目保存、丢弃和重放。等待期间可导出诊断包。
- 保存请求、保存调用返回、保存回读和工程关闭分别记录,避免保存后关闭失败被误记为未保存。
- 修复收尾记录写入失败跳过正常 Quit 的路径;CAD 主进程正常退出后,给自有辅助进程短暂自然退出机会,再决定是否需要强制清理。
- 正常退出、强制终止和物理资源释放分别报告。资源消失不再掩盖应用退出异常,已经核验的业务结果继续保留。
- 控制通信异常不会直接授权终止正在运行的交互批次;强制结束及进程退出诊断记录保持一致。
## 复测建议
1. 新建两个工程副本的少量位号任务,导入 BOM 并保存计划。
2. 在第一项执行或保存时点击普通暂停,核对当前项完成保存回读,下一项保持未执行;重新打开工程核对料号和连接。
3. 对长时间等待先导出诊断。仅在接受当前结果需要核验时确认“强制结束…”;重开后先“核验并恢复”,不要直接重复替换。
4. 如仍有异常,保留版本、触发步骤、诊断 ZIP 和失败工程名称。资源待处理时的清理不会重新执行替换。
为避免强制请求与任务切换同时发生而漏记目标,本版强制结束会保守保护本批可能涉及的任务。先前已完成任务也可能需要复核,但其保存结果和执行证据不被覆盖或重放。
## 验证范围与已知边界
实现阶段扩展回归 **1147 项通过**。源码监督入口在两份新工程副本上验证了保存期间请求普通停止:第一工程 completed,第二工程 pending;新 CAD 独立读回确认第一工程 57 器件、103 引脚连接正确、56 个非目标器件不变,第二工程完整图未变化。源工程及中央库 `.lmc` 哈希不变,执行批次正常退出且没有强制清理。
本轮发布门禁 923 项通过,打包与验收脚本检查 17 项通过。正式 EXE 的冻结监督进程再次完成真实 CAD 保存期间普通停止及新实例独立读回;当前项完成、下一项未执行,正常退出且无强制终止。该实机入口由源码控制器驱动,不等同于完整 GUI 交互验收。
发布构建、解压后完整 EXE 启动、冻结监督进程及资源释放检查另见[Beta 4 构建记录](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/src/tag/v1.1.4-beta.4/docs/releases/v1.1.4-beta.4-build.md)和附件 manifest。源码/受控辅助进程故障测试不等同于完整冻结 GUI 下的真实 CAD 强制中断或掉电无损验证。
**现场资源残留根因仍未全部闭环。** 原工单全量 102 个工程及特定系统组合仍需现场复测,本版不宣称所有环境已根治,不自动关闭关联工单。完整范围见[实施验收](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/src/tag/v1.1.4-beta.4/docs/testing/2026-10-26-safe-stop-acceptance.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` 均通过。
@@ -0,0 +1,62 @@
# Safe User Stop Implementation Plan
> **For agentic workers:** use superpowers:subagent-driven-development or executing-plans; continue within the user-approved scope without another approval round.
**Goal:** 用户停止只在安全边界收尾;写入中的无进度不自动杀进程;明确强制中断有保护证据和恢复入口。
**Architecture:** 保留现有 Event/队列边界和独立监督进程,加入独立的强制停止意图、等待状态反馈;退出正常性与物理资源复核分开。使用既有任务保护与批次记录,不新增数据库版本。
**Tech Stack:** Python 3.12、Windows pywin32、PySide6、VBScript、pytest。
**Spec:** `docs/superpowers/specs/2026-10-26-safe-user-stop.md`
## Global Constraints
- 不修改原工程、中心库或未跟踪 feedback;真实测试只复制 fixture。
- 不放宽身份、写前保护、禁止不确定重放的门禁。
- 普通停止与强制结束独立,确认前不得产生强制意图。
- 不通过无限创建桌面或删除旧保护绕过异常。
- 不发布、不推送;本地分支 `codex/day0-safe-stop`。
## Task 1: 退出协议与后代进程
Files: `isolated_cad.py`, `isolated_desktop.py`, `tests/v1_1/test_isolated_cad.py`, `test_isolated_desktop.py`.
- [x] 先复现 closing 记录失败跳过 Quit、父进程自然退出后仍立即终止子进程的测试。
- [x] 诊断写入失败不跳过满足身份/空工程条件的 Quit;将记录失败独立保留并报告,禁止伪报 clean。
- [x] 根进程正常退出时,给 Job 后代 2 秒有界自然退出窗口;到期再强制并记录 `forced_termination`, `termination_reason`, `forced_descendant_pids`。根进程仍活着的异常清理仍有界。
- [x] 保留所有权和退出确认,测试自然退出与强制升级两条路径。
## Task 2: 停止、等待与强制中断
Files: new `stop_control.py`; `worker_monitor.py`, `project_session.py`, `batch_runtime.py`, `batch_supervisor.py`, `execution_feedback.py`; associated tests.
Interface:
```python
class StopControl(Event):
force_event: Event
def request_force(self):
self.set()
self.force_event.set()
```
- [x] 首先测试 unsafe 保存阶段超时仍保留 worker,安全阶段仍有 watchdog,显式 force 才结束并产生 uncertain。
- [x] `ProgressWatch` 提供 `waiting_for_safe_completion`;新进度可清除提示,不把心跳当作写入推进。
- [x] 监督父进程持久化停止意图;force 先写保护与中断记录,再只终止已拥有的监督进程 Job,不使用名字或不可信 PID 清理。
- [x] 业务完成但退出异常仍通过独立错误返回,附带已核验业务结果;纯桌面延迟且退出正常可继续使用现有复核恢复。
- [x] 普通停止在当前项收尾后暂停;会话 close 不得因普通等待超时反向强杀仍在写入的 worker。
## Task 3: 保存边界与界面
Files: `QuickPartReplace.vbs`, `ui/run_panel.py`, `ui/main_window.py`, `ui/execution_feedback.py`, protocol serializer if required; VBS/UI tests.
- [x] VBS 用真实函数的失败先行测试覆盖保存已发出但工程关闭失败;保存请求前设置“可能保存”状态,独立记录保存返回/回读/关闭,不再声称未保存。
- [x] 正常停止显示等待当前项收尾;unsafe 超时显示保存/核验尚未确认。
- [x] 提供与普通暂停分离的强制结束按钮,确认文案明确结果待核验;取消确认没有副作用。
- [x] 关闭窗口在运行/等待期间保持打开;任务失败后取消自动关闭,让用户看见恢复信息。
## Task 4: 集成验证与审查
- [x] 运行相关 VBS、monitor、supervisor、queue、UI、资源测试及完整发布门禁范围。
- [x] 实机空 CAD 自然退出、两工程副本正常保存及独立回读;停止/强制结束采用受控替身或独立进程,明确验证边界。
- [x] 独立审查关注:不确定写入重放、等待时 GUI 退出杀进程、force 身份校验与持久化失败、结果错误被吞、已完成项保留。
- [x] 更新操作文档和验收记录,交付本地可审查修改及测试证据。
@@ -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.**
@@ -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.
@@ -0,0 +1,9 @@
# 用户停止与安全收尾(已批准)
用户已在本会话批准:普通停止冻结后续调度;未修改可取消;当前已修改项完成校验、保存、回读后停止;保存或修改结果不明保持保护,不自动重放或盲目保存/丢弃;关闭窗口等待收尾;强制结束必须由用户明确选择,记录中断并在重启后核验。
业务结果、应用正常退出、强制清理和物理资源状态分别记录。资源消失不抹去应用异常。保存中无进度超时进入等待/提示,不自动终止。提供明确确认的强制结束出口;其范围仅为本程序当前监督进程及自有 Job。
普通停止以当前替换项及其必要收尾为边界,不启动同工程下一项。保护记录、批次身份和进程创建时间核验继续有效。先保存中断证据再执行工具内强制终止;持久化失败时拒绝工具内强制操作并反馈错误。系统外强杀/掉电依赖已有写前保护与重启核验,不承诺可在终止时写入日志。
保持源码版本,不发布包、不关闭远端事故工单。真实工程验证只用全新副本。
@@ -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.
@@ -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 |
## 验收进度
@@ -0,0 +1,134 @@
# Day0:保存、关闭、退出全链路排查
2026-10-26,北京时间。源码基线 `b9391015650bf5c43a77a017c5484e9a8f352266`。承接[资源事故分析](2026-10-26-day0-resource-release-analysis.md),本轮以核心应用完成条件为主线。
## 结论
发现并验证了四个具体的生命周期问题:
1. **CAD 主进程正常退出后,程序会立即终止 Job 中的剩余辅助进程,没有先给予其自然退出窗口。** 本机实测 `vnsd.exe` 再等约 0.813 秒即可自然退出;这个等待可以避免该次强制终止。
2. **退出阶段的记录写入失败会跳过正常 Quit。** 随后仍进入自有进程清理。用生产退出控制流和替身 CAD 复现。
3. **保存调用返回、工程关闭失败时,“已保存”标记仍可能没有设置。** 实际 VBS 函数配合替身 CAD 复现,说明保存与关闭的状态记录边界混在一起。
4. **物理资源复核通过后,上层可能返回已完成业务结果,同时不再向调用方抛出正常退出失败。** 生产运行时记录可以出现 `cad_shutdown.clean=false`、资源状态 `released`、监督记录 outcome 为 `error`,但函数返回 completed 任务结果。日志证据保留,调用方的返回语义却丢失了退出异常。
这些问题与“只盯桌面释放”的分析不同:资源消失不能代表应用正常退出,更不能取代数据保存确认。
同时,两份全新工程副本的真实替换、保存、退出及新 CAD 实例独立回读均通过,未发现本轮保存数据丢失。上述问题不是已经证明的工单 #1 唯一根因;原附件中的 #1 属于读取后的收尾失败,#3 尚未启动 CAD。现场版本、实际触发路径和完整诊断仍须对照。
## 一、实机正常链路
使用已有 Audio_D2128、Audio_T605A fixture 的全新副本,生产服务入口执行 BOM 导入及两工程队列,每工程仅 RV05:`4100-CA0000-T7 → 4100-CA0000-T0`,保留原生目标 Value 策略。原始 fixture 未修改。
证据目录:`build/day0-resource-analysis-20261026/live-chain-bbd9fba7ce444e3d8e5627a6f4a385d0/`。汇总:`build/day0-resource-analysis-20261026/live-chain-summary.json`。进程时间线为同级 `live-chain-bbd9fba7ce444e3d8e5627a6f4a385d0-process-timeline.jsonl`。
| 检查 | 本次结果 |
| --- | --- |
| BOM 导入 | 2 行通过,3.875 秒 |
| 顺序执行 | 两任务 completed,155.843 秒,共用一个执行 CAD |
| 保存关闭图页调用 | D2128 约 0.32 秒,T605A 约 0.21 秒 |
| 保存后的 CloseProject | 两次返回 True,耗时约 1.68 / 1.33 秒;均先观察到 ProjectClosed 事件,再观察到调用返回 |
| 同实例重开回读 | 每工程完成内容核验后才发布 saved_ok,随后关闭工程 |
| CAD 自然退出 | 本轮五个 CAD 实例(前读 2、执行 1、后读 2)主进程均正常退出码 0,无 CAD 主进程 terminate_request |
| CAD 退出等待 | Quit helper 成功后的主进程等待约 0.234–0.390 秒,不是整个 Quit 的总耗时 |
| 退出后的独立回读 | 每工程用新 CAD 实例读取;57 器件、103 引脚连接,56 个非目标器件不变,目标身份、位置、连接和目标料号正确 |
| 原件 | 两个源工程目录哈希不变,中央库 `.lmc` 哈希不变;未宣称整个库目录全部文件无变化 |
| 前台与资源 | 观察器未发现自有 CAD 进入输入桌面或成为前台,已记录 CAD 退出和桌面消失 |
这轮运行的是源码服务层、真实 CAD 和加入边界日志的 VBS 副本;没有修改生产源码。不是完整冻结 GUI/监督进程验收,时序日志可能增加少量开销,耗时不能作为性能承诺。
第一轮记录 `live-chain-366340e7a6a942ccadaf2e1eedd9c4f4` 因探针复制 VBS 时引入 UTF-8 BOM,导致第 1 行编译失败,替换前停止。该失败属于探针,不是产品故障;保留原记录和阻断状态,修正编码后使用另一组全新副本,未重放该次失败任务。
## 二、已验证问题与含义
### A. 辅助进程尚未自然退出,就调用 TerminateJobObject
位置:`isolated_desktop.py:380` 的 `_Win32Process.close`。处理顺序为:确认/停止根进程 → 收集后代 → **TerminateJobObject** → 最多等待 5 秒确认进程退出。因此这 5 秒是强制终止后的确认窗口,不是辅助进程正常收尾的宽限期。
两工程实机中五次 CAD 主进程退出后,Job 都仍有一个成员。原时间线的映像查询使用了本机 pywin32 未提供的函数,查询错误已保留;不能凭 PID 将五个成员全部命名。独立 CIM 快照将最后一次回读的 CAD PID 55500、子进程 PID 45368 对应到实际 `vnsd.exe`。
随后单独进行空 CAD 实验,改用 Win32 `QueryFullProcessImageNameW` 确认成员身份。仅推迟探针中对已正常退出 CAD 的 Job 清理,最长观察预算 75 秒,未打开任何工程:
- CAD PID 60332 正常退出码 0。
- 此时 `vnsd.exe` PID 34960 仍存活。
- 约 **0.813 秒**后该辅助进程自行退出,随后原清理函数运行时已没有需终止的后代。
- 最终自有资源全部释放,桌面检查耗时 0 秒,整个关闭阶段约 2.625 秒。
证据:`build/day0-resource-analysis-20261026/empty-cad-descendants-9fd000b5f0af48dc82e12b6b034d2917/report.json`;探针 `probe_descendant_exit.py` 位于同级分析目录。
既有原生进程研究将 `vnsd.exe` 识别为许可证辅助程序,另有工程数据库 `iCDBNetServer.exe` 和交叉探查 `xprobSvr.exe`。本次空 CAD 仅验证 vnsd 的自然退出,不证明数据库提交被终止,也不证明它就是工单现场残留桌面的持有者。
建议方向:对已经正常退出的根进程,其明确自有的后代先进行有界自然退出等待;到期才进入可审计的强制清理。区分根进程、数据库辅助进程、许可证辅助进程;记录终止对象、原因及发生时的业务阶段。不能只把等待常量变大,也不能对不明进程进行清理。
### B. 收尾记录的 I/O 错误会改变应用退出方式
位置:`isolated_cad.py:207`。正常 Quit 前的 `_record(state='closing')` 与 Quit 在同一个 try 内,记录失败会直接跳入异常处理,接着 finally 仍关闭自有资源。
故障注入:仅让这一次 closing 记录写入抛 PermissionError,其他记录正常。结果为:没有发出 Quit;写入 uncertain;调用资源 close。真实 CAD 由替身代替,因此本项证明控制流缺陷,没有实际杀死用户 CAD。
含义:目录权限、同步软件/安全软件的文件访问冲突、磁盘问题等不只影响日志,还可能改变应用本来可以正常退出的路径。上述环境来源只是候选,没有确定具体软件责任。
建议方向:隔离诊断/资源收尾记录错误与正常退出协议;写前业务保护失败仍应禁止写入,但不能混同为“所有诊断写入失败都必须跳过 Quit”。退出请求仍须满足实例身份、工程状态等原有条件。
### C. 保存状态在工程关闭成功之后才记录
位置:`QuickPartReplace.vbs:934`。调用 `QPCloseBatchProject(TargetPath, True)` 返回后才执行 `QP_BatchSaveCommitted=True`;这个子程序内部既保存图页,又关闭工程。
用真实 `QPFinishBatchItem` / `QPCloseBatchProject` 函数与 CAD 形状替身运行:图页的 SaveChanges=True 调用已经返回,随后工程拒绝关闭。观察到 `save_called=True`、`commit_flag=0`、错误已抛出,没有发布成功 checkpoint。
因此,这个标记不能独立表达“保存已发出/可能完成,而工程关闭失败”。某些后续错误文案依赖它,可能把已涉及保存的故障描述为未保存。`failed_native_result_uncertain` 仍会依据原生修改证据阻断重试,本轮没有发现借此自动重放替换;问题主要在恢复判断所需的状态粒度和事实描述。
建议方向:拆分 save_requested、save_returned、saved_readback_verified、project_closed 等证据;保存请求开始后发生异常应保留“可能已保存”的状态,不能因 CloseProject 失败回退成“未保存”。
### D. 资源消失后,正常退出异常不再向调用方抛出
位置:`batch_supervisor.py:159` 的 `_finish_response`,及 `batch_resources.py:117` 的重查逻辑。
用生产 BatchCadRuntime 创建合法资源记录,替身 CAD 在退出时抛出 Quit 超时,已保存业务结果独立保留;模拟拥有资源的监督进程结束后,再调用生产资源复核及返回函数。结果:
- 会话先 blocked;物理桌面已不存在后变为 released。
- `cad_shutdown.clean=false` 和 `orderly_shutdown` 异常仍保留在 JSON。
- 监督记录 `state=finished`、`outcome=error`。
- 调用方收到含 completed 条目的 TaskQueueResult,没有收到退出异常。
这是状态返回语义不一致,不是原始错误日志被删除,也不证明已独立核验的保存结果应当被作废。应保留正确业务结果,同时显式暴露“业务完成、应用异常退出/强制清理”的收尾状态,不能仅凭物理资源消失将异常归为恢复成功。
上述 B/C/D 复现实物:`build/day0-resource-analysis-20261026/fault-chains-907a534cc6bb441e8cddf7670dd04aac/`,脚本 `probe_fault_chains.py`。较早的临时探针曾因日志文件句柄尚未关闭而无法删除临时目录;后续改为保留独立证据目录并关闭日志句柄,此探针清理错误不计入产品结论。
## 三、仍需验证的高影响风险
| 风险 | 已知机制 | 尚不能断言的部分 |
| --- | --- | --- |
| 慢保存被当成无响应 | 保存前仅发一次阶段进度;默认 150 秒请求停止、300 秒判无响应,safe_stop=False 不阻止监督器升级。保存没有类似原生快照的进度旁路 | 没有真实慢保存达到 300 秒的实验,不证明现场曾在保存中被终止 |
| Quit/关闭响应延迟 | Quit helper 20 秒;成功后 CAD 15 秒;checkpoint 20 秒;项目会话关闭另有 60 秒宽限 | 本次正常样例均未超过这些时限,不能据此证明大工程也安全或必须统一加长 |
| 同实例回读可能不足以覆盖持久性 | 生产路径在同一个 CAD 实例中重开回读 | 本次新实例读回通过,未发现缓存掩盖丢失;仍缺断电、数据库服务异常和大型工程覆盖 |
| 关闭工程返回值被忽略 | 本机 TypeLib 表明 CloseProject 返回 Boolean;VBS 常直接调用并用工程路径为空作为补充判断 | 本次调用均为 True,尚未观察到 False 且空路径的真实异常,不能将其直接确定为事故根因 |
| GUI/监督进程异常终止 | Job 关闭会清理受控进程,不能保证正在进行的保存完成 | 正常点击关闭时 GUI 会等待当前 worker,不能把所有 GUI 关闭行为描述成直接杀进程。崩溃/强制结束另需故障实验 |
| 数据库和跨进程协作收尾 | 既有研究存在 iCDBNetServer、xprobSvr 等进程;仅确认 viewdraw 退出不足以描述全部应用状态 | 本轮没有捕获这些服务器在未提交数据时被终止;不能把 vnsd 的观察套用成数据库丢失证据 |
## 四、厂商接口核对的重要限制
实际安装的 7.9.5 `viewdraw.tlb` 已离线核对,结果保存在 `cad-lifecycle-typelib.json`:
- `IVdApp.CloseProject` 返回 Boolean。
- `IVdApp.Quit`、`IVdSchematicSheetDocument.Close(SaveChanges, FileName)` 返回 void,不能虚构它们有保存成功布尔返回值。
- `IVdDoc` 有 Saved 属性,但不能据此假定当前 `IVdSchematicSheetDocument` 也有同样的属性契约。
- 事件接口包含 BeforeDocumentSave、AfterDocumentSave、ProjectClosed。
真实 VBS 日志已订阅上述事件:本轮 ProjectClosed 被正常观测,但在两次 SaveChanges=True 路径中**没有观测到 BeforeDocumentSave/AfterDocumentSave**。因此不能因为类型库列出了保存事件,就把等待该事件直接设为当前原理图保存路径的必经条件。需要核实它适用的文档类型、接口和触发条件。
本轮读取的安装帮助未取得足以证明上述调用异步/同步持久化语义的详细契约。类型库提供方法签名,不能替代持久化保证。
## 五、验证与修改范围
本轮补充回归:
```text
python -m pytest -q tests/integration/test_quick_value_modes.py tests/v1_1/test_project_session.py tests/v1_1/test_failed_native_recovery_guard.py tests/v1_1/test_supervisor_result.py tests/v1_1/test_ui_operation_lifecycle.py
94 passed in 34.11s
```
上一轮已执行 IsolatedCad 和 worker monitor 的 60 项验证,本轮不重复当成新结果。测试通过说明已有行为与保护仍按测试约定运行,不证明上述异常路径合理。
仅增加分析报告和 `build/day0-resource-analysis-20261026/` 下的诊断脚本/证据。生产 Python、正式 VBS、版本号及发布包未修改,未向远端工单发消息或关闭工单。
下一轮修改应优先针对已复现的四个问题,并保留原有身份校验、写前保护和禁止不确定重放。验收必须分别报告数据结果、工程关闭结果、应用退出方式、后代进程退出方式和最终资源状态,不能汇成一个“释放成功”。
@@ -0,0 +1,140 @@
# Day0:资源释放工单原因分析
日期:2026-10-26(北京时间)。严重级别依据用户本次升级说明;未修改远端工单标签或状态。分析源码基线:`b9391015650bf5c43a77a017c5484e9a8f352266`。
后续已完成[核心应用生命周期专项排查](2026-10-26-application-lifecycle-investigation.md):包含两工程真实保存与独立读回、vnsd 自然退出对照及三类异常控制流复现。后续结论以该记录注明的证据和边界为准。
## 判断
工单 #1 的现场根因仍未闭环。根据用户对核心应用生命周期的补充,排查主线调整为“业务操作完成 → 保存确认 → 工程关闭确认 → 应用正常退出 → 进程及辅助资源释放 → 桌面回收”。桌面是隐藏操作的基础设施,其消失不能替代业务完成或进程正常退出证明。第一轮对桌面末端症状的侧重不足以覆盖保存与异常退出风险。
对已经观察到的桌面残留,需要继续追查自有清理范围以外的引用,包括本程序未登记的额外引用、无窗口线程、其他进程持有的桌面句柄和由系统或服务创建的辅助进程。这里的“范围以外”不等于已经证明是第三方软件造成,也不能将其提前确定为整个事故的业务根因。
工单 #3 在 CAD 未启动时出现同类残留,说明 CAD Quit 或 COM 释放不足不能解释全部现场现象。两个工单具有相同末端症状,但尚不能断言具体持有者或底层根因相同。
Beta 3 的进程隔离、批次复用和资源复核覆盖了部分故障机制。产品仍缺少现场持有者的完整识别能力,缺少对应现场版本的复测成功回执,不能凭本机通过将事故关闭。
## 补充核查:核心应用完成和退出链路
本节优先于后文仅针对桌面残留的原因排序。关闭桌面句柄不会自动结束进程;当前实现实际先清理受控进程、再关闭桌面句柄。应分别证明数据保存成功、工程关闭成功、应用正常退出、强制终止是否发生和资源是否消失。
### 正常路径已有保存回读,但完成语义仍需核实
- `QuickPartReplace.vbs:863` 的 `QPCloseBatchProject` 调用 `Document.Close(True, "")` 保存关闭图页,随后调用 `CloseProject`,立即查询工程路径为空。
- `QuickPartReplace.vbs:934` 起会重新打开同一工程、校验目标及非目标内容,之后才给出 `saved_ok`。不能把现有程序描述为“完全没有等待或没有保存核验”。
- COM 调用返回是现有的等待边界,但当前函数没有单独的保存完成事件、持久化提交状态或忙碌状态等待。是否需要增加这些机制,必须核对该版本 CAD 的 API 契约及真实行为;不能仅因缺少 sleep 就断言保存是异步或保存未完成。
- 生产路径是在同一个 CAD 实例中重开工程进行回读。这提供了内容核验,但若要证明进程退出后的持久性,应增加正常退出后的独立新实例读取实验,并核查实际数据库服务的提交语义。不能仅凭同实例回读断言存在缓存问题,也不能将开发验收中的独立回读当成每次生产执行都有此保障。
- 图页/工程关闭后立即查询一次状态,没有针对“调用已返回、关闭状态稍后才就绪”的有界状态等待。若厂商允许这种延迟,可能把仍在正常收尾的应用判为失败;目前还没有确认其 API 是这种行为。
### 明确存在的风险路径:应用未确认正常退出时进入强制清理
`isolated_cad.py:207` 的退出流程为:Quit helper 最多等待 20 秒;helper 成功才进一步等待 CAD 进程最多 15 秒。Quit helper 超时或失败时会跳过这次 CAD 正常退出等待,记录 uncertain,然后仍执行 `desktop.close()`。在批次模式中这里首先是 lease 的进程清理,随后才是批次拥有者的桌面关闭;底层 `_Win32Process.close()` 对存活自有进程/Job 有终止逻辑。
因此存在需要验证的链路:应用仍忙或关闭被拒绝 → 固定时限/单次状态检查失败 → 保护标记 → 强制终止自有进程 → 后续桌面释放检查。保护标记能避免无依据重放,不能保证被终止前未完成的保存可以安全完成。强制终止后的“资源清理成功”也不能用作“业务正常完成”的证据。
Quit helper 在发现工程仍打开时会拒绝退出,避免隐式保存或丢弃;但外层依然会进入自有进程清理兜底,所以必须检查当时是否有未完成的写操作。业务状态、退出方式和资源状态需要分开判断。
### 保存期间的无进度监控需要专项验证
`QuickPartReplace.vbs:874` 在进入保存关闭前发出一次 `save_and_close` 进度。若后续阻塞在 COM 调用中,脚本不能继续发送阶段进度。当前原生侧进度旁路只处理 `native_snapshot_read`,没有相同的保存进度旁路。
默认无进度时限为 150 秒。`ProgressWatch.action()` 在 150 秒请求停止、300 秒判定 unresponsive;这个判定不因 `safe_stop=False` 而停止升级。VBS 会在安全边界才响应协作停止,但监督器仍可在后续超时中终止 worker,并进入 CAD 收尾。真实慢保存是否能达到这个条件尚未实测;普通进程存活或无条件心跳也不能当成保存正在推进的证据。
### 本轮验证与证据边界
- `python -m pytest -q tests/v1_1/test_isolated_cad.py tests/trial_task_center/test_worker_monitor.py`:**60 passed,16.22 秒**。包括控制流替身和真实辅助进程测试,没有真实 CAD 慢保存实验。
- 对生产 `IsolatedCadSession` 注入模拟 Quit helper 超时:确认等待参数为 20 秒、CAD wait 未被调用、uncertain 标记已写入、之后进入资源 close。未终止真实 CAD。
- 对生产 `ProgressWatch` 输入模拟 `save_and_close / safe_stop=False`:150 秒为 request_stop,300 秒为 unresponsive。
- 证据:`build/day0-resource-analysis-20261026/application-lifecycle-probe.json`。这是控制流证据,不能写成现场已发生保存中断或数据丢失。
- 原始 #1 是库存读取后的收尾失败,四次日志记录 Quit helper/CAD 正常退出;#3 尚未启动 CAD。当前附件没有证明替换保存被提前打断。上述生命周期风险必须调查,但不能直接取代这两组现场证据。
优先实验应改为:在工程副本上记录保存调用前后、关闭调用前后、回读结果、Quit 请求与返回、CAD 自然退出、强制终止请求及原因的完整时间线;分别验证正常保存、长时间忙碌、关闭延迟和退出拒绝。数据正确性及退出契约查清后,再处理桌面末端残留。无需通过开启可见桌面或无界等待来替代这些完成条件。
## 已核实的证据
### 工单 #1:V1.1.3,四次资源释放确认失败
原件位置:`build/issue-1-20261021/evidence/`。四份失败的 `private-desktop.json` 一致记录:
- CAD 和退出辅助程序的退出码均为 0。
- 所记录进程的 `job_active_processes=0`,已知后代句柄均已确认退出,自有进程及 Job 句柄关闭。
- `desktop_handle_closed=true`,但 5 秒后 `released=false`。
- 四次清理来自同一个主程序 PID 14608,调用线程不完全相同,调用线程桌面为 `Default`。
- 另有一份采集中途取消的记录停在 `ready`,没有最终收尾证据;不能算作释放成功,也不能直接算作第五次同因泄漏。
这些证据降低了“已登记 CAD 主进程没有退出”“完全没等待终止”“已登记桌面句柄没有关闭”的优先级。它们不能证明进程所有线程、所有其他桌面句柄或所有系统辅助进程都已释放。
### 工单 #3:Beta 2,CAD 未启动也残留
本地 `build/issue-3-20261023/application.log` 与本次重新读取的远端附件逐字节一致。SHA256:`28d164039831973b7f6596a081d305421131f883514feae2fd652ff90ec54241`。
| 北京时间 | 日志位置 | 事实 |
| --- | --- | --- |
| 10-23 14:29:13 | 第 2 行 | 运行版本 `1.1.4-beta.2`,Windows 基础 build 26200 |
| 14:29:34 | 第 14 行 | 开始整批 BOM 检查 |
| 14:30:01 | 第 669 行 | 开始批末释放 |
| 14:30:06 | 第 1098 行 | 桌面句柄关闭,桌面仍存在;CAD PID 为空,显示 CAD 未启动;窗口列表为空 |
| 同一次失败的异常链 | 第 1102、1116 行及后续堆栈 | 首发错误为内部 attempt 目录创建的 `FileNotFoundError / WinError 3`,失败路径长 265 字符;释放错误发生在异常收尾期间 |
| 14:30:47 | 第 1155 行 | 用户请求重新清理资源 |
| 14:30:54 | 第 2013 行 | 同一个桌面仍存在,句柄关闭、CAD 未启动、窗口列表为空 |
前后失败样本相隔约 48 秒。未连续观测期间的状态,但相同随机桌面名两次均被枚举,明显削弱了“只需略微增加 5 秒超时”的解释。
`cleanup.processes=[]` 仅表示该次最终清理列表为空。原生 CLI 在批内执行后会从活动列表移除,不能据此说整批从未启动过子进程。
长路径是已记录的首发业务错误;桌面残留是收尾时另一个故障。修复路径问题不自动证明桌面引用来源已修复。
## 桌面残留的原因排序和判别办法
“优先级”表示调查顺序,不表示统计概率。
| 原因候选 | 优先级及证据 | 如何区分 |
| --- | --- | --- |
| 本程序的其他线程或额外桌面句柄仍持有引用 | 高;旧版 GUI 生命周期长,关闭登记句柄不能排除额外引用。四次 #1 来自同一主程序 | 现场保留桌面名,由独立观察进程在监督进程退出前后、主程序正常退出前后枚举;配合句柄和线程归属快照。随某进程退出而消失可缩小范围,但仍需持有者证据,不能单凭时间相关性定责 |
| Job 外的无窗口辅助线程/进程 | 高;完全符合“Job 清空但桌面存在”。历史受控实验发现过无窗口 Python 和 conhost 线程 | 对目标桌面扫描全局线程,记录 PID/TID、映像、创建时间、会话及 Job 归属;与原生 CLI 启停时间对齐。历史 conhost 只是实验线索,现场未确认 |
| 其他进程仅持有 OpenDesktop 句柄 | 高;可在不启动 CAD、没有窗口和目标线程的情况下复现相同症状 | 在线程扫描之外查询 Desktop 类型句柄。已有外部句柄实验复现阻断,持有者释放后可恢复;具体现场持有者未知 |
| 原生 CLI、运行库、服务代理触发额外引用 | 中高;#3 没启动 CAD,但此前走过原生读取链 | 在现场副本依次运行空桌面、简单子进程、单个 icdb2ascii 读取、多个读取、CAD 回退,找出首次出现残留的阶段。普通子进程默认受 Job 约束,不能笼统断言所有后代都逃逸 |
| 安全软件、录屏、远程协助、桌面管理组件等参与桌面访问 | 中,属于上一类的来源候选;当前没有具体软件或 PID 证据 | 先采集持有者或模块证据,再做单因素环境对照;没有证据时不能指定某个软件为元凶 |
| Windows USER 引用延迟回收/系统环境差异 | 中低;短暂延迟有受控实验支持,但现场约 48 秒后的重查仍失败 | 使用独立进程记录 0、1、5、15、60 秒存在性,同时记录所有者退出状态。本机和 #3 均为基础 build 26200,仍需对照累计更新、运行库和注入组件,不能只按系统大版本归因 |
| 句柄继承、Job 接管或异常清理分支缺陷 | 中低,需保留;当前代码在恢复运行前分配 Job,重定向使用明确的继承句柄清单,已有后代退出和句柄关闭失败测试 | 检查实际运行 EXE 哈希、Job 分配结果、句柄继承标志、失败时保留的所有权记录。当前源码机制不能倒推所有旧版或现场程序完全相同 |
| 历史资源记录损坏或残留,形成逻辑阻断 | 对“升级后仍不能用”须优先区分;代码会保守阻断坏记录 | 对比当前物理桌面/进程与 `session.json`、`supervisor.json` 的状态。若物理对象已消失而记录无法校验,是证据恢复问题;不应归为仍有物理泄漏。原始 #1/#3 明确枚举到桌面,坏记录不能解释其最初残留 |
| CAD COM 引用未释放 | 对 CAD 路径仍可查;作为统一解释优先级低 | 仅在已启动 CAD 的场景核对 COM、helper 与 CAD 生命周期。#3 的 CAD 未启动是其无法解释全部现象的直接证据 |
| 工程数量上限、桌面堆耗尽、纯 BOM 匹配错误 | 当前证据弱 | 查桌面创建失败、内存资源错误和随批量增长的资源曲线。当前主要失败在销毁确认;BOM 行不匹配是独立业务结果,不能据此改资源配额或放宽匹配 |
微软文档确认:关闭一个桌面句柄与销毁所有引用不是一回事;线程的桌面连接不依赖存在可见窗口;Job 默认覆盖普通 CreateProcess 子进程,但不是整个系统的进程集合。例如 WMI `Win32_Process.Create` 创建的进程不属于这类默认继承范围。此机制说明需要查 Job 外所有者,不代表本软件已经被证明使用了 WMI 启动逃逸进程。
## 当前实现为何仍可能卡住
1. **判定严格,但归属诊断不足。** `isolated_desktop.py:66` 通过枚举确认桌面是否消失;失败时只收集窗口。`isolated_desktop.py:565` 明确将外部句柄持有者标为 unknown。空窗口不是无引用证明。独立工具 `tools/diagnose_private_desktop.py` 能查无窗口线程,但不能列出所有仅持有句柄的进程,权限错误也必须保留。
2. **监督进程隔离只解决其覆盖的所有权。** `batch_supervisor.py:279` 等待并关闭受控 Job,随后 `_finish_response` 复核资源。这能够处理监督进程自身存活期间的引用;其他长寿命进程的引用不会因该监督进程退出而消失。
3. **重新清理有能力边界。** `batch_resources.py:117` 对仍有 live owner 的会话可重试其关闭;没有 live owner 时只确认桌面消失和历史 PID 身份,不会接管或终止不明持有者。Beta 3 常见的是监督进程已经退出后的复核分支。反复点击不会凭空释放外部引用。
4. **未释放门禁扩大可用性影响。** `guard_pending_resources` 阻止同一数据根的新批次。这一保护避免在旧资源不明时继续创建桌面或重放业务,却使一个释放问题阻断后续使用。恢复机制和持有者识别必须共同补齐。
5. **损坏记录不会自动修复。** 无法校验的记录保持 blocked;无 session_id 时跳过自动资源操作。这是证据不足时的保护行为,但意味着资源已消失仍可能需要审计式人工核对。不能通过删除保护文件掩盖该状态。
6. **诊断包不等于实时取证。** 当前导出函数主要打包已有文本,未自动执行全局线程或句柄扫描。上限为单文件 4 MiB、总计 32 MiB、512 个文件,超限写入 omissions;必须检查 manifest,不能把未导出的记录当成从未发生。
## 本次新验证
运行环境:Python 3.12.10、Windows 11 基础 build 26200。以下测试未打开或替换用户工程。
- `python -m pytest -q tests/v1_1/test_isolated_desktop.py tests/v1_1/test_desktop_diagnostics.py tests/v1_1/test_batch_runtime_windows.py tests/v1_1/test_batch_supervisor.py`:**44 passed,87.89 秒**。包含真实 Windows 资源测试与替身测试,覆盖无窗口引用、延迟释放、后代进程、诊断边界、监督恢复等;不等同于现场完整 EXE 验收。
- `python -m pytest -q tests/v1_1/test_batch_runtime.py::test_unknown_resource_evidence_never_authorizes_cleanup tests/v1_1/test_batch_runtime.py::test_restart_reconciles_only_confirmed_disappearance_without_replacement`:**4 passed,1.22 秒**。确认坏证据继续阻断、重启后仅复核的代码行为;这两类为模拟边界测试。
- 资源循环探针 `build/day0-resource-analysis-20261026/probe_cycles.py`:20 次空桌面、20 次带重定向的简单 Python 子进程,**40/40 正常释放**。自有桌面残留 0,观察前后没有新增桌面,调用线程桌面未改变。两组的释放枚举检查最大耗时均约 0.016 秒,该数字不包含完整子进程启动耗时。
- 循环证据:`build/day0-resource-analysis-20261026/cycles-6819874fa1b04649a6dbb9c6b1992034.json`。它是源码 Python 基础路径实验,没有覆盖 Qt GUI、冻结 EXE、实际 icdb2ascii、CAD 或现场常驻组件,不排除这些组合中的问题。
这批结果降低了本机基础资源封装普遍失效的可能性,不能证明现场根因已修复,也不能证明 102 个原工程全部可用。
## 下一步应取得什么证据
1. **先确认事故实际版本和状态。** 当前收到的两组原件属于 V1.1.3 与 Beta 2;没有 Beta 3 的新失败诊断包。记录现场 EXE 哈希、是否完整配套 `_internal`、原始 data 是否迁移、故障是再次产生桌面还是旧记录阻断。Beta 3 公开 EXE 的历史记录哈希为 `df08cd328f6f4c9019f232c845e09627551e7b2d24790a466d53efcd291622d1`,以实际安装文件核对。
2. **残留发生时立即取证。** 在原机器、原登录会话,从 session 记录取得准确桌面名;运行独立线程诊断并查询 Desktop 句柄持有者,保留 PID、创建时间、映像、线程、会话、Job 和时间戳。仅复制旧日志到开发机无法找回消失的 Windows 引用。
3. **独立观察所有者退出前后。** 先保存结果及诊断,再观察监督进程退出、应用正常退出后的同名桌面变化。桌面随监督进程退出消失,优先追监督进程内部引用;随 GUI 退出消失,追 GUI 或其关联组件;GUI 退出后仍存在,追其他进程或系统引用。上述变化用于缩小范围,最终需结合持有者证据确认因果。
4. **按最小场景逐层复现。** 空桌面 → 简单 worker → 单次原生 CLI → 多次原生 CLI → CAD 回退 → 批量混合。每层记录创建数量、已退出进程、最终桌面状态。实际读取使用工程副本,首次失败就保留证据,避免反复业务执行污染现场。
5. **修复后验证恢复结果。** 对已识别的持有者修复生命周期;完成正常、异常、取消、重启和跨批次检查,确认只清理资源时业务结果不变、没有重复替换,随后在现场原目录和原批量范围验收。只有现场复测通过并留存回执,才具备关闭工单的依据。
技术交付首先补齐业务完成与应用退出的分阶段证据,并专项验证等待与强制清理边界;对完成业务后仍残留的桌面,再补自动捕获持有者的能力。单纯延长等待、反复要求重新清理或继续发布未取得现场证据的候选包,都不足以建立闭环。
## 关联记录
- [工单 #1](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/issues/1)、[工单 #3](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/issues/3)。
- [首轮工单分析](2026-10-21-issue-1-bom-import.md)、[Windows 生命周期受控实验](2026-10-21-windows-desktop-lifecycle.md)、[外部句柄持有者实验](2026-10-23-issue-3-external-holder.md)、[Beta 3 验收边界](2026-10-23-issue-3-safe-recovery.md)。
- 微软:[CloseDesktop](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-closedesktop)、[线程连接桌面的规则](https://learn.microsoft.com/en-us/windows/win32/winstation/thread-connection-to-a-desktop)、[Job Objects](https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects)。本次重新核对官方页面。
@@ -0,0 +1,59 @@
# 用户停止与安全收尾实施验收
日期:2026-10-26;分支:`codex/day0-safe-stop`。实现用户在本会话批准的[停止流程](../superpowers/specs/2026-10-26-safe-user-stop.md),对应[全链路调查](2026-10-26-application-lifecycle-investigation.md)。
## 实现行为
- 普通暂停和关闭窗口不启动下一项,当前写入、保存和回读安全完成后停止。当前项并不等于整个工程。
- 有写入可能的阶段长期没有进度时显示等待,不自动把超时升级成杀进程。纯只读采集保留有界取消。
- 写入失败、保存结果不确定或 Quit 被拒绝/超时且 CAD 仍存活时,交互执行保留应用与工程,等待处理,不自动保存或丢弃。
- “强制结束…”独立确认;先持久化业务保护及批次中断证据,再终止明确自有的监督进程及 Job。证据写入或 fsync 失败时拒绝该次 force,界面保留重试能力。
- 保存请求、保存调用返回、保存回读和工程关闭分别记录,工程关闭失败不会丢失此前保存边界。
- 收尾记录写入失败不跳过满足身份检查的正常 Quit。主进程自然退出后给予自有后代 2 秒自然退出窗口,之后的强制清理单独记录。
- 资源消失不吞掉异常退出;调用方收到独立收尾错误,已核验业务结果仍附带保留。
- 等待期间可独立导出诊断包。关闭窗口在操作结束前不退出;失败/强制结束后取消自动关闭,保留恢复提示。
## 自动验证与审查
针对安全边界先运行失败用例再实现,包括慢保存不会被 watchdog 终止、明确 force 的保护先行、保护写入失败保留进程、已有文件也重新 fsync、保存后工程关闭拒绝、窗口关闭不触发 force、确认取消无副作用。
真实辅助进程测试覆盖监督子进程强制中断、控制回调异常时保持子进程存活、拒绝第一次保护写入后仍允许后续明确重试、资源清理不重放业务。CAD 调用在这些故障测试中使用替身,不能据此声称真实保存中断无损。
退出协议独立审查修复了部分子进程查询失败时丢失已知强制终止 PID 的问题。整体验证另外修复:只读入口没有 force 出口却进入无限等待、证据首次 fsync 失败后因文件存在被误放行、已执行 force 被诊断异常降级为普通停止、控制通信异常绕过确认清理、直接执行 force 失败没有反馈,以及 force 意图与监控异常并发的竞态。复审未发现新增阻断问题。
首次完整实现回归 **1063 passed,1 条既有 pywinauto STA 警告,363.82 秒**。之后完成审查修正,最终扩大回归 **1147 passed,1 条既有 pywinauto STA 警告,325.64 秒**:
```powershell
python -m pytest -q tests/v1_1 tests/trial_task_center/test_application.py tests/trial_task_center/test_execution_safety.py tests/part_replace/test_verification.py tests/integration/test_quick_value_modes.py tests/integration/test_quick_session_startup.py tests/integration/test_quick_part_replace_contract.py tests/integration/test_automatic_replacement_regression.py tests/trial_task_center/test_worker_monitor.py tests/trial_task_center/test_batch.py
```
本命令包含项目发布脚本要求的源码测试范围,并补充此次受影响的 VBS、worker monitor 与批次测试。`git diff --check` 与新探针/停止模块语法检查通过。
两个旧 VBS fixture 在基线 `b939101` 也失败:未填 owned project path 且错误期望 no-match 保存、图页清单断言未使用规范化路径。修正的是 fixture 和断言,没有放宽生产所有权或保存规则;脚本契约组 72 项通过。
## 真实保存期间请求普通停止
工具:[probe_safe_stop_live.py](../../tools/probe_safe_stop_live.py)。本轮输出为 `build/safe-stop-live-a9113545bf/`,报告 `report.json`,最终资源复核 `final-resource-check.json`。
入口为源码 supervised service → 真实独立 batch worker → 生产 VBS → 真实 CAD,使用两份全新 Audio_D2128/Audio_T605A 副本。监听第一工程的生产 `save_requested` 日志后只设置普通停止 Event,不设置 force。此触发处于保存请求附近,不能声称精确暂停在 COM 内某一条指令。
| 检查 | 结果 |
| --- | --- |
| 第一个工程 | 当前 RV05 替换、保存和回读完成,任务 completed |
| 第二个工程 | 保持 pending,没有继续替换 |
| 队列 | paused=true,未请求强制结束 |
| 第一工程新 CAD 独立读回 | 57 器件、103 引脚连接;目标 RV05 为新料号,56 个非目标器件保持;目标身份、位置和连接保持 |
| 第二工程新 CAD 独立读回 | 57 个器件的完整属性及连接图与执行前一致 |
| 执行批次退出 | orderly_shutdown=true、clean=true、forced_termination=false |
| 资源 | 资源记录全部 released,最终枚举确认其桌面消失,没有本轮残留 CAD |
| 原件 | 两个源工程目录哈希不变,中央库 `.lmc` 哈希不变 |
没有重放任何已经完成的替换;测试用的 before/after 独立读取使用新 CAD 实例。
## 边界
本轮没有构建或发布新 EXE,未做完整冻结 GUI 的保存中强制结束、掉电或原工单全量现场复测。GUI 行为使用 Qt 测试验证,真实保存停止使用源码监督入口验证;二者不能混写为冻结 GUI 全流程验收。
强制结束为避免切换任务竞态,保守标记本批可能涉及的目标;已完成业务证据和结果保留,但先前已完成任务也可能需要恢复核验。不能清除保护来继续。
保持应用等待不代表保存结果一定可以恢复。外部任务管理器强杀/系统掉电没有安全收尾保证,重启必须依赖原先写前证据核验。工单现场具体桌面持有者仍未查明,本实现不能当作现场根因已经闭环。
@@ -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 分配失败或自动重放替换来换取表面兼容。环境不允许安全执行时,必须停止并保留明确诊断。
@@ -0,0 +1,21 @@
# 字段采集鲁棒性与回退策略
器件采集不能假设 COM 属性、用户属性或显示属性都存在且可读。当前按字段用途分层:
| 字段 | 作用 | 读取失败时 |
|---|---|---|
| UID | 组件唯一身份、选择、回读和恢复关联 | 优先使用真实 UID;缺失时使用图页内唯一 Refdes 的本地键 `@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 阻断和无位号场景。兼容性回归持续要求无歧义时才允许继续。
@@ -0,0 +1,45 @@
# 库存入口与独立脚本回归
工作分支:`codex/project-optimization`。本轮延续无 UID、可选字段回退和监督进程可靠性修复。
## 复现并修复的问题
- 独立原生读取脚本只抽取固定函数列表,遗漏 `QPSnapshotAttribute` 新增依赖 `QPReadOptionalAttributeValue`,真实 `cscript.exe` 执行返回 `Variable is undefined`。已把依赖加入脚本生成列表,沿用实际生成和执行脚本的回归用例。
- 库存入口生成 fallback identity 后,导出前仍检查 CAD 原始 UID 非空,导致无 UID 工程仍被阻断。已检查本地 identity 和非空位号;导出后关联键为 `@REFDES|<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 <prefix>`;工具自带 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 工程保存重开回读、网络共享和其他机器许可证/中央库条件,仍需现场证据。
+30
View File
@@ -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|<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 选择、保存回读和窗口收尾。
@@ -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 收尾异常后的人工清理、跨重启资源复核、保存中断后的恢复入口,以及冻结包中的同一行为。
@@ -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 工程、中央库/许可证和网络共享条件仍待现场验收。
@@ -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 替换、中央库/许可证兼容与冻结包实机仍须单独验收;模拟进程通过不能代替这些现场结论。未构建或发布新版本;修改保留在目标分支的工作树中。
@@ -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 注册、网络共享和企业策略现场验收。
@@ -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`。
@@ -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"
}
}
@@ -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
}
}
+2
View File
@@ -1,5 +1,7 @@
# R8:进度式超时与分阶段引脚索引
> 本文保留 R8 历史行为。2026-10-26 起,有写入可能的阶段无进度超时改为等待安全收尾,只有明确确认的强制结束才中断;当前停止与恢复规则见[停止、强制结束与恢复](safe-stop-and-recovery.md)。只读采集继续采用有界取消。
## 改变的行为
- 150 秒不再是整个任务的总时限,而是连续没有有效进度的观察窗口。有有效进度时可以继续执行超过 150 秒。
+48
View File
@@ -0,0 +1,48 @@
# 停止、强制结束与恢复
执行期间点击“完成当前项后暂停”,程序不再启动下一替换项。已经进入修改或保存的当前项会继续完成必要校验、保存、重开回读和工程关闭,然后正常退出 CAD,并等待自有辅助进程收尾。关闭主窗口也遵循这个流程,操作仍在进行时窗口保持打开。
“当前项”是当前图页和替换规则对应的执行单元,不是整个工程或整个批次。已经保存核验的结果保留,未开始的任务保持未执行。
## 保存或修改长时间没有进度
有写入可能的阶段,程序不会仅因超过无进度时限就自动终止应用。界面显示保存或核验尚未确认,可以继续等待并导出诊断包。等待不会启动下一项,也不会自动清除保护记录。
如果操作已经失败且保存结果不确定,程序保留当前应用和工程现场,不再自动保存或丢弃。应先导出诊断、保留原始工程与备份,再决定是否需要强制结束。此时“继续等待”不表示自动恢复执行。
纯读取的 BOM/库存采集仍采用有界取消;不存在写入需要完成时,不要求用户无限等待。确认未修改的启动失败也保留既有有界停止机制。
## 明确选择强制结束
“强制结束…”与普通暂停分开。确认框明确提示:当前操作可能正在修改或保存,强制结束可能留下未保存或部分保存结果,之后必须先核验恢复。
确认后,程序先持久化中断保护和批次记录,再终止自己拥有的监督进程及 Job。保护记录无法可靠保存时,不执行这次强制结束,界面提示原因并允许重试或导出诊断。取消确认没有终止副作用。
为避免强制请求与工程切换同时发生而漏记目标,强制结束会保守保护本批可能涉及的任务。先前已保存结果不会被覆盖或重放,但这些任务也可能显示需复核或被保留在当前列表。不要删除保护文件绕过核验。
## 程序重启之后
- 资源复核只处理进程和桌面状态,不重新替换。
- 结果不确定的任务必须使用“核验并恢复”,按照已有快照和当前实际工程核对。
- 已保存核验结果继续保留;资源清理成功不自动解除业务保护,也不自动继续队列。
- 如果应用没有正常退出或后代进程需要强制清理,会单独报告收尾异常,不能把“资源已释放”理解为“保存和退出均正常”。
任务管理器强杀、系统关机或掉电可能不给程序写最终日志的机会。这些情况依赖修改前已经落盘的证据及下次启动的中断核验;程序不能保证被外部强杀时完成当前保存。
## 后台进程启动或结果异常
`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`、保存回读和保护记录仍是执行/恢复的依据。辅助日志写入失败不会覆盖已经提交的业务结果。若强制结束后的确认记录写入失败,仍显示资源/记录待处理并保留强制结束事实,不能直接开始下一次替换。
诊断包会包含上述文件,并沿用账号、令牌与用户目录脱敏规则。原始工程和库文件不会随诊断包导出。
@@ -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 & _
+225 -56
View File
@@ -25,10 +25,12 @@ 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
Dim QP_BatchSaveCommitted
Dim QP_BatchSaveCommitted, QP_BatchSaveRequested
Dim QP_DiscoveryMode, QP_TargetSchematic, QP_TargetSheet
Dim QP_FailureNumber, QP_FailureSource, QP_FailureDescription
Dim QP_NcSyncSummary
@@ -60,6 +62,7 @@ QP_BatchMode = False
QP_OwnedProject = False
QP_BatchState = ""
QP_BatchSaveCommitted = False
QP_BatchSaveRequested = False
QP_DiscoveryMode = False
QP_TargetSchematic = ""
QP_TargetSheet = ""
@@ -68,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"
@@ -110,6 +115,10 @@ Else
QP_FailureDescription = Err.Description
If QP_DiscoveryMode Then QPCleanupDiscoveryFailure
FailureState = "hard_failure"
If QP_BatchSaveRequested Then
FailureState = "batch_saved_unverified"
QP_FailureDescription = "save was requested; persisted outcome requires reconciliation. " & QP_FailureDescription
End If
CloseError = ""
If QP_BatchMode Or QP_SessionMode Then
QPDiscardFailedBatchProject CloseError
@@ -318,6 +327,7 @@ Sub QPSessionNext()
QP_BatchMode = True
QP_BatchState = ""
QP_BatchSaveCommitted = False
QP_BatchSaveRequested = False
QP_NativeMutationPossible = False
If Trim(QPReadUtf8(QP_Fso.BuildPath(QP_Root, "batch.request"))) <> QP_ExpectedJobId Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.ProjectSession", "next batch authorization mismatch"
@@ -406,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
@@ -472,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
@@ -708,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
@@ -733,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
@@ -770,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")
@@ -872,12 +929,21 @@ Sub QPCloseBatchProject(ByVal ExpectedPath, ByVal SaveChanges)
If Documents.Count <> 1 Then Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "open sheet set changed; no automatic close"
Set Document = Documents.Item(1)
QPProgress "save_and_close", 0, 1
If SaveChanges Then
QP_BatchSaveRequested = True
QPLog QP_Root, "INFO", "save_requested job=" & QP_CurrentJobId
End If
Document.Close CBool(SaveChanges), ""
If SaveChanges Then
QP_BatchSaveCommitted = True
QPLog QP_Root, "INFO", "save_returned job=" & QP_CurrentJobId
End If
If QPCanonicalPath(QPProjectPath()) <> QPCanonicalPath(ExpectedPath) Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "project changed during sheet save"
QP_App.CloseProject
If Len(CStr(QP_App.GetProjectData().GetProjectFilePath())) > 0 Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "project did not close; queue stopped"
QPLog QP_Root, "INFO", "project_closed job=" & QP_CurrentJobId
QP_OwnedProject = False
QP_OwnedProjectPath = ""
QPInvalidatePinIndex
@@ -888,6 +954,10 @@ Sub QPDiscardFailedBatchProject(ByRef ErrorDescription)
Dim ErrorNumber, ErrorSource, Description
ErrorDescription = ""
If (Not QP_BatchMode And Not QP_SessionMode) Or Not QP_OwnedProject Or QP_App Is Nothing Then Exit Sub
If QP_NativeMutationPossible Or QP_BatchSaveRequested Or QP_BatchSaveCommitted Then
ErrorDescription = "write or save outcome requires reconciliation; owned project retained without automatic save or discard"
Exit Sub
End If
On Error Resume Next
QPCloseBatchProject QP_OwnedProjectPath, False
ErrorNumber = Err.Number
@@ -932,7 +1002,6 @@ Sub QPFinishBatchItem()
If FinalState = "restored_ok" Then
QPMarkBatchMutation Command
QPCloseBatchProject TargetPath, True
QP_BatchSaveCommitted = True
ElseIf Not QP_SessionMode Then
QPCloseBatchProject TargetPath, False
End If
@@ -945,13 +1014,14 @@ 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
QPVerifyRestoredPlans Command, Records, Plans, ValueStates
If Not QP_SessionMode Then QPCloseBatchProject TargetPath, False
FinalState = "saved_ok"
QPLog QP_Root, "INFO", "saved_readback_verified job=" & QP_CurrentJobId
If QP_SessionMode Then
QP_BatchMessage = QP_BatchMessage & " Saved and reopened for readback; owned project checkpoint verified."
Else
@@ -1153,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
@@ -1166,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")
@@ -1216,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
@@ -1229,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)
@@ -1245,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
@@ -1299,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)
@@ -1341,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)
@@ -1381,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)
@@ -1390,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
@@ -1452,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)) & _
@@ -1469,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
@@ -1511,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
@@ -1520,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 & _
@@ -1579,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
@@ -1617,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
@@ -1776,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
@@ -1885,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
@@ -1895,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"), _
@@ -1913,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
@@ -2578,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)
@@ -2625,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
@@ -2654,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) & "|" & _
+4 -4
View File
@@ -1,8 +1,8 @@
# UTF-8
VSVersionInfo(
ffi=FixedFileInfo(
filevers=(1, 1, 4, 3),
prodvers=(1, 1, 4, 3),
filevers=(1, 1, 4, 4),
prodvers=(1, 1, 4, 4),
mask=0x3f,
flags=0x2,
OS=0x40004,
@@ -15,11 +15,11 @@ VSVersionInfo(
StringTable('040904B0', [
StringStruct('CompanyName', 'DxProductivity'),
StringStruct('FileDescription', 'DxDesigner Part Replacement'),
StringStruct('FileVersion', '1.1.4.3'),
StringStruct('FileVersion', '1.1.4.4'),
StringStruct('InternalName', 'DxPartReplace'),
StringStruct('OriginalFilename', 'DxPartReplace.exe'),
StringStruct('ProductName', 'DxDesigner Part Replacement'),
StringStruct('ProductVersion', '1.1.4-beta.3')
StringStruct('ProductVersion', '1.1.4-beta.4')
])
]),
VarFileInfo([VarStruct('Translation', [1033, 1200])])
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "dx-productivity"
version = "1.1.4b3"
version = "1.1.4b4"
requires-python = ">=3.12,<3.13"
dependencies = ["PySide6>=6.7,<7", "pywin32>=306", "pywinauto>=0.6.9,<1", "openpyxl>=3.1,<4"]
@@ -66,10 +66,11 @@ class _DesktopLease:
for process in reversed(self.processes):
try:
self.owner._release_process(process, desktop=self.desktop, generation=self.generation)
records.append(getattr(process, 'cleanup_record', {}))
except Exception as error:
errors.append(str(error))
remaining.append(process)
finally:
records.append(getattr(process, 'cleanup_record', {}))
self.processes = list(reversed(remaining))
self.cleanup_record = {'scope': 'operation', 'processes': records,
'workers_closed': not errors, 'desktop_retained_by_batch': True, 'errors': errors}
@@ -99,6 +100,10 @@ class BatchCadRuntime:
self._retry_counts = {}
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."""
@@ -116,6 +121,17 @@ class BatchCadRuntime:
return self
def __exit__(self, error_type, error, traceback):
if error is not None and self._active_evidence is not None:
# 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:
if not getattr(getattr(self.stop_event, 'force_event', None), 'is_set', lambda: False)():
raise
# Explicit force has durable protection; cleanup is now allowed.
try:
self.close()
except Exception as cleanup:
@@ -190,7 +206,8 @@ class BatchCadRuntime:
started = time.monotonic()
self._save(cad_start_requested=True)
cad = factory(self.path.parent / f'generation-{self.generation + (self.desktop is None)}',
abort_event=abort_event, desktop=lease)
abort_event=abort_event, desktop=lease,
**({'stop_control': self.stop_event} if hasattr(self.stop_event, 'force_event') else {}))
self.cad = cad # Retain cleanup ownership even if startup fails.
try:
cad.__enter__()
@@ -241,12 +258,14 @@ 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
from .isolated_cad import PrivateCadStartupError, is_identity_conflict
from .worker_monitor import run_monitored_process
root = canonical_path(command[3])
self._active_evidence = root
while True:
check_cancelled(abort_event or self.stop_event)
attempt_id = uuid4().hex
@@ -258,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
@@ -272,7 +296,11 @@ class BatchCadRuntime:
def start_worker(launch, **options):
return cad.start_worker(launch, extra_environment=environment, **options)
result = run_monitored_process(command, timeout, abort_event=abort_event,
cooperative_abort=cooperative_abort, process_factory=start_worker)
cooperative_abort=cooperative_abort, process_factory=start_worker,
**({'force_control': self.stop_event} if hasattr(self.stop_event, 'force_event') else {}))
if result.returncode:
self._retain_uncertain(root, project, expected,
WorkerMonitorError(result.stderr or 'worker failed after possible mutation'))
self._checkpoint(root, project, attempt_id)
if result.returncode not in (2, 3):
return result
@@ -294,6 +322,7 @@ class BatchCadRuntime:
and not (abort_event is not None and abort_event.is_set())
and not (self.stop_event is not None and self.stop_event.is_set()))
if not safe:
self._retain_uncertain(root, project, expected, failure)
raise failure
key = str(project).casefold()
self._retry_counts[key] = 1
@@ -302,6 +331,13 @@ class BatchCadRuntime:
self.close(rebuilding=True) # Prove disappearance before creating another.
self.desktop = self.cad = None
def _retain_uncertain(self, root, project, expected, error):
from .batch_attempt import can_retry_attempt
from .stop_control import wait_for_resolution
attempts = self._project_attempts.get(str(project).casefold(), expected)
if attempts and not all(can_retry_attempt(path, record) for path, record in attempts):
wait_for_resolution(self.stop_event, root, error)
def recover_native_failure(self, project, evidence, reason):
"""One read-only recovery, based on durable source and worker proof."""
from .batch_attempt import can_retry_attempt
@@ -357,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):
@@ -380,14 +425,17 @@ 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 {}
self.record['cad_shutdown'] = {key: cad_record[key] for key in (
'state', 'clean', 'cad_exit_code', 'quit_helper_exit_code', 'cleanup_errors',
'shutdown_seconds') if key in cad_record}
'shutdown_seconds', 'orderly_shutdown', 'diagnostic_errors', 'forced_termination',
'forced_root_termination', 'forced_descendant_pids', 'termination_reason') if key in cad_record}
if self.desktop is not None:
try:
self.desktop.close()
@@ -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,28 +24,41 @@ _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
from .batch_resources import write_record, _valid_record
from .stop_control import protect_interruption
journal.update(state='blocked', outcome='interrupted', error=str(reason))
if journal['operation'] in _WRITE_OPERATIONS:
# A missing final result is not proof of no mutation. Publish the same
# protection the existing recovery UI understands, without overwriting
# any earlier, more specific marker.
journal['business_uncertain'] = True
for task_id in journal['task_ids']:
task_ids = journal['task_ids']
resource_path = Path(root) / 'batch-resources' / journal['batch_id'] / 'session.json'
try:
resource = json.loads(internal_path(resource_path).read_text(encoding='utf-8'))
if (not journal.get('protect_all_targets') and _valid_record(resource, resource_path)
and resource.get('active_task') in task_ids):
task_ids = [resource['active_task']]
except (OSError, ValueError, TypeError):
pass # Without valid active ownership, protect every possible target.
journal['protected_task_ids'] = list(task_ids)
for task_id in task_ids:
allowed = {canonical_path(root), canonical_path(Path(root) / 'tasks' / task_id)}
actual = journal.get('uncertainty_roots', {}).get(task_id)
roots = {canonical_path(actual)} if actual and canonical_path(actual) in allowed else allowed
for evidence in roots:
marker = internal_path(evidence / 'quick-run/uncertain.flag')
marker.parent.mkdir(parents=True, exist_ok=True)
try:
with marker.open('x', encoding='utf-8') as stream:
stream.write('reason=supervisor_interrupted; batch=' + journal['batch_id'] + '; manual reconciliation required\n')
stream.flush()
os.fsync(stream.fileno())
except FileExistsError:
pass
protect_interruption(evidence / 'quick-run',
'reason=supervisor_interrupted; batch=' + journal['batch_id'] + '; manual reconciliation required\n')
write_record(Path(root) / 'batch-resources' / journal['batch_id'] / 'supervisor.json', journal)
@@ -53,6 +67,85 @@ def _worker_context(root):
_WORKER_ROOT = canonical_path(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.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
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)
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:
_mark_interrupted(root, journal, error)
except Exception as failure:
control.control_error += ';中断证据尚未落盘:' + str(failure)
try:
write_record(Path(root) / 'batch-resources' / journal['batch_id'] / 'cancel.json',
{'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)
except Exception as failure:
control.force_event.clear()
control.control_error = '强制结束未确认,应用仍保留:' + str(failure)
else:
return True
try:
process.wait(timeout=.2)
return False
except subprocess.TimeoutExpired:
pass
except Exception as failure:
control.control_error = '无法确认应用退出,保留所有权等待处理:' + str(failure)
threading.Event().wait(.2)
def _file_lock(handle, acquire):
from .task_queue import _os_lock
_os_lock(handle, acquire)
@@ -159,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'))
@@ -170,6 +263,35 @@ def _finish_response(root, journal, response, context):
write_record(Path(root) / 'batch-resources' / journal['batch_id'] / 'supervisor.json', journal)
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 = {}
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', ()))):
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'])
@@ -186,6 +308,7 @@ def _finish_response(root, journal, response, context):
def run_supervised(service, operation, arguments, *, stop_event=None, on_progress=None, on_phase=None):
from .batch_resources import write_record, process_identity
from .isolated_desktop import _Win32Api
from .stop_control import StopControl
root = canonical_path(service._logging.root)
if stop_event is not None and stop_event.is_set():
raise TaskCenterError('operation_cancelled', '本批操作已取消,未启动监督进程。')
@@ -216,35 +339,81 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
journal['parent_process'] = process_identity(parent)
write_record(request, {'version': VERSION, 'batch_id': batch_id, 'attempt_id': attempt_id,
'root': str(root), 'database': str(canonical_path(service._store.path)),
'operation': operation, 'arguments': encode(arguments), 'task_ids': journal['task_ids']})
'operation': operation, 'arguments': encode(arguments), 'task_ids': journal['task_ids'],
'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:
try:
_force_owned_supervisor(root, journal, process)
except Exception as error:
force.clear()
stop_event.control_error = '强制结束未执行或未确认;请保留窗口和诊断:' + str(error)
service._logging.logger.error('force_stop_failed batch_id=%s error=%s', batch_id, error)
else:
force_accepted = True
break
if stop_event is not None and stop_event.is_set() and not sent_cancel:
write_record(directory / 'cancel.json', {'version': VERSION, 'batch_id': batch_id, 'attempt_id': attempt_id})
sent_cancel = True
try:
write_record(directory / 'cancel.json', {'version': VERSION, 'batch_id': batch_id, 'attempt_id': attempt_id})
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:
if hasattr(stop_event, 'control_error'):
stop_event.control_error = '停止请求尚未成功记录,正在重试:' + str(error)
try:
value = messages.get(timeout=.1)
except queue.Empty:
@@ -254,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'])
@@ -264,6 +434,7 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
state = decode(value['payload'])
service._task_queue_feedback = state['queue']
service._batch_feedback = state['batch']
service._supervisor_waiting = bool(state.get('waiting_for_resolution'))
if on_phase is not None and state['phase'] in {'recycling', 'closing'}:
from .bom_import_state import BomImportPhase
on_phase(BomImportPhase.RECOVERING if state['phase'] == 'recycling' else BomImportPhase.RELEASING,
@@ -271,20 +442,68 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
process.wait(timeout=15)
except Exception as error:
transport_error = error
if process is not None and isinstance(stop_event, StopControl):
force_accepted = _retain_supervisor_after_control_error(root, journal, process, stop_event, error)
finally:
service._task_queue_feedback = None
service._batch_feedback = None
service._supervisor_waiting = False
try:
if process is not None:
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:
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)
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'))
@@ -303,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)
@@ -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)
@@ -75,15 +70,25 @@ def run(request_path):
request = json.loads(request_bytes)
batch_id, attempt_id = request['batch_id'], request['attempt_id']
if (request.get('version') != VERSION or UUID(batch_id).hex != batch_id or UUID(attempt_id).hex != attempt_id
or request['operation'] not in OPERATIONS):
or request['operation'] not in OPERATIONS or type(request.get('interactive_stop', False)) is not bool):
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)
stop, finished, output_lock = threading.Event(), threading.Event(), threading.Lock()
from .stop_control import StopControl
stop = StopControl() if request.get('interactive_stop', False) else threading.Event()
finished, output_lock = threading.Event(), threading.Lock()
kind = {'inspect_bom_batch': 'bom_import', 'preview_bom_batch': 'bom_import',
'execute_task_queue': 'task_queue', 'execute_quick_run_queue': 'quick_run_queue'}.get(request['operation'], request['operation'])
runtime = BatchCadRuntime(root, kind, stop, session_id=batch_id)
@@ -113,7 +118,9 @@ def run(request_path):
emit('heartbeat')
feedback = getattr(service, '_task_queue_feedback', None)
emit('state', encode({'phase': runtime.record['state'], 'queue': feedback,
'batch': service.batch_feedback()}))
'batch': service.batch_feedback(),
'waiting_for_resolution': getattr(stop, 'waiting_for_resolution', False),
'resolution_error': getattr(stop, 'resolution_error', '')}))
except Exception:
stop.set() # Lost control evidence cannot authorize new work.
finished.wait(1)
@@ -124,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):
@@ -162,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})
@@ -169,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
@@ -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'}
@@ -13,6 +13,7 @@ class ExecutionFeedback:
message: str = '正在准备执行'
stage: str = ''
stage_seconds: float = 0.0
waiting_for_safe_completion: bool = False
def read_feedback(task_id, position, total, message, root: Path, started_after: float):
@@ -27,15 +28,16 @@ def read_feedback(task_id, position, total, message, root: Path, started_after:
except (OSError, ValueError, TypeError, AttributeError):
pass
path = root / 'quick-run' / 'worker-progress.json'
stage, seconds = '', 0.0
stage, seconds, waiting = '', 0.0, False
try:
modified = path.stat().st_mtime
if modified >= started_after:
record = json.loads(path.read_text(encoding='utf-8'))
waiting = record.get('waiting_for_safe_completion') is True
candidate = record.get('stage', '')
if candidate and candidate != 'finished':
seconds = float(record.get('stage_seconds', {}).get(candidate, 0)) + max(0, time.time() - modified)
stage = candidate
except (OSError, ValueError, TypeError, AttributeError):
pass # Display feedback is optional; it never changes execution state.
return ExecutionFeedback(task_id, position, total, message, stage, seconds)
return ExecutionFeedback(task_id, position, total, message, stage, seconds, waiting)
@@ -87,7 +87,7 @@ def _designer_executable():
class IsolatedCadSession:
"""The supervisor never attaches to COM on the user's input desktop."""
def __init__(self, root, *, abort_event=None, desktop=None):
def __init__(self, root, *, abort_event=None, desktop=None, stop_control=None):
self.root = canonical_path(root)
self.token = uuid4().hex
self.borrowed_desktop = desktop is not None
@@ -96,6 +96,7 @@ class IsolatedCadSession:
self.binding = None
self.started = time.monotonic()
self.abort_event = abort_event
self.stop_control = stop_control
self.record = {'desktop': self.desktop.name, 'token': self.token, 'state': 'starting'}
def _record(self, **values):
@@ -206,52 +207,115 @@ class IsolatedCadSession:
def __exit__(self, error_type, error, traceback):
closing_started = time.monotonic()
cleanup_error = None
cleanup_errors = []
diagnostic_errors = []
failures = []
exit_codes = {}
closing = None
def remember(stage, failure):
cleanup_errors.append({'stage': stage, 'message': str(failure)})
message = '; '.join(f"{item['stage']}: {item['message']}" for item in cleanup_errors)
self._uncertain(message, cleanup_errors=cleanup_errors,
failures.append(failure)
def diagnose(stage, action):
try:
action()
except Exception as failure:
diagnostic_errors.append({'stage': stage, 'message': str(failure)})
failures.append(failure)
self.record['clean'] = False
def failure_message():
return '; '.join(f"{item['stage']}: {item['message']}"
for item in cleanup_errors + diagnostic_errors)
def persist_uncertain():
message = failure_message() or str(error)
self.record.update(state='uncertain', clean=False, error=message,
cleanup_errors=cleanup_errors, diagnostic_errors=diagnostic_errors,
shutdown_seconds=time.monotonic() - closing_started,
desktop_cleanup=getattr(self.desktop, 'cleanup_record', None), **exit_codes)
diagnose('uncertain_record', lambda: self._uncertain(message,
cleanup_errors=cleanup_errors, diagnostic_errors=diagnostic_errors,
clean=False, orderly_shutdown=self.record['orderly_shutdown'],
shutdown_seconds=time.monotonic() - closing_started,
desktop_cleanup=getattr(self.desktop, 'cleanup_record', None), **exit_codes))
self.record.update(clean=False, orderly_shutdown=False, forced_termination=False,
forced_root_termination=False, forced_descendant_pids=[], termination_reason=None,
cleanup_errors=cleanup_errors, diagnostic_errors=diagnostic_errors)
diagnose('closing_record', lambda: self._record(state='closing', clean=False,
cleanup_errors=cleanup_errors, diagnostic_errors=diagnostic_errors))
try:
self._record(state='closing', cleanup_errors=cleanup_errors)
if error is not None:
self._uncertain(str(error))
if self.cad is not None and self.cad.poll() is not None:
raise WorkerMonitorError('owned CAD exited before the authorized quit request')
if self.cad is not None and self.binding is not None:
closing = self.desktop.start_process(_helper_command('quit', self.root / 'private-desktop.json', self.cad.pid),
env=_child_environment(self.binding.environment()), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
quit_code = closing.wait(timeout=20)
exit_codes['quit_helper_exit_code'] = quit_code
if quit_code:
raise WorkerMonitorError('owned CAD refused orderly exit: ' + closing.stderr.read().decode('utf-8', errors='replace'))
cad_code = self.cad.wait(timeout=15)
exit_codes['cad_exit_code'] = cad_code
if cad_code != 0:
raise WorkerMonitorError('owned CAD returned a nonzero exit code after quit')
finally:
closing.close()
quit_code = closing.wait(timeout=20)
exit_codes['quit_helper_exit_code'] = quit_code
if quit_code:
raise WorkerMonitorError('owned CAD refused orderly exit: ' + closing.stderr.read().decode('utf-8', errors='replace'))
cad_code = self.cad.wait(timeout=15)
exit_codes['cad_exit_code'] = cad_code
if cad_code != 0:
raise WorkerMonitorError('owned CAD returned a nonzero exit code after quit')
self.record['orderly_shutdown'] = True
except Exception as failure:
cleanup_error = failure
remember('orderly_shutdown', failure)
finally:
if error is not None or failures:
# Evidence failure must never prevent the identity-checked Quit
# above or release of the handles we own below.
persist_uncertain()
if self.stop_control is not None and cleanup_errors and self.cad is not None:
cad_running = True
try:
cad_running = self.cad.poll() is None
except Exception as failure:
remember('cad_exit_check', failure)
if cad_running:
# A refused/timed-out Quit may still be saving or closing.
# Keep both CAD and its helper owned and alive until the
# interactive caller confirms force, including ordinary stop.
from .stop_control import wait_for_resolution
try:
wait_for_resolution(self.stop_control, self.root, failure_message())
except WorkerMonitorError as failure:
force_event = getattr(self.stop_control, 'force_event', None)
if force_event is None or not force_event.is_set():
raise
remember('forced_stop', failure)
if closing is not None:
try:
closing.close()
except Exception as failure:
remember('quit_helper_release', failure)
try:
self.desktop.close()
self._record(state='session_closed' if self.borrowed_desktop else 'released',
desktop_retained_by_batch=self.borrowed_desktop,
clean=error is None and cleanup_error is None,
shutdown_seconds=time.monotonic() - closing_started,
desktop_cleanup=getattr(self.desktop, 'cleanup_record', None), **exit_codes)
except Exception as failure:
cleanup_error = failure
remember('desktop_release', failure)
if cleanup_error is not None:
message = '; '.join(f"{item['stage']}: {item['message']}" for item in cleanup_errors)
raise WorkerMonitorError('private CAD cleanup requires reconciliation: ' + message) from cleanup_error
desktop_cleanup = getattr(self.desktop, 'cleanup_record', None) or {}
forced = [item for item in desktop_cleanup.get('processes', []) if item.get('forced_termination')]
self.record.update(forced_termination=bool(forced),
forced_root_termination=any(item.get('forced_root_termination') for item in forced),
termination_reason=next((item.get('termination_reason') for item in forced
if item.get('termination_reason')), None),
forced_descendant_pids=sorted({pid for item in forced for pid in item.get('forced_descendant_pids', [])}))
if forced:
remember('forced_cleanup', WorkerMonitorError('owned processes required forced termination'))
if error is not None or failures:
persist_uncertain()
else:
diagnose('released_record', lambda: self._record(
state='session_closed' if self.borrowed_desktop else 'released',
desktop_retained_by_batch=self.borrowed_desktop, clean=True,
shutdown_seconds=time.monotonic() - closing_started,
desktop_cleanup=desktop_cleanup, **exit_codes))
if failures:
persist_uncertain()
if failures:
raise WorkerMonitorError('private CAD cleanup requires reconciliation: ' + failure_message()) from failures[0]
def _uncertain(self, message, **details):
from .quick_run import _write_uncertain_flag
@@ -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
@@ -23,6 +24,7 @@ from .errors import TaskCenterError
DESKTOP_RELEASE_TIMEOUT_SECONDS = 5.0
DESCENDANT_EXIT_GRACE_SECONDS = 2.0
def _native_handle(handle):
@@ -124,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
@@ -147,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
@@ -192,7 +206,10 @@ def _stop_and_wait(process):
errors.append(error)
if running:
try:
process.terminate()
if isinstance(process, _Win32Process):
process.terminate(reason='root_process_active')
else:
process.terminate()
except Exception as error:
errors.append(error)
try:
@@ -313,7 +330,9 @@ class _Win32Process:
self.job_assigned = False
self.returncode = None
self.stdout = self.stderr = None
self.cleanup_record = {'pid': self.pid}
self.cleanup_record = {'pid': self.pid, 'forced_termination': False,
'forced_root_termination': False, 'forced_descendant_pids': [],
'termination_reason': None}
def poll(self):
if self.returncode is not None or self.handle is None:
@@ -340,34 +359,59 @@ class _Win32Process:
self.returncode = self._process_api.GetExitCodeProcess(self.handle)
return self.returncode
def terminate(self):
def terminate(self, *, reason='explicit_termination'):
if self.handle is None:
return
errors = []
root_active = True
try:
root_active = self.poll() is None
except Exception as error:
errors.append(error)
self.cleanup_record.setdefault('root_exit_before_cleanup', not root_active)
if not self.job_assigned:
self._process_api.TerminateProcess(self.handle, 1)
self._record_termination(reason, root_active, ())
_raise_cleanup_errors(errors)
return
errors = []
descendants = set()
try:
self._remember_descendants()
# Keep confirmed members of this snapshot even if opening a later
# member fails; the Job termination below still includes them.
self._remember_descendants(active=descendants)
except Exception as error:
errors.append(error)
try:
self._job_api.TerminateJobObject(self.job, 1)
self._record_termination(reason, root_active, descendants)
except Exception:
self._process_api.TerminateProcess(self.handle, 1)
self._record_termination(reason, root_active, ())
_raise_cleanup_errors(errors)
kill = terminate
def _remember_descendants(self):
def _record_termination(self, reason, root_active, descendants):
self.cleanup_record.update(forced_termination=True,
forced_root_termination=self.cleanup_record['forced_root_termination'] or root_active,
forced_descendant_pids=sorted(set(self.cleanup_record['forced_descendant_pids']) | set(descendants)),
termination_reason=self.cleanup_record['termination_reason'] or reason)
def _remember_descendants(self, *, active=None):
if active is None:
active = set()
if self._open_process is None or self.job is None:
return
return active
for pid in self._job_api.QueryInformationJobObject(self.job, self._job_api.JobObjectBasicProcessIdList):
if pid == self.pid or pid in self._descendant_pids:
if pid == self.pid:
continue
if pid in self._descendant_pids:
active.add(pid)
continue
try:
self._descendant_handles.append(self._open_process(0x100000, False, pid))
self._descendant_pids.add(pid)
active.add(pid)
except Exception as error:
# pywintypes.error is not an OSError. A child that exited
# between the job snapshot and OpenProcess reports error 87.
@@ -376,22 +420,47 @@ class _Win32Process:
code = error.args[0]
if code != 87:
raise
return active
def close(self):
errors = []
job_empty = not self.job_assigned or self.job is None
if self.handle is not None:
if 'root_exit_before_cleanup' not in self.cleanup_record:
try:
self.cleanup_record['root_exit_before_cleanup'] = self.poll() is not None
except Exception as error:
errors.append(error)
errors.extend(_stop_and_wait(self))
deadline = time.monotonic() + 5.0
if self.job is not None and self.job_assigned:
try:
# A completed root can leave grandchildren holding the desktop.
self._remember_descendants()
self._job_api.TerminateJobObject(self.job, 1)
grace_started = time.monotonic()
grace_deadline = grace_started + DESCENDANT_EXIT_GRACE_SECONDS
# A completed root may leave children finishing normal shutdown.
# An empty job takes no grace delay; an active root was already
# stopped above and does not receive this normal-exit allowance.
while True:
active = self._job_api.QueryInformationJobObject(
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
self.cleanup_record['job_active_processes'] = active
if not active:
job_empty = True
break
self._remember_descendants()
if (self.cleanup_record.get('root_exit_before_cleanup') is not True
or self.cleanup_record['forced_termination'] or time.monotonic() >= grace_deadline):
self.terminate(reason='descendant_grace_expired')
break
time.sleep(min(0.01, max(0, grace_deadline - time.monotonic())))
self.cleanup_record['descendant_grace_seconds'] = time.monotonic() - grace_started
deadline = time.monotonic() + 5.0
while True:
active = self._job_api.QueryInformationJobObject(
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
self.cleanup_record['job_active_processes'] = active
if not active:
job_empty = True
break
if time.monotonic() >= deadline:
raise TimeoutError('owned job descendants did not exit within 5 seconds')
@@ -417,6 +486,10 @@ class _Win32Process:
self._descendant_handles = retained
for name in ('job', 'thread', 'handle'):
handle = getattr(self, name)
if name in ('job', 'handle') and not job_empty:
# Closing a KILL_ON_JOB_CLOSE job would silently force remaining
# children and discard the capability needed for cleanup retry.
continue
if name == 'handle' and self.returncode is None:
# Keep the only termination/wait capability until cleanup can retry.
continue
@@ -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:
@@ -111,6 +111,22 @@ class ProjectSessionWorker:
return
if not self._done.is_set():
_write_control(self.root / 'project-session-next.tsv', f'{self.token}\tclose')
from .stop_control import StopControl
interactive = isinstance(getattr(self.runtime, 'stop_event', None), StopControl)
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):
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()
@@ -925,6 +925,9 @@ class TaskCenterService:
message = batch[2] if batch else '正在准备执行'
value = read_feedback(task_id, position, total, message,
child._quick_run_preparer.data_root, started_after)
if getattr(self, '_supervisor_waiting', False):
from dataclasses import replace
value = replace(value, stage='awaiting_reconciliation', waiting_for_safe_completion=True)
if read_control is not None:
read_control.checkpoint()
return value
@@ -951,12 +954,12 @@ class TaskCenterService:
return delegated.has_interrupted_quick_run(task_id)
return self._quick_run_preparer.has_interrupted(task_id)
def recover_quick_run(self, task_id: str, *, confirmed: bool = False, runtime=None) -> QuickRunStepResult:
def recover_quick_run(self, task_id: str, *, confirmed: bool = False, runtime=None, stop_event=None) -> QuickRunStepResult:
if runtime is None and self._supervised:
return self._supervise('recover_quick_run', {'task_id': task_id, 'confirmed': confirmed})
return self._supervise('recover_quick_run', {'task_id': task_id, 'confirmed': confirmed}, stop_event=stop_event)
if runtime is None:
return self._owned_cad_operation('recover_quick_run', lambda owned:
self.recover_quick_run(task_id, confirmed=confirmed, runtime=owned), task_id=task_id)
self.recover_quick_run(task_id, confirmed=confirmed, runtime=owned), stop_event, task_id=task_id)
delegated = self._project_task_service(task_id)
if delegated is not self:
return delegated.recover_quick_run(task_id, confirmed=confirmed, runtime=runtime)
@@ -0,0 +1,81 @@
"""Separate ordinary stop intent from an explicitly confirmed forced interruption."""
from threading import Event
import json
import os
from .path_access import internal_path
class StopControl(Event):
def __init__(self):
super().__init__()
self.force_event = Event()
self.control_error = ''
self.waiting_for_resolution = False
self.resolution_error = ''
def request_force(self):
self.set()
self.control_error = ''
self.force_event.set()
def protect_interruption(root, reason):
"""Create or re-sync a marker without overwriting prior protection evidence."""
root = internal_path(root)
root.mkdir(parents=True, exist_ok=True)
marker = root / 'uncertain.flag'
try:
stream = marker.open('x', encoding='utf-8')
except FileExistsError:
# 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:
stream.write(str(reason))
stream.flush()
os.fsync(stream.fileno())
def wait_for_resolution(control, root, error):
"""Interactive owners retain a possibly changed project until explicit force.
The GUI-side supervisor can also terminate its own Job after durable force
evidence. Ordinary cancellation never releases this wait.
"""
if not isinstance(control, StopControl):
return
from .worker_monitor import WorkerMonitorError, _publish_progress
root = internal_path(root)
control.set()
control.waiting_for_resolution = True
control.resolution_error = str(error)
def protect():
protect_interruption(root, 'operation_interrupted: ' + str(error))
try:
protect()
except OSError as failure:
control.control_error = '保护记录写入失败,保留应用等待处理:' + str(failure)
try:
record = json.loads((root / 'worker-progress.json').read_text(encoding='utf-8'))
if not isinstance(record, dict):
record = {}
except (OSError, ValueError):
record = {}
record.update(stage='awaiting_reconciliation', waiting_for_safe_completion=True,
resolution_error=str(error), stop_requested=True, safe_stop=False)
_publish_progress(root / 'worker-progress.json', record)
while True:
if control.force_event.wait(.1):
try:
protect()
except OSError as failure:
control.force_event.clear()
control.control_error = '强制结束未执行,保护记录无法保存:' + str(failure)
continue
raise WorkerMonitorError('user forced stop of uncertain operation; reconciliation required')
@@ -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 = '后台进程结果损坏或身份校验不通过'
@@ -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
@@ -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()
@@ -15,6 +15,7 @@ STAGES = {
'restore_attribute_fields': '恢复属性字段', 'restore_attributes': '恢复属性',
'verify_restoration': '核验恢复结果', 'verify_attribute_fields': '核验属性字段',
'save_and_close': '保存并关闭工程', 'verify_saved_project': '回读已保存工程',
'awaiting_reconciliation': '当前结果待核验,已保留工程现场',
}
@@ -22,4 +23,6 @@ def format_feedback(value, task_name, elapsed, stopping):
stage = STAGES.get(value.stage, '')
details = f'{stage}({value.stage_seconds:.1f} 秒)' if stage else value.message
prefix = '等待当前步骤保存核验后暂停 · ' if stopping else ''
if value.waiting_for_safe_completion:
prefix = '当前保存或核验尚未确认,未自动结束应用;可继续等待、导出诊断,或确认强制结束后核验 · '
return f'{prefix}任务 {value.position}/{value.total}:{task_name} · {details} · 总计 {elapsed} 秒'
@@ -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
@@ -1,6 +1,7 @@
"""Single-project task presentation; persistence and CAD remain in the service."""
from __future__ import annotations
from threading import Event
from ..stop_control import StopControl
from shiboken6 import isValid
import time
from PySide6.QtCore import QElapsedTimer, QSettings, QSignalBlocker, QThreadPool, QTimer, Qt
@@ -10,7 +11,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
@@ -20,6 +21,7 @@ from .styles import APP_STYLESHEET
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
@@ -39,6 +41,7 @@ class TrialTaskCenterWindow(QMainWindow):
self._tasks = ()
self._known_task_groups = {}
self._workers = set()
self._diagnostic_worker = None
self._closed = False
self._batch_stop = None
self._active_quick_run_task_id = None
@@ -50,6 +53,7 @@ class TrialTaskCenterWindow(QMainWindow):
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)
@@ -88,6 +92,7 @@ class TrialTaskCenterWindow(QMainWindow):
self._log_presenter = TaskLogPresenter(service, self.run_panel, self)
self.run_panel.quick_run_button.clicked.connect(self._prepare_quick_run)
self.run_panel.stop_queue_button.clicked.connect(self._pause_queue)
self.run_panel.force_stop_button.clicked.connect(self._force_batch_stop)
self.run_panel.retry_button.clicked.connect(self._retry_failed)
self.run_panel.recover_button.clicked.connect(self._recover_quick_run)
self.run_panel.cleanup_resources_button.clicked.connect(self._cleanup_batch_resources)
@@ -113,6 +118,8 @@ class TrialTaskCenterWindow(QMainWindow):
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 getattr(service, '_supervised', False):
@@ -245,6 +252,7 @@ class TrialTaskCenterWindow(QMainWindow):
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()
super().closeEvent(event)
@@ -318,9 +326,9 @@ class TrialTaskCenterWindow(QMainWindow):
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)
group_unsaved = self._group_has_unsaved_plans() if state.editable or state.new_round else False
context = self._context
@@ -337,6 +345,10 @@ class TrialTaskCenterWindow(QMainWindow):
self.run_panel.history_button.setEnabled(bool(context and not busy))
self.run_panel.stop_queue_button.setVisible(self._batch_stop is not None)
self.run_panel.stop_queue_button.setEnabled(bool(self._batch_stop and not self._batch_stop.is_set()))
self.run_panel.force_stop_button.setVisible(self._batch_stop is not None)
self.run_panel.force_stop_button.setEnabled(bool(self._batch_stop and
not self._batch_stop.force_event.is_set()))
self.run_panel.diagnostics_button.setEnabled(self._diagnostic_worker is None)
self.run_panel.copy_value_check.setEnabled(state.editable)
rule_editable = state.editable and not group_unsaved
self.mapping_table.set_rules_editable(rule_editable)
@@ -370,7 +382,9 @@ class TrialTaskCenterWindow(QMainWindow):
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'
@@ -381,7 +395,9 @@ class TrialTaskCenterWindow(QMainWindow):
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):
@@ -550,8 +566,7 @@ class TrialTaskCenterWindow(QMainWindow):
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)))
@@ -571,6 +586,8 @@ class TrialTaskCenterWindow(QMainWindow):
self._finish_run()
self._metadata_changed(None)
failures = sum(e.state in {'failed', 'blocked'} for e in result.entries)
if failures:
self._close_pending = False
self._show_task_log(force=True)
self._set_operator_message(f'本批任务已暂停。失败/阻断 {failures} 项。' if result.paused else f'本批任务已结束。失败 {failures} 项。',
'warning' if result.paused or failures else 'success')
@@ -578,7 +595,7 @@ class TrialTaskCenterWindow(QMainWindow):
self.task_list.queue_button)
def _begin_run(self, task_id):
self._batch_stop = Event()
self._batch_stop = StopControl()
self._active_quick_run_task_id = task_id
self._run_started_at = time.time()
self._quick_run_elapsed.start()
@@ -601,6 +618,24 @@ class TrialTaskCenterWindow(QMainWindow):
self._set_operator_message('已请求暂停;当前项安全收尾后停止。', 'warning')
self._update_gates()
def _force_batch_stop(self):
control = self._batch_stop
if control is None or control.force_event.is_set():
return
if QMessageBox.question(self, '强制结束当前操作',
'当前操作可能正在修改或保存工程。强制结束可能留下未保存或部分保存的结果。\n\n'
'已完成的执行证据会保留;中断后必须先核验恢复,不能直接重复替换。'
'建议先导出诊断包。确定强制结束本程序当前操作吗?',
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No) != QMessageBox.StandardButton.Yes:
return
if self._batch_stop is not control:
return # Completion/new run while the confirmation dialog was open.
self._close_pending = False
control.request_force()
self._set_operator_message('已请求强制结束;正在记录中断证据,结果将进入待核验。', 'warning')
self._update_gates()
def _show_batch_result(self, result):
owner = self._active_quick_run_task_id or self._task_id
name = next((t.name for t in self._tasks if t.task_id == owner), owner)
@@ -609,6 +644,8 @@ class TrialTaskCenterWindow(QMainWindow):
if owner == self._task_id:
self._show_task_log(force=True)
p = result.progress
if p.failed_items:
self._close_pending = False
self._set_operator_message(f"任务「{name}」{'已暂停' if result.paused else '已结束'}:已保存 {p.saved_items},失败 {p.failed_items},未命中 {p.no_match_items}。",
'warning' if result.paused or p.failed_items else 'success')
@@ -626,10 +663,10 @@ class TrialTaskCenterWindow(QMainWindow):
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.
@@ -637,10 +674,31 @@ class TrialTaskCenterWindow(QMainWindow):
from pathlib import Path
name = 'DxPartReplace-诊断-' + time.strftime('%Y%m%d-%H%M%S') + '.zip'
value, _ = QFileDialog.getSaveFileName(self, '导出脱敏诊断包', name, 'ZIP 文件 (*.zip)')
if value:
self._run_async(lambda: self.service.export_diagnostics(Path(value)),
lambda path: self._set_operator_message(f'脱敏诊断包已导出:{path}', 'success'),
self.run_panel.diagnostics_button)
if not value or self._diagnostic_worker is not None:
return
# A read-only support export remains available while CAD is waiting.
worker = ServiceWorker(lambda: self.service.export_diagnostics(Path(value)))
self._diagnostic_worker = worker
self._workers.add(worker)
def exported(path):
if not self._closed:
self._set_operator_message(f'脱敏诊断包已导出:{path}', 'success')
def failed(error):
self._close_pending = False
if not self._closed:
self._show_error(error)
def finished():
self._workers.discard(worker)
self._diagnostic_worker = None
if not self._closed:
self._update_gates()
if self._close_pending and not self._workers:
QTimer.singleShot(0, self.close)
worker.signals.succeeded.connect(exported)
worker.signals.failed.connect(failed)
worker.signals.finished.connect(finished)
self._update_gates()
QThreadPool.globalInstance().start(worker)
def _retry_failed(self):
if self.run_panel.retry_button.isEnabled():
@@ -700,14 +758,14 @@ class TrialTaskCenterWindow(QMainWindow):
self._finish_run()
self._metadata_changed(None)
self._set_operator_message(result.message)
self._run_async(lambda: self.service.recover_quick_run(task_id, confirmed=True), done,
stop = self._batch_stop
self._run_async(lambda: self.service.recover_quick_run(task_id, confirmed=True, stop_event=stop), done,
self.run_panel.recover_button, expected_task_id=task_id)
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 会话中打开。'
@@ -725,6 +783,9 @@ class TrialTaskCenterWindow(QMainWindow):
self.run_panel.execution_status.setText(format_feedback(
value, name, self._quick_run_elapsed.elapsed() // 1000,
bool(self._batch_stop and self._batch_stop.is_set())))
if self._batch_stop and self._batch_stop.control_error:
self._set_operator_message(self._batch_stop.control_error, 'warning')
self._update_gates()
self._feedback_query.request(lambda control: self.service.execution_feedback(task_id, started_after=started, read_control=control),
received, lambda _: None, cancellable=True)
if self.run_panel.toggle_log_button.isChecked():
@@ -799,6 +860,12 @@ class TrialTaskCenterWindow(QMainWindow):
callback(result)
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):
@@ -820,6 +887,7 @@ class TrialTaskCenterWindow(QMainWindow):
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:
@@ -4,6 +4,8 @@ import json
import re
ERROR_TEXT = {
'user_forced_stop': '操作已强制中断,结果待核验。',
'application_shutdown_failed': '业务结果已保留,但应用收尾异常。',
"batch_resources_pending": "后台资源尚未确认释放,请使用“重新清理资源”后再开始下一批。",
"supervisor_busy": "另一批操作正在执行,请先安全收尾。",
"supervisor_interrupted": "批次监督连接异常,保护记录已保留,没有自动重放替换。",
@@ -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()
@@ -23,6 +23,8 @@ class RunPanel(QWidget):
self.busy_indicator.hide()
self.quick_run_button = QPushButton('执行当前任务', objectName='quickRunButton')
self.stop_queue_button = QPushButton('完成当前项后暂停', objectName='stopQueueButton')
self.force_stop_button = QPushButton('强制结束…', objectName='forceStopButton')
self.force_stop_button.setToolTip('异常情况下使用;会中断当前操作,结果必须重新核验。')
self.retry_button = QPushButton('重试失败项', objectName='retryTaskItems')
self.recover_button = QPushButton('核验并恢复', objectName='recoverQuickRunButton')
self.cleanup_resources_button = QPushButton('重新清理资源', objectName='cleanupBatchResources')
@@ -42,7 +44,7 @@ class RunPanel(QWidget):
self.toggle_log_button.toggled.connect(self._toggle_log)
buttons = QHBoxLayout()
for button in (self.quick_run_button, self.retry_button, self.recover_button, self.cleanup_resources_button, self.diagnostics_button,
self.new_round_button, self.history_button, self.stop_queue_button):
self.new_round_button, self.history_button, self.stop_queue_button, self.force_stop_button):
buttons.addWidget(button)
buttons.addStretch()
buttons.addWidget(self.toggle_log_button)
@@ -1 +1 @@
VERSION = "1.1.4-beta.3"
VERSION = "1.1.4-beta.4"
@@ -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:
@@ -35,6 +38,7 @@ class ProgressWatch:
self.done = 0
self.total = 0
self.safe_stop = True
self.waiting_for_safe_completion = False
self.started = started
self.stage_started = started
self.stage_seconds: dict[str, float] = {}
@@ -67,6 +71,7 @@ class ProgressWatch:
self.native_sequence = 0
self.last_progress = now
self.stage, self.done, self.total, self.safe_stop = fields[4], done, total, bool(safe)
self.waiting_for_safe_completion = False
return True
def accept_native(self, line: bytes, now: float) -> bool:
@@ -102,6 +107,11 @@ class ProgressWatch:
self.stop_requested = True
return 'request_stop'
if self.stop_requested and idle >= self.idle_timeout * 2:
if not self.safe_stop:
if not self.waiting_for_safe_completion:
self.waiting_for_safe_completion = True
return 'await_safe_completion'
return None
return 'unresponsive'
return None
@@ -109,7 +119,7 @@ class ProgressWatch:
now = time.monotonic() if now is None else now
stages = dict(self.stage_seconds)
stages[self.stage] = stages.get(self.stage, 0.0) + max(0.0, now - self.stage_started)
return {name: getattr(self, name) for name in ('token', 'sequence', 'stage', 'done', 'total', 'safe_stop', 'stop_requested')} | {
return {name: getattr(self, name) for name in ('token', 'sequence', 'stage', 'done', 'total', 'safe_stop', 'stop_requested', 'waiting_for_safe_completion')} | {
'job_id': self.current_job_id,
'native_progress_frames': self.native_progress_frames,
'elapsed_seconds': max(0.0, now - self.started), 'stage_seconds': stages}
@@ -125,8 +135,14 @@ def _publish_progress(path: Path, snapshot: dict) -> None:
try:
_atomic_text(path, json.dumps(snapshot, ensure_ascii=False))
except OSError as error:
if not isinstance(error, PermissionError) and getattr(error, 'winerror', None) not in {5, 32, 33}:
raise
# 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:
@@ -190,7 +206,7 @@ class _OwnedWorkers:
def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abort_event=None,
process_factory=None, cooperative_abort=False) -> subprocess.CompletedProcess[str]:
process_factory=None, cooperative_abort=False, force_event=None, force_control=None) -> subprocess.CompletedProcess[str]:
root = Path(command[3]).resolve()
job_id = command[-1]
token = uuid4().hex
@@ -200,8 +216,11 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
native_progress_path = root / f'monitor-{token}.native-progress'
root.mkdir(parents=True, exist_ok=True)
watch = ProgressWatch(token, job_id, idle_timeout, time.monotonic())
if force_control is not None:
force_event = force_control.force_event
owned = _OwnedWorkers(token, {command[0], command[4]})
process = None
durable_force_accepted = False
threads = []
messages = queue.Queue(maxsize=1024)
stop_readers = threading.Event()
@@ -244,15 +263,32 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
_publish_progress(progress_path, watch.snapshot())
_atomic_text(ready, token)
while process.poll() is None:
if force_event is not None and force_event.is_set():
from .stop_control import protect_interruption
try:
protect_interruption(root, 'user_forced_stop')
except OSError as error:
force_event.clear()
if force_control is not None:
force_control.control_error = '强制结束未执行,保护记录无法保存:' + str(error)
watch.waiting_for_safe_completion = True
_publish_progress(progress_path, watch.snapshot())
else:
durable_force_accepted = True
raise WorkerMonitorError('user forced stop; execution requires reconciliation')
if abort_event is not None and abort_event.is_set():
if not cooperative_abort:
if not cooperative_abort and watch.safe_stop:
raise WorkerMonitorError('project session supervisor requested worker cleanup')
if not watch.stop_requested:
# Read-only callers request the VBS safe boundary first.
# The existing idle watchdog still bounds a stalled COM call.
watch.stop_requested = True
watch.last_progress = time.monotonic()
_atomic_text(stop, token)
try:
_atomic_text(stop, token)
except OSError:
if watch.safe_stop:
raise
_publish_progress(progress_path, watch.snapshot())
try:
receive(*messages.get(timeout=min(0.1, idle_timeout / 10)))
@@ -273,7 +309,17 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
break
action = watch.action(time.monotonic())
if action == 'request_stop':
_atomic_text(stop, token)
try:
_atomic_text(stop, token)
except OSError:
if watch.safe_stop:
raise
_publish_progress(progress_path, watch.snapshot())
elif action == 'await_safe_completion':
if cooperative_abort:
# This entry point is explicitly read-only. A blocked COM
# read has no write to finish and retains its bounded cancel.
raise subprocess.TimeoutExpired(command, idle_timeout * 2)
_publish_progress(progress_path, watch.snapshot())
elif action == 'unresponsive':
raise subprocess.TimeoutExpired(command, idle_timeout * 2)
@@ -287,6 +333,15 @@ def run_monitored_process(command: tuple[str, ...], idle_timeout: float, *, abor
return subprocess.CompletedProcess(command, process.returncode, _decode(output[0]), _decode(output[1]))
except Exception as error:
if process is not None:
if (not watch.safe_stop and not cooperative_abort and force_control is not None
and not durable_force_accepted):
from .stop_control import wait_for_resolution
try:
wait_for_resolution(force_control, root, error)
except WorkerMonitorError:
if not force_event.is_set():
raise
durable_force_accepted = True
failures = owned.stop_workers(process)
process.wait(timeout=10)
for thread in threads:
@@ -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):
@@ -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
@@ -175,13 +175,13 @@ Set QP_App=FakeApp
FakeApp.ProjectPath=Target
QP_CurrentJobId="batch-job": QP_ExpectedJobId="batch-job"
QPWriteUtf8 QP_Fso.BuildPath(QP_Root,"command.tsv"),"task_id" & vbTab & "job_id" & vbTab & "project_path" & vbTab & "old_part" & vbTab & "new_part" & vbTab & "value_mode" & vbCrLf & "task" & vbTab & "batch-job" & vbTab & Target & vbTab & "OLD" & vbTab & "NEW" & vbTab & "preserve" & vbCrLf
QP_BatchMode=True: QP_OwnedProject=True
QP_BatchMode=True: QP_OwnedProject=True: QP_OwnedProjectPath=Target
QPWriteStatus QP_Root,QP_CurrentJobId,"no_match","","not found"
Require Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"status.tsv")),"published success before close"
Require QPReadState(QP_Fso.BuildPath(QP_Root,"status.tsv"),QP_CurrentJobId)="no_match","pending no-match not visible to worker"
QPFinishBatchItem
Require FakeApp.CloseCount=1,"project not closed exactly once"
Require FakeApp.SaveCount=1,"sheet not closed with saving allowed"
Require FakeApp.SaveCount=0,"no-match must close without saving"
Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"status.tsv")),"batch_closed")>0,"no completion proof"
''')
@@ -368,8 +368,8 @@ Require FakeApp.ProjectPath = "", "discovery left the project open"
Require FakeApp.Documents.OpenCount = 0, "discovery opened a page"
Dim Manifest
Manifest = QPReadUtf8(QP_Fso.BuildPath(QP_Root, "sheets.tsv"))
Require InStr(Manifest, Target & vbTab & "Main" & vbTab & "1") > 0, "sheet one missing"
Require InStr(Manifest, Target & vbTab & "Main" & vbTab & "2") > 0, "sheet two missing"
Require InStr(Manifest, LCase(Target) & vbTab & "Main" & vbTab & "1") > 0, "sheet one missing"
Require InStr(Manifest, LCase(Target) & vbTab & "Main" & vbTab & "2") > 0, "sheet two missing"
''')
+47 -1
View File
@@ -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
@@ -253,6 +253,31 @@ End Sub''', routines)
'Sub QPVerifyUntouched(ByRef Records)\n' + injection)
def test_save_then_project_close_refusal_retains_save_boundary(tmp_path):
run_vbs(tmp_path, 'part_number', '''
Dim FullChecks
FullChecks=0
Command.Add "project_path",QPProjectPath()
QP_BatchMode=True: QP_SessionMode=True
QP_SessionToken=String(32,"b")
QP_OwnedProject=True: QP_OwnedProjectPath=QPProjectPath()
QPRestoreAfterOperatorReplace Command,SnapshotPath
QP_App.RefuseClose=True
On Error Resume Next
QPFinishBatchItem
Failure=Err.Number
Err.Clear
On Error GoTo 0
Require Failure<>0,"refused close must stop the operation"
Require QP_BatchSaveCommitted,"returned save was lost when project close failed"
QP_App.RefuseClose=False
QPDiscardFailedBatchProject CloseError
Require Len(CloseError)>0,"uncertain save must not discard the open project"
Require Len(QPProjectPath())>0,"failed saved project was silently discarded"
Require Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"project-session-result.tsv")),"failure published success"
''', routines_transform=_batch_readback_fixture)
@pytest.mark.parametrize('fault', ['', 'before_save', 'after_reopen'])
def test_owned_batch_checks_full_state_at_save_and_disk_boundaries(tmp_path, fault):
run_vbs(tmp_path, 'part_number', '''
@@ -346,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
+1 -1
View File
@@ -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)
@@ -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():
@@ -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()
+149 -1
View File
@@ -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
+63
View File
@@ -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)
+108 -1
View File
@@ -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'
+117
View File
@@ -1,10 +1,12 @@
import io
import json
import os
import subprocess
import weakref
from pathlib import Path
from threading import Event
from threading import Timer
from threading import Thread
from types import SimpleNamespace
from traceback import extract_tb
@@ -194,6 +196,121 @@ def test_clean_release_records_helper_and_cad_exit_codes(monkeypatch, tmp_path):
assert record['cleanup_errors'] == []
@pytest.mark.parametrize('persistent', [False, True])
def test_closing_journal_failure_still_quits_and_reports_diagnostic_error(monkeypatch, tmp_path, persistent):
events, cad = fake_desktop(monkeypatch, tmp_path)
session = runtime.IsolatedCadSession(tmp_path)
session.__enter__()
record = session._record
def failing_record(**values):
if persistent or values.get('state') == 'closing':
raise PermissionError('closing journal locked')
record(**values)
session._record = failing_record
with pytest.raises(WorkerMonitorError, match='closing journal locked'):
session.__exit__(None, None, None)
assert 'quit' in events and cad.returncode == 0
assert session.record['orderly_shutdown'] is True
assert session.record['clean'] is False
assert session.record['diagnostic_errors'][0]['stage'] == 'closing_record'
assert (tmp_path / 'uncertain.flag').exists()
@pytest.mark.parametrize('helper_failure', ['refused', 'timeout'])
def test_interactive_quit_failure_retains_cad_and_helper_until_explicit_force(monkeypatch, tmp_path, helper_failure):
from dx_productivity.trial_task_center.stop_control import StopControl
from dx_productivity.trial_task_center import worker_monitor
events, cad = fake_desktop(monkeypatch, tmp_path, quit_code=1, cad_exit_code=None)
control = StopControl()
session = runtime.IsolatedCadSession(tmp_path, stop_control=control)
session.__enter__()
start = session.desktop.start_process
def start_quit(command, **options):
helper = start(command, **options)
if command[0] == 'quit':
if helper_failure == 'timeout':
helper.wait = lambda timeout: (_ for _ in ()).throw(subprocess.TimeoutExpired('quit', timeout))
helper.poll = lambda: None
helper.close = lambda: events.append('close-quit-helper')
return helper
session.desktop.start_process = start_quit
waiting, finished = Event(), Event()
publish = worker_monitor._publish_progress
def observe_wait(path, snapshot):
publish(path, snapshot)
if snapshot.get('stage') == 'awaiting_reconciliation':
waiting.set()
monkeypatch.setattr(worker_monitor, '_publish_progress', observe_wait)
failures = []
def close_session():
try:
session.__exit__(None, None, None)
except Exception as failure:
failures.append(failure)
finally:
finished.set()
thread = Thread(target=close_session)
thread.start()
try:
assert waiting.wait(1), 'failed Quit must enter protected waiting before resource release'
control.set()
assert not finished.wait(.05)
assert cad.poll() is None
assert 'close-quit-helper' not in events
assert not any(isinstance(event, tuple) and event[0] == 'release' for event in events)
assert (tmp_path / 'uncertain.flag').exists()
control.request_force()
assert finished.wait(2)
assert 'close-quit-helper' in events and ('release', True) in events
assert cad.poll() == 1
assert isinstance(failures[0], WorkerMonitorError)
assert 'forced' in str(failures[0])
assert session.record['orderly_shutdown'] is False and session.record['clean'] is False
finally:
control.request_force()
thread.join(3)
@pytest.mark.parametrize('failure', ['cad_exited', 'diagnostic'])
def test_interactive_exit_does_not_hold_completed_cad_for_resolution(monkeypatch, tmp_path, failure):
from dx_productivity.trial_task_center.stop_control import StopControl
from dx_productivity.trial_task_center import stop_control
events, _cad = fake_desktop(monkeypatch, tmp_path, quit_code=1 if failure == 'cad_exited' else 0)
session = runtime.IsolatedCadSession(tmp_path, stop_control=StopControl())
session.__enter__()
monkeypatch.setattr(stop_control, 'wait_for_resolution', lambda *_args: pytest.fail('exited CAD must not wait'))
if failure == 'diagnostic':
record = session._record
def failed_journal(**values):
if values.get('state') == 'closing':
raise PermissionError('closing diagnostic unavailable')
record(**values)
session._record = failed_journal
with pytest.raises(WorkerMonitorError):
session.__exit__(None, None, None)
assert ('release', True) in events
def test_forced_descendant_cleanup_is_not_reported_as_clean_orderly_shutdown(monkeypatch, tmp_path):
fake_desktop(monkeypatch, tmp_path)
session = runtime.IsolatedCadSession(tmp_path)
session.__enter__()
release = session.desktop.close
def forced_release():
release()
session.desktop.cleanup_record = {'processes': [{'pid': 123, 'forced_termination': True,
'forced_root_termination': False, 'termination_reason': 'descendant_grace_expired',
'forced_descendant_pids': [456]}]}
session.desktop.close = forced_release
with pytest.raises(WorkerMonitorError, match='forced'):
session.__exit__(None, None, None)
record = json.loads((tmp_path / 'private-desktop.json').read_text())
assert record['clean'] is False and record['forced_termination'] is True
assert record['orderly_shutdown'] is True
assert record['forced_descendant_pids'] == [456]
assert record['termination_reason'] == 'descendant_grace_expired'
def test_exit_code_diagnostics_do_not_interrupt_the_normal_cad_wait(monkeypatch, tmp_path):
_, cad = fake_desktop(monkeypatch, tmp_path)
waited = []
+237
View File
@@ -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():
@@ -367,6 +459,99 @@ def test_descendant_handles_are_owned_before_terminating_the_job():
assert ('close', 'child') in events
@pytest.mark.parametrize('child_exit_after', [0.5, None])
def test_completed_root_gives_owned_descendants_bounded_natural_exit_grace(monkeypatch, child_exit_after):
from dx_productivity.trial_task_center import isolated_desktop as module
api, events, state, *_ = native_api()
clock = [0.0]
monkeypatch.setattr(module.time, 'monotonic', lambda: clock[0])
monkeypatch.setattr(module.time, 'sleep', lambda seconds: clock.__setitem__(0, clock[0] + seconds))
state['running'] = False
child = Handle('child', events)
child_running = lambda: not any(event[0] == 'terminate' for event in events) and (
child_exit_after is None or clock[0] < child_exit_after)
api.job.JobObjectBasicProcessIdList = 3
api.job.QueryInformationJobObject = lambda job, kind: (
{'BasicLimitInformation': {'LimitFlags': 0}} if kind == 9 else
([43] if child_running() else []) if kind == 3 else {'ActiveProcesses': int(child_running())})
api.open_process = lambda *_args: child
root_wait = api.event.WaitForSingleObject
api.event.WaitForSingleObject = lambda handle, timeout: (
258 if child_running() else 0) if handle is child else root_wait(handle, timeout)
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
process.close()
assert child.closed
if child_exit_after is not None:
assert child_exit_after <= clock[0] < 2
assert ('terminate',) not in events
assert process.cleanup_record['forced_termination'] is False
assert process.cleanup_record['forced_descendant_pids'] == []
else:
assert 2 <= clock[0] < 2.1
assert ('terminate',) in events
assert process.cleanup_record['forced_termination'] is True
assert process.cleanup_record['forced_root_termination'] is False
assert process.cleanup_record['forced_descendant_pids'] == [43]
assert process.cleanup_record['termination_reason'] == 'descendant_grace_expired'
def test_completed_root_without_descendants_has_no_grace_delay(monkeypatch):
from dx_productivity.trial_task_center import isolated_desktop as module
api, events, state, *_ = native_api()
state['running'] = False
monkeypatch.setattr(module.time, 'sleep', lambda seconds: pytest.fail('empty job must not wait'))
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
process.close()
assert ('terminate',) not in events
assert process.cleanup_record['forced_termination'] is False
def test_active_root_cleanup_is_forced_without_descendant_grace(monkeypatch):
from dx_productivity.trial_task_center import isolated_desktop as module
api, events, *_ = native_api()
monkeypatch.setattr(module.time, 'sleep', lambda seconds: pytest.fail('active root must be bounded immediately'))
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
process.close()
assert process.cleanup_record['forced_termination'] is True
assert process.cleanup_record['forced_root_termination'] is True
assert process.cleanup_record['termination_reason'] == 'root_process_active'
def test_native_poll_error_still_terminates_owned_root_and_records_force():
api, events, state, *_ = native_api()
wait = api.event.WaitForSingleObject
def failing_poll(handle, timeout):
if timeout == 0 and state['running']:
raise OSError('native poll failed')
return wait(handle, timeout)
api.event.WaitForSingleObject = failing_poll
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
with pytest.raises(TaskCenterError, match='native poll failed'):
process.close()
assert state['running'] is False
assert process.cleanup_record['forced_root_termination'] is True
def test_failed_job_cleanup_retains_ownership_until_retry(monkeypatch):
from dx_productivity.trial_task_center import isolated_desktop as module
api, events, state, root, job = native_api()
clock = [0.0]
monkeypatch.setattr(module.time, 'monotonic', lambda: clock[0])
monkeypatch.setattr(module.time, 'sleep', lambda seconds: clock.__setitem__(0, clock[0] + seconds))
api.job.TerminateJobObject = lambda *_args: (_ for _ in ()).throw(OSError('job termination failed'))
query = api.job.QueryInformationJobObject
child_running = [True]
api.job.QueryInformationJobObject = lambda handle, kind: (
{'ActiveProcesses': int(state['running']) + int(child_running[0])} if kind == 1 else query(handle, kind))
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
with pytest.raises(TaskCenterError, match='descendants did not exit'):
process.close()
assert not job.closed and process.job is job
child_running[0] = False
process.close()
assert job.closed
@pytest.mark.parametrize('error_code', [87, 5])
def test_native_descendant_open_error_only_ignores_already_exited_process(error_code):
# pywintypes.error derives directly from Exception, rather than OSError.
@@ -391,6 +576,30 @@ def test_native_descendant_open_error_only_ignores_already_exited_process(error_
assert caught.value is error
def test_forced_job_preserves_partial_current_descendant_snapshot_after_open_failure():
api, events, state, *_ = native_api()
api.job.JobObjectBasicProcessIdList = 3
query = api.job.QueryInformationJobObject
snapshot = [45]
api.job.QueryInformationJobObject = lambda job, kind: list(snapshot) if kind == 3 else query(job, kind)
def open_child(_access, _inherit, pid):
if pid == 44:
raise OSError(5, 'child access denied')
return Handle('child-' + str(pid), events)
api.open_process = open_child
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
# PID 45 was observed earlier but is no longer part of the active Job.
process._remember_descendants()
snapshot[:] = [42, 43, 44]
with pytest.raises(TaskCenterError, match='child access denied'):
process.close()
assert state['running'] is False
assert process.cleanup_record['forced_termination'] is True
assert process.cleanup_record['forced_descendant_pids'] == [43]
assert process.cleanup_record['descendant_pids'] == [43, 45]
assert process.cleanup_record['job_handle_closed'] is True
def test_unconfirmed_descendant_exit_retains_handle_for_cleanup_retry():
api, events, state, *_ = native_api()
process = api.create_process(('python.exe',), 'winsta0\\private', None, None)
@@ -430,9 +639,37 @@ def test_native_private_job_kills_descendant_after_root_exit(parent_exits):
if parent_exits:
assert process.wait(timeout=5) == 0
assert win32event.WaitForSingleObject(child_handle, 0) == 0
assert process.cleanup_record['forced_termination'] is True
assert process.cleanup_record['forced_root_termination'] is (not parent_exits)
assert child_pid in process.cleanup_record['forced_descendant_pids']
with pytest.raises(pywintypes.error):
unexpected = win32service.OpenDesktop(name, 0, False, 1)
unexpected.CloseDesktop()
finally:
if child_handle is not None:
child_handle.Close()
@pytest.mark.skipif(os.name != 'nt', reason='native Windows descendant shutdown grace')
def test_native_private_job_allows_descendant_to_finish_after_root_exit():
import win32api
import win32event
import win32process
child_handle = None
try:
with PrivateDesktop('dx-test-' + uuid4().hex, enabled=True) as desktop:
process = desktop.start_process((sys.executable, '-c',
"import subprocess,sys; p=subprocess.Popen([sys.executable,'-c','import time;time.sleep(0.5)']); print(p.pid,flush=True)"),
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
child_pid = int(process.stdout.readline())
child_handle = win32api.OpenProcess(0x100000 | 0x1000, False, child_pid)
assert process.wait(timeout=5) == 0
assert win32event.WaitForSingleObject(child_handle, 0) == 0
assert win32process.GetExitCodeProcess(child_handle) == 0
assert process.cleanup_record['forced_termination'] is False
assert process.cleanup_record['forced_descendant_pids'] == []
assert desktop.cleanup_record['desktop_confirmation']['released'] is True
finally:
if child_handle is not None:
child_handle.Close()
+135
View File
@@ -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']
@@ -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)
+2
View File
@@ -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')
+103
View File
@@ -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()
+75
View File
@@ -0,0 +1,75 @@
from threading import Event
from PySide6.QtGui import QCloseEvent
from PySide6.QtWidgets import QMessageBox
from test_ui_remediation import window
from test_ui_batch_retry import open_project
def test_pause_and_window_close_never_request_force(window, qtbot, tmp_path):
open_project(window, qtbot, tmp_path)
window._begin_run(window._task_id)
worker = object()
window._workers.add(worker)
try:
event = QCloseEvent()
window.closeEvent(event)
assert not event.isAccepted()
assert window._batch_stop.is_set()
assert not window._batch_stop.force_event.is_set()
assert not window._closed
finally:
window._workers.discard(worker)
window._close_pending = False
window._finish_run()
def test_force_requires_confirmation_and_keeps_window_for_reconciliation(window, qtbot, tmp_path, monkeypatch):
open_project(window, qtbot, tmp_path)
window._begin_run(window._task_id)
control = window._batch_stop
try:
monkeypatch.setattr(QMessageBox, 'question', lambda *args: QMessageBox.StandardButton.No)
window._force_batch_stop()
assert not control.is_set() and not control.force_event.is_set()
monkeypatch.setattr(QMessageBox, 'question', lambda *args: QMessageBox.StandardButton.Yes)
window._close_pending = True
window._force_batch_stop()
assert control.is_set() and control.force_event.is_set()
assert not window._close_pending
finally:
window._finish_run()
def test_uncertain_wait_is_visible_in_execution_feedback(tmp_path):
import json
from dx_productivity.trial_task_center.execution_feedback import read_feedback
from dx_productivity.trial_task_center.ui.execution_feedback import format_feedback
root = tmp_path / 'quick-run'
root.mkdir()
(root / 'worker-progress.json').write_text(json.dumps({
'stage': 'save_and_close', 'waiting_for_safe_completion': True, 'stage_seconds': {}}))
feedback = read_feedback('task', 1, 1, '', tmp_path, 0)
text = format_feedback(feedback, 'board', 400, True)
assert '尚未确认' in text and '强制' in text
def test_diagnostic_export_during_wait_does_not_finish_or_cancel_execution(window, qtbot, tmp_path, monkeypatch):
from PySide6.QtWidgets import QFileDialog
open_project(window, qtbot, tmp_path)
window._begin_run(window._task_id)
control = window._batch_stop
owner = object()
window._workers.add(owner)
destination = tmp_path / 'support.zip'
monkeypatch.setattr(QFileDialog, 'getSaveFileName', lambda *args: (str(destination), ''))
try:
window._export_diagnostics()
qtbot.waitUntil(lambda: window._diagnostic_worker is None, timeout=5000)
assert destination.is_file()
assert owner in window._workers and window._batch_stop is control
assert not control.is_set() and not control.force_event.is_set()
finally:
window._workers.discard(owner)
window._finish_run()
+430
View File
@@ -0,0 +1,430 @@
import json
from pathlib import Path
import subprocess
import sys
from threading import Event, Thread
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)
assert watch.action(10) == 'request_stop'
assert watch.action(20) == 'await_safe_completion'
assert watch.snapshot(now=20)['waiting_for_safe_completion'] is True
assert watch.action(200) != 'unresponsive'
assert watch.accept(b'DX_PROGRESS\ttoken\tjob\t2\tverify_saved_project\t1\t1\t0\n', 201)
assert watch.snapshot(now=201)['waiting_for_safe_completion'] is False
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]
while not (root/f'monitor-{token}.ready').exists():time.sleep(.01)
print(f'DX_PROGRESS\\t{token}\\t{job}\\t1\\tsave_and_close\\t0\\t1\\t0',flush=True)
time.sleep(1.5)
(root/'saved.txt').write_text('saved and independently verified')
''', encoding='utf-8')
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, 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]
while not (root/f'monitor-{token}.ready').exists():time.sleep(.01)
print(f'DX_PROGRESS\\t{token}\\t{job}\\t1\\tnative_snapshot_read\\t0\\t1\\t0',flush=True)
time.sleep(1.5)
''', encoding='utf-8')
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, 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]
while not (root/f'monitor-{token}.ready').exists():time.sleep(.01)
print(f'DX_PROGRESS\\t{token}\\t{job}\\t1\\tsave_and_close\\t0\\t1\\t0',flush=True)
(root/'started').touch()
time.sleep(30)
(root/'unexpected_completed').touch()
''', encoding='utf-8')
force = Event()
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')
# 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):
from dx_productivity.trial_task_center.batch_supervisor import _finish_response
from dx_productivity.trial_task_center.batch_resources import write_record
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.supervisor_protocol import encode, encode_error
from dx_productivity.trial_task_center.errors import TaskCenterError
root = tmp_path
journal = {'operation': 'probe', 'batch_id': 'a' * 32, 'state': 'blocked'}
write_record(root / 'batch-resources' / journal['batch_id'] / 'session.json', {
'version': 1, 'session_id': 'a' * 32, 'kind': 'probe', 'state': 'released',
'generation': 1, 'desktop': 'dx-' + 'b' * 32, 'processes': [], 'cad_pid': None,
'cad_guid': None, 'checkpoints': {}, 'retries': {}, 'timings': [],
'cad_shutdown': {'clean': False, 'cleanup_errors': [{'stage': 'orderly_shutdown', 'message': 'Quit timed out'}]}})
response = {'business_succeeded': True, 'status': 'error', 'result': encode({'verified': True}),
'error': encode_error(TaskCenterError('batch_resources_pending', 'Quit timed out'))}
with pytest.raises(TaskCenterError) as caught:
_finish_response(root, journal, response, configure_logging(root))
assert caught.value.code == 'application_shutdown_failed'
assert caught.value.completed_result == {'verified': True}
assert journal['resource_recheck']['pending'] is False
def test_force_cannot_terminate_before_interruption_evidence_is_durable(tmp_path, monkeypatch):
from types import SimpleNamespace
from dx_productivity.trial_task_center import batch_supervisor as module
calls = []
process = SimpleNamespace(terminate=lambda: calls.append('terminate'))
journal = {'operation': 'execute_task_queue', 'batch_id': 'a' * 32, 'attempt_id': 'b' * 32,
'task_ids': ['task'], 'state': 'running'}
monkeypatch.setattr(module, '_mark_interrupted', lambda *args: (_ for _ in ()).throw(PermissionError('denied')))
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):
from types import SimpleNamespace
from dx_productivity.trial_task_center.batch_supervisor import _force_owned_supervisor
record = {}
def terminate():
record['forced_termination'] = True
raise OSError('child enumeration failed after force')
process = SimpleNamespace(terminate=terminate, cleanup_record=record)
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)
saved = json.loads((tmp_path / 'batch-resources' / ('a' * 32) / 'supervisor.json').read_text(encoding='utf-8'))
assert saved['stop_mode'] == 'forced'
assert 'enumeration' in saved['force_cleanup_error']
def test_interruption_targets_active_project_without_invalidating_previous_tasks(tmp_path):
from dx_productivity.trial_task_center.batch_supervisor import _mark_interrupted
from dx_productivity.trial_task_center.batch_resources import write_record
batch = 'a' * 32
write_record(tmp_path / 'batch-resources' / batch / 'session.json', {
'version': 1, 'session_id': batch, 'kind': 'task_queue', 'state': 'ready',
'generation': 1, 'desktop': 'dx-' + 'b' * 32, 'processes': [], 'cad_pid': 123,
'cad_guid': 'owned', 'checkpoints': {}, 'retries': {}, 'timings': [], 'active_task': 'current'})
journal = {'operation': 'execute_task_queue', 'batch_id': batch, 'attempt_id': 'c' * 32,
'task_ids': ['previous', 'current'], 'state': 'running'}
_mark_interrupted(tmp_path, journal, 'forced stop')
assert (tmp_path / 'tasks/current/quick-run/uncertain.flag').exists()
assert not (tmp_path / 'tasks/previous/quick-run/uncertain.flag').exists()
@pytest.mark.parametrize('interactive', [False, True])
def test_supervisor_only_holds_for_resolution_when_caller_has_force_control(tmp_path, monkeypatch, interactive):
import textwrap
from dx_productivity.trial_task_center import batch_supervisor as module
from dx_productivity.trial_task_center.stop_control import StopControl
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.logging_setup import configure_logging
bootstrap = textwrap.dedent('''
import sys,json
from pathlib import Path
from dx_productivity.trial_task_center import batch_worker,batch_runtime
from dx_productivity.trial_task_center.stop_control import StopControl
original=batch_runtime.BatchCadRuntime
def runtime(*args,**kwargs):
result=original(*args,**kwargs)
Path(sys.argv[1]).with_name('interactive-witness.json').write_text(json.dumps(isinstance(result.stop_event,StopControl)))
return result
batch_runtime.BatchCadRuntime=runtime
batch_worker.run(Path(sys.argv[1]))
''')
monkeypatch.setattr(module, '_command', lambda request: (sys.executable, '-c', bootstrap, str(request)))
store = TaskStore(tmp_path / 'tasks.db')
try:
service = TaskCenterService(store, configure_logging(tmp_path))
module.run_supervised(service, 'probe', {}, stop_event=StopControl() if interactive else Event())
witness = next((tmp_path / 'batch-resources').glob('*/interactive-witness.json'))
assert json.loads(witness.read_text()) is interactive
finally:
store.close()
def test_uncertain_operation_keeps_owner_until_explicit_force(tmp_path):
from dx_productivity.trial_task_center.stop_control import StopControl, wait_for_resolution
control = StopControl()
failures = []
def owner():
try:
wait_for_resolution(control, tmp_path, 'save result unknown')
except Exception as error:
failures.append(error)
thread = Thread(target=owner)
thread.start()
try:
for _ in range(100):
if (tmp_path / 'worker-progress.json').exists():
break
Event().wait(.01)
control.set() # Ordinary stop never authorizes discarding a changed project.
thread.join(.15)
assert thread.is_alive()
assert (tmp_path / 'uncertain.flag').exists()
assert json.loads((tmp_path / 'worker-progress.json').read_text())['waiting_for_safe_completion']
control.request_force()
thread.join(3)
assert not thread.is_alive()
assert 'forced' in str(failures[0])
finally:
control.request_force()
thread.join(3)
def test_failed_protection_fsync_is_retried_before_force_is_allowed(tmp_path, monkeypatch):
from dx_productivity.trial_task_center import stop_control as module
control = module.StopControl()
original = module.os.fsync
attempts, failures = [], []
def denied(fd):
attempts.append(fd)
raise OSError('evidence fsync denied')
monkeypatch.setattr(module.os, 'fsync', denied)
def owner():
try:
module.wait_for_resolution(control, tmp_path, 'save unknown')
except Exception as error:
failures.append(error)
thread = Thread(target=owner)
thread.start()
try:
for _ in range(100):
if control.control_error:
break
Event().wait(.01)
assert control.control_error
control.request_force()
# 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)
control.request_force()
thread.join(3)
assert failures and 'forced' in str(failures[0])
finally:
monkeypatch.setattr(module.os, 'fsync', original)
control.request_force()
thread.join(3)
@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
import win32api
import win32event
from dx_productivity.trial_task_center import batch_supervisor as module
from dx_productivity.trial_task_center.stop_control import StopControl
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.errors import TaskCenterError
store = TaskStore(tmp_path / 'tasks.db')
service = TaskCenterService(store, configure_logging(tmp_path), supervised=True)
task = store.create_task('force fixture')
evidence = service._project_task_service(task.task_id)._quick_run_preparer.data_root / 'quick-run'
witness = evidence / 'started'
bootstrap = textwrap.dedent('''
import sys,os,time,json
from pathlib import Path
from dx_productivity.trial_task_center import batch_worker
from dx_productivity.trial_task_center.service import TaskCenterService
def business(self, task_id, **kwargs):
root=self._project_task_service(task_id)._quick_run_preparer.data_root/'quick-run'
root.mkdir(parents=True,exist_ok=True)
(root/'started').write_text(str(os.getpid()))
request=json.loads(Path(sys.argv[1]).read_text(encoding='utf-8'))
print(json.dumps({'version':1,'batch_id':request['batch_id'],'attempt_id':request['attempt_id'],
'generation':1,'sequence':100,'type':'progress','payload':'fixture executing'}),flush=True)
time.sleep(30)
(root/'unexpected_completed').touch()
TaskCenterService.execute_quick_run=business
batch_worker.run(Path(sys.argv[1]))
''')
original_command = module._command
monkeypatch.setattr(module, '_command', lambda request:
(sys.executable, '-c', bootstrap, str(request))
if json.loads(request.read_text())['operation'] == 'execute_quick_run' else original_command(request))
original_mark = module._mark_interrupted
denied = []
def mark(*args):
if deny_first and not denied:
denied.append(True)
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():
try:
deadline = time.monotonic() + 15
while not witness.exists() and time.monotonic() < deadline:
time.sleep(.02)
assert witness.exists()
pid = int(witness.read_text())
if transport_failure:
while not control.control_error and time.monotonic() < deadline:
time.sleep(.02)
assert control.control_error
handle = win32api.OpenProcess(0x100000, False, pid)
try:
survived.append(win32event.WaitForSingleObject(handle, 0) == 258)
finally:
handle.Close()
control.request_force()
if deny_first:
while not control.control_error and time.monotonic() < deadline:
time.sleep(.02)
assert control.control_error
handle = win32api.OpenProcess(0x100000, False, pid)
try:
survived.append(win32event.WaitForSingleObject(handle, 0) == 258)
finally:
handle.Close()
control.request_force()
except Exception as error:
driver_errors.append(error)
thread = Thread(target=request)
thread.start()
try:
with pytest.raises(TaskCenterError) as caught:
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 == ('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()
if deny_first or transport_failure:
assert survived == [True]
finally:
thread.join(3)
store.close()
+107
View File
@@ -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
+74
View File
@@ -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'
+1 -1
View File
@@ -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())
+27
View File
@@ -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')
+60
View File
@@ -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'
+3 -2
View File
@@ -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()
+29
View File
@@ -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
+129
View File
@@ -0,0 +1,129 @@
"""Request ordinary stop during save on disposable projects; never replay a run."""
from dataclasses import asdict
import hashlib
import json
from pathlib import Path
import re
import shutil
import sys
from threading import Event, Thread
import time
from uuid import uuid4
REPO = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(REPO / 'src'), str(REPO / 'tools')]
from probe_batch_runtime_live import hashes, compare_saved, save, OLD, NEW, NAMES
from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.bom_plan import write_bom_template
from dx_productivity.trial_task_center.stop_control import StopControl
from dx_productivity.trial_task_center.task_queue import execute_task_queue
def main():
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--source-root', required=True, type=Path)
parser.add_argument('--library', required=True, type=Path)
parser.add_argument('--run-native', action='store_true', required=True)
args = parser.parse_args()
output = REPO / 'build' / ('safe-stop-live-' + uuid4().hex[:10])
output.mkdir()
source_root, library = args.source_root.resolve(strict=True), args.library.resolve(strict=True)
source_hashes = {name: hashes(source_root / name) for name in NAMES}
library_hash = hashlib.sha256(library.read_bytes()).hexdigest()
projects = []
for name in NAMES:
folder = output / 'projects' / name
shutil.copytree(source_root / name, folder)
project = folder / (name + '.prj')
text, count = re.subn(r'(?m)^KEY CentralLibrary .*$', lambda _: f'KEY CentralLibrary "{library.as_posix()}"',
project.read_text(encoding='utf-8-sig'))
assert count == 1
project.write_text(text, encoding='utf-8')
projects.append(project)
report = {'scope': 'source supervised service, production VBS and real CAD; no GUI or frozen EXE',
'output': str(output), 'projects': list(map(str, projects))}
print(json.dumps(report, ensure_ascii=False), flush=True)
store = TaskStore(output / 'tasks.db')
service = TaskCenterService(store, configure_logging(output / 'data'), supervised=True)
control, finished = StopControl(), Event()
watcher = None
def inspect(project, sheets, label):
evidence = output / (label + '-' + project.stem)
evidence.mkdir()
spec, result = evidence / 'spec.json', evidence / 'graph.json'
save(spec, {'project': str(project), 'sheets': sorted(sheets), 'destination': str(result)})
with IsolatedCadSession(evidence) as cad:
child = cad.start_worker((sys.executable, str(REPO / 'tools/probe_batch_runtime_live.py'),
'--repo', str(REPO), '--inspect-file', str(spec), '--run-native'), stdout=-1, stderr=-1)
try:
code = child.wait(timeout=180)
(evidence / 'worker-output.txt').write_bytes(child.stdout.read() + b'\n' + child.stderr.read())
assert code == 0, (evidence / 'worker-output.txt').read_text(errors='replace')
finally:
child.close()
return json.loads(result.read_text(encoding='utf-8'))
try:
tasks = service.create_project_tasks(output / 'projects', selected_paths=tuple(projects))
bom = write_bom_template(output / 'input.xlsx', ({'project_path': str(p), 'old_part_number': OLD,
'new_part_number': NEW, 'refdes': 'RV05', 'enabled': True, 'sync_nc': True} for p in projects))
imported = service.inspect_bom_batch(tasks[0].task_id, bom)
assert imported.passed_rows == 2 and imported.preview is not None
service.save_bom_batch(imported.preview, {p.task_id: p.rows for p in imported.preview.projects})
before, sheet_sets = {}, {}
for project in projects:
rows = next(p.rows for p in imported.preview.projects if p.project_path.resolve() == project.resolve())
sheet_sets[project.stem] = {(row.target.schematic_name, row.target.sheet_number) for row in rows}
before[project.stem] = inspect(project, sheet_sets[project.stem], 'before')
def stop_during_save():
while not finished.wait(.01):
for path in (output / 'data/tasks').glob('*/quick-run/quick-part-replace.log'):
try:
text = path.read_text(encoding='utf-8-sig')
except OSError:
continue
if 'save_requested job=' in text:
report['stop_requested'] = {'time_ns': time.time_ns(), 'log': str(path),
'trigger': 'production save_requested log; ordinary stop only'}
control.set()
return
watcher = Thread(target=stop_during_save, daemon=True)
watcher.start()
result = execute_task_queue(service, tuple(task.task_id for task in tasks), stop_event=control)
finished.set()
watcher.join(3)
report['queue_result'] = asdict(result)
assert report.get('stop_requested') and not control.force_event.is_set()
assert result.paused and [row.state for row in result.entries] == ['completed', 'pending'], result
report['comparisons'] = []
for index, project in enumerate(projects):
after = inspect(project, sheet_sets[project.stem], 'after')
if index == 0:
report['comparisons'].append(compare_saved(before[project.stem], after))
else:
assert after == before[project.stem], 'second project was changed after stop'
report['comparisons'].append({'unchanged': True, 'components': len(after)})
assert not service.pending_batch_resources()
assert not list((output / 'data').rglob('uncertain.flag'))
report['verified'] = True
except BaseException as error:
import traceback
report.update(error=repr(error), traceback=traceback.format_exc())
raise
finally:
finished.set()
if watcher is not None:
watcher.join(3)
store.close()
report['source_unchanged'] = all(hashes(source_root / name) == source_hashes[name] for name in NAMES)
report['library_lmc_unchanged'] = hashlib.sha256(library.read_bytes()).hexdigest() == library_hash
save(output / 'report.json', report)
print(json.dumps(report, ensure_ascii=False), flush=True)
return 0 if report.get('verified') and report['source_unchanged'] and report['library_lmc_unchanged'] else 1
if __name__ == '__main__':
raise SystemExit(main())