Compare commits

..
Author SHA1 Message Date
阿白叔 a11eb3c766 fix: harden PCB and iCDB verification lifecycle for beta release 2026-10-26 15:32:01 +08:00
33 changed files with 1226 additions and 267 deletions

No files matched your search

+1
View File
@@ -28,6 +28,7 @@ venv/
/dist/
/dist-*/
/*.zip
/*.zip.sha256
/*.rar
# Native compiler intermediates; retain the two verified x86 runtime bridges
+1 -1
View File
@@ -237,7 +237,7 @@ function Invoke-CbbReuseBlockContentCheck {
$combined = @($stdout, $stderr) -join "`r`n"
[IO.File]::WriteAllText($report, $combined, [Text.UTF8Encoding]::new($false))
if ($result -eq 'unknown') {
if ($combined -match '(?i)completed successfully' -and $combined -notmatch '(?i)failed|error') {
if ($exitCode -eq 0 -and $combined -match '(?i)completed successfully' -and $combined -notmatch '(?i)failed|error') {
$result = 'pass'
}
elseif ($combined -match '(?i)failed|error|cannot connect|bad data') {
+5 -3
View File
@@ -1,4 +1,6 @@
# CBB 中心库上传后校验工具
当前补丁:**[v0.2.2-beta.1](docs/releases/v0.2.2-beta.1.md)(Beta 预发布,待现场验收)**,替代 0.2.1。数据库所有权或关闭状态不确定时停止批次、保留现场;不自动移走地址文件、接管带锁设计或强杀 PCB 进程树。已修复真实试跑发现的已验证块重复 Forward Annotation 问题,以及工程文件编码、目录越界、互斥和依赖漏洞。详见 [本地验证与安全审查](docs/releases/2026-10-26-local-validation-and-audit.md)。下方日期记录描述历史版本,以本补丁说明为准。
本目录是独立的 CBB 中心库上传后校验工具,不属于 Part Replacement,也不负责 CBB 工程复制或中心库上传。
@@ -10,9 +12,9 @@
| `cbb-upload-795` | CBB 工程/中心库 | 复制、Packager、上传、Verify | 中心库新增或更新 CBB |
| 本工具 | 已上传到中心库的 CBB 条目 | 在 Library Manager 中执行/确认 Verify,检查条目和 CBB 状态标志 | 中心库 CBB 校验完成且结果可追溯 |
这里的“刷新检验”明确指 CBB 上传中心库后的收尾校验:在 Library Manager 找到刚上传的 CBB,右键执行 `Verify`,处理 Verify 流程中的按钮/错误,并确认条目状态标志变为绿色。它不替换 Part Number,不同步 PCB,不改写中心库、`.prj` 或网表文件,也不负责上传动作本身。
这里的“刷新检验”指 CBB 上传中心库后的收尾校验。当前生产路径打开 ReusableCircuit PCB,执行 Project Integration、保存和关闭,再读取中心库权威状态。它不替换 Part Number,也不负责上传动作本身。
本校验工具不读取或修改 PCB 文件,也不依赖工作区中的 CBB 工程样本。
本工具会通过 Mentor 原生接口更新和保存 CBB 工程,并可能修正 `PCB/pcb.prj` 的 CentralLibrary 路径(保留原文件副本)。这不是只读检查;不要将“校验”理解为工程数据完全不变。
## 当前实现
@@ -134,7 +136,7 @@ python -m cbb_refresh_verify
- **不注入鼠标或键盘输入,不切换 Windows 桌面,不改写中心库源文件。** 生产路径在本次运行创建的私有桌面内启动中心库目录绑定的 ReusableCircuit PCB,随后用 COM 完成刷新、保存和关闭;只读状态桥在结束后确认 LMC 的 READY。Library Manager 菜单派发仅作为显式诊断路径保留。`tests/test_safety.py` 会在离线测试中阻止真实输入注入 API 重新进入生产代码。
- 所有自动化都跑在本进程创建的**私有 Windows 桌面**上:用户看不到窗口,光标不会被移动。
- 弹窗只认领本次运行自己启动的进程,并在该进程的私有桌面内投递 `BM_CLICK`。数据库异常恢复提示默认选择“最后一次用户保存的数据库”;其他弹窗优先确认/继续/恢复,只有 Project Integration 进度窗(仅有 Cancel)保持不动。所有弹窗文本和实际控件 ID 都写入 `dialogs.jsonl`。
- 超时或失败时,worker 所在 Job Object 连同 PCB、cscript 助手一并终止;如果外层 supervisor 先终止 worker,还会依据本次 CBB 的 PCB 命令行和启动前 PID 基线做一次定向清理。证据目录的 `events.jsonl` 会记录 `source=supervisor_cleanup` 的 `process_cleanup` 事件。
- 超时或失败时停止自动化客户端,保留可能仍在写库的原生会话并停止批次。监督器核对进程是否退出,记录 `process_exit_confirmed` 或 `process_retained`;无法确认释放时输出 `recovery-required.json`,不以强杀代替数据库安全关闭。
## 环境要求
@@ -0,0 +1,38 @@
# 0.2.2 异常关闭补充验证
本轮没有修改生产代码,原 0.2.2 修复包仍适用。新增 10 项故障注入/进程测试,
与此前事故及扩展安全测试合跑,**47 项全部通过**。
命令:
```powershell
build\release-env\Scripts\python.exe -m pytest -q tests/test_abnormal_shutdown.py tests/test_incident_safety.py tests/test_extended_safety.py
```
## 覆盖情况
| 场景 | 结果与边界 |
| --- | --- |
| 正常关闭忙碌中的校验界面 | 现有 UI 行为为请求停止队列、等待当前项结束,不直接退出正在操作的进程。 |
| 强制结束监督程序 | 真实 Windows 无害进程试验:worker 退出码 75,已登记的保存助手退出,模拟原生数据库的进程仍存活。 |
| PCB 在 COM 就绪前退出 | 注入目标文档断开,worker 输出 FAIL、safe_to_continue=False,不启动保存。 |
| PCB/iCDB 在同步时断开 | 注入 COM/RPC 断开错误,worker 输出 FAIL、safe_to_continue=False,不启动保存。 |
| 保存助手异常退出 | 没有日志、只有 SAVE_START 或即使残留全部成功标记,只要退出码非零,均失败。 |
| 保存/关闭证据不完整 | 助手退出码为 0,但缺少 IS_SAVED 或 CLOSE_OK,仍然失败。 |
| worker 自身崩溃 | 无 outcome 文件或留下 PASS 文件但退出码非零,监督器仍判失败;不查询旧 READY 状态把失败变成成功。 |
| 进程或数据库地址残留 | 既有测试确认不报释放成功、不自动删地址文件、不强杀原生进程。 |
| 批次后续动作 | 既有测试确认 safe_to_continue=False 停止后续项;CLI/UI 保留恢复所需的临时网络映射。 |
真实进程试验使用独立的 Python 睡眠进程模拟监督程序、保存助手和原生数据库,
没有人为强杀真正处于写库中的 Mentor/iCDB。COM 断开和保存崩溃通过边界注入验证,
不能将它们说成已经实测过原生数据库崩溃后的完整性恢复。
## 不能保证的部分
工具退出保护旨在停止新的自动化操作并保留现场。已经发出的原生 COM 操作可能仍在进行,
本工具无法保证取消或回滚该操作。若 PCB/iCDB 本身被强杀、机器断电或磁盘写入失败,
数据库可能已经不一致,需要 Mentor 原生恢复或一致备份;程序不自动选择恢复版本或覆盖文件。
正常流程的本地真实验证另见
[本地验证与安全审查](2026-10-26-local-validation-and-audit.md):最终 EXE 校验成功,
Work/Cert 正常重新打开,最终无 PCB/iCDB 残留,原样本 20,994 个文件内容未变。
@@ -0,0 +1,72 @@
# 0.2.1 — PCB / iCDB emergency safety fix
Superseded by **0.2.2** after local Mentor testing found the already-verified
Forward Annotation defect. See [local validation and audit](2026-10-26-local-validation-and-audit.md).
Base: `11a2b2e` (`r4-stability-20260929`). This patch addresses confirmed unsafe
code paths. It does not establish the cause of a particular field incident and
does not repair an already damaged PCB/database.
## Changes
- Preserve `database/cdbsvr/sAddress.adr`. A TCP timeout is not evidence that its
owner is dead. Any existing advertisement (live, unreachable or invalid)
blocks automatic Verify until the native session is reviewed.
- Block a new Verify while any Expedition PCB session is already running,
before changing project configuration or attaching to shared COM objects.
- Do not auto-confirm design-lock overrides or choose a database restoration.
Record `DATABASE_RECOVERY_REQUIRED` without clicking the prompt.
- Release Python COM references before starting the save helper; release the
helper's document/integration references before `app.Quit`.
- Observe PCB/launcher/Library Manager exit. Neither a cleanup request nor
Save/Close markers alone count as process release. A surviving iCDB address
also prevents success.
- On timeout or failure, stop automation clients without killing the native
PCB/iCDB process tree. The Job Object no longer kills native descendants when
its handle closes. Library Manager shutdown requests no longer fall back to
force termination.
- Stop the batch on `safe_to_continue=False`, including unexpected exceptions.
Preserve available evidence backups on failure. Record retained processes,
the private desktop and project in `recovery-required.json`; the UI points
the operator to the recovery evidence.
## Operational consequences
The normal path still performs Project Integration and saves through Mentor.
It is **not** a read-only PCB check. The adapter can also align the CentralLibrary
entry in `PCB/pcb.prj`, preserving its original in the evidence directory.
A failure can deliberately leave a native session alive on the private desktop.
This avoids interrupting a database write; it is reported as a failure, never as
successful release. Review the recorded desktop/PIDs with the maintainer before
closing/recovering that session. Do not delete the address/lock files merely
because a connection probe timed out. The presence of an old address may require
manual Mentor recovery even when no local PCB process remains.
Existing partial backups exclude volatile database files and network runs may
skip them; they are **not** a complete database rollback. Before field testing,
use a consistent, isolated copy made after the original native session closes.
Opening a copied database can itself cause Mentor to report a moved/copied DB.
## Validation
The incident regression suite covers address preservation, ownership checks,
recovery prompts without clicks, COM reference lifetime, retained PCB/iCDB state,
supervisor rejection of false PASS, evidence retention and batch stopping.
A real Windows Job Object test uses harmless Python processes to check that
automation abort/handle closure does not terminate a native-like child.
Run `python -m pytest -q` and `python -m compileall -q src tests`. Packaging uses
`packaging/Build-CbbRefreshVerify.ps1`, including its frozen UI smoke check.
These checks do not validate recovery of the user's affected production PCB.
Validation completed on Windows 11 / Python 3.12.10:
- Full suite: **354 passed** (including 18 incident regression cases).
- Python compilation and Git whitespace checks passed.
- The 32-bit cscript host compiled a non-executing wrapper of the save helper
successfully; this checks syntax without connecting to Mentor.
- PyInstaller 6.22.3 build and frozen UI creation/clean-shutdown smoke passed.
Live Mentor verification and damaged-database recovery remain unvalidated.
The package has not been deployed to production or tested on Windows 10.
@@ -0,0 +1,82 @@
# 0.2.2 本地功能验证与安全审查
日期:2026-10-26。基线:`11a2b2e`,在 0.2.1 紧急补丁之上继续修复。
环境:Windows 11、Python 3.12.10、Mentor EE7.9.5EE。
## 真实试跑结果
使用仓库已有的历史成功样本 `IP101GR`,完整复制到
`diagnostics/local-verification-20261026/library`。原样本 20,994 个文件、
556,098,864 字节,验证前后 SHA-256 比对没有变化。所有写操作针对隔离副本。
| 检查 | 结果及证据 |
| --- | --- |
| 0.2.1 EXE 首次试跑 | **发现功能缺陷**:四项同步均为 2,`IsForwardAnnotationAllowed=False`,程序仍调用 ForwardAnnotate;Mentor 明确拒绝。保护机制停止批次、保留会话,没有误报 PASS。 |
| 首次失败后的关闭 | 精确核对隔离工程身份后,32 位助手正常 Close(False)/Quit;记录 `IsSaved=True`、`Close=True`。未强杀 PCB/iCDB。 |
| 修复后的源码试跑 | PASS、GREEN;`IsVerifiedReuseBlock` 返回 `READY/raw=1`;保存、关闭、进程退出均确认。 |
| 再次完整校验 | 第二次 PASS、GREEN;验证同一副本可重复校验。 |
| 最终 0.2.2 EXE 实测 | 打包后的 EXE 再次完成真实校验,退出码 0、PASS/GREEN;未遗留 PCB 进程。 |
| 正常打开 Work PCB | 不带 `-ReusableCircuit`,按普通 PCB 启动,COM 工程身份匹配,正常关闭,未发生数据库打不开错误。 |
| 正常打开 Cert PCB | 同上,Cert 工程身份匹配并正常关闭。 |
| 最终 EXE 校验后再打开 Work PCB | 再次普通打开、身份核对和正常关闭通过。 |
每次成功退出后检查没有残留的对应 PCB 进程,且工程的 `sAddress.adr`
已消失。新生成的 iCDB 日志记录 `Database closed` 和 `icdb.dat closed`。
保存日志仍有 Mentor 的子客户端自动解锁警告;随后数据库关闭、两份 PCB
重新打开均成功,未将这些警告视为已证实的数据库损坏。
原始证据在 `diagnostics/local-verification-20261026`:
`frozen-first-run`、`close-first-run.log`、`source-second-run`、
`source-third-run`、`normal-reopen-work`、`normal-reopen-cert`、
`frozen-final-run`、`normal-reopen-work-final`、`source-integrity.json`。
工程与原始现场文件不包含在发布 ZIP 中。
## 本轮修复
| 问题 | 修复 |
| --- | --- |
| 已验证、全同步的块被重复执行禁止的 Forward Annotation | 尊重原生 Allowed 标志;保留后续 Save/Close 和 READY 权威确认。需要同步却不允许执行时仍报错。 |
| `pcb.prj` 容错 UTF-8 解码可能替换 ANSI 字符 | 只替换目标字段的字节;严格处理 BOM/UTF-16,备份保持原字节。 |
| 写入异常可能截断 `pcb.prj` | 同卷暂存后原子替换;替换失败保持原文件。 |
| 目录 Junction 可绕过工程范围校验 | 校验真实解析路径;交给 Mentor 的路径仍保留原盘符表达。 |
| “成功文本”覆盖非零退出码 | Python 和旧 PowerShell 内容检查都要求退出码 0。 |
| 内容检查位于互斥锁之外 | 全流程持有串行互斥锁,包括内容检查及最终状态读取。 |
| 保留原生会话后仍可能卸载临时网络盘 | CLI 和 UI 在不安全结果/异常时保留本次映射,供恢复使用。 |
| 监督程序异常退出后自动化可能继续写入 | 子进程监视监督程序句柄;停止自动化及保存助手,保留原生数据库拥有者。用真实无害 Windows 进程测试此行为。 |
| 旧树状态读取路径强杀 Library Manager | 请求正常关闭并确认退出;未关闭则保留并报错。 |
| 动态 WMI 查询与相对系统命令查找 | 固定进程查询白名单;使用系统目录中的 taskkill.exe。移除未使用的自动丢弃文档处理器。 |
| 随包/构建依赖的已知漏洞 | Pillow 12.3.0 → 13.0.0;setuptools 84.0.0 → 84.1.0。只更新独立构建环境,不修改全局 Python。 |
## 自动化验证与扫描范围
- **373 项 pytest 通过**,包含初始事故和本轮扩展回归用例。
- 两组 PowerShell 测试通过。新增测试用无害的假 importer 验证“输出成功但退出码 7”必须失败,不打开 Mentor 数据。
- Python 编译检查通过。
- PyInstaller 构建、冻结版界面启动/关闭及真实校验均通过;检查 EXE
内部代码确认为 0.2.2,内部 Pillow 版本确认为 13.0.0,VBS 和规则文件
与已测试源码逐字节一致。
- Coverage:语句覆盖 72.19%,分支覆盖 62.69%,综合 70%。实际 Mentor
子进程试跑不计入此覆盖率;没有声称全部代码分支均被覆盖。
- Bandit 扫描整个 `src`:0 高、0 中、55 低。低等级提示分类:
8 个 subprocess 导入、12 个受控参数列表调用、25 个容错异常处理、
8 个诊断枚举跳过、2 个 Windows 打开日志目录操作。原始报告全部保留。
- 一处全接口端口检测明确标注 B104 排除:只尝试 bind 后立即关闭,
不调用 listen/accept,不提供网络服务。
- pip-audit 检查 18 个运行/构建依赖及其依赖:首次发现 38 条已知公告
(Pillow 37、setuptools 1),升级后复扫 **0 条已知漏洞**。
- 发布依赖清单:`packaging/requirements-build.txt`。扫描报告与覆盖率数据
在证据目录的 `bandit-final.json`、`dependency-audit-fixed.json`、`coverage.json`。
扫描覆盖本仓库 Python 运行代码、相关旧 PowerShell 入口和本次构建依赖;
没有对闭源 Mentor 二进制或整台 Windows 系统作“无漏洞”保证。
## 使用范围与未验证项
本轮真实样本为单个已经验证过的 IP101GR。非绿色状态推进、错误弹窗、
超时、取消、网络映射故障、并发和异常终止主要由隔离自动测试覆盖。
没有使用生产网络库、生产 DMS 凭据,也没有在 Windows 10 上运行。
本补丁防止不安全的继续操作;没有恢复用户原事故中受损的 PCB。
Verify 本身会通过 Mentor 更新工程数据,不能当作只读检查。
异常时可能保留私有桌面中的原生会话和临时盘符;查看恢复记录后进行
正常关闭或使用 Mentor 恢复流程,不应直接删锁或强杀数据库进程。
+41
View File
@@ -0,0 +1,41 @@
# CBB Verify v0.2.2-beta.1
**Beta 预发布,供隔离副本和小范围现场验收使用,尚未完成生产验收。**
本版本修复 PCB/iCDB 校验流程中的会话释放与失败收尾风险。运行逻辑与已完成
本地真实验证的 0.2.2 一致,发布版本标识为 `0.2.2b1`,Git 标签为
`v0.2.2-beta.1`。
## 主要修复
- 保存关闭前释放 COM 引用,确认 PCB 退出及数据库地址释放后才允许成功。
- 不自动移走 `sAddress.adr`、接管带锁设计或选择数据库恢复;异常时停止批次,
保留原生会话、证据及恢复所需的网络映射。
- 监督程序异常退出时停止自动化和保存助手,不连带强杀原生 PCB/iCDB。
- 已验证且全同步的 CBB 尊重 Mentor 的 Allowed 标志,避免重复执行被禁止的
Forward Annotation;仍要求 Save/Close 和权威 READY 确认。
- 保留 `pcb.prj` 原始编码和备份,以同卷原子替换防止截断;阻止目录链接越界。
- 全流程串行互斥;Python/PowerShell 内容检查不再让成功文本覆盖非零退出码。
- 更新 Pillow 至 13.0.0、构建 setuptools 至 84.1.0;18 个依赖复扫无已知漏洞。
## 已有验证
- Windows 11 / Mentor EE7.9.5EE,本地 IP101GR 隔离副本重复真实校验通过。
- Work/Cert PCB 普通重新打开及关闭通过;最终无 PCB/iCDB 残留。
- 原样本 20,994 个文件内容指纹保持不变。
- 发布前全量 Python 测试 **383 项通过**,两组 PowerShell 测试和编译检查通过。
测试期间出现 1 条 pywinauto 的 STA COM 模式提示,未导致失败。
冻结版 UI 检查及构建指纹记录在安装包的 `BUILD-MANIFEST.json` 中。
- 异常关闭专项覆盖 47 项相关测试,包括真实 Windows 无害进程的监督程序退出试验。
## 安装及验收边界
下载 ZIP 后完整解压,运行 `CbbRefreshVerify.exe`,保留 `_internal` 目录。
先在正常关闭后取得的一致、隔离工程副本上验收,不直接覆盖原事故数据。
现场仍需验证客户实际机器、生产网络库、DMS、多个未校验 CBB 的连续运行和
原事故样本;Windows 10 尚未验证。本补丁不代表受损 PCB 已恢复,也不能保证
PCB/iCDB 被强杀或机器断电后数据库自动修复。
详见 [本地验证与安全审查](2026-10-26-local-validation-and-audit.md) 和
[异常关闭补充验证](2026-10-26-abnormal-shutdown-validation.md)。
+19
View File
@@ -0,0 +1,19 @@
# Audited Windows release environment, 2026-10-26.
PySide6==6.11.2
PySide6_Addons==6.11.2
PySide6_Essentials==6.11.2
altgraph==0.17.5
comtypes==1.4.16
et_xmlfile==2.0.0
openpyxl==3.1.5
packaging==26.3
pefile==2024.8.26
pillow==13.0.0
pyinstaller==6.22.3
pyinstaller-hooks-contrib==2026.7
pywin32==312
pywin32-ctypes==0.2.3
pywinauto==0.6.9
setuptools==84.1.0
shiboken6==6.11.2
six==1.17.0
+3 -3
View File
@@ -1,11 +1,11 @@
[build-system]
requires = ["setuptools>=75"]
requires = ["setuptools>=84.1.0"]
build-backend = "setuptools.build_meta"
[project]
name = "cbb-refresh-verify"
version = "0.2.0"
version = "0.2.2b1"
requires-python = ">=3.12,<3.13"
dependencies = ["PySide6>=6.7,<7", "pywin32>=306", "pywinauto>=0.6.9,<0.7"]
dependencies = ["PySide6>=6.7,<7", "pywin32>=306", "pywinauto>=0.6.9,<0.7", "Pillow>=13.0.0,<14"]
[project.scripts]
cbb-refresh-verify = "cbb_refresh_verify.application:main"
+1 -1
View File
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.2.2b1"
+7 -1
View File
@@ -98,7 +98,13 @@ def main(argv: Sequence[str] | None = None) -> int:
mapped_remote_root=lease.remote_root,
native_lm_verify=native_lm_verify_enabled())
for name in names]
results = run_batch(requests)
try:
results = run_batch(requests)
except Exception:
lease.retain_for_recovery()
raise
if any(not result.safe_to_continue for result in results):
lease.retain_for_recovery()
report = write_batch_report(requests, results, context.root/'reports')
logger.info('batch_report path=%s', report)
logger.info('failure_report path=%s', failure_report_path(report))
@@ -15,22 +15,18 @@
{
"rule_id": "database-restoration",
"priority": 95,
"category": "SAFE_RECOVERY",
"category": "FATAL_ACK",
"phases": ["PCB_STARTING", "DATABASE_RECOVERY", "DOCUMENT_ATTACH"],
"semantic_any": ["database restoration", "last user saved database", "数据库恢复"],
"action": "restore_last_saved",
"channels": ["uia_select", "win32_button_click", "uia_invoke"],
"postcondition": "window_gone"
"error_code": "DATABASE_RECOVERY_REQUIRED"
},
{
"rule_id": "open-lock-confirm",
"priority": 90,
"category": "SAFE_RECOVERY",
"category": "FATAL_ACK",
"phases": ["PCB_STARTING", "DOCUMENT_ATTACH"],
"semantic_any": ["continue to open the design with read/write access permission", "confirm that no other users have the design open"],
"action": "confirm_open_lock",
"channels": ["win32_button_click", "uia_invoke"],
"postcondition": "window_gone"
"error_code": "DATABASE_RECOVERY_REQUIRED"
},
{
"rule_id": "forward-annotation-confirm",
+62 -22
View File
@@ -1,5 +1,7 @@
"""Launch automation on a private Windows desktop, never the input desktop."""
import subprocess
import os
import threading
import win32con
import win32event
import win32job
@@ -7,6 +9,46 @@ import win32process
import win32service
_automation_clients = []
_automation_clients_lock = threading.Lock()
def launch_save_helper(command, **kwargs):
"""Register the save client atomically with respect to supervisor loss."""
with _automation_clients_lock:
helper = subprocess.Popen(command, **kwargs)
_automation_clients.append(helper)
return helper
def start_supervisor_watchdog(supervisor_pid):
"""A crashed supervisor must not leave unsupervised automation writing.
Stop only this Python worker and its registered automation clients. Native
database owners remain alive, including a COM call already in progress.
"""
import win32api
parent = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(supervisor_pid))
def watch():
try:
win32event.WaitForSingleObject(parent, win32event.INFINITE)
with _automation_clients_lock:
for helper in _automation_clients:
try:
if helper.poll() is None:
helper.terminate()
except OSError:
pass
finally:
parent.Close()
os._exit(75)
thread = threading.Thread(target=watch, name='supervisor-watchdog', daemon=True)
thread.start()
return thread
def startup_desktop_name(name, *, interactive_input=False):
"""Return the CreateProcess desktop target for a worker.
@@ -25,8 +67,8 @@ class DesktopWorker:
self.interactive_input = bool(interactive_input)
self.desktop = None if self.interactive_input else win32service.CreateDesktop(
name, 0, win32con.GENERIC_ALL, None)
# The worker spawns Expedition PCB and a cscript helper. Bind the whole
# tree to a job so a supervisor kill can never orphan a process.
# Track the session, but never use kill-on-close: PCB descendants can
# still own a database transaction when the Python supervisor exits.
self.job = win32job.CreateJobObject(None, '')
self.job_bound = False
self.companions = []
@@ -39,9 +81,6 @@ class DesktopWorker:
win32con.CREATE_NO_WINDOW, None, None, startup)
thread.Close()
try:
info = win32job.QueryInformationJobObject(self.job, win32job.JobObjectExtendedLimitInformation)
info['BasicLimitInformation']['LimitFlags'] |= win32job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
win32job.SetInformationJobObject(self.job, win32job.JobObjectExtendedLimitInformation, info)
win32job.AssignProcessToJobObject(self.job, self.handle)
self.job_bound = True
except Exception:
@@ -80,20 +119,23 @@ class DesktopWorker:
return self.poll()
def kill(self):
if self.job_bound:
try:
win32job.TerminateJobObject(self.job, 1)
return
except Exception:
pass
# Fallback: terminate the worker and every descendant it launched.
"""Stop automation clients only; preserve native PCB/iCDB sessions."""
try:
subprocess.run(['taskkill', '/PID', str(self.pid), '/T', '/F'],
capture_output=True, timeout=20,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
win32process.TerminateProcess(self.handle, 1)
except Exception:
pass
# The worker is now stopped and cannot spawn another save helper.
from .mentor_adapter import _process_details, _terminate_pid
try:
for pid, _exe, command, parent in _process_details('cscript.exe'):
if (int(parent or 0) == self.pid
and 'save_verified_block.vbs' in str(command).casefold()):
_terminate_pid(pid) # single automation process, no /T
except Exception:
pass
for companion in self.companions:
try:
win32process.TerminateProcess(self.handle, 1)
win32process.TerminateProcess(companion, 1)
except Exception:
pass
@@ -103,10 +145,7 @@ class DesktopWorker:
return self.poll()
def close(self):
# A job may be unavailable on a machine where the worker is already
# assigned to another job. In that case merely closing our handles
# would orphan PCB/cscript descendants, so terminate the tree before
# releasing the desktop and job handles.
# Releasing Python resources must not kill native database owners.
try:
if self.poll() is None:
self.kill()
@@ -114,14 +153,15 @@ class DesktopWorker:
pass
for companion in self.companions:
try:
if win32process.GetExitCodeProcess(companion) == 259:
win32process.TerminateProcess(companion, 1)
companion.Close()
except Exception:
pass
self.companions.clear()
self.companion_pids.clear()
self.handle.Close()
# Closing the last job handle reaps any survivor thanks to
# KILL_ON_JOB_CLOSE; this also covers a supervisor crash.
# Native processes keep their desktop alive if manual recovery is needed.
self.job.Close()
if self.desktop is not None:
self.desktop.CloseDesktop()
+8
View File
@@ -193,6 +193,14 @@ def decide(snapshot: WindowSnapshot, rules: Sequence[DialogRule]) -> RuleDecisio
and not snapshot.controls):
return RuleDecision(rule_id='ime-floating-language-bar', category='OBSERVE_PROGRESS')
combined = normalize_text(' '.join((snapshot.title, *snapshot.texts)))
# These prompts require knowledge of database ownership or a recovery
# choice. A generic Yes/restore rule cannot establish either fact.
if any(marker in combined for marker in (
'continue to open the design with read/write access permission',
'confirm that no other users have the design open',
'database restoration', 'last user saved database', '数据库恢复')):
return RuleDecision(error_code='DATABASE_RECOVERY_REQUIRED',
detail=' | '.join(snapshot.texts) or snapshot.title)
if ('problem encountered while opening one of the database files' in combined
and 'loading of the job has failed' in combined):
return RuleDecision(
+2 -2
View File
@@ -370,14 +370,14 @@ def classify_dialog(snapshot: DialogSnapshot) -> DialogDecision:
if ("database restoration" in title
or ("restore database" in joined and "last user saved database" in joined)
or "数据库恢复" in joined):
return DialogDecision(action="restore_last_saved")
return DialogDecision(error_code="DATABASE_RECOVERY_REQUIRED", detail=combined)
if "select the desired product license" in joined:
return DialogDecision(action="accept_license")
if "do you want to save the reusable block cell" in joined:
return DialogDecision(action="save_reusable_block")
if ("continue to open the design with read/write access permission" in joined
or "confirm that no other users have the design open" in joined):
return DialogDecision(action="confirm_open_lock")
return DialogDecision(error_code="DATABASE_RECOVERY_REQUIRED", detail=combined)
if "tip of the day" in combined:
return DialogDecision(action="dismiss_tip")
+7
View File
@@ -51,6 +51,13 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time
emit(stage='integration_status', statuses=state)
if green_entry is None:
green_entry = all(status == 2 for status in state.values())
if green_entry and not bool(value(pi, 'IsForwardAnnotationAllowed')):
# Verified blocks can forbid another ForwardAnnotate despite
# all four statuses being InSynch. Respect the native gate;
# the caller still requires Save/Close and authoritative READY.
emit(stage='integration_complete', statuses=state, actions=[],
policy='already_synchronized_native_action_disallowed')
return state
if previous_state is not None and state != previous_state and deadline is not None:
deadline.touch()
previous_state = state
@@ -150,13 +150,11 @@ class LibraryVerifySession:
pass
try:
if self.process.poll() is None:
self.process.terminate()
self.process.wait(timeout=5)
except Exception:
try:
self.process.kill()
except Exception:
pass
# A slow native shutdown can still be flushing the library.
# The caller records the retained process and stops the batch.
pass
def launch_library_verify(
@@ -291,9 +289,6 @@ def launch_library_verify(
session = LibraryVerifySession(target, process, int(tree.handle), library, block_name)
return session
except Exception:
if session is None:
try:
process.terminate()
except Exception:
pass
emit(stage='lm_recovery_required', lm_pid=process.pid,
detail='Verify 启动异常;保留原生会话供检查,停止批次。')
raise
+140 -140
View File
@@ -25,6 +25,8 @@ import traceback
import logging
import shutil
import socket
import gc
import tempfile
from collections import Counter
logger = logging.getLogger(__name__)
@@ -109,31 +111,46 @@ def ensure_pcb_central_library(pcb_path, lmc_path, *, evidence_dir=None,
# Different drive letters or an unresolved UNC mapping cannot be
# represented relatively; preserve the explicit selected path.
project_target = target
try:
with project.open('r', encoding='utf-8', errors='replace', newline='') as stream:
original = stream.read()
except OSError:
return False
changed = False
def replace_match(match):
nonlocal changed
if match.group(2) == project_target:
return match.group(0)
changed = True
return f'{match.group(1)}{project_target}{match.group(3)}'
updated = _CENTRAL_LIBRARY_KEY.sub(replace_match, original)
if not changed:
original = project.read_bytes()
if original.startswith((b'\xff\xfe', b'\xfe\xff')):
# Strict, round-trippable Unicode; never substitute undecodable bytes.
codec = 'utf-16-le' if original.startswith(b'\xff\xfe') else 'utf-16-be'
text = original[2:].decode(codec)
updated = original[:2] + _CENTRAL_LIBRARY_KEY.sub(
lambda match: match[1] + project_target + match[3], text).encode(codec)
else:
# Patch only the ASCII key value, preserving all native ANSI comments,
# unrelated fields, newline bytes and the exact original backup.
try:
target_bytes = project_target.encode('ascii')
except UnicodeEncodeError:
if original.startswith(b'\xef\xbb\xbf'):
codec = 'utf-8'
elif any(byte > 127 for byte in original):
try:
original.decode('utf-8')
codec = 'utf-8'
except UnicodeDecodeError:
codec = 'mbcs'
else:
codec = 'mbcs' # unmarked EE7.9 native text uses Windows ACP
target_bytes = project_target.encode(codec, errors='strict')
bom = b'\xef\xbb\xbf' if original.startswith(b'\xef\xbb\xbf') else b''
pattern = re.compile(_CENTRAL_LIBRARY_KEY.pattern.encode('ascii'), re.I | re.M)
updated = bom + pattern.sub(
lambda match: match[1] + target_bytes + match[3], original[len(bom):])
if updated == original:
emit(stage='central_library_path', status='MATCH', path=str(project), target=target)
return False
if evidence_dir is not None:
evidence = Path(evidence_dir)
evidence.mkdir(parents=True, exist_ok=True)
(evidence / 'pcb.prj.before-central-library').write_text(original, encoding='utf-8')
with project.open('w', encoding='utf-8', newline='') as stream:
stream.write(updated)
(evidence / 'pcb.prj.before-central-library').write_bytes(original)
# Stage on the same volume. Failure cannot leave a truncated project file.
with tempfile.TemporaryDirectory(prefix='.cbb-prj-write-', dir=project.parent) as temporary:
staged = Path(temporary) / 'pcb.prj'
staged.write_bytes(updated)
os.replace(staged, project)
emit(stage='central_library_path', status='UPDATED', path=str(project), target=target)
return True
@@ -193,14 +210,19 @@ def launch_pcb_process(command, cwd):
def _process_details(name):
queries = {
'ExpeditionPCB.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='ExpeditionPCB.exe'",
'LibraryManager.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='LibraryManager.exe'",
'cscript.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='cscript.exe'",
}
if name not in queries:
raise ValueError(f'Unsupported process query: {name}')
import win32com.client
service = win32com.client.GetObject('winmgmts:')
return [
(int(p.ProcessId), str(p.ExecutablePath or ''), str(p.CommandLine or ''),
int(p.ParentProcessId or 0))
for p in service.ExecQuery(
f"SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine "
f"FROM Win32_Process WHERE Name='{name}'")
for p in service.ExecQuery(queries[name])
]
@@ -338,8 +360,10 @@ def choose_dialog_action(title, texts):
joined = ' '.join(values).lower()
if ('database restoration' in str(title or '').lower()
or ('restore database' in joined and 'last user saved database' in joined)
or '数据库恢复' in joined):
return 'restore_last_saved'
or '数据库恢复' in joined
or 'continue to open the design with read/write access permission' in joined
or 'confirm that no other users have the design open' in joined):
return None
if 'your task is being' in joined and 'elapsed time' in joined:
return None
positive = ('yes', 'ok', 'continue', 'retry', 'restore', 'open', 'use', 'load',
@@ -582,6 +606,11 @@ def execute_job(job, emit):
root = Path(job['root'])
block_root = Path(job['project']).parent.parent
# COM publishes a shared active application. Never attach while another
# native PCB session (including a retained failure) is still running.
pcb_before = {pid for pid, _, _ in pcb_processes()}
if pcb_before:
raise RuntimeError(f'PCB_SESSION_ACTIVE: 已有 PCB 会话,请正常关闭后再校验: {sorted(pcb_before)}')
# Do not recursively snapshot a live CBB over SMB immediately before
# launching Mentor. The backup is evidence only; on Win10 a timed-out
# robocopy can retain network handles/oplocks and make Verify nondeterministic.
@@ -606,8 +635,10 @@ def execute_job(job, emit):
# database/log directories are intentionally excluded to avoid file locks.
icdb_address = prepare_icdb_address(
Path(job['project']).parent / 'database', root, emit=emit)
if icdb_address.get('status') == 'STALE_UNMOVED':
emit(stage='icdb_address_warning', detail='旧 iCDB 地址无法隔离;继续启动以保留现场。')
if icdb_address.get('status') != 'ABSENT':
raise RuntimeError(
'ICDB_SESSION_UNCONFIRMED: 数据库存在服务器地址记录,无法确认独占使用;'
'已保留地址文件,请先检查原 PCB/iCDB 会话。' + str(icdb_address))
from .verify_diagnostics import collect_preflight_diagnostics, collect_native_failure_logs
diagnostics = collect_preflight_diagnostics(job['pcb'], root)
emit(stage='preflight_diagnostics', non_blocking=True,
@@ -633,7 +664,6 @@ def execute_job(job, emit):
}
except OSError:
integration_offsets[path] = {'size': 0, 'mtime_ns': None, 'digest': ''}
pcb_before = {pid for pid, _, _ in pcb_processes()}
emit(stage='pcb_baseline', pids=sorted(pcb_before))
lm_session = None
lm_pid = None
@@ -642,6 +672,7 @@ def execute_job(job, emit):
launcher_process = None
completed = False
helper = None
app = doc = pi = None
try:
ensure_pcb_central_library(
job['pcb'],
@@ -717,11 +748,16 @@ def execute_job(job, emit):
timeout_seconds=job['timeout'],
error_reader=lambda: pcb_log_reports_integration_error(job['pcb'], offsets=integration_offsets),
)
# The x86 helper acquires its own COM references. Keeping these Python
# references alive prevents the native PCB server from exiting cleanly.
pi = doc = app = None
gc.collect()
emit(stage='save_start')
script = Path(__file__).with_name('save_verified_block.vbs')
cscript = Path(os.environ.get('WINDIR', r'C:\Windows'))/'SysWOW64'/'cscript.exe'
from .desktop_worker import launch_save_helper
with (root/'save.log').open('w', encoding='utf-8') as log:
helper = subprocess.Popen([str(cscript), '//nologo', str(script), job['project']],
helper = launch_save_helper([str(cscript), '//nologo', str(script), job['project']],
stdout=log, stderr=log, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
emit(stage='save_helper', helper_pid=helper.pid)
last_markers = ()
@@ -753,17 +789,16 @@ def execute_job(job, emit):
process_exit_deadline = time.monotonic() + (15 if 'QUIT_OK' not in markers else 30)
while any(p in {lm_pid, worker_pid, worker_parent_pid} for p, _, _ in pcb_processes()):
if time.monotonic() >= process_exit_deadline:
emit(stage='pcb_exit_recovered', detail='Save/Close 已确认但 PCB 未自行退出,执行定向清理。')
cleanup_verify_processes(
lm_pid,
worker_pid,
worker_parent_pid,
expected_pcb=job.get('pcb'),
baseline_pids=pcb_before,
emit=emit,
)
break
raise RuntimeError('PCB_RELEASE_UNCONFIRMED: Save/Close 返回成功,但未确认 PCB 退出;保留原生进程并停止批次。')
time.sleep(.5)
released = cleanup_verify_processes(
lm_pid, worker_pid, worker_parent_pid, expected_pcb=job.get('pcb'),
baseline_pids=pcb_before, emit=emit)
if not released or not released.get('safe_to_continue'):
raise RuntimeError(f'PCB_RELEASE_UNCONFIRMED: 未确认 PCB 退出: {released}')
address = Path(job['project']).parent / 'database/cdbsvr/sAddress.adr'
if address.exists():
raise RuntimeError(f'ICDB_RELEASE_UNCONFIRMED: PCB 已退出但数据库地址记录仍存在;保留文件并停止批次: {address}')
cert_pcb = block_root/'Cert'/Path(job['pcb']).relative_to(block_root/'Work')
cert_evidence = collect_cert_evidence(block_root, cert_project, cert_pcb)
emit(stage='cert_diagnostics', **cert_evidence)
@@ -771,6 +806,8 @@ def execute_job(job, emit):
emit(stage='complete', status='PASS', certified_project=str(cert_project), cert_evidence=cert_evidence)
return {'status': 'PASS', 'stdout': 'ReusableCircuit 校验;四项 InSynch;Save/IsSaved/Close 成功;PCB 已退出。'}
finally:
pi = doc = app = None
gc.collect()
if not completed:
try:
logs = collect_native_failure_logs(job['pcb'], root)
@@ -781,7 +818,8 @@ def execute_job(job, emit):
# Diagnostic failure must not erase the native error or cleanup.
pass
if helper is not None and helper.poll() is None:
_terminate(helper, grace_seconds=1)
# Stop only the automation client, never a native database tree.
helper.terminate()
cleanup_verify_processes(
lm_pid,
worker_pid,
@@ -790,15 +828,15 @@ def execute_job(job, emit):
baseline_pids=pcb_before,
emit=emit,
)
if lm_session is not None:
if completed and lm_session is not None:
lm_session.close()
def _terminate_pid(pid, *, timeout=20):
"""Terminate only the PCB PID associated with the current Verify item."""
"""Terminate a validated automation helper PID, never its native children."""
try:
subprocess.run(
['taskkill', '/PID', str(int(pid)), '/T', '/F'],
[str(Path(os.environ.get('WINDIR', r'C:\Windows'))/'System32'/'taskkill.exe'), '/PID', str(int(pid)), '/F'],
capture_output=True,
timeout=timeout,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0),
@@ -809,49 +847,30 @@ def _terminate_pid(pid, *, timeout=20):
def cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid=None, *,
expected_pcb=None, baseline_pids=(), emit=None):
"""Terminate every process created by one Verify session, child first.
"""Observe native process release without interrupting database writes.
The worker can be killed by the outer supervisor before it has observed the
``common``/``wg`` pair or before its own ``finally`` block runs. In that
case the explicit PIDs are incomplete, so discover only new
``ExpeditionPCB.exe`` processes whose command line contains this CBB's
``-ReusableCircuit`` path. Existing PCB sessions are excluded by the
baseline snapshot.
A cleanup request is not evidence of release. Retained or unobservable
sessions block the batch and require recovery; they must never be killed.
"""
seen = set()
targets = [('pcb_worker', worker_pid), ('pcb_launcher', worker_parent_pid)]
baseline = {int(pid) for pid in (baseline_pids or ())}
if expected_pcb:
try:
records = _process_details('ExpeditionPCB.exe')
except Exception:
records = ()
discovered = []
for pid, executable, command, parent_pid in records:
pid = int(pid)
if pid in baseline or not is_reusable_circuit_pcb(executable, command, expected_pcb):
continue
discovered.append((pid, int(parent_pid or 0), str(executable or '').casefold()))
discovered_pids = {pid for pid, _parent_pid, _executable in discovered}
# The worker path is the child in the normal common/wg pair. Parent
# first is intentionally avoided here: taskkill /T on the child is
# harmless, and the explicit parent is terminated in the next pass.
for pid, parent_pid, executable in discovered:
role = 'pcb_worker' if '\\wg\\' in executable else 'pcb_launcher'
targets.append((role, pid))
# Do not infer an arbitrary parent (usually this Python worker) as
# a Mentor launcher. Only add a parent that is itself one of the
# newly discovered PCB pair processes.
if parent_pid in discovered_pids:
targets.append(('pcb_launcher', parent_pid))
targets.append(('library_manager', lm_pid))
for role, pid in targets:
if not pid or int(pid) in seen:
continue
seen.add(int(pid))
_terminate_pid(int(pid))
if emit is not None:
emit(stage='process_cleanup', role=role, pid=int(pid))
tracked = {int(pid) for pid in (lm_pid, worker_pid, worker_parent_pid) if pid}
try:
pcb_records = _process_details('ExpeditionPCB.exe')
lm_records = _process_details('LibraryManager.exe') if lm_pid else ()
for pid, executable, command, _parent in pcb_records:
if (expected_pcb and int(pid) not in baseline
and is_reusable_circuit_pcb(executable, command, expected_pcb)):
tracked.add(int(pid))
alive = {int(record[0]) for record in (*pcb_records, *lm_records)}
remaining = sorted(tracked & alive)
result = {'safe_to_continue': not remaining, 'remaining_pids': remaining}
except Exception as error:
result = {'safe_to_continue': False, 'remaining_pids': sorted(tracked),
'error': str(error)}
if emit is not None:
emit(stage=('process_exit_confirmed' if result['safe_to_continue']
else 'process_retained'), **result)
return result
def _terminate(process, *, grace_seconds=5, kill_timeout_seconds=20):
@@ -862,7 +881,7 @@ def _terminate(process, *, grace_seconds=5, kill_timeout_seconds=20):
return
except subprocess.TimeoutExpired:
pass
subprocess.run(['taskkill', '/PID', str(process.pid), '/T', '/F'], capture_output=True,
subprocess.run([str(Path(os.environ.get('WINDIR', r'C:\Windows'))/'System32'/'taskkill.exe'), '/PID', str(process.pid), '/T', '/F'], capture_output=True,
timeout=max(1.0, float(kill_timeout_seconds)),
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
except Exception:
@@ -872,25 +891,6 @@ def _terminate(process, *, grace_seconds=5, kill_timeout_seconds=20):
pass
def abort_job_document(job):
"""Discard a failed item's PCB document so later CBBs can still run."""
try:
import win32com.client
app = win32com.client.GetActiveObject('MGCPCB.Application')
doc = app.ActiveDocument
if doc and same_path(doc.ProjectIntegration.ProjectFile, job['project']):
try:
doc.Close(False)
except Exception:
pass
try:
app.Quit()
except Exception:
pass
except Exception:
pass
def worker_main(request_path):
import pythoncom
pythoncom.CoInitialize()
@@ -904,6 +904,9 @@ def worker_main(request_path):
stream.write(json.dumps(event, ensure_ascii=False) + '\n')
try:
if job.get('supervisor_pid'):
from .desktop_worker import start_supervisor_watchdog
start_supervisor_watchdog(job['supervisor_pid'])
start_gate = Path(job.get('start_gate', '')) if job.get('start_gate') else None
if start_gate is not None:
gate_deadline = time.monotonic() + 15
@@ -914,10 +917,8 @@ def worker_main(request_path):
outcome = execute_job(job, emit)
except Exception as error:
traceback.print_exc()
abort_job_document(job)
message = str(error)
unsafe = any(marker in message for marker in ('timeout', '超时', 'unknown dialog', '未确认 PCB 退出'))
outcome = {'status': 'FAIL', 'stderr': message, 'safe_to_continue': not unsafe}
outcome = {'status': 'FAIL', 'stderr': message, 'safe_to_continue': False}
emit(stage='failed', error=message)
finally:
pythoncom.CoUninitialize()
@@ -986,14 +987,10 @@ def probe_icdb_server(hosts, port, *, timeout_seconds=0.25):
def prepare_icdb_address(database_dir, evidence_root, *, emit=lambda **event: None):
"""Quarantine an unreachable stale iCDB address advertisement.
"""Inspect the advertisement without removing database ownership state.
``sAddress.adr`` is volatile runtime state. When a previous PCB/cdbsvr
session crashed, EE7.9 keeps the old network endpoint and the next PCB
launch spends its entire startup window retrying that dead server instead
of starting a local one. We preserve the file in this run's evidence and
remove only the unreachable advertisement; a live endpoint is never
touched.
Network timeout/refusal does not prove that the server has exited. Even a
reachable endpoint does not establish exclusive access for this Verify.
"""
address = Path(database_dir) / 'cdbsvr' / 'sAddress.adr'
if not address.is_file():
@@ -1012,20 +1009,9 @@ def prepare_icdb_address(database_dir, evidence_root, *, emit=lambda **event: No
'hosts': parsed['hosts'], 'detail': detail}
emit(stage='icdb_address_check', **result)
return result
archive = (Path(evidence_root) / 'diagnostics' / 'cdbsvr' /
f"sAddress.adr.stale-{time.strftime('%Y%m%d-%H%M%S')}")
try:
archive.parent.mkdir(parents=True, exist_ok=True)
shutil.move(str(address), str(archive))
except OSError as error:
result = {'status': 'STALE_UNMOVED', 'path': str(address), 'port': parsed['port'],
'hosts': parsed['hosts'], 'detail': f'{detail}; {error}'}
emit(stage='icdb_address_check', **result)
return result
result = {'status': 'STALE_MOVED', 'path': str(address), 'archive': str(archive),
result = {'status': 'UNREACHABLE', 'path': str(address),
'port': parsed['port'], 'hosts': parsed['hosts'], 'detail': detail}
emit(stage='icdb_address_check', **result)
emit(stage='icdb_address_stale', **result)
return result
@@ -1040,7 +1026,8 @@ def icdb_ports_bindable(ports=(10000, 10050, 10100)):
for port in ports:
probe = socket.socket()
try:
probe.bind(('0.0.0.0', port))
# Temporary native port preflight only; never listen or accept.
probe.bind(('0.0.0.0', port)) # nosec B104
return True, f'端口 {port} 可用'
except OSError as error:
failures.append(f'{port}: {error}')
@@ -1310,6 +1297,25 @@ def _private_library_window(desktop, library):
return None
def close_library_reader(pid, desktop, *, timeout_seconds=5):
"""Request native close and observe exit, never force-kill a reader."""
import win32con, win32gui, win32process
try:
for hwnd in desktop.EnumDesktopWindows():
if (win32process.GetWindowThreadProcessId(hwnd)[1] == int(pid)
and win32gui.GetWindowText(hwnd).startswith('Library Manager')):
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
deadline = time.monotonic() + max(0, timeout_seconds)
while True:
if not any(int(item[0]) == int(pid) for item in _process_details('LibraryManager.exe')):
return True
if time.monotonic() >= deadline:
return False
time.sleep(.2)
except Exception:
return False
def read_library_flag(library, name, *, timeout_seconds=90, log_path=None):
"""Return (flag, detail) for one CBB by reading its tree icon.
@@ -1372,13 +1378,7 @@ def read_library_flag(library, name, *, timeout_seconds=90, log_path=None):
except Exception as error:
return FLAG_UNKNOWN, f'状态读取失败: {error}'
finally:
if pid is not None:
try:
subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True,
timeout=5,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
except (OSError, subprocess.TimeoutExpired):
pass
released = pid is None or close_library_reader(pid, desktop)
if handle is not None:
try:
handle.Close()
@@ -1389,6 +1389,9 @@ def read_library_flag(library, name, *, timeout_seconds=90, log_path=None):
except Exception:
pass
if not released:
raise RuntimeError(f'LIBRARY_RELEASE_UNCONFIRMED: pid={pid}; desktop={desktop_name}')
def read_library_tree_image_map(library, names, *, timeout_seconds=45, log_path=None):
"""Read all CBB tree icon resource IDs in one private LM session.
@@ -1463,13 +1466,7 @@ def read_library_tree_image_map(library, names, *, timeout_seconds=45, log_path=
logger.warning('library_tree_image_item_missing name=%s reason=tree_path_lookup_failed', name)
return result
finally:
if pid is not None:
try:
subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True,
timeout=5,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
except (OSError, subprocess.TimeoutExpired):
pass
released = pid is None or close_library_reader(pid, desktop)
if handle is not None:
try:
handle.Close()
@@ -1480,6 +1477,9 @@ def read_library_tree_image_map(library, names, *, timeout_seconds=45, log_path=
except Exception:
pass
if not released:
raise RuntimeError(f'LIBRARY_RELEASE_UNCONFIRMED: pid={pid}; desktop={desktop_name}')
def read_library_status_map(library, names, *, timeout_seconds=45, log_path=None,
image_status_map=None):
+6 -1
View File
@@ -178,6 +178,7 @@ class LibraryPathLease:
created: bool = False
_backend: WNetBackend | None = None
_closed: bool = False
_retained_for_recovery: bool = False
@classmethod
def acquire(cls, path, *, backend: WNetBackend | None = None) -> "LibraryPathLease":
@@ -224,11 +225,15 @@ class LibraryPathLease:
f"NETWORK_UNAVAILABLE: 无法挂载中心库 {remote_root}: {error}"
) from error
def retain_for_recovery(self) -> None:
"""Keep the Mentor drive available to a retained native session."""
self._retained_for_recovery = True
def close(self) -> None:
if self._closed:
return
self._closed = True
if not self.created or self._backend is None or not self.drive:
if self._retained_for_recovery or not self.created or self._backend is None or not self.drive:
return
try:
current = self._backend.mappings().get(self.drive)
@@ -45,10 +45,14 @@ operationOK = doc.Close(True)
LogElapsed "close", measuredStart
If Not operationOK Then WScript.Quit 6
WScript.Echo "CLOSE_OK"
Set pi = Nothing
Set doc = Nothing
Set licensing = Nothing
measuredStart = Timer
app.Quit
LogElapsed "quit", measuredStart
WScript.Echo "QUIT_OK"
Set app = Nothing
Sub LogElapsed(phaseName, started)
Dim elapsed
+6
View File
@@ -383,6 +383,8 @@ class MainWindow(QWidget):
def _batch_done(self, outcome):
results, report = outcome
if self.library_lease is not None and any(not result.safe_to_continue for result in results):
self.library_lease.retain_for_recovery()
for result in results:
if not result.safe_to_continue and result.evidence_path:
self.recovery_path = result.evidence_path.parent
@@ -392,6 +394,8 @@ class MainWindow(QWidget):
failed = len(results)-passed-skipped
text = (f'已结束:成功 {passed},失败 {failed},预检跳过 {len(self.skipped_names)},'
f'队列未执行 {skipped}。\n完整日志:{report}\n仅失败日志:{failure_report_path(report)}')
if any(not result.safe_to_continue for result in results):
text += '\n未确认数据库或会话安全,已停止剩余校验。请查看恢复现场。'
self.status_label.setText(text); self.output.append(text)
self.summary = QMessageBox(self)
self.summary.setWindowTitle('CBB 校验结果'); self.summary.setText(text)
@@ -405,6 +409,8 @@ class MainWindow(QWidget):
self.stop.set(); self.status_label.setText('已请求停止;当前 CBB 完成或超时后停止队列。'); self._update()
def _failed(self, error):
if self.library_lease is not None:
self.library_lease.retain_for_recovery()
self.status_label.setText(str(error)); self.output.append(str(error))
if self.logging_context: self.logging_context.logger.error('cbb_worker_failed %s', error)
+89 -33
View File
@@ -127,7 +127,7 @@ def classify_output(exit_code: int | None, output: str, *, timed_out: bool = Fal
"""Never invent success: PASS needs the success text and no error marker."""
if timed_out:
return 'TIMEOUT'
if SUCCESS_TEXT.search(output) and not ERROR_TEXT.search(output):
if exit_code == 0 and SUCCESS_TEXT.search(output) and not ERROR_TEXT.search(output):
return 'PASS'
if ERROR_TEXT.search(output):
return 'FAIL'
@@ -156,6 +156,14 @@ def preflight(request):
pcb = absolute_preserving_drive(project.parent/refs[0])
if not pcb.is_relative_to(absolute_preserving_drive(work)):
raise ValueError(f'{name}: PCB 引用越出当前 CBB,停止操作: {pcb}')
real_work = work.resolve()
expected_work = library.parent.resolve() / 'ReusableBlocksLP' / name / 'Work'
if os.path.normcase(str(real_work)) != os.path.normcase(str(expected_work)):
raise ValueError(f'{name}: Work 路径通过链接指向其他工程,停止操作')
for candidate in (project, pcb, pcb.parent/'pcb.prj', pcb.parent/'Layout',
pcb.parent/'Layout'/'LayoutDB.lyt', work/'database', work/'CES'):
if not candidate.resolve().is_relative_to(real_work):
raise ValueError(f'{name}: 文件或目录链接越出当前 CBB,停止操作: {candidate}')
if not pcb.is_file() or not (pcb.parent/'Layout'/'LayoutDB.lyt').is_file():
raise ValueError(f'{name}: 缺少 PCB 或布局数据库: {pcb}')
return project, pcb
@@ -199,7 +207,7 @@ def _run_process(command, timeout_seconds, cwd):
# pipes. Bound both tree termination and the final drain so a hung
# helper cannot turn a 30-second item timeout into an unbounded wait.
try:
subprocess.run(['taskkill', '/PID', str(process.pid), '/T', '/F'],
subprocess.run([str(Path(os.environ.get('WINDIR', r'C:\Windows'))/'System32'/'taskkill.exe'), '/PID', str(process.pid), '/T', '/F'],
capture_output=True, timeout=5,
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
except (OSError, subprocess.TimeoutExpired):
@@ -216,7 +224,7 @@ def _run_process(command, timeout_seconds, cwd):
def _stop_worker(process, *, timeout=5):
"""Best-effort worker tree termination that never masks the item result."""
"""Stop automation clients; native database owners are preserved."""
try:
process.kill()
except Exception:
@@ -269,6 +277,10 @@ def read_library_flag_with_retry(reader, library, block_name, *, attempts=3,
def classify_failure_category(detail):
text = str(detail or '')
lower = text.casefold()
if any(code in lower for code in (
'pcb_release_unconfirmed', 'icdb_release_unconfirmed',
'icdb_session_unconfirmed', 'pcb_session_active', 'database_recovery_required')):
return 'DATABASE_SAFETY_STOP'
if 'tf_floatinglangbar_wndtitle' in lower or 'cicerouiwndframe' in lower:
return 'TOOL_FALSE_DIALOG'
if 'could not load job preferences db' in lower or 'unable to create local cell library' in lower:
@@ -472,13 +484,7 @@ def native_lm_verify_enabled():
def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline,
progress=None):
"""Reap Mentor processes after the worker can no longer run its ``finally``.
The outer supervisor owns the worker lifetime. A timeout or an unknown
dialog may therefore terminate the worker while its child LMC/PCB processes
remain alive. Delegate the same target-filtered cleanup used by the worker
and persist its events before the batch advances.
"""
"""Record native release or retention after automation has stopped."""
from .mentor_adapter import cleanup_verify_processes
event_path = Path(root) / 'events.jsonl'
@@ -497,7 +503,7 @@ def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline,
if progress:
progress(event)
cleanup_verify_processes(
return cleanup_verify_processes(
lm_pid,
target_pid,
None,
@@ -509,6 +515,29 @@ def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline,
def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, port_check=None,
dialog_supervisor_factory=None):
# Cover contents checking, native work and final state reads with one
# session-wide mutex. The worker-stage lock below is reentrant on Windows.
import win32event, win32con
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = False
try:
acquired = win32event.WaitForSingleObject(mutex, 0) in (
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
if not acquired:
return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。',
safe_to_continue=False)
return _run_verify_serialized(
request, progress=progress, flag_reader=flag_reader,
process_runner=process_runner, port_check=port_check,
dialog_supervisor_factory=dialog_supervisor_factory)
finally:
if acquired:
win32event.ReleaseMutex(mutex)
mutex.Close()
def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_runner=None,
port_check=None, dialog_supervisor_factory=None):
timing = RunTimings()
result = _run_verify(request, timing=timing, progress=progress, flag_reader=flag_reader,
process_runner=process_runner, port_check=port_check,
@@ -573,12 +602,12 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
runner = process_runner or _run_process
def finish(result):
# Backups are transient protection for the active worker. Keep the
# small, actionable evidence files but reclaim the copied CBB tree so
# large libraries do not accumulate tens of megabytes per item.
# Only reclaim auxiliary backups after confirmed success. Failed runs
# retain every available original file for incident investigation.
try:
from .mentor_adapter import cleanup_evidence_backup
cleanup_evidence_backup(root)
if result.status == 'PASS' and result.safe_to_continue:
cleanup_evidence_backup(root)
except Exception:
# Cleanup must never hide or change the verification result.
pass
@@ -635,6 +664,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
return finish(VerifyResult('BLOCKED', stderr=ports_detail))
job = {'lmc_file': str(absolute_preserving_drive(request.lmc_file)), 'block_name': request.block_name,
'supervisor_pid': os.getpid(),
'project': str(project), 'pcb': str(pcb), 'root': str(root), 'timeout': request.timeout_seconds,
'mentor_bin': str(request.mentor_bin or ''),
'mapped_drive': str(request.mapped_drive or ''),
@@ -789,11 +819,11 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
'FAIL',
stderr=f'{dialog_result.error_code}: {dialog_result.detail}',
error_stage=phase.value,
safe_to_continue=True,
safe_to_continue=False,
))
if deadline.expired():
_write_failure_windows(root, latest_snapshots)
# Kills the worker's whole job: worker, PCB and cscript helper.
# Stop automation without killing PCB/iCDB during a write.
_write_process_tree(root, process, target_pid)
if last_post_verify_observation:
(root/'post_verify_observations.jsonl').write_text(json.dumps({
@@ -817,8 +847,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
'detail': detail,
'observation': last_post_verify_observation,
})
return finish(VerifyResult(status, stderr=detail, safe_to_continue=True))
return finish(VerifyResult('TIMEOUT', stderr='操作超时;已记录现场,跳过当前 CBB。', error_stage=phase.value, safe_to_continue=True))
return finish(VerifyResult(status, stderr=detail, safe_to_continue=False))
return finish(VerifyResult('TIMEOUT', stderr='操作超时;已停止自动化并保留原生会话,停止批次,请检查恢复记录。', error_stage=phase.value, safe_to_continue=False))
time.sleep(.2)
worker_result = root/'outcome.json'
if not worker_result.exists():
@@ -832,12 +862,20 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
_stop_worker(process)
return finish(VerifyResult('FAIL', stderr=str(error), safe_to_continue=False))
finally:
close_errors = []
if 'dialog_agent' in locals() and dialog_agent is not None:
dialog_agent.close()
if 'process' in locals(): process.close()
try:
dialog_agent.close()
except Exception as error:
close_errors.append(str(error))
if 'process' in locals():
try:
_cleanup_session_processes(
process.close()
except Exception as error:
close_errors.append(str(error))
if 'process' in locals():
try:
release_result = _cleanup_session_processes(
root,
lm_pid=lm_pid,
target_pid=target_pid,
@@ -845,11 +883,30 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
pcb_baseline=pcb_baseline,
progress=progress,
)
except Exception:
# Cleanup is best effort; the original Verify result remains
# authoritative and the evidence directory records everything
# that was successfully reaped.
pass
except Exception as error:
release_result = {'safe_to_continue': False, 'error': str(error)}
try:
address = Path(project).parent / 'database/cdbsvr/sAddress.adr'
if address.exists():
release_result = {**(release_result or {}), 'safe_to_continue': False,
'icdb_address': str(address)}
except OSError as error:
close_errors.append(str(error))
if close_errors:
release_result = {**(release_result or {}), 'safe_to_continue': False,
'close_errors': close_errors}
if not release_result or not release_result.get('safe_to_continue'):
if 'result' in locals():
result = replace(result, status='FAIL', safe_to_continue=False,
stderr=(result.stderr + '\nPCB_RELEASE_UNCONFIRMED: 未确认原生会话退出').strip())
recovery = {'desktop': job['desktop'], 'project': str(project),
'pcb': str(pcb), 'release': release_result,
'action': '停止批次;检查原生会话及数据库;不要删除锁文件或自动恢复。'}
try:
(root/'recovery-required.json').write_text(
json.dumps(recovery, ensure_ascii=False, indent=2), encoding='utf-8')
except OSError:
pass # Evidence failure must not prevent mutex release.
# Drain the timing tail even when the worker exited before a poll.
# This is optional telemetry: an unreadable log must not mask failure
# or prevent releasing the serial-operation mutex.
@@ -891,7 +948,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
def run_batch(requests, max_workers=1, verifier=None, *, stop=None, progress=None):
"""Run CBBs serially; one item's failure never aborts the queue."""
"""Run serially and stop when database/session safety is unconfirmed."""
if max_workers != 1:
raise ValueError('Only serial Verify is supported for the shared Library Manager session')
if verifier is None:
@@ -905,13 +962,12 @@ def run_batch(requests, max_workers=1, verifier=None, *, stop=None, progress=Non
try:
result = verifier(request)
except Exception as error:
# An unexpected worker exception is a failed item, not a batch abort.
# An unexpected exception cannot establish database safety.
result = VerifyResult('FAIL', stderr=str(error), safe_to_continue=False)
results.append(result)
# Failure status and safe_to_continue describe this item, not the batch;
# the queue is skipped only after an explicit stop signal.
queue_stop = queue_stop or result.status == 'QUEUE_STOP'
if result.status == 'QUEUE_STOP' and stop is not None:
# Never start another writer after an unconfirmed native shutdown.
queue_stop = queue_stop or result.status == 'QUEUE_STOP' or not result.safe_to_continue
if queue_stop and stop is not None:
stop.set()
if progress: progress(request.block_name, result)
return results
+23
View File
@@ -0,0 +1,23 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$root = Split-Path $PSScriptRoot -Parent
Import-Module (Join-Path $root 'CBBRefresh.Core.psm1') -Force
$scratch = Join-Path $root ('build\content-check-exit-' + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $scratch | Out-Null
$exe = Join-Path $scratch 'ReuseBlockImporter.exe'
Add-Type -TypeDefinition @'
using System;
public class FakeImporter {
public static int Main(string[] args) {
Console.WriteLine("content checking completed successfully");
return 7;
}
}
'@ -OutputAssembly $exe -OutputType ConsoleApplication
$library = Join-Path $scratch 'test.lmc'
[IO.File]::WriteAllText($library, '')
$result = Invoke-CbbReuseBlockContentCheck -LmcFile $library -BlockName 'CBB' -LibSpec 'test' -DmsLoginConfig 'test' -MentorBin $scratch
if ($result.result -ne 'fail' -or $result.exitCode -ne 7) {
throw 'A nonzero native exit must not pass, even when stdout claims success.'
}
Write-Output 'Content-check exit-code regression passed (no Mentor data opened).'
+111
View File
@@ -0,0 +1,111 @@
"""Fault injection at native boundaries; real Windows supervisor-loss check."""
import json
from pathlib import Path
from types import SimpleNamespace
import pytest
from cbb_refresh_verify import mentor_adapter as adapter
from test_verify_worker_stability import exercise_worker
FULL_SAVE = 'LICENSE_OK\nSAVE_OK\nIS_SAVED\nPOST_SAVE_SYNCHRONIZED\nCLOSE_OK\nQUIT_OK\n'
@pytest.mark.parametrize('stage,log,exit_code', [
('startup', '', 0),
('integration', '', 0),
('save', '', 1),
('save', 'LICENSE_OK\nSAVE_START\n', 1),
('save', FULL_SAVE, 7),
('save', FULL_SAVE.replace('CLOSE_OK\n', ''), 0),
('save', FULL_SAVE.replace('IS_SAVED\n', ''), 0),
])
def test_native_disconnect_or_interrupted_save_never_completes(tmp_path, monkeypatch, stage, log, exit_code):
job, _, _ = exercise_worker(tmp_path, monkeypatch,
integration_error='RPC_E_DISCONNECTED: PCB closed during integration' if stage == 'integration' else None)
if stage == 'startup':
from cbb_refresh_verify.pcb_bridge import PcbBridgeError
def disconnected(*a, **k):
raise PcbBridgeError('PCB_EXITED_BEFORE_COM', 'PCB exited before COM was ready')
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', disconnected)
if stage == 'save':
def interrupted_helper(command, **kwargs):
kwargs['stdout'].write(log)
kwargs['stdout'].flush()
return SimpleNamespace(pid=3, poll=lambda: exit_code, returncode=exit_code)
monkeypatch.setattr(adapter.subprocess, 'Popen', interrupted_helper)
root = Path(job['root'])
request = root/'request.json'
request.write_text(json.dumps(job), encoding='utf-8')
assert adapter.worker_main(request) == 1
result = json.loads((root/'outcome.json').read_text(encoding='utf-8'))
assert result['status'] == 'FAIL'
assert result['safe_to_continue'] is False
events = [json.loads(line) for line in (root/'events.jsonl').read_text(encoding='utf-8').splitlines()]
assert not any(event['stage'] in ('complete', 'saved_and_closed') for event in events)
if stage != 'save':
assert not (root/'save.log').exists()
@pytest.mark.parametrize('outcome', [None, {'status': 'PASS'}])
def test_crashed_worker_cannot_pass_even_with_pass_file(tmp_path, monkeypatch, outcome):
from test_verify_runner import fixture, request
from cbb_refresh_verify.verify_runner import run_verify
fixture(tmp_path)
class CrashedWorker:
desktop = None
returncode = 7
def __init__(self, command, *_args, **_kwargs):
if outcome:
Path(command[-1]).with_name('outcome.json').write_text(json.dumps(outcome))
def poll(self): return 7
def close(self): pass
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', CrashedWorker)
monkeypatch.setattr('cbb_refresh_verify.verify_runner._cleanup_session_processes',
lambda *a, **k: {'safe_to_continue': True, 'remaining_pids': []})
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *a: None)
def must_not_query(*a):
pytest.fail('A crashed writer must not become PASS via a pre-existing READY flag')
result = run_verify(request(tmp_path), port_check=lambda: (True, ''), flag_reader=must_not_query)
assert result.status == 'FAIL'
assert result.safe_to_continue is False
def test_supervisor_crash_stops_registered_save_helper_but_not_native(tmp_path):
import subprocess, sys, time
import win32api, win32con, win32process, win32event
supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])
ready = tmp_path/'processes.json'
script = tmp_path/'worker.py'
script.write_text(
'import subprocess, sys, time, json\nfrom pathlib import Path\n'
'from cbb_refresh_verify.desktop_worker import start_supervisor_watchdog, launch_save_helper\n'
f'start_supervisor_watchdog({supervisor.pid})\n'
'native = subprocess.Popen([sys.executable,"-c","import time; time.sleep(30)"])\n'
'helper = launch_save_helper([sys.executable,"-c","import time; time.sleep(30)"])\n'
f'Path({str(ready)!r}).write_text(json.dumps([native.pid,helper.pid]))\n'
'time.sleep(30)\n', encoding='utf-8')
worker = subprocess.Popen([sys.executable, str(script)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
handles = []
try:
deadline = time.monotonic()+6
while not ready.exists() and worker.poll() is None and time.monotonic()<deadline:
time.sleep(.05)
assert ready.exists()
for pid in json.loads(ready.read_text()):
handles.append(win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION |
win32con.PROCESS_TERMINATE | win32con.SYNCHRONIZE, False, pid))
supervisor.terminate()
supervisor.wait(5)
assert worker.wait(8) == 75
assert win32event.WaitForSingleObject(handles[1], 3000) == win32con.WAIT_OBJECT_0
assert win32process.GetExitCodeProcess(handles[0]) == 259
finally:
for process in (supervisor, worker):
if process.poll() is None: process.terminate()
process.wait(5)
for handle in handles:
if win32process.GetExitCodeProcess(handle) == 259:
win32process.TerminateProcess(handle, 0)
handle.Close()
+3 -3
View File
@@ -78,7 +78,7 @@ def test_license_selector_is_allowed_after_com_attach_wait():
assert decision.error_code == ""
def test_open_lock_confirmation_is_allowed_during_pcb_starting():
def test_open_lock_confirmation_blocks_during_pcb_starting():
rules = load_rules(ASSETS / "dialog_rules.v1.json")
snapshot = prompt(
"Continue to open the design with read/write access permission?",
@@ -87,8 +87,8 @@ def test_open_lock_confirmation_is_allowed_during_pcb_starting():
decision = decide(snapshot, rules)
assert decision.rule_id == "open-lock-confirm"
assert decision.error_code == ""
assert not decision.action
assert decision.error_code == "DATABASE_RECOVERY_REQUIRED"
def test_forward_annotation_confirmation_is_accepted_during_document_attach():
+10 -10
View File
@@ -345,14 +345,14 @@ def test_phase_aware_unknown_window_fails_without_action():
def test_phase_aware_action_is_pending_until_window_disappears():
restore = evidence_window(
'Database Restoration - Load the last user saved database',
phase=DialogPhase.DATABASE_RECOVERY,
'Do you want to save the reusable block cell',
phase=DialogPhase.SAVE,
)
backend = PhaseBackend([[restore], []])
supervisor = DialogSupervisor(backend, rules=RULES, retry_seconds=60)
first = supervisor.poll(DialogPhase.DATABASE_RECOVERY)
second = supervisor.poll(DialogPhase.DATABASE_RECOVERY)
first = supervisor.poll(DialogPhase.SAVE)
second = supervisor.poll(DialogPhase.SAVE)
assert first.action_sent and not first.resolved
assert second.resolved
@@ -361,19 +361,19 @@ def test_phase_aware_action_is_pending_until_window_disappears():
def test_phase_aware_changed_content_is_not_window_gone():
first = evidence_window(
'Database Restoration - Load the last user saved database',
phase=DialogPhase.DATABASE_RECOVERY,
'Do you want to save the reusable block cell',
phase=DialogPhase.SAVE,
)
changed = evidence_window(
'Database Restoration - Load the last user saved database - selected',
phase=DialogPhase.DATABASE_RECOVERY,
'Do you want to save the reusable block cell - selected',
phase=DialogPhase.SAVE,
selected=True,
)
backend = PhaseBackend([[first], [changed]])
supervisor = DialogSupervisor(backend, rules=RULES, retry_seconds=60)
first_result = supervisor.poll(DialogPhase.DATABASE_RECOVERY)
second_result = supervisor.poll(DialogPhase.DATABASE_RECOVERY)
first_result = supervisor.poll(DialogPhase.SAVE)
second_result = supervisor.poll(DialogPhase.SAVE)
assert first_result.action_sent and not first_result.resolved
assert not second_result.resolved
+207
View File
@@ -0,0 +1,207 @@
from pathlib import Path
from threading import Event, Thread
import pytest
from cbb_refresh_verify import mentor_adapter as adapter
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult, classify_output, preflight
def test_project_rewrite_preserves_non_utf8_bytes_and_exact_backup(tmp_path):
pcb = tmp_path / 'Work/PCB/sample.pcb'
pcb.parent.mkdir(parents=True)
config = pcb.with_name('pcb.prj')
original = b'# native ANSI text: \xd6\xd0\xce\xc4\r\nKEY CentralLibrary "S:\\old.lmc"\r\n'
config.write_bytes(original)
evidence = tmp_path / 'evidence'
adapter.ensure_pcb_central_library(pcb, tmp_path / 'new.lmc', evidence_dir=evidence)
assert config.read_bytes() == original.replace(b'S:\\old.lmc', b'..\\..\\new.lmc')
assert (evidence / 'pcb.prj.before-central-library').read_bytes() == original
def test_project_rewrite_is_atomic_on_replace_failure(tmp_path, monkeypatch):
pcb = tmp_path / 'Work/PCB/sample.pcb'
pcb.parent.mkdir(parents=True)
config = pcb.with_name('pcb.prj')
original = b'KEY CentralLibrary "S:\\old.lmc"\r\n'
config.write_bytes(original)
def denied(*a):
raise PermissionError('simulated sharing violation')
monkeypatch.setattr(adapter.os, 'replace', denied)
with pytest.raises(PermissionError):
adapter.ensure_pcb_central_library(pcb, tmp_path / 'new.lmc', evidence_dir=tmp_path/'evidence')
assert config.read_bytes() == original
@pytest.mark.parametrize('code', [1, 2, -1, None])
def test_content_check_success_text_cannot_override_bad_exit(code):
assert classify_output(code, 'content checking completed successfully') != 'PASS'
def test_preflight_rejects_pcb_junction_outside_current_block(tmp_path):
import _winapi
library = tmp_path / 'lib.lmc'
library.touch()
work = tmp_path / 'ReusableBlocksLP/CBB/Work'
work.mkdir(parents=True)
(work/'CBB.prj').write_text('KEY PCBDesignPath "PCB\\CBB.pcb"\n')
outside = tmp_path / 'other-project'
(outside/'Layout').mkdir(parents=True)
(outside/'CBB.pcb').touch()
(outside/'Layout/LayoutDB.lyt').touch()
_winapi.CreateJunction(str(outside), str(work/'PCB'))
with pytest.raises(ValueError):
preflight(VerifyRequest(library, 'CBB', tmp_path/'evidence'))
def test_serial_guard_covers_preflight_and_content_check(tmp_path, monkeypatch):
from cbb_refresh_verify import verify_runner as runner
entered, release = Event(), Event()
calls, results = [], []
def operation(request, **kwargs):
calls.append(request.block_name)
entered.set()
if request.block_name == 'first':
assert release.wait(5)
return VerifyResult('PASS')
monkeypatch.setattr(runner, '_run_verify', operation)
request = VerifyRequest(tmp_path/'lib.lmc', 'first', tmp_path/'evidence')
thread = Thread(target=lambda: results.append(runner.run_verify(request)))
thread.start()
assert entered.wait(3)
try:
second = runner.run_verify(VerifyRequest(tmp_path/'lib.lmc', 'second', tmp_path/'evidence'))
assert second.status == 'BLOCKED'
assert not second.safe_to_continue
assert calls == ['first']
finally:
release.set()
thread.join(6)
def test_recovery_retains_temporary_network_mapping():
from test_network_path import FakeWNet
from cbb_refresh_verify.network_path import LibraryPathLease
backend = FakeWNet()
lease = LibraryPathLease.acquire(r'\\server\share\lib\mentor_lib.lmc', backend=backend)
lease.retain_for_recovery()
lease.close()
assert backend.cancelled == []
assert lease.drive in backend.existing
def test_verified_green_block_does_not_call_forbidden_forward_annotation():
from cbb_refresh_verify.integration import refresh_integration
class AlreadyVerified:
ForwardAnnotationStatus = SynchCESStatus = LoadCESStatus = BackAnnotationStatus = 2
IsForwardAnnotationAllowed = IsSynchCESAllowed = IsLoadCESAllowed = IsBackAnnotationAllowed = False
def ForwardAnnotate(self):
pytest.fail('Mentor explicitly forbids ForwardAnnotate on this verified block')
result = refresh_integration(AlreadyVerified(), settle_seconds=0)
assert all(value == 2 for value in result.values())
def test_wmi_process_query_rejects_non_allowlisted_names():
with pytest.raises(ValueError):
adapter._process_details("ExpeditionPCB.exe' OR Name='other.exe")
def test_supervisor_loss_stops_automation_but_preserves_native_like_process(tmp_path):
import subprocess
import sys
import time
import win32api, win32con, win32process
supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])
script = tmp_path/'watchdog-worker.py'
ready = tmp_path/'native.pid'
script.write_text(
'import sys, subprocess, time\nfrom pathlib import Path\n'
'from cbb_refresh_verify.desktop_worker import start_supervisor_watchdog\n'
f'start_supervisor_watchdog({supervisor.pid})\n'
'native = subprocess.Popen([sys.executable,"-c","import time; time.sleep(30)"])\n'
f'Path({str(ready)!r}).write_text(str(native.pid))\n'
'time.sleep(30)\n', encoding='utf-8')
worker = subprocess.Popen([sys.executable, str(script)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
native_handle = None
try:
deadline = time.monotonic()+5
while not ready.exists() and worker.poll() is None and time.monotonic()<deadline:
time.sleep(.05)
assert ready.exists(), 'Supervised automation worker could not start'
native_handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_TERMINATE,
False, int(ready.read_text()))
supervisor.terminate()
supervisor.wait(5)
assert worker.wait(8) == 75
assert win32process.GetExitCodeProcess(native_handle) == 259
finally:
if supervisor.poll() is None: supervisor.terminate()
if worker.poll() is None: worker.terminate()
supervisor.wait(5)
worker.wait(5)
if native_handle is not None:
win32process.TerminateProcess(native_handle, 0)
native_handle.Close()
@pytest.mark.parametrize('encoding', ['utf-8-sig', 'utf-16-le', 'utf-16-be'])
def test_project_rewrite_preserves_bom_and_unicode(tmp_path, encoding):
pcb = tmp_path/'Work/PCB/sample.pcb'
pcb.parent.mkdir(parents=True)
bom = {'utf-8-sig': b'', 'utf-16-le': b'\xff\xfe', 'utf-16-be': b'\xfe\xff'}[encoding]
original = bom + ('KEY CentralLibrary "S:\\old.lmc"\r\n# 中文工程\r\n').encode(encoding)
config = pcb.with_name('pcb.prj')
config.write_bytes(original)
adapter.ensure_pcb_central_library(pcb, tmp_path/'new.lmc', evidence_dir=tmp_path/'evidence')
expected = bom + ('KEY CentralLibrary "..\\..\\new.lmc"\r\n# 中文工程\r\n').encode(encoding)
assert config.read_bytes() == expected
assert (tmp_path/'evidence/pcb.prj.before-central-library').read_bytes() == original
def test_cli_unsafe_batch_preserves_created_drive(tmp_path, monkeypatch):
from test_network_path import FakeWNet
from cbb_refresh_verify.network_path import LibraryPathLease
from cbb_refresh_verify import application
backend = FakeWNet()
lease = LibraryPathLease.acquire(r'\\server\share\lib.lmc', backend=backend)
monkeypatch.setattr(LibraryPathLease, 'acquire', lambda path: lease)
monkeypatch.setattr('cbb_refresh_verify.network_path.select_library_file', lambda path: tmp_path/'lib.lmc')
monkeypatch.setattr('cbb_refresh_verify.verify_runner.run_batch', lambda requests: [VerifyResult('FAIL', safe_to_continue=False)])
code = application.main(['--batch-lmc', r'\\server\share\lib.lmc', '--block', 'CBB', '--data-root', str(tmp_path/'run')])
assert code == 2
assert not backend.cancelled
assert lease.drive in backend.existing
def test_ui_unsafe_batch_preserves_created_drive(qtbot, tmp_path):
from test_network_path import FakeWNet
from cbb_refresh_verify.network_path import LibraryPathLease
from cbb_refresh_verify.ui.main_window import MainWindow
backend = FakeWNet()
lease = LibraryPathLease.acquire(r'\\server\share\lib.lmc', backend=backend)
window = MainWindow(data_root=tmp_path)
qtbot.addWidget(window)
window.library_lease = lease
window._batch_done(([VerifyResult('FAIL', safe_to_continue=False)], tmp_path/'batch.log'))
window.close()
assert not backend.cancelled
assert lease.drive in backend.existing
def test_legacy_library_reader_only_requests_owned_window_close(monkeypatch):
import win32gui, win32process, win32con
from types import SimpleNamespace
posted = []
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Library Manager: example.lmc')
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 23 if hwnd == 10 else 99))
monkeypatch.setattr(win32gui, 'PostMessage', lambda *args: posted.append(args))
monkeypatch.setattr(adapter, '_process_details', lambda name: [])
desktop = SimpleNamespace(EnumDesktopWindows=lambda: [10, 11])
assert adapter.close_library_reader(23, desktop, timeout_seconds=0)
assert posted == [(10, win32con.WM_CLOSE, 0, 0)]
def test_legacy_library_reader_retains_unclosed_native_session(monkeypatch):
from types import SimpleNamespace
monkeypatch.setattr(adapter, '_process_details', lambda name: [(23, 'LibraryManager.exe', '', 1)])
assert not adapter.close_library_reader(23, SimpleNamespace(EnumDesktopWindows=lambda: []), timeout_seconds=0)
+6 -6
View File
@@ -17,7 +17,7 @@ def test_parse_icdb_server_address_extracts_port_and_hosts(tmp_path):
}
def test_stale_icdb_address_is_moved_to_evidence(tmp_path, monkeypatch):
def test_unreachable_icdb_address_is_preserved(tmp_path, monkeypatch):
from cbb_refresh_verify import mentor_adapter
database = tmp_path / "database"
@@ -33,12 +33,12 @@ def test_stale_icdb_address_is_moved_to_evidence(tmp_path, monkeypatch):
emit=lambda **event: events.append(event),
)
assert result["status"] == "STALE_MOVED"
assert not address.exists()
assert result["status"] == "UNREACHABLE"
assert address.exists()
moved = list((tmp_path / "evidence" / "diagnostics" / "cdbsvr").glob("sAddress.adr.stale-*"))
assert len(moved) == 1
assert moved[0].read_text(encoding="ascii") == "guid\n127.0.0.1\n10062\n1094\n"
assert events[-1]["stage"] == "icdb_address_stale"
assert len(moved) == 0
assert address.read_text(encoding="ascii") == "guid\n127.0.0.1\n10062\n1094\n"
assert events[-1]["stage"] == "icdb_address_check"
def test_live_icdb_address_is_left_in_place(tmp_path, monkeypatch):
+236
View File
@@ -0,0 +1,236 @@
"""Regression cases for the PCB/iCDB emergency shutdown defects.
Native Mentor boundaries are replaced; no production library is opened.
"""
from pathlib import Path
from types import SimpleNamespace
import pytest
from cbb_refresh_verify import mentor_adapter as adapter
from cbb_refresh_verify.verify_runner import VerifyResult, run_batch
from test_verify_worker_stability import exercise_worker
from test_dialog_rules import prompt, ASSETS
from cbb_refresh_verify.dialog_rules import decide, load_rules
def test_network_timeout_cannot_remove_database_advertisement(tmp_path, monkeypatch):
address = tmp_path / 'database/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
original = b'guid\n192.168.1.10\n10062\n1094\n'
address.write_bytes(original)
monkeypatch.setattr(adapter, 'probe_icdb_server', lambda *a: (False, 'timeout'))
result = adapter.prepare_icdb_address(tmp_path / 'database', tmp_path / 'evidence')
assert address.exists(), 'A timeout is not proof the database owner is dead'
assert address.read_bytes() == original
assert result['status'] == 'UNREACHABLE'
@pytest.mark.parametrize('state', ['LIVE', 'UNREACHABLE', 'INVALID'])
def test_existing_database_owner_blocks_before_pcb_launch(tmp_path, monkeypatch, state):
job, events, execute = exercise_worker(tmp_path, monkeypatch)
monkeypatch.setattr(adapter, 'prepare_icdb_address', lambda *a, **k: {'status': state})
with pytest.raises(RuntimeError, match='ICDB_SESSION_UNCONFIRMED'):
execute()
assert not any(e['stage'] == 'pcb_launch' for e in events)
@pytest.mark.parametrize('text', [
'Continue to open the design with read/write access permission?',
'Confirm that no other users have the design open',
'Database restoration: last user saved database',
])
def test_database_recovery_requires_manual_review_without_click(text):
decision = decide(prompt(text), load_rules(ASSETS / 'dialog_rules.v1.json'))
assert decision.error_code == 'DATABASE_RECOVERY_REQUIRED'
assert not decision.action
def test_unsafe_result_stops_batch_even_when_reported_pass():
calls = []
def verify(item):
calls.append(item)
return VerifyResult('PASS', safe_to_continue=False)
results = run_batch(['A', 'B'], verifier=verify)
assert calls == ['A']
assert results[1].status == 'SKIPPED'
def test_cleanup_does_not_force_terminate_native_database_owner(monkeypatch):
killed = []
monkeypatch.setattr(adapter, '_terminate_pid', lambda pid, **k: killed.append(pid))
monkeypatch.setattr(adapter, '_process_details', lambda name:
[(22, 'ExpeditionPCB.exe', 'pcb', 11)] if name == 'ExpeditionPCB.exe' else [])
events = []
result = adapter.cleanup_verify_processes(None, 22, 11, emit=lambda **e: events.append(e))
assert killed == []
assert result['safe_to_continue'] is False
assert result['remaining_pids'] == [22]
assert any(e['stage'] == 'process_retained' for e in events)
def test_cleanup_enumeration_failure_cannot_confirm_release(monkeypatch):
def fail(name):
raise OSError('access denied')
monkeypatch.setattr(adapter, '_process_details', fail)
result = adapter.cleanup_verify_processes(None, 22)
assert result['safe_to_continue'] is False
def test_python_com_references_are_released_before_save_helper(tmp_path, monkeypatch):
import weakref
job, events, execute = exercise_worker(tmp_path, monkeypatch)
references = []
class ComObject:
pass
def attach(*a, **k):
objects = tuple(ComObject() for _ in range(3))
references.extend(weakref.ref(obj) for obj in objects)
return objects
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', attach)
original = adapter.subprocess.Popen
def helper(*a, **k):
assert all(ref() is None for ref in references), 'Python still owns PCB COM references'
return original(*a, **k)
monkeypatch.setattr(adapter.subprocess, 'Popen', helper)
assert execute()['status'] == 'PASS'
def test_desktop_abort_does_not_kill_native_process_tree(monkeypatch):
from cbb_refresh_verify import desktop_worker as desktop
worker = desktop.DesktopWorker.__new__(desktop.DesktopWorker)
worker.job_bound = True
worker.job = object()
worker.handle = object()
worker.pid = 101
worker.companions = []
terminated = []
job_kills = []
monkeypatch.setattr(desktop.win32job, 'TerminateJobObject', lambda *a: job_kills.append(a))
monkeypatch.setattr(desktop.win32process, 'TerminateProcess', lambda h, code: terminated.append(h))
monkeypatch.setattr(adapter, '_process_details', lambda name: [])
worker.kill()
assert job_kills == [], 'Terminating the job interrupts PCB and cdbsvr writes'
assert worker.handle in terminated
def test_saved_pcb_with_retained_icdb_address_cannot_pass(tmp_path, monkeypatch):
job, events, execute = exercise_worker(tmp_path, monkeypatch)
original = adapter.subprocess.Popen
def helper(*a, **k):
address = Path(job['project']).parent / 'database/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
address.write_text('still owned', encoding='ascii')
return original(*a, **k)
monkeypatch.setattr(adapter.subprocess, 'Popen', helper)
with pytest.raises(RuntimeError, match='ICDB_RELEASE_UNCONFIRMED'):
execute()
assert not any(e['stage'] == 'complete' for e in events)
def test_retained_pcb_after_close_cannot_pass_or_be_killed(tmp_path, monkeypatch):
from itertools import count
job, events, execute = exercise_worker(tmp_path, monkeypatch)
calls = count()
monkeypatch.setattr(adapter, 'pcb_processes', lambda:
() if next(calls) == 0 else ((2, 'ExpeditionPCB.exe', 'pcb'),))
clock = count(step=40)
monkeypatch.setattr(adapter.time, 'monotonic', lambda: next(clock))
with pytest.raises(RuntimeError, match='PCB_RELEASE_UNCONFIRMED'):
execute()
assert not any(e['stage'] == 'complete' for e in events)
def test_library_manager_close_does_not_force_kill_database_owner():
from cbb_refresh_verify.library_manager_verify import LibraryVerifySession
killed = []
def wait(**kw):
import subprocess
raise subprocess.TimeoutExpired('LibraryManager', 5)
process = SimpleNamespace(poll=lambda: None, wait=wait,
terminate=lambda: killed.append('terminate'),
kill=lambda: killed.append('kill'))
session = LibraryVerifySession(SimpleNamespace(close=lambda: None), process, 1, Path('lib.lmc'), 'CBB')
session.close()
assert killed == []
def test_supervisor_rejects_pass_when_native_release_is_unconfirmed(tmp_path, monkeypatch):
import json
from test_verify_runner import fixture, request, PORTS_OK
from cbb_refresh_verify.verify_runner import run_verify
fixture(tmp_path)
class Worker:
desktop = None
returncode = 0
def __init__(self, command, *_a, **_k):
root = Path(command[-1]).parent
(root / 'outcome.json').write_text('{"status":"PASS"}', encoding='utf-8')
(root / 'backup/CBB').mkdir(parents=True)
(root / 'backup/CBB/original.bin').write_bytes(b'original')
def poll(self): return 0
def close(self): pass
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
monkeypatch.setattr('cbb_refresh_verify.verify_runner._cleanup_session_processes',
lambda *a, **k: {'safe_to_continue': False, 'remaining_pids': [42]})
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *a: None)
bridge_calls = []
result = run_verify(request(tmp_path), port_check=PORTS_OK,
flag_reader=lambda *a: bridge_calls.append(a))
assert result.status == 'FAIL'
assert not result.safe_to_continue
assert bridge_calls == []
root = result.evidence_path.parent
assert (root / 'backup/CBB/original.bin').read_bytes() == b'original'
recovery = json.loads((root / 'recovery-required.json').read_text(encoding='utf-8'))
assert recovery['release']['remaining_pids'] == [42]
assert recovery['desktop'].startswith('CbbVerify_')
def test_existing_pcb_session_blocks_before_mutating_project(tmp_path, monkeypatch):
job, events, execute = exercise_worker(tmp_path, monkeypatch)
monkeypatch.setattr(adapter, 'pcb_processes', lambda: ((20, 'ExpeditionPCB.exe', 'user design'),))
with pytest.raises(RuntimeError, match='PCB_SESSION_ACTIVE'):
execute()
assert not events
def test_real_windows_job_abort_preserves_native_like_child(tmp_path):
"""Exercise real Win32 job/handle semantics using harmless Python processes."""
import sys
import time
import uuid
import win32api
import win32con
import win32process
from cbb_refresh_verify.desktop_worker import DesktopWorker
script = tmp_path / 'parent.py'
pid_path = tmp_path / 'child.pid'
script.write_text(
'import subprocess, sys, time\nfrom pathlib import Path\n'
'child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])\n'
f'Path({str(pid_path)!r}).write_text(str(child.pid))\n'
'time.sleep(30)\n', encoding='utf-8')
worker = DesktopWorker([sys.executable, str(script)], 'CbbSafetyTest_' + uuid.uuid4().hex[:8])
child_handle = None
closed = False
try:
deadline = time.monotonic() + 10
while not pid_path.exists() and time.monotonic() < deadline:
time.sleep(.05)
assert pid_path.exists(), 'Harmless child process failed to start'
child_handle = win32api.OpenProcess(
win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_TERMINATE, False,
int(pid_path.read_text()))
worker.kill()
worker.wait()
worker.close()
closed = True
assert win32process.GetExitCodeProcess(child_handle) == 259
finally:
if not closed:
worker.kill()
worker.close()
if child_handle is not None:
win32process.TerminateProcess(child_handle, 0)
child_handle.Close()
+13 -9
View File
@@ -32,13 +32,13 @@ class FakeProjectIntegration:
return True
def test_database_restoration_dialog_selects_last_saved_database():
def test_database_restoration_dialog_requires_manual_recovery():
action = choose_dialog_action(
'Database Restoration',
['Restore Database', 'Load the last user saved database dated 2026/9/3 19:32.',
'Load the autosave database dated 2026/9/3 19:32.', 'OK', 'Cancel'],
)
assert action == 'restore_last_saved'
assert action is None
def test_library_manager_fresh_pid_filter_rejects_stale_sessions():
@@ -77,14 +77,16 @@ def test_interactive_verify_focuses_library_manager_before_real_right_click():
assert calls == ['focus', 'select', ('click', 'right')]
def test_cleanup_verify_processes_terminates_worker_parent_and_lm(monkeypatch):
def test_cleanup_verify_processes_confirms_absent_worker_parent_and_lm(monkeypatch):
from cbb_refresh_verify import mentor_adapter
terminated = []
monkeypatch.setattr(mentor_adapter, '_terminate_pid',
lambda pid, **_kwargs: terminated.append(int(pid)))
cleanup_verify_processes(101, 202, 303, emit=lambda **event: None)
assert terminated == [202, 303, 101]
monkeypatch.setattr(mentor_adapter, '_process_details', lambda _name: ())
result = cleanup_verify_processes(101, 202, 303, emit=lambda **event: None)
assert terminated == []
assert result['safe_to_continue']
def test_cleanup_verify_processes_discovers_unbound_target_pcb_processes(monkeypatch):
@@ -106,7 +108,7 @@ def test_cleanup_verify_processes_discovers_unbound_target_pcb_processes(monkeyp
monkeypatch.setattr(mentor_adapter, '_terminate_pid',
lambda pid, **_kwargs: terminated.append(int(pid)))
mentor_adapter.cleanup_verify_processes(
result = mentor_adapter.cleanup_verify_processes(
101,
None,
None,
@@ -115,7 +117,9 @@ def test_cleanup_verify_processes_discovers_unbound_target_pcb_processes(monkeyp
emit=lambda **event: None,
)
assert terminated == [202, 303, 204, 101]
assert terminated == []
assert result['remaining_pids'] == [202, 204, 303]
assert not result['safe_to_continue']
def test_verify_cleanup_is_present_on_all_exit_paths():
@@ -279,11 +283,11 @@ def test_refresh_integration_never_accepts_initial_green_when_refresh_fails(fail
assert 'SynchCES' not in pi.actions
def test_refresh_integration_attempts_disabled_green_action_once():
def test_refresh_integration_skips_native_disabled_green_action():
pi = GreenProjectIntegration()
pi.IsForwardAnnotationAllowed = False
refresh_integration(pi, settle_seconds=0)
assert pi.actions == ['ForwardAnnotate']
assert pi.actions == []
def test_refresh_integration_never_counts_rejected_green_action_as_completed():
+2 -2
View File
@@ -89,7 +89,7 @@ def test_dialog_handling_only_posts_button_clicks():
text = (SOURCE_ROOT/'mentor_adapter.py').read_text(encoding='utf-8')
assert 'BM_CLICK' in text
# The only posted messages are the documented dialog button clicks.
assert text.count('PostMessage') == 1
assert 'win32con.WM_CLOSE' in text
def test_desktop_worker_close_kills_unbound_worker_tree():
@@ -108,5 +108,5 @@ def test_verification_never_deletes_or_moves_library_files():
assert 'shutil.rmtree(backup)' in adapter
# The only library-side mutation is quarantining the volatile, unreachable
# cdbsvr address advertisement; stable CBB content remains untouched.
assert adapter.count('shutil.move') == 1
assert 'shutil.move' not in adapter
assert "sAddress.adr" in adapter
+11 -11
View File
@@ -212,12 +212,12 @@ def test_preflight_uses_project_pcb_reference_not_block_name(tmp_path):
assert preflight(request(tmp_path)) == (work/'CBB1.prj', work/'PCB'/'actual.pcb')
def test_preflight_preserves_mapped_drive_without_resolve(tmp_path, monkeypatch):
def test_preflight_uses_canonical_paths_for_safety_but_preserves_mentor_path(tmp_path, monkeypatch):
work = fixture(tmp_path)
monkeypatch.setattr(
Path,
'resolve',
lambda self: pytest.fail(f'preflight must preserve mapped drive: {self}'),
lambda self: Path(str(self).replace(str(tmp_path), str(tmp_path/'canonical-share'))),
)
assert preflight(request(tmp_path)) == (work/'CBB1.prj', work/'PCB'/'actual.pcb')
@@ -458,18 +458,18 @@ def test_batch_continues_past_item_failure_and_logs_all_items(tmp_path):
assert [r.status for r in results] == ['BLOCKED','FAIL','PASS']
def test_batch_does_not_stop_for_timeout_failure(tmp_path):
def test_batch_stops_for_unsafe_timeout_failure(tmp_path):
calls = []
outcomes = [VerifyResult('TIMEOUT', stderr='PCB remains open', safe_to_continue=False), VerifyResult('PASS')]
def verify(req):
calls.append(req.block_name)
return outcomes.pop(0)
results = run_batch([request(tmp_path, n) for n in ['A','B']], verifier=verify)
assert calls == ['A','B']
assert [r.status for r in results] == ['TIMEOUT','PASS']
assert calls == ['A']
assert [r.status for r in results] == ['TIMEOUT','SKIPPED']
def test_batch_continues_when_verifier_raises(tmp_path):
def test_batch_stops_when_verifier_raises(tmp_path):
calls = []
def verify(req):
@@ -479,10 +479,10 @@ def test_batch_continues_when_verifier_raises(tmp_path):
return VerifyResult('PASS')
results = run_batch([request(tmp_path, n) for n in ['A', 'B']], verifier=verify)
assert calls == ['A', 'B']
assert calls == ['A']
assert results[0].status == 'FAIL'
assert 'unexpected worker error' in results[0].stderr
assert results[1].status == 'PASS'
assert results[1].status == 'SKIPPED'
def test_batch_cancel_between_items(tmp_path):
@@ -499,7 +499,7 @@ def test_parallelism_rejected_for_shared_library(tmp_path):
run_batch([], max_workers=2)
def test_timeout_terminates_worker_job_before_next_item(tmp_path, monkeypatch):
def test_timeout_stops_automation_and_blocks_next_item(tmp_path, monkeypatch):
"""A timed-out item must not leave a Mentor process holding the library."""
fixture(tmp_path)
killed = []
@@ -521,7 +521,7 @@ def test_timeout_terminates_worker_job_before_next_item(tmp_path, monkeypatch):
assert result.status == 'TIMEOUT'
assert killed == [True]
assert result.safe_to_continue
assert not result.safe_to_continue
def test_fatal_dialog_terminates_current_worker_and_cannot_become_pass(tmp_path, monkeypatch):
@@ -1013,7 +1013,7 @@ def test_saved_closed_without_cert_uses_bridge_and_never_reexecutes_verify(
def close(self): ordering.append('worker_close')
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker',FinishedWorker)
monkeypatch.setattr('cbb_refresh_verify.verify_runner._cleanup_session_processes',
lambda *a,**kw: ordering.append('cleanup'))
lambda *a,**kw: (ordering.append('cleanup') or {'safe_to_continue': True}))
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout',
lambda *_: ordering.append('failure_logs'))
waits=[]
+1 -1
View File
@@ -26,7 +26,7 @@ def exercise_worker(tmp_path, monkeypatch, *, integration_error=None):
monkeypatch.setattr(adapter, 'launch_pcb_process', lambda *a: SimpleNamespace(pid=1))
monkeypatch.setattr(adapter, '_process_details', lambda _: ())
monkeypatch.setattr(adapter, 'select_reusable_circuit_worker', lambda *a, **kw: (2,'pcb','command',1))
monkeypatch.setattr(adapter, 'cleanup_verify_processes', lambda *a, **kw: None)
monkeypatch.setattr(adapter, 'cleanup_verify_processes', lambda *a, **kw: {'safe_to_continue': True, 'remaining_pids': []})
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document',
lambda *a, **kw: (None, None, object()))
def integration(*args, **kwargs):