Compare commits

...
Author SHA1 Message Date
Codex 427095212b Import DxPartReplace 1.2.0-beta.31 and Windows x64 dependencies 2026-11-04 11:35:20 +08:00
154 changed files with 8079 additions and 267 deletions

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
# 项目维护约定
## 持续版本记录
- 项目的统一问题与修改记录是根目录 `版本问题与修改记录.md`。
- 后续每次修改功能、修复问题、分析新的诊断包或发布版本,均同步更新该文件;不得只在聊天中说明。
- 工作尚未完成时写入“未发布与文档维护”;发布时补全真实版本、日期、问题、证据、修改、验证、限制和交付信息。
- 若改变了历史方案,保留旧记录并明确标注替代关系,同时更新“当前有效行为”。
- 未验证的根因、版本归属和现场结果写“待核实”;不能把模拟测试、便携版自检写成全部真实 CAD 工程验收通过。
- 纯文档维护不自动增加程序版本,不覆盖既有发布 ZIP;需要更新交付包时遵循该次任务范围。
- `tools/package_portable.py` 会把主记录复制为包内的 `版本问题与修改记录.md`;以后打包前确保记录已同步。
+5 -1
View File
@@ -4,7 +4,11 @@
面向 **Windows + DxDesigner / Expedition Enterprise 7.9.5** 的器件批量替换工具。一个工程对应一个任务,可以只替换同料号下的部分位号;支持直接编辑和 Excel BOM 导入,同批工程按顺序执行。
**最新版本: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)
**当前本地交付版本:1.2.0-beta.31。** 从早期版本到当前的问题、修改、诊断依据、验证范围及后续更新,统一见 [版本问题与修改记录](版本问题与修改记录.md)。下面的 1.1.x 发布链接和教程属于历史资料,当前行为以该记录为准。
本分支附有 [Windows x64 离线依赖和安装说明](dependencies/windows-x64/README.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 4;旧 Git 标签保留供回溯。升级前备份 `data`,完整更新 EXE 与 `_internal`,不要只替换 EXE。
+16
View File
@@ -0,0 +1,16 @@
# beta.31 Windows x64 离线依赖
`wheels/` 收录 Python 3.12 x64 使用的运行、测试和打包依赖。`requirements.lock` 固定每个包的版本和 SHA256。运行库版本与 beta.31 便携包的第三方版本清单一致。
安装 Python 3.12 x64 后,从仓库根目录执行以下 PowerShell 命令。
```powershell
py -3.12 -m venv .venv
.\.venv\Scripts\python.exe -m pip install --no-index --find-links dependencies/windows-x64/wheels --require-hashes -r dependencies/windows-x64/requirements.lock
.\.venv\Scripts\python.exe -m pip install --no-index --no-build-isolation --no-deps -e .
.\.venv\Scripts\python.exe -m pip check
```
打包入口为 `packaging/Build-DxPartReplace.ps1`。真实 CAD 操作需要本机安装 DxDesigner、有效许可证及工程使用的器件库。
核对基准为 `DxPartReplace-1.2.0-beta.31-Windows-x64.zip`,SHA256 为 `7f0fc33a9c3e83e647eeb8f29641ae04dc9aa2895b923f91884f499d157b33f6`。已逐一核对包内 103 个应用模块、程序入口及两份 VBS 脚本与本分支导入的源码。
+29
View File
@@ -0,0 +1,29 @@
# Windows x64, CPython 3.12. Runtime, test and build dependencies.
altgraph==0.17.5 --hash=sha256:f3a22400bce1b0c701683820ac4f3b159cd301acab067c51c653e06961600597
build==1.6.1 --hash=sha256:ecd351a4be9d35a9eaaba244a7687143c9c7d4aea6ac964e7e7ddab20cbcf4e7
colorama==0.4.6 --hash=sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6
comtypes==1.4.17 --hash=sha256:4e0a221dde2c589b82977bed802efd71cd5eb67a380347b732e02735e597f586
coverage==7.17.0 --hash=sha256:88a1a9fb0075b83389dd9b403bb3fce976031b3c2aa0e05e3e57143cf639de69
et_xmlfile==2.0.0 --hash=sha256:7a91720bc756843502c3b7504c77b8fe44217c85c537d85037f0f536151b2caa
iniconfig==2.3.1 --hash=sha256:9121e2c1fdb355232495be3194c8dfe87ccc2d5dee45947b78e68f499790d7a7
openpyxl==3.1.5 --hash=sha256:5282c12b107bffeef825f4617dc029afaf41d0ea60823bbb665ef3079dc79de2
packaging==26.3 --hash=sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c
pefile==2024.8.26 --hash=sha256:76f8b485dcd3b1bb8166f1128d395fa3d87af26360c2358fb75b80019b957c6f
pillow==12.3.0 --hash=sha256:a2b55dd6b2a4c4b7d87ffa56bdb33fdc5fdb9a462173861a7bc097f17d91cb09
pluggy==1.7.0 --hash=sha256:7dd7b0d8832ba3cb632c306926ded123429211b83641b35dc5c41ad2d34f9bec
Pygments==2.21.0 --hash=sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9
pyinstaller==6.22.3 --hash=sha256:500bd58c7bf7e584a8435adccbd763a0b918d5c12b08d74ff50fd79b2915458b
pyinstaller-hooks-contrib==2026.9 --hash=sha256:fb3e67dd64122561724c48ad0366ac9e8c16e638b63bf7d85de03bf3fdbf06c3
pyproject_hooks==1.3.3 --hash=sha256:5fc53fdac9f7bd63fbcdc868fb5f90b4784d78a53a3d3388cd738b807441a20b
PySide6_Essentials==6.8.3 --hash=sha256:3c0fae5550aff69f2166f46476c36e0ef56ce73d84829eac4559770b0c034b07
pytest==8.4.2 --hash=sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79
pytest-cov==6.3.0 --hash=sha256:440db28156d2468cafc0415b4f8e50856a0d11faefa38f30906048fe490f1749
pytest-qt==4.5.0 --hash=sha256:ed21ea9b861247f7d18090a26bfbda8fb51d7a8a7b6f776157426ff2ccf26eff
pywin32==312 --hash=sha256:b457f6d628a47e8a7346ce22acb7e1a46a4a78b52e1d17e1af56871bd19a93bc
pywin32-ctypes==0.2.3 --hash=sha256:8a1513379d709975552d202d942d9837758905c8d01eb82b8bcc30918929e7b8
pywinauto==0.6.9 --hash=sha256:5924b3072864a1d730c5546bbeb17cf4063ba518b618dbc5e43c18276c7c9356
setuptools==84.2.1 --hash=sha256:ca140b45d5325a9478f880e351484f62fef15f336462e449ca0c03c873d589c2
shiboken6==6.8.3 --hash=sha256:bca3a94513ce9242f7d4bbdca902072a1631888e0aa3a8711a52cc5dbe93588f
six==1.17.0 --hash=sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274
typing_extensions==4.16.0 --hash=sha256:481caa481374e813c1b176ada14e97f1f67a4539ce9cfeb3f350d78d6370c2e8
wheel==0.48.0 --hash=sha256:3217dcc807155e45db462d7ef2431f5ddda0d7273b700d05a67b271ceb1287ab
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+11
View File
@@ -0,0 +1,11 @@
# DxPartReplace 1.2.0-beta.10
- 替换结果及 Excel 导出增加原 Value / 实际回读 Value。原值来自替换前快照,实际值在原有属性核验后读取;保存重开核验后重新记录实际值。按 job_id / 返回状态关联并立即保存在事件日志,避免下一个规则覆盖证据。旧历史缺失数据明确标注,不推算实际值。
- 左侧工程列表分为正在替换、未完成、已完成,三组独立折叠并记忆偏好;使用蓝色、浅橙色、绿色背景,损坏/校验失败工程额外标红。只将真实活动工程归入正在替换;旧轮次保留在历史。
- 按用户要求:损坏/无法读取的工程关闭读取会话后跳过,不修复、不重复尝试打开。只读采集取消、CAD 启动和无进展等待均有界;在确认资源退出后继续后面的工程。
- 旧版强制关闭留下的只读监督进程可在新导入前回收,必须同时核验旧主窗口已退出、监督进程 PID 和创建时间吻合、操作属于只读白名单。写入过程保护和其他 CAD 进程不受此规则影响。
- 新增“停止当前工程”,外层结束本次自有进程而不等待 COM,标红“已停止 / 结果待核验”。界面批量调度按工程隔离监督进程,保护当前工程后停止本轮;尚未执行的工程可另行继续。原有 Replace Part、属性恢复、保存和核验逻辑不变。安全暂停继续等待当前项收尾。
升级时保留完整 data,与新 EXE 和 _internal 配套使用。修复的是采集、取消和调度流程,不修复工程 iCDB 数据库。
验证及边界详见 docs/superpowers/plans/2026-10-29-values-and-project-groups.md。
+13
View File
@@ -0,0 +1,13 @@
# DxPartReplace 1.2.0-beta.11
本版增加读取和解析的诊断记录,用于定位 UID 报告格式、CAD 启动、工程打开及器件采集中的具体问题。保持所有解析接受条件、UID 校验、原生替换、Value/NC、取消和资源释放策略不变。
- `application.log` 增加受监督只读操作的进度、校验返回的通过/失败/未核对行数,以及最多 100 条失败行的工程、Excel 行号和原因。明确区分“调用返回”和“校验通过”。
- 每次原生清单/后台 CAD 读取的证据目录增加 `read-trace.jsonl` 和 `read-state.json`,记录 UTC 时间、进程、工程/任务标识、解析阶段和退出结果。后台工作进程每 5 秒记录等待状态、无进展秒数、计数和是否请求取消;不模拟进度。
- 原生 UID 错误增加报告文件名、行号、UID 原文和字符编码、附近报告片段;同时保留有界的正常器件标识样例。可区分大写 I、小写 l、数字 1 及隐藏字符;不会自动改写 UID。
- 诊断包新增上述记录及现有原生采集元数据、工作进程状态/输出、读取会话释放记录;优先导出近期读取记录。继续执行容量限制和用户路径/凭证脱敏,不导出完整原生报告、BOM、数据库或工程。
- 所有新增追踪写入均为尽力记录。诊断存储不可写时,不改变业务结果或跳过资源清理。
使用:升级后重新导入有问题的清单,出现等待或失败时点击“诊断包”,发送生成的完整 ZIP。仅发送 application.log 无法包含详细报告片段。新版本不能补录旧版已经发生的解析过程。
便携版已包含运行依赖;仍需本机安装可用的 DxDesigner。升级保留完整 data,并配套使用新 EXE 和 _internal。
+7
View File
@@ -0,0 +1,7 @@
# DxPartReplace 1.2.0-beta.12
- 诊断包 `DxPartReplace-诊断-20261030-112928` 显示:工程实际已打开;原生展开报告中的 `POWER_IN`,符号 `in.1` 且没有位号,被误判为缺少 UID 的物料。随后 CAD 采集又因为某对象 UID 或位号为空而停止。该诊断没有记录第二个对象的字段,因此不能断定它就是 POWER_IN,也不能由此认定工程损坏。
- 原生解析只排除明确的 POWER_IN/in.1、POWER_OUT/out.1 无位号命名端口,要求符号类型单一、两份报告端口数量一致;其他不明对象继续执行原有安全检查和 CAD 回退。
- CAD 采集按 BOM 位号匹配:无位号对象无法作为替换目标,跳过并记录;可定位的器件仍必须具有 UID。保留全部有位号器件,继续检测不同料号下重复位号。跳过对象的 UID、料号及图页写入日志,最多记录 20 条并显示总数。诊断包现在包含相关运行日志。
- 真正的“有位号但缺 UID”单独报告器件身份不完整,不再直接断言工程损坏。替换、核验及 Value/NC 规则不变。
- 本版按诊断证据构造报告、使用实际 Windows Script Host 运行模拟 CAD 的真实采集脚本,并执行回归及便携版启动检查。未对真实用户工程进行替换验收。
+10
View File
@@ -0,0 +1,10 @@
# DxPartReplace 1.2.0-beta.13
- 排查诊断包 `20261030-132517`:`LDO_S2032_SOT23_1V5` 两次通过后台 CAD 采集,随后工程文件指纹不可用;第二次快照读取 `database/icdb.dat` 时返回拒绝访问。当时同一自有只读 CAD 会话仍存活,但记录不足以证明具体占用者。
- 当采集后完整指纹无法读取,仅在当前自有只读 CAD 的工程及工作进程已经通过同一工程和会话检查点确认关闭时,释放本次会话后重新读取完整指纹。设计文件变化仍不能通过;仅数据库会话数据变化仍需两次相同的器件清单。资源释放失败立即停止。若确认释放后仍无法核验,报告并跳过该工程,其他工程可以继续。
- 原生清单按 `in.1` / `out.1` 类型识别无位号命名端口,如 `POWER_EN`,并保留两份报告的计数核对;不明、重复或不一致的符号仍回退到 CAD 验证。
- 端口名称不限制为某个 CBB 或某个器件,可包含中文及常见标点;CAD 回退对无位号对象也采用通用规则。`20261030-134255` 诊断包中,BOB_XPON、CA_CARD_HIGH 等也出现了工程指纹校验失败;由于日志遗漏,不能断定各工程的具体占用原因相同。
- Value 识别增加 `_NC`、`_nc`。旧 Value 含标识时,以新 Part Label 加 `/NC`;新值已有任一有效标识不重复,Part Label 不存在时保留旧 Value。`NC_OPTION`、`_NCM` 等较长文本不会误判。
- 诊断包优先收集最近任务、后台记录及主日志的有界尾部;文件数与总大小仍有限制,继续脱敏并排除原始工程及数据库。
升级保留整个 `data`,配套使用 EXE 与 `_internal`。自动测试使用模拟工程和 Windows Script Host;本次未修改真实 CAD 工程,请在目标电脑先重新导入并核对实际结果。
+14
View File
@@ -0,0 +1,14 @@
# DxPartReplace 1.2.0-beta.9
修复强制退出后 CBB 导入被旧后台会话记录长期阻塞的问题。
- 导入/采集/执行前,在独占批次锁内自动复核已退出进程和资源记录,归并中断的监督记录;不重放旧请求。
- 主窗口退出时,后台的人工核验等待可在持久保存中断保护后退出收尾,避免等待已不存在的主窗口。
- 已退出监督进程、已退出 CAD/工作进程和空私有桌面的联合证据允许回收残留桌面对象;未知或仍存活的资源保持阻断。
- 手动“检查后台会话”不再启动另一个清理监督进程;仍有待处理资源时明确提示,不再一律报告成功。
- 保留全部中断写入保护、已保存执行结果和历史数据;原生替换和 Value/NC 规则未修改。
- 白色界面优化工程名称/次要信息、导航选中态、状态标记、卡片间距和简短错误提示;器件明细保持主要空间,进度仍可折叠。
升级:关闭旧版,保存并保留完整 data,将新包 EXE 和 _internal 配套更新。若旧版后台已卡住,先保存其他 CAD 工程并重启一次电脑再使用新版。不要删除保护记录绕过核验。此更新处理软件恢复,不修复已损坏的工程 iCDB 数据库。
验证记录见 2026-10-29 interrupted-import 实施计划。进程级验证仅使用临时目录、真实 Windows 监督进程和模拟业务,没有打开或修改用户 CAD 工程。
@@ -0,0 +1,38 @@
# Workspace Redesign Implementation Plan
> **For agentic workers:** Use superpowers:executing-plans inline. Track the checked tasks and evidence here.
**Goal:** 实现讨论确认的白色工作区、可重复导入和 Value/NC 规则,同时保持原生器件替换机制。
**Architecture:** 原生 VBS 继续负责替换与属性核验;Python 导入层负责每次实时库存、路径映射与按工程校验;新的 Qt 工作区复用已验证服务、后台 worker 和持久化接口。
**Tech Stack:** Python 3.12, PySide6, pywin32, openpyxl, VBScript, pytest, PyInstaller.
**Spec:** docs/superpowers/specs/2026-10-27-workspace-redesign.md
## Global Constraints
- 固定白色,工程列表/器件明细独立卡片,紧凑进度。
- 原生替换和连接/身份核验不移除;开发不修改真实 CAD 工程。
- 源码无 Git;在当前目录工作,ZIP 是原始回退点。
## Review Focus
- 新旧料号并存、同位号跨图页、其他料号不能被误认已完成。
- 已完成或删除任务不能让新导入使用陈旧库存。
- 多文件重复器件和工程重名必须报告而不重复替换。
- 缺少/空白 Part Label 的赋值、保存回读和恢复路径须一致。
- 后台资源释放失败不能抹掉已核验结果,未核验不能当成功。
## Tasks
- [ ] 1. 环境/基线:保存源码备份,建立 Python 3.12 环境,运行现有 pytest;建立本文件作为进度账本。
- [ ] 2. Value/NC:添加并运行真实 cscript 替身回归(先失败),实现缺失 Part Label 使用快照旧 Value,独立 NC 不分大小写,正常/恢复/验证一致。
- [ ] 3. 导入:添加新旧料号混合回归(先失败);增加实时新旧料号库存和逐器件已达目标跳过、工程路径映射、多表合并、独立工程错误隔离。
- [ ] 4. Qt 工作区:添加界面行为回归(先失败);新建白色桌面 shell,单工程/批量入口、独立工程与器件框、内嵌结果/历史、紧凑进度与日志导出,接入实际后台服务。
- [ ] 5. 集成/交付:运行完整默认测试及受影响 VBS/应用/打包回归,截图检查桌面,构建 EXE 和说明;复核核心接口和未决实机风险。
## Ledger
Ruling: 用户已明确批准界面及功能并要求开始实现,因此不再要求重复批准实施计划。
Ruling: 没有 Git 元数据,原位修改并保留同级源码 ZIP,不能创建 Git worktree 或提交。
@@ -0,0 +1,45 @@
# Interrupted import recovery implementation plan
> **For agentic workers:** Use superpowers:executing-plans to implement this plan task-by-task.
**Goal:** Permit CBB import after a crashed read session once resource exit is proven, retaining write uncertainty and improving the approved white workspace.
**Architecture:** Reconcile abandoned ownership under the existing supervisor lease before native admission; use the same path for manual cleanup. Keep business protection independent from physical resource release.
**Tech Stack:** Python 3.12, PySide6, Win32, PyInstaller.
**Spec:** ../specs/2026-10-29-interrupted-import-design.md
## Global constraints
- Do not change replacement/property rules or delete engineering data.
- Retain uncertain-write markers and previously saved results.
- Keep fixed white theme, left project list, compact progress and portable runtime.
- The workspace has no Git metadata; edit in place and preserve user data.
## Review focus
- Dead read supervisors must not permanently block unrelated imports.
- Interrupted writes must stay protected after physical cleanup.
- Live or unknown process/desktop ownership must remain blocked.
- A cleanup request must not create another stale cleanup supervisor.
- Pending cleanup must not be presented as successful release.
## Tasks
- [x] Add failing crash-record/admission, empty-desktop and UI cleanup-feedback regression tests.
- [x] Implement preflight reconciliation in batch_supervisor.py and proven orphan checks in batch_resources.py; run the targeted tests and existing safety coverage.
- [x] Refine workspace_window.py/workspace_styles.py with compact status/notice, project list delegate, empty state and accurate cleanup feedback; render the actual Qt window.
- [x] Review changes, run relevant checks, update version/release notes, build and inspect the portable beta.9 ZIP.
## Execution ledger
- Investigation: uploaded application.log contains only recent overview reads and export_diagnostics, without the shown earlier exception. Screenshot identifies batch_resources_pending; source confirms unconditional pending-supervisor admission and manual-only reconciliation.
- Ruling: apply established UI choices and authorized repair directly; no new UX workflow or approval round needed.
- Investigation: wait_for_resolution retained an interactive wait even after the actual GUI parent exited. Added durable owner-exit notification; pipe loss with a live parent only requests ordinary stop.
- RED/GREEN: old admission reproduced batch_resources_pending; old manual cleanup unnecessarily launched another supervisor; old orphan empty-desktop recovery remained blocked. Regression tests now pass. A real temporary GUI was killed while its worker awaited reconciliation; worker exit and next batch admission passed without CAD.
- Review: fixed exact CAD identity preference for PID reuse and gated orphan marker publication on confirmed parent exit. Fresh read-only reviewer found no remaining medium/high issues.
- Ruling: corrected existing test fixtures for atomic PID observation, explicit extended-path directory creation and intended empty-residue semantics; no safety assertions for live windows were removed.
- Ruling: native junction creation uses an existing 8.3 DOS target when available; direct extended-prefix targets are not valid for CPython's helper. Canonical target verification remains mandatory. Existing regression failed before correction and passes afterward.
- Verification: 162 tests passed in 45.83s across interrupted-import recovery, supervisor/runtime, safe stop, isolated desktop/CAD, workspace import/operations/feedback/window and packaging. Real Qt screenshots inspected at 1400x940 and 1280x800.
- Packaging: PyInstaller build and frozen EXE runtime smoke passed (real child process identity and private-desktop release, environment Python variables removed). ZIP CRC, required runtime files, beta.9 version and SHA256 sidecar verified; 254 files, including 229 dependency files, no user data. Archive SHA256: 79741a786ca8cf8cac3e7d5b28e0044d852b95432da96d40215874e8eb30691d.
@@ -0,0 +1,32 @@
# Value readback and collapsible project groups
User-authorized scope: display original and actual readback Value in results/export; three independently collapsible, clearly colored project groups. Existing white layout and replacement/attribute rules remain binding.
Design: collect Value using a read-only report after existing restore/save verification. Bind reports to the exact job/state and persist structured values in task events before the next queue item starts. Missing historical evidence displays “未记录”; do not derive actual Value from Part Label or a planned new value. Preserve existing five-column result parser callers through an optional detailed mode. The workspace/export use eight columns.
Sidebar groups: 正在替换 (blue), 未完成 (amber), 已完成 (green), including counts. Running and unfinished default expanded; completed defaults collapsed. Remember each group's preference. Only the active queue project is running; completed and validated already-target-only projects belong to completed. Failures/paused/unconfigured remain unfinished. Group headings do not select an engineering project or change execution order. Feedback moves projects between groups and refreshes terminal statuses.
- [x] Add failing structured Value parsing/report correlation tests and real WSH read-only report tests.
- [x] Record and ingest Value readback, extend workspace result and Excel columns; preserve historical/failed-result honesty.
- [x] Add collapsible groups, backgrounds, persistence and active-project updates with UI regression tests.
- [x] Render actual results/group states, review, run relevant checks, build and verify portable beta.10.
Constraints: no actual user CAD project writes during verification; preserve old data and interrupted-write protections. No Git metadata is present.
## Steering and execution ledger
- User added corrupt-project cancellation/restart failure, then explicitly requested skip/no recovery for read failures, then immediate current-project Stop without waiting for COM. These are part of this delivery.
- Read cancellation/runtime work was delegated under subagent-driven-development; Value/UI and final integration stayed in the parent. Read task limits: startup45s, no-progress45s +5s grace; cancel5s, bounded owned Quit/Job cleanup. Failed read sessions release before skip; no automatic failed-project retry.
- Task reviews caught and resolved native CLI nonzero skip semantics, exact request/claim provenance for orphan termination, recovered Value reporting, first-project Running rendering and same-window old-round duplication. Reviewer usage limit prevented a final independent reread; parent finishes with regression and integration checks.
- Immediate Stop uses one existing supervised engine per project. It durably protects only that project, exits owned processes, marks persistent manual_stop and leaves later projects unlaunched. Existing mixed/write interruption handling remains.
## Final verification — 2026-10-29
- Combined regression: **365 passed in 91.44s**, covering read/restart resource admission, real owned-process cancellation and forced Stop, isolated CAD lifecycle, native inventory handling, grouped UI, results/export, recovery evidence, real WSH Value reports, unchanged Value policy and packaging tests. Uses base Python with project/venv packages and offscreen Qt; does not open user CAD projects.
- Initial concurrent build/regression run had one failure in the 1-second native-sidecar liveness timing test (364 passed). No monitor logic was changed to suppress it: the full monitor module passed 33/33, then the complete combined selection passed serially as above. Heavy concurrent build load is suspected, not proven, as the initial timing cause.
- Actual Qt previews inspected at 1280 and 1400 widths: `build/beta10-workspace-1280.png`, `build/beta10-workspace.png`, `build/beta10-workspace-results.png`. Red current-project Stop remains visible in the compact bottom toolbar.
- PyInstaller completed successfully; EXE FileVersion `1.2.0.10`, ProductVersion `1.2.0-beta.10`.
- `tools/verify_packaged_runtime.py dist/DxPartReplace/DxPartReplace.exe build/beta10-frozen-check` passed with Python environment overrides removed. Actual frozen supervisor launched, process creation time read, owned desktop and process resources positively released, clean application exit recorded.
- ZIP verified: 254 files, bundled Python DLL and native VBS present; VBS byte-identical to source; no `data/` user records; CRC test and SHA-256 sidecar match.
- Deliverable: `dist/DxPartReplace-1.2.0-beta.10-Windows-x64.zip` (51,642,840 bytes), SHA-256 `ab4585e1580cbf638ccfab5e45cb8c44144619fafe55559095467553a8cc0151`.
- Boundary: no real user DxDesigner project was replaced during verification. Existing developer-machine long-path fixture failures remain outside this change and are disclosed in the bundled instructions.
@@ -0,0 +1,17 @@
# Discovery fingerprint and signal ports
Evidence from `DxPartReplace-诊断-20261030-132517.zip`: the LDO_S2032_SOT23_1V5 native reader rejected the named `POWER_EN` symbol of type `in.1`. The fallback CAD reader opened the project and completed twice, each time returning ten component identities. The second native snapshot got `PermissionError` on `database/icdb.dat`; the same read-only CAD session remained alive. The task then failed in DiscoveryCache after its post-read fingerprint could not be verified. This indicates a locked/unreadable database; the bundle cannot prove which process held the lock. No CAD replacement occurred in this import.
Fix: recognize named in.1/out.1 ports independent of their names, retaining empty-reference, unique symbol-type, empty path, and paired-report count checks. For a post-discovery fingerprint that is unavailable, request release only from the active owned read-only CAD session after validating its matching project, session and closed-project/worker checkpoint. Recompute the entire fingerprint after confirmed release, then keep existing design-change, database-churn, equal-inventory, and cache-admission guards. A failed release or still-unreadable/changed project is not treated as valid; write sessions are never terminated by this path.
Diagnostic export: the supplied bundle reached the 512-file cap and omitted current task evidence, and application.log exceeded the per-file limit. Export a bounded redacted tail of the current application log, prioritize recently active tasks and recent batch sessions, and raise the still-bounded count to 4096 while retaining the existing 32 MiB and symlink/allowlist limits.
Tests: reproduce an owned CAD database lock, ensure real project modifications remain rejected, verify per-read database churn requires two equal inventories, verify failed cleanup is not cached, test the actual runtime checkpoint/ownership guard and write-session exclusion, cover POWER_EN and other named ports plus ambiguous symbol cases, and test bundle prioritization/tail redaction. No user CAD write during testing.
Steering: additionally recognize old Value's `_NC` and `_nc` as NC markers, using the existing `/NC` output convention, preventing duplicates, preserving original Value without a new Part Label, and rejecting longer tokens such as NC_OPTION. If a positively released read session still leaves the project unverifiable, report a released-project error so the BOM batch can continue with other projects.
The later `20261030-134255` bundle also shows the same final fingerprint error for BOB_XPON and CA_CARD_HIGH; several preceding projects passed and remaining rows were left unchecked. Its 512-file export cap omitted 485 records, so the exact underlying fingerprint issue for each project is not available. Do not assert they all have the same lock or are damaged. Port recognition is not tied to a project or fixed UID/name: named in.1/out.1 symbols with no Ref Designator are checked in both reports, including names with punctuation and non-ASCII text; unsupported or ambiguous cases still fall back to CAD. The existing CAD inventory handles no-reference objects generically across projects while retaining all addressable components.
Verification: 529 regression tests passed before the `_NC` and released-project reporting steering. Afterwards, 375 focused tests passed covering the modified NC flow, full actual VBS restoration on WSH fakes, cache safety, batch continuation, resource ownership, diagnostic export and UI. After further generalizing port-name characters, 73 impacted tests passed, including Chinese/punctuation names, ambiguous identities, actual WSH `_NC`, and verified-release skip behavior. The beta.12 VBS and final source differ only in the requested NC recognition regex. No real CAD engineering project was opened or written.
PyInstaller beta.13 build succeeded. Frozen supervisor, process identity and resource-release smoke passed with a fresh data root; ZIP CRC and SHA-256 sidecar passed, Python runtime included, source VBS matches bundled VBS, updated parser/cache modules archived, version 1.2.0.13, and no user data included. Deliverable: `dist/DxPartReplace-1.2.0-beta.13-Windows-x64.zip`, SHA-256 `855cffb0bfe3a3e98a2363ed22a4f612442905ea4b578c41a153b1f2a28a5fc2`.
@@ -0,0 +1,11 @@
# Power port identity during BOM inventory
Evidence: the supplied 11:29 diagnostic bundle for `LDO_1117_1V5` records a successful CAD open, followed by `inventory_read_component ... component identity is incomplete`. The native flat report rejects `POWER_IN`, whose symbol name is `in.1` and whose reference designator is blank. The native report also shows actual components including CP04 with `$1I44`. The exact CAD object with the incomplete identity is not recorded, so the CAD-side cause is not proven to be the same port.
Fix both causes without changing replacement or attribute rules: the native parser excludes only unambiguously identified POWER_IN/in.1 or POWER_OUT/out.1 named ports, and verifies that their counts match between both reports. Other malformed or ambiguous symbols continue to require CAD fallback. During CAD inventory, objects without a reference designator cannot be addressed by a BOM row and are excluded with logged details and a visible progress message. A nonempty reference with missing UID continues to fail. The wildcard inventory still collects all addressable components to detect duplicate references. A true missing UID is reported as an identity issue, not conclusively as a corrupt project.
Verification: use report fixtures based on the observed evidence, negative cases for conflicting symbol types and mismatched reports, real WSH tests using fake COM-shaped objects for the actual discovery routine, read-only recovery/cleanup tests, the existing Value/NC and native replace contract tests, and a frozen portable build. No actual user CAD project is opened or modified during automated tests.
Completed: 486 selected regression tests passed in 84.51s, including actual WSH execution of the discovery routine with named POWER_IN, a valid component, and a duplicate reference using another part. Tests verify missing UID for a real reference still fails, ambiguous named ports do not bypass checks, and released resources permit subsequent work. The extracted recovery-label test needed a stub for the existing Value-readback reporter; the real readback is separately covered by integration tests.
Compared the previous beta.11 bundled VBS with the current source: every routine outside the discovery subroutine and its new helper is identical after newline normalization. Frozen beta.12 passed the packaged supervisor/resource-release smoke check. ZIP CRC, version metadata, bundled Python, new VBS/source match, parser presence and SHA-256 sidecar all verified; user data is excluded. Deliverable: `dist/DxPartReplace-1.2.0-beta.12-Windows-x64.zip`, SHA-256 `083cf51bd37c6c9a34081d325bd5c8a8220b9c5d66de6eb389f5e92ccc10bf8c`. Actual user project and the precise CAD object missing UID/refdes remain to be checked on the target computer.
@@ -0,0 +1,17 @@
# Read and parser diagnostics
User request: record the parsing process so a later diagnostic export identifies the stalled stage. This is observability only; preserve parser acceptance, UID checks, replacement/property rules, cancellation and resource ownership behavior.
Implementation: bounded, best-effort JSONL traces plus an atomic last-stage snapshot beside existing native/discovery evidence. Record timestamp/PID/project, parser stages, tool exits, exact failing UID and report line context, CAD startup/worker stages, progress and stop/timeout/exit outcomes. Keep the main application log useful by recording supervised read progress and row/project report outcomes. Extend the diagnostic allowlist for these artifacts, worker status/output and existing native metadata; export sampled report context rather than full source reports or databases.
- [x] Add failing tests for invalid UID context, trace bounds/non-interference, real worker stage traces and diagnostic export.
- [x] Implement parser and read-stage evidence, application summaries, bundle coverage.
- [x] Run focused regressions and packaged runtime verification; build portable beta.11 and document how to reproduce/export.
No user CAD writes during verification. Trace I/O failure must never alter a business result or bypass resource cleanup.
Verification so far: 6 new targeted checks initially failed for missing tracing/progress/export, then passed. Read/runtime/diagnostic regression: 169 passed. Combined selection including UI, native ASCII parsing, stop/restart, service, unchanged Value/NC policy, real WSH reports and packaging: 413 passed in 82.62s. After adding report-header samples, reran all affected native-parser/reader/trace/export tests: 60 passed in 1.95s. No acceptance criteria or native VBS replacement code changed.
Final package verification: PyInstaller build succeeded. Frozen EXE runtime smoke passed using a fresh `build/beta11-frozen-check` with Python environment overrides removed; real owned supervisor/desktop resources released and clean exit recorded. Confirmed the new read_trace module is archived in PYZ, EXE FileVersion 1.2.0.11 / ProductVersion 1.2.0-beta.11. ZIP CRC test passed, Python runtime present, no user data included, native VBS byte-identical to source and SHA-256 sidecar matched.
Deliverable: `dist/DxPartReplace-1.2.0-beta.11-Windows-x64.zip`, 51,655,699 bytes; SHA-256 `bd4b50eaf2263ca28b86ea13548df6d3876e643ad52ee913d41d5f12f7c88087`. No real user CAD project opened or modified. Reproduce after upgrade and send the full diagnostic ZIP; old incidents cannot be retroactively traced.
@@ -0,0 +1,20 @@
# 物料替换工作区重构
## 已确认的产品行为
- 单工程和批量替换分别提供入口,共用原生替换、属性恢复和保存回读执行链。默认使用自有独立后台 CAD。
- 固定白色界面、蓝色主按钮、柔和玻璃卡片。工程列表与当前工程器件明细分别成框。紧凑进度栏保留阶段、时间、进度、日志和诊断导出;详细阶段按需展开。
- 支持总表、多份 CBB 分表及分表目录。先精确匹配路径,再按相对目录和唯一 CBB 名称匹配。冲突必须人工指定,不能猜测同名工程。
- 每次重新读取当前库存。相同器件当前料号为旧料号则待替换,为目标新料号则禁用并标记已是目标,其他料号或身份不唯一则报错。一个 Excel 行多个位号分别判定。
- 部分工程校验失败时,通过的工程可先保存执行;不确定 CAD 资源或保存结果继续阻断执行。
- 删除只移出当前列表,历史证据保留;再次导入新建处理轮次,不能重放有未核验结果的旧操作。
- 默认替换后 Value 来源为新器件有效 Part Label。属性不存在或空白时使用替换前快照中的旧 Value。旧 Value 的独立 NC/nc 或 /NC、/nc 标记同步到新 Value,避免重复追加;不把 NCM、NC_OPTION 判为 NC。旧 Value 样式按现有机制恢复。
- 实际保存回读结果在独立结果页可见,不能把计划当实际成功。日志记录运行版本、工程、阶段、目标、异常编号、堆栈、阶段耗时和后台退出证据,支持导出诊断 ZIP。
## 工程约束
继续使用 Python 3.12 / PySide6,原生对话框与 Library Wins、器件身份和连接完整性检查保持。保留旧服务和旧数据兼容入口。不得在用户真实 CAD 工程上自动做开发测试;使用 VBS 替身、临时工程和 Qt offscreen 验证。
## 交付
当前源码目录无 Git 元数据,在同级保存修改前 ZIP。交付更新源码、可运行桌面程序与使用说明,标注实机 CAD 验证范围。
@@ -0,0 +1,13 @@
# Interrupted import recovery and workspace polish
The user authorized fixing CBB import after a forced exit and improving the existing white workspace. Existing decisions remain binding: project list in the sidebar, large component table, compact progress, persistent history, original replacement and Value/NC rules.
Recovery runs under the existing exclusive supervisor lease before launching another native batch. Recheck exact process identities, reconcile abandoned supervisor journals, retain all interrupted-write markers, and release only resources proven inactive. A dead supervisor plus exited recorded workers/CAD and an empty private desktop can be reconciled without requiring the obsolete owner to publish a final handle-close certificate. Live or unreadable ownership stays blocked with an actionable message. Never delete project databases or replay replacement work.
Explicit resource recheck uses the same local reconciliation path rather than creating another cleanup supervisor. The UI reports pending versus released resources accurately. Diagnostics retain the underlying error; the banner presents a short actionable description.
Visual changes refine the existing layout: neutral white cards, clearer navigation and project selection, compact header/controls, muted project metadata, accessible table text and a compact status badge. No dark mode or larger progress area.
Approaches: deleting protection files would lose uncertainty evidence; always requiring manual cleanup reproduces the reported dead end. Automatic evidence-based reconciliation is selected within the user's existing authorized recovery scope.
Validation uses synthetic persistent crash records, real Windows supervisor child processes without CAD, mocked Win32 observations for empty/live desktop cases, existing safety tests, UI rendering, and portable packaging. Real project writes are not part of validation.
+35
View File
@@ -0,0 +1,35 @@
# beta.16 代码审查与修复记录
日期:2026-10-30。
范围:工作区 BOM 导入/草稿/保存、工程切换、任务暂停/继续、只读 CAD 采集错误、后台资源复核。通过实际分支追踪、临时工程/Excel 回归用例和独立审查确认问题;不将推测作为已修复根因。
## 发现与修复
| 优先级 | 触发条件与影响 | 修复位置与行为 |
|---|---|---|
| P1 | 重新导入某工程时,监督器在读取报告产生前拒绝/取消;该工程原有草稿、预览和已保存计划的执行资格可能继续保留 | workspace_window.py `_error`、`_stage_import`:无报告错误也带回工程范围;撤销本次工程旧草稿/预览,添加待处理状态;工程路径标识统一大小写,执行时排除这些工程,其他工程保持可用 |
| P2 | 逐份导入两个 CBB 且第一份未保存;第二次导入覆盖第一份的完整核验预览,保存时第一份被迫重新采集或因旧基线失败 | `_stage_import` 合并不受本次导入影响的预览,保留原始基线;仅更新当前清单涉及的工程,保存仍在事务内重新验证基线和工程身份 |
| P2 | 导入后 current_id 已变,旧器件表尚未刷新且 dirty=true;旧行可能被保存到新工程草稿名下,造成串行显示/身份不符拒绝保存 | `_table_task_id` 绑定表格所属任务,记忆草稿前检查任务身份;导入切换时清空和禁止编辑旧表格,等待新上下文返回 |
| P2 | 共享 TargetFilter 默认隐藏所有未选中行,手工配置页没有“显示未选中”开关,导致未勾选器件全部消失且无法勾选 | 共享器件表恢复默认显示全部;新版工作区显式保持默认隐藏未选中,并可用复选框展开 |
| P2 | 在两个工程之间暂停,外层返回 paused=false 和临时随机 token;界面错误显示“运行结束”。在通知当前工程后、监督器启动前暂停,又会显示取消异常 | workspace_execution.py 正确返回暂停及下一工程;工程边界暂停没有内层 token,仅中途暂停保留真实 token;继续仅使用当前可见且校验可执行的工程,避免使用不存在的队列编号 |
| P2 | 读取超时被统一显示成“工程损坏或无法读取”,误导排查 | batch_runtime.py 在确认只读会话释放后区分打开超时、其他读取超时、CAD 启动失败、身份不完整和一般读取失败;工程列表同步显示超时状态 |
| P2 | 原生报告位号与属性冲突只记录计数和一句话,诊断包无法找出触发回退的器件 | native_inventory_protocol.py 在保留原有回退判定的同时,记录具体 UID、标题位号、属性位号、报告行号;未放宽身份/重复位号核验 |
## 验证与证据
- 新增导入回归:连续未保存导入、失败重导入、监督器提前拒绝、已有计划执行排除、旧表格编辑串入防护。
- 新增暂停回归:工程边界暂停没有虚假 token,继续只启动剩余工程;启动前取消保留真实续跑 token。
- 新增读取回归:超时仍关闭只读资源并保留释放证明,消息不推断损坏;报告冲突带回 UID 和两个字段。
- 完整测试:1016 passed,6 failed,1 warning。日志:build/beta16-suite.log。
- 6 个失败均在 fixture 的 Path.mkdir 阶段触发 WinError 206/3(当前 Windows 长路径策略限制),尚未执行产品逻辑:test_execution_metrics.py 的两项长证据路径用例,test_project_long_paths.py 的四项工程路径用例。未更改系统策略或跳过这些测试。
- 两项资源日志用例更新了过期的 Win32 API 模拟接口;真实 Windows 用例保留外部窗口,确认仍会阻断未释放的桌面,不会回放已完成结果。
- 便携 EXE 已通过 tools/verify_packaged_runtime.py 冻结版启动/监督进程/进程身份/资源释放检查;ZIP 完整性、校验和和不包含用户 data 均已核对。
- 独立审查复查了导入合并、暂停 token、路径大小写、旧表任务身份与资源保护分支,未发现遗留的本次改动相关实质性问题。
## 仍需明确的范围
- 本次没有对用户真实 CAD 工程进行端到端替换。FEM 工程在 DxDesigner OpenProject 内部等待的原因仍未由当前诊断包证明;新版完善了读取错误分类和位号冲突证据,不宣称已修复工程本体。
- Windows 源码从 .venv 的重定向 python.exe 直接启动监督器存在 PID 不同的研发运行限制;独立 EXE 不经过此路径且已自检。测试使用基解释器与 venv 依赖目录,未放宽精确进程身份校验。
- 原生 Replace Part、Value/Part Label/NC 规则、保存核验、强制停止后的待核验保护未改写。
交付:dist/DxPartReplace-1.2.0-beta.16-Windows-x64.zip。升级先关闭旧程序,保留旧 data 文件夹,完整解压程序和 _internal,确认窗口版本为 beta.16。
+40
View File
@@ -0,0 +1,40 @@
# 诊断分析:DxPartReplace-诊断-20261030-164044
分析时间:2026-10-30。所有过程时间均换算为北京时间(日志 timestamp_utc 加 8 小时)。本次仅分析诊断记录和代码,未修改程序或工程。
## 已确认结论
1. 本次导入 8 份分表,共 32 行。前 5 个 ETHERNET 工程通过;FEM_KCT8529H 读取失败后已跳过并继续 RTC5638HE。
2. FEM_KCT8529H 和 RTC5638HE 的直接阻塞点都是 QuickPartReplace.vbs 的 `QPEnsureProject`,`App.OpenProject(CStr(TargetPath))` 调用之后没有返回到 `session_ready`。
3. 读取监控在 open_project 无进展 45 秒后请求停止;FEM_KCT8529H 再等待约 5 秒后结束只读会话,确认资源释放,继续下一工程。该故障发生在器件枚举和 BOM 匹配前。
4. “工程损坏或无法读取”是 batch_runtime.py 将读取异常统一包装后的提示,不是 DxDesigner 返回的数据库损坏诊断。本包没有提供足够证据认定数据库损坏。
5. 诊断包在第 7/8 个工程仍在读取时导出,supervisor.json 为 running,session.json 为 ready/generation 3,缺少最终 result.json。FEM_SKY85717 只有任务事件文件,没有本次读取过程。截图中的最终 3 个失败不能全部由本包核实。
## 逐工程证据
| 工程 | 原生报告解析 | CAD 路径 | 可确认的结论 |
|---|---|---|---|
| FEM_KCT8529H | 两次 icdb2ascii 均退出码 0;有效报告解析 67 个符号,29 个符号的标题位号与 Ref Designator 属性不一致;9 个命名端口被忽略 | 16:39:01 进入 open_project;16:39:47 无进展约 45 秒触发停止;16:39:52 已确认释放后台会话 | OpenProject 调用等待超时,未进入器件枚举或 BOM 匹配;数据库损坏未被证实 |
| FEM_RTC5638HE / RTC5638HE.prj | 两次 icdb2ascii 均退出码 0;有效报告解析 65 个符号,21 个符号位号字段不一致;5 个命名端口被忽略 | 16:40:00 进入 open_project;16:40:45.823 已无进展 45.062 秒并请求停止;诊断快照到此截止 | 同样卡在 OpenProject,包内没有最终退出、释放和匹配结果 |
| FEM_SKY85717 | 包内无本次解析记录 | 包内无本次 CAD 读取记录 | 截图显示失败,但本包不能确认具体阶段和原因 |
| ETHERNET_1G(对照) | 同样出现 2 个位号字段不一致并回退 | 两次 CAD 核验均成功;OpenProject 分别约 6.219 秒、4.046 秒;读到 10 个器件 | 位号字段不一致并不必然导致工程无法打开 |
## 原生报告为什么回退
native_inventory_protocol.py 比较 FLAT BLOCKS 报告的符号标题 `ref des ...` 与属性 `Ref Designator`。两个字符串不一致时将 UID 放入 inconsistent_refs。随后 HIER BLOCKS 解析遇到涉及该 UID 的请求物料就转 CAD 核验。
工作区导入会请求旧料号、新料号和 `*`(全库存用于检测重复位号),因此未直接列入替换清单的其他器件也可能触发回退。记录中的 29/21 是字段不一致数量,不是缺少 UID 的数量,也不是清单错误数量。
原生工具正常输出报告,且摘要记录 source_hashes_unchanged=true,说明这一步能读取工程快照,并未修改源文件。它不能证明 CAD 能正常打开,也不能证明全部器件身份一致。
## 仍无法从本包确定的底层原因
OpenProject 未返回的内部原因尚不可见:可能是工程打开流程等待对话框、工程数据库/依赖问题,或正常加载超过 45 秒。这些都是待验证假设。本包没有对话框正文、打开阶段的内部 CAD 错误,不能将过去其他工程出现的 iCDB 报错直接套用到本次。
原生完整 scope.txt/effective.txt 被诊断包白名单排除(manifest 中标为 outside_allowlist);现有追踪只保存符号标题样本和不一致计数,没有保存触发失败的 UID 及两个位号值。因此本包不能继续确定究竟哪个器件的哪两个字段不同。
## 建议的下一步
先在前台 DxDesigner 单独打开这三个 FEM 工程,记录是否弹窗、具体正文,以及到可正常操作所需时间。若前台也无法打开,应根据实际 CAD 报错处理工程;若前台能很快打开,应调查后台 OpenProject 的等待窗口/会话差异;若正常加载确实超过 45 秒,则需要调整工程打开阶段的超时策略。
程序后续应记录触发回退的 UID、标题位号和属性位号;读取超时时捕获自有后台窗口的标题/正文;提示应区分“打开超时”“CAD 明确拒绝打开”“身份核验失败”,避免将超时直接呈现为工程损坏。若需要核对 SKY85717,请在整批结束后再次导出完整诊断包。
+29
View File
@@ -0,0 +1,29 @@
# 诊断分析:20261102-112907
日期:2026-11-02。诊断来源:工作目录内 `DxPartReplace-诊断-20261102-112907`。
## 导入结果
本次日志记录 9 个工程、27 行 BOM:24 行通过,3 行失败。失败行均属于 `LDO_S2032_SOT23_1V5`。完整导入约 7 分 47 秒,期间存在反复回退 CAD 采集和重复库存检查。
失败工程的一致性检查涉及 `database/icdb.dat` 与 `database/cdbsvr/sAddress.adr` 的内容变化。旧逻辑只容许前者作为读取会话变化候选,后者导致核对失败。这条失败证据不能直接证明原理图损坏。
## 为什么界面没有 UF?,日志却出现 UF?
日志中的“标题位号”指原生导出报告 Symbol 记录的 `ref des` 字段,不是窗口标题,也不是截图识别结果。同一 UID 的另一字段 `Prop Ref Designator` 是实例属性。日志记录前者为 `UF?`、后者为 `UP01`。
因此,画面显示 UP01 与导出报告还带有 UF? 可以同时发生。带问号的值呈现占位形式;它究竟来自库的默认属性、内部注释还是报告展开机制,当前证据不足,不能断言用户工程上的位号有误。
旧解析器要求两个字段完全一致,一旦不一致就放弃快速报告读取,改用较慢的 CAD 读取。这样的回退反复发生,是此次慢导入的可见原因之一。
## 本次调整
1. 对 UF?、C? 等占位标题,只有分层、展开两份报告对同一唯一 UID 的实际实例位号均一致,才采用实例属性继续快速读取。重复、嵌套 UID 和实际属性冲突仍不放行。规则对所有工程通用。
2. 将精确的会话地址路径 `database/cdbsvr/sAddress.adr` 纳入已有的运行数据库变化判定;其余设计内容须一致,还需两次库存身份完全一致。缓存完整指纹仍包含这些文件,不全面忽略 database 目录。
3. BOM 匹配本身已经使用位号等信息;保留 UID 作为报告关联和内部定位信息。本次优化减少不必要的回退,不删除身份核对。
不增加导入/保存阶段的中心库查询,维持替换失败后再查询的策略。
## 验证边界
相关解析、缓存和 BOM 导入回归共 107 项通过。覆盖占位标题与实际属性一致、真实属性冲突、会话地址变化、真实设计文件变化等。模拟回归和冻结程序自检不能代替全部真实 CBB 的现场验收;实际耗时改善需用同批 BOM 复测。
+25
View File
@@ -0,0 +1,25 @@
# 诊断分析:20261103-160621
日期:2026-11-03;现场版本:1.2.0-beta.30。
## 现场结果
工程 Xview 导入从 15:58:43 到 15:59:23,约 40 秒。原生报告解析本身约 0.4 秒,但因 CP? / CX05 位号字段差异两次回退 CAD 采集。
替换第 0 组目标 `4300-CF5610-7100` 未加载,DxDataBook 轮询预算为 30 秒,最终 batch_failed;该组工程关闭未保存。之后四组均记录 saved_ok:CX24、CX25/CX32、CX27、CX29/CX31,共 6 个器件。CX20/CX21 所属失败组没有保存成功证据。本次不能描述成整个任务从未继续。
第 0 组失败完成于 16:01:06;后续四组完成于 16:01:59、16:03:00、16:03:46、16:04:38。成功步骤仍包含原生替换、属性/引脚恢复、保存重开回读等耗时。末组记录 native_replace 约 24.1 秒,index_pins 约 4.8 秒,其余时间分布于快照、恢复和回读,不能全部归因于 UID 匹配。
用户确认中心库缺少目标料号;日志单独能证明的是未加载该目标并超时,不能据超时自动断言中心库确实缺料。
## beta.31 调整
- 按最新用户决定,取消替换失败后的中心库存在性查询。导入、保存和失败处理均不启动这类额外查询;具体旧/新料号、图页和原始错误保留,失败项留待人工处理,其他可执行项继续。显式目标封装选择及核心原生替换核验仍保留。
- DxDataBook 目标列表轮询预算从 30 秒缩短到 8 秒,并记录目标、状态和实际耗时。8 秒是轮询预算;单次原生/UIA 调用如阻塞仍受外层看门狗约束,不承诺完整收尾仅用 8 秒。
- 后续成功步骤不再让早先失败的目标与错误详情被“当前步骤”遮盖。
- 分层属性保留与展开标题完全相同的占位位号时,允许同一唯一 UID 的展开实例生效位号参与快速读取;真正的具体位号冲突仍回退 CAD。诊断包没有完整报告字段,此次分层字段具体值仍需现场新日志确认;新增 scope_refdes 信息供后续核实。
- 修正复选框鼠标命中区域:点击整个复选框单元格即可切换,不再必须点中小方框;已有目标、执行中和历史证据锁定规则保留。
## 验证范围
166 项解析、缓存、失败隔离、停止、目标执行与 Qt 鼠标交互回归通过。测试覆盖无目标料号的短轮询、失败后不查中心库且继续下一工程、后续成功不遮盖失败详情、复选框中央点击和锁定状态。真实 CBB 速度和库加载行为仍需现场复测;不减少保存回读核验来宣称提速。
@@ -0,0 +1,38 @@
# 诊断分析:LNB_TPS65233
日期:2026-11-03。诊断包:`DxPartReplace-诊断-20261103-164814`。任务:`9a224826-316c-4119-a36b-be5656aa486a`。本次仅诊断与记录,不修改运行代码或发布包。
## 结论
首发异常是软件执行结果交接阶段的 `ADODB.Stream` 文件写入错误,错误编号 3004(写入文件失败),不是日志已经证明的缺料号、位号或连接错误。
CN09 有保存返回、关闭、重新打开、回读验证通过及 saved_ok 状态证据。随后结果交接文件写入出错,被保守记为 batch_saved_unverified,工程因此停止。不能把整工程状态理解为所有器件均未替换。
## 逐项证据
| 器件 | 替换 | 日志证据 |
| --- | --- | --- |
| CN07、CN08 | 4300-CF1000-5200 → 4300-CF1000-5100 | 18:49:36 saved_readback_verified;saved_ok;事件包含实际料号和 Value=10pF/50V/NC,已保存回读成功 |
| CN09,UID $1I1302 | 4300-CF1010-7200 → 4300-CF1010-7100 | 18:49:54 native_helper_exit code=0;18:50:02 save_returned;18:50:03 project_closed;18:50:11 saved_readback_verified;status.tsv 先有 saved_ok,随后写入 batch_saved_unverified/3004 |
CN09 的原 Value 是 `100PF/50V`。此包没有其独立 value-readback.tsv 或成功业务事件,不能仅凭预览替代实际最终 Value 记录;保存回读通过本身有日志和 status.tsv 双重证据。其他后续条目没有相应执行完成证据。
原始错误为:`ADODB.Stream: save was requested; persisted outcome requires reconciliation. 写入文件失败。`。日志位置:该任务 `quick-run/quick-part-replace.log:57` 为保存回读成功,紧接第 58 行出错;`status.tsv:5` 成功,第 6 行为后续异常。
18:50:17 随后记录 auto_project_timeout 和后台进程退出未确认。这些是首发文件写入异常之后的收尾记录,不是首先发生的替换故障。`stage=query_pins` 是保留的阶段标签;异常来源实际为 ADODB.Stream,不能据此判为引脚连接冲突。
## 定位与推断边界
对照 beta.31 代码 `QPFinishBatchItem`:保存重开核验后先记录 saved_readback_verified,再追加 saved_ok,然后调用 QPWriteUtf8 写 project-session-result.tsv。QPWriteUtf8 通过 ADODB.Stream.SaveToFile(..., 2) 直接覆盖文件。
Python 的 project_session.py 同时反复 read_text 轮询同一文件。根据已存在 saved_ok 和其后的 ADODB.Stream 错误,最符合执行顺序的失败位置是 project-session-result.tsv 的覆盖写入。代码没有给这次写入附带目标路径或底层占用信息,因此具体文件定位属于高可信推断,并非诊断包直接记录了文件路径。
本机最小实验:只在临时文件上使用真实 ADODB.Stream.SaveToFile;Python 持有读取句柄时写入失败,COM scode=-2146825284(低位 3004);释放句柄后同一路径同一内容写入成功。未访问 CAD 或修改用户工程。
这验证了读写竞争能够产生完全相同的错误,且与现有轮询实现吻合。现场到底由该轮询、其他进程占用或其他文件系统问题触发,包内证据不足以唯一确认;不声称已证实具体占用进程、磁盘满或工程损坏。
## 处理建议
先人工打开工程核对 CN09 的实际料号与 Value,再处理尚未完成条目;保留 CN07、CN08 的成功记录。不要因交接报错就重复认定这些器件换料失败。
后续代码修复应针对结果交接:避免读写同一文件发生覆盖冲突,增加受限重试或可靠的独立/原子交接,并记录具体失败路径和底层错误;区分“工程回读已通过、结果通知失败”与“CAD 换料失败”。本次未实施或发布此修复。
@@ -0,0 +1,24 @@
# 诊断分析:TUNER+DEMOD_DVB-C_M88RC6800
日期:2026-11-03。来源:`DxPartReplace-诊断-20261103-164814`,任务 `81210983-ea44-474a-a94c-e960fafce99e`,现场 beta.31。本次仅诊断,不改程序。
## 首发原因
失败组:`4300-CA4750-A200 → 4300-CA4750-A210`,包含 CT64($1I2612)、CT65($1I2614)、CT61($1I2616)。原理图 M88RC6800、图页 CVBS_BUFFER。
17:01:38 原生 helper 返回 code=0,随后进行属性恢复与回读。17:01:44 日志记录 `display_readback_mismatch uid=$1I2612 refdes=CT64 attribute=Part Label fields=font=13->0`。
这意味着程序按旧快照恢复 Part Label 显示字体后,实际读回的字体编号是 0,预期编号是 13。13/0 是 Font 属性值,不是字号,也不是 Part Label 的文字内容。源码 QPRestoreDisplay 写入保存的 Font,QPVerifyDisplay 要求数值完全一致;不一致抛出 vbObjectError+837,日志错误号为 -2147220667。
此例不是缺料号,也不是已记录的引脚网络对调;它在显示属性字体核验处失败。具体为何字体无法保持为 13,诊断不能确定:可能涉及属性继承、原生字体编号归一化或库符号行为,需要在同一 CAD 会话观察写入/读回或对照符号属性。不能直接断言电脑缺字体或库一定损坏。
## 结果范围
- CT34:4300-CA105E-T5 → 4300-CA1050-A100,saved_ok。
- CT47、CT48:4300-NF150C-T5 → 4300-CF1500-5100,saved_ok。
- CT51、CT55:4300-CF3300-5200 → 4300-CF3300-5100,saved_ok。
- CT64、CT65、CT61 是同一失败组,未获得保存并重开回读成功证据,不能只把 CT64 一只视为未完成。
队列原有 7 组;前三组已确认成功,第 4 组因字体属性核验失败中断,后续 3 组未见启动状态。
失败后状态为 hard_failure / awaiting_reconciliation,收尾无法确认,17:01:58 触发自动超时并记录后台进程退出未确认。超时与释放失败是后续收尾问题;首发错误是 CT64 的 Part Label 字体 13 → 0 不一致。既不能把原生 helper 返回 0 当作全部成功,也不应把前三组已保存的成功记录抹掉。
+29
View File
@@ -0,0 +1,29 @@
# 诊断分析:T18250_IN_LOOP_Filter_Shield
日期:2026-11-03。来源:`DxPartReplace-诊断-20261103-164814`,现场版本 beta.31。本次仅分析,不修改程序、不打包。
## 结论与成功范围
导入的最终 9 行 BOM 均通过器件对应校验。本次队列有 2 个实际替换组:
- 16:46:46,第 0 组 `CT29`、`CT32`:`4300-CA2250-A200 → 4300-CA2250-A100`,状态 saved_ok,已保存并重开回读成功。
- 第 1 组 `DT01`(UID `$9I3008`):`452F-7T0301-0S00 → 452N-7TUS03-0S00`。16:47:07 原生 helper 返回 code=0;16:47:10 连接校验失败,状态 hard_failure。没有该组保存并回读成功证据。
## 首发错误
图页为 `T18250 / TUNER_T18250`。quick-part-replace.log 给出:
| 引脚 | 替换前 | 替换后 |
| --- | --- | --- |
| 1 | (680,1120),`$9N4338 / GND` | (670,1130),`$9N2774 / LOOPOUT` |
| 2 | (680,1180),`$9N2774 / LOOPOUT` | (670,1190),`$9N4338 / GND` |
两端网络按引脚号发生对调。当前 QPConnectionsMatch 允许统一平移,但要求已知引脚号、网络及相对几何匹配;此例无法匹配。因此不是单纯的坐标平移,也不是目标料号未找到。原生替换调用成功不等于连接关系验证通过。
更深层原因可能涉及新旧库符号的引脚编号/几何布局/方向,以及替换时的引脚对应关系。诊断包不能确定其中哪一项是库定义错误;需要对照两个料号的符号和真实引脚定义确认,不能仅凭日志判断应旋转还是修改库。
## 为什么随后停止
校验失败后进入 awaiting_reconciliation,safe_stop=false,程序无法确认这一组写入/保存的最终状态,保留 uncertain.flag。16:47:15 监督器自动超时停止;同时记录后台 CAD 进程退出未确认。因此“超时”和“后台释放失败”是首发连接冲突后的收尾问题,不是缺料号查询导致的首发错误。
建议人工检查 DT01 的实际料号及 1/2 脚网络,核对新旧符号对应关系后再决定处理方式。CT29、CT32 的成功证据应保留;不要将两组统称全部未替换,也不要跳过连接验证把 DT01 标为成功。
+131 -39
View File
@@ -715,7 +715,7 @@ 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 ComponentText, ValueText, ComponentMode, Eligible, SkippedNoRef, ComponentUid, ComponentRef
Set Requested = QPReadInventoryRequest()
Set Seen = CreateObject("Scripting.Dictionary")
Text = "project_path" & vbTab & "schematic_name" & vbTab & "sheet_number" & vbTab & "old_part" & vbCrLf
@@ -723,6 +723,7 @@ Sub QPDiscoverInventory(ByRef Documents)
ComponentText = "project_path" & vbTab & "schematic_name" & vbTab & "sheet_number" & vbTab & _
"uid" & vbTab & "refdes" & vbTab & "old_part" & vbTab & "old_value" & vbCrLf
Count = 0
SkippedNoRef = 0
Set Schematics = Documents.GetAvailableSchematics()
For Each Schematic In Schematics
Set Sheets = Documents.GetAvailableSheets(CStr(Schematic))
@@ -745,24 +746,36 @@ Sub QPDiscoverInventory(ByRef Documents)
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 Len(PartNumber) > 0 And (Requested.Exists(PartKey) Or Requested.Exists("*")) Then
Eligible = True
If ComponentMode Then
ComponentUid = CStr(Comp.UID)
ComponentRef = CStr(Comp.Refdes)
Eligible = QPInventoryEligible(ComponentUid, ComponentRef)
If Not Eligible Then
SkippedNoRef = SkippedNoRef + 1
If SkippedNoRef <= 20 Then QPLog QP_Root, "WARN", _
"inventory_skipped_no_ref uid=" & QPLogField(ComponentUid) & _
" part=" & QPLogField(PartNumber) & " sheet=" & QPLogField(CStr(Sheet))
End If
End If
If ComponentMode And Eligible 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 _
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(ComponentUid) & vbTab & QPEncode(ComponentRef) & vbTab & _
QPEncode(CStr(PartNumber)) & vbTab & QPEncode(CStr(ValueText)) & vbCrLf
End If
HitKey = QPSheetKey(Schematic, Sheet) & Chr(30) & PartKey
If Not Seen.Exists(HitKey) Then
Seen.Add HitKey, True
Text = Text & QPEncode(QPProjectPath()) & vbTab & _
QPEncode(CStr(Schematic)) & vbTab & QPEncode(CStr(Sheet)) & vbTab & _
QPEncode(CStr(Requested(PartKey))) & vbCrLf
If Eligible Then
HitKey = QPSheetKey(Schematic, Sheet) & Chr(30) & PartKey
If Not Seen.Exists(HitKey) Then
Seen.Add HitKey, True
Text = Text & QPEncode(QPProjectPath()) & vbTab & _
QPEncode(CStr(Schematic)) & vbTab & QPEncode(CStr(Sheet)) & vbTab & _
QPEncode(CStr(PartNumber)) & vbCrLf
End If
End If
End If
Next
@@ -774,8 +787,18 @@ Sub QPDiscoverInventory(ByRef Documents)
Next
QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "inventory.tsv"), Text
If ComponentMode Then QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "components.tsv"), ComponentText
If SkippedNoRef > 0 Then WScript.StdErr.WriteLine _
"inventory: skipped " & CStr(SkippedNoRef) & " objects without Ref Designator; see quick-part-replace.log"
End Sub
Function QPInventoryEligible(ByVal Uid, ByVal Refdes)
QPInventoryEligible = False
If Len(CStr(Refdes)) = 0 Then Exit Function
If Len(CStr(Uid)) = 0 Then Err.Raise vbObjectError + 872, _
"QuickPartReplace.Discovery", "component identity is incomplete: missing UID for refdes=" & CStr(Refdes)
QPInventoryEligible = True
End Function
Function QPReadInventoryRequest()
Dim Path, Lines, Header, Result, Index, Fields, OldPart, Key
Path = QP_Fso.BuildPath(QP_Root, "inventory-request.tsv")
@@ -968,6 +991,7 @@ Sub QPFinishBatchItem()
Plans.Add CStr(Index), Candidate
Next
QPVerifyRestoredPlans Command, Records, Plans, ValueStates
QPRecordValueReadback Command, Records, Plans, "saved_ok"
If Not QP_SessionMode Then QPCloseBatchProject TargetPath, False
FinalState = "saved_ok"
QPLog QP_Root, "INFO", "saved_readback_verified job=" & QP_CurrentJobId
@@ -1689,6 +1713,7 @@ Sub QPRestoreAfterOperatorReplace(ByRef Command, ByVal SnapshotPath)
End If
ReplacementSummary = QPReplacementSummary(Records, Plans, CStr(Command("old_part_number")), _
CStr(Command("new_part_number")))
QPRecordValueReadback Command, Records, Plans, "restored_ok"
SuccessMessage = "Native replacement and parameter readback passed on current sheet. Replacements: " & _
ReplacementSummary & " " & QPDisplayVerificationSummary(Records)
If Len(QPTargetCell(Command)) > 0 Then SuccessMessage = SuccessMessage & _
@@ -1699,6 +1724,54 @@ Sub QPRestoreAfterOperatorReplace(ByRef Command, ByVal SnapshotPath)
QPLog QP_Root, "INFO", "verified replacement count=" & Plans.Count & " value_mode=" & QPValueMode(Command)
End Sub
Sub QPRecordValueReadback(ByRef Command, ByRef Records, ByRef Plans, ByVal State)
' Reporting only: never restore an attribute, save, or open a transaction.
Dim Text, Failure
On Error Resume Next
Text = QPValueReadbackText(Command, Records, Plans, State)
If Err.Number = 0 Then QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "value-readback.tsv"), Text
If Err.Number <> 0 Then
Failure = Err.Description
Err.Clear
QPLog QP_Root, "WARN", "Value readback report unavailable: " & Failure
End If
Err.Clear
On Error GoTo 0
End Sub
Function QPValueReadbackText(ByRef Command, ByRef Records, ByRef Plans, ByVal State)
Dim Text, Index, AttributeIndex, Original, Candidate, Record, Attr, Ref, OldValue, Value
Dim OldPresent, Present
Text = "V" & vbTab & "1" & vbTab & QPEncode(CStr(Command("job_id"))) & vbTab & _
QPEncode(State) & vbTab & QPEncode(CStr(Records.Item("meta")("project_path"))) & vbTab & _
QPEncode(QP_TargetSchematic) & vbTab & QPEncode(QP_TargetSheet) & vbCrLf
For Index = 0 To Plans.Count - 1
Set Original = Records.Item("components").Item(CStr(Index))
Set Candidate = Plans.Item(CStr(Index))
OldValue = "": OldPresent = "0"
For AttributeIndex = 0 To Records.Item("attributes").Count - 1
Set Record = Records.Item("attributes").Item(CStr(AttributeIndex))
If CStr(Record("uid")) = CStr(Original("uid")) And _
StrComp(CStr(Record("name")), "Value", vbTextCompare) = 0 Then
OldValue = QPSavedEffectiveValue(Record): OldPresent = "1"
Exit For
End If
Next
Value = "": Present = "0"
Set Attr = QPFindCurrentAttribute(Candidate, "Value")
If Not Attr Is Nothing Then
Value = QPEffectiveAttributeValue(Attr): Present = "1"
End If
Ref = CStr(Original("refdes"))
If Len(Ref) = 0 Then Ref = "UID=" & CStr(Original("uid"))
Text = Text & "C" & vbTab & QPEncode(CStr(Original("uid"))) & vbTab & QPEncode(Ref) & vbTab & _
QPEncode(CStr(Command("old_part_number"))) & vbTab & _
QPEncode(QPRequiredAttributeValue(Candidate, "Part Number")) & vbTab & _
QPEncode(OldValue) & vbTab & QPEncode(Value) & vbTab & OldPresent & vbTab & Present & vbCrLf
Next
QPValueReadbackText = Text
End Function
Function QPReplacementSummary(ByRef Records, ByRef Plans, ByVal OldPartNumber, ByVal NewPartNumber)
Dim Index, Candidate, ComponentRecord, Identity, Entry
QPReplacementSummary = ""
@@ -1851,6 +1924,7 @@ Sub QPRecoverSavedJob(ByRef Command, ByVal SnapshotPath)
Err.Clear
On Error GoTo 0
If ErrorNumber = 0 Then
QPRecordValueReadback Command, Records, Plans, "restored_ok"
If QP_BatchMode Then
If QP_SessionMode Then Err.Raise vbObjectError + 865, _
"QuickPartReplace.Recovery", "read-only recovery cannot reuse a project session"
@@ -1945,13 +2019,12 @@ Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _
Next
Set LabelAttr = QPFindCurrentAttribute(Comp, "Part Label")
If LabelAttr Is Nothing Then
Err.Raise vbObjectError + 805, "QuickPartReplace", _
"Part Label readback failed: attribute is unavailable; refdes=" & _
QPComponentIdentity(Comp) & "; uid=" & CStr(Comp.UID)
LabelValue = ""
Else
LabelValue = QPEffectiveAttributeValue(LabelAttr)
End If
LabelValue = QPEffectiveAttributeValue(LabelAttr)
If Len(LabelValue) = 0 Then Err.Raise vbObjectError + 805, _
"QuickPartReplace", "Part Label readback failed: value is blank; refdes=" & _
If Len(Trim(LabelValue)) = 0 And Not HasOldValue Then Err.Raise vbObjectError + 805, _
"QuickPartReplace", "Part Label and original Value are unavailable; refdes=" & _
QPComponentIdentity(Comp) & "; uid=" & CStr(Comp.UID)
Set ValueAttr = QPFindCurrentAttribute(Comp, "Value")
If ValueAttr Is Nothing Then
@@ -2068,6 +2141,7 @@ Sub QPRestoreComponent(ByRef Comp, ByRef ComponentRecord, ByRef Attributes, _
Dim ValueVisible
Dim DefaultedFields
Dim HasOldNc
Dim UsedOldValue
Comp.Orientation = CLng(ComponentRecord("orientation"))
Comp.Scale = CDbl(ComponentRecord("scale"))
@@ -2078,6 +2152,7 @@ Sub QPRestoreComponent(ByRef Comp, ByRef ComponentRecord, ByRef Attributes, _
Err.Raise vbObjectError + 803, "QuickPartReplace", "Refdes restore failed"
End If
LabelValue = ""
UsedOldValue = False
HasOldNc = False
Set ValueRecord = Nothing
For Index = 0 To Attributes.Count - 1
@@ -2127,16 +2202,25 @@ Sub QPRestoreComponent(ByRef Comp, ByRef ComponentRecord, ByRef Attributes, _
LabelValue = QPRequiredAttributeValue(Comp, "Part Number")
Else
If LabelAttr Is Nothing Then
Err.Raise vbObjectError + 805, "QuickPartReplace", _
"Part Label readback failed: attribute is unavailable; refdes=" & _
QPComponentIdentity(Comp) & "; uid=" & CStr(Comp.UID)
LabelValue = ""
Else
LabelValue = QPEffectiveAttributeValue(LabelAttr)
End If
If Len(Trim(LabelValue)) = 0 Then
If ValueRecord Is Nothing Then Err.Raise vbObjectError + 805, "QuickPartReplace", _
"Part Label and original Value are unavailable; refdes=" & QPComponentIdentity(Comp)
LabelValue = QPSavedEffectiveValue(ValueRecord)
UsedOldValue = True
QPLog QP_Root, "INFO", "value_source=original_value refdes=" & QPComponentIdentity(Comp) & _
" reason=missing_or_blank_part_label"
Else
QPLog QP_Root, "INFO", "value_source=target_part_label refdes=" & QPComponentIdentity(Comp)
End If
LabelValue = QPEffectiveAttributeValue(LabelAttr)
End If
If Len(LabelValue) = 0 Then Err.Raise vbObjectError + 805, _
If Len(LabelValue) = 0 And Not UsedOldValue Then Err.Raise vbObjectError + 805, _
"QuickPartReplace", "Part Label readback failed: selected Value source is blank; refdes=" & _
QPComponentIdentity(Comp) & "; uid=" & CStr(Comp.UID)
If HasOldNc Then LabelValue = QPAppendNcMarker(LabelValue)
If HasOldNc And Not UsedOldValue Then LabelValue = QPAppendNcMarker(LabelValue)
End If
If ValueAttr Is Nothing Then
If ValueRecord Is Nothing Then
@@ -2207,22 +2291,18 @@ End Function
Function QPNativeAttributeAbsenceAllowed(ByVal Name, ByVal ValueMode)
QPNativeAttributeAbsenceAllowed = _
(StrComp(CStr(Name), "Value", vbTextCompare) = 0)
If QPNativeAttributeAbsenceAllowed Or ValueMode <> "preserve" Then Exit Function
If QPNativeAttributeAbsenceAllowed Then Exit Function
If ValueMode <> "preserve" And ValueMode <> "part_label" And ValueMode <> "legacy_part_label" Then Exit Function
QPNativeAttributeAbsenceAllowed = _
(StrComp(CStr(Name), "Part Label", vbTextCompare) = 0)
End Function
Function QPValueHasNcMarker(ByVal Value)
Dim Parts, Index
QPValueHasNcMarker = False
If InStr(CStr(Value), "/") = 0 Then Exit Function
Parts = Split(CStr(Value), "/")
For Index = 1 To UBound(Parts)
If StrComp(Trim(CStr(Parts(Index))), "NC", vbTextCompare) = 0 Then
QPValueHasNcMarker = True
Exit Function
End If
Next
Dim Matcher
Set Matcher = New RegExp
Matcher.Pattern = "(^|[^A-Za-z0-9])nc($|[^A-Za-z0-9_])"
Matcher.IgnoreCase = True
QPValueHasNcMarker = Matcher.Test(CStr(Value))
End Function
Function QPAppendNcMarker(ByVal Value)
@@ -2240,12 +2320,15 @@ End Function
Sub QPSetRequiredNc(ByRef State, ByRef ComponentRecord, ByRef Attributes, ByVal Enabled)
Dim Index, Record
State("nc_required") = False
If Not CBool(Enabled) Then Exit Sub
State("fallback_value") = ""
State("fallback_exists") = False
For Index = 0 To Attributes.Count - 1
Set Record = Attributes.Item(CStr(Index))
If CStr(Record("uid")) = CStr(ComponentRecord("uid")) And _
StrComp(CStr(Record("name")), "Value", vbTextCompare) = 0 Then
State("nc_required") = QPValueHasNcMarker(QPSavedEffectiveValue(Record))
State("fallback_value") = QPSavedEffectiveValue(Record)
State("fallback_exists") = True
State("nc_required") = CBool(Enabled) And QPValueHasNcMarker(QPSavedEffectiveValue(Record))
Exit Sub
End If
Next
@@ -2374,7 +2457,8 @@ Function QPCaptureValueState(ByRef Comp)
End Function
Sub QPVerifyValuePolicy(ByRef Comp, ByVal ValueMode, ByRef Before)
Dim Current, SourceName, ExpectedValue, ExpectedExists
Dim Current, SourceName, ExpectedValue, ExpectedExists, UsedFallback
UsedFallback = False
If ValueMode = "preserve" Then
Set Current = QPCaptureValueState(Comp)
ExpectedExists = Before("exists")
@@ -2393,9 +2477,17 @@ Sub QPVerifyValuePolicy(ByRef Comp, ByVal ValueMode, ByRef Before)
ExpectedValue = CStr(Before("custom_value"))
Else
If ValueMode = "part_number" Then SourceName = "Part Number"
ExpectedValue = QPRequiredAttributeValue(Comp, SourceName)
ExpectedValue = QPEffectiveComponentAttribute(Comp, SourceName)
If SourceName = "Part Label" And Len(Trim(ExpectedValue)) = 0 Then
If Not Before.Exists("fallback_exists") Then _
Err.Raise vbObjectError + 818, "QuickPartReplace", "original Value verification context is missing"
If Not Before("fallback_exists") Then _
Err.Raise vbObjectError + 818, "QuickPartReplace", "Part Label and original Value are unavailable"
ExpectedValue = CStr(Before("fallback_value"))
UsedFallback = True
End If
End If
If Before("nc_required") Then ExpectedValue = QPAppendNcMarker(ExpectedValue)
If Before("nc_required") And Not UsedFallback Then ExpectedValue = QPAppendNcMarker(ExpectedValue)
If QPRequiredAttributeValue(Comp, "Value") <> ExpectedValue Then _
Err.Raise vbObjectError + 818, "QuickPartReplace", "final Value mismatch"
End If
+6 -1
View File
@@ -45,6 +45,11 @@ if (-not $PythonExecutable) {
}
$PythonExecutable = (Resolve-Path -LiteralPath $PythonExecutable -ErrorAction Stop).Path
$env:PYTHONPATH = $sourceRoot
$dependencyRoot = & $PythonExecutable -c "import sysconfig; print(sysconfig.get_path('purelib'))"
$verificationPython = & $PythonExecutable -c "import sys; print(sys._base_executable)"
# The venv redirector creates another PID on Windows. Supervisor identity
# regressions must exercise the real Python process, as the frozen EXE does.
$env:PYTHONPATH = $sourceRoot + [System.IO.Path]::PathSeparator + $dependencyRoot
$env:QT_QPA_PLATFORM = "offscreen"
$smokeRoot = $null
$distRoot = [System.IO.Path]::GetFullPath((Join-Path $repositoryRoot "dist\DxPartReplace"))
@@ -69,7 +74,7 @@ try {
Push-Location $repositoryRoot
$locationPushed = $true
& $PythonExecutable -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
& $verificationPython -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_workspace_value_policy.py tests\integration\test_quick_value_modes.py tests\integration\test_automatic_replacement_regression.py tests\packaging
if ($LASTEXITCODE -ne 0) { throw "Release verification failed; build stopped" }
Remove-VerifiedBuildTarget -RepositoryRoot $repositoryRoot -RelativeTarget "build\DxPartReplace"
Remove-VerifiedBuildTarget -RepositoryRoot $repositoryRoot -RelativeTarget "dist\DxPartReplace"
+15 -8
View File
@@ -10,12 +10,18 @@ import win32com.client.gencache
project_root = Path(SPECPATH).parent
generated_root = project_root / "build" / "DxPartReplace-generated"
generated_root.mkdir(parents=True, exist_ok=True)
viewdraw_api = win32com.client.gencache.EnsureModule(
"{4ADEF4E1-690A-11CE-9261-0020C5E26659}", 0, 1, 0
)
if viewdraw_api is None:
raise RuntimeError("DxDesigner EE 7.9.5 type library is required for packaging")
shutil.copyfile(viewdraw_api.__file__, generated_root / "viewdraw_api.py")
optional_bindings = []
try:
viewdraw_api = win32com.client.gencache.EnsureModule(
"{4ADEF4E1-690A-11CE-9261-0020C5E26659}", 0, 1, 0
)
except Exception:
viewdraw_api = None
if viewdraw_api is not None:
shutil.copyfile(viewdraw_api.__file__, generated_root / "viewdraw_api.py")
optional_bindings.append("viewdraw_api")
# Otherwise com_api generates equivalent bindings from the target machine's
# installed DxDesigner typelib, independent of Python and frozen gen_py writes.
legacy_runtime_datas = [
(
str(project_root / 'src/dx_productivity/trial_task_center/assets/app.ico'),
@@ -36,8 +42,9 @@ a = Analysis(
pathex=[str(project_root / "src"), str(generated_root)],
binaries=[],
datas=legacy_runtime_datas,
hiddenimports=[
"viewdraw_api",
hiddenimports=optional_bindings + [
"win32com.client.genpy",
"win32com.client.build",
"win32com.client",
"win32com.client.dynamic",
"win32con",
+106
View File
@@ -0,0 +1,106 @@
DxDesigner 物料替换 1.2.0-beta.31
运行
将整个 DxPartReplace 文件夹解压到本机可写目录,再双击 DxPartReplace.exe。
必须保留 _internal 文件夹;请勿只复制 EXE。已附带 Python、Qt、Excel 读写和 Windows 自动化依赖,无需安装 Python、Qt 或 Microsoft Excel。
适用 Windows 10/11 64 位。目标电脑仍需安装可用且具有许可证的 DxDesigner,以及工程所用的器件库。本版沿用原软件的 DxDesigner EE 7.9.5 接口。
当前策略(beta.31,替代下文 beta.22~28 历史中心库存在性查询方案)
导入、保存及替换失败后,均不再额外查询中心库料号存在性。失败项记录旧/新料号、图页及原始原因,跳过并留待人工处理;继续后续可执行项。目标料号列表轮询预算缩短为 8 秒,不把超时当作已证实缺料。核心原生替换与指定目标封装所需核验保留。
工作流程
1. 选择“单工程替换”或“批量替换”,添加 .prj 或工程根目录。
2. 导入一份总表、多份 CBB 分表或分表文件夹。旧路径失效时按唯一工程名称匹配,未找到的项目需要人工指定。
3. 核对工程和器件明细。新读入的工程显示在列表前部,并保持本次读取顺序。已是目标料号的器件自动跳过;同位号存在歧义、当前料号冲突或缺失时按 Excel 行报告。有问题的工程保留待处理,通过的工程可先保存计划并执行。逐份导入只更新命中的工程,其他工程的计划和完成记录继续保留。
4. 执行前保存并关闭本次处理的 CAD 工程。软件沿用原生 Replace Part / Library Wins,使用独立后台 CAD,并保留原来的连接、身份和显示属性核验。
5. 查看“替换结果”,可以导出 Excel;新增“原 Value”和“实际回读 Value”,前者取替换前快照,后者取实际器件回读。旧历史未记录的 Value 标为“未记录”,不会根据计划推算。保存未确认、失败和未匹配记录单独标明。
6. 工程列表按蓝色“正在替换”、浅橙色“未完成”、绿色“已完成”分组,点击标题可分别展开/收起。默认收起已完成,记住用户折叠设置;旧处理轮次保留在历史记录中。
7. 执行时“安全暂停”等当前项收尾;“停止当前工程”直接结束本软件为当前项目创建的后台进程,不等待 COM 返回。该项目标红为“已停止 / 结果待核验”,保留日志、备份和已有结果;其余尚未启动的工程可继续处理。停止不代表回滚工程改动。
Value 与 NC
默认取替换后器件的有效 Part Label。Part Label 缺少或空白时使用替换前快照中的旧 Value。
识别独立的 NC、nc、/NC、/nc、_NC、_nc;旧 Value 含 NC 时,新 Value 保留 NC,已有标记不重复添加。NC_OPTION、NCM 等文本不当作 NC。
自定义 Value 沿用原软件的逐行优先规则,可在器件明细中检查和修改。Value 的原位置、字体、方向和可见性继续保留。
如 Part Label 与旧 Value 都不可用,工程会进入明确的错误或待核验状态,不应重复点击替换。
历史、日志与恢复
“移出当前列表”保留历史结果和工程文件;重新导入是按当前工程状态核对的新一轮处理。
进度条显示实际阶段与工程计数,不使用计时模拟百分比。“展开详情”查看过程,“日志”查看错误信息,“诊断包”导出 ZIP 后可发回排查。
出现保存未确认或中断时先使用“核验恢复”。它会核查已有结果,不直接重复原生替换。后台资源释放异常时使用“检查后台会话”。如果主界面被强制关闭,后台监督进程会检测主进程退出,先请求安全停止并尝试正常关闭 CAD;若替换卡在不安全的写入阶段,经过等待后会留下待核验保护记录,再收尾自有进程。中断结果不会自动重放。下次启动先查看日志和“核验恢复”。
运行记录、历史和配置保存在 EXE 旁的 data 文件夹。升级时先保留整个 data;仅替换程序和 _internal。
beta.9 起,导入前会自动检查强制退出后遗留的后台记录;确认后台已退出即可继续,不需要删除 data。底部“检查后台会话”可手动复核,仍待处理时不会显示为成功。
beta.10 对损坏或无法打开的工程不再自动恢复或重复读取:CAD 启动或读取无进展限时为45秒,读取超时后最多再给5秒合作退出时间,然后关闭本次只读会话、报告“工程损坏或无法读取,已跳过”,再处理其他工程。取消只读采集的合作退出限时为5秒,随后执行有界的自有进程收尾。重启后自动核验并关闭已失去主窗口的旧只读监督进程,要求请求、进程身份和启动记录一致;不会按进程名称清理其他 CAD。
中断的实际替换/保存仍保留待核验保护;恢复导入不表示替换结果已经核验。
验证范围
验证包括临时目录中的真实 Windows 进程、实际 Windows Script Host 上的模拟器件报告、界面交互与打包启动检查;未打开或修改用户真实 CAD 工程。
本次没有对用户真实 CAD 工程进行端到端替换验收。
当前开发机的 Windows 长路径策略未启用,6 项超长路径夹具测试未能完成;建议将程序和工程放在较短的本地路径。
第三方组件许可请见 THIRD_PARTY 目录。
诊断解析过程(beta.11)
导入时自动记录报告解析、原始 UID、失败行附近片段、CAD 启动、工作进程阶段、5 秒等待快照、超时/取消/退出与资源释放。主日志记录导入进度、最终行数及失败原因。
遇到问题时点击“诊断包”,发送完整 ZIP;不必仅复制 application.log。即使后台仍在等待也可以导出。新记录位于 data/quick-run-history 各次读取目录的 read-trace.jsonl 和 read-state.json。日志有限额并滚动保存;诊断包仅包含必要片段,不包含完整数据库或工程。新日志只能记录升级后发生的读取过程。
本次仅增加诊断记录,不放宽 UID 检查,不调整替换及 Value/NC 规则。
电源端口与器件采集(beta.12)
针对诊断包发现的 POWER_IN 电源端口导致采集失败问题,原生报告会识别明确的 POWER_IN/in.1 与 POWER_OUT/out.1,并交叉校验两份报告。CAD 采集跳过并记录无位号对象;带位号但缺少 UID 的对象仍报告身份不完整。其他无法确认的符号仍需 CAD 核验,替换逻辑及 Value/NC 属性规则不变。
本版已通过报告模拟和 Windows Script Host 假对象验证;尚需在真实 DxDesigner 工程中验证。
工程读取与诊断包(beta.13)
原生报告按 in.1/out.1 端口类型识别没有位号的命名端口,仍交叉核验两份报告。工程采集后若指纹不可读,仅在本软件的只读后台已确认关闭工程和工作进程时,先关闭该后台,再重新核验完整工程;数据仍不可读或确实变化时不会按通过处理。
诊断包优先保留近期任务和日志;主日志过大时保留最新部分,文件和总容量仍有限制。替换及 Value/NC 规则未修改。
批量导入容错(beta.14)
工作区 BOM 导入遇到无法认证释放的读取错误时停止后续采集,但保留此前已完整通过的工程计划草稿,可保存并执行这些已通过项目;失败和未检查工程不会生成替换计划。工程文件在两次读取间变化或无法验证时,只有监督器确认本软件的只读后台会话已释放,才会标记该工程跳过并继续批次;否则仍停止,避免带着未确认的 CAD 会话继续运行。
Value 中的 _NC(beta.13)
旧 Value 含 _NC 或 _nc 时,默认以新 Part Label 为 Value,并补充 /NC;如果新 Part Label 已带 NC 标识,不重复添加。无 Part Label 时保留原 Value,NC_OPTION、NCM 等较长文本不作为 NC 标识。
后台会话复核(beta.15)
修复暂停/强制停止遗留 cleanup=null 时,资源复核反复报 NoneType 错误并阻断后续导入的问题。启动检查或“检查后台会话”确认原进程和桌面已释放后可继续处理其他工程;原工程的待核验标记和历史结果保留。无需删除 data 或 session.json。
代码审查修复(beta.16)
连续导入多个 CBB 时保留其他工程尚未保存的核验预览;重新导入失败时撤销受影响工程的旧草稿并阻止沿用旧计划执行,其他工程保留。切换工程时旧表格编辑不能记入新工程。工作台默认隐藏未选中器件,手工配置页仍可显示和勾选全部器件。工程之间或启动前暂停会正确显示暂停并从剩余工程继续,暂停位置不使用不存在的队列编号。
读取超时区分打开工程超时和其他读取超时,不再直接推断工程损坏。位号与属性冲突记录具体 UID、标题位号、属性位号和报告行号,仍要求 CAD 核验;原生替换、Value/NC 和待核验保护规则保持。
beta.16 验证:完整测试 1016 项通过;6 项超长路径测试在建立夹具目录时被当前 Windows 策略限制(WinError 206/3),未进入产品逻辑。便携版独立监督进程启动和资源释放自检通过;未对用户真实 CAD 工程执行端到端替换。
beta.17:调整浅色工作台的渐变、卡片、字体和进度样式,左侧增加工程搜索;工程分组、器件明细和原操作流程保留。
beta.18:任意工程替换卡在结果待核验状态时,软件在确认写入保护记录后限时停止该工程所属的后台进程并标记自动跳过;确认资源释放后继续其他工程。已有待核验工程在导入时单独标记,其他工程继续核验;执行时跳过该工程。待核验记录保留,实际结果请人工检查。后台资源不能确认释放时仍会阻止下一工程启动。
目标料号预检(beta.22)
工作台导入 BOM 时,在只读工程核对后检查该工程关联的中心库,批量查询目标新料号。不存在、重名和查询失败分别显示;失败条目在器件明细中标红并只供查看,所在工程不可保存执行,其他通过工程可继续处理。保存编辑后的计划时再次查询。查询只验证中心库料号存在性,原生替换时仍需实际器件和符号核验。
中心库副本与报告入口(beta.23)
复制库的校验依据完整文件清单、大小与 SHA256,不再因文件系统时间戳精度差异拒绝内容一致的副本。源库或副本内容差异报告具体文件;诊断包包含脱敏校验明细,不包含库文件。顶部固定 BOM 校验报告入口可重复打开最近报告;选择记录可复制完整说明。
工程读取一致性(beta.24)
读取前后以完整文件清单、大小与 SHA256 比较内容,消除只更新 mtime/ctime 或文件身份导致的读取拒绝;缓存仍以严格指纹失效。数据库会话内容变化仍要求连续两次相同 CAD 采集结果且其他设计文件内容稳定。实际内容变化或读取不完整时保留拦截,提示具体文件并把 discovery-checks 差异纳入诊断包。
表格复制(beta.25)
器件明细单击旧料号或新料号后 Ctrl+C 复制当前列,多选行时逐行复制,Ctrl+A 后可复制当前列全部可见行。右键菜单可复制当前列、旧料号或新料号。只读、缺失物料和筛选状态均可复制完整单元格内容,不改变替换计划。
单元格复制与部分计划(beta.26)
器件明细改为单元格选中,Ctrl+C 复制选择范围的 TSV,含复选框值;复制短暂虚线并在成功后消失,显示已复制提示。多选行的批量填料号继续根据选中单元格所在行处理。
中心库校验失败项默认不勾选但仍显示原因,禁止直接重新勾选已知失败目标。工程位号核验通过时,可编辑同工程其余正常条目的勾选并保存执行;保存时仍复查所选新料号。取消勾选的清单行保留可见,便于重新选择,未列入替换计划的其他器件仍默认隐藏。已执行或中断待核验任务继续保持保护。
保存复用中心库证据(beta.27)
中心库存在性查询结果按库路径与内容哈希保存,证据摘要、后台资源退出和目标料号覆盖范围均核验后可复用。保存只复核所选料号证据,禁止重新启动 Library Manager;库变化、证据失效或未检查的新目标明确提示重新导入 BOM。保存过程显示阶段信息,文件内容校验可取消,取消后不写入计划。升级后请重新导入一次 BOM 建立新版证据缓存。
中心库改为失败后诊断(beta.28,取代前置检查)
导入 BOM 和保存计划不再调用额外的中心库检查。只有原生替换失败、后台会话结束且可确定失败步骤时,才查询该目标料号,报告未找到、存在但替换失败、重名或无法查询。原始错误与待核验保护保留;失败后的中心库查询异常不阻止其他工程继续处理。手动停止或已取消时不启动诊断查询。升级后重新导入 BOM 清除此前前置检查产生的旧草稿状态。
结果列宽和历史工程(beta.29)
替换结果所有列均可拖动标题边界调整宽度,列边界可见,取消末列强制拉伸;长内容通过水平滚动查看。列宽自动保存,下次打开恢复,也可点击按内容调整列宽。点击左侧已完成工程即读取该任务的保存历史,显示原 Value 与实际回读 Value;切换工程立即清除旧结果并防止迟到的查询覆盖新选择。历史缺失字段仍显示未记录,不用计划值补造实际结果。
本版读取优化(beta.30)
报告标题含 UF? 等占位值时,两份报告的实际位号一致即可继续原生读取;不会把占位标题当成工程显示的位号。读取期间仅会话地址文件和 iCDB 数据库变化时,仍须两次库存一致且其他设计内容不变才接受。不取消 UID 身份核对。
beta.31 读取优化补充
分层报告仍保留与展开标题相同的占位位号,而展开报告携带实际实例位号时,按同一唯一 UID 的有效属性读取。其他具体位号冲突仍要求 CAD 核对。后续成功项不再遮盖先前失败项详情。
复选框(beta.31)
点击“替换”整格即可勾选/取消,也可选中单元格按空格。运行期间、已是目标及历史证据锁定的条目仍受只读限制。
+4 -4
View File
@@ -1,8 +1,8 @@
# UTF-8
VSVersionInfo(
ffi=FixedFileInfo(
filevers=(1, 1, 4, 4),
prodvers=(1, 1, 4, 4),
filevers=(1, 2, 0, 31),
prodvers=(1, 2, 0, 31),
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.4'),
StringStruct('FileVersion', '1.2.0.31'),
StringStruct('InternalName', 'DxPartReplace'),
StringStruct('OriginalFilename', 'DxPartReplace.exe'),
StringStruct('ProductName', 'DxDesigner Part Replacement'),
StringStruct('ProductVersion', '1.1.4-beta.4')
StringStruct('ProductVersion', '1.2.0-beta.31')
])
]),
VarFileInfo([VarStruct('Translation', [1033, 1200])])
+2 -2
View File
@@ -4,9 +4,9 @@ build-backend = "setuptools.build_meta"
[project]
name = "dx-productivity"
version = "1.1.4b4"
version = "1.2.0b31"
requires-python = ">=3.12,<3.13"
dependencies = ["PySide6>=6.7,<7", "pywin32>=306", "pywinauto>=0.6.9,<1", "openpyxl>=3.1,<4"]
dependencies = ["PySide6-Essentials>=6.8,<6.9", "pywin32>=306", "pywinauto>=0.6.9,<1", "openpyxl>=3.1,<4"]
[project.scripts]
dx-part-replace = "dx_productivity.part_replace.cli:main"
@@ -24,14 +24,17 @@ _DEFAULT_HOME_PARTS = ("DxProductivity", "trial-task-center")
def _load_window():
"""Import Qt widgets only after diagnostics are ready for UI construction."""
from dx_productivity.trial_task_center.ui import TrialTaskCenterWindow
from dx_productivity.trial_task_center.ui.workspace_window import WorkspaceWindow
return TrialTaskCenterWindow
return WorkspaceWindow
def configure_application_identity(application: QApplication) -> None:
"""Use one stable Windows identity and icon for source and portable launches."""
from dx_productivity import trial_task_center
from dx_productivity.trial_task_center.ui.workspace_styles import light_palette
application.setStyle('Fusion')
application.setPalette(light_palette())
if os.name == 'nt':
import ctypes
@@ -121,6 +124,14 @@ def _flush(context: LoggingContext) -> None:
def _smoke_batch_runtime(data_root: Path, context: LoggingContext) -> None:
"""Exercise process tracking inside the frozen supervisor before releasing."""
from dx_productivity.trial_task_center.batch_supervisor import run_supervised
from dx_productivity.trial_task_center.com_api import registered_api
api = registered_api('{420B2830-E718-11CF-893D-00A0C9054228}', required=('IDictionary',))
import win32com.client
probe = api.IDictionary(win32com.client.Dispatch('Scripting.Dictionary')._oleobj_)
probe.Add('portable', 'ok')
if probe.Item('portable') != 'ok':
raise RuntimeError('Generated COM bindings failed the portable runtime check')
context.logger.info('packaged_com_bindings_smoke_passed')
with_store = TaskStore(data_root / 'task-center.db')
try:
service = TaskCenterService(with_store, context, recover_on_startup=False)
+18 -8
View File
@@ -9,6 +9,7 @@ import time
from .errors import TaskCenterError
from .logging_setup import append_task_event
from .value_readback import value_readback_details
from .models import TaskStatus
from .quick_run import (
BATCH_FAILED_STATE,
@@ -67,13 +68,21 @@ def execute_batch(
if task.status is TaskStatus.PAUSED and not stop_event.is_set():
service._store.set_task_status(task_id, TaskStatus.RUNNING, summary='automatic queue resumed after confirmation')
with measure_phase(service._logging, task_id, 'prepare_queue', unit='step') as metric:
session = preparer.begin_batch_session(
task_id,
projects,
rules,
copy_part_number_to_value=task.copy_part_number_to_value,
**({'runtime': runtime} if runtime is not None else {}),
)
try:
session = preparer.begin_batch_session(
task_id,
projects,
rules,
copy_part_number_to_value=task.copy_part_number_to_value,
**({'runtime': runtime} if runtime is not None else {}),
)
except TaskCenterError as error:
# Sheet discovery here precedes execution of the batch. Do not classify
# the same error from a later execution stage as safe to continue.
if error.code != 'quick_run_sheet_discovery':
raise
raise TaskCenterError('quick_run_preflight',
f'工程只读检查失败,已跳过,请人工检查;{error.message}') from error
metric['completed_units'] = len(session.commands)
initial_progress = progress
progress = session.progress
@@ -184,7 +193,8 @@ def execute_batch(
} else 'INFO'
append_task_event(service._logging, level=event_level,
task_id=task_id, project_id=project_id, operation='quick_run_executed', message=last_result.message,
details={'state': last_result.state, 'continue_queue': not updated.queue_complete,
details={**value_readback_details(last_result, service._logging.logger),
'state': last_result.state, 'continue_queue': not updated.queue_complete,
'timing': metric,
'project_path': str(current),
'schematic_name': progress.next_schematic_name,
@@ -25,8 +25,7 @@ def write_release_event(root, path, record, phase, *, context=None, reason='batc
confirmation = confirmation if isinstance(confirmation, dict) else {}
cad = record.get('cad_shutdown') if isinstance(record.get('cad_shutdown'), dict) else {}
disappeared = confirmation.get('released')
if phase == 'succeeded' and record.get('reconciled'):
disappeared = True
release_accepted = confirmation.get('release_accepted') is True or disappeared is True
details = {'batch_id': record.get('session_id'), 'batch_kind': record.get('kind'),
'generation': record.get('generation'), 'desktop': record.get('desktop'),
'cad_pid': record.get('cad_pid'), 'cad_guid': record.get('cad_guid'),
@@ -34,7 +33,9 @@ def write_release_event(root, path, record, phase, *, context=None, reason='batc
'released' if phase == 'succeeded' else 'blocked'),
'elapsed_seconds': elapsed, 'record_path': str(path),
'desktop_handle_closed': cleanup.get('desktop_handle_closed'),
'desktop_disappeared': disappeared, 'cad_exit_code': cad.get('cad_exit_code'),
'desktop_disappeared': disappeared, 'desktop_release_accepted': release_accepted,
'desktop_residual_only': confirmation.get('residual_only') is True,
'cad_exit_code': cad.get('cad_exit_code'),
'quit_helper_exit_code': cad.get('quit_helper_exit_code'),
'cad_shutdown': cad, 'shutdown_warning': record.get('shutdown_warning'),
'retries': record.get('retries', {}), 'cleanup': cleanup, 'error': str(error)}
@@ -51,6 +52,8 @@ def write_release_event(root, path, record, phase, *, context=None, reason='batc
label = lambda value: '已确认' if value is True else '未确认'
message += (f" · 桌面句柄关闭:{label(details['desktop_handle_closed'])}"
f" · 桌面消失:{label(disappeared)}")
if details['desktop_residual_only']:
message += ' · 空桌面残留已记录,不阻塞后续任务'
if cad:
message += f" · CAD 退出码={details['cad_exit_code']},退出助手码={details['quit_helper_exit_code']}"
if error:
@@ -145,14 +145,36 @@ def cleanup_pending_resources(root, *, logging_context=None):
if not record.get('session_id'):
continue # Corrupt evidence cannot authorize resource actions.
log('started')
desktop = record.get('desktop')
if desktop:
_Win32Api().confirm_desktop_released(desktop)
if not all(_process_gone(item) for item in record.get('processes', ())):
log('failed', '记录中的进程仍未确认退出')
continue
# After restart only confirm disappearance. Never attach to COM,
# close a project, or terminate a PID whose handles we no longer own.
cad_pid = record.get('cad_pid')
cad_identity = next((item for item in record.get('processes', ())
if item['pid'] == cad_pid and item.get('creation_time')),
{'pid': cad_pid, 'creation_time': None})
if cad_pid is not None and not _process_gone(cad_identity):
log('failed', '记录中的 CAD 进程仍未确认退出')
continue
# After restart, only inspect process identities and desktop state.
# Never attach to COM, close a project, or terminate a PID whose
# handles we no longer own. An empty desktop may remain enumerated
# after every recorded process/handle is gone; retain that evidence
# without making it a permanent execution lock.
desktop = record.get('desktop')
if desktop:
api = _Win32Api()
if _record_proves_owned_cleanup(record) or _orphan_owner_exited(record, path):
api.confirm_desktop_released_or_empty(desktop)
else:
api.confirm_desktop_released(desktop)
# An interrupted owner persists cleanup=null until it has a
# receipt. setdefault does not replace an existing null value.
cleanup = record.get('cleanup')
if cleanup is None:
cleanup = record['cleanup'] = {}
if not isinstance(cleanup, dict):
raise ValueError('invalid resource cleanup receipt')
cleanup['desktop_confirmation'] = api.desktop_release_check
record.pop('record_path', None)
record = dict(record, state='released', reconciled=True, released_at=time.time())
write_record(path, record)
@@ -165,6 +187,47 @@ def cleanup_pending_resources(root, *, logging_context=None):
return pending_resources(root)
def _orphan_owner_exited(record, path):
"""A crashed owner's process exit closes its handles without a final receipt.
Recorded workers and CAD have already been checked by the caller. This
permits inspecting an empty desktop residue, never closing a live CAD.
Old write uncertainty remains independent of this physical-resource proof.
"""
from .supervisor_protocol import READ_ONLY_OPERATIONS, valid_journal
try:
owner = json.loads(internal_path(path.with_name('supervisor.json')).read_text(encoding='utf-8'))
cleanup = owner.get('orphan_read_cleanup') or {}
read_bootstrap_closed = (owner.get('operation') in READ_ONLY_OPERATIONS
and cleanup.get('reason') == 'old_parent_exited_read_only'
and cleanup.get('process') == owner.get('process')
and cleanup.get('exit_confirmed') is True
and _process_gone(owner['parent_process']))
return (valid_journal(owner, record['session_id']) and owner.get('process') is not None
and _process_gone(owner['process'])
and (read_bootstrap_closed or not (
record.get('cad_start_requested') and record.get('cad_pid') is None)))
except (OSError, ValueError, TypeError, KeyError):
return False
def _record_proves_owned_cleanup(record):
cleanup = record.get('cleanup')
if (not isinstance(cleanup, dict) or cleanup.get('desktop_handle_closed') is not True
or cleanup.get('errors')):
return False
history = cleanup.get('process_history')
if not isinstance(history, list):
return False
return all(isinstance(item, dict)
and isinstance(item.get('cleanup'), dict)
and item['cleanup'].get('process_handle_closed') is True
and item['cleanup'].get('job_handle_closed') is True
and item['cleanup'].get('job_active_processes', 0) == 0
and item['cleanup'].get('unconfirmed_descendant_handles', 0) == 0
and not item['cleanup'].get('errors') for item in history)
def project_checkpoint_released(root):
root = canonical_path(root)
try:
@@ -87,6 +87,7 @@ class BatchCadRuntime:
def __init__(self, root, kind, stop_event=None, *, desktop_factory=None, cad_factory=None, session_id=None):
self.root = canonical_path(root)
self.kind, self.stop_event = kind, stop_event
self.read_only = kind in {'bom_import', 'collect_target_inventory'}
self.session_id = uuid4().hex if session_id is None else UUID(str(session_id)).hex
self.path = self.root / 'batch-resources' / self.session_id / 'session.json'
self.desktop_factory = desktop_factory or PrivateDesktop
@@ -95,6 +96,7 @@ class BatchCadRuntime:
self.generation = 0
self.result = None
self.record = {'version': 1, 'session_id': self.session_id, 'kind': kind, 'state': 'new',
'read_only': self.read_only,
'generation': 0, 'desktop': None, 'cad_pid': None, 'cad_guid': None,
'processes': [], 'checkpoints': {}, 'retries': {}, 'timings': [], 'native_operations': {}}
self._retry_counts = {}
@@ -192,6 +194,27 @@ class BatchCadRuntime:
def native_scope(self, evidence):
return _DesktopLease(self, evidence, native=True)
def release_idle_for_fingerprint(self, project):
"""Release only our checkpointed read session before hashing its files."""
if (not self.read_only or self.cad is None or self._active_evidence is None
or self.record.get('state') != 'ready'):
return False
root = canonical_path(self._active_evidence)
try:
checkpoint = json.loads(internal_path(root / 'project-checkpoint.json').read_text(encoding='utf-8'))
if (canonical_path(checkpoint['project_path']) != canonical_path(project)
or canonical_path(checkpoint['resource_record']) != canonical_path(self.path)
or not project_checkpoint_released(root)):
return False
except (OSError, ValueError, TypeError, KeyError):
return False
# close() confirms our CAD, workers and desktop have exited. A failed
# release propagates and never authorizes a hash or another project.
self.close(reason='discovery_fingerprint')
self.desktop = self.cad = None
self._active_evidence = None
return True
def _ensure_cad(self, abort_event=None):
if self.cad is None:
from .isolated_cad import IsolatedCadSession
@@ -201,7 +224,9 @@ class BatchCadRuntime:
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,
**({'stop_control': self.stop_event} if hasattr(self.stop_event, 'force_event') else {}))
**({'read_only': True} if self.read_only else {}),
**({'stop_control': self.stop_event}
if not self.read_only and hasattr(self.stop_event, 'force_event') else {}))
self.cad = cad # Retain cleanup ownership even if startup fails.
try:
cad.__enter__()
@@ -257,7 +282,12 @@ class BatchCadRuntime:
from .discovery_cache import project_fingerprint
from .isolated_cad import PrivateCadStartupError, is_identity_conflict
from .worker_monitor import run_monitored_process
# Operation provenance grants the read-only policy even when callers
# do not expose a cancellation button/event.
cooperative_abort = cooperative_abort or self.read_only
root = canonical_path(command[3])
from .read_trace import ReadTrace
trace = ReadTrace(root, backend='cad-runtime')
self._active_evidence = root
while True:
check_cancelled(abort_event or self.stop_event)
@@ -276,8 +306,13 @@ class BatchCadRuntime:
status_before = status.read_bytes() if status.exists() else None
started = time.monotonic()
failure = None
trace.project = str(project)
trace.emit('cad_start', state='starting', session_id=self.session_id,
attempt_id=attempt_id, job_id=command[-1], read_only=self.read_only)
try:
cad = self._ensure_cad(abort_event)
trace.emit('cad_start', state='completed', cad_pid=self.record['cad_pid'],
generation=self.generation, job_id=command[-1])
cad.checkpoint(root / ('cad-ready-' + attempt_id + '.json'))
environment = {'DXPART_ATTEMPT_FILE': str(directory), 'DXPART_ATTEMPT_ID': attempt_id,
'DXPART_BATCH_SESSION_ID': self.session_id, 'DXPART_BATCH_GENERATION': str(self.generation)}
@@ -285,23 +320,84 @@ class BatchCadRuntime:
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,
owner_loss_path=self.path.parent / 'owner-loss.json',
**({'force_control': self.stop_event} if hasattr(self.stop_event, 'force_event') else {}))
if result.returncode and self.read_only and cooperative_abort:
raise WorkerMonitorError(result.stderr or result.stdout or 'read worker failed')
if result.returncode:
self._retain_uncertain(root, project, expected,
WorkerMonitorError(result.stderr or 'worker failed after possible mutation'))
self._checkpoint(root, project, attempt_id)
trace.emit('project_checkpoint', state='completed', job_id=command[-1],
returncode=result.returncode)
if result.returncode not in (2, 3):
return result
failure = WorkerMonitorError(result.stderr or 'pre-mutation worker startup failed')
except (PrivateCadStartupError, WorkerMonitorError, subprocess.TimeoutExpired, OSError) as error:
except Exception as error:
from .worker_monitor import WorkerMonitorCancelled, WorkerMonitorSafeTimeout
if not isinstance(error, (PrivateCadStartupError, WorkerMonitorError,
subprocess.TimeoutExpired, OSError)):
raise
failure = error
trace.emit('project_worker', state='failed', job_id=command[-1],
error_type=type(error).__name__, error=str(error))
finally:
self._timing('project_worker', started, project=str(project), attempt_id=attempt_id,
task_id=self.record['active_task'], job_id=self.record['active_job'])
if self.read_only and cooperative_abort:
# The whole session belongs to an import/read operation. A
# stuck COM read has no pending authorized mutation to retain.
# Do not report a failed project (or continue) until its owned
# workers, CAD and desktop are positively released.
trace.emit('read_session_release', state='starting', error=str(failure))
self.close(reason='read_failure')
trace.emit('read_session_release', state='completed', resources_released=True,
error=str(failure))
release = {'version': 1, 'session_id': self.session_id,
'generation': self.generation, 'project_path': str(project),
'resources_released': True, 'error': str(failure)}
write_record(root / 'project-read-release.json', release)
self.desktop = self.cad = None
self._active_evidence = None
check_cancelled(abort_event or self.stop_event)
from .project_discovery import ReleasedProjectReadError
if isinstance(failure, (subprocess.TimeoutExpired, WorkerMonitorSafeTimeout)):
stage = getattr(failure, 'stage', '')
try:
stage = json.loads(internal_path(root / 'worker-progress.json').read_text(
encoding='utf-8')).get('stage', stage)
except (OSError, ValueError, TypeError, AttributeError):
pass # Missing diagnostics cannot change release evidence.
reason = '打开工程超时' if stage == 'open_project' else '工程读取超时'
elif isinstance(failure, PrivateCadStartupError):
reason = '后台 CAD 启动失败'
else:
reason = ('工程器件身份不完整' if 'component identity is incomplete' in str(failure)
else '工程读取失败')
raise ReleasedProjectReadError('quick_run_sheet_discovery',
f'{reason},已跳过 / 无法处理:{project.name};'
f'后台读取会话已释放;诊断原因:{failure}') from failure
# Missing proof, changed inputs/status, cancellation or a previous
# write anywhere in this project can never authorize replay.
after = project_fingerprint(project, self.root)
status_after = status.read_bytes() if status.exists() else None
from .worker_monitor import WorkerMonitorCancelled, WorkerMonitorSafeTimeout
if isinstance(failure, (WorkerMonitorCancelled, WorkerMonitorSafeTimeout)):
read_only_stages = {'starting', 'session_startup', 'launch_designer', 'wait_designer',
'check_project', 'open_project', 'open_sheet', 'query_components', 'snapshot_component'}
cancel_proven = (failure.stage in read_only_stages and not failure.cleanup_notes
and before is not None and before == after and status_before == status_after
and self._can_retry(project, expected)
and not internal_path(root / 'recovery.request').exists())
if cancel_proven:
if isinstance(failure, WorkerMonitorSafeTimeout):
raise TaskCenterError('quick_run_project_open',
f'打开工程超过安全等待时间,已停止该工程且未执行器件替换:{project.name}') from failure
raise TaskCenterError('operation_cancelled',
f'已在安全边界取消;未执行器件替换:{project.name}') from failure
self._retain_uncertain(root, project, expected, failure)
raise TaskCenterError('quick_run_uncertain',
'取消时无法证明工程未发生变化,结果待核验;请保留工程和诊断后执行核验恢复。') from failure
# Runtime database changes require separate identity evidence. Until
# available for a failed read, use the stricter full fingerprint.
safe = (not is_identity_conflict(failure) and before is not None and before == after
@@ -320,6 +416,8 @@ class BatchCadRuntime:
self.desktop = self.cad = None
def _retain_uncertain(self, root, project, expected, error):
if self.read_only:
return # Import/read sessions have no authorized CAD write to reconcile.
from .batch_attempt import can_retry_attempt
from .stop_control import wait_for_resolution
attempts = self._project_attempts.get(str(project).casefold(), expected)
@@ -331,7 +429,7 @@ class BatchCadRuntime:
from .batch_attempt import can_retry_attempt
project, evidence = canonical_path(project), canonical_path(evidence)
key = str(project).casefold()
if (self._retry_counts.get(key) or self.record['state'] == 'blocked'
if (self.read_only or self._retry_counts.get(key) or self.record['state'] == 'blocked'
or self.stop_event is not None and self.stop_event.is_set()):
return False
try:
@@ -412,7 +510,8 @@ class BatchCadRuntime:
self.record['cad_shutdown'] = {key: cad_record[key] for key in (
'state', 'clean', 'cad_exit_code', 'quit_helper_exit_code', 'cleanup_errors',
'shutdown_seconds', 'orderly_shutdown', 'diagnostic_errors', 'forced_termination',
'forced_root_termination', 'forced_descendant_pids', 'termination_reason') if key in cad_record}
'forced_root_termination', 'forced_descendant_pids', 'termination_reason',
'read_only_released') if key in cad_record}
if self.desktop is not None:
try:
self.desktop.close()
@@ -15,11 +15,14 @@ from uuid import uuid4
from .errors import TaskCenterError
from .path_access import canonical_path, internal_path, legacy_path
from .supervisor_protocol import VERSION, MessageGate, valid_journal, encode, decode, decode_error
from .supervisor_protocol import (VERSION, READ_ONLY_OPERATIONS, MessageGate, valid_journal,
encode, decode, decode_error)
_WORKER_ROOT = None
_LAUNCH_LOCK = threading.Lock()
_LIVE_SUPERVISORS = {}
AUTO_UNCERTAIN_GRACE_SECONDS = 5
AUTO_PROJECT_IDLE_SECONDS = 360
_WRITE_OPERATIONS = frozenset({'execute_task_queue', 'execute_quick_run_queue', 'execute_quick_run', 'recover_quick_run'})
@@ -64,7 +67,10 @@ def _force_owned_supervisor(root, journal, process):
# The running worker can cross a task boundary while evidence is written.
# Protect all possible targets before force; keep their saved results intact.
journal['protect_all_targets'] = True
_mark_interrupted(root, journal, '用户明确强制结束;写入与保存结果须核验,禁止自动重放。')
reason = ('当前工程长时间无响应,自动结束;写入与保存结果须核验,禁止自动重放。'
if journal.get('stop_initiator') == 'automatic_timeout' else
'用户明确强制结束;写入与保存结果须核验,禁止自动重放。')
_mark_interrupted(root, journal, reason)
try:
process.terminate()
except Exception as error:
@@ -199,6 +205,120 @@ def reconcile_supervisors(root):
return pending_supervisors(root)
def recheck_abandoned_resources(root, context, *, on_progress=None):
"""Called with the admission lease held; never replays business work."""
from .batch_resources import cleanup_pending_resources, pending_resources
before = (*pending_resources(root), *pending_supervisors(root))
if not before and not _LIVE_SUPERVISORS:
return ()
if on_progress is not None:
on_progress('正在检查上次运行的后台会话;不会重复替换器件…')
context.logger.info('resource_preflight_started pending=%d', len(before))
for record in pending_supervisors(root):
try:
_close_orphan_read_supervisor(root, record)
except Exception as error:
context.logger.error('orphan_read_cleanup_failed batch_id=%s error=%s',
record.get('batch_id'), error)
for key, (owned_api, owned_process) in tuple(_LIVE_SUPERVISORS.items()):
if key.parent.parent != root / 'batch-resources':
continue
try:
owned_process.wait(timeout=0)
except subprocess.TimeoutExpired:
continue # A current process is not abandoned ownership.
owned_process.close()
owned_api.close_pending()
_LIVE_SUPERVISORS.pop(key)
physical = cleanup_pending_resources(root, logging_context=context)
supervisors = reconcile_supervisors(root)
remaining = (*physical, *supervisors)
context.logger.info('resource_preflight_finished pending=%d business_replayed=false', len(remaining))
return remaining
def _read_request_proves_operation(root, record):
"""Cross-check the actual one-shot request before abandoned-process action."""
from .batch_resources import _valid_record
directory = internal_path(Path(root) / 'batch-resources' / record['batch_id'])
try:
raw = (directory / 'request.json').read_bytes()
request = json.loads(raw)
if (not isinstance(request, dict)
or hashlib.sha256(raw).hexdigest() != record['request_sha256']
or request.get('version') != VERSION
or request.get('batch_id') != record['batch_id']
or request.get('attempt_id') != record['attempt_id']
or request.get('operation') != record['operation']
or request.get('task_ids') != record['task_ids']
or canonical_path(request['root']) != canonical_path(root)):
return False
claim = json.loads((directory / 'claim.json').read_text(encoding='utf-8'))
if not isinstance(claim, dict) or any(claim.get(key) != value for key, value in {
'version': VERSION, 'batch_id': record['batch_id'],
'attempt_id': record['attempt_id'], 'process': record['process']}.items()):
return False
path = directory / 'session.json'
if path.exists():
resource = json.loads(path.read_text(encoding='utf-8'))
expected_kind = ('collect_target_inventory' if record['operation'] == 'collect_target_inventory'
else 'bom_import')
if (not _valid_record(resource, path) or resource.get('kind') != expected_kind
or resource.get('read_only') is False):
return False
return True
except (OSError, ValueError, TypeError, KeyError):
return False
def _close_orphan_read_supervisor(root, record):
"""Admission-lease-only recovery of a legacy blocked read owner.
Keep a handle to the exact creation identity across the final checks and
termination. Closing that supervisor releases its own child Job handles;
physical child/desktop cleanup is independently checked by the caller.
"""
from .batch_resources import _process_gone, write_record
if (not valid_journal(record, record.get('batch_id'))
or record['operation'] not in READ_ONLY_OPERATIONS
or record.get('process') is None
or not _read_request_proves_operation(root, record)
or not _process_gone(record['parent_process'])):
return False
import win32api
import win32event
import win32process
identity = record['process']
try:
handle = win32api.OpenProcess(0x101001, False, identity['pid'])
except Exception as error:
code = getattr(error, 'winerror', error.args[0] if error.args else None)
if code == 87:
return False
raise
try:
if (str(win32process.GetProcessTimes(handle)['CreationTime']) != identity['creation_time']
or win32event.WaitForSingleObject(handle, 0) == 0
or not _process_gone(record['parent_process'])):
return False
record['orphan_read_cleanup'] = {'reason': 'old_parent_exited_read_only',
'process': identity, 'requested_at': time.time(), 'exit_confirmed': False}
_mark_interrupted(root, record, '只读后台会话的原窗口已退出;正在释放其自有资源。')
# A live parent never grants permission, even if other evidence changed
# while the interruption diagnostic was flushed to disk.
if not _process_gone(record['parent_process']):
return False
win32api.TerminateProcess(handle, 1)
if win32event.WaitForSingleObject(handle, 5000) != 0:
raise TimeoutError('orphan read supervisor exit was not confirmed within 5 seconds')
record['orphan_read_cleanup'].update(exit_confirmed=True, confirmed_at=time.time())
path = Path(root) / 'batch-resources' / record['batch_id'] / 'supervisor.json'
write_record(path, record)
return True
finally:
handle.Close()
def _command(request):
if bool(getattr(sys, 'frozen', False)):
return (sys.executable, '--batch-worker', str(request))
@@ -235,7 +355,17 @@ def _finish_response(root, journal, response, context):
except FileNotFoundError:
resource = {}
shutdown = resource.get('cad_shutdown') or {}
if (shutdown.get('orderly_shutdown') is False or shutdown.get('forced_termination')
cleanup = resource.get('cleanup') or {}
confirmation = cleanup.get('desktop_confirmation') or {}
read_release = (journal['operation'] in READ_ONLY_OPERATIONS
and resource.get('read_only') is True and resource.get('state') == 'released'
and shutdown.get('read_only_released') is True
and not shutdown.get('diagnostic_errors')
and all(item.get('stage') in {'orderly_shutdown', 'forced_cleanup'}
for item in shutdown.get('cleanup_errors', ()))
and cleanup.get('desktop_handle_closed') is True and not cleanup.get('errors')
and (confirmation.get('released') is True or confirmation.get('release_accepted') is True))
if not read_release and (shutdown.get('orderly_shutdown') is False or shutdown.get('forced_termination')
or shutdown.get('diagnostic_errors')
or any(item.get('stage') != 'desktop_release' for item in shutdown.get('cleanup_errors', ()))):
failure = TaskCenterError('application_shutdown_failed',
@@ -263,14 +393,15 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
if stop_event is not None and stop_event.is_set():
raise TaskCenterError('operation_cancelled', '本批操作已取消,未启动监督进程。')
with supervisor_lease(root):
remaining = recheck_abandoned_resources(root, service._logging, on_progress=on_progress)
if operation == 'cleanup_batch_resources':
for key, (owned_api, owned_process) in tuple(_LIVE_SUPERVISORS.items()):
if key.parent.parent == root / 'batch-resources':
owned_process.close()
owned_api.close_pending()
_LIVE_SUPERVISORS.pop(key)
if operation != 'cleanup_batch_resources' and pending_supervisors(root):
raise TaskCenterError('batch_resources_pending', '上一监督进程尚未完成资源复核,请先重新清理资源。')
return remaining # Resource inspection needs no new supervisor.
if remaining:
raise TaskCenterError('batch_resources_pending',
'上次运行的后台会话仍未确认退出,暂不能连接 CAD。'
'请检查后台会话或导出诊断包;已保存结果和待核验保护均已保留。')
if stop_event is not None and stop_event.is_set():
raise TaskCenterError('operation_cancelled', '后台会话检查已结束,本次操作已取消。')
batch_id, attempt_id = uuid4().hex, uuid4().hex
directory = root / 'batch-resources' / batch_id
request = directory / 'request.json'
@@ -296,6 +427,8 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
gate, messages = MessageGate(batch_id, attempt_id), queue.Queue()
api, process, reader = _Win32Api(), None, None
sent_cancel, last_heartbeat = False, time.monotonic()
uncertain_since, auto_force_accepted = None, False
last_project_progress, last_progress_signature = time.monotonic(), None
transport_error = None
force_accepted = False
def receive():
@@ -310,16 +443,57 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
try:
# A normal input-desktop supervisor owns its private-desktop children.
# Only the supervisor, never the GUI, creates a private desktop.
# Keep the guardian alive if the GUI is forcibly closed. It watches
# the parent identity and can still ask its owned CAD session to
# close through the normal Quit path.
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.DEVNULL,
survive_parent_exit=True)
journal.update(state='running', process=process_identity(process))
write_record(directory / 'supervisor.json', journal)
reader = threading.Thread(target=receive, daemon=True)
reader.start()
while process.poll() is None or not messages.empty():
if (operation == 'execute_task_queue' and getattr(stop_event, 'auto_skip_uncertain', False)
and process.poll() is None):
signature = []
for project_root in journal['uncertainty_roots'].values():
try:
progress = json.loads(internal_path(Path(project_root) / 'quick-run' / 'worker-progress.json').read_text(encoding='utf-8'))
signature.append(tuple(progress.get(k) for k in ('token', 'job_id', 'sequence', 'stage', 'done', 'total', 'native_progress_frames')))
except (OSError, ValueError, AttributeError):
signature.append(None)
signature = tuple(signature)
if signature != last_progress_signature:
last_progress_signature, last_project_progress = signature, time.monotonic()
if time.monotonic() - last_project_progress >= AUTO_PROJECT_IDLE_SECONDS:
try:
journal['stop_initiator'] = 'automatic_timeout'
_force_owned_supervisor(root, journal, process)
except Exception as error:
service._logging.logger.error('auto_idle_cleanup_failed batch_id=%s error=%s', batch_id, error)
last_project_progress = time.monotonic()
else:
service._logging.logger.warning('auto_skip_idle_project batch_id=%s', batch_id)
force_accepted = auto_force_accepted = True
break
if (uncertain_since is not None and time.monotonic() - uncertain_since >= AUTO_UNCERTAIN_GRACE_SECONDS
and process.poll() is None
and not getattr(getattr(stop_event, 'force_event', None), 'is_set', lambda: False)()):
try:
journal['stop_initiator'] = 'automatic_timeout'
_force_owned_supervisor(root, journal, process)
except Exception as error:
service._logging.logger.error('auto_skip_cleanup_failed batch_id=%s error=%s', batch_id, error)
uncertain_since = time.monotonic()
else:
service._logging.logger.warning('auto_skip_uncertain_project batch_id=%s', batch_id)
force_accepted = auto_force_accepted = True
break
force = getattr(stop_event, 'force_event', None)
if force is not None and force.is_set() and process.poll() is None:
try:
journal['stop_initiator'] = 'user'
_force_owned_supervisor(root, journal, process)
except Exception as error:
force.clear()
@@ -348,6 +522,7 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
continue
last_heartbeat = time.monotonic()
if value['type'] == 'progress' and on_progress is not None:
last_project_progress = time.monotonic()
on_progress(value['payload'])
elif value['type'] == 'phase' and on_phase is not None:
payload = decode(value['payload'])
@@ -357,6 +532,11 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
service._task_queue_feedback = state['queue']
service._batch_feedback = state['batch']
service._supervisor_waiting = bool(state.get('waiting_for_resolution'))
if (operation == 'execute_task_queue' and getattr(stop_event, 'auto_skip_uncertain', False)
and service._supervisor_waiting):
uncertain_since = uncertain_since or time.monotonic()
else:
uncertain_since = None
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,
@@ -387,8 +567,9 @@ def run_supervised(service, operation, arguments, *, stop_event=None, on_progres
cleanup_pending_resources(root, logging_context=service._logging)
except Exception as error:
service._logging.logger.error('forced_stop_cleanup_pending batch_id=%s error=%s', batch_id, error)
raise TaskCenterError('user_forced_stop',
'已按确认强制结束;已保存的执行证据保留,当前结果待核验。请先核验恢复,不要重复替换。')
raise TaskCenterError('auto_project_timeout' if auto_force_accepted else 'user_forced_stop',
('当前工程超时,已保护待核验记录并结束所属后台会话。' if auto_force_accepted else
'已按确认强制结束;已保存的执行证据保留,当前结果待核验。请先核验恢复,不要重复替换。'))
try:
try:
response = json.loads(internal_path(directory / 'result.json').read_text(encoding='utf-8'))
@@ -15,6 +15,28 @@ from uuid import UUID
from .path_access import internal_path, canonical_path
from .supervisor_protocol import VERSION, OPERATIONS, valid_journal, encode, decode, encode_error
def record_owner_exit(path, record, control, context, reason):
"""A broken pipe requests stop; only proven GUI exit permits orphan cleanup."""
from .batch_resources import _process_gone, write_record
from .stop_control import StopControl
control.set()
try:
if not _process_gone(record['parent_process']):
context.logger.warning('batch_control_lost_parent_alive batch_id=%s reason=%s',
record['batch_id'], reason)
return False
if not internal_path(path).exists():
write_record(path, dict(record, reason=reason, detected_at=time.time()))
if isinstance(control, StopControl):
control.owner_lost_event.set()
context.logger.error('batch_owner_lost batch_id=%s reason=%s', record['batch_id'], reason)
return True
except Exception as error:
context.logger.exception('batch_owner_loss_record_failed batch_id=%s error=%s', record['batch_id'], error)
return False
def _output_stream():
if sys.stdout is not None:
return sys.stdout, False
@@ -39,6 +61,9 @@ def _admit_request(directory, request, request_bytes):
journal = json.loads(internal_path(directory / 'supervisor.json').read_text(encoding='utf-8'))
if (not valid_journal(journal, request['batch_id'])
or journal.get('attempt_id') != request['attempt_id']
or journal.get('operation') != request.get('operation')
or journal.get('task_ids') != request.get('task_ids')
or canonical_path(request['root']) != canonical_path(directory.parent.parent)
or journal.get('state') not in {'starting', 'running'}
or journal.get('request_sha256') != hashlib.sha256(request_bytes).hexdigest()
or not journal.get('parent_process', {}).get('creation_time')
@@ -59,6 +84,7 @@ def _admit_request(directory, request, request_bytes):
'process': own}, stream)
stream.flush()
os.fsync(stream.fileno())
return journal['parent_process']
def run(request_path):
@@ -81,7 +107,7 @@ def run(request_path):
directory = root / 'batch-resources' / batch_id
if request_path != directory / 'request.json':
raise ValueError('Supervisor request ownership path mismatch')
_admit_request(directory, request, request_bytes)
parent_identity = _admit_request(directory, request, request_bytes)
_worker_context(root)
from .stop_control import StopControl
stop = StopControl() if request.get('interactive_stop', False) else threading.Event()
@@ -89,7 +115,13 @@ def run(request_path):
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)
owner_loss_path = directory / 'owner-loss.json'
parent_exited = threading.Event()
sequence = 0
def mark_owner_lost(reason):
if record_owner_exit(owner_loss_path, {'version': VERSION, 'batch_id': batch_id,
'attempt_id': attempt_id, 'parent_process': parent_identity}, stop, context, reason):
parent_exited.set()
def emit(kind, payload=None, *, persist=False):
nonlocal sequence
with output_lock:
@@ -103,10 +135,14 @@ def run(request_path):
output.write(json.dumps(envelope, ensure_ascii=True, allow_nan=False) + '\n')
output.flush()
except (BrokenPipeError, OSError):
stop.set()
mark_owner_lost('control_pipe_disconnected')
def monitor():
while not finished.is_set():
try:
if not parent_exited.is_set():
from .batch_resources import _process_gone
if _process_gone(parent_identity):
mark_owner_lost('parent_process_exited')
cancel = internal_path(directory / 'cancel.json')
if cancel.is_file():
value = json.loads(cancel.read_text(encoding='utf-8'))
@@ -14,6 +14,7 @@ class BomProjectPreview:
baseline: tuple
inventory: tuple
rows: tuple
fresh_inventory: bool = False
@dataclass(frozen=True)
@@ -21,6 +22,7 @@ class BomBatchPreview:
anchor_task_id: str
scope: tuple
projects: tuple[BomProjectPreview, ...]
explicit_scope: bool = False
def _scope(store, task_id):
@@ -42,6 +44,21 @@ def _identity(scope):
return tuple(sorted((task.task_id, str(project.path.resolve()).casefold()) for task, project in scope))
def explicit_scope(store, task_ids):
result = []
if len(set(task_ids)) != len(task_ids):
raise TaskCenterError('project_binding', '本次工程列表存在重复任务')
for task_id in task_ids:
task = store.get_task(task_id)
projects = store.list_projects(task_id)
if task.status is TaskStatus.DELETED or len(projects) != 1:
raise TaskCenterError('project_binding', '本次工程已移除或不是独立工程任务')
result.append((task, projects[0]))
if len({str(p.path.resolve()).casefold() for _, p in result}) != len(result):
raise TaskCenterError('project_binding', '本次列表不能重复处理同一个工程')
return tuple(result)
def export_template(store, task_id, path):
records = () if task_id is None else tuple({'project_name': task.name, 'project_path': str(project.path),
'enabled': True, 'sync_nc': True} for task, project in _scope(store, task_id))
@@ -50,6 +67,8 @@ def export_template(store, task_id, path):
def _assert_editable(service, task_id):
context = service.task_context(task_id)
if context.interrupted:
raise TaskCenterError('quick_run_uncertain', f'工程 {context.task.name} 有未核验结果,请先核验恢复')
if (context.task.status not in {TaskStatus.DRAFT, TaskStatus.READY} or context.has_evidence
or context.interrupted or not context.project or not context.project.available):
raise TaskCenterError('target_plan_locked', f'工程任务当前不能修改 BOM 计划:{context.task.name}')
@@ -76,13 +95,20 @@ def save_batch(service, preview, rows_by_task):
raise TaskCenterError('bom_import', '本批 BOM 预览的工程范围已改变')
result = {}
with store.transaction():
if _identity(_scope(store, preview.anchor_task_id)) != preview.scope:
current_scope = (explicit_scope(store, tuple(t for t, _ in preview.scope)) if preview.explicit_scope else
_scope(store, preview.anchor_task_id))
if _identity(current_scope) != preview.scope:
raise TaskCenterError('project_binding', '本批工程范围已改变,请重新导入 BOM')
for project in preview.projects:
_assert_editable(service, project.task_id)
if tuple(store.list_target_plan(project.task_id)) != project.baseline:
raise TaskCenterError('target_identity', '工程位号计划在 BOM 预览后已改变,请重新导入')
for project in preview.projects:
if project.fresh_inventory:
# The immutable preview baseline was checked above under this
# same transaction. A new live inventory replaces stale edits,
# never an in-flight or executed task (guarded by _assert_editable).
store._connection.execute('DELETE FROM target_plan WHERE task_id = ?', (project.task_id,))
store.record_target_inventory(project.task_id, project.inventory)
result[project.task_id] = store.replace_target_plan(project.task_id, rows_by_task[project.task_id])
return result
@@ -7,13 +7,13 @@ from zipfile import BadZipFile
from openpyxl import load_workbook
from .bom_batch import BomBatchPreview, BomProjectPreview, _assert_editable, _identity, _scope
from .bom_batch import BomBatchPreview, BomProjectPreview, _assert_editable, _identity, _scope, explicit_scope
from .bom_plan import BomMatcher, _normal_headers, _text, parse_bom_row
from .errors import TaskCenterError
from .project_discovery import ReleasedProjectReadError
from .bom_import_state import BomImportPhase
from .store_target_plan import merge_target_inventory
from .target_plan import ProjectIdentityResolver, compile_target_rules
from .target_plan import ProjectIdentityResolver, TargetPlanRow, compile_target_rules
@dataclass(frozen=True)
@@ -39,6 +39,8 @@ class BomImportReport:
projects: tuple[BomProjectResult, ...]
preview: BomBatchPreview | None = None
errors: tuple[str, ...] = ()
inspection_projects: tuple[BomProjectPreview, ...] = ()
library_issues: tuple[tuple[str, str, str], ...] = ()
@property
def total_rows(self):
@@ -82,7 +84,7 @@ class BomInspectionError(TaskCenterError):
self.bom_report = report
def _report(rows, preview=None, errors=()):
def _report(rows, preview=None, errors=(), inspection_projects=(), library_issues=()):
grouped = {}
for row in rows:
grouped.setdefault((row.project_path, row.project_name), []).append(row)
@@ -90,7 +92,7 @@ def _report(rows, preview=None, errors=()):
'failed' if any(row.status == 'failed' for row in items) else
'unchecked' if any(row.status == 'unchecked' for row in items) else 'passed',
tuple(row.excel_row for row in items)) for (path, name), items in grouped.items())
return BomImportReport(tuple(rows), projects, preview, tuple(errors))
return BomImportReport(tuple(rows), projects, preview, tuple(errors), tuple(inspection_projects), tuple(library_issues))
def _check_record_conflicts(rows, records, indices):
@@ -123,7 +125,8 @@ def _check_record_conflicts(rows, records, indices):
rows[index] = replace(rows[index], status='failed', reason=reason)
def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None):
def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None,
workspace_mode=False, workspace_tasks=None):
"""Validate all independent rows; never persist inventory or return partial plans.
Operational failures (including cancellation and native cleanup) propagate so
@@ -136,7 +139,8 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
with BatchCadRuntime(service._logging.root, 'bom_import', stop_event) as owned_runtime:
try:
report = inspect_batch(service, task_id, path, stop_event=stop_event,
on_progress=on_progress, on_phase=on_phase, runtime=owned_runtime)
on_progress=on_progress, on_phase=on_phase, runtime=owned_runtime, workspace_mode=workspace_mode,
workspace_tasks=workspace_tasks)
except BomInspectionError as error:
report = error.bom_report
raise
@@ -159,7 +163,7 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
check_cancelled()
notify('正在读取并校验 BOM Excel 表格…', BomImportPhase.READING)
scope = _scope(service._store, task_id)
scope = explicit_scope(service._store, workspace_tasks) if workspace_mode and workspace_tasks else _scope(service._store, task_id)
runtime.bind_logs(service._logging, tuple(task.task_id for task, _ in scope))
routing = ProjectIdentityResolver()
owners = {routing.project(project.path): (task, project) for task, project in scope}
@@ -241,25 +245,40 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
for key, indices in grouped.items():
check_cancelled()
_check_record_conflicts(rows, records, indices)
if workspace_mode and any(row.status == 'failed' and row.project_path
and routing.project(row.project_path) == key for row in rows):
for index in indices:
if rows[index].status != 'failed':
rows[index] = replace(rows[index], status='failed', reason='本工程其他行未通过校验,工程整体保留待处理')
try:
_assert_editable(service, owners[key][0].task_id)
except TaskCenterError as error:
for index in indices:
rows[index] = replace(rows[index], status='failed', reason=error.message)
if any(row.status == 'failed' for row in rows):
if any(row.status == 'failed' for row in rows) and not workspace_mode:
return _report(rows)
notify(f'BOM 表格校验通过,共 {len(rows)} 行、涉及 {len(grouped)} 个工程;开始核验工程位号。')
previews = []
inspection_projects, library_issues, library_cache = [], [], {}
for position, (key, indices) in enumerate(grouped.items(), 1):
check_cancelled()
if workspace_mode and any(rows[index].status == 'failed' for index in indices):
for index in indices:
if rows[index].status == 'unchecked':
rows[index] = replace(rows[index], status='failed', reason='本工程其他行未通过校验,工程整体保留待处理')
continue
task, project = owners[key]
def progress(message, phase=None):
notify(f'工程 {position}/{len(grouped)} · {message}', phase)
progress(f'正在核验「{task.name}」的位号库存…', BomImportPhase.INVENTORY)
baseline = tuple(service._store.list_target_plan(task.task_id))
requested = tuple(dict.fromkeys(records[index].old for index in indices))
if workspace_mode:
# Full inventory is necessary to see duplicate refdes whose current
# part number is neither the old nor the target part number.
requested = tuple(dict.fromkeys((*requested, *(records[index].new for index in indices), '*')))
known = tuple(dict.fromkeys(row.target.old_part_number for row in baseline))
if baseline and {part.casefold() for part in requested} <= {part.casefold() for part in known}:
if not workspace_mode and baseline and {part.casefold() for part in requested} <= {part.casefold() for part in known}:
progress(f'「{task.name}」复用已有位号库存,无需启动 CAD')
inventory = tuple(row.target for row in baseline)
else:
@@ -289,14 +308,17 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
for index, row in enumerate(rows):
if row.status == 'unchecked':
rows[index] = replace(row, reason='前序工程采集异常,已停止后续核验')
partial_preview = (BomBatchPreview(task_id, _identity(scope), tuple(previews), True)
if workspace_mode and workspace_tasks and previews else None)
report = _report(rows, errors=(
f'已停止批量采集:{task.name};{error.message}(错误编号:{error.error_id})',))
f'已停止批量采集:{task.name};{error.message}(错误编号:{error.error_id})',),
preview=partial_preview)
raise BomInspectionError(error, report) from error
check_cancelled()
progress(f'「{task.name}」正在匹配 {len(indices)} 行 BOM 与工程位号…', BomImportPhase.MATCHING)
merged = merge_target_inventory(baseline, inventory)
merged = tuple(TargetPlanRow(target) for target in inventory) if workspace_mode else merge_target_inventory(baseline, inventory)
matched_rows, touched = {}, {}
matcher = BomMatcher(merged, service.rules(task.task_id)) if merged else None
matcher = BomMatcher(merged, service.rules(task.task_id), allow_target=workspace_mode) if merged else None
for index in indices:
check_cancelled()
try:
@@ -308,7 +330,9 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
for target_key, row in matched.items():
touched.setdefault(target_key, []).append(index)
matched_rows[target_key] = row
rows[index] = replace(rows[index], status='passed', reason='校验通过')
skipped = sum(r.target.old_part_number.casefold() == r.new_part_number.casefold() for r in matched.values())
reason = f'校验通过;{skipped} 个器件已是目标料号,自动跳过' if skipped else '校验通过'
rows[index] = replace(rows[index], status='passed', reason=reason)
except TaskCenterError as error:
rows[index] = replace(rows[index], status='failed', reason=error.message)
for target, indices_for_target in touched.items():
@@ -323,9 +347,10 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
for index in indices:
if rows[index].status == 'passed':
rows[index] = replace(rows[index], status='failed', reason=error.message)
previews.append(BomProjectPreview(task.task_id, project.path, baseline, inventory, combined))
if not workspace_mode or all(rows[index].status == 'passed' for index in indices):
previews.append(BomProjectPreview(task.task_id, project.path, baseline, inventory, combined, workspace_mode))
progress(f'「{task.name}」校验完成')
check_cancelled()
preview = None if any(row.status != 'passed' for row in rows) else BomBatchPreview(
task_id, _identity(scope), tuple(previews))
return _report(rows, preview)
preview = (BomBatchPreview(task_id, _identity(scope), tuple(previews), bool(workspace_mode and workspace_tasks)) if previews and
(workspace_mode or all(row.status == 'passed' for row in rows)) else None)
return _report(rows, preview, inspection_projects=inspection_projects, library_issues=library_issues)
@@ -163,12 +163,14 @@ def parse_bom_row(cells, positions):
class BomMatcher:
"""Index one immutable inventory once, then match independent Excel rows."""
def __init__(self, baseline, rules=()):
def __init__(self, baseline, rules=(), *, allow_target=False):
if not baseline:
raise TaskCenterError('target_inventory', '未采集到可匹配的工程位号')
self.by_key = {}
self.projects = set()
self.by_identity = {}
self.by_refdes = {}
self.allow_target = allow_target
self.cell_defaults = {(rule.old_part_number.casefold(), rule.new_part_number): rule.target_cell
for rule in rules if rule.enabled}
self.identities = ProjectIdentityResolver()
@@ -180,22 +182,28 @@ class BomMatcher:
self.by_key[key] = row
self.projects.add(key[0])
self.by_identity.setdefault((target.old_part_number.casefold(), target.refdes.casefold()), []).append((key, target))
self.by_refdes.setdefault(target.refdes.casefold(), []).append((key, target))
def match_record(self, record):
if record.project_path and self.identities.project(record.project_path) not in self.projects:
raise TaskCenterError('bom_import', 'BOM 工程与当前任务不一致')
matched = {}
for refdes in record.refs:
candidates = [(key, target) for key, target in self.by_identity.get((record.old.casefold(), refdes.casefold()), ())
pool = (self.by_refdes.get(refdes.casefold(), ()) if self.allow_target else
self.by_identity.get((record.old.casefold(), refdes.casefold()), ()))
candidates = [(key, target) for key, target in pool
if (not record.schematic or target.schematic_name.casefold() == record.schematic.casefold())
and (not record.sheet or target.sheet_number.casefold() == record.sheet.casefold())
and (not record.uid or target.uid == record.uid)]
if len(candidates) != 1:
raise TaskCenterError('bom_import', f'位号 {refdes} 无法唯一匹配当前采集清单')
key, target = candidates[0]
already_target = self.allow_target and target.old_part_number.casefold() == record.new.casefold()
if self.allow_target and not already_target and target.old_part_number.casefold() != record.old.casefold():
raise TaskCenterError('bom_import', f'位号 {refdes} 当前料号为 {target.old_part_number},不是旧料号或目标新料号')
if key in matched:
raise TaskCenterError('bom_import', f'位号 {refdes} 在 BOM 中重复')
matched[key] = TargetPlanRow(target, record.enabled, record.new,
matched[key] = TargetPlanRow(target, record.enabled and not already_target, record.new,
bool(record.custom), record.custom, record.sync_nc,
(self.by_key[key].target_cell if self.by_key[key].new_part_number == record.new else '')
or self.cell_defaults.get((target.old_part_number.casefold(), record.new), ''))
@@ -46,8 +46,16 @@ def _write_json(path, value):
temporary.replace(path)
def _hash(path):
def _hash(path, *, stop_event=None):
with path.open('rb') as stream:
if stop_event is not None:
from .operation_control import check_cancelled
digest = hashlib.sha256()
while chunk := stream.read(1024 * 1024):
check_cancelled(stop_event)
digest.update(chunk)
check_cancelled(stop_event)
return digest.hexdigest()
return hashlib.file_digest(stream, 'sha256').hexdigest()
@@ -55,7 +63,7 @@ def _hidden(path):
return path.name.startswith('.') or bool(getattr(path.stat(), 'st_file_attributes', 0) & 2)
def _library_manifest(library):
def _library_manifest(library, *, stop_event=None):
"""Complete proven clone recipe: all root metadata and every physical PDB/CEL."""
library = Path(library).resolve()
if library.suffix.lower() != '.lmc' or not library.is_file():
@@ -86,10 +94,13 @@ def _library_manifest(library):
_fail(f'missing physical dependency: {name}')
manifest = {}
for path in sorted(paths, key=lambda p: p.relative_to(root).as_posix()):
if stop_event is not None:
from .operation_control import check_cancelled
check_cancelled(stop_event)
if path.is_symlink() or getattr(path.lstat(), 'st_file_attributes', 0) & 0x400:
_fail('library dependency cannot be a reparse point')
before = path.stat()
digest = _hash(path)
digest = _hash(path, **({'stop_event': stop_event} if stop_event is not None else {}))
after = path.stat()
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
_fail('source library changed while fingerprinting')
@@ -103,8 +114,8 @@ def _fingerprint(manifest):
def _content_fingerprint(manifest):
# LM normally touches the copied cfg timestamp while opening. Source proof
# and prelaunch copy proof include timestamps; post-open proof binds bytes.
# Copying between file systems may round timestamps. Content identity
# still requires the exact file list, size and SHA256 for every file.
return _fingerprint({name: {key: value for key, value in row.items() if key != 'mtime_ns'}
for name, row in manifest.items()})
@@ -117,8 +128,19 @@ def _clone_library(library, root, manifest):
target.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(library.parent / name, target)
clone = destination / library.name
if _library_manifest(clone) != manifest or _library_manifest(library) != manifest:
_fail('source library changed or clone dependency proof failed')
copied, current = _library_manifest(clone), _library_manifest(library)
def differences(actual):
return [name for name in sorted(set(manifest) | set(actual))
if name not in manifest or name not in actual or any(
manifest[name].get(key) != actual[name].get(key) for key in ('size', 'sha256'))]
clone_changes, source_changes = differences(copied), differences(current)
_write_json(root / 'clone-proof.json', {'source': str(library), 'before': manifest,
'clone': copied, 'source_after': current, 'clone_content_differences': clone_changes,
'source_content_differences': source_changes})
if source_changes:
_fail('复制期间中心库文件内容发生变化,请关闭库编辑后重试:' + '、'.join(source_changes[:8]))
if clone_changes:
_fail('中心库副本内容校验失败,请检查磁盘和文件权限:' + '、'.join(clone_changes[:8]))
return clone
@@ -153,7 +175,10 @@ def _catalog_from_evidence(evidence, library, part, partition, fingerprint, path
def _require_cleanup(cleanup):
if not isinstance(cleanup, dict) or cleanup.get('errors') or not cleanup.get('desktop_handle_closed') or not cleanup.get('desktop_confirmation', {}).get('released'):
confirmation = cleanup.get('desktop_confirmation', {}) if isinstance(cleanup, dict) else {}
if (not isinstance(cleanup, dict) or cleanup.get('errors')
or not cleanup.get('desktop_handle_closed')
or not (confirmation.get('released') or confirmation.get('release_accepted'))):
_fail('catalog private desktop cleanup was not proven')
if not cleanup.get('processes'):
_fail('catalog owned process cleanup is missing')
@@ -241,6 +266,75 @@ def read_library_cell_catalog(library_path: Path, part_number: str, evidence_roo
raise
def read_library_part_presence(library_path, part_numbers, evidence_root, *, allow_cold_read=True, stop_event=None):
"""Batch existence query, without requiring Cell associations or mutating the source."""
library = Path(library_path).resolve()
requested = tuple(dict.fromkeys(part_numbers))
if not requested:
return {}
evidence_root = Path(evidence_root).resolve()
if evidence_root == library.parent or library.parent in evidence_root.parents:
_fail('catalog evidence must be outside the source library')
from .operation_control import check_cancelled
check_cancelled(stop_event)
manifest = _library_manifest(library, **({'stop_event': stop_event} if stop_event is not None else {}))
content = _content_fingerprint(manifest)
cache = evidence_root / 'presence-cache' / (hashlib.sha256(_json_bytes([str(library), content])).hexdigest() + '.json')
previous_values = {}
if cache.is_file():
try:
index = json.loads(cache.read_text(encoding='utf-8'))
evidence = (evidence_root / index['evidence']).resolve()
evidence.relative_to(evidence_root)
if _hash(evidence) != index['sha256']:
raise ValueError('changed part evidence')
record = json.loads(evidence.read_text(encoding='utf-8'))
values = record['parts']
_require_cleanup(record['cleanup'])
if (record.get('library') != str(library) or record.get('source_content_fingerprint') != content
or record.get('schema') != 1 or not isinstance(values, dict)
or any(value not in {'found', 'missing', 'ambiguous'} for value in values.values())):
raise ValueError('invalid part evidence binding')
if _content_fingerprint(_library_manifest(library, **({'stop_event': stop_event} if stop_event is not None else {}))) != content:
_fail('中心库在校验期间发生变化,请重新导入 BOM。')
previous_values = values
if set(requested) <= set(values):
return {part: values[part] for part in requested}
except (OSError, ValueError, KeyError, TypeError, AttributeError):
check_cancelled(stop_event)
pass
if not allow_cold_read:
_fail('中心库已变化、目标料号未校验或校验证据失效,请重新导入 BOM 后保存;保存不会重新启动中心库扫描。')
root = evidence_root / 'part-checks' / uuid.uuid4().hex
root.mkdir(parents=True)
started = False
try:
clone = _clone_library(library, root, manifest)
started = True
result = _run_isolated_reader(clone, list(requested), None, root)
_require_cleanup(result.get('cleanup'))
if (_content_fingerprint(_library_manifest(library)) != _content_fingerprint(manifest) or result.get('library') != str(clone)
or not result.get('verified') or not result.get('quit_verified')):
_fail('library part check identity or consistency could not be verified')
values = result.get('parts')
if not isinstance(values, dict) or set(values) != set(requested) or any(
value not in {'found', 'missing', 'ambiguous'} for value in values.values()):
_fail('library part check returned incomplete results')
proof_path = root / 'verified-parts.json'
_write_json(proof_path, {'schema': 1, 'library': str(library),
'source_fingerprint': _fingerprint(manifest), 'source_content_fingerprint': content,
'parts': previous_values | values, 'cleanup': result['cleanup']})
finally:
cleanup = root / 'desktop-cleanup.json'
if started and cleanup.is_file():
_require_cleanup(json.loads(cleanup.read_text(encoding='utf-8')))
_dispose_clone(root)
elif not started:
_dispose_clone(root)
_write_json(cache, {'evidence': proof_path.relative_to(evidence_root).as_posix(), 'sha256': _hash(proof_path)})
return values
def project_library_path(project: Path) -> Path:
project = Path(project).resolve()
try:
@@ -112,6 +112,21 @@ def _read_associations(library, part_number, selected_partition, root):
'part_partition_count': int(parts.Count)}
def _read_part_presence(library, requested, root):
found = {part: [] for part in requested}
parts = library.Partitions(1)
for index in range(1, int(parts.Count) + 1):
partition = parts.Item(index)
name = str(partition.Name)
_physical(root, 'PartsDBLibs', name, '.pdb')
for position in range(1, int(partition.Count) + 1):
part = str(partition.Item(position).Name)
if part in found:
found[part].append(name)
return {'parts': {part: ('found' if len(rows) == 1 else 'missing' if not rows else 'ambiguous')
for part, rows in found.items()}}
def _verify_clone_content(clone, expected):
if _content_fingerprint(_library_manifest(clone)) != expected:
_fail('clone dependency content changed during library read')
@@ -151,7 +166,10 @@ def run(request_path: Path) -> int:
_verify_clone_content(clone, request['clone_content_fingerprint'])
_verify_app(app, request)
report['phase'] = 'associations'
result = _read_associations(library, request['part_number'], request['partition'], clone.parent)
if isinstance(request['part_number'], list):
result = _read_part_presence(library, request['part_number'], clone.parent)
else:
result = _read_associations(library, request['part_number'], request['partition'], clone.parent)
_verify_app(app, request)
_verify_clone_content(clone, request['clone_content_fingerprint'])
report.update(result, library=str(clone), desktop=request['desktop'], pid=request['pid'], verified=True, phase='complete')
@@ -0,0 +1,26 @@
"""Load generated COM bindings without requiring a writable frozen gen_py cache."""
from io import BytesIO, TextIOWrapper
import sys
from types import ModuleType
def registered_api(typelib_id, *, required=()):
import pythoncom
from win32com.client import genpy
typelib = pythoncom.LoadRegTypeLib(typelib_id, 1, 0, 0)
buffer = BytesIO()
stream = TextIOWrapper(buffer, encoding='utf-8')
genpy.Generator(typelib, None, genpy.GeneratorProgress()).generate(stream)
module = ModuleType('_dx_registered_' + typelib_id.strip('{}').replace('-', '_'))
sys.modules[module.__name__] = module
try:
stream.flush()
exec(compile(buffer.getvalue().decode('utf-8'), '<registered COM type library>', 'exec'), module.__dict__)
for name in required:
if not hasattr(module, name):
raise RuntimeError(f'DxDesigner COM 接口不兼容,缺少 {name}')
except BaseException:
sys.modules.pop(module.__name__, None)
raise
return module
@@ -12,12 +12,15 @@ from .path_access import canonical_path, internal_path
MAX_FILE_BYTES = 4 * 1024 * 1024
MAX_BUNDLE_BYTES = 32 * 1024 * 1024
MAX_FILES = 512
MAX_FILES = 4096
_BATCH_NAMES = {'session.json', 'supervisor.json', 'request.json', 'result.json',
'business-result.json', 'supervisor.log', 'resource-release.log', 'cancel.json'}
_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'}
'project-session.json', 'project-checkpoint.json', 'status.tsv',
'read-state.json', 'read-trace.jsonl', 'read-trace.jsonl.1', 'read-trace.jsonl.2',
'native-inventory.json', 'worker-progress.json', 'worker-output.txt', 'project-read-release.json',
'quick-part-replace.log'}
_SECRET_KEY = re.compile(r'(?:password|passwd|(?:client[_-]?)?secret|credentials?|authorization|api[_-]?key|access[_-]?token|refresh[_-]?token|token)', re.I)
_USER_KEY = re.compile(r'(?:user(?:[_-]?name)?|login[_-]?name)', re.I)
_SECRET_TEXT = re.compile(r'''(?ix)(\b(?:password|passwd|(?:client[_-]?)?secret|credentials?|authorization|api[_-]?key|access[_-]?token|refresh[_-]?token|token)\b["']?\s*[=:]\s*)(?:"[^"\r\n]*"|'[^'\r\n]*'|[^\s,;}]+)''')
@@ -51,6 +54,12 @@ def _scrub_json(value):
def _allowed(parts):
if parts[-1].endswith('.json') and ((len(parts) == 2 and parts[0] == 'discovery-checks')
or (len(parts) == 4 and parts[0] == 'tasks' and parts[2] == 'discovery-checks')):
return True
if len(parts) == 4 and parts[0] == 'cell-catalog' and parts[1] in {'part-checks', 'reads'}:
return parts[-1] in {'clone-proof.json', 'failure.json', 'desktop-cleanup.json',
'worker-result.json', 'verified-parts.json', 'source-hashes.json'}
if len(parts) == 2 and parts[0] == 'logs':
return bool(re.fullmatch(r'[^/]+\.log(?:\.\d+)?', parts[-1]))
if len(parts) == 3 and parts[0] == 'batch-resources':
@@ -68,6 +77,33 @@ def _allowed(parts):
return False
def _recent_task_activity(path):
"""Rank recent task evidence without following links or junctions."""
stamps = []
def inspect(candidate, *, directory=False):
try:
info = candidate.lstat()
if stat.S_ISLNK(info.st_mode) or getattr(info, 'st_file_attributes', 0) & 0x400:
return False
stamps.append(info.st_mtime_ns)
return stat.S_ISDIR(info.st_mode) if directory else True
except OSError:
return False
inspect(path)
inspect(path / 'events.jsonl')
if inspect(path / 'quick-run', directory=True):
inspect(path / 'quick-run' / 'read-state.json')
history = path / 'quick-run-history'
if inspect(history, directory=True):
try:
for child in history.iterdir():
if inspect(child, directory=True):
inspect(child / 'read-state.json')
except OSError:
pass
return max(stamps, default=0)
def export_diagnostic_bundle(data_root, destination: Path) -> Path:
"""Write a new ZIP, excluding links, binaries, inputs and credential configs.
@@ -77,7 +113,7 @@ def export_diagnostic_bundle(data_root, destination: Path) -> Path:
root, destination = canonical_path(data_root), canonical_path(destination)
if destination.suffix.lower() != '.zip':
raise ValueError('diagnostic destination must have a .zip suffix')
manifest = {'version': 1, 'included': [], 'omissions': [], 'unknowns': [
manifest = {'version': 1, 'included': [], 'omissions': [], 'truncated': [], 'unknowns': [
'External desktop handle holders are unknown; empty window/thread lists do not prove release.',
'Active logs and metadata are point-in-time reads; no source CAD, libraries, BOM, database or environment configuration is included.'],
'limits': {'max_file_bytes': MAX_FILE_BYTES, 'max_bundle_bytes': MAX_BUNDLE_BYTES, 'max_files': MAX_FILES}}
@@ -94,10 +130,35 @@ def export_diagnostic_bundle(data_root, destination: Path) -> Path:
info = entry.lstat()
if stat.S_ISLNK(info.st_mode) or getattr(info, 'st_file_attributes', 0) & 0x400:
manifest['omissions'].append({'path': _scrub_text(relative), 'reason': 'link_or_reparse_point'})
elif base == physical and name not in {'logs', 'batch-resources', 'attempts', 'tasks', 'queue', 'queues', 'task-queues', 'quick-run', 'quick-run-history'}:
elif base == physical and name not in {'logs', 'batch-resources', 'attempts', 'tasks', 'queue', 'queues', 'task-queues', 'quick-run', 'quick-run-history', 'cell-catalog', 'discovery-checks'}:
manifest['omissions'].append({'path': _scrub_text(relative), 'reason': 'outside_allowlist'})
elif relative.startswith('cell-catalog/') and name == 'library':
manifest['omissions'].append({'path': _scrub_text(relative), 'reason': 'library_input_excluded'})
else:
safe_folders.append(name)
if base == physical:
priority = {'logs': 0, 'tasks': 1, 'quick-run-history': 2, 'batch-resources': 3}
safe_folders.sort(key=lambda name: (priority.get(name, 3), name))
elif base == physical / 'tasks':
safe_folders.sort(key=lambda name: _recent_task_activity(base / name), reverse=True)
elif base == physical / 'batch-resources':
def batch_activity(name):
try:
return (base / name / 'supervisor.json').lstat().st_mtime_ns
except OSError:
try:
return (base / name).lstat().st_mtime_ns
except OSError:
return 0
safe_folders.sort(key=batch_activity, reverse=True)
elif base.name == 'quick-run-history':
# Keep the latest failed parse before old evidence exhausts the cap.
def modified(name):
try:
return (base / name).lstat().st_mtime_ns
except OSError:
return 0
safe_folders.sort(key=modified, reverse=True)
folders[:] = safe_folders
for name in sorted(files):
path = base / name
@@ -106,21 +167,28 @@ def export_diagnostic_bundle(data_root, destination: Path) -> Path:
reason = None
try:
info = path.lstat()
tail = (relative.parts == ('logs', 'application.log')
and info.st_size > MAX_FILE_BYTES and MAX_FILE_BYTES >= 1024)
read_size = min(info.st_size, min(MAX_FILE_BYTES, 1024 * 1024)) if tail else info.st_size
if not _allowed(relative.parts):
reason = 'outside_allowlist'
elif stat.S_ISLNK(info.st_mode) or getattr(info, 'st_file_attributes', 0) & 0x400 or info.st_nlink != 1:
reason = 'link_or_reparse_point'
elif not stat.S_ISREG(info.st_mode):
reason = 'not_regular_file'
elif info.st_size > MAX_FILE_BYTES or total + info.st_size > MAX_BUNDLE_BYTES or len(manifest['included']) >= MAX_FILES:
elif (info.st_size > MAX_FILE_BYTES and not tail) or total + read_size > MAX_BUNDLE_BYTES or len(manifest['included']) >= MAX_FILES:
reason = 'size_or_count_limit'
else:
with path.open('rb') as source:
payload = source.read(MAX_FILE_BYTES + 1)
if tail:
source.seek(-read_size, os.SEEK_END)
payload = source.read(MAX_FILE_BYTES + 1 if not tail else read_size)
if tail and b'\n' in payload:
payload = payload.split(b'\n', 1)[1]
if len(payload) > MAX_FILE_BYTES or b'\x00' in payload:
reason = 'binary_or_size_limit'
else:
text = payload.decode('utf-8-sig')
text = payload.decode('utf-8-sig', errors='replace' if tail else 'strict')
if relative.suffix == '.json':
text = json.dumps(_scrub_json(json.loads(text)), ensure_ascii=False, indent=2)
elif '.jsonl' in relative.name:
@@ -131,6 +199,9 @@ def export_diagnostic_bundle(data_root, destination: Path) -> Path:
archive.writestr(label, text)
total += len(payload)
manifest['included'].append(label)
if tail:
manifest['truncated'].append({'path': label, 'original_bytes': info.st_size,
'included_tail_bytes': len(payload)})
except (OSError, UnicodeError, ValueError):
reason = 'unreadable_or_invalid_text'
if reason:
@@ -100,8 +100,29 @@ def _unsafe(info):
def _without_runtime_database(stamp):
if stamp is None or len(stamp) != 2:
return None
return (stamp[0], tuple(row for row in stamp[1]
if row[0].replace('\\', '/').casefold() != 'database/icdb.dat'))
return (stamp[0], tuple((row[0], row[3], row[-1]) for row in stamp[1]
if row[0].replace('\\', '/').casefold() not in {
'database/icdb.dat', 'database/cdbsvr/saddress.adr'}))
def _same_content(before, after):
if before is None or after is None or len(before) != 2 or len(after) != 2:
return False
return before[0] == after[0] and tuple((r[0], r[3], r[-1]) for r in before[1]) == tuple(
(r[0], r[3], r[-1]) for r in after[1])
def _fingerprint_difference(before, after):
if before is None or after is None or len(before) != 2 or len(after) != 2:
return {'unverifiable': True, 'content_changed': [], 'metadata_changed': []}
old, new = ({r[0]: r for r in value[1]} for value in (before, after))
content, metadata = [], []
for name in sorted(set(old) | set(new)):
if name not in old or name not in new or (old[name][3], old[name][-1]) != (new[name][3], new[name][-1]):
content.append(name)
elif old[name] != new[name]:
metadata.append(name)
return {'unverifiable': False, 'content_changed': content, 'metadata_changed': metadata}
def _only_database_changed(before, after):
@@ -191,7 +212,7 @@ class DiscoveryCache:
except (OSError, ValueError, TypeError):
pass # Cache persistence is optional, unlike execution write-ahead proof.
def discover(self, project, parts, collect_components, loader, *, stop_event=None):
def discover(self, project, parts, collect_components, loader, *, stop_event=None, release_unverifiable=None):
with self.lock:
check_cancelled(stop_event)
def fingerprint():
@@ -223,7 +244,9 @@ class DiscoveryCache:
previous = None
previous_design = None
attempt, limit = 0, 2
last_released = False
while attempt < limit:
last_released = False
if attempt:
before = fingerprint()
check_cancelled(stop_event)
@@ -231,7 +254,23 @@ class DiscoveryCache:
check_cancelled(stop_event)
identity = _discovery_identity(discovery)
after = fingerprint()
if before is not None and before == after:
if after is None and release_unverifiable is not None:
# A positively checkpointed read-only owned CAD may retain
# an exclusive iCDB handle even after closing the project.
# Release it before retrying the full fingerprint; never
# treat an unreadable database as equal or cacheable.
if release_unverifiable(project):
last_released = True
check_cancelled(stop_event)
after = fingerprint()
difference = _fingerprint_difference(before, after)
try:
from .batch_resources import write_record
write_record(self.data_root / 'discovery-checks' / (hashlib.sha256(key.encode()).hexdigest() + '.json'),
{'project': str(project), 'attempt': attempt + 1, **difference})
except OSError:
pass
if _same_content(before, after):
break
if _only_database_changed(before, after):
design = _without_runtime_database(after)
@@ -246,6 +285,18 @@ class DiscoveryCache:
previous = previous_design = None
attempt += 1
else:
# A readable fingerprint that keeps changing is just as
# unverifiable as an unreadable one. If this read-only CAD
# session has positive release evidence, close it before
# allowing the batch importer to skip this project.
if not last_released and release_unverifiable is not None:
last_released = bool(release_unverifiable(project))
if last_released:
from .project_discovery import ReleasedProjectReadError
raise ReleasedProjectReadError('quick_run_sheet_discovery',
'工程读取后无法确认一致,已跳过;后台读取会话已释放。' +
('无法完整读取工程文件指纹,请检查文件占用及读取权限。' if difference['unverifiable'] else
'内容发生变化的文件:' + '、'.join(difference['content_changed'][:8])))
raise TaskCenterError("quick_run_sheet_discovery",
"project contents changed or could not be verified during discovery; retry read-only discovery")
# Refuse incomplete inventories and bound both projects and row count.
@@ -14,6 +14,9 @@ class ExecutionFeedback:
stage: str = ''
stage_seconds: float = 0.0
waiting_for_safe_completion: bool = False
stage_done: int = 0
stage_total: int = 0
last_update_seconds: float = 0.0
def read_feedback(task_id, position, total, message, root: Path, started_after: float):
@@ -29,10 +32,14 @@ def read_feedback(task_id, position, total, message, root: Path, started_after:
pass
path = root / 'quick-run' / 'worker-progress.json'
stage, seconds, waiting = '', 0.0, False
done, total_units, last_update = 0, 0, 0.0
try:
modified = path.stat().st_mtime
if modified >= started_after:
record = json.loads(path.read_text(encoding='utf-8'))
done = max(0, int(record.get('done', 0)))
total_units = max(0, int(record.get('total', 0)))
last_update = max(0, time.time() - modified)
waiting = record.get('waiting_for_safe_completion') is True
candidate = record.get('stage', '')
if candidate and candidate != 'finished':
@@ -40,4 +47,4 @@ def read_feedback(task_id, position, total, message, root: Path, started_after:
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, waiting)
return ExecutionFeedback(task_id, position, total, message, stage, seconds, waiting, done, total_units, last_update)
@@ -28,6 +28,8 @@ class PrivateCadIdentityError(PrivateCadStartupError, CadIdentityConflict):
PRIVATE_CAD_IDENTITY_EXIT_CODE = 4
READ_ONLY_STARTUP_TIMEOUT_SECONDS = 45
READ_ONLY_QUIT_TIMEOUT_SECONDS = 5
def is_identity_conflict(error):
@@ -87,7 +89,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, stop_control=None):
def __init__(self, root, *, abort_event=None, desktop=None, stop_control=None, read_only=False):
self.root = canonical_path(root)
self.token = uuid4().hex
self.borrowed_desktop = desktop is not None
@@ -97,6 +99,7 @@ class IsolatedCadSession:
self.started = time.monotonic()
self.abort_event = abort_event
self.stop_control = stop_control
self.read_only = read_only
self.record = {'desktop': self.desktop.name, 'token': self.token, 'state': 'starting'}
def _record(self, **values):
@@ -126,7 +129,7 @@ class IsolatedCadSession:
probe = self.desktop.start_process(_helper_command('probe', bootstrap, self.cad.pid), env=_child_environment(environment),
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
deadline = time.monotonic() + 120
deadline = time.monotonic() + (READ_ONLY_STARTUP_TIMEOUT_SECONDS if self.read_only else 120)
while probe.poll() is None:
self._check_abort()
cad_exit = self.cad.poll()
@@ -235,7 +238,11 @@ class IsolatedCadSession:
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,
# Read-only shutdown faults remain diagnostics, without presenting
# a write/save decision that an import caller cannot answer.
record_failure = (lambda message, **details: self._record(
state='closing', error=message, read_only=True, **details)) if self.read_only else self._uncertain
diagnose('uncertain_record', lambda: record_failure(message,
cleanup_errors=cleanup_errors, diagnostic_errors=diagnostic_errors,
clean=False, orderly_shutdown=self.record['orderly_shutdown'],
shutdown_seconds=time.monotonic() - closing_started,
@@ -252,11 +259,11 @@ class IsolatedCadSession:
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)
quit_code = closing.wait(timeout=20)
quit_code = closing.wait(timeout=READ_ONLY_QUIT_TIMEOUT_SECONDS if self.read_only else 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)
cad_code = self.cad.wait(timeout=READ_ONLY_QUIT_TIMEOUT_SECONDS if self.read_only else 15)
exit_codes['cad_exit_code'] = cad_code
if cad_code != 0:
raise WorkerMonitorError('owned CAD returned a nonzero exit code after quit')
@@ -268,7 +275,7 @@ class IsolatedCadSession:
# 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:
if not self.read_only and 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
@@ -304,6 +311,28 @@ class IsolatedCadSession:
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 self.read_only:
confirmation = desktop_cleanup.get('desktop_confirmation') or {}
released = ((desktop_cleanup.get('workers_closed') is True
and not desktop_cleanup.get('errors')) if self.borrowed_desktop else
(desktop_cleanup.get('desktop_handle_closed') is True
and not desktop_cleanup.get('errors')
and (confirmation.get('released') is True
or confirmation.get('release_accepted') is True)))
allowed = {'orderly_shutdown', 'forced_cleanup'}
if (released and not diagnostic_errors
and all(item['stage'] in allowed for item in cleanup_errors)):
diagnose('released_record', lambda: self._record(
state='session_closed' if self.borrowed_desktop else 'released',
desktop_retained_by_batch=self.borrowed_desktop, read_only=True,
read_only_released=True, clean=not cleanup_errors,
shutdown_seconds=time.monotonic() - closing_started,
desktop_cleanup=desktop_cleanup, **exit_codes))
if not diagnostic_errors:
return
elif not released:
remember('read_release_confirmation', WorkerMonitorError(
'read-only owned resource release was not confirmed'))
if error is not None or failures:
persist_uncertain()
else:
@@ -65,6 +65,18 @@ class _Win32Api:
return self.service.CreateDesktop(name, 0, self.con.GENERIC_ALL, None)
def confirm_desktop_released(self, name: str):
return self._confirm_desktop_released(name, accept_empty_residual=False)
def confirm_desktop_released_or_empty(self, name: str):
"""Accept an empty desktop residue after its owned processes are gone.
The caller must first prove every owned process and native handle is
closed. A residual desktop with windows still follows the strict,
bounded confirmation path.
"""
return self._confirm_desktop_released(name, accept_empty_residual=True)
def _confirm_desktop_released(self, name: str, *, accept_empty_residual: bool):
started = time.monotonic()
# USER references can outlive the owned processes briefly. Allow a
# bounded grace period, without delaying the usual immediate release
@@ -83,6 +95,20 @@ class _Win32Api:
'caller_pid': os.getpid(), 'caller_thread_id': get_native_id()}
if not present:
return
if accept_empty_residual:
try:
windows = self._desktop_windows(name)
self.desktop_release_check.update(remaining_windows=windows,
desktop_present=True, release_accepted=not windows,
residual_only=not windows,
acceptance_reason='owned processes closed; no desktop windows remain' if not windows else None)
except Exception as error:
self.desktop_release_check['window_query_error'] = str(error)
windows = None
if windows == []:
# This does not claim the USER desktop object disappeared:
# only that no owned process or visible desktop work remains.
return
if time.monotonic() >= deadline:
try:
from .cad_identity import current_desktop_name
@@ -91,6 +117,9 @@ class _Win32Api:
self.desktop_release_check['caller_desktop_error'] = str(error)
try:
self.desktop_release_check['remaining_windows'] = self._desktop_windows(name)
self.desktop_release_check['desktop_present'] = True
self.desktop_release_check['release_accepted'] = False
self.desktop_release_check['residual_only'] = False
except Exception as error:
self.desktop_release_check['window_query_error'] = str(error)
raise TaskCenterError('private_desktop_cleanup', 'owned private desktop still exists after cleanup')
@@ -124,7 +153,7 @@ class _Win32Api:
return windows
def create_process(self, command: Sequence[str], desktop: str, cwd: Path | None, env: Mapping[str, str] | None,
*, stdout=None, stderr=None):
*, stdout=None, stderr=None, survive_parent_exit=False):
startup = self.process.STARTUPINFO()
startup.lpDesktop = desktop
flags = self.con.CREATE_NO_WINDOW | self.con.CREATE_SUSPENDED
@@ -146,8 +175,9 @@ class _Win32Api:
open_process=getattr(self, 'open_process', None))
process.stdout, process.stderr = streams
process.thread = thread
self.job.AssignProcessToJobObject(job, handle)
process.job_assigned = True
if not survive_parent_exit:
self.job.AssignProcessToJobObject(job, handle)
process.job_assigned = True
self.process.ResumeThread(thread)
thread.Close()
process.thread = None
@@ -615,7 +645,15 @@ class PrivateDesktop:
confirm = getattr(self._api, 'confirm_desktop_released', None)
if confirm and self._handle is None:
try:
confirm(self.name)
accept_empty = (not errors and not self._processes
and not getattr(self._api, '_pending_cleanup', ())
and all(self._process_cleanup_confirmed(item)
for item in self._process_history))
if accept_empty:
confirm_empty = getattr(self._api, 'confirm_desktop_released_or_empty', None)
(confirm_empty or confirm)(self.name)
else:
confirm(self.name)
except Exception as error:
errors.append(error)
self.cleanup_record.update(desktop_handle_closed=self._handle is None,
@@ -630,6 +668,16 @@ class PrivateDesktop:
self.cleanup_record['owned_resource_diagnostics'] = self._owned_resource_diagnostics()
_raise_cleanup_errors(errors)
@staticmethod
def _process_cleanup_confirmed(history):
cleanup = history.get('cleanup') if isinstance(history, dict) else None
return (isinstance(cleanup, dict)
and cleanup.get('process_handle_closed') is True
and cleanup.get('job_handle_closed') is True
and cleanup.get('job_active_processes', 0) == 0
and cleanup.get('unconfirmed_descendant_handles', 0) == 0
and not cleanup.get('errors'))
def _owned_resource_diagnostics(self):
"""Bounded queries of our handles only; never terminate/query foreign PIDs.
@@ -50,7 +50,9 @@ class _AliasPool:
if link not in self.links:
# Junction creation needs no symbolic-link privilege. The
# junction contains no copies and never changes the target.
_winapi.CreateJunction(str(root), str(link))
# CPython's junction helper expects a DOS path, not a \\?\
# target. An existing 8.3 name also avoids its MAX_PATH limit.
_winapi.CreateJunction(_short_path(root) or str(root), str(link))
self.links[link] = root
if not link.is_junction() or canonical_path(link, strict=True) != root:
raise OSError('compatibility directory no longer points to the confirmed evidence')
@@ -230,7 +230,7 @@ def wait_for(callback, description, timeout=15):
raise TimeoutError(description)
def wait_for_library_rows(callback, target, timeout=30, stable_for=0.45, poll_interval=0.15):
def wait_for_library_rows(callback, target, timeout=8, stable_for=0.45, poll_interval=0.15):
deadline = time.monotonic() + timeout
previous = None
stable_since = None
@@ -338,8 +338,8 @@ def attach():
try:
import viewdraw_api as api
except ImportError:
api = win32com.client.gencache.EnsureModule(
"{4ADEF4E1-690A-11CE-9261-0020C5E26659}", 0, 1, 0)
from .com_api import registered_api
api = registered_api("{4ADEF4E1-690A-11CE-9261-0020C5E26659}", required=('IVdApp', 'IVdComp'))
app = api.IVdApp(win32com.client.GetActiveObject("Viewdraw.Application")._oleobj_)
from .cad_identity import verify_application, project_file_path
verify_application(app)
@@ -511,11 +511,9 @@ def replacement_matches(components, records, target, require_part_label, *, libr
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 ''
if require_part_label and not part_label:
raise ValueError(
f"Native replacement Part Label readback failed; refdes={identity}; uid={candidate.UID}; "
"Part Label is required by the selected Value policy"
)
# A missing label is valid for parts without that library attribute.
# VBS restores the original Value snapshot; a recorded nonblank library
# label must still match below, so an actual lost attribute is rejected.
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}')
@@ -703,7 +701,17 @@ def run_native(snapshot, report):
def current_library_rows():
return [(row, [text.window_text() for text in row.children(control_type="Text")])
for row in desktop.window(handle=list_handle).descendants(control_type="ListItem")]
rows = wait_for_library_rows(current_library_rows, metadata[3])
report['library_lookup'] = {'target': metadata[3], 'state': 'loading', 'timeout_seconds': 8}
save_report(snapshot, report)
lookup_started = time.monotonic()
try:
rows = wait_for_library_rows(current_library_rows, metadata[3])
except TimeoutError:
report['library_lookup'].update(state='unavailable_or_timeout',
elapsed_seconds=round(time.monotonic() - lookup_started, 3))
raise
report['library_lookup'].update(state='loaded',
elapsed_seconds=round(time.monotonic() - lookup_started, 3))
row, columns = exact_target(rows, metadata[3])
report["library_row"] = columns
symbol_parts = columns[2].split(":", 1)
@@ -17,6 +17,7 @@ from .path_access import canonical_path, internal_path, retry_sharing_violation
from .isolated_desktop import PrivateDesktop
from .native_inventory_protocol import NativeInventoryUnsupported, read_ascii_discovery, read_native_text
from .operation_control import check_cancelled, report_progress
from .read_trace import ReadTrace
_RETRY_NATIVE_READ = object()
@@ -28,6 +29,10 @@ class _NativeInventoryProcessError(NativeInventoryUnsupported):
"""A launch, watchdog or explicit crash failure, separate from invalid input."""
class _NativeInventoryExitError(NativeInventoryUnsupported):
"""An unclassified nonzero read exit is not proof of unsupported capability."""
def _hash(path):
with internal_path(path).open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest()
@@ -89,6 +94,9 @@ class NativeInventoryReader:
text, block, snapshot, database = _project_options(project)
tools = self._tools()
except (NativeInventoryUnsupported, OSError, ImportError, UnicodeError, TaskCenterError) as error:
ReadTrace(self.data_root / 'quick-run-history' / ('native-preflight-' + uuid4().hex),
project=project, backend='native').emit('native_preflight', state='fallback',
error_type=type(error).__name__, error=str(error))
report_progress(on_progress, f'使用 CAD 核验位号:{str(error)[:160]}')
return None
for attempt in range(2):
@@ -110,7 +118,10 @@ class NativeInventoryReader:
desktop = (runtime.native_scope(canonical_path(evidence)) if runtime is not None
else self.desktop_factory('dx-cli-' + token, enabled=True))
record = {'backend': 'native_cli', 'project': str(project), 'state': 'reading',
'workspace': str(workspace), 'tools': []}
'workspace': str(workspace), 'tools': [], 'trace_root': str(evidence)}
trace = ReadTrace(evidence, project=project, backend='native')
trace.emit('snapshot_prepare', state='started', root_block=block, snapshot=snapshot,
timeout_seconds=self.timeout)
started = time.monotonic()
clean = False
result = None
@@ -133,6 +144,7 @@ class NativeInventoryReader:
raise NativeInventoryUnsupported('无法创建稳定的短路径数据库快照')
output = workspace / 'output'
output.mkdir()
trace.emit('snapshot_prepare', state='completed', source_hashes={str(p): h for p, h in source_hashes.items()})
commands = (
(str(tools[0]), '-project=' + str(source), '-icdb=' + str(target_database),
'-snap=' + snapshot, '-h', '-t', '-offline', '-file=' + str(output / 'scope.txt')),
@@ -165,10 +177,12 @@ class NativeInventoryReader:
_verify_sources(source_hashes)
check_cancelled(stop_event)
result = read_ascii_discovery(project, output / 'scope.txt', old_part_numbers,
expected_root=block, effective_report_path=output / 'effective.txt')
expected_root=block, effective_report_path=output / 'effective.txt', on_diagnostic=trace.emit)
record.update(state='ready', components=len(result.components), source_hashes_unchanged=True)
report_progress(on_progress, f'原生采集完成,已核对 {len(result.components)} 个目标位号及 UID。')
except (NativeInventoryUnsupported, OSError, UnicodeError) as error:
trace.emit('native_read_error', state='failed', error_type=type(error).__name__,
error=str(error), **getattr(error, 'details', {}))
if record['state'] == 'uncertain':
raise TaskCenterError('inventory_cleanup', f'原生采集清理状态不确定:{evidence}') from error
_verify_sources(source_hashes)
@@ -176,7 +190,27 @@ class NativeInventoryReader:
source_hashes={str(path): digest for path, digest in source_hashes.items()})
retry = False
cleanup = getattr(desktop, 'cleanup_record', None)
session_failure = runtime is not None and isinstance(error, _NativeInventoryProcessError)
session_failure = runtime is not None and (
isinstance(error, _NativeInventoryProcessError)
or getattr(runtime, 'read_only', False) and isinstance(error, _NativeInventoryExitError))
if session_failure and getattr(runtime, 'read_only', False):
if (not clean or not isinstance(cleanup, dict)
or cleanup.get('workers_closed') is not True or cleanup.get('errors')):
raise TaskCenterError('inventory_cleanup',
f'原生读取进程尚未确认释放,已停止后续采集:{evidence};{error}') from error
record.update(state='closing_failed_read', reason=str(error))
# A failed import read is never reopened. Close its shared
# desktop/CAD generation too; a scope receipt only certifies
# the native worker, and cannot authorize the next project.
runtime.close(reason='native_read_failure')
runtime.desktop = runtime.cad = None
runtime._active_evidence = None
record.update(state='project_unreadable', resources_released=True)
check_cancelled(stop_event)
from .project_discovery import ReleasedProjectReadError
raise ReleasedProjectReadError('quick_run_sheet_discovery',
f'工程损坏或无法读取,已跳过 / 无法处理:{project.name};'
f'后台读取会话已释放;诊断原因:{error}') from error
if (session_failure
and clean and isinstance(cleanup, dict) and cleanup.get('workers_closed') is True):
check_cancelled(stop_event)
@@ -201,6 +235,8 @@ class NativeInventoryReader:
else:
report_progress(on_progress, f'原生清单未能完整核对,改用 CAD 采集:{str(error)[:160]}')
except TaskCenterError as error:
trace.emit('native_read_error', state='cancelled' if error.code == 'operation_cancelled' else 'failed',
error_type=type(error).__name__, error_code=error.code, error=error.message)
if record['state'] == 'uncertain':
try:
(evidence / 'uncertain.flag').write_text(error.message, encoding='utf-8')
@@ -212,6 +248,8 @@ class NativeInventoryReader:
finally:
record.update(seconds=time.monotonic() - started,
cleanup=getattr(desktop, 'cleanup_record', None))
trace.emit('native_read_end', state=record['state'], reason=record.get('reason', ''),
seconds=record['seconds'], cleanup=record['cleanup'])
# All diagnostics happen after process/desktop cleanup, never in
# between an exit request and the wait that confirms it.
try:
@@ -232,6 +270,10 @@ class NativeInventoryReader:
log_name = name + '-' + report_name
started = time.monotonic()
metric = {'name': name, 'report': report_name, 'exit_code': None}
# Evidence lives outside the disposable CLI workspace and survives a hang.
trace = ReadTrace(record['trace_root'], project=record['project'], backend='native')
trace.emit('native_tool', state='starting', tool=name, report=report_name,
timeout_seconds=self.timeout)
record['tools'].append(metric)
process = None
try:
@@ -240,11 +282,17 @@ class NativeInventoryReader:
try:
process = desktop.start_process(command, cwd=workspace, env=environment,
stdout=stdout, stderr=stderr)
trace.emit('native_tool', state='running', tool=name, report=report_name, worker_pid=process.pid)
except OSError as error:
raise _NativeInventoryProcessError(f'{name} 启动失败:{error}') from error
deadline = started + self.timeout
next_wait_log = time.monotonic() + 5
while process.poll() is None:
check_cancelled(stop_event)
if time.monotonic() >= next_wait_log:
trace.emit('native_tool', state='waiting', tool=name, report=report_name,
worker_pid=process.pid, waiting_seconds=time.monotonic() - started)
next_wait_log = time.monotonic() + 5
if time.monotonic() >= deadline:
raise _NativeInventoryProcessError(f'{name} 原生读取超时')
time.sleep(0.05)
@@ -252,14 +300,18 @@ class NativeInventoryReader:
if metric['exit_code'] & 0xFFFFFFFF in _NATIVE_CRASH_CODES:
raise _NativeInventoryProcessError(f'{name} 原生读取进程崩溃:{metric["exit_code"]}')
if metric['exit_code'] != 0:
raise NativeInventoryUnsupported(f'{name} 返回退出码 {metric["exit_code"]}')
raise _NativeInventoryExitError(f'{name} 返回退出码 {metric["exit_code"]}')
finally:
primary_error = sys.exception()
metric['seconds'] = time.monotonic() - started
trace.emit('native_tool_exit', state='failed' if primary_error else 'completed',
tool=name, report=report_name, returncode=metric['exit_code'], seconds=metric['seconds'],
error_type=type(primary_error).__name__ if primary_error else '', error=str(primary_error or ''))
if process is not None and callable(getattr(process, 'close', None)):
try:
process.close()
except Exception as error:
trace.emit('native_tool_cleanup', state='failed', tool=name, error=str(error))
record.update(state='uncertain', cleanup_error=str(error),
primary_error=str(primary_error) if primary_error is not None else None)
failure = TaskCenterError('inventory_cleanup',
@@ -4,6 +4,8 @@ from __future__ import annotations
import os
from pathlib import Path
import re
import hashlib
from collections import Counter
from .quick_run_protocol import ProjectDiscovery
from .target_plan import ComponentTarget
@@ -12,9 +14,31 @@ from .target_plan import ComponentTarget
class NativeInventoryUnsupported(ValueError):
"""The native report cannot certify the existing inventory contract."""
def __init__(self, message, *, details=None):
super().__init__(message)
self.details = details or {}
_UID = re.compile(r'^\$\d+I\d+$')
_PROPERTY = re.compile(r'\tProp (Ref Designator|Part Number|Value)(?: (.*))?')
_SYMBOL_NAME = re.compile(r'\tSymbol name (\S+) type\s*')
def _named_signal_port(uid, symbol_name, refdes):
"""Named in/out port symbols are not individually addressable components."""
return (not refdes and bool(uid.strip()) and len(uid) <= 1024
and all(ord(character) >= 32 for character in uid)
and symbol_name.casefold() in {'in.1', 'out.1'})
def _uid_error(message, uid, text, line_number):
from .read_trace import describe_uid
lines = text.splitlines()
index = max(0, line_number - 1)
return NativeInventoryUnsupported(message, details={**describe_uid(uid),
'line_number': line_number, 'raw_line': lines[index] if index < len(lines) else '',
'context_lines': [{'line': n + 1, 'text': lines[n]} for n in range(
max(0, index - 2), min(len(lines), index + 7))]})
def read_native_text(path: Path) -> str:
@@ -41,34 +65,48 @@ def _effective_properties(text):
if len(re.findall(r'^Config .+', text, re.MULTILINE)) != 1:
raise NativeInventoryUnsupported('展开报告必须具有唯一配置')
result, attributes, paths = {}, {}, []
inconsistent_refs = set()
inconsistent_refs = {}
ports = Counter()
symbol = None
symbol_name = ''
symbol_name_count = 0
symbol_line = 0
def finish():
if symbol is None:
return
uid, ref = symbol
if not _UID.fullmatch(uid):
if (symbol_name_count == 1 and _named_signal_port(uid, symbol_name, ref) and
not attributes.get('Ref Designator') and paths == ['']):
ports[(uid.casefold(), symbol_name.casefold())] += 1
return
if attributes.get('Part Number'):
raise NativeInventoryUnsupported('展开报告的物料缺少唯一 UID')
raise _uid_error('展开报告的物料缺少唯一 UID', uid, text, symbol_line)
return
if uid in result or paths != ['']:
raise NativeInventoryUnsupported('展开报告含重复或嵌套实例,需由 CAD 核验')
raise _uid_error('展开报告含重复或嵌套实例,需由 CAD 核验', uid, text, symbol_line)
if ref != attributes.get('Ref Designator', ''):
inconsistent_refs.add(uid)
inconsistent_refs[uid] = {'header_refdes': ref,
'attribute_refdes': attributes.get('Ref Designator', ''),
'effective_line_number': symbol_line}
result[uid] = dict(attributes)
if len(result) > 100000:
raise NativeInventoryUnsupported('原生器件数量超过读取上限')
for line in text.splitlines():
for line_number, line in enumerate(text.splitlines(), 1):
if line.startswith(' Symbol '):
finish()
symbol_line = line_number
match = re.fullmatch(r' Symbol (.+?)\tref des ([^\t]*)\tpage [^\t]*', line)
if match is None:
raise NativeInventoryUnsupported('展开报告含不明确的器件身份')
symbol, attributes, paths = match.groups(), {}, []
raise _uid_error('展开报告含不明确的器件身份', '', text, symbol_line)
symbol, attributes, paths, symbol_name, symbol_name_count = match.groups(), {}, [], '', 0
elif symbol is not None and line.startswith('\tPath '):
paths.append(line[6:])
elif symbol is not None and _SYMBOL_NAME.fullmatch(line):
symbol_name = _SYMBOL_NAME.fullmatch(line).group(1)
symbol_name_count += 1
elif symbol is not None:
match = _PROPERTY.fullmatch(line)
if match:
@@ -77,35 +115,56 @@ def _effective_properties(text):
raise NativeInventoryUnsupported('展开报告的器件属性不唯一')
attributes[key] = value or ''
finish()
return result, inconsistent_refs
return result, inconsistent_refs, ports
def _parse_scope(text, requested, effective, inconsistent_refs):
def _parse_scope(text, requested, effective, inconsistent_refs, expected_ports):
blocks, sheets, targets = set(), set(), set()
seen_uids = set()
block, symbol, attributes = '', None, {}
symbol_line = 0
symbol_name = ''
symbol_name_count = 0
ports = Counter()
def finish():
if symbol is None:
return
uid, sheet = symbol
if not _UID.fullmatch(uid):
if symbol_name_count == 1 and _named_signal_port(uid, symbol_name, attributes.get('Ref Designator', '')):
ports[(uid.casefold(), symbol_name.casefold())] += 1
return
if attributes.get('Part Number'):
raise NativeInventoryUnsupported('全工程报告中的请求物料缺少唯一身份')
raise _uid_error('全工程报告中的请求物料缺少唯一身份', uid, text, symbol_line)
return
if uid in seen_uids or uid not in effective:
raise NativeInventoryUnsupported('分层与展开报告的 UID 不完整或重复,需由 CAD 核验')
raise _uid_error('分层与展开报告的 UID 不完整或重复,需由 CAD 核验', uid, text, symbol_line)
seen_uids.add(uid)
values = effective[uid]
if attributes.get('Part Number') and 'Part Number' not in values:
raise NativeInventoryUnsupported('展开报告缺少物料生效属性')
if 'Value' in attributes and 'Value' not in values:
raise NativeInventoryUnsupported('展开报告缺少 Value 生效属性')
if values.get('Part Number', '').casefold() in requested:
if values.get('Part Number') and ('*' in requested or values['Part Number'].casefold() in requested):
# A library reference override on an unrelated part cannot affect
# the requested identities. Still validate every UID/config/path,
# and require CAD whenever the ambiguous component is requested.
if uid in inconsistent_refs:
raise NativeInventoryUnsupported('展开报告的位号与属性不一致')
from .read_trace import describe_uid
conflict = inconsistent_refs[uid]
# A library symbol title can retain UF? while the instance has
# UP01. HIER can retain that SAME placeholder as a base property;
# FLAT carries the effective instance override. A different
# concrete HIER reference or different placeholder is a conflict.
actual = values.get('Ref Designator', '')
placeholder = re.fullmatch(r'[A-Za-z_][A-Za-z0-9_]*\?+', conflict['header_refdes'])
if not (placeholder and actual and '?' not in actual
and attributes.get('Ref Designator') in {actual, conflict['header_refdes']}):
raise NativeInventoryUnsupported(
f'展开报告的位号与属性不一致:UID {uid},'
f'标题位号={conflict["header_refdes"]!r},属性位号={conflict["attribute_refdes"]!r}',
details={**describe_uid(uid), **conflict, 'scope_line_number': symbol_line,
'scope_refdes': attributes.get('Ref Designator', '')})
ref, part = values.get('Ref Designator', ''), values['Part Number']
if not ref or 'Ref Designator' not in attributes:
raise NativeInventoryUnsupported('全工程报告中的请求物料缺少位号')
@@ -115,21 +174,25 @@ def _parse_scope(text, requested, effective, inconsistent_refs):
targets.add(target)
if len(targets) > 100000:
raise NativeInventoryUnsupported('原生器件数量超过读取上限')
for line in text.splitlines():
for line_number, line in enumerate(text.splitlines(), 1):
if line.startswith('Block '):
finish()
name = line[6:].split('\t', 1)[0].rstrip()
if not name or name in blocks:
raise NativeInventoryUnsupported('全工程报告含不明确的原理图身份')
blocks.add(name)
block, symbol, attributes = name, None, {}
block, symbol, attributes, symbol_name, symbol_name_count = name, None, {}, '', 0
elif line.startswith(' Symbol '):
finish()
symbol_line = line_number
match = re.fullmatch(r' Symbol (.+?)\tpage (.+)', line)
if not block or match is None:
raise NativeInventoryUnsupported('全工程报告含不明确的图页身份')
symbol, attributes = match.groups(), {}
raise _uid_error('全工程报告含不明确的图页身份', '', text, symbol_line)
symbol, attributes, symbol_name, symbol_name_count = match.groups(), {}, '', 0
sheets.add((block, symbol[1]))
elif symbol is not None and _SYMBOL_NAME.fullmatch(line):
symbol_name = _SYMBOL_NAME.fullmatch(line).group(1)
symbol_name_count += 1
elif symbol is not None and line.startswith('\tProp '):
# Pin properties have two tabs and must never override a component.
match = _PROPERTY.fullmatch(line)
@@ -143,18 +206,55 @@ def _parse_scope(text, requested, effective, inconsistent_refs):
raise NativeInventoryUnsupported('全工程报告缺少原理图或图页')
if seen_uids != set(effective):
raise NativeInventoryUnsupported('分层与展开报告的器件范围不一致,需由 CAD 核验')
if ports != expected_ports:
raise NativeInventoryUnsupported('分层与展开报告的电源端口范围不一致,需由 CAD 核验')
return blocks, sheets, targets
def read_ascii_discovery(project, report_path, old_part_numbers, *, expected_root=None,
effective_report_path=None):
text = read_native_text(report_path)
_require_report(text, 'HIER BLOCKS')
if effective_report_path is None:
raise NativeInventoryUnsupported('缺少实例生效属性报告,需由 CAD 核验')
effective, inconsistent_refs = _effective_properties(read_native_text(effective_report_path))
requested = {part.casefold() for part in old_part_numbers}
blocks, available, targets = _parse_scope(text, requested, effective, inconsistent_refs)
effective_report_path=None, on_diagnostic=None):
from .read_trace import diagnose
stage, report = 'scope_read', Path(report_path).name
def mark(name, **details):
diagnose(on_diagnostic, name, report=report, **details)
def describe(text):
lines, samples = text.splitlines(), []
for i, line in enumerate(lines, 1):
if line.startswith(' Symbol '):
samples.append({'line': i, 'text': line[:512]})
if len(samples) == 10:
break
return {'line_count': len(lines),
'text_sha256': hashlib.sha256(text.encode('utf-8')).hexdigest(),
'headers': [line.strip()[:512] for line in lines if re.match(
r'^\s*(REPORT FORMAT|MODE|TEXT LISTS|SYMBOLS ONLY|NETS ONLY|USER BLOCKS):', line)][:20],
'symbol_samples': samples}
try:
mark(stage, state='started')
text = read_native_text(report_path)
mark(stage, state='completed', **describe(text))
stage = 'scope_header'
_require_report(text, 'HIER BLOCKS')
if effective_report_path is None:
raise NativeInventoryUnsupported('缺少实例生效属性报告,需由 CAD 核验')
stage, report = 'effective_read', Path(effective_report_path).name
mark(stage, state='started')
effective_text = read_native_text(effective_report_path)
mark(stage, state='completed', **describe(effective_text))
stage = 'effective_parse'
mark(stage, state='started')
effective, inconsistent_refs, ports = _effective_properties(effective_text)
mark(stage, state='completed', components=len(effective), inconsistent_refs=len(inconsistent_refs),
ignored_named_ports=sum(ports.values()))
stage, report = 'scope_parse', Path(report_path).name
mark(stage, state='started')
requested = {part.casefold() for part in old_part_numbers}
blocks, available, targets = _parse_scope(text, requested, effective, inconsistent_refs, ports)
mark(stage, state='completed', blocks=len(blocks), sheets=len(available), matched_components=len(targets))
except (NativeInventoryUnsupported, OSError, UnicodeError) as error:
mark(stage, state='failed', error_type=type(error).__name__, error=str(error),
**getattr(error, 'details', {}))
raise
if expected_root is not None and expected_root not in blocks:
raise NativeInventoryUnsupported('原生全工程报告与工程根块身份不一致')
components = tuple(ComponentTarget(Path(project), *row) for row in sorted(targets))
@@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
import json
import re
from pathlib import Path
import shutil
import subprocess
@@ -32,7 +33,8 @@ def _read_failure(root, message):
if (record['state'] == 'released' and record['clean'] is True
and record['cad_exit_code'] == 0 and record['quit_helper_exit_code'] == 0
and cleanup['desktop_handle_closed'] is True and cleanup['errors'] == []
and cleanup['desktop_confirmation']['released'] is True):
and (cleanup['desktop_confirmation'].get('released') is True
or cleanup['desktop_confirmation'].get('release_accepted') is True)):
error_type = ReleasedProjectReadError
except (OSError, ValueError, KeyError, TypeError):
pass # Absence of an uncertainty marker is never release evidence.
@@ -103,11 +105,17 @@ def discover_project(
*runtime.helper_launch_arguments(),
job_id,
)
from .read_trace import ReadTrace
trace = ReadTrace(root, project=project, backend='cad-discovery')
trace.emit('discovery_request', state='started', job_id=job_id,
collect_components=collect_components, requested_parts=list(requested.values())[:20])
try:
report_progress(on_progress, f'正在启动工程读取会话:{project.name}')
check_cancelled(stop_event)
completed = runtime.process_runner(command, runtime.timeout)
except (OSError, subprocess.SubprocessError) as error:
trace.emit('discovery_worker', state='failed', job_id=job_id,
error_type=type(error).__name__, error=str(error))
_require_released_reader(root)
failure = _read_failure(
root,
@@ -116,24 +124,47 @@ def discover_project(
if isinstance(failure, ReleasedProjectReadError):
check_cancelled(stop_event)
raise failure from error
_require_released_reader(root)
(root / "worker-output.txt").write_text(completed.stdout + "\n" + completed.stderr, encoding="utf-8")
if completed.returncode != 0:
detail = completed.stderr.strip() or completed.stdout.strip() or "sheet discovery failed"
failure = _read_failure(root, runtime.failure_message(detail))
if isinstance(failure, ReleasedProjectReadError):
check_cancelled(stop_event)
raise failure
check_cancelled(stop_event)
report_progress(on_progress, f'读取会话已结束,正在核对位号清单:{project.name}')
sheets = runtime.read_sheets(root / "sheets.tsv", project)
matches = runtime.read_matches(root / "inventory.tsv", project, sheets)
if collect_components:
from .inventory import read_component_inventory
components = read_component_inventory(root / "components.tsv", project, sheets)
else:
components = ()
check_cancelled(stop_event)
stage = 'discovery_worker_return'
trace.emit(stage, state='completed', job_id=job_id, returncode=completed.returncode,
stdout=completed.stdout[-2048:], stderr=completed.stderr[-2048:])
try:
_require_released_reader(root)
(root / "worker-output.txt").write_text(completed.stdout + "\n" + completed.stderr, encoding="utf-8")
if completed.returncode != 0:
detail = completed.stderr.strip() or completed.stdout.strip() or "sheet discovery failed"
failure = _read_failure(root, runtime.failure_message(detail))
if isinstance(failure, ReleasedProjectReadError):
check_cancelled(stop_event)
raise failure
for line in completed.stderr.splitlines():
skipped = re.fullmatch(r'inventory: skipped (\d+) objects without Ref Designator; see quick-part-replace\.log', line.strip())
if skipped:
report_progress(on_progress, f'已跳过 {skipped.group(1)} 个无位号对象,详情见诊断包。')
check_cancelled(stop_event)
report_progress(on_progress, f'读取会话已结束,正在核对位号清单:{project.name}')
stage = 'sheet_parse'
trace.emit(stage, state='started', report='sheets.tsv')
sheets = runtime.read_sheets(root / "sheets.tsv", project)
trace.emit(stage, state='completed', sheets=len(sheets))
stage = 'inventory_match_parse'
trace.emit(stage, state='started', report='inventory.tsv')
matches = runtime.read_matches(root / "inventory.tsv", project, sheets)
trace.emit(stage, state='completed', matched_sheets=len(matches))
if collect_components:
from .inventory import read_component_inventory
stage = 'component_parse'
trace.emit(stage, state='started', report='components.tsv')
components = read_component_inventory(root / "components.tsv", project, sheets)
else:
components = ()
check_cancelled(stop_event)
trace.emit('discovery_complete', state='completed', job_id=job_id, components=len(components),
samples=[{'uid': c.uid, 'refdes': c.refdes, 'sheet': c.sheet_number,
'schematic': c.schematic_name, 'part': c.old_part_number} for c in components[:10]])
except Exception as error:
trace.emit(stage, state='failed', job_id=job_id,
error_type=type(error).__name__, error=str(error))
raise
return ProjectDiscovery(sheets, matches, components)
@@ -91,6 +91,14 @@ class ProjectSessionWorker:
raise WorkerMonitorError('project session exited before authorized closing')
self._failure_delivered = True
return self._result
stop_control = getattr(self.runtime, 'stop_event', None)
if stop_control is not None and stop_control.is_set() and not self._abort.is_set():
# run_step waits for the verified session checkpoint, so it
# cannot return to the queue's usual stop check on its own.
# Bridge the user's stop request directly to the owned worker
# monitor; that monitor decides whether the current stage is
# safe to stop or must finish its write boundary.
self._abort.set()
try:
fields = (self.root / 'project-session-result.tsv').read_text(encoding='utf-8-sig').strip().split('\t')
except (FileNotFoundError, PermissionError):
@@ -128,7 +128,7 @@ def create_project_tasks(store: TaskStore, root: Path, *, selected_paths: tuple[
group_id = uuid4().hex
created: list[TaskRecord] = []
with store.transaction():
names = {task.name.casefold() for task in store.list_tasks()}
names = {task.name.casefold() for task in (*store.list_tasks(), *store.list_tasks(TaskStatus.DELETED))}
for position, path in enumerate(pending):
base_name = path.relative_to(root).as_posix()
name = base_name
@@ -711,8 +711,15 @@ class QuickRunPreparer:
return self._discover_project_sheets(project_path, old_part_numbers,
collect_components=True, **controls)
return self._discover_project_sheets(project_path, old_part_numbers, **controls)
release_idle = getattr(runtime, 'release_idle_for_fingerprint', None)
def release_unverifiable(path):
if release_idle(path):
report_progress(on_progress, f'已关闭后台只读会话,正在重新核验 {path.name} 的工程文件…')
return True
return False
return self._discovery_cache.discover(
Path(project_path), old_part_numbers, collect_components, load,
**({'release_unverifiable': release_unverifiable} if callable(release_idle) else {}),
**({'stop_event': stop_event} if stop_event is not None else {}))
def collect_inventory(self, project_path: Path, old_part_numbers: tuple[str, ...], *, stop_event=None, on_progress=None, runtime=None) -> tuple:
@@ -724,7 +731,7 @@ class QuickRunPreparer:
return tuple(component for component in self.discover_project(
project_path, old_part_numbers, collect_components=True, **control_options(stop_event, on_progress),
**({'runtime': runtime} if runtime is not None else {})).components
if component.old_part_number.casefold() in requested)
if component.old_part_number and ('*' in requested or component.old_part_number.casefold() in requested))
def _discover_project_sheets(
self,
@@ -167,10 +167,15 @@ def _read_snapshot_once(preparer, task_id, projects, rules, *, copy_part_number_
continue
root, commands = candidate, parsed
break
# A supervisor may durably create the protection before a worker has
# published command/status files. It must block the task even if no
# readable handoff exists or an older history handoff was found.
current_uncertain = internal_path(current / 'uncertain.flag').is_file()
if root is None:
files.checkpoint()
return QuickRunSnapshot(progress, False, None, False, 'selection')
interrupted = root == current and (internal_path(root / 'uncertain.flag')).is_file()
return QuickRunSnapshot(progress, current_uncertain, current if current_uncertain else None,
current_uncertain, 'selection')
interrupted = current_uncertain
phase = 'selection'
try:
details = api._read_status_details(files.path(root / 'status.tsv'))
@@ -0,0 +1,62 @@
"""Bounded diagnostic evidence; never grants permission or changes CAD flow."""
from datetime import datetime, timezone
import json
import os
from pathlib import Path
import time
from .path_access import internal_path
from .retention import rotate_text_log
def _bounded(value, depth=0):
if isinstance(value, str):
return value if len(value) <= 2048 else value[:2048] + '…[truncated]'
if depth < 4 and isinstance(value, dict):
return {str(k)[:80]: _bounded(v, depth + 1) for k, v in list(value.items())[:32]}
if depth < 4 and isinstance(value, (list, tuple)):
return [_bounded(v, depth + 1) for v in value[:20]]
if value is None or isinstance(value, (int, float, bool)):
return value
return str(value)[:2048]
class ReadTrace:
def __init__(self, root, *, project='', backend='cad'):
self.root = Path(root)
self.project, self.backend = str(project), backend
self.started = time.monotonic()
def emit(self, stage, *, state='progress', **details):
try:
row = _bounded(details)
row.update(version=1, timestamp_utc=datetime.now(timezone.utc).isoformat(),
pid=os.getpid(), project=self.project, backend=self.backend,
stage=str(stage), state=state, elapsed_seconds=round(time.monotonic() - self.started, 3))
encoded = json.dumps(row, ensure_ascii=False, allow_nan=False) + '\n'
root = internal_path(self.root)
root.mkdir(parents=True, exist_ok=True)
path = root / 'read-trace.jsonl'
rotate_text_log(path, max_bytes=1024 * 1024, backup_count=2)
with path.open('a', encoding='utf-8', newline='\n') as stream:
stream.write(encoded)
stream.flush()
temporary = root / 'read-state.json.tmp'
temporary.write_text(encoded, encoding='utf-8')
temporary.replace(root / 'read-state.json')
except Exception:
pass # Diagnostics must never prevent cleanup or change a business result.
def describe_uid(uid):
return {'uid': uid, 'uid_repr': repr(uid),
'uid_codepoints': ['U+%04X' % ord(char) for char in uid[:64]],
'expected_uid_pattern': r'^\$\d+I\d+$'}
def diagnose(callback, stage, **details):
if callback is not None:
try:
callback(stage, **details)
except Exception:
pass
@@ -46,6 +46,17 @@ def _recovery_command(commands, statuses, *, root=None):
raise TaskCenterError('quick_run_recovery', '多个保存后未核验步骤需要逐项人工核对,保护状态保持。')
if unverified:
return unverified[0]
# A failed native step can have a pre-mutation report, but an interruption
# marker still protects that step. Never move on to the next pending job.
if root is not None and (root / 'uncertain.flag').is_file():
try:
progress = json.loads((root / 'worker-progress.json').read_text(encoding='utf-8'))
job = progress.get('job_id')
except (OSError, ValueError, AttributeError):
job = None
matches = [command for command in commands if command[1] == job]
if len(matches) == 1 and latest.get(job) in {'batch_failed', 'awaiting_native_replace', 'batch_saved_unverified'}:
return matches[0]
return _next_unfinished_command(commands, statuses)
@@ -172,7 +183,9 @@ def recover(preparer, task_id, projects, rules, *, copy_part_number_to_value=Fal
if any(not value or any(character in value for character in '\t\r\n') for value in identity):
raise ValueError('invalid library identity')
except (OSError, ValueError, KeyError, TypeError, TaskCenterError) as error:
raise TaskCenterError('quick_run_recovery', '快照或原生替换证据无法与此任务绑定,保护状态保持不变。') from error
raise TaskCenterError('quick_run_recovery',
f'中断步骤 {job_id} 的快照或原生替换证据不完整或无法绑定;请人工核对该工程,保护状态保持不变。'
f'(诊断:{type(error).__name__}: {error})') from error
if not uncertain.is_file():
_write_uncertain_flag(uncertain, 'recovery_in_progress')
try:
@@ -247,6 +260,11 @@ def recover(preparer, task_id, projects, rules, *, copy_part_number_to_value=Fal
recovery_record = {'task_id': task_id, 'job_id': job_id, 'state': state, 'worker_result': after[0],
'snapshot_sha256': binding['snapshot_sha256'], 'evidence_before': evidence_before,
'project_before': project_before, 'native_replacement_called': False}
if state == 'restored_ok':
from .value_readback import value_readback_details
from types import SimpleNamespace
recovery_record['value_details'] = value_readback_details(SimpleNamespace(
status_path=worker / 'status.tsv', state=state, message=after[0][3]))
(audit / 'recovery.json').write_text(json.dumps(recovery_record, ensure_ascii=False, indent=2), encoding='utf-8')
try:
if state == 'retry_ready':
@@ -0,0 +1,113 @@
"""Readback evidence parsing, usable without importing the Qt package."""
import json
import re
def interrupted_details(root):
"""Read correlated current-job evidence; never infer a successful write."""
from pathlib import Path
from .quick_run import _read_commands, _read_status_details, _read_command_sheet_targets
root = Path(root)
try:
progress = json.loads((root / 'worker-progress.json').read_text(encoding='utf-8'))
job = progress['job_id']
statuses = _read_status_details(root / 'status.tsv')
latest = {row[0]: row for row in statuses}
# A later saved step must not hide an earlier failed target from the
# post-execution library diagnostic. Prefer the active job otherwise.
if job in latest and latest[job][1] in {'saved_ok', 'restored_ok'}:
failed = [row for row in latest.values() if row[1] in {
'batch_failed', 'batch_saved_unverified', 'hard_failure'}]
if failed:
job = failed[-1][0]
commands = [row for row in _read_commands(root / 'command.tsv') if row[1] == job]
if len(commands) != 1:
return {}
command = commands[0]
entries = [row for row in statuses if row[0] == job]
reason = entries[-1][3] if entries else str(progress.get('resolution_error', ''))
if 'Native DxDataBook did not finish loading target part' in reason:
reason = f'DxDataBook 加载目标料号超时:{command[4]};未加载或不存在,已跳过,请人工处理。'
elif not reason:
reason = '后台操作未返回明确原因,请查看日志。'
sheets = _read_command_sheet_targets(root / 'command.tsv') or {}
sheet = sheets.get(job, ('', ''))
return {'old_part_number': command[3], 'new_part_number': command[4],
'schematic_name': sheet[0], 'sheet_number': sheet[1], 'failure_reason': reason,
'failed_job_state': entries[-1][1] if entries else ''}
except Exception:
return {}
def replacement_parts(message, state):
if state not in {'saved_ok', 'restored_ok'}:
return ()
_, marker, summary = str(message).partition('Replacements:')
if not marker:
return ()
for suffix in (' Display verified:', ' NC synced:', ' Saved, reopened'):
summary = summary.partition(suffix)[0]
result = []
for entry in summary.strip().split('; '):
match = re.fullmatch(r'(\S+) (.+?) -> (.+)', entry.strip())
if match is None:
return ()
result.append(match.groups())
return tuple(result)
def _value_columns(details, ref, old, new):
records = details.get('value_readback')
if not isinstance(records, list):
return ('未记录', '未记录')
matches = [row for row in records if isinstance(row, dict)
and str(row.get('refdes', '')).casefold() == ref.casefold()
and str(row.get('old_part_number', '')).casefold() == old.casefold()
and str(row.get('actual_part_number', '')).casefold() == new.casefold()]
if len(matches) != 1:
return ('未记录', '未记录')
row = matches[0]
def display(key):
if row.get(key + '_present') is False:
return '(无 Value 属性)'
value = row.get(key)
return ('(空)' if value == '' else value) if isinstance(value, str) else '未记录'
return (display('old_value'), display('actual_value'))
def replacement_result_rows(lines, *, include_values=False):
result = []
for line in lines:
try:
event = json.loads(line)
except (ValueError, TypeError):
continue
if not isinstance(event, dict) or event.get('operation') not in (
{'quick_run_executed', 'quick_run_recovered', 'workspace_project_stopped', 'workspace_project_failed'}
if include_values else {'quick_run_executed'}):
continue
details = event.get('details')
if not isinstance(details, dict):
continue
state = details.get('state')
location = ':'.join(str(details.get(k) or '') for k in ('schematic_name', 'sheet_number'))
label = '已保存并核验' if state == 'saved_ok' or details.get('recovery_saved_verified') is True else '已回读,保存待确认'
parts = replacement_parts(event.get('message', ''), state)
if (include_values and not parts and event.get('operation') == 'quick_run_recovered'
and state == 'restored_ok' and isinstance(details.get('value_readback'), list)):
parts = tuple((row['refdes'], row['old_part_number'], row['actual_part_number'])
for row in details['value_readback'] if isinstance(row, dict)
and all(isinstance(row.get(key), str) and row[key] for key in
('refdes', 'old_part_number', 'actual_part_number')))
for ref, old, new in parts:
values = _value_columns(details, ref, old, new) if include_values else ()
result.append((location, ref, old, new, *values, label))
if state not in {'saved_ok', 'restored_ok'}:
message = str(event.get('message', ''))
reason = details.get('failure_reason', '')
if reason and str(reason) not in message:
message += ' ' + str(reason)
values = ('未记录', '未记录') if include_values else ()
result.append((location, '', str(details.get('old_part_number', '')),
'', *values, f'{state} · {message}'))
return tuple(result)
@@ -27,6 +27,7 @@ from .execution_metrics import measure_phase
from .read_control import QueryCancelled
from .batch_rules import batch_rule_view, mutate_batch_rules, seed_defaults
from .operation_control import check_cancelled, control_options, report_progress
from .value_readback import value_readback_details, recovered_value_readback_details
_Result = TypeVar("_Result")
@@ -703,7 +704,8 @@ class TaskCenterService:
task_id=task_id,
operation="quick_run_executed",
message=result.message,
details={"state": result.state, "status_path": str(result.status_path), "log_path": str(result.log_path)},
details={**value_readback_details(result, self._logging.logger),
"state": result.state, "status_path": str(result.status_path), "log_path": str(result.log_path)},
)
self._logging.logger.info("quick_run_executed task_id=%s state=%s", task_id, result.state)
return result
@@ -888,13 +890,18 @@ class TaskCenterService:
return self._operation('cleanup_batch_resources', None,
lambda: cleanup_pending_resources(self._logging.root, logging_context=self._logging))
def inspect_bom_batch(self, task_id: str, path: Path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None):
def inspect_bom_batch(self, task_id: str, path: Path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None,
workspace_mode=False, workspace_tasks=None):
if runtime is None and self._supervised:
return self._supervise('inspect_bom_batch', {'task_id': task_id, 'path': path},
arguments = {'task_id': task_id, 'path': path}
if workspace_mode:
arguments.update(workspace_mode=True, workspace_tasks=workspace_tasks)
return self._supervise('inspect_bom_batch', arguments,
stop_event=stop_event, on_progress=on_progress, on_phase=on_phase)
from .bom_import_report import inspect_batch
return self._idle_quick_run_operation('inspect_bom_batch', task_id,
lambda: inspect_batch(self, task_id, path, on_phase=on_phase, runtime=runtime,
lambda: inspect_batch(self, task_id, path, on_phase=on_phase, runtime=runtime, workspace_mode=workspace_mode,
workspace_tasks=workspace_tasks,
**control_options(stop_event, on_progress)))
def _pause_quick_run_queue(self, task_id: str, message: str) -> None:
@@ -981,7 +988,8 @@ class TaskCenterService:
self._store.set_task_status(task_id, TaskStatus.READY, summary="interrupted quick-run reconciled")
self._synchronize_quick_run_progress(task_id)
append_task_event(self._logging, level="INFO", task_id=task_id, operation="quick_run_recovered",
message=result.message, details={"state": result.state, "evidence": str(result.log_path.parent)})
message=result.message, details={**recovered_value_readback_details(result, task_id),
"state": result.state, "evidence": str(result.log_path.parent)})
return result
from .task_queue import assert_task_queue_idle
@@ -1117,6 +1125,38 @@ class TaskCenterService:
def _supervise(self, operation, arguments, **controls):
from .batch_supervisor import run_supervised
from .supervisor_protocol import READ_ONLY_OPERATIONS
if operation in READ_ONLY_OPERATIONS:
task_id = arguments.get('task_id', '')
callback = controls.get('on_progress')
def progress(message):
try:
self._logging.logger.info('read_progress operation=%s task_id=%s message=%s',
operation, task_id, str(message)[:2048])
except Exception:
pass
if callback is not None:
callback(message)
controls['on_progress'] = progress
def action():
result = run_supervised(self, operation, arguments, **controls)
try:
self._logging.logger.info(
'read_result operation=%s task_id=%s type=%s total_rows=%s passed_rows=%s failed_rows=%s unchecked_rows=%s',
operation, task_id, type(result).__name__, getattr(result, 'total_rows', ''),
getattr(result, 'passed_rows', ''), getattr(result, 'failed_rows', ''),
getattr(result, 'unchecked_rows', ''))
failed = [row for row in getattr(result, 'rows', ()) if row.status != 'passed']
for row in failed[:100]:
self._logging.logger.warning(
'read_row_result task_id=%s excel_row=%s project=%s status=%s reason=%s',
task_id, row.excel_row, row.project_path, row.status, row.reason[:2048])
if len(failed) > 100:
self._logging.logger.warning('read_row_result task_id=%s omitted_rows=%s', task_id, len(failed) - 100)
except Exception:
pass # Reporting failure must not discard a valid business result.
return result
return self._operation(operation, arguments.get('task_id'), action)
return self._operation(operation, arguments.get('task_id'),
lambda: run_supervised(self, operation, arguments, **controls))
@@ -10,6 +10,7 @@ class StopControl(Event):
def __init__(self):
super().__init__()
self.force_event = Event()
self.owner_lost_event = Event()
self.control_error = ''
self.waiting_for_resolution = False
self.resolution_error = ''
@@ -68,6 +69,15 @@ def wait_for_resolution(control, root, error):
resolution_error=str(error), stop_requested=True, safe_stop=False)
_publish_progress(root / 'worker-progress.json', record)
while True:
if control.owner_lost_event.is_set():
try:
protect()
except OSError as failure:
control.control_error = '主窗口已退出,但保护记录尚未保存,保留后台等待重试:' + str(failure)
control.force_event.clear()
control.force_event.wait(.1)
continue
control.force_event.set()
if control.force_event.wait(.1):
try:
protect()
@@ -75,4 +85,5 @@ def wait_for_resolution(control, root, error):
control.force_event.clear()
control.control_error = '强制结束未执行,保护记录无法保存:' + str(failure)
continue
raise WorkerMonitorError('user forced stop of uncertain operation; reconciliation required')
reason = 'owner exited' if control.owner_lost_event.is_set() else 'user forced stop'
raise WorkerMonitorError(reason + ' of uncertain operation; reconciliation required')
@@ -16,6 +16,10 @@ OPERATIONS = frozenset({'probe', 'runtime_smoke', 'inspect_bom_batch', 'preview_
'prepare_quick_run', 'prepare_failed_quick_run', 'recover_quick_run', 'cleanup_batch_resources',
'target_cell_choices'})
# Only these operations exclusively read CAD. Unknown and mixed read/write
# operations retain the interactive write-reconciliation policy.
READ_ONLY_OPERATIONS = frozenset({'inspect_bom_batch', 'preview_bom_batch', 'collect_target_inventory'})
def valid_journal(value, batch_id):
def identity(item):
@@ -17,7 +17,8 @@ from .quick_run import _write_atomic_queue_text
_QUEUE_LOCK = Lock()
_QUEUE_OWNER: int | None = None
_SAFE_FAILURES = frozenset({'quick_run_preflight', 'quick_run_session', 'quick_run_backup',
'target_identity', 'empty_target_scope', 'enabled_rule_required'})
'quick_run_project_open', 'target_identity', 'empty_target_scope',
'enabled_rule_required'})
_TERMINAL = frozenset({'completed', 'failed', 'skipped'})
@@ -30,7 +31,7 @@ class TaskQueueEntry:
@dataclass(frozen=True)
class TaskQueueResult:
queue_id: str
queue_id: str | None
entries: tuple[TaskQueueEntry, ...]
paused: bool
current_task_id: str | None = None
@@ -221,9 +222,13 @@ def execute_task_queue(service, task_ids: tuple[str, ...], *, stop_event: Event,
if incomplete:
paused = True
except TaskCenterError as error:
safe = error.code in _SAFE_FAILURES and not child.has_interrupted_quick_run(task_id)
entry = {'state': 'failed' if safe else 'blocked', 'message': error.message}
paused = not safe
if error.code == 'operation_cancelled' and not child.has_interrupted_quick_run(task_id):
entry = {'state': 'paused', 'message': error.message}
paused = True
else:
safe = error.code in _SAFE_FAILURES and not child.has_interrupted_quick_run(task_id)
entry = {'state': 'failed' if safe else 'blocked', 'message': error.message}
paused = not safe
except Exception as error:
entry = {'state': 'blocked', 'message': str(error)}
paused = True
@@ -1,5 +1,10 @@
"""PySide6 task-center presentation layer."""
from .main_window import TrialTaskCenterWindow
__all__ = ["TrialTaskCenterWindow"]
def __getattr__(name):
if name == 'TrialTaskCenterWindow':
from .main_window import TrialTaskCenterWindow
return TrialTaskCenterWindow
raise AttributeError(name)
@@ -78,7 +78,7 @@ class _MappingDialog(QDialog):
self.custom_value_edit.setPlaceholderText("输入替换后的 Value")
self.custom_value_edit.setEnabled(custom_value_enabled)
self.custom_value_check.toggled.connect(self.custom_value_edit.setEnabled)
self.sync_nc_check = QCheckBox("识别并同步原 Value 中的 /NC")
self.sync_nc_check = QCheckBox("识别并同步原 Value 中的 NC 标识")
self.sync_nc_check.setChecked(sync_nc)
form = QFormLayout(self)
form.addRow("旧物料编码", self.old_edit)
@@ -1,7 +1,8 @@
"""Model-based editable target plan; filtering never changes saved identities."""
from dataclasses import replace
from PySide6.QtCore import QAbstractTableModel, QModelIndex, QSortFilterProxyModel, Qt, Signal
from PySide6.QtWidgets import QAbstractItemView, QHeaderView, QTableView
from PySide6.QtCore import QAbstractTableModel, QModelIndex, QSortFilterProxyModel, Qt, Signal, QItemSelectionModel, QTimer, QEvent
from PySide6.QtWidgets import QAbstractItemView, QApplication, QHeaderView, QTableView, QMenu, QToolTip, QStyledItemDelegate
from PySide6.QtGui import QColor, QKeySequence, QPainter, QPen
HEADERS = ('替换', '原理图', '图页', '位号', '旧料号', '原 Value', '新料号', '自定义 Value', '自定义内容', '同步 NC')
CHECK_FIELDS = {0: 'enabled', 7: 'custom_value_enabled', 9: 'sync_nc'}
@@ -15,6 +16,12 @@ class TargetPlanModel(QAbstractTableModel):
self.enabled_count = 0
self.search_text = []
self.editable = True
self.workspace_status = False
self.project_status = None
self.library_issues = {}
def already_target(self, number):
row = self.rows[number]
return bool(row.new_part_number) and row.new_part_number.casefold() == row.target.old_part_number.casefold()
@staticmethod
def searchable(row):
t = row.target
@@ -29,16 +36,33 @@ class TargetPlanModel(QAbstractTableModel):
def rowCount(self, parent=QModelIndex()):
return 0 if parent.isValid() else len(self.rows)
def columnCount(self, parent=QModelIndex()):
return 0 if parent.isValid() else len(HEADERS)
return 0 if parent.isValid() else len(HEADERS) + int(self.workspace_status)
def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole):
if role == Qt.ItemDataRole.DisplayRole:
return HEADERS[section] if orientation == Qt.Orientation.Horizontal else section + 1
return (HEADERS + ('状态',))[section] if orientation == Qt.Orientation.Horizontal else section + 1
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
if not index.isValid():
return None
row = self.rows[index.row()]
t = row.target
col = index.column()
issue = self.library_issues.get(row.new_part_number, '')
if issue and role == Qt.ItemDataRole.ToolTipRole:
return issue
if issue and role == Qt.ItemDataRole.BackgroundRole:
return QColor('#fff0ed')
if col == 10:
if role == Qt.ItemDataRole.DisplayRole:
if issue:
return issue
if self.already_target(index.row()):
return '已是目标 · 自动跳过'
if (self.workspace_status
and getattr(self.project_status, 'value', self.project_status) == 'completed'
and row.enabled):
return '已完成'
return '待替换' if row.enabled else '未选中'
return None
if role == Qt.ItemDataRole.CheckStateRole and col in CHECK_FIELDS:
return Qt.CheckState.Checked if getattr(row, CHECK_FIELDS[col]) else Qt.CheckState.Unchecked
if role in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole):
@@ -50,15 +74,18 @@ class TargetPlanModel(QAbstractTableModel):
if not index.isValid():
return Qt.ItemFlag.NoItemFlags
flags = Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEnabled
if self.editable:
if self.editable and not (self.workspace_status and self.already_target(index.row())):
if index.column() in CHECK_FIELDS:
flags |= Qt.ItemFlag.ItemIsUserCheckable
if index.column() != 0 or not self.library_issues.get(self.rows[index.row()].new_part_number) or self.rows[index.row()].enabled:
flags |= Qt.ItemFlag.ItemIsUserCheckable
elif index.column() in TEXT_FIELDS:
flags |= Qt.ItemFlag.ItemIsEditable
return flags
def setData(self, index, value, role=Qt.ItemDataRole.EditRole):
if not self.editable or not index.isValid():
return False
if self.workspace_status and self.already_target(index.row()):
return False
col = index.column()
if role == Qt.ItemDataRole.CheckStateRole and col in CHECK_FIELDS:
change = {CHECK_FIELDS[col]: value in (Qt.CheckState.Checked, Qt.CheckState.Checked.value)}
@@ -70,6 +97,8 @@ class TargetPlanModel(QAbstractTableModel):
if 'new_part_number' in change and change['new_part_number'] != self.rows[number].new_part_number:
change['target_cell'] = ''
updated = replace(self.rows[number], **change)
if change.get('enabled') and self.library_issues.get(updated.new_part_number):
return False
if updated == self.rows[number]:
return False
self.enabled_count += int(updated.enabled) - int(self.rows[number].enabled)
@@ -86,6 +115,10 @@ class TargetPlanModel(QAbstractTableModel):
field = CHECK_FIELDS[column] if role == Qt.ItemDataRole.CheckStateRole else TEXT_FIELDS[column]
changed = []
for number in numbers:
if field == 'enabled' and value and self.library_issues.get(self.rows[number].new_part_number):
continue
if self.workspace_status and self.already_target(number):
continue
changes = {field: value}
if field == 'new_part_number' and value != self.rows[number].new_part_number:
changes['target_cell'] = ''
@@ -104,9 +137,31 @@ class TargetFilter(QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
self.query = ''
self.show_unselected = True
self.setDynamicSortFilter(False)
def filterAcceptsRow(self, row, parent):
return not self.query or self.query in self.sourceModel().search_text[row]
source = self.sourceModel()
if (not self.show_unselected and not source.rows[row].enabled
and not source.library_issues.get(source.rows[row].new_part_number)
and not (source.workspace_status and source.rows[row].new_part_number)):
return False
return not self.query or self.query in source.search_text[row]
class CheckboxCellDelegate(QStyledItemDelegate):
"""Use the entire checkbox cell as its click target, without double toggles."""
def editorEvent(self, event, model, option, index):
if (index.data(Qt.ItemDataRole.CheckStateRole) is not None
and index.flags() & Qt.ItemFlag.ItemIsUserCheckable
and event.type() in {QEvent.Type.MouseButtonPress, QEvent.Type.MouseButtonRelease,
QEvent.Type.MouseButtonDblClick}
and event.button() == Qt.MouseButton.LeftButton):
if event.type() == QEvent.Type.MouseButtonRelease:
state = index.data(Qt.ItemDataRole.CheckStateRole)
return model.setData(index, Qt.CheckState.Unchecked if state == Qt.CheckState.Checked
else Qt.CheckState.Checked, Qt.ItemDataRole.CheckStateRole)
return True
return super().editorEvent(event, model, option, index)
class TargetTable(QTableView):
changed = Signal()
@@ -117,12 +172,14 @@ class TargetTable(QTableView):
self.proxy = TargetFilter(self)
self.proxy.setSourceModel(self.plan_model)
self.setModel(self.proxy)
self.setItemDelegate(CheckboxCellDelegate(self))
self.verticalHeader().hide()
self.setSortingEnabled(True)
self.sortByColumn(-1, Qt.SortOrder.AscendingOrder)
self.setAlternatingRowColors(True)
self.setMinimumHeight(160)
self.setEditTriggers(QAbstractItemView.EditTrigger.DoubleClicked | QAbstractItemView.EditTrigger.EditKeyPressed)
self.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
self.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectItems)
self.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
self.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Interactive)
for col, width in enumerate((55, 90, 50, 70, 145, 115, 145, 90, 125, 75)):
@@ -130,10 +187,81 @@ class TargetTable(QTableView):
self.dirty = False
self._next_match = -1
self.plan_model.changed.connect(self._changed)
self._copy_cells = []
self._copy_timer = QTimer(self)
self._copy_timer.setSingleShot(True)
self._copy_timer.timeout.connect(self._copy_finished)
def _changed(self):
self.dirty = True
self.proxy.invalidateFilter()
self.changed.emit()
def _cell_text(self, index):
value = index.data(Qt.ItemDataRole.DisplayRole)
checked = index.data(Qt.ItemDataRole.CheckStateRole)
return str(value or '') if checked is None else ('是' if checked == Qt.CheckState.Checked else '否')
def copy_column(self, column=None):
current = self.currentIndex()
if not current.isValid():
return
selected = self.selectionModel().selectedIndexes() or [current]
rows = sorted({index.row() for index in selected})
if column is not None:
selected = [self.model().index(row, column) for row in rows]
positions = {(index.row(), index.column()) for index in selected}
columns = sorted({index.column() for index in selected})
text = '\n'.join('\t'.join(self._cell_text(self.model().index(row, col)) if (row, col) in positions else ''
for col in columns) for row in rows)
QApplication.clipboard().setText(text)
self._copy_cells = selected
self.viewport().update()
self._copy_timer.start(450)
def _copy_finished(self):
count = len(self._copy_cells)
self._copy_cells = []
self.viewport().update()
if count and self.isVisible():
QToolTip.showText(self.viewport().mapToGlobal(self.visualRect(self.currentIndex()).bottomLeft()),
f'已复制 {count} 个单元格', self, self.rect(), 1200)
def paintEvent(self, event):
super().paintEvent(event)
painter = QPainter(self.viewport())
painter.setPen(QPen(QColor('#2878ed'), 1.5, Qt.PenStyle.DashLine))
for index in self._copy_cells:
if index.isValid():
painter.drawRect(self.visualRect(index).adjusted(1, 1, -2, -2))
def mouseReleaseEvent(self, event):
super().mouseReleaseEvent(event)
if event.button() == Qt.MouseButton.LeftButton and self.currentIndex().isValid():
QToolTip.showText(event.globalPosition().toPoint(), '已选中单元格 · Ctrl+C 复制', self, self.rect(), 1000)
def keyPressEvent(self, event):
if event.matches(QKeySequence.StandardKey.Copy):
self.copy_column()
event.accept()
return
super().keyPressEvent(event)
def contextMenuEvent(self, event):
index = self.indexAt(event.pos())
if not index.isValid():
return
selection = self.selectionModel()
if not selection.isSelected(index):
selection.select(index, QItemSelectionModel.SelectionFlag.ClearAndSelect)
selection.setCurrentIndex(index, QItemSelectionModel.SelectionFlag.NoUpdate)
menu = QMenu(self)
menu.addAction('复制选中单元格(Ctrl+C)', lambda: self.copy_column())
menu.addAction('复制旧料号', lambda: self.copy_column(4))
menu.addAction('复制新料号', lambda: self.copy_column(6))
menu.exec(event.globalPos())
def populate(self, rows):
self._copy_timer.stop()
self._copy_cells = []
self.plan_model.populate(rows)
self._next_match = -1
self.dirty = False
@@ -157,6 +285,10 @@ class TargetTable(QTableView):
self.proxy.invalidateFilter()
self._next_match = -1
return self.rowCount()
def set_show_unselected(self, show):
self.proxy.show_unselected = bool(show)
self.proxy.invalidateFilter()
self._next_match = -1
def find_next(self):
if not self.rowCount():
return None
@@ -168,5 +300,5 @@ class TargetTable(QTableView):
source_rows = [self.proxy.mapToSource(self.proxy.index(row, 0)).row() for row in range(self.rowCount())]
self.plan_model.apply_changes(source_rows, 0, bool(enabled), Qt.ItemDataRole.CheckStateRole)
def apply_to_selected(self, new_part_number):
source_rows = [self.proxy.mapToSource(index).row() for index in self.selectionModel().selectedRows()]
source_rows = sorted({self.proxy.mapToSource(index).row() for index in self.selectionModel().selectedIndexes()})
self.plan_model.apply_changes(source_rows, 6, str(new_part_number).strip(), Qt.ItemDataRole.EditRole)
@@ -0,0 +1,46 @@
"""Background work with QObject slots so all presentation runs on the GUI thread."""
from PySide6.QtCore import QObject, QThreadPool, Signal, Slot
from .workers import ServiceWorker
class WorkspaceJobs(QObject):
progress = Signal(str)
current_project = Signal(str, int, int)
changed = Signal()
def __init__(self, parent=None):
super().__init__(parent)
self.active = {}
self.closed = False
def run(self, action, succeeded, failed, finished=lambda: None):
worker = ServiceWorker(action)
self.active[worker.signals] = (worker, succeeded, failed, finished)
worker.signals.succeeded.connect(self._succeeded)
worker.signals.failed.connect(self._failed)
worker.signals.finished.connect(self._finished)
QThreadPool.globalInstance().start(worker)
self.changed.emit()
@Slot(object)
def _succeeded(self, value):
entry = self.active.get(self.sender())
if entry and not self.closed:
try:
entry[1](value)
except Exception as error:
entry[2](error)
@Slot(object)
def _failed(self, error):
entry = self.active.get(self.sender())
if entry and not self.closed:
entry[2](error)
@Slot()
def _finished(self):
entry = self.active.pop(self.sender(), None)
if entry and not self.closed:
entry[3]()
self.changed.emit()
@@ -0,0 +1,78 @@
"""Compact project rows with readable names, status and secondary counts."""
from PySide6.QtCore import Qt, QRect
from PySide6.QtGui import QColor, QFont, QFontMetrics, QPen
from PySide6.QtWidgets import QStyledItemDelegate, QStyle
PROJECT_SUMMARY_ROLE = Qt.ItemDataRole.UserRole + 1
PROJECT_GROUP_ROLE = Qt.ItemDataRole.UserRole + 2
PROJECT_EXPANDED_ROLE = Qt.ItemDataRole.UserRole + 3
PROJECT_GROUPS = {
'running': ('正在替换', '#dceaff', '#edf4ff', '#2864bb'),
'unfinished': ('未完成', '#ffedcf', '#fff8ec', '#99601c'),
'completed': ('已完成', '#d7f0e6', '#eef9f3', '#24775c'),
}
class ProjectListDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
group = index.data(PROJECT_GROUP_ROLE)
if group in PROJECT_GROUPS:
_, background, _, foreground = PROJECT_GROUPS[group]
painter.save()
painter.setRenderHint(painter.RenderHint.Antialiasing)
painter.setPen(Qt.PenStyle.NoPen)
painter.setBrush(QColor(background))
painter.drawRoundedRect(option.rect.adjusted(0, 2, -2, -2), 7, 7)
painter.setPen(QPen(QColor(foreground), 1.5))
x, y = option.rect.left() + 12, option.rect.center().y()
if index.data(PROJECT_EXPANDED_ROLE):
painter.drawLine(x - 3, y - 2, x, y + 2)
painter.drawLine(x, y + 2, x + 3, y - 2)
else:
painter.drawLine(x - 2, y - 3, x + 2, y)
painter.drawLine(x + 2, y, x - 2, y + 3)
painter.setPen(QColor(foreground))
font = QFont(option.font)
font.setPixelSize(12)
font.setWeight(QFont.Weight.DemiBold)
painter.setFont(font)
painter.drawText(option.rect.adjusted(25, 0, -8, 0),
Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, index.data())
painter.restore()
return
summary = index.data(PROJECT_SUMMARY_ROLE)
if not summary:
return super().paint(painter, option, index)
selected = bool(option.state & QStyle.StateFlag.State_Selected)
hovered = bool(option.state & QStyle.StateFlag.State_MouseOver)
painter.save()
painter.setRenderHint(painter.RenderHint.Antialiasing)
rect = option.rect.adjusted(0, 2, -2, -2)
painter.setPen(Qt.PenStyle.NoPen)
_, strong, soft, foreground = PROJECT_GROUPS.get(summary.get('group'), PROJECT_GROUPS['unfinished'])
if any(word in summary['state'] for word in ('损坏', '校验失败', '已停止')):
strong, soft, foreground = '#f8dbd8', '#fff0ee', '#ac4d46'
painter.setBrush(QColor(strong if selected else soft))
painter.drawRoundedRect(rect, 8, 8)
if selected:
painter.setBrush(QColor(foreground))
painter.drawRoundedRect(QRect(rect.left(), rect.top() + 9, 3, rect.height() - 18), 1.5, 1.5)
text_rect = rect.adjusted(11, 6, -9, -5)
font = QFont(option.font)
font.setPixelSize(12)
font.setWeight(QFont.Weight.DemiBold if selected else QFont.Weight.Medium)
painter.setFont(font)
painter.setPen(QColor(foreground if selected else '#26364c'))
title = QFontMetrics(font).elidedText(summary['name'], Qt.TextElideMode.ElideRight, text_rect.width())
painter.drawText(text_rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop, title)
font.setPixelSize(10)
font.setWeight(QFont.Weight.Normal)
painter.setFont(font)
state = summary['state']
color = '#ac4d46' if any(word in state for word in ('损坏', '校验失败', '已停止')) else '#279182' if '已完成' in state or '已是目标' in state else '#b66b28' if '失败' in state or '待处理' in state else '#6c7d93'
painter.setPen(QColor(color))
subtitle = f"{state} · 待换 {summary['pending']} / 已是目标 {summary['skipped']}"
subtitle = QFontMetrics(font).elidedText(subtitle, Qt.TextElideMode.ElideRight, text_rect.width())
painter.drawText(text_rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignBottom, subtitle)
painter.restore()
@@ -0,0 +1,74 @@
"""Fixed light desktop palette; data and selections never inherit white text."""
from PySide6.QtGui import QColor, QPalette
def light_palette():
palette = QPalette()
colors = {QPalette.ColorRole.Window: '#f4f6fa', QPalette.ColorRole.WindowText: '#243247',
QPalette.ColorRole.Base: '#ffffff', QPalette.ColorRole.AlternateBase: '#f8fafd',
QPalette.ColorRole.Text: '#202733', QPalette.ColorRole.Button: '#ffffff',
QPalette.ColorRole.ButtonText: '#202733', QPalette.ColorRole.Highlight: '#e7f0ff',
QPalette.ColorRole.HighlightedText: '#202733', QPalette.ColorRole.PlaceholderText: '#7d8899',
QPalette.ColorRole.ToolTipBase: '#ffffff', QPalette.ColorRole.ToolTipText: '#202733'}
for role, value in colors.items():
for group in (QPalette.ColorGroup.Active, QPalette.ColorGroup.Inactive):
palette.setColor(group, role, QColor(value))
palette.setColor(QPalette.ColorGroup.Disabled, role, QColor('#8c98aa') if role in {
QPalette.ColorRole.Text, QPalette.ColorRole.ButtonText, QPalette.ColorRole.WindowText} else QColor(value))
return palette
WORKSPACE_STYLE = '''
QMainWindow#workspaceWindow { background: #f4f6fa; }
QWidget#workspaceRoot { background: qlineargradient(x1:0,y1:0,x2:1,y2:1,stop:0 #edf3fa,stop:0.48 #f6f8fb,stop:1 #edf1f7); }
QWidget { font-family:"Inter","Microsoft YaHei UI","Microsoft YaHei"; font-size:12px; color:#243247; }
QLabel { background:transparent; border:0; }
QLabel#workspaceHeading { font-size:26px; font-weight:700; color:#172744; }
QLabel#workspaceSubtitle, QLabel#sourceCaption, QLabel#progressTiming { color:#7a879a; font-size:11px; }
QLabel#cardHeading { font-size:13px; font-weight:600; color:#243247; }
QLabel#workspaceStatus { color:#4779bb; background:#eaf1fc; border:1px solid #dce7f7; border-radius:11px; padding:5px 12px; font-size:11px; }
QLabel#emptyHint { color:#8a97a8; font-size:12px; padding:12px; background:#f8fafd; border-radius:8px; }
QLabel#workspaceNotice { padding:10px 14px; border:1px solid #dce9fb; border-radius:10px; background:#eef5ff; color:#38679e; }
QLabel#workspaceNotice[tone="error"] { color:#a05641; background:#fff6f1; border-color:#f2e0d5; }
QFrame#workspaceSidebar { background:rgba(252,254,255,238); border:1px solid #e2eaf4; border-radius:20px; }
QFrame#workspaceCard { background:rgba(252,254,255,240); border:1px solid #e1e9f3; border-radius:18px; }
QFrame#sourceCard { background:rgba(252,254,255,240); border:1px solid #e1e9f3; border-radius:16px; }
QLabel#workspaceBrand { color:#215dd2; font-size:18px; font-weight:700; padding:8px 10px; }
QPushButton { min-height:28px; padding:2px 12px; border:1px solid #dce3ed; border-radius:10px; background:#fbfdff; color:#202733; }
QPushButton:hover { background:#f0f5fd; border-color:#bed2ef; }
QPushButton:pressed { background:#e8f0fd; }
QPushButton:disabled { color:#99a4b3; border-color:#e6ebf2; background:#f7f9fc; }
QPushButton#primaryButton { background:#2878ed; border:1px solid #2878ed; color:white; }
QPushButton#primaryButton:hover { background:#1b69d8; }
QPushButton#primaryButton:disabled { background:#b3cff4; border-color:#b3cff4; color:#ffffff; }
QPushButton#workspaceNav { text-align:left; min-height:32px; padding-left:14px; border:1px solid transparent; background:transparent; color:#718096; }
QPushButton#workspaceNav:hover { background:#f7f9fc; color:#3b6197; }
QPushButton#workspaceNav:checked { background:#e2efff; color:#1762d5; border:1px solid #d5e7ff; font-weight:600; }
QPushButton#quietButton { border:0; background:transparent; color:#2878ed; }
QPushButton#dangerButton { color:#b84452; }
QLineEdit, QComboBox { min-height:27px; padding:2px 8px; border:1px solid #dce3ed; border-radius:7px; background:white; color:#202733; selection-background-color:#2878ed; selection-color:white; }
QLineEdit:focus, QComboBox:focus { border-color:#2878ed; }
QTableView { color:#243247; background:#ffffff; alternate-background-color:#fafbfd; gridline-color:#eef2f7; border:0; selection-background-color:#eaf2ff; selection-color:#243247; outline:0; }
QTableView::item { padding:5px; border-bottom:1px solid #f0f3f8; color:#243247; }
QTableView#workspaceProjects::item { border:0; padding:0; }
QTableView::item:selected { background:#e8f0fd; color:#202733; }
QHeaderView::section { background:#f1f5fa; color:#516683; border:0; border-bottom:1px solid #e7ecf4; padding:8px 6px; font-weight:400; }
QTableCornerButton::section { background:#f6f8fc; border:0; }
QPlainTextEdit { color:#202733; background:#f5f7fa; border:1px solid #e4e9f1; border-radius:8px; font-family:"Microsoft YaHei"; font-size:11px; padding:6px; }
QProgressBar { border:0; background:#e4ebf4; border-radius:3px; max-height:6px; min-height:6px; }
QProgressBar::chunk { border:0; background:#2878ed; border-radius:3px; }
QSplitter::handle { background:transparent; height:8px; }
QSplitter::handle:hover { background:#dce7f7; border-radius:3px; }
QScrollBar:vertical { width:9px; background:transparent; }
QScrollBar::handle:vertical { background:#c7d1df; border-radius:4px; min-height:30px; }
QScrollBar::add-line:vertical,QScrollBar::sub-line:vertical { height:0; }
QScrollBar:horizontal { height:9px; background:#ffffff; }
QScrollBar::handle:horizontal { background:#c7d1df; border-radius:4px; min-width:30px; }
QScrollBar::add-line:horizontal,QScrollBar::sub-line:horizontal { width:0; }
QAbstractScrollArea::corner { background:#ffffff; border:0; }
QMenu { background:white; color:#202733; border:1px solid #dce3ed; padding:5px; }
QMenu::item { padding:8px 20px; }
QMenu::item:selected { background:#e8f0fd; color:#202733; }
QDialog { background:#f8faff; }
QToolTip { background:white; color:#202733; border:1px solid #dce3ed; padding:5px; }
'''
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,59 @@
"""Read-only Value evidence bound to the terminal job before it advances."""
from .path_access import internal_path
from .quick_run_protocol import _decode_vbs_field
import json
def value_readback_details(result, logger=None):
if result.state not in {'saved_ok', 'restored_ok'}:
return {}
try:
from .logging_setup import _tail_utf8_lines
status = _tail_utf8_lines(internal_path(result.status_path), 1)
terminal = tuple(_decode_vbs_field(field) for field in status[-1].split('\t'))
if len(terminal) != 4 or terminal[1] != result.state or terminal[3] != result.message:
raise ValueError('terminal job does not match returned result')
report = internal_path(result.status_path.parent / 'value-readback.tsv')
if report.stat().st_size > 8 * 1024 * 1024:
raise ValueError('Value report exceeds size limit')
lines = report.read_text(encoding='utf-8-sig').splitlines()
header = tuple(_decode_vbs_field(field) for field in lines[0].split('\t'))
if len(header) != 7 or header[:4] != ('V', '1', terminal[0], result.state):
raise ValueError('Value report belongs to a different job or phase')
rows, seen = [], set()
for line in lines[1:]:
if not line:
continue
fields = tuple(_decode_vbs_field(field) for field in line.split('\t'))
if len(fields) != 9 or fields[0] != 'C' or fields[7] not in {'0', '1'} or fields[8] not in {'0', '1'}:
raise ValueError('invalid Value record')
_, uid, ref, old, actual, old_value, value, old_present, present = fields
if not uid or uid in seen:
raise ValueError('missing or duplicate Value record identity')
seen.add(uid)
rows.append(dict(uid=uid, refdes=ref, old_part_number=old, actual_part_number=actual,
old_value=old_value, actual_value=value, old_value_present=old_present == '1',
actual_value_present=present == '1'))
return dict(value_readback=rows, value_readback_job_id=header[2],
project_path=header[4], schematic_name=header[5], sheet_number=header[6])
except (OSError, UnicodeError, ValueError, IndexError) as error:
if logger is not None:
logger.warning('value_readback_unavailable status=%s reason=%s', result.status_path, error)
return {'value_readback_status': 'unavailable'}
def recovered_value_readback_details(result, task_id):
"""Use the audit captured inside the already-verified recovery boundary."""
try:
audit = json.loads(internal_path(result.log_path.parent / 'recovery.json').read_text(encoding='utf-8'))
worker = audit['worker_result']
details = audit.get('value_details')
if (audit['task_id'] != task_id or result.state != 'restored_ok' or audit['state'] != result.state
or audit.get('native_replacement_called') is not False
or not isinstance(worker, list) or len(worker) != 4
or worker[0] != audit['job_id'] or worker[1] != result.state
or not isinstance(details, dict) or details.get('value_readback_job_id') != audit['job_id']):
return {}
return dict(details, recovery_saved_verified=worker[2] == 'recovery_saved_verified')
except (OSError, ValueError, TypeError, KeyError):
return {}
Loaded 100 of 154 files, more files were not shown because too many files have changed in this diff. Show more