Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e230e1a46 | ||
|
|
a56fd17b60 | ||
|
|
766d67cb57 | ||
|
|
311d0dd9a4 | ||
|
|
39985f7cfd | ||
|
|
ac9836a2d5 | ||
|
|
cdb8a6a28b | ||
|
|
a851e47eca | ||
|
|
9cd4968a97 | ||
|
|
2490067b2d | ||
|
|
8a517d6f38 | ||
|
|
a11eb3c766 |
No files matched your search
@@ -28,6 +28,7 @@ venv/
|
||||
/dist/
|
||||
/dist-*/
|
||||
/*.zip
|
||||
/*.zip.sha256
|
||||
/*.rar
|
||||
|
||||
# Native compiler intermediates; retain the two verified x86 runtime bridges
|
||||
|
||||
@@ -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') {
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
# CBB 中心库上传后校验工具
|
||||
|
||||
当前补丁:**[v0.2.3-beta.1](docs/releases/v0.2.3-beta.1.md)(Beta 预发布,待现场验收)**,替代 0.2.2-beta.1。数据库所有权或关闭状态不确定时停止批次、保留现场;不自动移走地址文件、接管带锁设计或强杀 PCB 进程树。新增全库 ICDB 维护、同名 CBB 完整备份恢复弹窗及严格的原生复核流程。详见 [v0.2.3-beta.1 发布说明](docs/releases/v0.2.3-beta.1.md)。下方日期记录描述历史版本,以本补丁说明为准。
|
||||
|
||||
本目录是独立的 CBB 中心库上传后校验工具,不属于 Part Replacement,也不负责 CBB 工程复制或中心库上传。
|
||||
|
||||
针对 `The last server session ... lost the connection to the database`,修复分支增加
|
||||
`ICDB_REPAIR_REQUIRED` 专项识别、共享符号库路径诊断及停止批次处理。现场原生修复步骤及实现边界见
|
||||
[iCDB 旧服务器会话失联分析](docs/diagnostics/2026-10-27-icdb-lost-server-session.md)。
|
||||
当前分支会在目标明确、完整备份和所有权释放证据齐全时尝试一次同版本原生 Repair;共享符号库没有 PRJ 入口时停止并要求人工处理。
|
||||
|
||||
## 功能边界
|
||||
|
||||
| 工具 | 处理对象 | 核心动作 | 结果 |
|
||||
@@ -10,9 +17,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 路径(保留原文件副本)。这不是只读检查;不要将“校验”理解为工程数据完全不变。
|
||||
|
||||
## 当前实现
|
||||
|
||||
@@ -133,8 +140,8 @@ 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` 事件。
|
||||
- 弹窗只认领本次运行的私有桌面/进程,依据已知规则投递 `BM_CLICK`。数据库恢复、会话失联和设计锁接管提示会阻断并保留现场;不会自动选择恢复或接管。未知弹窗也会阻断,Project Integration 进度窗保持观察。动作/错误弹窗和实际控件 ID 写入 `dialogs.jsonl`。
|
||||
- 超时或失败时停止自动化客户端,保留可能仍在写库的原生会话并停止批次。内容检查超时也不再强杀整个进程树;监督器核对进程是否退出,记录 `process_exit_confirmed` 或 `process_retained`;无法确认释放时输出 `recovery-required.json`,不以强杀代替数据库安全关闭。已确认身份且原生调用返回的失败文档会通过 32 位助手执行 `Close(False)`,再确认进程和 iCDB 地址释放。
|
||||
|
||||
## 环境要求
|
||||
|
||||
@@ -161,7 +168,7 @@ Windows 版本判断以 Build 为准:`22000` 及以上归为 Win11,`10240–
|
||||
|
||||
## 批量策略
|
||||
|
||||
每个 CBB 独立执行并写入自己的 evidence 和结果行。缺少 PCB/PRJ、内容检查失败、刷新失败或超时只影响当前项,记录 `BLOCKED/FAIL/TIMEOUT` 后继续下一项;不会因为单项失败终止整批。只有点击“当前项完成后停止”(或执行器返回 `QUEUE_STOP`)才把后续项目标为 `SKIPPED`。
|
||||
每个 CBB 独立执行并写入自己的 evidence 和结果行。可确认安全的单项失败允许继续;遇到 `ICDB_REPAIR_REQUIRED`、超时、原生会话释放未确认等情况,会停止整批并把后续项目标为 `SKIPPED`。点击“当前项完成后停止”或执行器返回 `QUEUE_STOP` 也会停止队列。
|
||||
|
||||
GUI 中状态为 `绿色 / Ready` 的条目不能勾选、也不会进入批量队列。
|
||||
|
||||
@@ -194,7 +201,7 @@ Mentor 桥耗时需在目标机器另行测量。
|
||||
- `save.log`:`LICENSE_OK / SAVE_OK / IS_SAVED / POST_SAVE_SYNCHRONIZED / CLOSE_OK / QUIT_OK`;
|
||||
- `result.json`:状态、中心库标志读取结果、阻断原因;
|
||||
- `image-status-map.json`:按中心库路径隔离的本机动态 `image_id` 校准快照(当前格式 `schema_version=2`);旧版本或无版本缓存会自动忽略并重新校准,日志会记录当前样本、缓存样本、Ready 主 ID、未映射 ID 和逐项决策原因;
|
||||
- `backup/`:仅本地盘操作前的稳定文件副本;网络盘/UNC 跳过前置递归备份(`backup_skipped`);`database/LogFiles/tmp` 等易变目录和日志文件会排除,备份超时或失败不会阻断 PCB 校验。每个 CBB 结束后默认自动删除该目录,只保留 `events.jsonl`、`dialogs.jsonl`、`save.log` 和 `result.json` 等轻量证据。
|
||||
- `backup/`:仅本地盘操作前的稳定文件副本;网络盘/UNC 跳过前置递归备份(`backup_skipped`);`database/LogFiles/tmp` 等易变目录和日志文件会排除,备份超时或失败不会阻断 PCB 校验。仅在成功且确认安全后清理该目录,失败时保留已有副本及日志;它不是完整数据库回滚备份。
|
||||
|
||||
超时采用无进展计时:界面和批量请求默认 600 秒无进展超时;它不是整个 CBB 的固定总时限,而是连续 600 秒没有可观察进展时才终止。PCB/COM 阶段切换、Project Integration 状态变化、保存标记变化和弹窗处理都会刷新计时器;重复的状态轮询不算进展。需要更快失败时仍可在界面将单项预算调低,但 30 秒只适合已确认 PCB 启动稳定的环境。
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
# iCDB 原生手动修复与工程更新
|
||||
|
||||
适用:本机 Mentor EE7.9.5。菜单已从实际安装的原生程序资源核对;修复、失败诊断和修复后重新打开已在隔离工程实测。
|
||||
|
||||
## 先确定要处理的目标
|
||||
|
||||
以本轮原生日志的数据库路径为准。`ReusableBlocksLP/<CBB>/Work/database` 属于该工程;`SymbolLibs/__ReusableBlocksSymb` 属于中心库共享符号数据库。共享符号库不能靠修复无关的 CBB Work 工程替代。
|
||||
|
||||
操作前正常关闭使用相同库的 PCB、Library Manager、原理图/符号编辑客户端,核对原服务器会话已结束,保留包含数据库、原备份和日志的完整一致副本。以下写入先在副本上进行。
|
||||
|
||||
## 原生工程数据库 Repair
|
||||
|
||||
1. 启动本机的 iCDB Project Backup,并打开实际报错工程对应的 `.prj`。本机可用 PowerShell 命令形式如下,工程路径需替换为自己的隔离副本:
|
||||
|
||||
```powershell
|
||||
& 'D:\Mentor\7.9.5EE\SDD_HOME\iCDB\win32\bin\iCDBProjectBackup.exe' -p 'F:\隔离副本\ReusableBlocksLP\IP101GR\Work\IP101GR.prj'
|
||||
```
|
||||
|
||||
2. 核对窗口标题中的完整 PRJ 路径,确认它关联本轮日志中的数据库。
|
||||
3. 选择 **Project → Repair**,阅读确认框后执行。原生工具会检查工程并尝试修复,事先创建当前状态备份。
|
||||
4. 查看 **View → Output** 中的完整输出;必要时使用 **Backup → View operation log**。记录选择了哪份历史备份、是否恢复旧版本,以及是否明确报错。
|
||||
5. 正常关闭原生修复工具,再打开原先失败的同一工程。检查原数据库错误是否消失,并执行下节同步/Verify。
|
||||
|
||||
前面的自动化测试调用的就是此原生 Repair 入口。手动操作可直接看到原生诊断和恢复选择,但不能保证对同样的损坏取得不同修复结果。
|
||||
|
||||
### 如何判断结果
|
||||
|
||||
| 原生结果 | 后续操作 |
|
||||
| --- | --- |
|
||||
| 原位检查/修复完成 | 再次打开同一目标,核对数据库加载、工程同步、保存和关闭 |
|
||||
| 从某个历史备份恢复 | 核对备份日期、原理图、PCB 和约束内容版本,再决定是否使用;不能直接等同无损修复 |
|
||||
| `Cannot repair the project - there is no backup of missing iCDB` | 停止反复重试;当前状态备份只保留损坏现场,寻找有效恢复源或厂商支持 |
|
||||
| 仍有占用或原生操作未结束 | 先确认实际会话和操作状态,不通过强杀或删除地址/锁文件替代修复 |
|
||||
|
||||
文件头损坏且没有历史备份的本机实测已得到第三种结果。会话失联故障是否能够原位修复仍需按实际原生诊断单独验证。
|
||||
|
||||
## 能够正常打开后的手动工程更新
|
||||
|
||||
1. 打开当前工程的 PCB,进入 **Project Integration** 窗口。
|
||||
2. 查看 Forward Annotation、CES/约束加载与同步、Back Annotation 状态。对原生允许且标记 Required 的动作逐项执行,每次执行后重新观察状态。
|
||||
3. 确认所需项目达到同步状态,保存工程并正常关闭。
|
||||
4. 在 Library Manager 中选中对应 Reusable Block,使用 **Verify**,再读取真实校验结果。工具中的权威状态检查可以进一步确认 `READY / raw=1`。
|
||||
|
||||
本机生产流程及实机日志已验证四项同步、保存、关闭和权威 Ready 状态。这些更新动作要求工程数据库能够加载;若一打开就报告 ICDB 数据库无法加载,应先处理修复/恢复。
|
||||
|
||||
## 检查与升级相关入口的实际用途
|
||||
|
||||
| 本机确认的入口 | 用途与验证边界 |
|
||||
| --- | --- |
|
||||
| iCDB Server Manager → Tools → Diagnostics → Check database | 数据库诊断;本轮只确认菜单存在,未在现场库执行。不能视为另一种已验证的修复方式 |
|
||||
| iCDB Project Backup → Project → Create support package | 创建支持资料,供对应版本技术支持分析;本轮只确认菜单存在 |
|
||||
| Library Manager → Tools → Update DxSystems Designer Cache | DxSystems Designer 缓存更新,不能据菜单名称推断它能恢复损坏的工程或共享符号数据库 |
|
||||
| `conv2iCDBpi` | 本机内置帮助标明用于旧 CDB/CES 到 iCDB 的转换,`-updatepowers` 只更新已转换库的 POWER 属性;未验证为损坏库修复路线 |
|
||||
| `SideFiles2iCDB` | 包装到 `sandboxfile.exe`,帮助标明从文件导入 pin package length 数据,要求已有 iCDB、snapshot、topblock;没有验证其具有通用重建能力 |
|
||||
|
||||
本次实际核对的 iCDB Project Backup 菜单中没有独立的“强制升级损坏数据库”入口。更新 Mentor 软件、安装补丁或正常格式迁移应根据工程创建版本、目标版本兼容说明及正式向导,在完整副本上操作;升级软件不能保证补回损坏或丢失的数据。
|
||||
|
||||
本机旧 CHM 帮助重定向到 InfoHub,但当前安装的完整 InfoHub 文档不齐全。上述菜单以实际二进制菜单资源为依据,转换工具用途以本机内置帮助字符串为依据;未确认的升级步骤没有作为可执行指令列出。
|
||||
|
||||
## 本仓库实机记录
|
||||
|
||||
- `docs/releases/2026-10-28-icdb-open-repair-reopen-validation.md`:实际报错 → 原生恢复 → 同一工程重新打开 PASS/READY。
|
||||
- `docs/releases/2026-10-28-icdb-no-history-repair-validation.md`:没有历史备份时同类文件头损坏未能修复,保留原生 no-backup 错误文本。
|
||||
|
||||
若现场报错明确指向共享符号数据库,应继续核对该共享存储支持的原生恢复/重建入口和原始符号来源。目前没有已验证的共享库强制更新修复步骤。
|
||||
|
||||
## 工具中的“从备份恢复 CBB”弹窗
|
||||
|
||||
读取中心库后,点击数据库维护区的“从备份恢复 CBB…”,选择目标名称及独立备份中的同名工程 PRJ,再点击“恢复并校验”。备份必须包含完整工程目录,包括原理图 ICDB、PCB、布局数据库和相关工程文件;仅有 PRJ 或单个 `icdb.dat` 不够。
|
||||
|
||||
程序保留完整目标库副本,先在独立库副本中验证所选备份。验证通过后把原 Work 保留为 `Work.before-restore-*`,恢复完整 Work,再打开目标执行集成、保存、关闭和 Ready 检查。副本准备阶段会显示文件数量,运行时需等待原生操作完成再关闭弹窗。整个过程需要存放两个中心库副本及工程副本的额外磁盘空间。
|
||||
|
||||
恢复结果采用所选备份的工程版本。原工程、来源副本、库快照和原生日志都保留在恢复记录中。程序不会在失败后自动覆盖共享库回退,也不会把未确认结果标为成功。
|
||||
|
||||
此入口用于已有同名 CBB 的完整工程恢复,不删除库条目,不等同跨库原生重新导入。共享符号库自身损坏、备份引用外部工程或数据库、工程不完整、仍有活动会话时会停止;当前工程数据库需要位于完整 Work 目录中。
|
||||
|
||||
公开网络资料核对见 `docs/diagnostics/2026-10-28-icdb-web-recovery-research.md`,包含可读原文链接及适用范围。原生 Repair 的图文用户案例与上述菜单步骤一致;新的网络监听配置案例不能代替数据库修复。文件头损坏的无备份试验也不能代表原始会话失联故障必定不可原位修复。
|
||||
@@ -0,0 +1,116 @@
|
||||
# iCDB 旧服务器会话失联:分析、恢复步骤与分支改动
|
||||
|
||||
分支:`codex/icdb-lost-session-diagnostics`,基于 `a11eb3c`(`v0.2.2-beta.1`)。
|
||||
|
||||
## 错误的含义和影响范围
|
||||
|
||||
现场消息为:
|
||||
|
||||
```text
|
||||
The last server session on different server machine lost the connection
|
||||
to the database. Please use iCDB Project Backup 'Repair project'
|
||||
functionality to fix this issue.
|
||||
Machine: [200091-CBB]
|
||||
Session: [2026-10-24 08.22.29-10308]
|
||||
```
|
||||
|
||||
原生 iCDB 检测到先前服务器会话的数据库状态需要检查/修复。这不是
|
||||
“当前网络连接已经恢复就可以继续”的提示,也不能仅凭它认定端口被 Windows 保留。
|
||||
`Machine` 和 `Session` 是故障记录中的原服务器、会话标识,不能当作当前进程 PID。
|
||||
|
||||
用户贴出的路径包含 `5:\5ymbolLibsReusableBlockssymb`,存在文字识别/转录歧义。
|
||||
本机隔离样本确实存在 `SymbolLibs\__ReusableBlocksSymb\icdb.dat`:如果现场原文
|
||||
指向这个结构,问题影响的是中心库共享符号数据库,而非单个 CBB 的
|
||||
`ReusableBlocksLP\<CBB>\Work\database`。必须以现场原始日志为准,不能自动把
|
||||
`5:` 改成 `S:`,或根据 CBB 名称推导修复目标。
|
||||
|
||||
异常关闭、原服务器/网络存储断开、写库期间复制工程、跨机器访问不一致的
|
||||
路径,都可能涉及此类状态。现有消息不能区分这些原因,也不能证明
|
||||
“校验失败一定导致损坏”;数据库异常本身同样会导致校验失败。共享符号库
|
||||
出问题时,不同失败 CBB 可能只是访问了同一个有问题的数据库。
|
||||
|
||||
## 现场恢复步骤
|
||||
|
||||
1. 停止批量校验,核对 `200091-CBB` 和其他访问同一中心库的客户端。
|
||||
正常关闭占用该目标的 Library Manager、PCB、原理图/符号编辑会话,并确认
|
||||
原数据库服务器的实际状态;只看本机没有 PCB 或 TCP 探测失败不足以确认独占。
|
||||
2. 在会话正常关闭后保留完整、一致的工程/中心库副本。共享库问题需包括
|
||||
`.lmc`、相关 `SymbolLibs` 数据库、原有 `ProjectBackup`/数据库备份和日志。
|
||||
工具日常的 `backup/` 排除了 `database` 等目录,网络操作还可能跳过备份,
|
||||
因而不能把它当作这次修复所需的完整回滚副本。
|
||||
3. 使用现场相同 Mentor 版本的 **iCDB Project Backup**,先核对打开的工程或
|
||||
备份存储确实关联报错数据库,再执行 **Project → Repair**(错误提示称为
|
||||
`Repair project`)。如果目标是共享符号库,而工具无法识别对应库/存储,
|
||||
应由库管理员按该版本支持的库恢复流程处理,不能改选无关的 Work PRJ。
|
||||
4. 保留原生工具的修复输出及 `Project Repair` 备份,确认修复结果。
|
||||
不通过删除 `sAddress.adr`、锁文件、`icdb.dat`,也不通过强杀数据库服务器
|
||||
来代替 Repair。如果原生修复失败,再依据原生日志选择一致备份恢复。
|
||||
5. 正常重新打开原先失败的目标,先复测一个 CBB,检查本轮原生日志、
|
||||
Project Integration、Save/Close 和进程释放,再以 `IsVerifiedReuseBlock`
|
||||
返回 `READY/raw=1` 确认校验成功。之后再扩大批次。
|
||||
|
||||
本机已确认原生入口:
|
||||
|
||||
```text
|
||||
%SDD_HOME%\iCDB\win32\bin\iCDBProjectBackup.exe
|
||||
```
|
||||
|
||||
只读检查其命令行帮助显示 `-p <project file>`、`-ver`、`-h`;菜单资源确认存在
|
||||
`Project → Repair`。没有查到公开的直接 Repair 命令行开关,因此本分支没有
|
||||
编造 `-repair` 参数,也没有自动投递写库修复命令。曾在本机既有隔离样本上
|
||||
打开原生管理界面进行入口检查,未执行 Repair;本机没有用户本次故障库,
|
||||
未在 `200091-CBB` 或现场网络库上执行恢复。
|
||||
|
||||
## 代码修复
|
||||
|
||||
- 在内容检查输出、实时弹窗和本轮 `PCBLogFile.txt`、`ForwardAnnotation.txt`、
|
||||
`MsgBoxResponse.log`、`PartPkg.log` 中识别完整的服务器会话失联消息。
|
||||
- 分类为 `ICDB_REPAIR_REQUIRED`,保留原始错误和 `Machine/Path/Session`,
|
||||
区分共享符号库、Work 工程数据库及不能确定的目标;写入
|
||||
`diagnostics/icdb-recovery.json` 并在失败清单显示恢复说明。
|
||||
- 该错误优先于通用打开失败、JobPrefs 错误及清理失败;即使内容检查同时
|
||||
输出成功文字和退出码 0,也不能变成 PASS。标记 `safe_to_continue=False`,
|
||||
后续条目标记 SKIPPED,沿用现有会话、证据、网络映射保留机制。
|
||||
- 将当前日志与启动前检查点比较,支持追加、截断、等长/增长重写、UTF-16
|
||||
及 UTF-8;恢复识别每次最多读取两个 128 KiB 片段。不能确认日志新旧时
|
||||
保留原始失败,不据旧记录推荐本轮修复。重写后保留的同一历史会话不作为
|
||||
新证据;可验证的追加则允许识别同一会话再次报错。
|
||||
- 通用 `Could not run server` 不再直接推断端口保留并提示重启 WinNAT。
|
||||
启动前独立端口绑定探测保留原逻辑。
|
||||
|
||||
## 验证和边界
|
||||
|
||||
原分支基线:`python -m pytest -q`,383 项通过。
|
||||
新增回归包括原错误、共享库身份、内容检查停止队列、多行集成日志、
|
||||
日志重写及历史错误隔离、编码、证据写入失败和监督器失败摘要。
|
||||
|
||||
诊断修复阶段验证(后续生命周期修复见下方链接):
|
||||
|
||||
- 全量 Python 测试 **417 项通过**,含 34 项新增回归;仍有基线已有的
|
||||
一条 pywinauto STA COM 模式提示。
|
||||
- 两组 PowerShell 测试、`python -m compileall -q src tests`、
|
||||
`git diff --check` 通过。
|
||||
- 只读解析仓库留存的真实 TUNER_R836_A 现场日志,正确提取到机器 `EDA03`、
|
||||
`S:\ReusableBlocksLP\TUNER_R836_A\Work\database` 及会话
|
||||
`2025-01-21 14.44.46-2323356`。该记录仅用于解析验证,不代表本次现场故障。
|
||||
- 独立代码复查发现的集成日志遗漏、通用端口建议覆盖具体原因、历史日志
|
||||
重写误判、不可读/超出窗口的基线问题,均补充回归并通过复核。
|
||||
|
||||
本分支修复自动化的错误识别、误导性建议和失败后的继续执行风险,
|
||||
现在还会在目标明确、会话完全释放且完整备份成功时,自动调用同版本原生
|
||||
Project → Repair,并只在原生诊断、Project Repair 备份、目标数据库关闭等
|
||||
证据全部满足时重试一次校验。共享符号库路径没有 PRJ 入口时会明确阻断,
|
||||
不会猜测或改修某个 CBB 的 Work 数据库。
|
||||
|
||||
这次在新复制的 IP101GR 工程上完成了真实隔离验证:Repair 菜单确认、完整
|
||||
备份、原生 `dbcheck`/数据库日志、`Project Repair` ZIP 校验和数据库关闭均
|
||||
成功;Repair worker 退出码为 0,随后才允许重试。该验证没有接触生产库。
|
||||
|
||||
仍然**没有修复现场受损数据库,也没有在 `200091-CBB` 上执行恢复**。
|
||||
恢复识别仅覆盖已知日志末尾的有限窗口;错误出现在窗口外、相同历史会话
|
||||
在重写时被原样重报、超出基线窗口的大日志发生重写、以及现场不同日志位置/格式,
|
||||
仍需结合完整原生日志确认。这些不能确认新旧的情况不会自动提升为本轮修复诊断。
|
||||
|
||||
用户后续确认事故版本为 r4 或更早。失败强杀根因、正常关闭修复、自动 Repair
|
||||
范围和 448 项最终回归详见
|
||||
[失败生命周期与恢复验证](../releases/2026-10-27-icdb-failure-lifecycle-and-recovery.md)。
|
||||
@@ -0,0 +1,47 @@
|
||||
# ICDB 故障修复公开网络资料核对
|
||||
|
||||
检索日期:2026-10-28。目标是寻找原生会话失联、无备份恢复以及中心库共享符号数据库的可核实修复步骤。
|
||||
|
||||
## 能够读到原文的资料
|
||||
|
||||
1. **《Mentor XP如何修复文件》**,CSDN 用户经验文章:
|
||||
|
||||
https://blog.csdn.net/qq_53931654/article/details/147240707
|
||||
|
||||
已获取实际正文,内容为:打开 iCDB Project Backup、指定损坏文件路径、执行 Project → Repair,并有三张操作截图。该路线与本机 EE7.9.5 实际菜单及隔离实测一致。文章没有提供无健康恢复源时的通用强制修复办法,也没有验证共享 SymbolLibs 存储的修复入口。
|
||||
|
||||
2. **《Xpedition 2409 Designer 无法打开 icdb 连接》**,CSDN 用户经验文章:
|
||||
|
||||
https://blog.csdn.net/kkisscj/article/details/144323677
|
||||
|
||||
已获取实际正文。作者参考 VX2.8 的 icdb_admin 文档,将工作目录下 Server 配置中 Network/Address 设置为本机监听地址,解决其连接失败。这是 2409 版本的连接配置案例,不能直接当作 EE7.9.5 的数据库损坏或旧服务器会话失联修复方法,本轮未据此修改任何网络或数据库配置。
|
||||
|
||||
3. **Siemens Support Center 官方入口**:
|
||||
|
||||
https://support.sw.siemens.com/en-US/
|
||||
|
||||
本环境请求返回 HTTP 403,未读取到官方知识库文章。因此没有将检索摘要、第三方说法或猜测写成官方恢复结论。可以由具备相应产品支持权限的用户在此检索 iCDB administration、Project Backup Repair 和原始失联错误文本。
|
||||
|
||||
## 未找到能够验证的公开方案
|
||||
|
||||
本轮没有找到可核实的公开说明,能够保证在缺少有效恢复源时重建已经破坏的 icdb.dat,或直接修复 `SymbolLibs/__ReusableBlocksSymb` 的跨机器会话失联。检索无结果不能证明厂商没有内部恢复方式,只能说明当前没有依据提供相应可执行指令。
|
||||
|
||||
已排除 DLL 修复下载广告、AI 聚合回答,以及与原始故障不同的连接配置说明。没有根据这些结果操作现场库或删除数据库/锁文件。
|
||||
|
||||
## 对前面试验结论的必要限定
|
||||
|
||||
人为清零文件头得到的是 `old, unsupported format`。在无历史备份时,该特定试验原生 Repair 报告 `Cannot repair the project - there is no backup of missing iCDB`。
|
||||
|
||||
用户最初讨论的是 `The last server session on different server machine lost the connection to the database`。两者不是同一种故障,不能从文件头损坏试验推出“实际会话失联没有备份就一定修不好”。后者仍应在完整副本、实际占用已释放、目标身份明确的前提下尝试同版本原生 Repair,并根据修复后再次打开的结果判断。
|
||||
|
||||
## 当前可执行路线
|
||||
|
||||
对明确属于 Work/database 的工程数据库:正常结束实际写入会话 → 保留完整工程副本 → 用原生 iCDB Project Backup 打开对应 PRJ → Project/Repair → 查看 Output 是否原位修复、选择历史备份或明确失败 → 正常重新打开同一工程 → Project Integration/保存关闭/Verify。
|
||||
|
||||
若错误路径属于共享 SymbolLibs 数据库,本轮未找到已验证的 PRJ 替代入口。不能把任意 CBB 的 Work PRJ 指给它。需要针对该存储使用对应版本厂商支持的恢复/重建路线。
|
||||
|
||||
本机静态资源复核也显示 iCDBProjectBackup 的工程选择过滤器为 `Project file (*.prj)|*.prj||`,没有据此推断它可直接接受 LMC 或共享符号库目录。
|
||||
|
||||
已有 r4 现场日志中还保留一条具体工程失联记录,路径为 `S:\ReusableBlocksLP\TUNER_R836_A\Work\database`、原服务器 `EDA03`。这是旧日志,不能自动等同于当前 `200091-CBB` 故障,但说明现场历史中也存在工程数据库故障,需要逐条按原生 Path 区分。
|
||||
|
||||
网络检索原始页面及正文摘录保存在 `diagnostics/icdb-web-research-20261028`;未随源码发布第三方完整网页。
|
||||
@@ -0,0 +1,47 @@
|
||||
# Issue 5:私有桌面泄露
|
||||
|
||||
## 现象
|
||||
|
||||
代码与回归测试确认:原生 worker 创建私有 Windows Desktop 后,如果 Job Object 创建或 worker 进程启动失败,构造函数直接抛出异常,已创建的 Desktop 和 Job 句柄没有释放。重复失败存在累积私有桌面资源的风险;本次未对现场所有桌面异常作归因。
|
||||
|
||||
状态读取路径还有一个相同的边界:`read_library_flag` 在进入资源清理保护前加载 UI 依赖;依赖加载异常时,刚创建的私有桌面无法执行 `CloseDesktop`。
|
||||
|
||||
## 根因
|
||||
|
||||
1. `DesktopWorker.__init__` 的资源申请跨越多个可能失败的步骤,但没有事务式回滚。
|
||||
2. 进程句柄、Job 句柄和 Desktop 句柄的释放顺序没有覆盖构造阶段异常。
|
||||
3. `read_library_flag` 的依赖导入位于 `CreateDesktop` 之后、`try/finally` 之前。
|
||||
4. `read_library_flag` 和 `read_library_tree_image_map` 都在清理保护外先创建 Desktop,再执行 `Path(library)` 等准备逻辑;准备阶段抛异常时也会泄漏。
|
||||
|
||||
## 解决方案
|
||||
|
||||
- 在 `DesktopWorker` 构造开始时初始化资源字段,并把 Desktop、Job、worker 创建放入同一异常边界。
|
||||
- 构造失败时按 worker、Job、Desktop 顺序回滚;已启动的 worker 先终止,避免留下无父级的原生进程。
|
||||
- 在状态读取创建 Desktop 前完成 UI 依赖加载,确保依赖异常不会产生未受保护的 Desktop。
|
||||
- 两处状态读取均在准备逻辑完成后、`try/finally` 内创建 Desktop,清理时检查资源是否存在。
|
||||
- 测试中分离 Desktop、Job、Process、Thread 的接口,断言失败后关闭顺序;覆盖四个构造失败点与进程创建后的终止分支。
|
||||
|
||||
## 影响范围
|
||||
|
||||
- 覆盖原生 worker 构造失败,以及状态读取准备和启动阶段的资源清理路径。
|
||||
- 正常任务的私有桌面、Job Object、进程树和数据库释放策略不变。
|
||||
- 不会切换用户输入桌面,也不会改变中心库写入策略。
|
||||
|
||||
## 验证
|
||||
|
||||
- 修复分支完整 `python -m pytest -q --tb=short`:480 passed。
|
||||
- 同环境、同工作树切换到 `origin/main`(`39985f7`)完整运行相同命令:471 passed。两次运行依次执行,均仅有一条 pywinauto STA 警告。
|
||||
- `tests/test_desktop_worker.py`:9 passed。两处状态读取准备阶段泄漏的新增测试在修复前 2 failed,修复后通过。
|
||||
- 变异验证:把回滚中的 `self.job.Close()` 故意改为 `self.job.CloseDesktop()`,测试产生 2 failed;恢复正确代码后通过。
|
||||
- `python -m compileall -q src tests`:通过。
|
||||
- `git -c core.whitespace=cr-at-eol diff origin/main HEAD --check`:通过。命令显式识别 Windows CRLF;仓库 `.gitattributes` 为 `* -text`,保留源文件换行,不依赖本机 `core.whitespace` 配置。
|
||||
|
||||
此前 #7 将一次运行中的 `test_backup_recovery.py::test_invalid_source_does_not_touch_target` 失败称为既有基线问题,缺少同环境对照,撤回该归因。本次该用例在 main 与修复分支均通过。审查者在旧 WIP 基线测得的 19 个失败不属于本次从 main 独立提交后的测试范围。
|
||||
|
||||
## 合并范围修正
|
||||
|
||||
原 PR #7 以 ICDB WIP 分支为父提交,却以 main 为合并目标,实际携带 59 个提交、118 个文件。现从 main 单独 cherry-pick 桌面修复并补充上述审查修正;相对 main 只有本工单、两个源码文件和一个测试文件,共 4 个文件。独立代码复审未发现待处理问题。
|
||||
|
||||
## 分支
|
||||
|
||||
`codex/desktop-leak-main`
|
||||
@@ -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 恢复流程,不应直接删锁或强杀数据库进程。
|
||||
@@ -0,0 +1,99 @@
|
||||
# r4 失败后 iCDB 断库:根因修复与受控恢复
|
||||
|
||||
分支:`codex/icdb-lost-session-diagnostics`。用户确认现场事故使用 r4 或更早版本。
|
||||
|
||||
## 工具侧根因
|
||||
|
||||
在 r4(`11a2b2e`)中,单项校验失败和监督器超时都会进入强制进程树清理:
|
||||
|
||||
- `DesktopWorker` 设置 `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE`,`kill()` 调用
|
||||
`TerminateJobObject`,回退路径是 `taskkill /T /F`。
|
||||
- `cleanup_verify_processes()` 对目标 PCB 执行同样的强制进程树终止。
|
||||
- `prepare_icdb_address()` 会在 TCP 探测失败后移走地址文件。连通性失败
|
||||
不能证明服务器已安全退出;移走地址也不能修复数据库中未完成的会话状态。
|
||||
|
||||
这条失败收尾路径能连带杀死 PCB 启动的 iCDB 服务器,打断数据库事务或关闭过程,
|
||||
留下 dirty session。下次由其他机器打开时,iCDB 拒绝继续并要求 Repair,
|
||||
与用户描述的“未校验成功后才出现失联消息”吻合。现有代码足以确认该缺陷,
|
||||
但没有 200091-CBB 本次完整进程/数据库日志,不能确定具体中断的是哪次事务。
|
||||
|
||||
`a11eb3c` 已修掉主要 PCB 强杀路径;继续检查发现内容检查超时仍保留
|
||||
`taskkill /T /F`,以及失败文档只保留在隐藏桌面、没有正常关闭尝试。
|
||||
这些遗漏在本分支继续修复,不能让用户继续用 r4 的收尾逻辑测试。
|
||||
|
||||
## 本轮行为
|
||||
|
||||
- 内容检查超时保留 importer 及原生子进程,使用文件承接输出,防止父进程
|
||||
退出造成管道阻塞/断管;记录 PID、命令和退出状态并停止后续队列。
|
||||
- 校验超时/监督器丢失改为协作取消。阻塞中的原生调用先返回,再在操作边界
|
||||
停止后续动作;不终止正在 Save 的助手。
|
||||
- 原生调用返回且已确认身份的失败文档,由 x86 助手核对 PRJ 和 PCB 名称后
|
||||
执行 `Close(False)`,释放引用后正常 Quit。若另一个文档仍活动则保留应用;
|
||||
慢 Close 同样保留助手,不用强杀代替完成。
|
||||
- 写入前和成功前同时核对 Work、PCB 及中心库
|
||||
`SymbolLibs/__ReusableBlocksSymb` 的地址所有权。隐藏 Library Manager
|
||||
在失败 PCB 正常关闭后也请求正常关闭;所有权未知时继续停止批次。
|
||||
- 只读状态桥先关闭标准输入并等待 EOF 清理,避免正常退出也被立即终止。
|
||||
|
||||
## 自动 Repair 的范围
|
||||
|
||||
只有本轮明确的 `ICDB_REPAIR_REQUIRED`、完整会话身份、可映射到当前
|
||||
Work/PCB PRJ 的数据库,才进入一次原生恢复尝试。TIMEOUT、未释放的
|
||||
importer/PCB/Library Manager/助手、外部目标及路径歧义都不会启动 Repair。
|
||||
|
||||
恢复前通过 Windows 独占文件访问确认目标 `icdb.dat` 没有仍打开的客户端,
|
||||
完整复制工程全部文件(包括数据库、锁、原备份、日志),逐项校验副本及
|
||||
源文件集合。该副本与平日排除数据库的轻量 evidence 备份不同。
|
||||
|
||||
Repair worker 绑定监督进程和确切私有桌面。它从安装程序资源解析
|
||||
`Project → Repair`,核对原生窗口中的完整 PRJ,只接受实际验证过的
|
||||
Repair 确认文本。备份期间取消会阻止后续启动;原生 Repair 已经开始后,
|
||||
超时或监督器丢失不会强制终止它。
|
||||
|
||||
恢复成功需要本次原生 PID 的诊断完成、数据库关闭、完整 `Project Repair`
|
||||
ZIP、原生进程正常退出及地址释放。满足后只重试一次完整校验,仍以
|
||||
`IsVerifiedReuseBlock READY/raw=1` 为最终成功条件。原错误、原 evidence
|
||||
和恢复结果保存在 `recovery_attempts`,完整修复备份保留。
|
||||
|
||||
**共享符号库边界:** 本机同版本 Project Backup 直接打开符号数据库目录时,
|
||||
明确拒绝并要求 `.prj`。因此对 `SymbolLibs/__ReusableBlocksSymb` 的已损坏
|
||||
数据库,本分支会报告 `SHARED_DATABASE_REPAIR_UNSUPPORTED`,不会改修一个
|
||||
不相关的 Work 工程,也不会删地址/锁或猜测私有 ABI。正常关闭和共享所有权
|
||||
检查用于防止再制造这类问题;已有共享库损坏仍需匹配该库的原生恢复入口。
|
||||
|
||||
## 实际验证
|
||||
|
||||
以下写入均针对本轮新副本,未修复或改动现场 200091-CBB 数据:
|
||||
|
||||
- 真实 Windows 无害 importer/子进程试验:超时后均存活,后续队列停止。
|
||||
- x86 cscript 测试:核对身份、失败关闭用 `Close(False)`、不保存错误结果、
|
||||
另有活动文档时不 Quit、Save 返回后响应取消。
|
||||
- 新 IP101GR 副本主动注入空闲时的同步失败:输出 `FAILURE_CLOSE_OK`、
|
||||
`FAILURE_QUIT_OK`,随后确认 PCB 进程退出;失败结果保持 FAIL。
|
||||
- 同一隔离库随后正常校验:PASS/GREEN,Save/Close/退出和
|
||||
`READY (IsVerifiedReuseBlock, raw=1)` 均确认。
|
||||
- 原生 Repair 模块真实执行:完整备份、Repair 确认、本 PID 的 dbcheck/
|
||||
数据库关闭日志、Project Repair ZIP 校验齐全,最终 `REPAIRED`、退出码 0。
|
||||
这验证了原生恢复链,不代表复现并恢复了现场同一个 dirty session。
|
||||
- 完整自动恢复链:向隔离副本的首次校验注入合成失联错误,之后实际执行
|
||||
正常关闭、完整备份和原生 Repair,再启动第二次真实校验,最终
|
||||
PASS/GREEN、`safe_to_continue=True`。只启动两次校验 worker、一次 Repair;
|
||||
原错误及 evidence 保留。没有故意破坏数据库,也没有恢复现场损坏库。
|
||||
- PyInstaller 目录包构建和冻结 GUI 生命周期冒烟通过;直接使用生成的 EXE
|
||||
在另一个独立工程副本上运行 Repair worker,最终 `REPAIRED`、退出码 0。
|
||||
因此原生修复入口也已验证能在冻结包内工作。
|
||||
- 原样本 21,011 个文件在上述试验前后 SHA-256 内容指纹一致。
|
||||
- 最终 Python 全量回归 **448 项通过**(含前次 417 项基线);仍有基线已有的
|
||||
一条 pywinauto STA COM 模式提示。两组 PowerShell 测试、编译和 Git 空白检查通过。
|
||||
- 独立复查指出的未释放 importer 进入 Repair、备份期间取消、修复 worker
|
||||
父进程丢失、共享 COM 应用退出等边界均已修正,并补充对应回归。
|
||||
|
||||
本地原始证据位于 `diagnostics/icdb-auto-recovery-20261027`,不随包发布。
|
||||
|
||||
## 分支试用包
|
||||
|
||||
本地 `CbbRefreshVerify-icdb-recovery-20261027.zip` 包含完整目录程序、试用说明、
|
||||
本报告和逐文件 SHA-256 构建清单;压缩包旁另有 SHA-256 文件。源码构建提交为
|
||||
`9cd4968`,版本号仍为 `0.2.2b1`,这是分支修复试用包,不是新正式版本。
|
||||
解压整个目录后启动 `CbbRefreshVerify.exe`,必须保留 `_internal`。尚未部署到
|
||||
200091-CBB,也未推送远端;现场已有共享库损坏仍受上述原生入口限制。
|
||||
@@ -0,0 +1,47 @@
|
||||
# CBB 完整工程备份恢复弹窗实机验证
|
||||
|
||||
分支:`codex/icdb-global-repair`。本轮新增“从备份恢复 CBB…”入口,弹窗仅包含目标 CBB、备份工程 PRJ、“恢复并校验”和“关闭”。
|
||||
|
||||
## 已验证的行为
|
||||
|
||||
现有同名 CBB 的完整工程备份先在独立中心库副本中验证;只有原生流程、权威 Ready 和会话释放全部通过,才保留原 Work 并恢复目标,再对目标实际打开、保存、关闭和读取状态。全程保留完整库回退副本、来源副本、原工程和日志。
|
||||
|
||||
## 真实损坏与恢复
|
||||
|
||||
测试材料位于 `diagnostics/icdb-backup-dialog-live-20261028`,只改动新建的隔离副本,CBB 为 `IP101GR`。
|
||||
|
||||
1. 从健康库复制目标,移除目标 Work 中的 `ProjectBackup`、`cdbback` 历史目录,清零 `Work/database/icdb.dat` 的前 64 字节。
|
||||
2. 实际打开该目标,得到 `MENTOR_DATABASE_LOAD_FAILED`。原生日志明确为 `Database cannot be loaded ... Please use iCDB Project Backup 'Repair project' ... icdb.dat ... old, unsupported format`。正常关闭未加载工程的原生窗口后继续。
|
||||
3. 在真实主窗口点击“从备份恢复 CBB…”,选择独立健康来源 `verify-library/ReusableBlocksLP/IP101GR/Work/IP101GR.prj`,再点击“恢复并校验”;没有替换 reader、recoverer 或 verifier。
|
||||
4. 验证库位于 `C:\Users\ABS_Windows\.cbb-restore\b963898c4e91`。17:47:59 原生 COM 确认该副本工程身份;四项集成状态均为 InSynch。17:48:19 已确认原生退出,结果 `PASS / GREEN / READY (raw=1)`。
|
||||
5. 保留旧工程为 `Work.before-restore-b963898c4e91`,安装验证过的完整 Work。
|
||||
6. 17:50:54 原生 COM 确认重新打开的正是最初失败的目标路径。四项集成状态均为 InSynch,Save、IsSaved、Close、Quit 标记完整;17:51:24 确认退出。目标结果 `PASS / GREEN / READY (raw=1)`,`safe_to_continue=True`,原来的数据库加载错误消失。
|
||||
|
||||
`comparison.json` 的七项检查全部通过:真实打开失败、备份副本验证成功、相同目标重新打开、最终 Ready、损坏 Work 保留、原始源库未变、原生加载错误消失。原始源库 21,029 个文件的 SHA-256 对照一致。
|
||||
|
||||
主要证据:
|
||||
|
||||
- `open-before.json`、`open-before-PCBLogFile.txt`。
|
||||
- `data/evidence/backup-restore-b963898c4e91/recovery-result.json`。
|
||||
- `source-verify/verify-bec9008be868`、`target-verify/verify-22808a7f330a` 下的结果、事件和原生日志。
|
||||
- `dialog-before.png`、`dialog-after.png`、`window-after.png`、`ui-state.json`、`comparison.json`。
|
||||
|
||||
## 停止条件和兼容修正
|
||||
|
||||
实际观察到“工程已打开保存、但最终仍为 PENDING”的失败情形,恢复流程在替换目标前停止;损坏目标字节保持原样。短路径复核取得 Ready 后,生产实现采用真正较短的用户目录,避免深层证据目录及 MSIX 对 AppData 的重定向。
|
||||
|
||||
原生 PCB 工程允许多个配置段具有 `CentralLibrary` 引用,所有需要变更的引用均对齐。语义已指向目标库的引用保留原字节及时间。绝对工程/数据库引用、目录链接、不完整来源、来源/目标内容变化、未释放会话均会阻止安装。原生目标操作保留映射盘/别名;文件快照和 Work 替换使用实际路径。
|
||||
|
||||
## 回归与交付
|
||||
|
||||
- `python -m pytest -q`:471 passed,1 个既有 pywinauto STA 提示。
|
||||
- 16 项新增恢复和弹窗测试覆盖输入校验、来源失败、最终失败、来源保护、并行快照、junction 拒绝、目标别名、等价引用、界面响应及异常终态。
|
||||
- PyInstaller 构建及冻结程序启动/退出 smoke 检查通过。
|
||||
- `frozen-modules.json` 确认恢复模块、弹窗、主窗和 Mentor 适配模块的冻结代码与当前源代码一致。
|
||||
- 程序:`dist-icdb-backup-dialog-20261028/CbbRefreshVerify-Background/CbbRefreshVerify.exe`。
|
||||
|
||||
复现脚本:`tools/validate_backup_recovery_dialog.py`,依次执行 `setup`、`open-before`、`restore`、`compare`。`setup` 仅创建全新的测试目录;再次试验前保留现有证据,并将脚本中的输出 `root` 改为另一个全新诊断目录。
|
||||
|
||||
## 范围
|
||||
|
||||
本功能恢复已有同名 CBB 的完整工程,依赖目标库中已有的依赖数据,不删除库条目,不等同原生跨库重新导入或共享符号库重建。测试复现的是数据库文件头损坏;没有复现原始不同服务器会话丢失的问题,不能据此承诺所有 ICDB 故障均可恢复。恢复内容采用用户选择的备份版本。
|
||||
@@ -0,0 +1,46 @@
|
||||
# 全库 ICDB 检查与修复按钮实机验证
|
||||
|
||||
日期:2026-10-28。分支:`codex/icdb-global-repair`。
|
||||
|
||||
## 行为与界面
|
||||
|
||||
独立“数据库维护”面板提供“全库 ICDB 检查与修复”按钮。确认框展示当前库、可用项目数、Ready 项数和缺文件跳过数,说明操作会重新校验并保存工程。队列包含 Ready 项,串行执行,显示当前检查/备份/原生修复阶段及已处理数量;可请求当前项结束后停止。
|
||||
|
||||
复用现有本轮失联证据、目标身份、独占访问、完整备份、原生 Repair 和单次重试约束。共享符号库自动修复仍不支持。Ready 项同样预检工程文件,缺少 Work/PRJ/PCB 时跳过。失败后清除本次无法确认的绿色状态。
|
||||
|
||||
结果分别报告校验通过、失败/受阻、队列未执行、原生修复完成、恢复旧备份待复核和修复未确认。额外 JSON 报告保留每项原始错误、恢复尝试和证据路径。
|
||||
|
||||
## 实机按钮路径
|
||||
|
||||
使用 Windows 本机 Qt 界面,通过 QTest 点击实际按钮和实际确认框,未替换 reader/verifier,未注入合成失联错误。测试工具为 `tools/validate_icdb_button.py`。
|
||||
|
||||
输入是新复制的本地隔离库,包含一个实际可校验的 IP101GR(读取时 Ready)。按钮仍运行此项目,最终:
|
||||
|
||||
- `PASS`、`safe_to_continue=True`。
|
||||
- `READY (IsVerifiedReuseBlock, raw=1)`。
|
||||
- 四项集成状态 InSynch;Save/IsSaved/Close 成功,PCB 正常退出。
|
||||
- 本轮正常校验不需要原生 Repair,恢复尝试数为 0。
|
||||
- 测试源库 21,029 个文件逐项 SHA-256 比对一致。
|
||||
|
||||
原始证据:`diagnostics/icdb-button-live-20261028/outcome.json`、`data/evidence/verify-ebe807433b4f/result.json`、`data/reports/icdb-20261028-120446-f28be8.json`。`loaded.png`、`confirmation.png`、`finished.png` 是实际运行截图;`final-layout.png` 使用该次真实结果回放验证最后的两行摘要布局。
|
||||
|
||||
## 真实损坏试验及修复边界
|
||||
|
||||
`tools/probe_icdb_corruption.py` 在另一个新隔离工程副本中将 `database/icdb.dat` 前 64 字节清零,然后直接调用生产原生修复 worker。完整备份、原生 Repair 备份均保留。该试验直接验证原生修复 worker,不代表按钮已经复现现场跨机器失联故障的自动恢复链。
|
||||
|
||||
原生工具在本机从工程内的 `2026-09-18 10.44.14 - consDefUpgr.zip` 恢复数据库,输出 dbrestore DONE 和 Diagnostic FINISH。程序识别这属于已有备份恢复,返回 `RESTORED_REVIEW_REQUIRED`,正常关闭空闲原生窗口并确认进程退出,未将其提升为 `REPAIRED`,未自动重试校验。恢复版本和工程内容仍需复核,不能据此宣称无损修复。
|
||||
|
||||
原始证据:`diagnostics/icdb-corruption-probe-20261028-r2/damage.json`、`repair/repair-result.json`、`repair/complete-backup/backup-manifest.json`。原生测试 PID 29096 已退出;前次保留 PID 18452 也已核对空闲状态并请求正常关闭。
|
||||
|
||||
## 验证与试用程序
|
||||
|
||||
- 全量 Python 回归 455 项通过;一条既有 pywinauto STA COM 提示。最终摘要布局另跑 21 项界面回归通过。
|
||||
- 独立代码审查提出 Ready 缺文件预检遗漏,已补充先失败后通过的回归并修正。
|
||||
- 首次回归与实机校验并发,12 项被真实全局互斥锁拦住;实机结束后完整重跑得到上述 455 项通过,未改弱互斥保护。
|
||||
- PyInstaller 目录程序位于 `dist-icdb-button-20261028/CbbRefreshVerify-Background/CbbRefreshVerify.exe`。已核对冻结包内四个变更模块与最终源码语义一致。
|
||||
- 最终冻结程序启动冒烟完成,记录 `gui_smoke_window_created` 和 `application_clean_shutdown exit_code=0`;日志在 `diagnostics/icdb-button-live-20261028/frozen-smoke-final/logs/application.log`。
|
||||
- 未部署或修改现场库。实机正常路径仅覆盖这个隔离库中的一个有效 CBB;共享符号库原生修复入口尚未支持。
|
||||
|
||||
## 后续实际打开对照验证
|
||||
|
||||
用户要求确认修复前工程实际报错、修复后再次打开。后续已在新隔离库完成:原生打开报告 ICDB 数据库格式不支持,原生恢复后同一工程重新打开并达到 PASS/READY raw=1。恢复使用旧备份,仍保留内容版本复核要求;没有复现现场跨机器失联报错。完整范围和前后证据见 `2026-10-28-icdb-open-repair-reopen-validation.md`。
|
||||
@@ -0,0 +1,41 @@
|
||||
# ICDB 无历史备份修复试验
|
||||
|
||||
日期:2026-10-28。分支:`codex/icdb-global-repair`。
|
||||
|
||||
## 实机结论
|
||||
|
||||
针对本次清零 `icdb.dat` 前 64 字节产生的文件头/格式损坏,没有可用历史备份时,EE7.9.5 原生 Repair 未能修复。此结论限于该故障类型,不应推断所有会话状态或一致性故障都必须使用历史备份。
|
||||
|
||||
原生 Project Backup 输出:
|
||||
|
||||
> The iCDB database is corrupted.
|
||||
> There is no backup of iCDB.
|
||||
> Repair project error [Cannot repair the project - there is no backup of missing iCDB].
|
||||
> The project is probably in inconsistent state. Please restore the project from one of previous backups.
|
||||
|
||||
修复尝试前后数据库 SHA-256 相同:`b3a026b0bc9f695444220008527cbc3c5c2e53a7ce4eaf7bbccac297158b7e20`。本轮新建的 Project Repair ZIP 保存的是已经损坏的状态,不能充当损坏前的健康恢复源。原生输出也区分了“修复前状态备份”与缺失的有效 iCDB 备份。
|
||||
|
||||
## 测试范围与证据
|
||||
|
||||
使用新的隔离工程副本,复制时排除所有 ProjectBackup 和 cdbback 目录,只复制使用相对路径 `./backups` 的配置,启动前断言工程树内没有 ZIP 历史档案。完整损坏副本另行保留,再调用生产原生修复 worker,没有删除源工程的任何备份。
|
||||
|
||||
程序未取得修复成功证据,结果为 `UNCONFIRMED`,没有自动重新校验。读取原生 Output 面板确认明确的 no-backup 终止错误、窗口空闲且地址释放后,正常关闭测试进程 PID 18092。
|
||||
|
||||
原始证据目录:`diagnostics/icdb-no-history-probe-20261028`。
|
||||
|
||||
- `damage.json`:破坏方式和无历史备份测试标记。
|
||||
- `native-output.json`:从原生 RichEdit Output 控件读取的完整错误文本。
|
||||
- `probe-result.json`:结果及修复前后字节一致的 SHA-256 证据。
|
||||
- `repair/complete-backup`:完整损坏状态保留副本。
|
||||
|
||||
可复现命令(需先设置 `PYTHONPATH=src`):
|
||||
|
||||
`python tools/probe_icdb_corruption.py --without-history --output icdb-no-history-probe-20261028`
|
||||
|
||||
输出目录必须新建,已有目录会拒绝覆盖。
|
||||
|
||||
## 没有历史备份时的处理
|
||||
|
||||
首先正常关闭写入方并完整保留当前损坏工程,在副本上使用匹配版本的原生工具检查。仅会话状态或部分一致性问题是否能够原位修复,应按实际诊断分别验证。
|
||||
|
||||
当原生工具像本次一样明确报告缺少有效恢复源,应寻找服务器快照、文件历史版本、其他机器的完整工程副本或厂商支持的恢复方式。若没有任何有效恢复源且原生修复失败,只能评估数据恢复或由仍完整的设计资料重建,不能承诺恢复全部原始内容。
|
||||
@@ -0,0 +1,61 @@
|
||||
# ICDB 实际打开报错、修复与再次打开对照验证
|
||||
|
||||
日期:2026-10-28。分支:`codex/icdb-global-repair`。环境:Windows 11,Mentor EE7.9.5。
|
||||
|
||||
## 结论
|
||||
|
||||
在新的本地隔离库中,实际打开 IP101GR 工程确认原生 ICDB 数据库加载失败;调用原生 Repair 后,再次打开同一工程,数据库报错消失,完整校验、保存、关闭和权威 Ready 确认均成功。本次补齐了前次试验缺少的修复前打开失败与修复后再次打开对照。
|
||||
|
||||
本次错误由清零隔离副本 `Work/database/icdb.dat` 前 64 字节产生,实际原生错误为数据库格式不支持。没有复现现场的 `last server session on different server machine lost the connection` 错误,不能将两种故障混为一谈。
|
||||
|
||||
## 修复前实际打开
|
||||
|
||||
使用生产 ReusableCircuit PCB 打开与校验流程,设置 `auto_repair=False`,没有注入合成错误或替换原生结果。工程为:
|
||||
|
||||
`diagnostics/icdb-open-repair-reopen-20261028/library/ReusableBlocksLP/IP101GR/Work/IP101GR.prj`
|
||||
|
||||
2026-10-28 13:49:14,PCB 原生弹窗报告:
|
||||
|
||||
> Problem encountered while opening one of the database files. See PCBLogFile.txt for details. Loading of the job has failed.
|
||||
|
||||
原生 PCBLogFile.txt 明确报告并指出被破坏的确切路径:
|
||||
|
||||
> Database cannot be loaded and the server cannot be started, the project will not be opened. Please use iCDB Project Backup 'Repair project' functionality to fix this issue. (Database [...]\Work\database\icdb.dat in old, unsupported format)
|
||||
|
||||
结果为 `FAIL / MENTOR_DATABASE_LOAD_FAILED`,未进入自动修复。原生失败会话经核对启动命令、空工程窗口和数据库地址释放后,请求正常关闭;没有强杀原生进程或删除地址文件。
|
||||
|
||||
证据目录 `diagnostics/icdb-open-repair-reopen-20261028` 中保留:
|
||||
|
||||
- `open-before.json` 与 `open-before-PCBLogFile.txt`。
|
||||
- `open-before/verify-95ae0baa92f3/dialogs.jsonl` 中的原生弹窗及控件文本。
|
||||
- `open-before/verify-95ae0baa92f3/diagnostics` 中的原生日志副本。
|
||||
|
||||
## 原生修复
|
||||
|
||||
先保留完整一致工程备份,再对同一 `IP101GR.prj` 和同一 `Work/database` 调用生产原生修复 worker。原生工具从工程内 `2026-09-18 10.44.14 - consDefUpgr.zip` 恢复数据库,完成 dbrestore 和 Diagnostic FINISH,并正常退出。
|
||||
|
||||
生产修复结果仍为 `RESTORED_REVIEW_REQUIRED`。本次依据用户明确要求,在隔离副本上手动继续再次打开验证;没有放宽旧备份恢复后的生产自动重试限制,也不宣称无损修复。
|
||||
|
||||
证据:`repair/repair-result.json`、`repair/repair-events.jsonl`、`repair/complete-backup/backup-manifest.json`、`repair-summary.json`。
|
||||
|
||||
## 修复后再次打开
|
||||
|
||||
再次使用生产校验流程和相同工程路径,保持 `auto_repair=False`。
|
||||
|
||||
- 13:54:45,原生 COM 确认工程身份:相同 `IP101GR.prj` 和 `IP101GR.pcb`;四项集成状态均为 InSynch。
|
||||
- 保存、IsSaved、保存后同步检查、Close 和 Quit 均完成。
|
||||
- 13:55:19,确认 PCB 进程退出;数据库地址释放。
|
||||
- 最终 `PASS`、`safe_to_continue=True`、`READY (IsVerifiedReuseBlock, raw=1)`。
|
||||
- 修复后的本轮 PCBLogFile.txt 不再出现修复前的 `Database cannot be loaded` 错误。
|
||||
|
||||
证据:`open-after.json`、`open-after-PCBLogFile.txt`、`open-after/verify-6b9874897377/events.jsonl`、`open-after/verify-6b9874897377/save.log`。
|
||||
|
||||
## 对照检查与可复现步骤
|
||||
|
||||
`comparison.json` 的七项检查全部为 true:同一工程、同一数据库、修复前原生 ICDB 报错成立、修复后工程身份匹配、原错误消失、完整校验通过、源工程数据库 SHA-256 不变。
|
||||
|
||||
测试脚本:`tools/validate_icdb_open_repair_reopen.py`。设置 `PYTHONPATH=src` 后,按顺序执行 `setup`、`open-before`、`repair`、`open-after`、`compare`。每一步需检查上一步的真实原生日志;失败打开留下的空闲原生窗口应核对目标后正常关闭,不能强杀或删锁。
|
||||
|
||||
本次按阶段直接调用生产原生修复 worker,未通过新按钮自动触发格式损坏修复。当前按钮的自动 Repair 仍以受支持的本轮失联诊断为入口。本次证明的是原生恢复确实消除了这个实际打开时的 ICDB 格式损坏报错;已有备份恢复后的工程内容版本仍需复核。
|
||||
|
||||
后续又用不含历史备份的副本测试相同文件头损坏,原生工具明确报告没有可用 iCDB 备份,无法修复,数据库字节未变。见 `2026-10-28-icdb-no-history-repair-validation.md`。
|
||||
@@ -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)。
|
||||
@@ -0,0 +1,27 @@
|
||||
# CBB Verify v0.2.3-beta.1
|
||||
|
||||
**Beta 预发布,供隔离副本和小范围现场验收使用。**
|
||||
|
||||
版本标识:`0.2.3b1`;Git 标签:`v0.2.3-beta.1`。
|
||||
|
||||
## 本版内容
|
||||
|
||||
- 增加“全库 ICDB 检查与修复”维护入口,包含 Ready 项、串行进度、结果统计和维护报告。
|
||||
- 增加“从备份恢复 CBB…”弹窗:选择同名 CBB 的完整工程 `.prj` 备份,先在独立短路径副本中验证,再保留原 Work、恢复目标并重新打开校验。
|
||||
- 恢复只有在原生打开、集成、保存关闭、进程释放和权威 `READY / raw=1` 全部满足时才报告成功;失败保留回退副本并清除旧绿色状态。
|
||||
- 加强 ICDB 会话所有权、原生修复备份恢复状态、目录链接/外部路径检查和异常 worker 的安全处理。
|
||||
- 补充真实损坏工程恢复记录、无历史备份边界说明、手动原生修复文档和自动化验证脚本。
|
||||
|
||||
## 验证记录
|
||||
|
||||
- `python -m pytest -q`:471 passed,1 条既有 pywinauto STA 提示。
|
||||
- `tests/CBBRefresh.Core.Tests.ps1`:通过。
|
||||
- `tests/ContentCheck.ExitCode.Tests.ps1`:通过。
|
||||
- EE7.9.5 隔离实测:破坏 ICDB 文件头后实际打开失败;通过恢复弹窗恢复同名完整备份;同一目标重新打开达到 `PASS / GREEN / READY (raw=1)`。
|
||||
- PyInstaller 构建和冻结程序 smoke 检查通过。
|
||||
|
||||
## 范围限制
|
||||
|
||||
恢复功能支持已有同名 CBB 的完整工程备份,不自动删除旧库条目,不实现跨库重导入或共享符号数据库重建。共享库损坏、外部路径引用、不完整备份和未释放原生会话会停止操作。恢复采用用户选择的备份版本,发布前请在隔离副本和现场版本上复核。
|
||||
|
||||
详细实机记录:[2026-10-28-cbb-backup-dialog-validation.md](2026-10-28-cbb-backup-dialog-validation.md)。
|
||||
@@ -0,0 +1,13 @@
|
||||
# 独立审查 1:只读进程探测与原生任务准入队列
|
||||
|
||||
基于 main `a56fd17`,不叠加 PR #3 的历史提交。
|
||||
|
||||
**行为变化:** 增加有界、版本化、只读 owner-probe helper 和可取消的 FIFO 原生任务队列。应用仅新增隐藏的只读 helper 分派;现有 UI、验证和原生写操作保持原来的入口。
|
||||
|
||||
**失败安全:** 查询超时或协议/身份异常返回未确认;暂停或取消的队列票据不会运行其动作,也不阻止后面的可执行任务。不启动或终止 Mentor 进程。
|
||||
|
||||
**回滚方式:** 尚未合入下游时撤销本提交即可;下游接入后须按依赖从上层向下回退。
|
||||
|
||||
完整回归:491 passed,1 条既有 pywinauto STA 提示。编译和 diff 检查通过。
|
||||
|
||||
与原评论建议的差别:`management_transaction` 直接导入管理模型和库锁;`scoped_manifest` 的协调记录与独占快照依赖库锁协议。这两者在后端审查中一起交付,避免构造导入不完整的“纯工具”基线。
|
||||
@@ -0,0 +1,42 @@
|
||||
# iCDB failure lifecycle and recovery implementation plan
|
||||
|
||||
**Goal:** Prevent failed verification from aborting iCDB owners, close owned idle failed documents normally, and attempt native recovery only against a validated idle target with a complete backup.
|
||||
|
||||
**Context:** User confirmed the incident used r4 or earlier. r4 enables `JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE`, calls `TerminateJobObject` and `taskkill /T /F` on failures and native owners. a11eb3c removed those PCB paths, but content-check timeout still kills a process tree. Failed worker exceptions currently release COM references without requesting normal document closure.
|
||||
|
||||
**Architecture:** Keep native transactions alive through timeouts. Signal cooperative cancellation to the worker; let blocking native calls return before closing a document. A separate recovery module owns target checks, backup and the official GUI repair transaction; failed or ambiguous repair remains failed. Work and central-library database ownership are checked separately.
|
||||
|
||||
**Tech stack:** Python 3.12, Windows process handles/private desktops, x86 cscript and Mentor EE7.9.5 native tools. Continue on `codex/icdb-lost-session-diagnostics`.
|
||||
|
||||
## Task 1 — Retain native owners on content timeout
|
||||
|
||||
- [x] Add a regression that starts a harmless importer with a harmless child, times out, and checks both survive until normal exit or test cleanup.
|
||||
- [x] Add a content-timeout batch regression proving the second item does not start.
|
||||
- [x] Remove tree/client termination from `_run_process` timeout; retain/drain the process in a background observer, record PID/command/release in evidence, and return TIMEOUT with `safe_to_continue=False`.
|
||||
- [x] Verify targeted tests and the existing content-check tests.
|
||||
|
||||
## Task 2 — Normal close of failed owned sessions
|
||||
|
||||
- [x] Add x86 cscript harness tests for failed Save and close-only operation, checking `Close(False)`, reference release and Quit, with failure status preserved and mismatched identity untouched.
|
||||
- [x] Implement an identity-checked close helper and call it only after the worker has returned from its native call and released Python references. Retain busy helpers instead of terminating them.
|
||||
- [x] Add cooperative cancellation at Python/VBS native-operation boundaries; a supervisor timeout/crash must not terminate a pending native Save.
|
||||
- [x] Add targeted tests for cancel before Save, cancel after a pending call returns, supervisor loss and blocked native-call retention.
|
||||
- [x] Check shared `SymbolLibs/__ReusableBlocksSymb` ownership as well as Work/PCB database ownership before writing and before declaring release.
|
||||
- [x] Give the status reader an EOF shutdown grace period before any fallback termination.
|
||||
|
||||
## Task 3 — Attempt official native recovery
|
||||
|
||||
- [x] Probe native UI on new isolated copies only; record accepted project/database identity, exact command, backup creation and completion signals. Do not invent CLI Repair flags or manipulate native ownership metadata.
|
||||
- [x] Add recovery-policy tests: reject foreign/unresolved targets, active clients, inconsistent backups and repeat repair; preserve the original error; at most one recovery attempt.
|
||||
- [x] Implement the supported native repair route with a complete backup and exact target checks. If the installed native tool cannot accept a shared symbol database, retain that explicit blocked result instead of repairing an unrelated Work project.
|
||||
- [x] Retry opening/verifying only after native success and confirmed release; authoritative READY remains the success criterion.
|
||||
|
||||
## Validation and delivery
|
||||
|
||||
- [x] Run full Python and PowerShell regressions, compilation and whitespace checks, plus independent code review.
|
||||
- [x] Verify a successful native run and a controlled failed integration/close on an isolated library, and source fingerprints afterward.
|
||||
- [x] Record precisely which recovery target types were actually exercised and any remaining field-only limitations; commit changes on the current branch.
|
||||
|
||||
## Execution evidence
|
||||
|
||||
Completed on 2026-10-27. See `docs/releases/2026-10-27-icdb-failure-lifecycle-and-recovery.md` for 448 passing Python regressions, PowerShell and x86 harness checks, real failed-session close, real native Repair, the synthetic-error/real-repair/real-retry chain, frozen EXE Repair validation, and trial-package scope. Shared symbol databases without a supported PRJ entry remain explicitly unsupported; no field database was repaired.
|
||||
@@ -0,0 +1,55 @@
|
||||
# 从健康 CBB 来源恢复的增量设计
|
||||
|
||||
状态:同名 CBB 完整工程备份恢复弹窗已实现,并通过真实损坏打开失败 → 弹窗恢复 → 同一目标重新打开 PASS/READY 的闭环验证。471 项回归通过;原生跨库重新导入替换闭环仍未验证。
|
||||
|
||||
实机记录:`docs/releases/2026-10-28-cbb-backup-dialog-validation.md`。
|
||||
|
||||
## 本轮确定的实现范围
|
||||
|
||||
采用简单弹窗:目标 CBB、完整备份工程 PRJ、“恢复并校验”和“关闭”。选择的工程名和顶层块必须与目标一致,必须保留完整 Work 数据,不能仅选择单个数据库文件。
|
||||
|
||||
后台先保存完整目标库快照及 SHA256 清单,复制来源工程,在另一份目标库副本中恢复来源并通过原生 PCB 打开、集成、保存、关闭及权威 Ready 检查。通过后再次检查来源/目标内容及会话释放状态,保留原 Work 为同级 `Work.before-restore-*`,安装已验证的工程副本,再实际打开目标校验。只有 PASS、GREEN 和已释放的结果视为成功。失败保留回退库、来源快照、旧 Work 和原生日志,不自动覆盖共享数据库回退。
|
||||
|
||||
验证副本放在用户目录 `.cbb-restore/<编号>`,记录其实际路径。避开深层日志目录及 MSIX 对 AppData 的长路径重定向;本机短路径原生复核已通过 Ready 检查。目标原生操作保留映射盘/别名,文件快照和 Work 替换使用实际路径。原本已指向目标库的等价相对引用不重写,保留工程字节和日期。
|
||||
|
||||
该功能恢复现有条目的工程数据;不删除库条目,不宣称已完成原生跨库重新导入或共享符号库重建。原生导入探测已在新建的隔离库通过“Reusable Blocks / New Reusable Block”从 PRJ 创建 IP101GR,但替换损坏条目和依赖完整性尚未形成完整验证闭环,因此本轮不开放自动先删后导入。
|
||||
|
||||
真实验证脚本:`tools/validate_backup_recovery_dialog.py`,分 setup、open-before、restore、compare 四阶段。测试目标移除历史备份并破坏数据库文件头,使用独立健康来源;restore 阶段点击实际主窗口按钮及弹窗“恢复并校验”,使用生产后端。
|
||||
|
||||
目标:原生 Repair 无法恢复时,允许选择其他目录或库中的健康 CBB 作为恢复来源,重建目标 CBB 的可用状态,并保留完整回退材料。
|
||||
|
||||
## 已确认的原生行为
|
||||
|
||||
安装自带 `SDD_HOME/lm/win32/lib/upc_utils.tcl` 提供以下依据:
|
||||
|
||||
- `runReuseBlockLoader` 使用 DMS 的 `com.mentor.dms.rbloader.jar` 执行 `copyout`,下载/恢复 CBB。
|
||||
- `processReuseBlockChunk` 在下载 CBB 和相关 Cell 后调用 `runReuseBlockImporter ... move`,同步 CBB 在共享 iCDB 中的符号。
|
||||
- `removeReuseBlocks` 经 CleanWGLib 处理 CBB 移除;注释明确说明 Cell 和 iCDB 符号也由原生清理处理。
|
||||
- `ReuseBlockImporter` 因此不能仅凭名字被当成通用完整 CBB 备份导入程序。任意本地目录/压缩包的完整原生导入方式仍需验证。
|
||||
|
||||
## 两种故障分开处理
|
||||
|
||||
1. 单个 CBB 的 Work 数据库损坏、目标中心库及共享符号库可用:可验证恢复该 CBB,并通过原生流程重新建立元数据、Cell 和符号关联。
|
||||
2. `SymbolLibs/__ReusableBlocksSymb` 等共享数据库损坏:在同一损坏目标中删除或导入 CBB 都可能因无法访问共享库失败。应先恢复共享库,或评估建立健康新库并迁移 CBB 及依赖;该迁移路线尚未实测。
|
||||
|
||||
不将删除日志、地址文件、锁文件或修改私有数据库会话字段作为恢复方法。
|
||||
|
||||
## 建议操作流程
|
||||
|
||||
1. 选择健康来源:完整工程/可导入的 CBB 包、来源库或受支持的 DMS 版本。核对 CBB 类型、名称、分区、软件版本、来源时间及符号/Cell/零件依赖;名称相同不证明内容等价。
|
||||
2. 在来源副本上验证可以打开,保留原始来源不被校验保存修改。
|
||||
3. 在隔离目标副本中验证原生导入/恢复路径。目标中心库必须可访问,错误类别和数据库路径必须明确。
|
||||
4. 通过原生流程替换/移除旧条目并恢复健康 CBB,确认 .lmc 元数据、CBB 工程、Cell 和共享符号关联一致。先验证是否支持原生替换;不盲目采用先删后导入,也不假定导入具备原子回退。
|
||||
5. 实际打开恢复的工程,完成集成、保存、关闭和权威 Verify 状态确认。失败保留原始工程、恢复来源、原生日志和完整备份;不将失败结果改为绿色。
|
||||
|
||||
## 对现有按钮的建议升级
|
||||
|
||||
维护入口展示原生修复结果;受阻项目可选择“从健康 CBB 恢复”。启动前显示来源与目标、覆盖范围和版本差异;结果区分别报告“原位修复”“从备份恢复”“重新导入恢复”“共享库阻断”和“未确认”。
|
||||
|
||||
程序可以负责来源核对、隔离验证、备份、原生操作编排和复核。若本机版本只能使用 GUI 导入,先提供原生窗口及操作指引;没有可靠的原生命令/界面路径之前,不声称支持自动重新导入。
|
||||
|
||||
## 后续验证门槛
|
||||
|
||||
- 确定一个完整、可打开的来源 CBB,并在独立副本上复现目标报错。
|
||||
- 记录本机原生本地导入/跨库复制/受支持 DMS 恢复的确切入口、替换策略、冲突提示和日志。
|
||||
- 对照恢复前后的实际工程打开结果与依赖完整性,包含导入失败、来源版本不匹配、目标共享库不可用的停止行为。
|
||||
@@ -0,0 +1,24 @@
|
||||
# ICDB Global Check and Repair Implementation Plan
|
||||
|
||||
**Goal:** Provide a separate full-library action that checks each available CBB and invokes the existing evidence-bound iCDB recovery when appropriate.
|
||||
|
||||
**Architecture:** Reuse the serial verification queue, including Ready rows, and the existing backup, ownership, native repair and retry safeguards. Confirm the full-library write scope before starting; stop on unconfirmed ownership and retain existing reports and evidence. Shared symbol database repair remains unsupported.
|
||||
|
||||
**Tech Stack:** Python 3.12, PySide6, pytest, Windows native Mentor tools.
|
||||
|
||||
1. Add UI tests for confirmation, Ready-row inclusion, skipped projects, busy state and cancellation.
|
||||
2. Add the button and route the full set of eligible rows through the existing queue without changing ordinary Verify behavior.
|
||||
3. Run focused and full regression tests. On a new isolated project copy, attempt a reproducible native session failure and run the repair; retain evidence and explicitly report whether actual corruption and recovery were demonstrated.
|
||||
|
||||
## Button design and acceptance (2026-10-28)
|
||||
|
||||
- [x] Put the action in a separate database maintenance panel with the supported scope beside it.
|
||||
- [x] Confirmation displays the exact library, eligible count, Ready count and missing-project count, and explains that verification saves projects.
|
||||
- [x] Show item progress and separate checking, backup and native repair phases; allow stopping after the current item.
|
||||
- [x] Report verified items, native repairs, restored backups requiring review, unconfirmed repairs and unexecuted items separately. Save a JSON maintenance report with original error and repair evidence.
|
||||
- [x] Detect native restoration from an existing backup without treating it as an automatic repair success or retrying verification. Close only after restoration/diagnostics complete, the native main window is idle and the database address has been released.
|
||||
- [x] Exercise the actual Qt button on a fresh local library copy using the production reader and verifier, capture screenshots and check source fingerprints afterwards.
|
||||
- [x] Repeat the real header-corruption/native-repair probe with a 180-second native deadline and verify the restoration-review result and normal process exit.
|
||||
- [x] Run the full regression suite and record the supported scope and practical limits in a release validation note.
|
||||
|
||||
Results: `docs/releases/2026-10-28-icdb-maintenance-button-validation.md`. Actual button run PASS/READY raw=1 on IP101GR; all 21,029 source file hashes unchanged. Real header corruption led to native restoration of an older backup and RESTORED_REVIEW_REQUIRED, without automatic retry. Full regression: 455 passed. Final Windows EXE smoke: clean exit 0.
|
||||
@@ -13,11 +13,12 @@ a = Analysis(
|
||||
if verify_probe_path.is_file() else []
|
||||
),
|
||||
datas=[(str(project_root / 'src' / 'cbb_refresh_verify' / 'save_verified_block.vbs'), 'cbb_refresh_verify'),
|
||||
(str(project_root / 'src' / 'cbb_refresh_verify' / 'close_failed_block.vbs'), 'cbb_refresh_verify'),
|
||||
(str(assets_root / 'cbb_refresh_verify.ico'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'cbb_refresh_verify.png'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'cbb_refresh_verify.svg'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'dialog_rules.v1.json'), 'cbb_refresh_verify/assets')],
|
||||
hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'],
|
||||
hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'],
|
||||
excludes=['PySide6.QtDesigner','PySide6.QtMultimedia','PySide6.QtMultimediaWidgets','PySide6.QtOpenGL','PySide6.QtOpenGLWidgets','PySide6.QtPdf','PySide6.QtPdfWidgets','PySide6.QtQml','PySide6.QtQuick','PySide6.QtQuickWidgets','PySide6.QtWebChannel','PySide6.QtWebEngineCore','PySide6.QtWebEngineWidgets'],
|
||||
)
|
||||
excluded_qt_binaries = {'icudt78.dll','icuuc.dll','qpdf.dll','qt6pdf.dll','qt6qml.dll','qt6qmlmeta.dll','qt6qmlmodels.dll','qt6qmlworkerscript.dll','qt6quick.dll','qt6quickwidgets.dll'}
|
||||
|
||||
@@ -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
@@ -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.3b1"
|
||||
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 @@
|
||||
__version__ = "0.1.0"
|
||||
__version__ = "0.2.3b1"
|
||||
@@ -1,126 +1,140 @@
|
||||
from __future__ import annotations
|
||||
import argparse, os, sys
|
||||
from pathlib import Path
|
||||
from typing import Sequence
|
||||
from .runtime_logging import configure
|
||||
|
||||
|
||||
def configure_qt_application(app):
|
||||
"""Apply a predictable light palette across machines and Qt themes."""
|
||||
from PySide6.QtGui import QColor, QPalette
|
||||
|
||||
app.setStyle('Fusion')
|
||||
palette = app.palette()
|
||||
colors = {
|
||||
QPalette.ColorRole.Window: '#f5f6f8',
|
||||
QPalette.ColorRole.WindowText: '#202124',
|
||||
QPalette.ColorRole.Base: '#ffffff',
|
||||
QPalette.ColorRole.AlternateBase: '#eef1f5',
|
||||
QPalette.ColorRole.Text: '#202124',
|
||||
QPalette.ColorRole.Button: '#eef1f5',
|
||||
QPalette.ColorRole.ButtonText: '#202124',
|
||||
QPalette.ColorRole.Highlight: '#2f6fed',
|
||||
QPalette.ColorRole.HighlightedText: '#ffffff',
|
||||
QPalette.ColorRole.ToolTipBase: '#ffffff',
|
||||
QPalette.ColorRole.ToolTipText: '#202124',
|
||||
QPalette.ColorRole.PlaceholderText: '#6b7280',
|
||||
}
|
||||
for role, color in colors.items():
|
||||
palette.setColor(role, QColor(color))
|
||||
app.setPalette(palette)
|
||||
app.setStyleSheet(
|
||||
'QWidget { color: #202124; }'
|
||||
'QTableWidget, QLineEdit, QTextEdit, QSpinBox { '
|
||||
'background: #ffffff; color: #202124; selection-color: #202124; }'
|
||||
'QHeaderView::section { background: #e9edf2; color: #202124; }'
|
||||
'QPushButton, QToolButton { color: #202124; }'
|
||||
)
|
||||
return app
|
||||
|
||||
def resolve_data_root(cli_value: Path | None) -> Path:
|
||||
if cli_value is not None: return cli_value.expanduser().resolve()
|
||||
if getattr(sys, "frozen", False): return Path(sys.executable).resolve().parent / "data"
|
||||
local = os.environ.get("LOCALAPPDATA")
|
||||
return (Path(local) / "CbbRefreshVerify" if local else Path.home() / "AppData" / "Local" / "CbbRefreshVerify").resolve()
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="CbbRefreshVerify")
|
||||
parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--batch-lmc', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--block', action='append', default=[], help=argparse.SUPPRESS)
|
||||
parser.add_argument('--libspec', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dms-login-config', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS); parser.add_argument("--data-root", type=Path, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args(list(argv) if argv is not None else None)
|
||||
if args.collect_failure_logs:
|
||||
from .verify_diagnostics import failure_logs_main
|
||||
return failure_logs_main(args.collect_failure_logs)
|
||||
if args.dialog_agent:
|
||||
from .dialog_agent import dialog_agent_main
|
||||
return dialog_agent_main(args.dialog_agent)
|
||||
if args.verify_worker:
|
||||
from .mentor_adapter import worker_main
|
||||
import contextlib
|
||||
with args.verify_worker.with_name('worker.log').open('a', encoding='utf-8') as log:
|
||||
with contextlib.redirect_stdout(log), contextlib.redirect_stderr(log):
|
||||
return worker_main(args.verify_worker)
|
||||
context = configure(resolve_data_root(args.data_root)); logger = context.logger; logger.info("application_start")
|
||||
try:
|
||||
if args.batch_lmc:
|
||||
from .verify_runner import (VerifyRequest, native_lm_verify_enabled,
|
||||
run_batch, write_batch_report, failure_report_path)
|
||||
from .library_reader import read_library_fast
|
||||
from .batch_status import authoritative_status_map
|
||||
from .network_path import LibraryPathLease, select_library_file
|
||||
with LibraryPathLease.acquire(args.batch_lmc) as lease:
|
||||
mentor_path = select_library_file(lease.mentor_path)
|
||||
logger.info(
|
||||
'library_path_lease display=%s mentor=%s drive=%s remote=%s created=%s',
|
||||
lease.display_path, mentor_path, lease.drive or '-',
|
||||
lease.remote_root or '-', lease.created)
|
||||
names = list(args.block)
|
||||
if not names:
|
||||
library = read_library_fast(
|
||||
mentor_path,
|
||||
status_reader=lambda path, names: authoritative_status_map(
|
||||
names, path, context.root/'status-scan.jsonl'),
|
||||
)
|
||||
if library.error: raise RuntimeError(library.error)
|
||||
names = [record.name for record in library.records if record.status != 'READY']
|
||||
if not names: raise RuntimeError('中心库未读取到需要校验的 CBB')
|
||||
requests = [VerifyRequest(mentor_path, name, context.root/'evidence',
|
||||
libspec=args.libspec, dms_login_config=args.dms_login_config,
|
||||
mentor_bin=args.mentor_bin, logging_context=context,
|
||||
mapped_drive=lease.drive,
|
||||
mapped_remote_root=lease.remote_root,
|
||||
native_lm_verify=native_lm_verify_enabled())
|
||||
for name in names]
|
||||
results = run_batch(requests)
|
||||
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))
|
||||
code = 0 if all(result.status == 'PASS' for result in results) else 2
|
||||
logger.info('application_clean_shutdown exit_code=%s', code)
|
||||
return code
|
||||
from .ui.main_window import MainWindow, application_icon_path
|
||||
if args.smoke_test:
|
||||
os.environ.setdefault('QT_QPA_PLATFORM', 'offscreen')
|
||||
from PySide6.QtWidgets import QApplication
|
||||
app = QApplication.instance() or QApplication([sys.executable])
|
||||
configure_qt_application(app)
|
||||
window = MainWindow(logging_context=context); window.show()
|
||||
icon_path = application_icon_path()
|
||||
if icon_path.is_file():
|
||||
from PySide6.QtGui import QIcon
|
||||
app.setWindowIcon(QIcon(str(icon_path)))
|
||||
if args.smoke_test:
|
||||
from PySide6.QtCore import QTimer
|
||||
logger.info('gui_smoke_window_created title=%s', window.windowTitle())
|
||||
QTimer.singleShot(250, app.quit)
|
||||
code = int(app.exec()); logger.info("application_clean_shutdown exit_code=%s", code); return code
|
||||
except Exception: logger.exception("application_startup_failure"); return 1
|
||||
finally:
|
||||
for handler in logger.handlers: handler.flush()
|
||||
from __future__ import annotations
|
||||
import argparse, os, sys
|
||||
from pathlib import Path
|
||||
from typing import Sequence
|
||||
from .runtime_logging import configure
|
||||
|
||||
|
||||
def configure_qt_application(app):
|
||||
"""Apply a predictable light palette across machines and Qt themes."""
|
||||
from PySide6.QtGui import QColor, QPalette
|
||||
|
||||
app.setStyle('Fusion')
|
||||
palette = app.palette()
|
||||
colors = {
|
||||
QPalette.ColorRole.Window: '#f5f6f8',
|
||||
QPalette.ColorRole.WindowText: '#202124',
|
||||
QPalette.ColorRole.Base: '#ffffff',
|
||||
QPalette.ColorRole.AlternateBase: '#eef1f5',
|
||||
QPalette.ColorRole.Text: '#202124',
|
||||
QPalette.ColorRole.Button: '#eef1f5',
|
||||
QPalette.ColorRole.ButtonText: '#202124',
|
||||
QPalette.ColorRole.Highlight: '#2f6fed',
|
||||
QPalette.ColorRole.HighlightedText: '#ffffff',
|
||||
QPalette.ColorRole.ToolTipBase: '#ffffff',
|
||||
QPalette.ColorRole.ToolTipText: '#202124',
|
||||
QPalette.ColorRole.PlaceholderText: '#6b7280',
|
||||
}
|
||||
for role, color in colors.items():
|
||||
palette.setColor(role, QColor(color))
|
||||
app.setPalette(palette)
|
||||
app.setStyleSheet(
|
||||
'QWidget { color: #202124; }'
|
||||
'QTableWidget, QLineEdit, QTextEdit, QSpinBox { '
|
||||
'background: #ffffff; color: #202124; selection-color: #202124; }'
|
||||
'QHeaderView::section { background: #e9edf2; color: #202124; }'
|
||||
'QPushButton, QToolButton { color: #202124; }'
|
||||
)
|
||||
return app
|
||||
|
||||
def resolve_data_root(cli_value: Path | None) -> Path:
|
||||
if cli_value is not None: return cli_value.expanduser().resolve()
|
||||
if getattr(sys, "frozen", False): return Path(sys.executable).resolve().parent / "data"
|
||||
local = os.environ.get("LOCALAPPDATA")
|
||||
return (Path(local) / "CbbRefreshVerify" if local else Path.home() / "AppData" / "Local" / "CbbRefreshVerify").resolve()
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="CbbRefreshVerify")
|
||||
parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--repair-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--owner-probe', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--batch-lmc', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--block', action='append', default=[], help=argparse.SUPPRESS)
|
||||
parser.add_argument('--libspec', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dms-login-config', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS); parser.add_argument("--data-root", type=Path, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args(list(argv) if argv is not None else None)
|
||||
if args.owner_probe:
|
||||
from .owner_probe import owner_probe_main
|
||||
return owner_probe_main(args.owner_probe)
|
||||
if args.repair_worker:
|
||||
from .native_repair_ui import repair_worker_main
|
||||
return repair_worker_main(args.repair_worker)
|
||||
if args.collect_failure_logs:
|
||||
from .verify_diagnostics import failure_logs_main
|
||||
return failure_logs_main(args.collect_failure_logs)
|
||||
if args.dialog_agent:
|
||||
from .dialog_agent import dialog_agent_main
|
||||
return dialog_agent_main(args.dialog_agent)
|
||||
if args.verify_worker:
|
||||
from .mentor_adapter import worker_main
|
||||
import contextlib
|
||||
with args.verify_worker.with_name('worker.log').open('a', encoding='utf-8') as log:
|
||||
with contextlib.redirect_stdout(log), contextlib.redirect_stderr(log):
|
||||
return worker_main(args.verify_worker)
|
||||
context = configure(resolve_data_root(args.data_root)); logger = context.logger; logger.info("application_start")
|
||||
try:
|
||||
if args.batch_lmc:
|
||||
from .verify_runner import (VerifyRequest, native_lm_verify_enabled,
|
||||
run_batch, write_batch_report, failure_report_path)
|
||||
from .library_reader import read_library_fast
|
||||
from .batch_status import authoritative_status_map
|
||||
from .network_path import LibraryPathLease, select_library_file
|
||||
with LibraryPathLease.acquire(args.batch_lmc) as lease:
|
||||
mentor_path = select_library_file(lease.mentor_path)
|
||||
logger.info(
|
||||
'library_path_lease display=%s mentor=%s drive=%s remote=%s created=%s',
|
||||
lease.display_path, mentor_path, lease.drive or '-',
|
||||
lease.remote_root or '-', lease.created)
|
||||
names = list(args.block)
|
||||
if not names:
|
||||
library = read_library_fast(
|
||||
mentor_path,
|
||||
status_reader=lambda path, names: authoritative_status_map(
|
||||
names, path, context.root/'status-scan.jsonl'),
|
||||
)
|
||||
if library.error: raise RuntimeError(library.error)
|
||||
names = [record.name for record in library.records if record.status != 'READY']
|
||||
if not names: raise RuntimeError('中心库未读取到需要校验的 CBB')
|
||||
requests = [VerifyRequest(mentor_path, name, context.root/'evidence',
|
||||
libspec=args.libspec, dms_login_config=args.dms_login_config,
|
||||
mentor_bin=args.mentor_bin, logging_context=context,
|
||||
mapped_drive=lease.drive,
|
||||
mapped_remote_root=lease.remote_root,
|
||||
native_lm_verify=native_lm_verify_enabled())
|
||||
for name in names]
|
||||
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))
|
||||
code = 0 if all(result.status == 'PASS' for result in results) else 2
|
||||
logger.info('application_clean_shutdown exit_code=%s', code)
|
||||
return code
|
||||
from .ui.main_window import MainWindow, application_icon_path
|
||||
if args.smoke_test:
|
||||
os.environ.setdefault('QT_QPA_PLATFORM', 'offscreen')
|
||||
from PySide6.QtWidgets import QApplication
|
||||
app = QApplication.instance() or QApplication([sys.executable])
|
||||
configure_qt_application(app)
|
||||
window = MainWindow(logging_context=context); window.show()
|
||||
icon_path = application_icon_path()
|
||||
if icon_path.is_file():
|
||||
from PySide6.QtGui import QIcon
|
||||
app.setWindowIcon(QIcon(str(icon_path)))
|
||||
if args.smoke_test:
|
||||
from PySide6.QtCore import QTimer
|
||||
logger.info('gui_smoke_window_created title=%s', window.windowTitle())
|
||||
QTimer.singleShot(250, app.quit)
|
||||
code = int(app.exec()); logger.info("application_clean_shutdown exit_code=%s", code); return code
|
||||
except Exception: logger.exception("application_startup_failure"); return 1
|
||||
finally:
|
||||
for handler in logger.handlers: handler.flush()
|
||||
@@ -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",
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
"""Restore a complete same-CBB Work backup, then reopen through native Verify.
|
||||
|
||||
This restores project data for an existing library entry. It is not a native
|
||||
cross-library import and does not delete library records or shared symbols.
|
||||
"""
|
||||
from contextlib import ExitStack
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from dataclasses import asdict, replace
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import stat
|
||||
from uuid import uuid4
|
||||
|
||||
from .verify_runner import VerifyResult, preflight
|
||||
|
||||
|
||||
def _hash(path):
|
||||
digest = hashlib.sha256()
|
||||
with path.open('rb') as stream:
|
||||
for data in iter(lambda: stream.read(1024 * 1024), b''):
|
||||
digest.update(data)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _scan(root):
|
||||
if root.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError(f'不支持通过目录链接恢复:{root}')
|
||||
entries = {}
|
||||
folders = [root]
|
||||
while folders:
|
||||
with os.scandir(folders.pop()) as scanned:
|
||||
for entry in scanned:
|
||||
metadata = entry.stat(follow_symlinks=False)
|
||||
if metadata.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError(f'目录含文件或目录链接,停止恢复:{entry.path}')
|
||||
path = Path(entry.path)
|
||||
entries[path] = metadata
|
||||
if stat.S_ISDIR(metadata.st_mode):
|
||||
folders.append(path)
|
||||
return dict(sorted(entries.items()))
|
||||
|
||||
|
||||
def _paths(root):
|
||||
return list(_scan(root))
|
||||
|
||||
|
||||
def _manifest(root, *, exclude=None):
|
||||
entries = _scan(root)
|
||||
paths = [path for path in entries
|
||||
if exclude is None or not path.is_relative_to(exclude)]
|
||||
def entry(path):
|
||||
return str(path.relative_to(root)), _hash(path) if stat.S_ISREG(entries[path].st_mode) else None
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
return dict(pool.map(entry, paths))
|
||||
|
||||
|
||||
def _copy_verified(source, destination, *, progress=None):
|
||||
"""Copy every file, refusing links and changed files; lock native databases."""
|
||||
import win32con, win32file
|
||||
entries = _scan(source)
|
||||
paths = list(entries)
|
||||
destination.mkdir(parents=True, exist_ok=False)
|
||||
record = {}
|
||||
with ExitStack() as stack:
|
||||
locks = {}
|
||||
for path in paths:
|
||||
if stat.S_ISREG(entries[path].st_mode) and (path.name.casefold() == 'icdb.dat' or path.suffix.casefold() == '.lmc'):
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
locks[path] = handle
|
||||
files = []
|
||||
for path in paths:
|
||||
relative = str(path.relative_to(source))
|
||||
copied = destination / relative
|
||||
if stat.S_ISDIR(entries[path].st_mode):
|
||||
copied.mkdir(parents=True, exist_ok=True)
|
||||
record[relative] = None
|
||||
continue
|
||||
files.append(path)
|
||||
def copy_file(path):
|
||||
relative = str(path.relative_to(source))
|
||||
copied = destination / relative
|
||||
before = entries[path]
|
||||
copied.parent.mkdir(parents=True, exist_ok=True)
|
||||
if path in locks:
|
||||
with copied.open('wb') as stream:
|
||||
while True:
|
||||
_, data = win32file.ReadFile(locks[path], 1024 * 1024)
|
||||
if not data:
|
||||
break
|
||||
stream.write(data)
|
||||
shutil.copystat(path, copied)
|
||||
digest = _hash(copied)
|
||||
else:
|
||||
digest = _hash(path)
|
||||
shutil.copy2(path, copied)
|
||||
if _hash(copied) != digest:
|
||||
raise OSError(f'副本校验失败:{path}')
|
||||
after = path.stat()
|
||||
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
|
||||
raise OSError(f'复制期间文件变化:{path}')
|
||||
return relative, digest
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
for count, (relative, digest) in enumerate(pool.map(copy_file, files), 1):
|
||||
record[relative] = digest
|
||||
if progress and (count % 256 == 0 or count == len(files)):
|
||||
progress(count, len(files))
|
||||
if _manifest(source) != record:
|
||||
raise OSError(f'复制期间目录内容变化:{source}')
|
||||
return record
|
||||
|
||||
|
||||
def _idle_check(library, project, pcb):
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
if pcb_processes() or any(_process_details(name) for name in (
|
||||
'LibraryManager.exe', 'viewdraw.exe')):
|
||||
raise ValueError('请先正常关闭 PCB、Library Manager 和原理图编辑器,再恢复备份。')
|
||||
addresses = unreleased_addresses(library, project, pcb)
|
||||
if addresses:
|
||||
raise ValueError(f'数据库会话尚未释放,停止恢复:{addresses}')
|
||||
|
||||
|
||||
def _project_text(path):
|
||||
data = path.read_bytes()
|
||||
if data.startswith((b'\xff\xfe', b'\xfe\xff')):
|
||||
return data.decode('utf-16')
|
||||
try:
|
||||
return data.decode('utf-8-sig')
|
||||
except UnicodeDecodeError:
|
||||
return data.decode('mbcs')
|
||||
|
||||
|
||||
def _source_check(source, name, original):
|
||||
if not source.is_file() or source.suffix.casefold() != '.prj':
|
||||
raise ValueError('请选择完整健康备份中的工程 .prj。')
|
||||
_paths(source.parent)
|
||||
text = _project_text(source)
|
||||
roots = re.findall(r'^KEY RootBlock "([^"]+)"', text, re.M)
|
||||
if (source.name.casefold() != original.name.casefold() or not roots
|
||||
or any(root.casefold() != name.casefold() for root in roots)):
|
||||
raise ValueError('当前仅支持同名 CBB 的完整工程备份,工程名和顶层块必须与目标一致。')
|
||||
refs = re.findall(r'^KEY PCBDesignPath "([^"]+)"', text, re.M)
|
||||
if len(refs) != 1:
|
||||
raise ValueError('备份工程必须包含唯一的 PCBDesignPath。')
|
||||
def require_relative(value):
|
||||
if Path(value).drive or Path(value).root:
|
||||
raise ValueError('备份工程、PCB 和数据库必须使用工程内的相对路径,不能指向原备份位置。')
|
||||
require_relative(refs[0])
|
||||
pcb = (source.parent / refs[0]).resolve()
|
||||
if not pcb.is_relative_to(source.parent.resolve()):
|
||||
raise ValueError('备份 PCB 引用越出工程目录。')
|
||||
if not all(path.is_file() for path in (pcb, pcb.parent/'Layout/LayoutDB.lyt',
|
||||
pcb.parent/'pcb.prj', source.parent/'database/icdb.dat')):
|
||||
raise ValueError('备份不完整:需要原理图数据库、PCB、布局数据库和 PCB 工程文件。')
|
||||
project_refs = re.findall(r'^KEY ProjectFile "([^"]+)"', _project_text(pcb), re.M)
|
||||
for value in project_refs:
|
||||
require_relative(value)
|
||||
if len(project_refs) != 1 or (pcb.parent/project_refs[0]).resolve() != source.resolve():
|
||||
raise ValueError('备份 PCB 必须引用所选工程,不能引用其他位置的工程。')
|
||||
for project in (source, pcb.parent/'pcb.prj'):
|
||||
data = _project_text(project)
|
||||
for value in re.findall(r'^KEY iCDBDir "([^"]+)"', data, re.M):
|
||||
require_relative(value)
|
||||
if not (project.parent/value).resolve().is_relative_to(source.parent.resolve()):
|
||||
raise ValueError('备份数据库引用越出工程目录。')
|
||||
return pcb
|
||||
|
||||
|
||||
def _align_library(project, pcb, library):
|
||||
from .mentor_adapter import ensure_project_central_library, ensure_pcb_central_library
|
||||
for path in (project, pcb.parent/'pcb.prj'):
|
||||
if not re.findall(r'^KEY CentralLibrary "([^"]*)"', _project_text(path), re.M):
|
||||
raise ValueError('备份工程缺少 CentralLibrary 引用。')
|
||||
def already_references(path):
|
||||
return all((path.parent/value).resolve() == Path(library).resolve()
|
||||
for value in re.findall(r'^KEY CentralLibrary "([^"]*)"', _project_text(path), re.M))
|
||||
if not already_references(project):
|
||||
ensure_project_central_library(project, library)
|
||||
if not already_references(pcb.parent/'pcb.prj'):
|
||||
ensure_pcb_central_library(pcb, library)
|
||||
|
||||
|
||||
def recover_from_backup(request, source, *, progress=None, verifier=None, idle_check=None):
|
||||
"""Validate in copies before changing Work; preserve all recovery evidence."""
|
||||
import pythoncom, win32con, win32event
|
||||
from .network_path import absolute_preserving_drive
|
||||
from .verify_runner import run_verify, read_library_flag_if_available
|
||||
verifier = verifier or run_verify
|
||||
idle_check = idle_check or _idle_check
|
||||
source = Path(source).resolve()
|
||||
library = absolute_preserving_drive(request.lmc_file)
|
||||
library_root = library.parent.resolve()
|
||||
root = Path(request.evidence_dir).resolve() / ('backup-restore-' + uuid4().hex[:12])
|
||||
# Packaged desktop apps can redirect AppData to a very deep MSIX path.
|
||||
# Keep native EE7.9 files in a genuinely short, user-owned directory.
|
||||
validation_base = Path.home() / '.cbb-restore'
|
||||
validation_dir = validation_base.resolve() / root.name.removeprefix('backup-restore-')
|
||||
if root.is_relative_to(library_root) or library_root.is_relative_to(root):
|
||||
return VerifyResult('BLOCKED', stderr='恢复记录目录与目标中心库不能相互包含。')
|
||||
if root.is_relative_to(source.parent):
|
||||
return VerifyResult('BLOCKED', stderr='恢复记录目录不能位于备份工程内。')
|
||||
if validation_dir.is_relative_to(library_root) or validation_dir.is_relative_to(source.parent):
|
||||
return VerifyResult('BLOCKED', stderr='本机验证目录不能位于目标库或备份工程内。')
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
installed = False
|
||||
record = {'operation': 'same_cbb_complete_project_restore', 'source': str(source),
|
||||
'library': str(library), 'block': request.block_name, 'installed': False,
|
||||
'validation_library': str(validation_dir/library.name)}
|
||||
def emit(stage, message):
|
||||
if progress:
|
||||
progress({'stage': stage, 'block': request.block_name, 'message': message})
|
||||
def save(result):
|
||||
record.update({'installed': installed, 'result': asdict(result)})
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
report = root/'recovery-result.json'
|
||||
report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
return replace(result, evidence_path=report)
|
||||
pythoncom.CoInitialize()
|
||||
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)
|
||||
if source.is_relative_to(library_root):
|
||||
raise ValueError('请选择目标中心库之外的独立备份工程。')
|
||||
project, pcb = preflight(replace(request, lmc_file=library))
|
||||
source_pcb = _source_check(source, request.block_name, project)
|
||||
idle_check(library, project, pcb)
|
||||
idle_check(library, source, source_pcb)
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
validation_base.mkdir(parents=True, exist_ok=True)
|
||||
library_size = sum(info.st_size for info in _scan(library_root).values() if stat.S_ISREG(info.st_mode))
|
||||
source_size = sum(info.st_size for info in _scan(source.parent).values() if stat.S_ISREG(info.st_mode))
|
||||
same_drive = root.anchor.casefold() == validation_dir.anchor.casefold()
|
||||
required = library_size * (2 if same_drive else 1) + source_size * 3
|
||||
if (shutil.disk_usage(root).free < required
|
||||
or shutil.disk_usage(validation_base).free < library_size + source_size * 2):
|
||||
raise ValueError('磁盘空间不足:需要容纳中心库回退副本和验证副本。')
|
||||
(root/'recovery-request.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
emit('backup', '正在保留完整中心库副本…')
|
||||
original_manifest = _copy_verified(library_root, root/'library-before', progress=lambda n, total:
|
||||
emit('backup', f'正在保留中心库副本:{n} / {total} 个文件…'))
|
||||
(root/'library-before-manifest.json').write_text(json.dumps(original_manifest, indent=2), encoding='utf-8')
|
||||
emit('prepare_source', '正在准备备份验证副本…')
|
||||
source_manifest = _copy_verified(source.parent, root/'source-before')
|
||||
_copy_verified(root/'library-before', validation_dir, progress=lambda n, total:
|
||||
emit('prepare_source', f'正在准备验证库:{n} / {total} 个文件…'))
|
||||
staged_library = validation_dir/library.name
|
||||
staged_work = staged_library.parent/'ReusableBlocksLP'/request.block_name/'Work'
|
||||
# Both paths are newly created private copies; originals are retained.
|
||||
resolved_work = staged_work.resolve()
|
||||
if not resolved_work.is_relative_to(validation_dir.resolve()):
|
||||
raise ValueError('验证副本工程路径越界。')
|
||||
shutil.rmtree(resolved_work)
|
||||
_copy_verified(root/'source-before', staged_work)
|
||||
staged_project = staged_work/source.name
|
||||
staged_pcb = staged_work/source_pcb.relative_to(source.parent)
|
||||
_align_library(staged_project, staged_pcb, staged_library)
|
||||
_source_check(staged_project, request.block_name, project)
|
||||
validation = replace(request, lmc_file=staged_library, evidence_dir=root/'source-verify',
|
||||
auto_repair=False, libspec='', dms_login_config='', mapped_drive='', mapped_remote_root='',
|
||||
native_lm_verify=False, interactive_input=False)
|
||||
preflight(validation)
|
||||
emit('validate_source', '正在副本中打开备份工程并执行原生校验…')
|
||||
source_result = verifier(validation, progress=progress, flag_reader=read_library_flag_if_available)
|
||||
record['source_validation'] = asdict(source_result)
|
||||
if (source_result.status != 'PASS' or source_result.library_flag != 'GREEN'
|
||||
or not source_result.safe_to_continue):
|
||||
return save(replace(source_result, status='FAIL', stderr=
|
||||
'备份校验未通过,目标未更改。\n' + (source_result.stderr or source_result.library_flag_detail)))
|
||||
idle_check(library, project, pcb)
|
||||
idle_check(staged_library, staged_project, staged_pcb)
|
||||
if _manifest(source.parent) != source_manifest:
|
||||
raise ValueError('备份来源在验证期间发生变化,目标未更改。')
|
||||
token = root.name.removeprefix('backup-restore-')
|
||||
work = project.parent.resolve()
|
||||
incoming = work.with_name('Work.restore-' + token)
|
||||
retained = work.with_name('Work.before-restore-' + token)
|
||||
_copy_verified(staged_work, incoming)
|
||||
_align_library(incoming/source.name, incoming/source_pcb.relative_to(source.parent), library)
|
||||
if _manifest(library_root, exclude=incoming.resolve()) != original_manifest:
|
||||
raise ValueError('目标中心库在验证期间发生变化,停止恢复。')
|
||||
emit('install', '备份已通过验证,正在保留原工程并恢复…')
|
||||
work.rename(retained)
|
||||
try:
|
||||
incoming.rename(work)
|
||||
except Exception:
|
||||
retained.rename(work)
|
||||
raise
|
||||
installed = True
|
||||
record['retained_work'] = str(retained)
|
||||
emit('verify_target', '正在重新打开恢复后的目标工程并校验…')
|
||||
final = verifier(replace(request, lmc_file=library, evidence_dir=root/'target-verify',
|
||||
auto_repair=False, native_lm_verify=False, interactive_input=False),
|
||||
progress=progress, flag_reader=read_library_flag_if_available)
|
||||
if final.status == 'PASS' and (final.library_flag != 'GREEN' or not final.safe_to_continue):
|
||||
final = replace(final, status='FAIL', stderr='目标工程恢复后的 Ready 状态未确认。')
|
||||
if final.status != 'PASS':
|
||||
final = replace(final, stderr=(final.stderr + '\n原工程和完整库副本已保留,请查看恢复记录。').strip())
|
||||
return save(final)
|
||||
except Exception as error:
|
||||
return save(VerifyResult('FAIL' if installed else 'BLOCKED', stderr=str(error),
|
||||
safe_to_continue=False))
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
pythoncom.CoUninitialize()
|
||||
@@ -0,0 +1,29 @@
|
||||
Option Explicit
|
||||
Dim app, doc, pi, licensing, token, closed, remaining
|
||||
Set app = GetObject(, "MGCPCB.Application")
|
||||
Set doc = app.ActiveDocument
|
||||
If doc Is Nothing Then WScript.Quit 3
|
||||
Set pi = doc.ProjectIntegration
|
||||
If LCase(pi.ProjectFile) <> LCase(WScript.Arguments(0)) Then WScript.Quit 2
|
||||
If LCase(doc.Name) <> LCase(WScript.Arguments(1)) Then WScript.Quit 2
|
||||
Set pi = Nothing
|
||||
WScript.Echo "FAILURE_IDENTITY_OK"
|
||||
Set licensing = CreateObject("MGCPCBAutomationLicensing.Application")
|
||||
token = licensing.GetToken(doc.Validate(0))
|
||||
doc.Validate token
|
||||
WScript.Echo "FAILURE_CLOSE_START"
|
||||
closed = doc.Close(False)
|
||||
If Not closed Then WScript.Quit 4
|
||||
WScript.Echo "FAILURE_CLOSE_OK"
|
||||
Set doc = Nothing
|
||||
Set licensing = Nothing
|
||||
Set remaining = app.ActiveDocument
|
||||
If Not remaining Is Nothing Then
|
||||
Set remaining = Nothing
|
||||
WScript.Echo "FAILURE_APP_RETAINED"
|
||||
WScript.Quit 5
|
||||
End If
|
||||
Set remaining = Nothing
|
||||
app.Quit
|
||||
WScript.Echo "FAILURE_QUIT_OK"
|
||||
Set app = Nothing
|
||||
@@ -0,0 +1,46 @@
|
||||
"""Run the native importer without aborting database owners on timeout."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
|
||||
|
||||
def run_content_check(command, timeout_seconds, cwd, *, evidence_root=None):
|
||||
root = (Path(evidence_root) if evidence_root is not None else
|
||||
Path(tempfile.mkdtemp(prefix='cbb-content-check-')))
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
stdout_path, stderr_path = root/'content-check.stdout.log', root/'content-check.stderr.log'
|
||||
record_path = root/'content-check-process.json'
|
||||
with stdout_path.open('wb') as stdout, stderr_path.open('wb') as stderr:
|
||||
process = subprocess.Popen(command, cwd=str(cwd), stdout=stdout, stderr=stderr,
|
||||
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
record = {'pid': process.pid, 'command': list(command), 'cwd': str(cwd),
|
||||
'stdout': str(stdout_path), 'stderr': str(stderr_path)}
|
||||
def save_record(**fields):
|
||||
try:
|
||||
record_path.write_text(json.dumps({**record, **fields}, ensure_ascii=False, indent=2),
|
||||
encoding='utf-8')
|
||||
except OSError:
|
||||
pass
|
||||
save_record(status='RUNNING')
|
||||
try:
|
||||
process.wait(timeout=max(.1, float(timeout_seconds)))
|
||||
except subprocess.TimeoutExpired:
|
||||
# Never kill this client or its descendants. The importer can own the
|
||||
# shared symbol database even though contents checking sounds read-only.
|
||||
# Files avoid pipe backpressure/BrokenPipe if the supervisor exits.
|
||||
save_record(status='RETAINED', safe_to_continue=False)
|
||||
def observe_exit():
|
||||
process.wait()
|
||||
save_record(status='EXITED', returncode=process.returncode,
|
||||
note='Importer exit alone does not prove native database release.')
|
||||
threading.Thread(target=observe_exit, name='retained-content-check', daemon=True).start()
|
||||
return (None, stdout_path.read_bytes().decode('utf-8', errors='replace'),
|
||||
stderr_path.read_bytes().decode('utf-8', errors='replace') +
|
||||
f'\nIMPORTER_RELEASE_UNCONFIRMED: pid={process.pid}; '
|
||||
f'内容检查超时,保留原生操作并停止批次;进程记录:{record_path}', True)
|
||||
save_record(status='EXITED', returncode=process.returncode)
|
||||
return (process.returncode, stdout_path.read_bytes().decode('utf-8', errors='replace'),
|
||||
stderr_path.read_bytes().decode('utf-8', errors='replace'), False)
|
||||
@@ -1,5 +1,8 @@
|
||||
"""Launch automation on a private Windows desktop, never the input desktop."""
|
||||
import subprocess
|
||||
import os
|
||||
from pathlib import Path
|
||||
import threading
|
||||
import win32con
|
||||
import win32event
|
||||
import win32job
|
||||
@@ -7,6 +10,52 @@ 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, *, cancel_path=None):
|
||||
"""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)
|
||||
if cancel_path is not None:
|
||||
# Let native calls finish before the worker closes its exact
|
||||
# document. Killing a pending automation client can abandon it.
|
||||
Path(cancel_path).touch()
|
||||
return
|
||||
with _automation_clients_lock:
|
||||
for helper in _automation_clients:
|
||||
try:
|
||||
if helper.poll() is None:
|
||||
helper.terminate()
|
||||
except OSError:
|
||||
pass
|
||||
finally:
|
||||
parent.Close()
|
||||
if cancel_path is None:
|
||||
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.
|
||||
|
||||
@@ -23,30 +72,52 @@ class DesktopWorker:
|
||||
def __init__(self, command, name, *, interactive_input=False):
|
||||
self.name = name
|
||||
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.
|
||||
self.job = win32job.CreateJobObject(None, '')
|
||||
self.job_bound = False
|
||||
self.desktop = None
|
||||
self.job = None
|
||||
self.handle = None
|
||||
self.companions = []
|
||||
self.companion_pids = []
|
||||
startup = win32process.STARTUPINFO()
|
||||
# subprocess.STARTUPINFO does NOT pass lpDesktop to CreateProcess.
|
||||
startup.lpDesktop = startup_desktop_name(name, interactive_input=self.interactive_input)
|
||||
self.handle, thread, self.pid, _ = win32process.CreateProcess(
|
||||
command[0], subprocess.list2cmdline(command), None, None, False,
|
||||
win32con.CREATE_NO_WINDOW, None, None, startup)
|
||||
thread.Close()
|
||||
self.job_bound = False
|
||||
# Track the session, but never use kill-on-close: PCB descendants can
|
||||
# still own a database transaction when the Python supervisor exits.
|
||||
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
|
||||
self.desktop = None if self.interactive_input else win32service.CreateDesktop(
|
||||
name, 0, win32con.GENERIC_ALL, None)
|
||||
self.job = win32job.CreateJobObject(None, '')
|
||||
startup = win32process.STARTUPINFO()
|
||||
# subprocess.STARTUPINFO does NOT pass lpDesktop to CreateProcess.
|
||||
startup.lpDesktop = startup_desktop_name(name, interactive_input=self.interactive_input)
|
||||
self.handle, thread, self.pid, _ = win32process.CreateProcess(
|
||||
command[0], subprocess.list2cmdline(command), None, None, False,
|
||||
win32con.CREATE_NO_WINDOW, None, None, startup)
|
||||
thread.Close()
|
||||
try:
|
||||
win32job.AssignProcessToJobObject(self.job, self.handle)
|
||||
self.job_bound = True
|
||||
except Exception:
|
||||
# Kill-through-job is a cleanup guarantee, not a launch requirement.
|
||||
self.job_bound = False
|
||||
except Exception:
|
||||
# Kill-through-job is a cleanup guarantee, not a launch requirement.
|
||||
self.job_bound = False
|
||||
if self.handle is not None:
|
||||
try:
|
||||
win32process.TerminateProcess(self.handle, 1)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
self.handle.Close()
|
||||
except Exception:
|
||||
pass
|
||||
if self.job is not None:
|
||||
try:
|
||||
self.job.Close()
|
||||
except Exception:
|
||||
pass
|
||||
if self.desktop is not None:
|
||||
try:
|
||||
self.desktop.CloseDesktop()
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
|
||||
def start_companion(self, command):
|
||||
startup = win32process.STARTUPINFO()
|
||||
@@ -80,48 +151,59 @@ 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
|
||||
|
||||
def bind_cancellation(self, path):
|
||||
self.cancel_path = Path(path)
|
||||
|
||||
def request_stop(self):
|
||||
if not hasattr(self, 'cancel_path'):
|
||||
# Compatibility for diagnostic workers without the Verify protocol.
|
||||
self.kill()
|
||||
return
|
||||
self.cancel_path.touch()
|
||||
|
||||
def wait(self, timeout=5):
|
||||
if win32event.WaitForSingleObject(self.handle, int(timeout*1000)) != win32con.WAIT_OBJECT_0:
|
||||
raise TimeoutError('Automation worker did not exit')
|
||||
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()
|
||||
self.request_stop()
|
||||
except Exception:
|
||||
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()
|
||||
@@ -193,6 +193,18 @@ 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)))
|
||||
from .icdb_recovery import ERROR_CODE, parse_session_loss
|
||||
if parse_session_loss(combined):
|
||||
return RuleDecision(error_code=ERROR_CODE,
|
||||
detail=' | '.join(snapshot.texts) or snapshot.title)
|
||||
# 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(
|
||||
|
||||
@@ -351,6 +351,11 @@ def classify_dialog(snapshot: DialogSnapshot) -> DialogDecision:
|
||||
joined = " ".join(snapshot.texts).casefold()
|
||||
combined = f"{title} {joined}"
|
||||
|
||||
from .icdb_recovery import ERROR_CODE, parse_session_loss
|
||||
if parse_session_loss(combined):
|
||||
return DialogDecision(error_code=ERROR_CODE,
|
||||
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 DialogDecision(action="decline_database_log",
|
||||
@@ -370,14 +375,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")
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Close only an idle document whose native identity was already established."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import time
|
||||
|
||||
|
||||
def close_failed_document(job, *, emit=lambda **event: None, timeout_seconds=30):
|
||||
from .desktop_worker import launch_save_helper
|
||||
root = Path(job['root'])
|
||||
log_path = root/'failure-close.log'
|
||||
command = [str(Path(os.environ.get('WINDIR', r'C:\Windows'))/'SysWOW64/cscript.exe'),
|
||||
'//nologo', str(Path(__file__).with_name('close_failed_block.vbs')),
|
||||
job['project'], Path(job['pcb']).name]
|
||||
with log_path.open('w', encoding='utf-8') as log:
|
||||
process = launch_save_helper(command, stdout=log, stderr=log)
|
||||
emit(stage='failure_close_started', helper_pid=process.pid)
|
||||
deadline = time.monotonic()+max(.1, float(timeout_seconds))
|
||||
while process.poll() is None and time.monotonic() < deadline:
|
||||
time.sleep(.2)
|
||||
# A slow native Close can still be flushing. Retain the helper as well as
|
||||
# PCB/iCDB; never force it to abandon the native call at this deadline.
|
||||
text = log_path.read_text('utf-8', errors='replace')
|
||||
result = {'status': ('CLOSED' if process.poll() == 0 and
|
||||
all(marker in text.splitlines() for marker in ('FAILURE_CLOSE_OK', 'FAILURE_QUIT_OK'))
|
||||
else 'RETAINED' if process.poll() is None else 'UNCONFIRMED'),
|
||||
'helper_pid': process.pid, 'returncode': process.poll(), 'log': str(log_path)}
|
||||
(root/'failure-close.json').write_text(json.dumps(result, indent=2), encoding='utf-8')
|
||||
emit(stage='failure_close_result', **result)
|
||||
return result
|
||||
@@ -0,0 +1,25 @@
|
||||
"""Reports for the explicit full-library maintenance action."""
|
||||
from dataclasses import asdict
|
||||
import json
|
||||
|
||||
|
||||
def maintenance_counts(results):
|
||||
attempts = [attempt for result in results for attempt in result.recovery_attempts]
|
||||
repaired = sum(attempt.get('status') == 'REPAIRED' for attempt in attempts)
|
||||
review = sum(attempt.get('status') == 'RESTORED_REVIEW_REQUIRED' for attempt in attempts)
|
||||
return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results),
|
||||
'skipped': sum(r.status == 'SKIPPED' for r in results),
|
||||
'repaired': repaired, 'review_required': review,
|
||||
'repair_unconfirmed': len(attempts)-repaired-review}
|
||||
|
||||
|
||||
def write_maintenance_report(requests, results, batch_report, skipped_names):
|
||||
path = batch_report.with_name(batch_report.name.replace('batch-', 'icdb-', 1)).with_suffix('.json')
|
||||
record = {'operation': 'full_library_icdb_check_and_repair',
|
||||
'library': str(requests[0].lmc_file), 'includes_ready': True,
|
||||
'shared_database_repair_supported': False,
|
||||
'preflight_skipped': sorted(skipped_names), 'counts': maintenance_counts(results),
|
||||
'results': [{'block': req.block_name, **asdict(result)}
|
||||
for req, result in zip(requests, results)]}
|
||||
path.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
return path
|
||||
@@ -0,0 +1,16 @@
|
||||
"""Known native database owners touched by a CBB verification operation."""
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def database_directories(lmc_file, project, pcb):
|
||||
return tuple(dict.fromkeys((
|
||||
Path(project).parent/'database',
|
||||
Path(pcb).parent/'database',
|
||||
Path(lmc_file).parent/'SymbolLibs/__ReusableBlocksSymb',
|
||||
)))
|
||||
|
||||
|
||||
def unreleased_addresses(lmc_file, project, pcb):
|
||||
return [str(path/'cdbsvr/sAddress.adr')
|
||||
for path in database_directories(lmc_file, project, pcb)
|
||||
if (path/'cdbsvr/sAddress.adr').exists()]
|
||||
@@ -0,0 +1,144 @@
|
||||
"""Recognize native iCDB recovery requests without changing database state."""
|
||||
import hashlib
|
||||
import ntpath
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
|
||||
ERROR_CODE = 'ICDB_REPAIR_REQUIRED'
|
||||
LOG_LIMIT = 128 * 1024
|
||||
RECOVERY_ACTION = (
|
||||
'停止批次并确认原服务器及其他客户端已正常关闭;保留完整一致副本后,'
|
||||
"使用同版本 iCDB Project Backup 的 Repair project 检查并修复正确目标。"
|
||||
'共享符号库问题需核对中心库,不能只修复单个 CBB 的 Work 工程。'
|
||||
'不要删除 sAddress.adr/锁文件;修复后重新打开、校验并确认权威 READY。'
|
||||
)
|
||||
_SESSION_LOSS = re.compile(
|
||||
r'\bthe\s+last\s+server\s+session\s+on\s+different\s+server\s+machine\s+'
|
||||
r'lost\s+the\s+connection\s+to\s+the\s+database\b', re.IGNORECASE)
|
||||
|
||||
|
||||
def parse_session_loss(text):
|
||||
"""Keep the native target verbatim; never infer a local repair target."""
|
||||
text = str(text or '')
|
||||
match = _SESSION_LOSS.search(text)
|
||||
if match is None:
|
||||
return None
|
||||
# A log may contain several sessions. Use the identity following this
|
||||
# error, never an unrelated Machine/Path header earlier in the log.
|
||||
message = text[match.start():]
|
||||
next_error = _SESSION_LOSS.search(message, 1)
|
||||
if next_error:
|
||||
message = message[:next_error.start()]
|
||||
record = {'error_code': ERROR_CODE, 'native_message': message,
|
||||
'machine': '', 'database_path': '', 'session': '', 'scope': 'unknown'}
|
||||
for native_key, key in (('machine', 'machine'), ('path', 'database_path'),
|
||||
('session', 'session')):
|
||||
field = re.search(
|
||||
rf'\b{native_key}\s*:\s*(?:\[([^\]]*)\]|([^\r\n]*?)(?=\s+\b(?:machine|path|session)\s*:|[\r\n]|$))',
|
||||
message, re.IGNORECASE)
|
||||
if field:
|
||||
record[key] = (field.group(1) if field.group(1) is not None else field.group(2)).strip()
|
||||
parts = ntpath.normpath(record['database_path']).casefold().split('\\')
|
||||
if 'symbollibs' in parts:
|
||||
record['scope'] = 'central_library'
|
||||
elif 'work' in parts and parts[-1] == 'database':
|
||||
record['scope'] = 'project'
|
||||
return record
|
||||
|
||||
|
||||
def recovery_detail(record):
|
||||
target = ('中心库共享符号数据库' if record['scope'] == 'central_library' else
|
||||
'工程数据库' if record['scope'] == 'project' else '报错中指定的数据库')
|
||||
return (
|
||||
f"{ERROR_CODE}: {target}的上次服务器会话失联。\n"
|
||||
f"{record['native_message']}\n"
|
||||
f'{RECOVERY_ACTION}'
|
||||
)
|
||||
|
||||
|
||||
def snapshot_log(path):
|
||||
"""Fingerprint a bounded tail before launch, including a missing log."""
|
||||
try:
|
||||
with Path(path).open('rb') as stream:
|
||||
stream.seek(0, 2)
|
||||
size = stream.tell()
|
||||
stream.seek(max(0, size-LOG_LIMIT))
|
||||
tail = stream.read(LOG_LIMIT)
|
||||
losses = [_session_identity(record) for record in _session_losses(decode_log(tail))]
|
||||
return {'size': size, 'tail_sha256': hashlib.sha256(tail).hexdigest(),
|
||||
'truncated': size > LOG_LIMIT,
|
||||
'session_losses': losses}
|
||||
except FileNotFoundError:
|
||||
return {'size': 0, 'tail_sha256': hashlib.sha256(b'').hexdigest()}
|
||||
except OSError as error:
|
||||
return {'error': str(error)}
|
||||
|
||||
|
||||
def _read_log_delta(path, baseline):
|
||||
"""Read new bytes, detecting truncate/rewrite as well as append.
|
||||
|
||||
Unknown baselines cannot prove freshness. At most two bounded tails are
|
||||
read, even when a native log is large or located on a network share.
|
||||
"""
|
||||
if not baseline or 'error' in baseline:
|
||||
return b'', False
|
||||
old_size = int(baseline['size'])
|
||||
try:
|
||||
with Path(path).open('rb') as stream:
|
||||
stream.seek(0, 2)
|
||||
size = stream.tell()
|
||||
offset = 0
|
||||
appended = False
|
||||
if size >= old_size:
|
||||
stream.seek(max(0, old_size-LOG_LIMIT))
|
||||
old_tail = stream.read(min(old_size, LOG_LIMIT))
|
||||
if hashlib.sha256(old_tail).hexdigest() == baseline['tail_sha256']:
|
||||
offset = old_size
|
||||
appended = True
|
||||
stream.seek(max(offset, size-LOG_LIMIT))
|
||||
return stream.read(LOG_LIMIT), appended
|
||||
except OSError:
|
||||
return b'', False
|
||||
|
||||
|
||||
def _session_losses(text):
|
||||
matches = list(_SESSION_LOSS.finditer(text))
|
||||
for index, match in enumerate(matches):
|
||||
end = matches[index+1].start() if index+1 < len(matches) else len(text)
|
||||
yield parse_session_loss(text[match.start():end])
|
||||
|
||||
|
||||
def _session_identity(record):
|
||||
return [record[key] for key in ('machine', 'database_path', 'session')]
|
||||
|
||||
|
||||
def read_log_failure(path, baseline):
|
||||
"""Return new log text and a recovery request with evidence of freshness.
|
||||
|
||||
A rewritten header does not prove that a retained old session failed
|
||||
again. In that ambiguous case keep the generic failure; a verified append
|
||||
can report even the same old session as a current native error.
|
||||
"""
|
||||
data, appended = _read_log_delta(path, baseline)
|
||||
text = decode_log(data)
|
||||
if not appended and baseline and baseline.get('truncated'):
|
||||
# A rewrite may expose an old error outside our bounded baseline.
|
||||
# Without positive append evidence its age cannot be established.
|
||||
return text, None
|
||||
historical = baseline.get('session_losses', []) if baseline else []
|
||||
recovery = next((record for record in _session_losses(text)
|
||||
if appended or _session_identity(record) not in historical), None)
|
||||
return text, recovery
|
||||
|
||||
|
||||
def decode_log(data):
|
||||
if data.startswith((b'\xff\xfe', b'\xfe\xff')):
|
||||
return data.decode('utf-16', errors='replace')
|
||||
# Appended UTF-16 bytes and bounded tails no longer contain a BOM.
|
||||
sample = data[:200]
|
||||
if sample[1::2].count(0) > len(sample)//8:
|
||||
return data.decode('utf-16-le', errors='replace')
|
||||
if sample[::2].count(0) > len(sample)//8:
|
||||
return data.decode('utf-16-be', errors='replace')
|
||||
return data.decode('utf-8', errors='replace')
|
||||
@@ -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
|
||||
@@ -25,6 +25,8 @@ import traceback
|
||||
import logging
|
||||
import shutil
|
||||
import socket
|
||||
import gc
|
||||
import tempfile
|
||||
from collections import Counter
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -99,7 +101,14 @@ def ensure_pcb_central_library(pcb_path, lmc_path, *, evidence_dir=None,
|
||||
is copied into evidence before a replacement, so the change is auditable
|
||||
and recoverable.
|
||||
"""
|
||||
project = Path(pcb_path).parent / 'pcb.prj'
|
||||
return ensure_project_central_library(Path(pcb_path).parent/'pcb.prj', lmc_path,
|
||||
evidence_dir=evidence_dir, emit=emit)
|
||||
|
||||
|
||||
def ensure_project_central_library(project, lmc_path, *, evidence_dir=None,
|
||||
emit=lambda **event: None):
|
||||
"""Align one native project file while preserving its original encoding."""
|
||||
project = Path(project)
|
||||
target = str(lmc_path)
|
||||
if not project.is_file() or not target:
|
||||
return False
|
||||
@@ -109,31 +118,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 +217,20 @@ 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'",
|
||||
'viewdraw.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='viewdraw.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 +368,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 +614,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.
|
||||
@@ -604,10 +641,13 @@ def execute_job(job, emit):
|
||||
emit(stage='backup_done', status=backup.get('status'), detail=backup.get('detail'))
|
||||
# Backup is evidence only, never a prerequisite for launching PCB. Volatile
|
||||
# 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 地址无法隔离;继续启动以保留现场。')
|
||||
from .icdb_ownership import database_directories, unreleased_addresses
|
||||
for database in database_directories(job['lmc_file'], job['project'], job['pcb']):
|
||||
icdb_address = prepare_icdb_address(database, root, emit=emit)
|
||||
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,
|
||||
@@ -616,10 +656,8 @@ def execute_job(job, emit):
|
||||
evidence=str(root/'diagnostics/preflight.json'))
|
||||
cert_project = block_root/'Cert'/Path(job['project']).name
|
||||
pcb_log = Path(job['pcb']).parent/'LogFiles'/'PCBLogFile.txt'
|
||||
try:
|
||||
pcb_log_offset = pcb_log.stat().st_size
|
||||
except OSError:
|
||||
pcb_log_offset = 0
|
||||
from .icdb_recovery import snapshot_log
|
||||
pcb_log_offset = snapshot_log(pcb_log)
|
||||
integration_log_dir = Path(job['pcb']).parent / 'LogFiles'
|
||||
integration_offsets = {}
|
||||
for name in ('ForwardAnnotation.txt', 'MsgBoxResponse.log', 'PartPkg.log'):
|
||||
@@ -633,7 +671,28 @@ def execute_job(job, emit):
|
||||
}
|
||||
except OSError:
|
||||
integration_offsets[path] = {'size': 0, 'mtime_ns': None, 'digest': ''}
|
||||
pcb_before = {pid for pid, _, _ in pcb_processes()}
|
||||
integration_offsets[path]['icdb_baseline'] = snapshot_log(path)
|
||||
try:
|
||||
(root/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
|
||||
'source': str(pcb_log), 'baseline': pcb_log_offset,
|
||||
'integration_logs': {str(path): value['icdb_baseline']
|
||||
for path, value in integration_offsets.items()},
|
||||
}, ensure_ascii=False), encoding='utf-8')
|
||||
except OSError as error:
|
||||
emit(stage='startup_log_baseline_unavailable', detail=str(error))
|
||||
|
||||
def native_error():
|
||||
if job.get('cancel_signal') and Path(job['cancel_signal']).exists():
|
||||
return 'VERIFY_CANCELLED: 停止后续操作并正常关闭目标文档'
|
||||
# Inspect every source before returning a generic server/packager
|
||||
# failure: either can be a consequence of a lost library session.
|
||||
startup = pcb_log_reports_missing_server(job['pcb'], offset=pcb_log_offset)
|
||||
integration = pcb_log_reports_integration_error(job['pcb'], offsets=integration_offsets)
|
||||
if 'ICDB_REPAIR_REQUIRED' in startup:
|
||||
return startup
|
||||
if 'ICDB_REPAIR_REQUIRED' in integration:
|
||||
return integration
|
||||
return startup or integration
|
||||
emit(stage='pcb_baseline', pids=sorted(pcb_before))
|
||||
lm_session = None
|
||||
lm_pid = None
|
||||
@@ -641,7 +700,9 @@ def execute_job(job, emit):
|
||||
worker_parent_pid = None
|
||||
launcher_process = None
|
||||
completed = False
|
||||
identity_verified = False
|
||||
helper = None
|
||||
app = doc = pi = None
|
||||
try:
|
||||
ensure_pcb_central_library(
|
||||
job['pcb'],
|
||||
@@ -681,6 +742,8 @@ def execute_job(job, emit):
|
||||
pcb_deadline = reset_verify_activity_deadline(
|
||||
0.0, time.monotonic(), timeout_seconds=pcb_startup_timeout_seconds(job['timeout']))
|
||||
while time.monotonic() < pcb_deadline:
|
||||
if job.get('cancel_signal') and Path(job['cancel_signal']).exists():
|
||||
raise RuntimeError('VERIFY_CANCELLED: PCB 启动期间收到停止请求')
|
||||
records = _process_details('ExpeditionPCB.exe')
|
||||
worker = select_reusable_circuit_worker(
|
||||
records,
|
||||
@@ -704,8 +767,9 @@ def execute_job(job, emit):
|
||||
process_alive=lambda: any(candidate_pid in {lm_pid, worker_pid, worker_parent_pid}
|
||||
for candidate_pid, _, _ in pcb_processes()),
|
||||
emit=emit,
|
||||
error_reader=lambda: pcb_log_reports_missing_server(job['pcb'], offset=pcb_log_offset),
|
||||
error_reader=native_error,
|
||||
)
|
||||
identity_verified = True
|
||||
# Do not acquire MGCPCBAutomationLicensing from this 64-bit Python
|
||||
# worker. EE7.9 registers that automation class for the 32-bit host;
|
||||
# the old, working path acquires the token inside the SysWOW64
|
||||
@@ -715,13 +779,21 @@ def execute_job(job, emit):
|
||||
pi,
|
||||
emit=emit,
|
||||
timeout_seconds=job['timeout'],
|
||||
error_reader=lambda: pcb_log_reports_integration_error(job['pcb'], offsets=integration_offsets),
|
||||
error_reader=native_error,
|
||||
)
|
||||
# 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()
|
||||
if job.get('cancel_signal') and Path(job['cancel_signal']).exists():
|
||||
raise RuntimeError('VERIFY_CANCELLED: 未开始保存')
|
||||
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'],
|
||||
Path(job['pcb']).name, str(job.get('cancel_signal') or '')],
|
||||
stdout=log, stderr=log, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
emit(stage='save_helper', helper_pid=helper.pid)
|
||||
last_markers = ()
|
||||
@@ -753,17 +825,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}')
|
||||
addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])
|
||||
if addresses:
|
||||
raise RuntimeError(f'ICDB_RELEASE_UNCONFIRMED: PCB 已退出但数据库地址记录仍存在;保留文件并停止批次: {addresses}')
|
||||
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 +842,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 +854,15 @@ 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)
|
||||
emit(stage='save_helper_retained', helper_pid=helper.pid)
|
||||
elif not completed and identity_verified:
|
||||
try:
|
||||
from .failed_session import close_failed_document
|
||||
failed_close = close_failed_document(job, emit=emit)
|
||||
if failed_close.get('status') == 'CLOSED' and lm_session is not None:
|
||||
lm_session.close()
|
||||
except Exception as error:
|
||||
emit(stage='failure_close_unconfirmed', detail=str(error))
|
||||
cleanup_verify_processes(
|
||||
lm_pid,
|
||||
worker_pid,
|
||||
@@ -790,15 +871,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 +890,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 +924,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 +934,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 +947,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'], cancel_path=job.get('cancel_signal'))
|
||||
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 +960,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 +1030,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 +1052,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 +1069,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}')
|
||||
@@ -1053,17 +1083,27 @@ def icdb_ports_bindable(ports=(10000, 10050, 10100)):
|
||||
|
||||
|
||||
def pcb_log_reports_missing_server(pcb_path, *, offset=0):
|
||||
"""Detect the same failure from ExpeditionPCB's own log, as a second signal."""
|
||||
"""Detect startup failure from this run, preserving native recovery causes."""
|
||||
from .icdb_recovery import (decode_log, parse_session_loss, read_log_failure,
|
||||
recovery_detail)
|
||||
log = Path(pcb_path).parent/'LogFiles'/'PCBLogFile.txt'
|
||||
try:
|
||||
with log.open('rb') as stream:
|
||||
stream.seek(max(0, offset))
|
||||
text = stream.read().decode('utf-8', errors='replace')
|
||||
if isinstance(offset, dict):
|
||||
text, recovery = read_log_failure(log, offset)
|
||||
else:
|
||||
# Retain the byte-offset interface for existing diagnostic callers.
|
||||
with log.open('rb') as stream:
|
||||
stream.seek(max(0, offset))
|
||||
text = decode_log(stream.read())
|
||||
recovery = parse_session_loss(text)
|
||||
except Exception:
|
||||
return ''
|
||||
if recovery:
|
||||
return recovery_detail(recovery)
|
||||
for marker in ("could not bring it alive", 'Could not run server'):
|
||||
if marker.lower() in text.lower():
|
||||
return RESERVED_PORT_MESSAGE
|
||||
return (f'DESIGN_SERVER_FAILED: {marker}。请检查本轮原生日志中的数据库/服务器原因;'
|
||||
'此信息本身不能证明端口被系统保留。')
|
||||
return ''
|
||||
|
||||
|
||||
@@ -1078,6 +1118,7 @@ _INTEGRATION_ERROR_LINE = re.compile(
|
||||
|
||||
def pcb_log_reports_integration_error(pcb_path, *, offsets=None):
|
||||
"""Read explicit Project Integration errors produced by this run."""
|
||||
from .icdb_recovery import parse_session_loss, read_log_failure, recovery_detail
|
||||
log_dir = Path(pcb_path).parent / 'LogFiles'
|
||||
offsets = offsets or {}
|
||||
details = []
|
||||
@@ -1086,6 +1127,19 @@ def pcb_log_reports_integration_error(pcb_path, *, offsets=None):
|
||||
try:
|
||||
size = path.stat().st_size
|
||||
baseline = offsets.get(path, offsets.get(str(path), 0))
|
||||
has_icdb_baseline = isinstance(baseline, dict) and any(
|
||||
key in baseline for key in ('icdb_baseline', 'tail_sha256', 'error'))
|
||||
icdb_baseline = baseline.get('icdb_baseline', baseline) if isinstance(baseline, dict) else None
|
||||
if icdb_baseline and 'tail_sha256' in icdb_baseline:
|
||||
text, recovery = read_log_failure(path, icdb_baseline)
|
||||
if recovery:
|
||||
return recovery_detail(recovery)
|
||||
if 'tail_sha256' in baseline:
|
||||
# New bounded checkpoints can also be supplied directly.
|
||||
details.extend(line.strip() for line in text.splitlines()
|
||||
if _INTEGRATION_ERROR_LINE.search(line.strip())
|
||||
and not parse_session_loss(line))
|
||||
continue
|
||||
if isinstance(baseline, dict):
|
||||
baseline_size = int(baseline.get('size', 0) or 0)
|
||||
baseline_mtime = baseline.get('mtime_ns')
|
||||
@@ -1116,8 +1170,17 @@ def pcb_log_reports_integration_error(pcb_path, *, offsets=None):
|
||||
text = stream.read().decode('utf-8', errors='replace')
|
||||
except OSError:
|
||||
continue
|
||||
# Legacy byte offsets/digests remain accepted by offline callers.
|
||||
if not has_icdb_baseline:
|
||||
recovery = parse_session_loss(text)
|
||||
if recovery:
|
||||
return recovery_detail(recovery)
|
||||
for line in text.splitlines():
|
||||
cleaned = line.strip()
|
||||
if has_icdb_baseline and parse_session_loss(cleaned):
|
||||
# read_log_failure already rejected this retained old session.
|
||||
# Do not let the generic ERROR filter promote it again.
|
||||
continue
|
||||
if _INTEGRATION_ERROR_LINE.search(cleaned) and cleaned not in details:
|
||||
details.append(cleaned)
|
||||
return ' | '.join(details[:8])
|
||||
@@ -1310,6 +1373,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.
|
||||
|
||||
@@ -1325,17 +1407,19 @@ def read_library_flag(library, name, *, timeout_seconds=90, log_path=None):
|
||||
if not executable.is_file():
|
||||
return FLAG_UNKNOWN, f'找不到 Library Manager: {executable}'
|
||||
|
||||
deadline = time.monotonic() + timeout_seconds
|
||||
desktop_name = 'CbbFlagRead_' + os.urandom(5).hex()
|
||||
desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None)
|
||||
from pywinauto import Desktop
|
||||
from pywinauto.controls.common_controls import TreeViewWrapper
|
||||
|
||||
deadline = time.monotonic() + timeout_seconds
|
||||
desktop_name = 'CbbFlagRead_' + os.urandom(5).hex()
|
||||
desktop = None
|
||||
|
||||
handle = None
|
||||
pid = None
|
||||
handled = set()
|
||||
log = log_path or Path(library).parent/'LogFiles'/'cbb_flag_read_dialogs.jsonl'
|
||||
try:
|
||||
desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None)
|
||||
startup = win32process.STARTUPINFO()
|
||||
startup.lpDesktop = 'winsta0\\' + desktop_name
|
||||
handle, thread, pid, _ = win32process.CreateProcess(
|
||||
@@ -1372,23 +1456,21 @@ 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()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
desktop.CloseDesktop()
|
||||
if desktop is not None:
|
||||
desktop.CloseDesktop()
|
||||
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.
|
||||
@@ -1411,12 +1493,13 @@ def read_library_tree_image_map(library, names, *, timeout_seconds=45, log_path=
|
||||
if not executable.is_file():
|
||||
return result
|
||||
desktop_name = 'CbbStatusRead_' + os.urandom(5).hex()
|
||||
desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None)
|
||||
desktop = None
|
||||
handle = None
|
||||
pid = None
|
||||
handled = set()
|
||||
log = log_path or Path(library).parent/'LogFiles'/'cbb_status_read_dialogs.jsonl'
|
||||
try:
|
||||
desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None)
|
||||
startup = win32process.STARTUPINFO()
|
||||
startup.lpDesktop = 'winsta0\\' + desktop_name
|
||||
handle, thread, pid, _ = win32process.CreateProcess(
|
||||
@@ -1463,23 +1546,21 @@ 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()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
desktop.CloseDesktop()
|
||||
if desktop is not None:
|
||||
desktop.CloseDesktop()
|
||||
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):
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
"""Backed-up, single-attempt recovery through the installed native tool."""
|
||||
from dataclasses import dataclass
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RepairTarget:
|
||||
project: Path
|
||||
database: Path
|
||||
|
||||
|
||||
def select_repair_target(request, diagnosis):
|
||||
from .verify_runner import preflight
|
||||
if diagnosis.get('error_code') != 'ICDB_REPAIR_REQUIRED' or not diagnosis.get('session'):
|
||||
raise ValueError('REPAIR_TARGET_UNCONFIRMED: 缺少原生失联会话信息')
|
||||
if diagnosis.get('scope') == 'central_library':
|
||||
raise ValueError('SHARED_DATABASE_REPAIR_UNSUPPORTED: 当前原生 Repair 入口要求 PRJ,不能改修无关的 Work 工程')
|
||||
project, pcb = preflight(request)
|
||||
try:
|
||||
reported = Path(diagnosis.get('database_path', '')).resolve(strict=True)
|
||||
except OSError as error:
|
||||
raise ValueError(f'REPAIR_TARGET_UNCONFIRMED: 报错数据库路径不可用:{error}') from error
|
||||
for candidate in (project, pcb.parent/'pcb.prj'):
|
||||
database = candidate.parent/'database'
|
||||
if (candidate.is_file() and database.resolve() == reported
|
||||
and reported.is_relative_to(project.parent.resolve())):
|
||||
if not (reported/'icdb.dat').is_file():
|
||||
raise ValueError('REPAIR_TARGET_UNCONFIRMED: 未找到目标 icdb.dat')
|
||||
return RepairTarget(candidate.resolve(), reported)
|
||||
raise ValueError('REPAIR_TARGET_UNCONFIRMED: 报错数据库不属于本次工程,未猜测或改写路径')
|
||||
|
||||
|
||||
def _hash(path):
|
||||
digest = hashlib.sha256()
|
||||
with path.open('rb') as stream:
|
||||
for chunk in iter(lambda: stream.read(1024*1024), b''):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def backup_project(target, destination):
|
||||
"""Keep every original file, holding icdb.dat exclusively during the copy."""
|
||||
import win32con, win32file, pywintypes
|
||||
source = target.project.parent.resolve()
|
||||
destination = Path(destination).resolve()
|
||||
if destination.is_relative_to(source) or source.is_relative_to(destination):
|
||||
raise ValueError('REPAIR_BACKUP_INVALID: 备份与源工程不能相互包含')
|
||||
paths = list(source.rglob('*'))
|
||||
if any(path.is_symlink() or path.is_junction() for path in paths):
|
||||
raise ValueError('REPAIR_BACKUP_INVALID: 工程包含目录链接,不能完整备份')
|
||||
if (source/'backup-manifest.json').exists():
|
||||
raise ValueError('REPAIR_BACKUP_INVALID: 原工程含同名备份清单')
|
||||
database_file = target.database/'icdb.dat'
|
||||
try:
|
||||
handle = win32file.CreateFile(str(database_file), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
except pywintypes.error as error:
|
||||
raise OSError(f'REPAIR_DATABASE_IN_USE: 无法独占读取数据库:{error}') from error
|
||||
try:
|
||||
destination.mkdir(parents=True, exist_ok=False)
|
||||
files = {}
|
||||
for path in paths:
|
||||
relative = path.relative_to(source)
|
||||
copied = destination/relative
|
||||
if path.is_dir():
|
||||
copied.mkdir(parents=True, exist_ok=True)
|
||||
continue
|
||||
before = path.stat()
|
||||
copied.parent.mkdir(parents=True, exist_ok=True)
|
||||
if path == database_file:
|
||||
with copied.open('wb') as stream:
|
||||
while True:
|
||||
try:
|
||||
_, data = win32file.ReadFile(handle, 1024*1024)
|
||||
except pywintypes.error as error:
|
||||
if error.winerror == 38: # ERROR_HANDLE_EOF
|
||||
break
|
||||
raise
|
||||
if not data:
|
||||
break
|
||||
stream.write(data)
|
||||
os.utime(copied, ns=(before.st_atime_ns, before.st_mtime_ns))
|
||||
digest = _hash(copied)
|
||||
else:
|
||||
digest = _hash(path)
|
||||
shutil.copy2(path, copied)
|
||||
if _hash(copied) != digest:
|
||||
raise OSError(f'REPAIR_BACKUP_CHANGED: 文件副本校验失败:{path}')
|
||||
after = path.stat()
|
||||
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
|
||||
raise OSError(f'REPAIR_BACKUP_CHANGED: 备份期间源文件变化:{path}')
|
||||
files[str(relative)] = {'size': before.st_size, 'sha256': digest}
|
||||
if {path.relative_to(source) for path in source.rglob('*')} != {
|
||||
path.relative_to(source) for path in paths}:
|
||||
raise OSError('REPAIR_BACKUP_CHANGED: 备份期间工程文件集合变化')
|
||||
record = {'complete': True, 'source': str(source), 'files': files}
|
||||
(destination/'backup-manifest.json').write_text(json.dumps(record, ensure_ascii=False, indent=2),
|
||||
encoding='utf-8')
|
||||
return record
|
||||
finally:
|
||||
handle.Close()
|
||||
|
||||
|
||||
def attempt_recovery(request, diagnosis, evidence_root, *, progress=None):
|
||||
root = Path(evidence_root)/'native-repair'
|
||||
root.mkdir(exist_ok=True)
|
||||
marker = root/'attempt.json'
|
||||
if marker.exists():
|
||||
return {'status': 'BLOCKED', 'detail': 'REPAIR_ALREADY_ATTEMPTED', 'evidence': str(root)}
|
||||
record = {'status': 'BLOCKED', 'evidence': str(root)}
|
||||
try:
|
||||
process_record = Path(evidence_root)/'content-check-process.json'
|
||||
if process_record.is_file():
|
||||
state = json.loads(process_record.read_text('utf-8')).get('status')
|
||||
if state in {'RUNNING', 'RETAINED'}:
|
||||
raise ValueError('IMPORTER_RELEASE_UNCONFIRMED: 内容检查客户端仍占有数据库,未尝试 Repair')
|
||||
with marker.open('x', encoding='utf-8') as stream:
|
||||
json.dump({'status': 'PREPARING', 'diagnosis': diagnosis}, stream, ensure_ascii=False)
|
||||
target = select_repair_target(request, diagnosis)
|
||||
if progress:
|
||||
progress({'stage': 'native_repair_start', 'block': request.block_name,
|
||||
'detail': '正在备份并尝试原生数据库修复', 'project': str(target.project)})
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
if pcb_processes() or _process_details('LibraryManager.exe'):
|
||||
raise ValueError('REPAIR_DATABASE_IN_USE: 原生 PCB/Library Manager 会话尚未退出')
|
||||
for _pid, _exe, command, _parent in _process_details('cscript.exe'):
|
||||
if str(target.project).casefold() in str(command).casefold():
|
||||
raise ValueError('REPAIR_DATABASE_IN_USE: 目标的保存/关闭助手尚未退出')
|
||||
callback = (lambda event: progress({'block': request.block_name, **event})) if progress else None
|
||||
record = run_native_repair(target, root, mentor_bin=request.mentor_bin,
|
||||
timeout_seconds=max(60, request.timeout_seconds), progress=callback)
|
||||
except Exception as error:
|
||||
record['detail'] = str(error)
|
||||
marker.write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
return record
|
||||
|
||||
|
||||
def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None):
|
||||
"""Supervise the GUI on its own desktop; a repair timeout never kills it."""
|
||||
from .desktop_worker import DesktopWorker
|
||||
root = Path(root)
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
job_path = root/'repair-request.json'
|
||||
cancel = root/'cancel-repair.signal'
|
||||
desktop = 'CbbRepair_'+uuid4().hex[:12]
|
||||
job_path.write_text(json.dumps({'project': str(target.project), 'database': str(target.database),
|
||||
'root': str(root), 'mentor_bin': str(mentor_bin or ''), 'timeout': timeout_seconds,
|
||||
'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop},
|
||||
ensure_ascii=False), encoding='utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)]
|
||||
worker = DesktopWorker(command, desktop)
|
||||
worker.bind_cancellation(cancel)
|
||||
deadline = time.monotonic()+max(1, float(timeout_seconds))+5
|
||||
event_count = 0
|
||||
try:
|
||||
while worker.poll() is None and time.monotonic()<deadline:
|
||||
event_file = root/'repair-events.jsonl'
|
||||
if progress and event_file.is_file():
|
||||
lines = event_file.read_text('utf-8').splitlines()
|
||||
for line in lines[event_count:]:
|
||||
try:
|
||||
progress(json.loads(line))
|
||||
except json.JSONDecodeError:
|
||||
break
|
||||
event_count += 1
|
||||
time.sleep(.2)
|
||||
outcome = root/'repair-result.json'
|
||||
if worker.poll() is None or not outcome.is_file():
|
||||
worker.request_stop()
|
||||
return {'status': 'RETAINED', 'worker_pid': worker.pid, 'desktop': desktop,
|
||||
'detail': '原生修复尚未确认结束,保留操作并停止批次', 'evidence': str(root)}
|
||||
result = json.loads(outcome.read_text('utf-8'))
|
||||
if worker.returncode != 0 and result.get('status') == 'REPAIRED':
|
||||
result['status'] = 'UNCONFIRMED'
|
||||
return {**result, 'evidence': str(root)}
|
||||
finally:
|
||||
worker.close()
|
||||
@@ -0,0 +1,225 @@
|
||||
"""Drive the installed Project Backup Repair command on a private desktop."""
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
import json
|
||||
import ntpath
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
import zipfile
|
||||
|
||||
|
||||
def confirmation_allowed(main_title, project, text):
|
||||
title = main_title.removeprefix('iCDB Project Backup - ')
|
||||
if ntpath.normcase(ntpath.normpath(title)) != ntpath.normcase(ntpath.normpath(str(project))):
|
||||
return False
|
||||
normalized = ' '.join(text.casefold().split())
|
||||
return all(marker in normalized for marker in (
|
||||
'the project content will be validated and repaired if necessary.',
|
||||
'a project backup will be created before any action take place.',
|
||||
'you will be able to revert all changes using that backup.'))
|
||||
|
||||
|
||||
def repair_completed(diagnostic_text, database_text, *, native_backup):
|
||||
combined = diagnostic_text+'\n'+database_text
|
||||
if re.search(r'\bERROR\s*:|not repaired|repair.*failed', combined, re.IGNORECASE):
|
||||
return False
|
||||
return bool(native_backup and 'Diagnostic: FINISH' in diagnostic_text
|
||||
and ('Diagnostic: Snapshot:' in diagnostic_text or 'Diagnostic: START' in diagnostic_text)
|
||||
and 'iCDBProjectBackupRepair' in database_text and 'IC: Closing project' in database_text)
|
||||
|
||||
|
||||
def _current_desktop_name():
|
||||
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||||
kernel32.GetCurrentThreadId.restype = wintypes.DWORD
|
||||
user32.GetThreadDesktop.argtypes = [wintypes.DWORD]
|
||||
user32.GetThreadDesktop.restype = wintypes.HANDLE
|
||||
user32.GetUserObjectInformationW.argtypes = [wintypes.HANDLE, ctypes.c_int,
|
||||
wintypes.LPVOID, wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
|
||||
desktop = user32.GetThreadDesktop(kernel32.GetCurrentThreadId())
|
||||
name = ctypes.create_unicode_buffer(512)
|
||||
required = wintypes.DWORD()
|
||||
if not user32.GetUserObjectInformationW(desktop, 2, name, ctypes.sizeof(name), ctypes.byref(required)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
return name.value
|
||||
|
||||
|
||||
def _repair_command(executable):
|
||||
import win32api, win32gui, win32con
|
||||
user32 = ctypes.WinDLL('user32')
|
||||
user32.GetMenuStringW.argtypes = [wintypes.HMENU, wintypes.UINT, wintypes.LPWSTR,
|
||||
ctypes.c_int, wintypes.UINT]
|
||||
library = win32api.LoadLibraryEx(str(executable), 0, 2) # data resources only
|
||||
commands = set()
|
||||
def walk(menu, parents=()):
|
||||
for index in range(win32gui.GetMenuItemCount(menu)):
|
||||
value = ctypes.create_unicode_buffer(512)
|
||||
user32.GetMenuStringW(menu, index, value, len(value), win32con.MF_BYPOSITION)
|
||||
label = value.value.replace('&', '').split('\t')[0].strip().casefold()
|
||||
sub = win32gui.GetSubMenu(menu, index)
|
||||
if sub:
|
||||
walk(sub, (*parents, label))
|
||||
elif parents == ('project',) and label == 'repair':
|
||||
commands.add(win32gui.GetMenuItemID(menu, index))
|
||||
try:
|
||||
for name in win32api.EnumResourceNames(library, 4):
|
||||
menu = win32gui.LoadMenu(library, name)
|
||||
try:
|
||||
walk(menu)
|
||||
finally:
|
||||
win32gui.DestroyMenu(menu)
|
||||
finally:
|
||||
win32api.FreeLibrary(library)
|
||||
if len(commands) != 1:
|
||||
raise ValueError('NATIVE_REPAIR_UNSUPPORTED: 未唯一定位原生 Project/Repair 菜单')
|
||||
return commands.pop()
|
||||
|
||||
|
||||
def _windows(pid):
|
||||
import win32gui, win32process
|
||||
windows = []
|
||||
def observe(hwnd, _):
|
||||
if win32process.GetWindowThreadProcessId(hwnd)[1] != pid or not win32gui.IsWindowVisible(hwnd):
|
||||
return
|
||||
children = []
|
||||
def child(handle, _):
|
||||
if win32gui.IsWindowVisible(handle):
|
||||
children.append({'hwnd': handle, 'text': win32gui.GetWindowText(handle),
|
||||
'class': win32gui.GetClassName(handle), 'id': win32gui.GetDlgCtrlID(handle),
|
||||
'enabled': bool(win32gui.IsWindowEnabled(handle))})
|
||||
win32gui.EnumChildWindows(hwnd, child, None)
|
||||
windows.append({'hwnd': hwnd, 'title': win32gui.GetWindowText(hwnd),
|
||||
'class': win32gui.GetClassName(hwnd), 'enabled': bool(win32gui.IsWindowEnabled(hwnd)),
|
||||
'children': children})
|
||||
win32gui.EnumWindows(observe, None)
|
||||
return windows
|
||||
|
||||
|
||||
def _native_evidence(project, database, pid, started_ns, baseline):
|
||||
"""Use only this native process's new diagnostic/close and backup evidence."""
|
||||
def read_logs(folder):
|
||||
text = []
|
||||
for path in (database/'cdbsvr'/folder).glob(f'*-{pid}/*.txt'):
|
||||
if path.stat().st_mtime_ns >= started_ns:
|
||||
text.append(path.read_bytes()[-256*1024:].decode('utf-8', errors='replace'))
|
||||
return '\n'.join(text)
|
||||
from .icdb_recovery import read_log_failure
|
||||
backup_log = project.parent/'Log Files/ProjectBackupLog.txt'
|
||||
text, _ = read_log_failure(backup_log, baseline)
|
||||
matches = re.findall(r'Successfully created \[Project Repair\] backup \[([^\]]+)\]', text)
|
||||
native_backup = False
|
||||
for name in matches:
|
||||
# Native default storage verified here; alternate storage needs its own
|
||||
# target/backup validation and must not silently become success.
|
||||
if Path(name).name != name:
|
||||
continue
|
||||
archive = project.parent/'ProjectBackup/backups'/name
|
||||
if archive.is_file():
|
||||
with zipfile.ZipFile(archive) as zipped:
|
||||
native_backup = bool(zipped.namelist()) and zipped.testzip() is None
|
||||
diagnostic, db_log = read_logs('dbcheck'), read_logs('log')
|
||||
restore_log = read_logs('dbrestore')
|
||||
restored = re.search(r'iCDB database restored from backup:\s*([^\r\n]+)', restore_log)
|
||||
valid = (not restored and str(database).casefold() in db_log.casefold()
|
||||
and repair_completed(diagnostic, db_log, native_backup=native_backup))
|
||||
return {'confirmed': bool(valid), 'native_backup': native_backup,
|
||||
'diagnostic_tail': diagnostic[-8192:], 'database_tail': db_log[-8192:],
|
||||
'restored_from_backup': restored.group(1).strip() if restored else '',
|
||||
'restore_complete': bool(restored and '[0] DONE' in restore_log
|
||||
and 'Diagnostic: FINISH' in diagnostic),
|
||||
'restore_tail': restore_log[-8192:]}
|
||||
|
||||
|
||||
def repair_worker_main(job_path):
|
||||
import win32con, win32gui
|
||||
from .native_repair import RepairTarget, backup_project
|
||||
from .mentor_paths import discover_sdd_home
|
||||
from .icdb_recovery import snapshot_log
|
||||
job = json.loads(Path(job_path).read_text('utf-8'))
|
||||
root = Path(job['root'])
|
||||
project, database = Path(job['project']), Path(job['database'])
|
||||
result = {'status': 'BLOCKED', 'project': str(project), 'database': str(database)}
|
||||
process = None
|
||||
def emit(**event):
|
||||
with (root/'repair-events.jsonl').open('a', encoding='utf-8') as stream:
|
||||
stream.write(json.dumps(event, ensure_ascii=False)+'\n')
|
||||
try:
|
||||
if (not str(job.get('desktop', '')).startswith('CbbRepair_')
|
||||
or _current_desktop_name() != job['desktop']):
|
||||
raise ValueError('REPAIR_PRIVATE_DESKTOP_REQUIRED')
|
||||
if not job.get('supervisor_pid'):
|
||||
raise ValueError('REPAIR_SUPERVISOR_REQUIRED')
|
||||
from .desktop_worker import start_supervisor_watchdog
|
||||
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job['cancel_signal'])
|
||||
if project.suffix.casefold() != '.prj' or database.resolve() != (project.parent/'database').resolve():
|
||||
raise ValueError('REPAIR_TARGET_UNCONFIRMED')
|
||||
home = discover_sdd_home(job.get('mentor_bin'))
|
||||
if home is None:
|
||||
raise ValueError('NATIVE_REPAIR_UNAVAILABLE: 未找到 Mentor 安装目录')
|
||||
executable = home/'iCDB/win32/bin/iCDBProjectBackup.exe'
|
||||
command_id = _repair_command(executable)
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
if pcb_processes() or _process_details('LibraryManager.exe'):
|
||||
raise ValueError('REPAIR_DATABASE_IN_USE')
|
||||
backup_project(RepairTarget(project, database), root/'complete-backup')
|
||||
emit(stage='complete_backup_verified', path=str(root/'complete-backup'))
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_AFTER_BACKUP')
|
||||
baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt')
|
||||
started_ns = time.time_ns()
|
||||
process = subprocess.Popen([str(executable), '-p', str(project)], cwd=str(project.parent))
|
||||
result['native_pid'] = process.pid
|
||||
emit(stage='native_started', pid=process.pid, project=str(project))
|
||||
deadline = time.monotonic()+max(1, float(job['timeout']))
|
||||
invoked = confirmed = closing = False
|
||||
evidence = None
|
||||
while process.poll() is None and time.monotonic()<deadline:
|
||||
windows = _windows(process.pid)
|
||||
main = next((w for w in windows if w['class'].startswith('Afx:')
|
||||
and w['title'].casefold() == ('iCDB Project Backup - '+str(project)).casefold()), None)
|
||||
dialogs = [w for w in windows if w['class'] == '#32770']
|
||||
if not invoked and main and not dialogs and main['enabled']:
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_START')
|
||||
win32gui.PostMessage(main['hwnd'], win32con.WM_COMMAND, command_id, 0)
|
||||
invoked = True
|
||||
emit(stage='native_repair_requested', command=command_id)
|
||||
if invoked and not confirmed and main:
|
||||
for dialog in dialogs:
|
||||
text = '\n'.join(c['text'] for c in dialog['children'])
|
||||
if confirmation_allowed(main['title'], project, text):
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_CONFIRM')
|
||||
button = next((c for c in dialog['children'] if c['class'] == 'Button'
|
||||
and c['id'] == 1 and c['enabled']), None)
|
||||
if button:
|
||||
win32gui.PostMessage(button['hwnd'], win32con.BM_CLICK, 0, 0)
|
||||
confirmed = True
|
||||
emit(stage='native_repair_confirmed', text=text)
|
||||
if confirmed and not closing and main and not dialogs and main['enabled']:
|
||||
evidence = _native_evidence(project, database, process.pid, started_ns, baseline)
|
||||
if (evidence['confirmed'] or (evidence.get('restore_complete')
|
||||
and evidence['native_backup']
|
||||
and not (database/'cdbsvr/sAddress.adr').exists())):
|
||||
win32gui.PostMessage(main['hwnd'], win32con.WM_CLOSE, 0, 0)
|
||||
closing = True
|
||||
emit(stage='native_repair_finished', **evidence)
|
||||
time.sleep(.2)
|
||||
if (closing and process.poll() == 0 and evidence and evidence.get('restore_complete')
|
||||
and not (database/'cdbsvr/sAddress.adr').exists()):
|
||||
result.update(status='RESTORED_REVIEW_REQUIRED', evidence=evidence,
|
||||
detail='原生工具从已有备份恢复数据库;需核对恢复版本和工程内容,未自动重试校验')
|
||||
elif (closing and process.poll() == 0 and evidence and evidence['confirmed']
|
||||
and not (database/'cdbsvr/sAddress.adr').exists()):
|
||||
result.update(status='REPAIRED', evidence=evidence)
|
||||
else:
|
||||
result.update(status='RETAINED' if process.poll() is None else 'UNCONFIRMED',
|
||||
detail='原生 Repair 未取得完整成功/释放证据,未重试校验')
|
||||
except Exception as error:
|
||||
result.update(detail=str(error), status='RETAINED' if process and process.poll() is None else 'BLOCKED')
|
||||
# Never terminate a repair or its database owner. Retained PID/desktop is
|
||||
# reported to the parent, including unknown native prompts.
|
||||
(root/'repair-result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
return 0 if result['status'] == 'REPAIRED' else 1
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Cooperative FIFO admission for this application's native operations."""
|
||||
from threading import Condition
|
||||
|
||||
|
||||
class QueueCancelled(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class NativeQueue:
|
||||
def __init__(self):
|
||||
self.condition = Condition()
|
||||
self.pending = []
|
||||
self.active = False
|
||||
|
||||
def wake(self):
|
||||
with self.condition: self.condition.notify_all()
|
||||
|
||||
def run(self, action, *, stop=None, waiting=None, ready=lambda: True):
|
||||
ticket = object()
|
||||
acquired, notified = False, False
|
||||
with self.condition: self.pending.append((ticket, ready))
|
||||
try:
|
||||
while not acquired:
|
||||
with self.condition:
|
||||
if stop is not None and stop.is_set():
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
first = next((token for token, available in self.pending if available()), None)
|
||||
if not self.active and first is ticket:
|
||||
self.pending = [entry for entry in self.pending if entry[0] is not ticket]
|
||||
self.active = acquired = True
|
||||
else:
|
||||
self.condition.wait(.1)
|
||||
if not acquired and not notified and waiting is not None:
|
||||
notified = True
|
||||
try: waiting()
|
||||
except Exception: pass
|
||||
if stop is not None and stop.is_set():
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
return action()
|
||||
finally:
|
||||
with self.condition:
|
||||
self.pending = [entry for entry in self.pending if entry[0] is not ticket]
|
||||
if acquired: self.active = False
|
||||
self.condition.notify_all()
|
||||
@@ -0,0 +1,88 @@
|
||||
"""Bound WMI enumeration in a read-only helper, never in the Qt thread."""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
|
||||
def query_owners(names, *, timeout_seconds=10):
|
||||
names = tuple(names)
|
||||
if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names):
|
||||
raise ValueError('OWNER_QUERY_INVALID')
|
||||
return _query_processes(names, timeout_seconds=timeout_seconds)
|
||||
|
||||
|
||||
def query_process_snapshot(*, timeout_seconds=10):
|
||||
"""Bound a complete process tree; retain unknown executable descendants."""
|
||||
return _query_processes(None, timeout_seconds=timeout_seconds)
|
||||
|
||||
|
||||
def _query_processes(names, *, timeout_seconds):
|
||||
full_snapshot = names is None
|
||||
request = {'snapshot_version': 1} if full_snapshot else {'names': names}
|
||||
with tempfile.TemporaryDirectory(prefix='cbb-owner-probe-') as directory:
|
||||
job = Path(directory)/'request.json'
|
||||
job.write_text(json.dumps(request), encoding='utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--owner-probe', str(job)]
|
||||
try:
|
||||
result = subprocess.run(command, timeout=max(.1, min(10, timeout_seconds)),
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
except subprocess.TimeoutExpired as error:
|
||||
# This helper only enumerates processes; it never launches Mentor
|
||||
# or acquires a native database session.
|
||||
raise RuntimeError('OWNER_QUERY_TIMEOUT: 进程查询超时,占用状态无法确认') from error
|
||||
try:
|
||||
record = json.loads(job.with_name('result.json').read_text('utf-8'))
|
||||
rows = record['owners']
|
||||
identity_matches = (type(record.get('snapshot_version')) is int and record['snapshot_version'] == 1
|
||||
if full_snapshot else record.get('names') == list(names))
|
||||
if result.returncode != 0 or not identity_matches or not isinstance(rows, list):
|
||||
raise ValueError('invalid result')
|
||||
seen = set()
|
||||
for row in rows:
|
||||
if (type(row['pid']) is not int or row['pid'] < (0 if full_snapshot else 1)
|
||||
or row['pid'] in seen or not isinstance(row['name'], str) or not row['name']
|
||||
or not full_snapshot and row['name'].casefold() not in {n.casefold() for n in names}
|
||||
or full_snapshot and not isinstance(row['created'], str)
|
||||
or not isinstance(row['command'], str) or not isinstance(row['executable'], str)
|
||||
or type(row['parent']) is not int or row['parent'] < 0):
|
||||
raise ValueError('invalid owner')
|
||||
seen.add(row['pid'])
|
||||
return rows
|
||||
except (OSError, ValueError, KeyError, TypeError, AttributeError) as error:
|
||||
raise RuntimeError('OWNER_QUERY_UNCONFIRMED: 进程查询未返回有效证据') from error
|
||||
|
||||
|
||||
def owner_probe_main(job):
|
||||
import pythoncom
|
||||
import win32com.client
|
||||
job = Path(job)
|
||||
request = json.loads(job.read_text('utf-8'))
|
||||
full_snapshot = request == {'snapshot_version': 1} and type(request.get('snapshot_version')) is int
|
||||
names = request.get('names')
|
||||
if not full_snapshot:
|
||||
if (not isinstance(names, list) or not names
|
||||
or any(not isinstance(name, str) or not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names)):
|
||||
return 1
|
||||
pythoncom.CoInitialize()
|
||||
try:
|
||||
service = win32com.client.GetObject('winmgmts:')
|
||||
query = ('SELECT ProcessId,Name,CommandLine,ExecutablePath,ParentProcessId' +
|
||||
(',CreationDate' if full_snapshot else '') + ' FROM Win32_Process')
|
||||
if not full_snapshot:
|
||||
query += ' WHERE ' + ' OR '.join(f"Name='{name}'" for name in names)
|
||||
rows = [{'pid': int(item.ProcessId), 'name': str(item.Name),
|
||||
'command': str(item.CommandLine or ''), 'executable': str(item.ExecutablePath or ''),
|
||||
'parent': int(item.ParentProcessId or 0),
|
||||
**({'created': str(item.CreationDate or '')} if full_snapshot else {})}
|
||||
for item in service.ExecQuery(query)]
|
||||
service = None
|
||||
response = {'snapshot_version': 1} if full_snapshot else {'names': names}
|
||||
job.with_name('result.json').write_text(json.dumps({**response, 'owners': rows}), encoding='utf-8')
|
||||
return 0
|
||||
finally:
|
||||
pythoncom.CoUninitialize()
|
||||
@@ -5,8 +5,12 @@ Set app = GetObject(, "MGCPCB.Application")
|
||||
Set doc = app.ActiveDocument
|
||||
Set pi = doc.ProjectIntegration
|
||||
If LCase(pi.ProjectFile) <> LCase(expected) Then WScript.Quit 2
|
||||
If WScript.Arguments.Count > 1 Then
|
||||
If LCase(doc.Name) <> LCase(WScript.Arguments(1)) Then WScript.Quit 2
|
||||
End If
|
||||
If pi.ForwardAnnotationStatus <> 2 Or pi.SynchCESStatus <> 2 Or pi.LoadCESStatus <> 2 Or pi.BackAnnotationStatus <> 2 Then WScript.Quit 3
|
||||
WScript.Echo "VALIDATE_START"
|
||||
CheckCancellation
|
||||
measuredStart = Timer
|
||||
Set licensing = CreateObject("MGCPCBAutomationLicensing.Application")
|
||||
token = licensing.GetToken(doc.Validate(0))
|
||||
@@ -14,11 +18,13 @@ doc.Validate token
|
||||
LogElapsed "validate", measuredStart
|
||||
WScript.Echo "LICENSE_OK"
|
||||
WScript.Echo "SAVE_START"
|
||||
CheckCancellation
|
||||
measuredStart = Timer
|
||||
operationOK = doc.Save()
|
||||
LogElapsed "save", measuredStart
|
||||
If Not operationOK Then WScript.Quit 4
|
||||
WScript.Echo "SAVE_OK"
|
||||
CheckCancellation
|
||||
measuredStart = Timer
|
||||
operationOK = doc.IsSaved()
|
||||
LogElapsed "is_saved", measuredStart
|
||||
@@ -40,15 +46,32 @@ If Not stable Then WScript.Quit 7
|
||||
WScript.Echo "POST_SAVE_SYNCHRONIZED"
|
||||
If Not doc.IsSaved() Then WScript.Quit 8
|
||||
WScript.Echo "CLOSE_START"
|
||||
CheckCancellation
|
||||
measuredStart = Timer
|
||||
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 CheckCancellation()
|
||||
Dim fso, cancelPath
|
||||
If WScript.Arguments.Count < 3 Then Exit Sub
|
||||
cancelPath = WScript.Arguments(2)
|
||||
If Len(cancelPath) = 0 Then Exit Sub
|
||||
Set fso = CreateObject("Scripting.FileSystemObject")
|
||||
If fso.FileExists(cancelPath) Then
|
||||
WScript.Echo "VERIFY_CANCELLED"
|
||||
WScript.Quit 9
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Sub LogElapsed(phaseName, started)
|
||||
Dim elapsed
|
||||
|
||||
@@ -315,15 +315,18 @@ class StatusBridgeServer:
|
||||
except OSError:
|
||||
pass
|
||||
if process.poll() is None:
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=2)
|
||||
except subprocess.TimeoutExpired:
|
||||
try:
|
||||
process.kill()
|
||||
except OSError:
|
||||
pass
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=2)
|
||||
except subprocess.TimeoutExpired:
|
||||
pass
|
||||
try:
|
||||
process.kill()
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
process.wait(timeout=2)
|
||||
except subprocess.TimeoutExpired:
|
||||
pass
|
||||
@@ -0,0 +1,128 @@
|
||||
"""Small, asynchronous entry point for recovery from a healthy CBB project."""
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import QObject, QThreadPool, Signal
|
||||
from PySide6.QtWidgets import (QComboBox, QDialog, QFileDialog, QFormLayout,
|
||||
QHBoxLayout, QLabel, QLineEdit, QPushButton)
|
||||
|
||||
from .workers import ServiceWorker
|
||||
|
||||
|
||||
class RecoverySignals(QObject):
|
||||
progress = Signal(object)
|
||||
|
||||
|
||||
class BackupRecoveryDialog(QDialog):
|
||||
started = Signal(str)
|
||||
completed = Signal(str, object)
|
||||
|
||||
def __init__(self, names, request_factory, parent=None, *, recoverer=None):
|
||||
super().__init__(parent)
|
||||
self.setWindowTitle('从备份恢复 CBB')
|
||||
self.resize(620, 230)
|
||||
self.request_factory = request_factory
|
||||
self.recoverer = recoverer
|
||||
self.busy = False
|
||||
self.pool = QThreadPool(self)
|
||||
self.pool.setMaxThreadCount(1)
|
||||
self.signals = RecoverySignals(self)
|
||||
self.signals.progress.connect(self._progress)
|
||||
self.block_combo = QComboBox()
|
||||
self.block_combo.addItems(list(names))
|
||||
self.source_edit = QLineEdit()
|
||||
self.source_edit.setPlaceholderText('选择同名 CBB 完整备份中的工程 .prj')
|
||||
self.browse_button = QPushButton('选择…')
|
||||
self.browse_button.clicked.connect(self._browse)
|
||||
self.restore_button = QPushButton('恢复并校验')
|
||||
self.restore_button.clicked.connect(self._restore)
|
||||
self.close_button = QPushButton('关闭')
|
||||
self.close_button.clicked.connect(self.reject)
|
||||
self.status_label = QLabel('使用所选备份版本恢复同名 CBB。先在库副本中验证,原工程与完整库副本会保留;需要额外磁盘空间。')
|
||||
self.status_label.setWordWrap(True)
|
||||
layout = QFormLayout(self)
|
||||
layout.addRow('目标 CBB', self.block_combo)
|
||||
source = QHBoxLayout()
|
||||
source.addWidget(self.source_edit)
|
||||
source.addWidget(self.browse_button)
|
||||
layout.addRow('备份工程', source)
|
||||
layout.addRow(self.status_label)
|
||||
buttons = QHBoxLayout()
|
||||
buttons.addStretch()
|
||||
buttons.addWidget(self.restore_button)
|
||||
buttons.addWidget(self.close_button)
|
||||
layout.addRow(buttons)
|
||||
|
||||
def _browse(self):
|
||||
path, _ = QFileDialog.getOpenFileName(self, '选择健康备份工程', '', '工程 (*.prj)')
|
||||
if path:
|
||||
self.source_edit.setText(path)
|
||||
|
||||
def _restore(self):
|
||||
if self.busy:
|
||||
return
|
||||
source = Path(self.source_edit.text().strip())
|
||||
if not source.is_file() or source.suffix.casefold() != '.prj':
|
||||
self.status_label.setText('请选择有效的备份工程 .prj 文件。')
|
||||
return
|
||||
name = self.block_combo.currentText()
|
||||
if not name:
|
||||
self.status_label.setText('请选择目标 CBB。')
|
||||
return
|
||||
try:
|
||||
request = self.request_factory(name)
|
||||
recoverer = self.recoverer
|
||||
if recoverer is None:
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
recoverer = recover_from_backup
|
||||
except Exception as error:
|
||||
self.status_label.setText(str(error))
|
||||
return
|
||||
self.busy = True
|
||||
self._set_enabled(False)
|
||||
self.status_label.setText('正在准备恢复…')
|
||||
self.started.emit(name)
|
||||
self.worker = ServiceWorker(lambda: recoverer(
|
||||
request, source, progress=self.signals.progress.emit))
|
||||
self.worker.signals.succeeded.connect(lambda result: self._result(name, result))
|
||||
self.worker.signals.failed.connect(lambda error: self._failed(name, error))
|
||||
self.worker.signals.finished.connect(self._finished)
|
||||
self.pool.start(self.worker)
|
||||
|
||||
def _set_enabled(self, enabled):
|
||||
for widget in (self.block_combo, self.source_edit, self.browse_button,
|
||||
self.restore_button, self.close_button):
|
||||
widget.setEnabled(enabled)
|
||||
|
||||
def _progress(self, event):
|
||||
if isinstance(event, dict):
|
||||
message = event.get('message') or event.get('detail')
|
||||
if message:
|
||||
self.status_label.setText(str(message))
|
||||
|
||||
def _result(self, name, result):
|
||||
if result.status == 'PASS':
|
||||
message = '恢复完成,工程打开与校验通过。'
|
||||
else:
|
||||
message = f'恢复未通过:{result.stderr or result.stdout or result.status}'
|
||||
if result.evidence_path:
|
||||
message += f'\n记录:{result.evidence_path}'
|
||||
self.status_label.setText(message)
|
||||
self.completed.emit(name, result)
|
||||
|
||||
def _failed(self, name, error):
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
self._result(name, VerifyResult('FAIL', stderr=f'恢复未完成:{error}', safe_to_continue=False))
|
||||
|
||||
def _finished(self):
|
||||
self.busy = False
|
||||
self._set_enabled(True)
|
||||
|
||||
def reject(self):
|
||||
if not self.busy:
|
||||
super().reject()
|
||||
|
||||
def closeEvent(self, event):
|
||||
if self.busy:
|
||||
event.ignore()
|
||||
else:
|
||||
super().closeEvent(event)
|
||||
@@ -8,7 +8,7 @@ from PySide6.QtCore import QObject, QThreadPool, Signal, Qt
|
||||
from PySide6.QtGui import QColor, QIcon
|
||||
from PySide6.QtWidgets import (QWidget, QLineEdit, QPushButton, QFormLayout,
|
||||
QTextEdit, QFileDialog, QHBoxLayout, QLabel, QSpinBox, QMessageBox, QToolButton,
|
||||
QTableWidget, QTableWidgetItem, QHeaderView, QCheckBox)
|
||||
QTableWidget, QTableWidgetItem, QHeaderView, QCheckBox, QGroupBox, QVBoxLayout, QProgressBar)
|
||||
from PySide6.QtWidgets import QStyle
|
||||
try:
|
||||
from dx_productivity.trial_task_center.ui.workers import ServiceWorker
|
||||
@@ -79,6 +79,8 @@ class MainWindow(QWidget):
|
||||
self.busy = False; self.loaded_path = None; self.stop = Event(); self.ready_names = set()
|
||||
self._row_by_name = {}
|
||||
self.skipped_names = set(); self.skip_reasons = {}
|
||||
self._global_icdb_run = False
|
||||
self._icdb_completed = 0
|
||||
self.signals = ProgressSignals(self); self.signals.event.connect(self._progress)
|
||||
self.lmc_edit = QLineEdit()
|
||||
self.libspec_edit = QLineEdit(os.environ.get('CBB_LIBSPEC', 'EE7.9'))
|
||||
@@ -112,6 +114,10 @@ class MainWindow(QWidget):
|
||||
self.browse_button = QPushButton('选择'); self.browse_button.clicked.connect(self.choose_library)
|
||||
self.selected_batch_button = QPushButton('批量校验已选'); self.selected_batch_button.clicked.connect(self.verify_selected)
|
||||
self.batch_button = QPushButton('批量校验全部 CBB'); self.batch_button.clicked.connect(self.verify_all)
|
||||
self.icdb_repair_button = QPushButton('全库 ICDB 检查与修复'); self.icdb_repair_button.clicked.connect(self.repair_icdb_all)
|
||||
self.icdb_repair_button.setToolTip('重新检查所有可用 CBB,包含 Ready 项;确认工程数据库失联后备份并尝试原生修复。')
|
||||
self.backup_restore_button = QPushButton('从备份恢复 CBB…')
|
||||
self.backup_restore_button.clicked.connect(self.restore_backup)
|
||||
self.stop_button = QPushButton('当前项完成后停止'); self.stop_button.clicked.connect(self.request_stop)
|
||||
self.logs_button = QPushButton('打开日志目录'); self.logs_button.clicked.connect(self.open_logs)
|
||||
self.recovery_button = QPushButton('打开异常证据'); self.recovery_button.setEnabled(False)
|
||||
@@ -120,6 +126,7 @@ class MainWindow(QWidget):
|
||||
self.browse_button.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_DirOpenIcon))
|
||||
self.selected_batch_button.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_ArrowRight))
|
||||
self.batch_button.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_MediaPlay))
|
||||
self.icdb_repair_button.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_BrowserReload))
|
||||
self.stop_button.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_MediaStop))
|
||||
self.logs_button.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_DirOpenIcon))
|
||||
self.recovery_button.setIcon(style.standardIcon(QStyle.StandardPixmap.SP_MessageBoxWarning))
|
||||
@@ -128,6 +135,7 @@ class MainWindow(QWidget):
|
||||
self.recovery_path = None
|
||||
self.status_label = QLabel('请选择并读取中心库。校验期间可以使用其他前台应用。')
|
||||
self.status_label.setWordWrap(True)
|
||||
self.status_label.setMinimumHeight(self.status_label.fontMetrics().lineSpacing()*2)
|
||||
self.output = QTextEdit(); self.output.setReadOnly(True)
|
||||
# Integration emits repeated status snapshots. Keep the visible
|
||||
# diagnostic buffer bounded so a 3,000-CBB run cannot grow the GUI
|
||||
@@ -141,7 +149,25 @@ class MainWindow(QWidget):
|
||||
'填写 LibSpec 与 DMS 登录配置后,会先执行一次 ReuseBlockImporter 内容检查。'))
|
||||
controls = QHBoxLayout()
|
||||
for widget in (self.selected_batch_button, self.batch_button, self.stop_button, self.logs_button): controls.addWidget(widget)
|
||||
layout.addRow(controls); layout.addRow(self.status_label); layout.addRow(self.output)
|
||||
layout.addRow(controls)
|
||||
maintenance = QGroupBox('数据库维护')
|
||||
maintenance_layout = QVBoxLayout(maintenance)
|
||||
maintenance_row = QHBoxLayout()
|
||||
maintenance_row.addWidget(self.icdb_repair_button)
|
||||
description = QLabel('包含 Ready 项,逐个重新校验。确认工程数据库失联后,先备份再修复。\n共享符号库需另行处理;无法确认会话安全时停止。')
|
||||
description.setWordWrap(True)
|
||||
maintenance_row.addWidget(description, 1)
|
||||
maintenance_layout.addLayout(maintenance_row)
|
||||
maintenance_layout.addWidget(self.backup_restore_button)
|
||||
self.icdb_progress = QProgressBar()
|
||||
self.icdb_progress.setVisible(False)
|
||||
self.icdb_phase = QLabel('')
|
||||
self.icdb_phase.setWordWrap(True)
|
||||
self.icdb_phase.setVisible(False)
|
||||
maintenance_layout.addWidget(self.icdb_progress)
|
||||
maintenance_layout.addWidget(self.icdb_phase)
|
||||
layout.addRow(maintenance)
|
||||
layout.addRow(self.status_label); layout.addRow(self.output)
|
||||
layout.addRow(self.recovery_button)
|
||||
self.lmc_edit.textChanged.connect(self._invalidate)
|
||||
for field in (self.libspec_edit, self.dms_edit, self.mentor_bin_edit):
|
||||
@@ -154,6 +180,8 @@ class MainWindow(QWidget):
|
||||
self._release_library_lease()
|
||||
self.loaded_path = None; self.mentor_lmc_path = None; self.cbb_table.setRowCount(0); self._row_by_name = {}
|
||||
self.ready_names = set(); self.skipped_names = set(); self.skip_reasons = {}; self._update()
|
||||
self._global_icdb_run = False
|
||||
self.icdb_progress.hide(); self.icdb_phase.hide()
|
||||
|
||||
def _release_library_lease(self):
|
||||
lease, self.library_lease = self.library_lease, None
|
||||
@@ -173,6 +201,10 @@ class MainWindow(QWidget):
|
||||
and self.cbb_table.item(row, 0).text() not in self.skipped_names
|
||||
for row in range(self.cbb_table.rowCount())))
|
||||
self.selected_batch_button.setEnabled(ready and bool(self._selected_names()))
|
||||
self.icdb_repair_button.setEnabled(ready and any(
|
||||
self.cbb_table.item(row, 0).text() not in self.skipped_names
|
||||
for row in range(self.cbb_table.rowCount())))
|
||||
self.backup_restore_button.setEnabled(ready)
|
||||
for widget in (self.lmc_edit, self.browse_button, self.load_button, self.cbb_table,
|
||||
self.timeout_spin, self.advanced_toggle, self.advanced_panel):
|
||||
widget.setEnabled(not self.busy)
|
||||
@@ -192,6 +224,8 @@ class MainWindow(QWidget):
|
||||
|
||||
def load_library(self):
|
||||
if self.busy or not self.lmc_edit.text(): return
|
||||
self._global_icdb_run = False
|
||||
self.icdb_progress.hide(); self.icdb_phase.hide()
|
||||
self._release_library_lease()
|
||||
self.loaded_path = None; self.cbb_table.setRowCount(0); self._row_by_name = {}; self.ready_names = set()
|
||||
self.skipped_names = set(); self.skip_reasons = {}
|
||||
@@ -223,10 +257,9 @@ class MainWindow(QWidget):
|
||||
skip_reasons = {}
|
||||
if not result.error:
|
||||
for record in result.records:
|
||||
if record.status != 'READY':
|
||||
reason = preflight_skip_reason(mentor_path, record.name)
|
||||
if reason:
|
||||
skip_reasons[record.name] = reason
|
||||
reason = preflight_skip_reason(mentor_path, record.name)
|
||||
if reason:
|
||||
skip_reasons[record.name] = reason
|
||||
return result, skip_reasons, lease, mentor_path
|
||||
except Exception:
|
||||
if 'lease' in locals():
|
||||
@@ -245,7 +278,7 @@ class MainWindow(QWidget):
|
||||
self.mentor_lmc_path = mentor_path
|
||||
self.loaded_path = path
|
||||
self.cbb_table.setRowCount(0)
|
||||
self.ready_names = {r.name for r in result.records if r.status == 'READY'}
|
||||
self.ready_names = {r.name for r in result.records if r.status == 'READY' and r.name not in skip_reasons}
|
||||
self.skip_reasons = dict(skip_reasons)
|
||||
self.skipped_names = set(skip_reasons)
|
||||
# Populating a large library emits itemChanged for every cell.
|
||||
@@ -274,7 +307,7 @@ class MainWindow(QWidget):
|
||||
# synchronously in this GUI callback, making large libraries
|
||||
# appear frozen. The verify worker performs it when queued.
|
||||
self.cbb_table.setItem(row, 3, QTableWidgetItem('未检查'))
|
||||
if record.status == 'READY':
|
||||
if record.status == 'READY' and record.name not in self.skipped_names:
|
||||
self._mark_row_ready(row)
|
||||
elif record.name in self.skipped_names:
|
||||
self.cbb_table.item(row, 0).setToolTip(self.skip_reasons[record.name])
|
||||
@@ -326,24 +359,97 @@ class MainWindow(QWidget):
|
||||
names = [self.cbb_table.item(row, 0).text() for row in range(self.cbb_table.rowCount())]
|
||||
self._run([name for name in names if name not in self.ready_names and name not in self.skipped_names])
|
||||
|
||||
def _run(self, names):
|
||||
def repair_icdb_all(self):
|
||||
if self.busy or not self.loaded_path: return
|
||||
names = [name for name in names if name not in self.ready_names and name not in self.skipped_names]
|
||||
names = [self.cbb_table.item(row, 0).text() for row in range(self.cbb_table.rowCount())
|
||||
if self.cbb_table.item(row, 0).text() not in self.skipped_names]
|
||||
if not names: return
|
||||
answer = QMessageBox.question(self, '全库 ICDB 检查与修复',
|
||||
f'当前中心库:{self.loaded_path}\n\n'
|
||||
f'将检查 {len(names)} 个 CBB,其中 Ready {len(self.ready_names)} 个;'
|
||||
f'缺少工程文件跳过 {len(self.skipped_names)} 个。\n'
|
||||
'操作会重新校验并保存工程。确认失联问题后,完整备份目标工程并尝试一次原生修复。\n'
|
||||
'共享符号库不支持自动修复。若原生工具恢复了旧备份,需人工核对版本;'
|
||||
'会话安全无法确认时停止队列。\n\n是否开始检查与修复?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
|
||||
if answer == QMessageBox.Yes:
|
||||
self._run(names, include_ready=True)
|
||||
|
||||
def restore_backup(self):
|
||||
if self.busy or not self.loaded_path:
|
||||
return
|
||||
from .backup_recovery_dialog import BackupRecoveryDialog
|
||||
names = [self.cbb_table.item(row, 0).text() for row in range(self.cbb_table.rowCount())]
|
||||
if not names:
|
||||
return
|
||||
self.backup_dialog = BackupRecoveryDialog(names, self._request, self)
|
||||
row = self.cbb_table.currentRow()
|
||||
if row >= 0:
|
||||
self.backup_dialog.block_combo.setCurrentText(self.cbb_table.item(row, 0).text())
|
||||
self.backup_dialog.started.connect(self._backup_started)
|
||||
self.backup_dialog.completed.connect(self._backup_completed)
|
||||
self.backup_dialog.finished.connect(lambda _code: self._finished())
|
||||
self.backup_dialog.setModal(True)
|
||||
self.backup_dialog.show()
|
||||
|
||||
def _backup_started(self, name):
|
||||
self.busy = True
|
||||
self._global_icdb_run = False
|
||||
self.ready_names.discard(name)
|
||||
row = self._row_by_name.get(name)
|
||||
if row is not None:
|
||||
self.cbb_table.item(row, 2).setText('恢复中 / 需确认')
|
||||
self.cbb_table.item(row, 0).setFlags(
|
||||
self.cbb_table.item(row, 0).flags() | Qt.ItemIsUserCheckable)
|
||||
for column in range(self.cbb_table.columnCount()):
|
||||
self.cbb_table.item(row, column).setBackground(QColor('#FFF8E1'))
|
||||
self._update()
|
||||
self.stop_button.setEnabled(False)
|
||||
|
||||
def _backup_completed(self, name, result):
|
||||
if not result.safe_to_continue and self.library_lease is not None:
|
||||
self.library_lease.retain_for_recovery()
|
||||
row = self._row_by_name.get(name)
|
||||
if row is not None and result.status != 'PASS':
|
||||
self.cbb_table.item(row, 2).setText('未知 / 需确认')
|
||||
self._progress({'block': name, 'stage': result.status, 'detail': result.stderr,
|
||||
'evidence': str(result.evidence_path or '')})
|
||||
|
||||
def _run(self, names, *, include_ready=False):
|
||||
if self.busy or not self.loaded_path: return
|
||||
names = [name for name in names if (include_ready or name not in self.ready_names) and name not in self.skipped_names]
|
||||
if not names:
|
||||
self.status_label.setText('没有需要校验的 CBB(全部已经是绿色 / Ready)。')
|
||||
self._update()
|
||||
return
|
||||
requests = [self._request(name) for name in names]
|
||||
self._global_icdb_run = include_ready
|
||||
self._icdb_completed = 0
|
||||
self.icdb_progress.setVisible(include_ready)
|
||||
self.icdb_phase.setVisible(include_ready)
|
||||
if include_ready:
|
||||
self.icdb_progress.setRange(0, len(requests))
|
||||
self.icdb_progress.setValue(0)
|
||||
self.icdb_progress.setFormat('已处理 %v / %m 个 CBB')
|
||||
self.icdb_phase.setText('准备检查…')
|
||||
self.stop.clear(); self.recovery_path = None; self.recovery_button.setEnabled(False)
|
||||
self.output.clear(); self.status_label.setText(f'正在校验:共 {len(names)} 个 CBB')
|
||||
self.output.clear(); self.status_label.setText(
|
||||
f'正在全库 ICDB 检查与修复:共 {len(names)} 个 CBB' if include_ready
|
||||
else f'正在校验:共 {len(names)} 个 CBB')
|
||||
def action():
|
||||
from cbb_refresh_verify.verify_runner import read_library_flag_if_available, run_verify
|
||||
verifier = self.verifier or (lambda req: run_verify(
|
||||
verify = self.verifier or (lambda req: run_verify(
|
||||
req, progress=lambda e: self.signals.event.emit(e),
|
||||
flag_reader=read_library_flag_if_available))
|
||||
def verifier(req):
|
||||
if include_ready:
|
||||
self.signals.event.emit({'block': req.block_name, 'stage': 'icdb_check_start'})
|
||||
return verify(req)
|
||||
results = run_batch(requests, verifier=verifier, stop=self.stop,
|
||||
progress=lambda name, result: self.signals.event.emit({'block': name, 'stage': result.status, 'detail': result.stderr, 'evidence': str(result.evidence_path or '')}))
|
||||
report = write_batch_report(requests, results, self.data_root/'reports')
|
||||
if include_ready:
|
||||
from cbb_refresh_verify.icdb_maintenance import write_maintenance_report
|
||||
write_maintenance_report(requests, results, report, self.skipped_names)
|
||||
return results, report
|
||||
self._start(action, self._batch_done)
|
||||
|
||||
@@ -353,6 +459,18 @@ class MainWindow(QWidget):
|
||||
if event.get(key): line += f' | {event[key]}'
|
||||
self.output.append(line)
|
||||
block = event.get('block'); stage = event.get('stage')
|
||||
if self._global_icdb_run:
|
||||
if stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED', 'QUEUE_STOP'}:
|
||||
self._icdb_completed += 1
|
||||
self.icdb_progress.setValue(self._icdb_completed)
|
||||
phases = {'icdb_check_start': '检查工程数据库与校验状态',
|
||||
'native_repair_start': '准备完整备份与原生修复',
|
||||
'complete_backup_verified': '完整备份已校验',
|
||||
'native_repair_requested': '正在原生修复',
|
||||
'native_repair_confirmed': '正在原生修复',
|
||||
'native_repair_finished': '原生修复已结束,正在核对结果'}
|
||||
if stage in phases:
|
||||
self.icdb_phase.setText(f'{block or ""} · {phases[stage]}')
|
||||
if block and stage in {'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT', 'SKIPPED'}:
|
||||
row = self._row_by_name.get(block)
|
||||
if row is not None:
|
||||
@@ -362,6 +480,13 @@ class MainWindow(QWidget):
|
||||
self.ready_names.add(block)
|
||||
self._mark_row_ready(row)
|
||||
else:
|
||||
if getattr(self, '_global_icdb_run', False) and stage != 'SKIPPED':
|
||||
self.ready_names.discard(block)
|
||||
self.cbb_table.item(row, 2).setText('未知 / 需确认')
|
||||
item = self.cbb_table.item(row, 0)
|
||||
item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
|
||||
for column in range(self.cbb_table.columnCount()):
|
||||
self.cbb_table.item(row, column).setBackground(QColor('#FFF8E1'))
|
||||
detail = str(event.get('detail') or event.get('error') or '').strip()
|
||||
label = str(stage) if not detail else f'{stage}: {detail[:96]}'
|
||||
self.cbb_table.item(row, 3).setText(label)
|
||||
@@ -383,6 +508,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,9 +519,25 @@ 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)}')
|
||||
self.status_label.setText(text); self.output.append(text)
|
||||
if self._global_icdb_run:
|
||||
from cbb_refresh_verify.icdb_maintenance import maintenance_counts
|
||||
counts = maintenance_counts(results)
|
||||
text = ('全库 ICDB 检查已结束\n'
|
||||
f'校验通过 {passed},失败/受阻 {failed},队列未执行 {skipped},预检跳过 {len(self.skipped_names)}\n'
|
||||
f'原生修复完成 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},'
|
||||
f'修复未确认 {counts["repair_unconfirmed"]}\n'
|
||||
f'完整日志:{report}\n仅失败日志:{failure_report_path(report)}')
|
||||
self.icdb_phase.setText('已停止,存在待处理项目' if failed or skipped else '已完成全库检查')
|
||||
if any(not result.safe_to_continue for result in results):
|
||||
text += '\n未确认数据库或会话安全,已停止剩余校验。请查看恢复现场。'
|
||||
status_text = text
|
||||
if self._global_icdb_run:
|
||||
status_text = (f'全库检查结束:通过 {passed},失败/受阻 {failed},未执行 {skipped}。\n'
|
||||
f'原生修复 {counts["repaired"]},恢复备份待复核 {counts["review_required"]},'
|
||||
f'修复未确认 {counts["repair_unconfirmed"]}。')
|
||||
self.status_label.setText(status_text); self.output.append(text)
|
||||
self.summary = QMessageBox(self)
|
||||
self.summary.setWindowTitle('CBB 校验结果'); self.summary.setText(text)
|
||||
self.summary.setWindowTitle('全库 ICDB 检查与修复结果' if self._global_icdb_run else 'CBB 校验结果'); self.summary.setText(text)
|
||||
self.summary.setIcon(QMessageBox.Warning if failed else QMessageBox.Information)
|
||||
# Modeless notification: do not force activation or steal keyboard focus.
|
||||
from PySide6.QtCore import Qt
|
||||
@@ -405,6 +548,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)
|
||||
|
||||
|
||||
@@ -113,6 +113,8 @@ def collect_native_failure_logs(pcb_path, evidence_root, *, max_bytes=NATIVE_LOG
|
||||
target = destination/(label+'-'+source.name)
|
||||
target.write_bytes(data)
|
||||
item.update(evidence=str(target), copied_bytes=len(data))
|
||||
if label == 'pcb-logfiles' and source.name != 'ReusableBlock.txt':
|
||||
_collect_icdb_recovery(source, evidence_root)
|
||||
except FileNotFoundError:
|
||||
item['exists'] = False
|
||||
except OSError as error:
|
||||
@@ -120,6 +122,23 @@ def collect_native_failure_logs(pcb_path, evidence_root, *, max_bytes=NATIVE_LOG
|
||||
return _write_record(destination.parent/'native-logs.json', record)
|
||||
|
||||
|
||||
def _collect_icdb_recovery(source, evidence_root):
|
||||
"""Only promote a native error whose log changed after this launch."""
|
||||
from .icdb_recovery import read_log_failure
|
||||
root = Path(evidence_root)/'diagnostics'
|
||||
try:
|
||||
baseline = json.loads((root/'startup-log-baseline.json').read_text('utf-8'))
|
||||
checkpoint = (baseline['baseline'] if baseline['source'] == str(source) else
|
||||
baseline.get('integration_logs', {}).get(str(source)))
|
||||
if checkpoint is None:
|
||||
return
|
||||
_, recovery = read_log_failure(source, checkpoint)
|
||||
if recovery:
|
||||
_write_record(root/'icdb-recovery.json', {**recovery, 'source': str(source)})
|
||||
except (OSError, ValueError, KeyError, TypeError):
|
||||
pass # Unknown freshness must not replace the original failure.
|
||||
|
||||
|
||||
|
||||
def failure_logs_main(request_path):
|
||||
job = json.loads(Path(request_path).read_text(encoding='utf-8'))
|
||||
|
||||
@@ -62,6 +62,7 @@ class VerifyRequest:
|
||||
# it before launching Library Manager/PCB.
|
||||
mapped_drive: str = ''
|
||||
mapped_remote_root: str = ''
|
||||
auto_repair: bool = True
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -80,6 +81,7 @@ class VerifyResult:
|
||||
bridge_attempts: tuple[dict, ...] = ()
|
||||
timings_seconds: dict[str, float] = field(default_factory=dict)
|
||||
incomplete_timing_phases: tuple[str, ...] = ()
|
||||
recovery_attempts: tuple[dict, ...] = ()
|
||||
|
||||
|
||||
def resolve_importer(mentor_bin: str | None = None) -> Path:
|
||||
@@ -127,7 +129,10 @@ 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):
|
||||
from .icdb_recovery import parse_session_loss
|
||||
if parse_session_loss(output):
|
||||
return 'FAIL'
|
||||
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 +161,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
|
||||
@@ -174,7 +187,7 @@ def preflight_skip_reason(lmc_file, block_name):
|
||||
return ''
|
||||
work = library.parent / 'ReusableBlocksLP' / str(block_name) / 'Work'
|
||||
if not work.is_dir():
|
||||
return ''
|
||||
return f'{block_name}: 缺少 Work 工程目录'
|
||||
request = VerifyRequest(library, str(block_name), library.parent / '.cbb-preflight-evidence')
|
||||
try:
|
||||
preflight(request)
|
||||
@@ -187,38 +200,18 @@ def preflight_skip_reason(lmc_file, block_name):
|
||||
return ''
|
||||
|
||||
|
||||
def _run_process(command, timeout_seconds, cwd):
|
||||
"""Run the importer, draining both pipes while it works (no pipe deadlock)."""
|
||||
process = subprocess.Popen(command, cwd=str(cwd), stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
try:
|
||||
stdout, stderr = process.communicate(timeout=timeout_seconds)
|
||||
return process.returncode, stdout.decode('utf-8', errors='replace'), stderr.decode('utf-8', errors='replace'), False
|
||||
except subprocess.TimeoutExpired:
|
||||
# A timed-out importer may have spawned a child that still owns the
|
||||
# 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'],
|
||||
capture_output=True, timeout=5,
|
||||
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
pass
|
||||
try:
|
||||
process.kill()
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
stdout, stderr = process.communicate(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
stdout, stderr = b'', b''
|
||||
return None, stdout.decode('utf-8', errors='replace'), stderr.decode('utf-8', errors='replace'), True
|
||||
def _run_process(command, timeout_seconds, cwd, *, evidence_root=None):
|
||||
from .content_check import run_content_check
|
||||
return run_content_check(command, timeout_seconds, cwd, evidence_root=evidence_root)
|
||||
|
||||
|
||||
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()
|
||||
if hasattr(process, 'request_stop'):
|
||||
process.request_stop()
|
||||
else:
|
||||
process.kill()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
@@ -269,6 +262,13 @@ def read_library_flag_with_retry(reader, library, block_name, *, attempts=3,
|
||||
def classify_failure_category(detail):
|
||||
text = str(detail or '')
|
||||
lower = text.casefold()
|
||||
from .icdb_recovery import ERROR_CODE, parse_session_loss
|
||||
if ERROR_CODE.casefold() in lower or parse_session_loss(text):
|
||||
return ERROR_CODE
|
||||
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 +472,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 +491,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 +503,52 @@ 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)
|
||||
result = _run_verify_serialized(
|
||||
request, progress=progress, flag_reader=flag_reader,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
dialog_supervisor_factory=dialog_supervisor_factory)
|
||||
if (request.auto_repair and result.status not in {'TIMEOUT', 'BLOCKED'}
|
||||
and result.error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
and result.evidence_path):
|
||||
from .native_repair import attempt_recovery
|
||||
try:
|
||||
diagnosis = json.loads((result.evidence_path.parent/'diagnostics/icdb-recovery.json').read_text('utf-8'))
|
||||
original = result
|
||||
recovery = attempt_recovery(request, diagnosis, result.evidence_path.parent, progress=progress)
|
||||
recovery = {**recovery, 'original_error': original.stderr,
|
||||
'original_evidence': str(original.evidence_path)}
|
||||
if recovery.get('status') == 'REPAIRED':
|
||||
result = _run_verify_serialized(
|
||||
replace(request, auto_repair=False), progress=progress, flag_reader=flag_reader,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
dialog_supervisor_factory=dialog_supervisor_factory)
|
||||
result = replace(result, recovery_attempts=(recovery,))
|
||||
result.evidence_path.write_text(json.dumps({'block_name': request.block_name, **asdict(result)},
|
||||
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
except Exception as error:
|
||||
result = replace(result, status='FAIL' if result.status == 'PASS' else result.status,
|
||||
safe_to_continue=False,
|
||||
stderr=(result.stderr+f'\nREPAIR_UNCONFIRMED: {error}').strip())
|
||||
return result
|
||||
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,
|
||||
@@ -525,6 +565,31 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
pass
|
||||
finally:
|
||||
timing.end('failure_diagnostics')
|
||||
if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'}:
|
||||
from .icdb_recovery import ERROR_CODE, RECOVERY_ACTION, parse_session_loss, recovery_detail
|
||||
recovery = parse_session_loss(result.stderr)
|
||||
if recovery is None and result.evidence_path:
|
||||
try:
|
||||
saved = json.loads(result.evidence_path.with_name('diagnostics').joinpath(
|
||||
'icdb-recovery.json').read_text('utf-8'))
|
||||
recovery = parse_session_loss(saved.get('native_message', ''))
|
||||
except (OSError, ValueError, AttributeError):
|
||||
pass
|
||||
if recovery or ERROR_CODE.casefold() in result.stderr.casefold():
|
||||
detail = result.stderr
|
||||
if recovery and RECOVERY_ACTION not in detail:
|
||||
detail = (detail + '\n' + recovery_detail(recovery)).strip()
|
||||
result = replace(result, safe_to_continue=False, stderr=detail,
|
||||
error_category=ERROR_CODE)
|
||||
if recovery and result.evidence_path:
|
||||
try:
|
||||
record_path = result.evidence_path.parent/'diagnostics/icdb-recovery.json'
|
||||
record_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if not record_path.is_file():
|
||||
record_path.write_text(json.dumps(recovery, ensure_ascii=False, indent=2),
|
||||
encoding='utf-8')
|
||||
except OSError:
|
||||
pass
|
||||
durations, unfinished = timing.snapshot()
|
||||
result = replace(result, timings_seconds=durations, incomplete_timing_phases=unfinished,
|
||||
error_category=(result.error_category or classify_failure_category(result.stderr)
|
||||
@@ -570,15 +635,16 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
progress({'stage': 'runtime_fingerprint', 'block': request.block_name,
|
||||
**fingerprint})
|
||||
result_path = root/'result.json'
|
||||
runner = process_runner or _run_process
|
||||
runner = process_runner or (lambda command, timeout, cwd:
|
||||
_run_process(command, timeout, cwd, evidence_root=root))
|
||||
|
||||
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
|
||||
@@ -592,6 +658,17 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
finally:
|
||||
timing.end('preflight')
|
||||
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
try:
|
||||
addresses = unreleased_addresses(request.lmc_file, project, pcb)
|
||||
if addresses:
|
||||
return finish(VerifyResult('BLOCKED', safe_to_continue=False,
|
||||
stderr='ICDB_SESSION_UNCONFIRMED: 原生数据库所有权未释放,未开始内容检查或校验:'
|
||||
+ '; '.join(addresses)))
|
||||
except OSError as error:
|
||||
return finish(VerifyResult('BLOCKED', safe_to_continue=False,
|
||||
stderr=f'ICDB_SESSION_UNCONFIRMED: 无法确认数据库所有权:{error}'))
|
||||
|
||||
content_check_enabled = bool(str(request.libspec).strip() and str(request.dms_login_config).strip())
|
||||
if content_check_enabled:
|
||||
try:
|
||||
@@ -619,7 +696,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
# A contents failure or an unusable DMS must block before touching the PCB.
|
||||
return finish(VerifyResult(status if status in ('FAIL', 'TIMEOUT') else 'BLOCKED',
|
||||
stderr=combined.strip() or f'内容检查结果未知(退出码 {exit_code})',
|
||||
stdout=stdout, report_path=report, content_check_exit_code=exit_code))
|
||||
stdout=stdout, report_path=report, content_check_exit_code=exit_code,
|
||||
safe_to_continue=not timed_out))
|
||||
if progress:
|
||||
progress({'stage': 'content_check_done', 'block': request.block_name, 'status': 'PASS'})
|
||||
|
||||
@@ -635,6 +713,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 ''),
|
||||
@@ -642,6 +721,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
'interactive_input': bool(request.interactive_input),
|
||||
'native_lm_verify': bool(request.native_lm_verify),
|
||||
'desktop': 'CbbVerify_' + uuid4().hex[:12],
|
||||
'cancel_signal': str(root/'cancel-worker.signal'),
|
||||
'start_gate': str(root/'start-worker.signal')}
|
||||
job_path = root/'request.json'
|
||||
job_path.write_text(json.dumps(job, ensure_ascii=False), encoding='utf-8')
|
||||
@@ -672,6 +752,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
worker_command(job_path), None, interactive_input=True)
|
||||
else:
|
||||
process = DesktopWorker(worker_command(job_path), job['desktop'])
|
||||
if hasattr(process, 'bind_cancellation'):
|
||||
process.bind_cancellation(job['cancel_signal'])
|
||||
deadline = ActivityDeadline(request.timeout_seconds)
|
||||
last_activity_key = None
|
||||
phase = DialogPhase.PCB_STARTING
|
||||
@@ -789,11 +871,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 +899,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 +914,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 +935,31 @@ 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:
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
addresses = unreleased_addresses(request.lmc_file, project, pcb)
|
||||
if addresses:
|
||||
release_result = {**(release_result or {}), 'safe_to_continue': False,
|
||||
'icdb_address': addresses[0], 'icdb_addresses': addresses}
|
||||
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 +1001,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 +1015,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
|
||||
|
||||
@@ -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).'
|
||||
@@ -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()
|
||||
@@ -0,0 +1,190 @@
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult
|
||||
|
||||
|
||||
def project(work, name, data):
|
||||
(work/'database').mkdir(parents=True)
|
||||
(work/'database/icdb.dat').write_bytes(data)
|
||||
(work/'PCB/Layout').mkdir(parents=True)
|
||||
(work/'PCB/Layout/LayoutDB.lyt').write_bytes(data)
|
||||
(work/'PCB'/f'{name}.pcb').write_text(f'SECTION Integration\nKEY ProjectFile "..\\{name}.prj"\nENDSECTION\n')
|
||||
(work/'PCB/pcb.prj').write_text('SECTION Project\nKEY CentralLibrary "old.lmc"\nENDSECTION\n')
|
||||
path = work/f'{name}.prj'
|
||||
path.write_text(f'SECTION Board1\nKEY RootBlock "{name}"\nKEY PCBDesignPath "PCB\\{name}.pcb"\nENDSECTION\n'
|
||||
'SECTION DesignInfo\nKEY CentralLibrary "old.lmc"\nENDSECTION\n'
|
||||
'SECTION iCDB\nKEY iCDBDir ".\\database"\nENDSECTION\n')
|
||||
return path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def recovery_files(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(Path, 'home', lambda: tmp_path/'profile')
|
||||
library = tmp_path/'target/target.lmc'
|
||||
original = project(library.parent/'ReusableBlocksLP/A/Work', 'A', b'damaged')
|
||||
library.write_bytes(b'library')
|
||||
source = project(tmp_path/'backup/Work', 'A', b'healthy')
|
||||
return VerifyRequest(library, 'A', tmp_path/'evidence'), original, source
|
||||
|
||||
|
||||
def test_restore_checks_source_in_library_copy_then_opens_actual_target(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
pcb_project = source.parent/'PCB/pcb.prj'
|
||||
pcb_project.write_text(pcb_project.read_text() +
|
||||
'SECTION SecondNativeConfiguration\nKEY CentralLibrary "old.lmc"\nENDSECTION\n')
|
||||
before = source.read_bytes()
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
work = req.lmc_file.parent/'ReusableBlocksLP/A/Work'
|
||||
assert (work/'database/icdb.dat').read_bytes() == b'healthy'
|
||||
if calls:
|
||||
assert req.lmc_file == request.lmc_file
|
||||
else:
|
||||
assert req.lmc_file != request.lmc_file
|
||||
assert not req.lmc_file.is_relative_to(request.evidence_dir)
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
calls.append(req)
|
||||
assert not req.auto_repair
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status == 'PASS'
|
||||
assert len(calls) == 2
|
||||
assert source.read_bytes() == before
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'healthy'
|
||||
retained = list(original.parent.parent.glob('Work.before-restore-*'))
|
||||
assert len(retained) == 1
|
||||
assert (retained[0]/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
assert list(request.evidence_dir.rglob('library-before/target.lmc'))
|
||||
|
||||
|
||||
def test_invalid_source_does_not_touch_target(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
source.write_text(source.read_text().replace('RootBlock "A"', 'RootBlock "OTHER"'))
|
||||
result = recover_from_backup(request, source, verifier=lambda *_: pytest.fail('must not verify'),
|
||||
idle_check=lambda *_: None)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert '同名' in result.stderr
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
|
||||
|
||||
def test_unusable_backup_never_replaces_target(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
result = recover_from_backup(request, source, verifier=lambda *a, **k:
|
||||
VerifyResult('FAIL', stderr='ICDB cannot load'), idle_check=lambda *_: None)
|
||||
assert result.status == 'FAIL'
|
||||
assert '目标未更改' in result.stderr
|
||||
assert not list(original.parent.parent.glob('Work.before-restore-*'))
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
|
||||
|
||||
def test_final_open_failure_preserves_original_and_does_not_report_pass(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
calls.append(req)
|
||||
return (VerifyResult('PASS', library_flag='GREEN') if len(calls) == 1 else
|
||||
VerifyResult('FAIL', stderr='共享符号库不可用', safe_to_continue=False))
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status == 'FAIL'
|
||||
assert not result.safe_to_continue
|
||||
assert '共享符号库不可用' in result.stderr
|
||||
assert list(original.parent.parent.glob('Work.before-restore-*'))
|
||||
|
||||
|
||||
def test_source_verify_must_confirm_green_and_release(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
result = recover_from_backup(request, source, verifier=lambda *a, **k:
|
||||
VerifyResult('PASS', library_flag='UNKNOWN'), idle_check=lambda *_: None)
|
||||
assert result.status != 'PASS'
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
|
||||
|
||||
def test_external_changes_during_validation_stop_install(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
def verify(*args, **kwargs):
|
||||
(original.parent/'database/icdb.dat').write_bytes(b'another-user')
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status != 'PASS'
|
||||
assert '变化' in result.stderr
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'another-user'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('field', ['ProjectFile', 'iCDBDir'])
|
||||
def test_absolute_source_references_are_rejected_before_native_open(recovery_files, field):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
if field == 'ProjectFile':
|
||||
path = source.parent/'PCB/A.pcb'
|
||||
path.write_text(f'SECTION Integration\nKEY ProjectFile "{source}"\nENDSECTION\n')
|
||||
else:
|
||||
source.write_text(source.read_text().replace('".\\database"', f'"{source.parent / "database"}"'))
|
||||
result = recover_from_backup(request, source, verifier=lambda *a, **k:
|
||||
pytest.fail('must not launch native against source'), idle_check=lambda *_: None)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert '相对' in result.stderr
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
|
||||
|
||||
def test_idle_guard_queries_supported_native_process_names(recovery_files, monkeypatch):
|
||||
import win32com.client
|
||||
from cbb_refresh_verify.backup_recovery import _idle_check
|
||||
request, original, source = recovery_files
|
||||
queries = []
|
||||
class EmptyProcesses:
|
||||
def ExecQuery(self, query):
|
||||
queries.append(query)
|
||||
return []
|
||||
monkeypatch.setattr(win32com.client, 'GetObject', lambda _: EmptyProcesses())
|
||||
_idle_check(request.lmc_file, original, original.parent/'PCB/A.pcb')
|
||||
assert any("Name='viewdraw.exe'" in query for query in queries)
|
||||
|
||||
|
||||
def test_copy_refuses_directory_junction_before_copying(tmp_path):
|
||||
import subprocess
|
||||
from cbb_refresh_verify.backup_recovery import _copy_verified
|
||||
source = tmp_path/'source'; source.mkdir()
|
||||
outside = tmp_path/'outside'; outside.mkdir()
|
||||
(outside/'sentinel').write_bytes(b'do not copy or modify')
|
||||
linked = source/'linked'
|
||||
result = subprocess.run(['cmd', '/c', 'mklink', '/J', str(linked), str(outside)], capture_output=True)
|
||||
assert result.returncode == 0
|
||||
with pytest.raises(ValueError, match='链接'):
|
||||
_copy_verified(source, tmp_path/'destination')
|
||||
assert not (tmp_path/'destination').exists()
|
||||
assert (outside/'sentinel').read_bytes() == b'do not copy or modify'
|
||||
|
||||
|
||||
def test_target_native_verification_keeps_selected_library_alias(recovery_files):
|
||||
import subprocess
|
||||
from dataclasses import replace
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
alias = request.lmc_file.parent.with_name('selected-library')
|
||||
made = subprocess.run(['cmd', '/c', 'mklink', '/J', str(alias), str(request.lmc_file.parent)], capture_output=True)
|
||||
assert made.returncode == 0
|
||||
selected = alias/request.lmc_file.name
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
calls.append(req.lmc_file)
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(replace(request, lmc_file=selected), source,
|
||||
verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status == 'PASS'
|
||||
assert calls[-1] == selected
|
||||
|
||||
|
||||
def test_equivalent_library_reference_preserves_native_project_bytes_and_date(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import _align_library
|
||||
request, original, source = recovery_files
|
||||
original.write_text(original.read_text().replace('old.lmc', '../../../target.lmc'))
|
||||
before = original.read_bytes(), original.stat().st_mtime_ns
|
||||
_align_library(original, original.parent/'PCB/A.pcb', request.lmc_file)
|
||||
assert (original.read_bytes(), original.stat().st_mtime_ns) == before
|
||||
@@ -0,0 +1,90 @@
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult
|
||||
|
||||
|
||||
def test_backup_dialog_requires_a_project_and_passes_selected_target(qtbot, tmp_path):
|
||||
from cbb_refresh_verify.ui.backup_recovery_dialog import BackupRecoveryDialog
|
||||
calls = []
|
||||
def recover(request, source, **kwargs):
|
||||
calls.append((request.block_name, source))
|
||||
return VerifyResult('PASS', stdout='restored')
|
||||
dialog = BackupRecoveryDialog(('A', 'B'),
|
||||
lambda name: VerifyRequest(tmp_path/'target.lmc', name, tmp_path/'evidence'),
|
||||
recoverer=recover)
|
||||
qtbot.addWidget(dialog)
|
||||
dialog.restore_button.click()
|
||||
assert not calls
|
||||
assert '备份' in dialog.status_label.text()
|
||||
source = tmp_path/'B.prj'; source.touch()
|
||||
dialog.block_combo.setCurrentText('B')
|
||||
dialog.source_edit.setText(str(source))
|
||||
dialog.restore_button.click()
|
||||
qtbot.waitUntil(lambda: not dialog.busy)
|
||||
assert calls == [('B', source)]
|
||||
assert '通过' in dialog.status_label.text()
|
||||
|
||||
|
||||
def test_backup_dialog_keeps_ui_responsive_and_cannot_close_during_restore(qtbot, tmp_path):
|
||||
from cbb_refresh_verify.ui.backup_recovery_dialog import BackupRecoveryDialog
|
||||
entered, release = Event(), Event()
|
||||
def recover(*args, **kwargs):
|
||||
entered.set(); release.wait(3)
|
||||
return VerifyResult('FAIL', stderr='共享符号库不可用', safe_to_continue=False)
|
||||
dialog = BackupRecoveryDialog(('A',),
|
||||
lambda name: VerifyRequest(tmp_path/'target.lmc', name, tmp_path/'evidence'),
|
||||
recoverer=recover)
|
||||
qtbot.addWidget(dialog)
|
||||
source = tmp_path/'A.prj'; source.touch()
|
||||
dialog.source_edit.setText(str(source)); dialog.show()
|
||||
dialog.restore_button.click()
|
||||
try:
|
||||
qtbot.waitUntil(entered.is_set)
|
||||
assert not dialog.close_button.isEnabled()
|
||||
dialog.reject()
|
||||
assert dialog.isVisible()
|
||||
finally:
|
||||
release.set()
|
||||
qtbot.waitUntil(lambda: not dialog.busy)
|
||||
assert '共享符号库不可用' in dialog.status_label.text()
|
||||
assert dialog.close_button.isEnabled()
|
||||
|
||||
|
||||
def test_main_window_opens_simple_dialog_and_failure_clears_ready(qtbot, tmp_path):
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
from cbb_refresh_verify.library_reader import CbbRecord, LibraryReadResult
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((
|
||||
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), data_root=tmp_path)
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('lib.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
window.cbb_table.selectRow(0)
|
||||
window.backup_restore_button.click()
|
||||
dialog = window.backup_dialog
|
||||
assert dialog.isVisible()
|
||||
assert dialog.block_combo.currentText() == 'A'
|
||||
dialog.started.emit('A')
|
||||
assert window.busy
|
||||
assert 'A' not in window.ready_names
|
||||
dialog.completed.emit('A', VerifyResult('FAIL', stderr='损坏'))
|
||||
assert window.cbb_table.item(0, 2).text() == '未知 / 需确认'
|
||||
dialog.reject()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
|
||||
|
||||
def test_unhandled_worker_error_notifies_main_window_of_unsafe_result(qtbot, tmp_path):
|
||||
from cbb_refresh_verify.ui.backup_recovery_dialog import BackupRecoveryDialog
|
||||
def recover(*args, **kwargs):
|
||||
raise OSError('磁盘写入失败')
|
||||
dialog = BackupRecoveryDialog(('A',),
|
||||
lambda name: VerifyRequest(tmp_path/'target.lmc', name, tmp_path/'evidence'), recoverer=recover)
|
||||
qtbot.addWidget(dialog)
|
||||
completed = []
|
||||
dialog.completed.connect(lambda name, result: completed.append((name, result)))
|
||||
source = tmp_path/'A.prj'; source.touch()
|
||||
dialog.source_edit.setText(str(source)); dialog.restore_button.click()
|
||||
qtbot.waitUntil(lambda: not dialog.busy)
|
||||
assert len(completed) == 1
|
||||
assert completed[0][0] == 'A'
|
||||
assert not completed[0][1].safe_to_continue
|
||||
@@ -0,0 +1,140 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize('failure, expected', [
|
||||
('desktop', []),
|
||||
('job', ['desktop']),
|
||||
('process', ['job', 'desktop']),
|
||||
('thread', ['thread', 'terminate', 'process', 'job', 'desktop']),
|
||||
])
|
||||
def test_constructor_rolls_back_allocated_resources(monkeypatch, failure, expected):
|
||||
import cbb_refresh_verify.desktop_worker as module
|
||||
|
||||
closed = []
|
||||
error = RuntimeError(failure + ' failed')
|
||||
|
||||
class FakeJob:
|
||||
def Close(self):
|
||||
closed.append('job')
|
||||
|
||||
class FakeDesktop:
|
||||
def CloseDesktop(self):
|
||||
closed.append('desktop')
|
||||
|
||||
class FakeProcess:
|
||||
def Close(self):
|
||||
closed.append('process')
|
||||
|
||||
class FakeThread:
|
||||
def Close(self):
|
||||
closed.append('thread')
|
||||
raise error
|
||||
|
||||
def allocate(stage, value):
|
||||
if failure == stage:
|
||||
raise error
|
||||
return value
|
||||
|
||||
process = FakeProcess()
|
||||
monkeypatch.setattr(module.win32service, 'CreateDesktop',
|
||||
lambda *args: allocate('desktop', FakeDesktop()))
|
||||
monkeypatch.setattr(module.win32job, 'CreateJobObject',
|
||||
lambda *args: allocate('job', FakeJob()))
|
||||
monkeypatch.setattr(module.win32process, 'STARTUPINFO', lambda: type('Startup', (), {})())
|
||||
monkeypatch.setattr(module.win32process, 'CreateProcess',
|
||||
lambda *args: allocate('process', (process, FakeThread(), 321, 1)))
|
||||
|
||||
def terminate(handle, code):
|
||||
assert handle is process
|
||||
closed.append('terminate')
|
||||
|
||||
monkeypatch.setattr(module.win32process, 'TerminateProcess', terminate)
|
||||
|
||||
with pytest.raises(RuntimeError) as caught:
|
||||
module.DesktopWorker(['worker.exe'], 'PrivateDesk')
|
||||
|
||||
assert caught.value is error
|
||||
assert closed == expected
|
||||
|
||||
|
||||
def test_library_flag_loads_ui_dependencies_before_creating_private_desktop(monkeypatch, tmp_path):
|
||||
import builtins
|
||||
import win32service
|
||||
import cbb_refresh_verify.mentor_adapter as module
|
||||
|
||||
created = []
|
||||
original_import = builtins.__import__
|
||||
executable = tmp_path / 'LibraryManager.exe'
|
||||
executable.touch()
|
||||
|
||||
def deny_pywinauto(name, *args, **kwargs):
|
||||
if name == 'pywinauto' or name.startswith('pywinauto.'):
|
||||
raise ImportError('pywinauto unavailable')
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(builtins, '__import__', deny_pywinauto)
|
||||
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
|
||||
monkeypatch.setattr(win32service, 'CreateDesktop',
|
||||
lambda *args: created.append(args))
|
||||
|
||||
with pytest.raises(ImportError, match='pywinauto unavailable'):
|
||||
module.read_library_flag('library.lmc', 'Block')
|
||||
|
||||
assert created == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reader', ['read_library_flag', 'read_library_tree_image_map'])
|
||||
def test_reader_does_not_create_desktop_before_preparation_finishes(monkeypatch, tmp_path, reader):
|
||||
import win32service
|
||||
import cbb_refresh_verify.mentor_adapter as module
|
||||
|
||||
closed = []
|
||||
|
||||
class FakeDesktop:
|
||||
def CloseDesktop(self):
|
||||
closed.append('closed')
|
||||
|
||||
class InvalidLibrary:
|
||||
def __fspath__(self):
|
||||
raise ValueError('invalid library path')
|
||||
|
||||
executable = tmp_path / 'LibraryManager.exe'
|
||||
executable.touch()
|
||||
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
|
||||
monkeypatch.setattr(win32service, 'CreateDesktop',
|
||||
lambda *args: closed.append('created') or FakeDesktop())
|
||||
|
||||
with pytest.raises(ValueError, match='invalid library path'):
|
||||
getattr(module, reader)(InvalidLibrary(), 'Block')
|
||||
|
||||
assert closed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reader', ['read_library_flag', 'read_library_tree_image_map'])
|
||||
def test_reader_closes_desktop_when_process_launch_fails(monkeypatch, tmp_path, reader):
|
||||
import win32service
|
||||
import win32process
|
||||
import cbb_refresh_verify.mentor_adapter as module
|
||||
|
||||
closed = []
|
||||
|
||||
class FakeDesktop:
|
||||
def CloseDesktop(self):
|
||||
closed.append('desktop')
|
||||
|
||||
def fail(*args):
|
||||
raise RuntimeError('launch failed')
|
||||
|
||||
executable = tmp_path / 'LibraryManager.exe'
|
||||
executable.touch()
|
||||
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
|
||||
monkeypatch.setattr(win32service, 'CreateDesktop', lambda *args: FakeDesktop())
|
||||
monkeypatch.setattr(win32process, 'CreateProcess', fail)
|
||||
if reader == 'read_library_flag':
|
||||
flag, detail = module.read_library_flag(tmp_path / 'library.lmc', 'Block')
|
||||
assert flag == module.FLAG_UNKNOWN
|
||||
assert 'launch failed' in detail
|
||||
else:
|
||||
with pytest.raises(RuntimeError, match='launch failed'):
|
||||
module.read_library_tree_image_map(tmp_path / 'library.lmc', ['Block'])
|
||||
assert closed == ['desktop']
|
||||
@@ -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():
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
@@ -0,0 +1,306 @@
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
|
||||
|
||||
def test_content_timeout_preserves_importer_and_native_like_child(tmp_path):
|
||||
import win32api, win32con, win32process
|
||||
script = tmp_path/'importer.py'
|
||||
pids = tmp_path/'pids.json'
|
||||
script.write_text(
|
||||
'import subprocess,sys,time,os,json\nfrom pathlib import Path\n'
|
||||
'child=subprocess.Popen([sys.executable,"-c","import time;time.sleep(30)"],'
|
||||
'stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)\n'
|
||||
f'Path({str(pids)!r}).write_text(json.dumps([os.getpid(),child.pid]))\n'
|
||||
'time.sleep(30)\n', encoding='utf-8')
|
||||
handles = []
|
||||
try:
|
||||
result = runner._run_process([sys.executable, str(script)], 2, tmp_path)
|
||||
assert result[3] is True
|
||||
assert pids.is_file()
|
||||
for pid in json.loads(pids.read_text()):
|
||||
try:
|
||||
handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION |
|
||||
win32con.PROCESS_TERMINATE, False, pid)
|
||||
except Exception:
|
||||
pytest.fail(f'Timeout terminated database-related process {pid}')
|
||||
handles.append(handle)
|
||||
assert win32process.GetExitCodeProcess(handle) == 259
|
||||
finally:
|
||||
for handle in handles:
|
||||
win32process.TerminateProcess(handle, 0)
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_content_timeout_stops_remaining_batch(tmp_path, monkeypatch):
|
||||
from test_verify_runner import fixture, request
|
||||
fixture(tmp_path)
|
||||
monkeypatch.setattr(runner, 'resolve_importer', lambda *_: Path('ReuseBlockImporter.exe'))
|
||||
calls = []
|
||||
def timeout(*args):
|
||||
calls.append(args)
|
||||
return None, '', 'content check still running', True
|
||||
req = request(tmp_path, libspec='EE', dms_login_config='login')
|
||||
results = runner.run_batch([req, req], verifier=lambda item: runner.run_verify(item,
|
||||
process_runner=timeout, port_check=lambda: pytest.fail('PCB must not start')))
|
||||
assert len(calls) == 1
|
||||
assert results[0].status == 'TIMEOUT'
|
||||
assert results[0].safe_to_continue is False
|
||||
assert results[1].status == 'SKIPPED'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('identity,close_ok,other_document,expected_code', [
|
||||
(True, True, False, 0), (False, True, False, 2), (True, False, False, 4), (True, True, True, 5)])
|
||||
def test_failed_document_close_uses_native_discard_and_rejects_wrong_identity(
|
||||
tmp_path, identity, close_ok, other_document, expected_code):
|
||||
source = Path('src/cbb_refresh_verify/close_failed_block.vbs').read_text('utf-8')
|
||||
classes = r'''
|
||||
Dim nativeClosed
|
||||
nativeClosed = False
|
||||
Class FakePi
|
||||
Public ProjectFile
|
||||
Private Sub Class_Initialize
|
||||
ProjectFile = PROJECT_VALUE
|
||||
End Sub
|
||||
End Class
|
||||
Class FakeDoc
|
||||
Public Name
|
||||
Private Sub Class_Initialize
|
||||
Name = "CBB.pcb"
|
||||
End Sub
|
||||
Public Property Get ProjectIntegration
|
||||
Set ProjectIntegration = New FakePi
|
||||
End Property
|
||||
Public Function Validate(value)
|
||||
Validate = 17
|
||||
End Function
|
||||
Public Function Close(saveChanges)
|
||||
If saveChanges Then WScript.Quit 90
|
||||
WScript.Echo "NATIVE_DISCARD_CLOSE"
|
||||
nativeClosed = CLOSE_RETURN
|
||||
Close = CLOSE_RETURN
|
||||
End Function
|
||||
End Class
|
||||
Class FakeApp
|
||||
Public Property Get ActiveDocument
|
||||
If nativeClosed And Not OTHER_DOCUMENT Then
|
||||
Set ActiveDocument = Nothing
|
||||
Else
|
||||
Set ActiveDocument = New FakeDoc
|
||||
End If
|
||||
End Property
|
||||
Public Sub Quit()
|
||||
WScript.Echo "NATIVE_QUIT"
|
||||
End Sub
|
||||
End Class
|
||||
Class FakeLic
|
||||
Public Function GetToken(value)
|
||||
GetToken = value
|
||||
End Function
|
||||
End Class
|
||||
'''.replace('PROJECT_VALUE', 'WScript.Arguments(0)' if identity else '"OTHER.prj"')
|
||||
classes = classes.replace('CLOSE_RETURN', str(close_ok))
|
||||
classes = classes.replace('OTHER_DOCUMENT', str(other_document))
|
||||
source = source.replace('Option Explicit', 'Option Explicit\n'+classes)
|
||||
source = source.replace('GetObject(, "MGCPCB.Application")', 'New FakeApp')
|
||||
source = source.replace('CreateObject("MGCPCBAutomationLicensing.Application")', 'New FakeLic')
|
||||
harness = tmp_path/'close.vbs'
|
||||
harness.write_text(source, encoding='ascii')
|
||||
outcome = subprocess.run([str(Path(os.environ['WINDIR'])/'SysWOW64/cscript.exe'),
|
||||
'//nologo', str(harness), 'expected.prj', 'CBB.pcb'], capture_output=True, text=True, timeout=10)
|
||||
assert outcome.returncode == expected_code, outcome.stderr
|
||||
assert ('NATIVE_DISCARD_CLOSE' in outcome.stdout) is identity
|
||||
assert ('NATIVE_QUIT' in outcome.stdout) is (identity and close_ok and not other_document)
|
||||
|
||||
|
||||
def test_cancel_after_native_save_returns_stops_later_native_operations(tmp_path):
|
||||
source = Path('src/cbb_refresh_verify/save_verified_block.vbs').read_text('utf-8')
|
||||
classes = r'''
|
||||
Class FakePi
|
||||
Public ProjectFile, ForwardAnnotationStatus, SynchCESStatus, LoadCESStatus, BackAnnotationStatus
|
||||
Private Sub Class_Initialize
|
||||
ProjectFile = WScript.Arguments(0)
|
||||
ForwardAnnotationStatus = 2
|
||||
SynchCESStatus = 2
|
||||
LoadCESStatus = 2
|
||||
BackAnnotationStatus = 2
|
||||
End Sub
|
||||
End Class
|
||||
Class FakeDoc
|
||||
Public Name
|
||||
Private Sub Class_Initialize
|
||||
Name = "CBB.pcb"
|
||||
End Sub
|
||||
Public Property Get ProjectIntegration
|
||||
Set ProjectIntegration = New FakePi
|
||||
End Property
|
||||
Public Function Validate(token)
|
||||
Validate = 17
|
||||
End Function
|
||||
Public Function Save()
|
||||
Dim fso, flag
|
||||
Set fso = CreateObject("Scripting.FileSystemObject")
|
||||
Set flag = fso.CreateTextFile(WScript.Arguments(2), True)
|
||||
flag.Close
|
||||
WScript.Sleep 120
|
||||
WScript.Echo "NATIVE_SAVE_RETURNED"
|
||||
Save = True
|
||||
End Function
|
||||
Public Function IsSaved()
|
||||
IsSaved = True
|
||||
End Function
|
||||
Public Function Close(saveChanges)
|
||||
Close = True
|
||||
End Function
|
||||
End Class
|
||||
Class FakeApp
|
||||
Public Property Get ActiveDocument
|
||||
Set ActiveDocument = New FakeDoc
|
||||
End Property
|
||||
Public Sub Quit()
|
||||
End Sub
|
||||
End Class
|
||||
Class FakeLic
|
||||
Public Function GetToken(value)
|
||||
GetToken = value
|
||||
End Function
|
||||
End Class
|
||||
'''
|
||||
source = source.replace('Option Explicit', 'Option Explicit\n'+classes)
|
||||
source = source.replace('GetObject(, "MGCPCB.Application")', 'New FakeApp')
|
||||
source = source.replace('CreateObject("MGCPCBAutomationLicensing.Application")', 'New FakeLic')
|
||||
harness = tmp_path/'save-cancel.vbs'
|
||||
harness.write_text(source, encoding='ascii')
|
||||
outcome = subprocess.run([str(Path(os.environ['WINDIR'])/'SysWOW64/cscript.exe'),
|
||||
'//nologo', str(harness), 'expected.prj', 'CBB.pcb', str(tmp_path/'cancel.signal')],
|
||||
capture_output=True, text=True, timeout=10)
|
||||
assert 'NATIVE_SAVE_RETURNED' in outcome.stdout
|
||||
assert outcome.returncode == 9, outcome.stderr
|
||||
assert 'VERIFY_CANCELLED' in outcome.stdout
|
||||
assert 'CLOSE_OK' not in outcome.stdout
|
||||
|
||||
|
||||
def test_worker_attempts_normal_close_after_idle_integration_failure(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch, integration_error='packaging rejected')
|
||||
closes = []
|
||||
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document',
|
||||
lambda value, **kwargs: closes.append(value) or {'status': 'CLOSED'})
|
||||
with pytest.raises(RuntimeError, match='packaging rejected'):
|
||||
execute()
|
||||
assert closes == [job]
|
||||
assert not any(event['stage'] == 'save_start' for event in events)
|
||||
|
||||
|
||||
def test_stop_requests_cooperative_cancel_without_terminating_busy_client(tmp_path):
|
||||
calls = []
|
||||
class BusyWorker:
|
||||
def request_stop(self): calls.append('cancel')
|
||||
def wait(self, timeout): raise TimeoutError('native Save still running')
|
||||
def kill(self): calls.append('kill')
|
||||
runner._stop_worker(BusyWorker(), timeout=0)
|
||||
assert calls == ['cancel']
|
||||
|
||||
|
||||
def test_status_server_close_allows_eof_cleanup_before_termination(tmp_path):
|
||||
from cbb_refresh_verify.status_bridge import StatusBridgeServer
|
||||
script = tmp_path/'reader.py'
|
||||
marker = tmp_path/'released.txt'
|
||||
script.write_text('import sys,time\nfrom pathlib import Path\n'
|
||||
'for line in sys.stdin: pass\n'
|
||||
'time.sleep(.1)\n'
|
||||
f'Path({str(marker)!r}).write_text("native handles released")\n', encoding='utf-8')
|
||||
server = StatusBridgeServer(script)
|
||||
server.start()
|
||||
time.sleep(.2)
|
||||
server.close()
|
||||
assert marker.is_file()
|
||||
|
||||
|
||||
def test_shared_symbol_database_owner_blocks_content_check_and_pcb(tmp_path, monkeypatch):
|
||||
from test_verify_runner import fixture, request
|
||||
fixture(tmp_path)
|
||||
address = tmp_path/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr'
|
||||
address.parent.mkdir(parents=True)
|
||||
address.write_bytes(b'unknown native owner')
|
||||
req = request(tmp_path, libspec='EE', dms_login_config='login')
|
||||
outcome = runner.run_verify(req, process_runner=lambda *args: pytest.fail('must not launch importer'))
|
||||
assert outcome.status == 'BLOCKED'
|
||||
assert outcome.safe_to_continue is False
|
||||
assert '__ReusableBlocksSymb' in outcome.stderr
|
||||
assert address.read_bytes() == b'unknown native owner'
|
||||
|
||||
|
||||
def test_shared_symbol_database_must_be_released_before_pass(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
original = adapter.subprocess.Popen
|
||||
def helper(*args, **kwargs):
|
||||
address = Path(job['lmc_file']).parent/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr'
|
||||
address.parent.mkdir(parents=True)
|
||||
address.write_bytes(b'shared owner retained')
|
||||
return original(*args, **kwargs)
|
||||
monkeypatch.setattr(adapter.subprocess, 'Popen', helper)
|
||||
with pytest.raises(RuntimeError, match='ICDB_RELEASE_UNCONFIRMED'):
|
||||
execute()
|
||||
assert not any(event['stage'] == 'complete' for event in events)
|
||||
|
||||
|
||||
def test_cooperative_supervisor_loss_keeps_pending_save_helper_alive(tmp_path):
|
||||
import win32api, win32con, win32process
|
||||
supervisor = subprocess.Popen([sys.executable, '-c', 'import time;time.sleep(30)'])
|
||||
script, ready, cancel = tmp_path/'worker.py', tmp_path/'ready.json', tmp_path/'cancel.signal'
|
||||
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},cancel_path={str(cancel)!r})\n'
|
||||
'helper=launch_save_helper([sys.executable,"-c","import time;time.sleep(30)"])\n'
|
||||
f'Path({str(ready)!r}).write_text(json.dumps(helper.pid))\n'
|
||||
'time.sleep(30)\n', encoding='utf-8')
|
||||
worker = subprocess.Popen([sys.executable, str(script)])
|
||||
handle = None
|
||||
try:
|
||||
deadline = time.monotonic()+5
|
||||
while not ready.exists() and time.monotonic()<deadline:
|
||||
time.sleep(.05)
|
||||
assert ready.exists()
|
||||
handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_TERMINATE,
|
||||
False, json.loads(ready.read_text()))
|
||||
supervisor.terminate()
|
||||
supervisor.wait(3)
|
||||
deadline = time.monotonic()+3
|
||||
while not cancel.exists() and time.monotonic()<deadline:
|
||||
time.sleep(.05)
|
||||
assert cancel.exists()
|
||||
assert worker.poll() is None
|
||||
assert win32process.GetExitCodeProcess(handle) == 259
|
||||
finally:
|
||||
for process in (worker, supervisor):
|
||||
if process.poll() is None: process.terminate()
|
||||
process.wait(3)
|
||||
if handle:
|
||||
win32process.TerminateProcess(handle, 0)
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_idle_failed_native_verify_closes_its_library_manager_owner(tmp_path, monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch, integration_error='packaging rejected')
|
||||
job['native_lm_verify'] = True
|
||||
closed = []
|
||||
monkeypatch.setattr('cbb_refresh_verify.library_manager_verify.launch_library_verify',
|
||||
lambda *a, **kw: SimpleNamespace(process=SimpleNamespace(pid=11), close=lambda: closed.append(11)))
|
||||
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document',
|
||||
lambda *a, **kw: {'status':'CLOSED'})
|
||||
with pytest.raises(RuntimeError, match='packaging rejected'):
|
||||
execute()
|
||||
assert closed == [11]
|
||||
@@ -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):
|
||||
|
||||
@@ -0,0 +1,310 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
|
||||
|
||||
ERROR = (
|
||||
"The last server session on different server machine lost the connection\n"
|
||||
"to the database. Please use iCDB Project Backup 'Repair project' functionality "
|
||||
"to fix this issue. (Machine:[200091-CBB] Path:"
|
||||
"[S:\\SymbolLibs\\__ReusableBlocksSymb] session:[2026-10-24 08.22.29-10308])"
|
||||
)
|
||||
|
||||
|
||||
def test_native_session_loss_has_priority_over_generic_load_and_cleanup_errors():
|
||||
detail = 'DATABASE_LOAD_FAILED: Could not load Job Preferences DB\n' + ERROR
|
||||
detail += '\nPCB_RELEASE_UNCONFIRMED'
|
||||
assert runner.classify_failure_category(detail) == 'ICDB_REPAIR_REQUIRED'
|
||||
|
||||
|
||||
def test_live_repair_dialog_blocks_without_clicking():
|
||||
from test_dialog_rules import prompt, ASSETS
|
||||
from cbb_refresh_verify.dialog_rules import decide, load_rules
|
||||
from cbb_refresh_verify.dialog_supervisor import classify_dialog, DialogSnapshot, DialogControl
|
||||
decision = decide(prompt(ERROR), load_rules(ASSETS/'dialog_rules.v1.json'))
|
||||
legacy = classify_dialog(DialogSnapshot(1, 2, 'Expedition PCB', (
|
||||
DialogControl(3, 1, 'Static', ERROR, True),)))
|
||||
for item in (decision, legacy):
|
||||
assert item.error_code == 'ICDB_REPAIR_REQUIRED'
|
||||
assert not item.action
|
||||
assert '200091-CBB' in item.detail
|
||||
|
||||
|
||||
def test_startup_probe_reports_session_loss_instead_of_port_reset(tmp_path):
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
log.write_text(ERROR + '\nCould not run server', encoding='utf-8')
|
||||
detail = adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb')
|
||||
assert 'ICDB_REPAIR_REQUIRED' in detail
|
||||
assert '__ReusableBlocksSymb' in detail
|
||||
assert 'Repair project' in detail
|
||||
assert 'net stop winnat' not in detail
|
||||
|
||||
|
||||
def test_generic_server_log_does_not_claim_port_reservation(tmp_path):
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
log.write_text('Could not run server', encoding='utf-8')
|
||||
detail = adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb')
|
||||
assert 'DESIGN_SERVER_FAILED' in detail
|
||||
assert 'WinError 10013' not in detail
|
||||
assert 'net stop winnat' not in detail
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode', ['append', 'shorter', 'same_size', 'longer'])
|
||||
def test_current_run_probe_handles_appended_and_rewritten_logs(tmp_path, mode):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
old = 'old harmless text\n' if mode == 'longer' else 'x'*(len(ERROR.encode())+40)
|
||||
if mode == 'same_size':
|
||||
old = 'x'*len(ERROR.encode())
|
||||
log.write_text(old, encoding='utf-8')
|
||||
baseline = snapshot_log(log)
|
||||
log.write_text(old+ERROR if mode == 'append' else ERROR, encoding='utf-8')
|
||||
assert 'ICDB_REPAIR_REQUIRED' in adapter.pcb_log_reports_missing_server(
|
||||
tmp_path/'CBB.pcb', offset=baseline)
|
||||
|
||||
|
||||
def test_old_loss_record_does_not_fail_a_new_run(tmp_path):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
log.write_text(ERROR, encoding='utf-8')
|
||||
baseline = snapshot_log(log)
|
||||
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
|
||||
with log.open('a', encoding='utf-8') as stream:
|
||||
stream.write('\nNormal startup completed\n')
|
||||
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
|
||||
|
||||
|
||||
def test_rewritten_header_does_not_promote_retained_old_session_loss(tmp_path):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
log.write_text('old header\n'+ERROR, encoding='utf-8')
|
||||
baseline = snapshot_log(log)
|
||||
log.write_text('new header\n'+ERROR+'\nNormal startup\n', encoding='utf-8')
|
||||
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
|
||||
|
||||
|
||||
def test_repeated_old_session_loss_is_current_when_verifiably_appended(tmp_path):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
log.write_text(ERROR+'\n', encoding='utf-8')
|
||||
baseline = snapshot_log(log)
|
||||
with log.open('a', encoding='utf-8') as stream:
|
||||
stream.write(ERROR)
|
||||
assert 'ICDB_REPAIR_REQUIRED' in adapter.pcb_log_reports_missing_server(
|
||||
tmp_path/'CBB.pcb', offset=baseline)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', ['MsgBoxResponse.log', 'ForwardAnnotation.txt', 'PartPkg.log'])
|
||||
def test_integration_log_retains_multiline_loss_identity(tmp_path, name):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
log = tmp_path/'LogFiles'/name
|
||||
log.parent.mkdir()
|
||||
baseline = snapshot_log(log)
|
||||
log.write_text(ERROR.replace(' (Machine:', '\nMachine:').replace(' Path:', '\nPath:')
|
||||
.replace(' session:', '\nSession:'), encoding='utf-8')
|
||||
detail = adapter.pcb_log_reports_integration_error(tmp_path/'CBB.pcb', offsets={log: baseline})
|
||||
assert 'ICDB_REPAIR_REQUIRED' in detail
|
||||
assert '200091-CBB' in detail
|
||||
assert '__ReusableBlocksSymb' in detail
|
||||
assert '2026-10-24 08.22.29-10308' in detail
|
||||
|
||||
|
||||
@pytest.mark.parametrize('production_checkpoint', [False, True])
|
||||
def test_integration_generic_filter_does_not_repromote_historical_repair(tmp_path, production_checkpoint):
|
||||
import hashlib
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
log = tmp_path/'LogFiles/MsgBoxResponse.log'
|
||||
log.parent.mkdir()
|
||||
old = 'old header\nERROR: '+ERROR.replace('\n', ' ')
|
||||
log.write_text(old, encoding='utf-8')
|
||||
baseline = snapshot_log(log)
|
||||
if production_checkpoint:
|
||||
raw = log.read_bytes()
|
||||
baseline = {'size': len(raw), 'digest': hashlib.sha256(raw).hexdigest(),
|
||||
'icdb_baseline': baseline}
|
||||
log.write_text(old.replace('old header', 'new header')+'\nnormal startup', encoding='utf-8')
|
||||
detail = adapter.pcb_log_reports_integration_error(tmp_path/'CBB.pcb', offsets={log: baseline})
|
||||
assert 'ICDB_REPAIR_REQUIRED' not in detail
|
||||
assert runner.classify_failure_category(detail) != 'ICDB_REPAIR_REQUIRED'
|
||||
|
||||
|
||||
def test_unreadable_integration_baseline_never_falls_back_to_historical_log(tmp_path):
|
||||
log = tmp_path/'LogFiles/MsgBoxResponse.log'
|
||||
log.parent.mkdir()
|
||||
log.write_text('ERROR: '+ERROR.replace('\n', ' '), encoding='utf-8')
|
||||
baseline = {'size': 0, 'mtime_ns': None, 'digest': '',
|
||||
'icdb_baseline': {'error': 'access denied'}}
|
||||
detail = adapter.pcb_log_reports_integration_error(tmp_path/'CBB.pcb', offsets={log: baseline})
|
||||
assert runner.classify_failure_category(detail) != 'ICDB_REPAIR_REQUIRED'
|
||||
|
||||
|
||||
def test_rewrite_of_large_log_does_not_promote_error_outside_baseline_window(tmp_path):
|
||||
from cbb_refresh_verify.icdb_recovery import LOG_LIMIT, snapshot_log
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
log.write_text(ERROR+'\n'+'normal\n'*LOG_LIMIT, encoding='utf-8')
|
||||
baseline = snapshot_log(log)
|
||||
log.write_text('changed header\n'+ERROR, encoding='utf-8')
|
||||
assert adapter.pcb_log_reports_missing_server(tmp_path/'CBB.pcb', offset=baseline) == ''
|
||||
|
||||
|
||||
def test_integration_session_loss_has_priority_over_generic_startup_error(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify.pcb_bridge import PcbBridgeError
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
def attach(job, provider, **kwargs):
|
||||
logs = Path(job['pcb']).parent/'LogFiles'
|
||||
logs.mkdir()
|
||||
(logs/'PCBLogFile.txt').write_text('Could not run server', encoding='utf-8')
|
||||
(logs/'MsgBoxResponse.log').write_text(ERROR, encoding='utf-8')
|
||||
detail = kwargs['error_reader']()
|
||||
assert 'ICDB_REPAIR_REQUIRED' in detail
|
||||
assert 'net stop winnat' not in detail
|
||||
raise PcbBridgeError('PCB_STARTUP_FAILED', detail)
|
||||
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', attach)
|
||||
with pytest.raises(PcbBridgeError, match='ICDB_REPAIR_REQUIRED'):
|
||||
execute()
|
||||
assert not any(event['stage'] == 'save_start' for event in events)
|
||||
|
||||
|
||||
def test_optional_baseline_evidence_write_failure_does_not_block_verify(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
original = Path.write_text
|
||||
def deny_baseline(path, *args, **kwargs):
|
||||
if path.name == 'startup-log-baseline.json':
|
||||
raise PermissionError('diagnostic evidence unavailable')
|
||||
return original(path, *args, **kwargs)
|
||||
monkeypatch.setattr(Path, 'write_text', deny_baseline)
|
||||
assert execute()['status'] == 'PASS'
|
||||
|
||||
|
||||
def test_recovery_record_preserves_identity_and_distinguishes_shared_library():
|
||||
from cbb_refresh_verify.icdb_recovery import parse_session_loss
|
||||
record = parse_session_loss(ERROR)
|
||||
assert record['machine'] == '200091-CBB'
|
||||
assert record['database_path'] == r'S:\SymbolLibs\__ReusableBlocksSymb'
|
||||
assert record['session'] == '2026-10-24 08.22.29-10308'
|
||||
assert record['scope'] == 'central_library'
|
||||
assert parse_session_loss(ERROR.replace(r'S:\SymbolLibs\__ReusableBlocksSymb',
|
||||
r'S:\ReusableBlocksLP\CBB\Work\database'))['scope'] == 'project'
|
||||
assert parse_session_loss('Could not run server') is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize('encoding', ['utf-8', 'utf-16', 'utf-16-le'])
|
||||
def test_new_utf16_or_utf8_log_errors_are_detected_after_append(tmp_path, encoding):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
log = tmp_path/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
log.write_bytes('old text\n'.encode(encoding))
|
||||
baseline = snapshot_log(log)
|
||||
with log.open('ab') as stream:
|
||||
stream.write(ERROR.encode('utf-16-le' if encoding == 'utf-16' else encoding))
|
||||
assert 'ICDB_REPAIR_REQUIRED' in adapter.pcb_log_reports_missing_server(
|
||||
tmp_path/'CBB.pcb', offset=baseline)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('baseline_state', ['stale', 'absent', 'unreadable'])
|
||||
def test_historical_log_evidence_never_creates_a_repair_recommendation(tmp_path, baseline_state):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
from cbb_refresh_verify.verify_diagnostics import collect_native_failure_logs
|
||||
pcb = tmp_path/'Work/PCB/CBB.pcb'
|
||||
log = pcb.parent/'LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir(parents=True)
|
||||
log.write_text(ERROR, encoding='utf-8')
|
||||
evidence = tmp_path/'evidence'
|
||||
(evidence/'diagnostics').mkdir(parents=True)
|
||||
if baseline_state != 'absent':
|
||||
baseline = snapshot_log(log) if baseline_state == 'stale' else {'error': 'access denied'}
|
||||
(evidence/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
|
||||
'source': str(log), 'baseline': baseline}), encoding='utf-8')
|
||||
collect_native_failure_logs(pcb, evidence)
|
||||
assert not (evidence/'diagnostics/icdb-recovery.json').exists()
|
||||
|
||||
|
||||
def test_content_success_text_cannot_override_native_session_loss():
|
||||
assert runner.classify_output(0, 'content checking completed successfully\n'+ERROR) == 'FAIL'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', ['PCBLogFile.txt', 'MsgBoxResponse.log', 'ForwardAnnotation.txt', 'PartPkg.log'])
|
||||
def test_native_log_capture_generates_only_fresh_repair_evidence(tmp_path, name):
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
from cbb_refresh_verify.verify_diagnostics import collect_native_failure_logs
|
||||
pcb = tmp_path/'Work/PCB/CBB.pcb'
|
||||
log = pcb.parent/'LogFiles'/name
|
||||
log.parent.mkdir(parents=True)
|
||||
evidence = tmp_path/'evidence'
|
||||
(evidence/'diagnostics').mkdir(parents=True)
|
||||
log.write_text('old text', encoding='utf-8')
|
||||
(evidence/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
|
||||
'source': str(pcb.parent/'LogFiles/PCBLogFile.txt'),
|
||||
'baseline': snapshot_log(pcb.parent/'LogFiles/PCBLogFile.txt'),
|
||||
'integration_logs': {str(log): snapshot_log(log)}}), encoding='utf-8')
|
||||
log.write_text(ERROR, encoding='utf-8')
|
||||
collect_native_failure_logs(pcb, evidence)
|
||||
report = json.loads((evidence/'diagnostics/icdb-recovery.json').read_text('utf-8'))
|
||||
assert report['scope'] == 'central_library'
|
||||
assert report['source'] == str(log)
|
||||
assert log.read_text('utf-8') == ERROR
|
||||
|
||||
|
||||
def test_contents_session_loss_stops_batch_before_any_pcb_launch(tmp_path, monkeypatch):
|
||||
from test_verify_runner import fixture, request
|
||||
fixture(tmp_path)
|
||||
monkeypatch.setattr(runner, 'resolve_importer', lambda *_: Path('ReuseBlockImporter.exe'))
|
||||
calls = []
|
||||
def content_check(*args):
|
||||
calls.append(args)
|
||||
return 1, ERROR, '', False
|
||||
results = runner.run_batch([request(tmp_path, libspec='EE', dms_login_config='login')]*2,
|
||||
verifier=lambda req: runner.run_verify(req, process_runner=content_check,
|
||||
port_check=lambda: pytest.fail('PCB must not start')))
|
||||
assert len(calls) == 1
|
||||
assert results[0].error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
assert results[0].safe_to_continue is False
|
||||
assert 'Repair project' in results[0].stderr
|
||||
assert results[1].status == 'SKIPPED'
|
||||
|
||||
|
||||
def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, monkeypatch):
|
||||
from test_verify_runner import fixture, request
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
from cbb_refresh_verify.verify_diagnostics import failure_logs_main
|
||||
work = fixture(tmp_path)
|
||||
log = work/'PCB/LogFiles/PCBLogFile.txt'
|
||||
log.parent.mkdir()
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = 1
|
||||
def __init__(self, command, *_a, **_kw):
|
||||
root = Path(command[-1]).parent
|
||||
(root/'diagnostics').mkdir()
|
||||
(root/'diagnostics/startup-log-baseline.json').write_text(json.dumps({
|
||||
'source': str(log), 'baseline': snapshot_log(log)}), encoding='utf-8')
|
||||
log.write_text(ERROR, encoding='utf-8')
|
||||
(root/'outcome.json').write_text(json.dumps({
|
||||
'status': 'FAIL', 'stderr': 'MENTOR_DATABASE_LOAD_FAILED: opening failed',
|
||||
'safe_to_continue': False}), encoding='utf-8')
|
||||
def poll(self): return 1
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', failure_logs_main)
|
||||
result = runner.run_verify(request(tmp_path), port_check=lambda: (True, ''))
|
||||
assert result.error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
assert result.safe_to_continue is False
|
||||
assert 'opening failed' in result.stderr
|
||||
assert '200091-CBB' in result.stderr
|
||||
assert '__ReusableBlocksSymb' in result.stderr
|
||||
assert '2026-10-24 08.22.29-10308' in result.stderr
|
||||
assert json.loads(result.evidence_path.read_text('utf-8'))['error_category'] == 'ICDB_REPAIR_REQUIRED'
|
||||
@@ -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()
|
||||
@@ -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():
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def target_fixture(tmp_path):
|
||||
from test_verify_runner import fixture, request
|
||||
from cbb_refresh_verify.native_repair import select_repair_target
|
||||
work = fixture(tmp_path)
|
||||
database = work/'database'
|
||||
database.mkdir()
|
||||
(database/'icdb.dat').write_bytes(b'original damaged database')
|
||||
diagnosis = {'error_code': 'ICDB_REPAIR_REQUIRED', 'machine': 'OLD-PC',
|
||||
'database_path': str(database), 'session': '2026-10-24 08.22.29-10308', 'scope': 'project'}
|
||||
return request(tmp_path), diagnosis, select_repair_target(request(tmp_path), diagnosis)
|
||||
|
||||
|
||||
def test_repair_target_matches_actual_database_not_guessed_cbb_name(tmp_path):
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
assert target.project.name == 'CBB1.prj'
|
||||
assert target.database == target.project.parent/'database'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['foreign', 'shared', 'missing_session'])
|
||||
def test_repair_target_rejects_unresolved_or_shared_database(tmp_path, change):
|
||||
from cbb_refresh_verify.native_repair import select_repair_target
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
if change == 'foreign': record['database_path'] = str(tmp_path/'foreign/database')
|
||||
if change == 'shared':
|
||||
record.update(scope='central_library', database_path=str(tmp_path/'SymbolLibs/__ReusableBlocksSymb'))
|
||||
if change == 'missing_session': record['session'] = ''
|
||||
with pytest.raises(ValueError):
|
||||
select_repair_target(request, record)
|
||||
|
||||
|
||||
def test_repair_backup_contains_database_locks_backups_and_original_files(tmp_path):
|
||||
from cbb_refresh_verify.native_repair import backup_project
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
for relative in ('database/cdbsvr/sAddress.adr', 'ProjectBackup/backups/old.zip',
|
||||
'PCB/copy.lock', 'Log Files/ProjectBackupLog.txt'):
|
||||
path = target.project.parent/relative
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(b'original '+relative.encode())
|
||||
destination = tmp_path/'repair-backup'
|
||||
manifest = backup_project(target, destination)
|
||||
assert manifest['complete'] is True
|
||||
assert (destination/'database/icdb.dat').read_bytes() == b'original damaged database'
|
||||
assert (destination/'database/cdbsvr/sAddress.adr').read_bytes().startswith(b'original')
|
||||
assert (destination/'ProjectBackup/backups/old.zip').is_file()
|
||||
assert (destination/'PCB/copy.lock').is_file()
|
||||
|
||||
|
||||
def test_repair_backup_refuses_database_open_by_another_client(tmp_path):
|
||||
import win32file, win32con
|
||||
from cbb_refresh_verify.native_repair import backup_project
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
handle = win32file.CreateFile(str(target.database/'icdb.dat'), win32con.GENERIC_READ,
|
||||
win32con.FILE_SHARE_READ, None, win32con.OPEN_EXISTING, 0, None)
|
||||
try:
|
||||
with pytest.raises(OSError):
|
||||
backup_project(target, tmp_path/'backup')
|
||||
finally:
|
||||
handle.Close()
|
||||
assert not (tmp_path/'backup/backup-manifest.json').exists()
|
||||
|
||||
|
||||
def test_repair_backup_rejects_reparse_directory(tmp_path):
|
||||
import _winapi
|
||||
from cbb_refresh_verify.native_repair import backup_project
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
outside = tmp_path/'outside'
|
||||
outside.mkdir()
|
||||
_winapi.CreateJunction(str(outside), str(target.project.parent/'linked'))
|
||||
with pytest.raises(ValueError):
|
||||
backup_project(target, tmp_path/'backup')
|
||||
|
||||
|
||||
def test_native_confirmation_requires_exact_target_and_known_repair_prompt():
|
||||
from cbb_refresh_verify.native_repair_ui import confirmation_allowed
|
||||
assert confirmation_allowed('iCDB Project Backup - C:\\exact.prj', r'C:\exact.prj',
|
||||
'The project content will be validated and repaired if necessary.\n'
|
||||
'A project backup will be created before any action take place.\n'
|
||||
'You will be able to revert all changes using that backup.')
|
||||
assert not confirmation_allowed('iCDB Project Backup - C:\\OTHER.prj', r'C:\exact.prj',
|
||||
'The project content will be validated and repaired if necessary.')
|
||||
assert not confirmation_allowed('iCDB Project Backup - C:\\exact.prj', r'C:\exact.prj',
|
||||
'Confirm no other clients are connected. Continue anyway?')
|
||||
|
||||
|
||||
def test_native_diagnostics_finish_alone_does_not_prove_repair_success():
|
||||
from cbb_refresh_verify.native_repair_ui import repair_completed
|
||||
assert not repair_completed('Diagnostic: FINISH', 'IC: Closing project', native_backup=False)
|
||||
assert not repair_completed('ERROR: invalid database\nDiagnostic: FINISH',
|
||||
'iCDBProjectBackupRepair\nIC: Closing project', native_backup=True)
|
||||
assert repair_completed('Diagnostic: START\nDiagnostic: FINISH',
|
||||
'iCDBProjectBackupRepair\nIC: Closing project', native_backup=True)
|
||||
|
||||
|
||||
def test_native_restore_of_old_backup_requires_review_even_when_diagnostics_finish(tmp_path):
|
||||
from cbb_refresh_verify.native_repair_ui import _native_evidence
|
||||
from cbb_refresh_verify.icdb_recovery import snapshot_log
|
||||
project = tmp_path/'project.prj'
|
||||
database = tmp_path/'database'
|
||||
project.touch()
|
||||
log = tmp_path/'Log Files/ProjectBackupLog.txt'
|
||||
log.parent.mkdir()
|
||||
baseline = snapshot_log(log)
|
||||
for folder, text in (
|
||||
('dbcheck', 'Diagnostic: START\nDiagnostic: FINISH'),
|
||||
('dbrestore', f'iCDB database restored from backup: {tmp_path}/old.zip\n[0] DONE')):
|
||||
path = database/'cdbsvr'/folder/'new-1234/log.txt'
|
||||
path.parent.mkdir(parents=True)
|
||||
path.write_text(text)
|
||||
evidence = _native_evidence(project, database, 1234, 0, baseline)
|
||||
assert evidence['restored_from_backup'] == f'{tmp_path}/old.zip'
|
||||
assert evidence['restore_complete'] is True
|
||||
assert evidence['confirmed'] is False
|
||||
|
||||
|
||||
def test_automatic_repair_retries_only_once_and_preserves_original_failure(tmp_path, monkeypatch):
|
||||
from dataclasses import replace
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'first/result.json'
|
||||
evidence.parent.mkdir()
|
||||
(evidence.parent/'diagnostics').mkdir()
|
||||
(evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(record))
|
||||
original = runner.VerifyResult('FAIL', stderr='native lost session',
|
||||
safe_to_continue=False, error_category='ICDB_REPAIR_REQUIRED', evidence_path=evidence)
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
calls.append(req)
|
||||
return original
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', verify)
|
||||
monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: {'status':'REPAIRED'})
|
||||
outcome = runner.run_verify(request)
|
||||
assert len(calls) == 2
|
||||
assert outcome.status == 'FAIL'
|
||||
assert outcome.stderr == 'native lost session'
|
||||
assert outcome.recovery_attempts[0]['original_error'] == 'native lost session'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('repair_status', ['BLOCKED', 'RESTORED_REVIEW_REQUIRED'])
|
||||
def test_automatic_repair_does_not_retry_busy_or_unsupported_target(tmp_path, monkeypatch, repair_status):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'first/result.json'
|
||||
evidence.parent.mkdir()
|
||||
(evidence.parent/'diagnostics').mkdir()
|
||||
(evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(record))
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
calls.append(req)
|
||||
return runner.VerifyResult('FAIL', stderr='native lost session',
|
||||
error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=False, evidence_path=evidence)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', verify)
|
||||
monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw: {'status':repair_status})
|
||||
outcome = runner.run_verify(request)
|
||||
assert len(calls) == 1
|
||||
assert outcome.safe_to_continue is False
|
||||
assert outcome.recovery_attempts[0]['status'] == repair_status
|
||||
|
||||
|
||||
def test_timeout_never_enters_automatic_repair(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'first/result.json'
|
||||
evidence.parent.mkdir()
|
||||
(evidence.parent/'diagnostics').mkdir()
|
||||
(evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(record))
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **kw:
|
||||
runner.VerifyResult('TIMEOUT', error_category='ICDB_REPAIR_REQUIRED',
|
||||
safe_to_continue=False, evidence_path=evidence))
|
||||
monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw:
|
||||
pytest.fail('A timed-out native owner must not be repaired'))
|
||||
assert runner.run_verify(request).status == 'TIMEOUT'
|
||||
|
||||
|
||||
def test_retained_importer_blocks_repair_even_without_an_open_database_handle(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'original'
|
||||
evidence.mkdir()
|
||||
(evidence/'content-check-process.json').write_text(json.dumps({'status':'RETAINED','pid':123}))
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **kw:
|
||||
pytest.fail('Do not repair between importer transactions'))
|
||||
outcome = native_repair.attempt_recovery(request, record, evidence)
|
||||
assert outcome['status'] == 'BLOCKED'
|
||||
assert 'IMPORTER_RELEASE_UNCONFIRMED' in outcome['detail']
|
||||
|
||||
|
||||
def test_repair_cancellation_during_backup_prevents_native_launch(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair_ui as ui, native_repair, mentor_adapter
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
root = tmp_path/'worker'
|
||||
root.mkdir()
|
||||
cancel = root/'cancel.signal'
|
||||
job = root/'request.json'
|
||||
job.write_text(json.dumps({'project':str(target.project),'database':str(target.database),
|
||||
'root':str(root),'cancel_signal':str(cancel),'timeout':1,
|
||||
'supervisor_pid':123,'desktop':'CbbRepair_test'}))
|
||||
calls = []
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.start_supervisor_watchdog',
|
||||
lambda *a, **kw: calls.append('watchdog'))
|
||||
monkeypatch.setattr(ui, '_current_desktop_name', lambda: 'CbbRepair_test', raising=False)
|
||||
monkeypatch.setattr(ui, '_repair_command', lambda *_: 32924)
|
||||
monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: [])
|
||||
monkeypatch.setattr(mentor_adapter, '_process_details', lambda *_: [])
|
||||
def backup(*a):
|
||||
assert calls == ['watchdog'], 'Watch supervisor before the potentially long backup'
|
||||
cancel.touch()
|
||||
calls.append('backup')
|
||||
monkeypatch.setattr(native_repair, 'backup_project', backup)
|
||||
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('Repair must not start'))
|
||||
assert ui.repair_worker_main(job) == 1
|
||||
outcome = json.loads((root/'repair-result.json').read_text('utf-8'))
|
||||
assert 'REPAIR_CANCELLED_AFTER_BACKUP' in outcome['detail']
|
||||
|
||||
|
||||
def test_repair_worker_refuses_the_input_desktop_before_backup(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair_ui as ui, native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
root = tmp_path/'worker'
|
||||
root.mkdir()
|
||||
job = root/'request.json'
|
||||
job.write_text(json.dumps({'project':str(target.project),'database':str(target.database),
|
||||
'root':str(root),'cancel_signal':str(root/'cancel.signal'),'timeout':1,
|
||||
'supervisor_pid':123,'desktop':'CbbRepair_test'}))
|
||||
monkeypatch.setattr(ui, '_current_desktop_name', lambda: 'Default', raising=False)
|
||||
monkeypatch.setattr(native_repair, 'backup_project', lambda *a: pytest.fail('Input desktop repair forbidden'))
|
||||
assert ui.repair_worker_main(job) == 1
|
||||
assert 'REPAIR_PRIVATE_DESKTOP_REQUIRED' in json.loads((root/'repair-result.json').read_text('utf-8'))['detail']
|
||||
|
||||
|
||||
def test_repair_worker_job_binds_supervisor_and_private_desktop(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.native_repair import run_native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
class Worker:
|
||||
returncode = 0
|
||||
def __init__(self, command, desktop):
|
||||
job = json.loads(Path(command[-1]).read_text('utf-8'))
|
||||
assert job['supervisor_pid'] > 0
|
||||
assert job['desktop'] == desktop
|
||||
assert desktop.startswith('CbbRepair_')
|
||||
(Path(job['root'])/'repair-result.json').write_text('{"status":"BLOCKED"}')
|
||||
def poll(self): return 0
|
||||
def bind_cancellation(self, path): pass
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
assert run_native_repair(target, tmp_path/'native')['status'] == 'BLOCKED'
|
||||
|
||||
|
||||
def test_backup_detects_a_file_added_during_the_copy(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
original = native_repair.shutil.copy2
|
||||
def changed(source, destination):
|
||||
result = original(source, destination)
|
||||
(target.project.parent/'new-file.txt').write_bytes(b'concurrent writer')
|
||||
return result
|
||||
monkeypatch.setattr(native_repair.shutil, 'copy2', changed)
|
||||
with pytest.raises(OSError, match='REPAIR_BACKUP_CHANGED'):
|
||||
native_repair.backup_project(target, tmp_path/'backup')
|
||||
@@ -0,0 +1,42 @@
|
||||
from threading import Event, Thread
|
||||
|
||||
from cbb_refresh_verify.operation_queue import NativeQueue, QueueCancelled
|
||||
|
||||
|
||||
def test_paused_ticket_yields_to_a_ready_native_task():
|
||||
queue=NativeQueue()
|
||||
ready, waiting, complete=Event(),Event(),Event()
|
||||
order=[]
|
||||
thread=Thread(target=lambda:queue.run(lambda:(order.append('resumed'),complete.set()),
|
||||
ready=ready.is_set,waiting=waiting.set))
|
||||
thread.start()
|
||||
try:
|
||||
assert waiting.wait(2)
|
||||
queue.run(lambda:order.append('ready'))
|
||||
assert order==['ready']
|
||||
ready.set();queue.wake()
|
||||
assert complete.wait(2)
|
||||
assert order==['ready','resumed']
|
||||
finally:
|
||||
ready.set();queue.wake();thread.join(2)
|
||||
assert not thread.is_alive()
|
||||
|
||||
|
||||
def test_cancelled_paused_ticket_does_not_hold_later_work():
|
||||
queue=NativeQueue()
|
||||
waiting,stop=Event(),Event()
|
||||
outcome=[]
|
||||
def wait():
|
||||
try:
|
||||
queue.run(lambda:outcome.append('unexpected'),ready=lambda:False,
|
||||
waiting=waiting.set,stop=stop)
|
||||
except QueueCancelled:outcome.append('cancelled')
|
||||
thread=Thread(target=wait);thread.start()
|
||||
try:
|
||||
assert waiting.wait(2)
|
||||
stop.set();queue.wake();thread.join(2)
|
||||
assert not thread.is_alive()
|
||||
queue.run(lambda:outcome.append('next'))
|
||||
assert outcome==['cancelled','next']
|
||||
finally:
|
||||
stop.set();queue.wake();thread.join(2)
|
||||
@@ -0,0 +1,92 @@
|
||||
import subprocess
|
||||
import pytest
|
||||
|
||||
|
||||
def test_wmi_timeout_is_unknown_not_idle(monkeypatch):
|
||||
from cbb_refresh_verify.owner_probe import query_owners
|
||||
def hang(command, **kw):
|
||||
assert 0 < kw['timeout'] <= 10
|
||||
raise subprocess.TimeoutExpired(command, kw['timeout'])
|
||||
monkeypatch.setattr(subprocess, 'run', hang)
|
||||
with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'):
|
||||
query_owners(['ExpeditionPCB.exe'])
|
||||
|
||||
|
||||
def test_malformed_wmi_response_is_not_empty_owner_list(monkeypatch):
|
||||
from cbb_refresh_verify.owner_probe import query_owners
|
||||
from pathlib import Path
|
||||
def run(command, **kw):
|
||||
Path(command[-1]).with_name('result.json').write_text('{}')
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
monkeypatch.setattr(subprocess, 'run', run)
|
||||
with pytest.raises(RuntimeError, match='OWNER_QUERY_UNCONFIRMED'):
|
||||
query_owners(['ExpeditionPCB.exe'])
|
||||
|
||||
|
||||
def test_full_snapshot_timeout_is_bounded_and_not_idle(monkeypatch):
|
||||
from cbb_refresh_verify.owner_probe import query_process_snapshot
|
||||
import json
|
||||
from pathlib import Path
|
||||
def hang(command, **kw):
|
||||
assert json.loads(Path(command[-1]).read_text('utf-8')) == {'snapshot_version': 1}
|
||||
assert 0 < kw['timeout'] <= .5
|
||||
raise subprocess.TimeoutExpired(command, kw['timeout'])
|
||||
monkeypatch.setattr(subprocess, 'run', hang)
|
||||
with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'):
|
||||
query_process_snapshot(timeout_seconds=.5)
|
||||
|
||||
|
||||
def test_full_snapshot_keeps_unknown_processes_and_creation_identity(monkeypatch):
|
||||
from cbb_refresh_verify.owner_probe import query_process_snapshot
|
||||
import json
|
||||
from pathlib import Path
|
||||
rows = [{'pid': 0, 'parent': 0, 'name': 'System Idle Process',
|
||||
'command': '', 'executable': '', 'created': ''},
|
||||
{'pid': 999, 'parent': 100, 'name': 'unlisted-child.exe',
|
||||
'command': 'unlisted-child.exe', 'executable': 'C:/native/unlisted-child.exe',
|
||||
'created': '20261105123456.000000+480'}]
|
||||
def run(command, **kw):
|
||||
Path(command[-1]).with_name('result.json').write_text(json.dumps({
|
||||
'snapshot_version': 1, 'owners': rows}), encoding='utf-8')
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
monkeypatch.setattr(subprocess, 'run', run)
|
||||
assert query_process_snapshot() == rows
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['version', 'created', 'duplicate', 'pid'])
|
||||
def test_full_snapshot_rejects_incomplete_response(monkeypatch, change):
|
||||
from cbb_refresh_verify.owner_probe import query_process_snapshot
|
||||
import json
|
||||
from pathlib import Path
|
||||
row = {'pid': 10, 'parent': 1, 'name': 'child.exe', 'command': '',
|
||||
'executable': '', 'created': ''}
|
||||
record = {'snapshot_version': 1, 'owners': [row]}
|
||||
if change == 'version': record['snapshot_version'] = 2
|
||||
if change == 'created': del row['created']
|
||||
if change == 'duplicate': record['owners'].append(dict(row))
|
||||
if change == 'pid': row['pid'] = -1
|
||||
def run(command, **kw):
|
||||
Path(command[-1]).with_name('result.json').write_text(json.dumps(record), encoding='utf-8')
|
||||
return subprocess.CompletedProcess(command, 0)
|
||||
monkeypatch.setattr(subprocess, 'run', run)
|
||||
with pytest.raises(RuntimeError, match='OWNER_QUERY_UNCONFIRMED'):
|
||||
query_process_snapshot()
|
||||
|
||||
|
||||
def test_full_snapshot_helper_does_not_filter_unknown_descendants(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.owner_probe import owner_probe_main
|
||||
import win32com.client
|
||||
from types import SimpleNamespace
|
||||
import json
|
||||
queries = []
|
||||
def query(sql):
|
||||
queries.append(sql)
|
||||
return [SimpleNamespace(ProcessId=42, ParentProcessId=12, Name='custom-native-child.exe',
|
||||
CommandLine='custom-native-child.exe', ExecutablePath='C:/custom-native-child.exe',
|
||||
CreationDate='creation-time')]
|
||||
monkeypatch.setattr(win32com.client, 'GetObject', lambda _: SimpleNamespace(ExecQuery=query))
|
||||
job = tmp_path/'request.json'; job.write_text('{"snapshot_version": 1}')
|
||||
assert owner_probe_main(job) == 0
|
||||
assert len(queries) == 1 and 'WHERE' not in queries[0]
|
||||
row = json.loads(job.with_name('result.json').read_text('utf-8'))['owners'][0]
|
||||
assert row['name'] == 'custom-native-child.exe' and row['created'] == 'creation-time'
|
||||
+10
-4
@@ -1,9 +1,10 @@
|
||||
"""Production safeguards: no input injection, no desktop switching, no menu driving.
|
||||
"""Production safeguards: no input injection or uncontrolled menu driving.
|
||||
|
||||
These asserts encode the operating rules for this tool: the user's mouse,
|
||||
keyboard and desktop are never touched. Dialogs that belong to a process this
|
||||
run started are answered with a posted BM_CLICK, which is a message to that
|
||||
dialog and not user input.
|
||||
dialog and not user input. Native Project Backup Repair is restricted to an
|
||||
owned private desktop/PID, a resolved resource command and an exact PRJ.
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
@@ -40,6 +41,11 @@ def test_no_input_injection_or_desktop_switching_in_production_code():
|
||||
for path in production_sources():
|
||||
text = path.read_text(encoding='utf-8')
|
||||
for api in FORBIDDEN_APIS:
|
||||
if path.name == 'native_repair_ui.py' and api == 'WM_COMMAND':
|
||||
# Only the installed Project Backup Repair menu is allowed:
|
||||
# resolve its native resource, own the private desktop/PID,
|
||||
# and confirm the exact PRJ before posting the command.
|
||||
continue
|
||||
if path.name == 'library_manager_verify.py' and api in {'WM_MOUSEMOVE', 'TVM_SELECTITEM'}:
|
||||
continue
|
||||
if path.name == 'library_manager_verify.py' and api == 'click_input':
|
||||
@@ -89,7 +95,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 +114,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
|
||||
@@ -20,6 +20,90 @@ def test_application_palette_keeps_table_text_readable(qtbot):
|
||||
assert base.value() > 180
|
||||
|
||||
def fake_reader(_): return LibraryReadResult((CbbRecord('CBB1','Reusable Blocks'),))
|
||||
|
||||
def test_global_icdb_action_confirms_and_includes_ready_rows(qtbot, tmp_path, monkeypatch):
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
calls = []
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((
|
||||
CbbRecord('READY', 'LP', status='READY'), CbbRecord('PENDING', 'LP'))),
|
||||
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
|
||||
data_root=tmp_path)
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('lib.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.No)
|
||||
window.icdb_repair_button.click()
|
||||
assert calls == []
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
window.icdb_repair_button.click()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert calls == ['READY', 'PENDING']
|
||||
|
||||
def test_global_icdb_action_disabled_without_library_or_while_busy(qtbot):
|
||||
window = MainWindow(reader=fake_reader)
|
||||
qtbot.addWidget(window)
|
||||
assert not window.icdb_repair_button.isEnabled()
|
||||
window.lmc_edit.setText('lib.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert window.icdb_repair_button.isEnabled()
|
||||
window.busy = True; window._update()
|
||||
assert not window.icdb_repair_button.isEnabled()
|
||||
|
||||
def test_global_icdb_failure_clears_ready_and_stops_queue(qtbot, tmp_path, monkeypatch):
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
calls = []
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((
|
||||
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))),
|
||||
verifier=lambda req: (calls.append(req.block_name) or VerifyResult(
|
||||
'FAIL', stderr='unconfirmed', safe_to_continue=False)), data_root=tmp_path)
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('lib.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
window.icdb_repair_button.click()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert calls == ['A']
|
||||
assert 'A' not in window.ready_names
|
||||
assert window.cbb_table.item(0, 2).text() == '未知 / 需确认'
|
||||
assert window.cbb_table.item(1, 3).text().startswith('SKIPPED')
|
||||
|
||||
def test_global_icdb_progress_and_repair_summary(qtbot, tmp_path, monkeypatch):
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
confirmations = []
|
||||
def confirm(*args, **kwargs):
|
||||
confirmations.append(args[2])
|
||||
return QMessageBox.Yes
|
||||
monkeypatch.setattr(QMessageBox, 'question', confirm)
|
||||
window = MainWindow(reader=fake_reader, data_root=tmp_path,
|
||||
verifier=lambda _: VerifyResult('FAIL', safe_to_continue=False, recovery_attempts=(
|
||||
{'status': 'RESTORED_REVIEW_REQUIRED', 'detail': 'restored old backup'},)))
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('lib.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
window.icdb_repair_button.click()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert 'lib.lmc' in confirmations[0]
|
||||
assert 'Ready' in confirmations[0]
|
||||
assert window.icdb_progress.value() == 1
|
||||
assert window.icdb_progress.maximum() == 1
|
||||
assert '恢复备份待复核 1' in window.status_label.text()
|
||||
assert window.summary.windowTitle() == '全库 ICDB 检查与修复结果'
|
||||
reports = list((tmp_path/'reports').glob('icdb-*.json'))
|
||||
assert len(reports) == 1
|
||||
|
||||
def test_ready_row_missing_project_is_skipped_by_global_maintenance(qtbot, tmp_path):
|
||||
library = tmp_path/'library.lmc'
|
||||
library.touch()
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((CbbRecord('MISSING', 'LP', status='READY'),)),
|
||||
data_root=tmp_path)
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText(str(library)); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert window.skipped_names == {'MISSING'}
|
||||
assert 'MISSING' not in window.ready_names
|
||||
assert not window.icdb_repair_button.isEnabled()
|
||||
assert window.cbb_table.item(0, 2).text().startswith('跳过')
|
||||
|
||||
def test_verify_requires_loaded_library_and_invalidates_on_path_change(qtbot):
|
||||
window=MainWindow(reader=fake_reader, verifier=lambda _: VerifyResult('PASS')); qtbot.addWidget(window)
|
||||
assert not window.batch_button.isEnabled()
|
||||
|
||||
+11
-11
@@ -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=[]
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
"""Destructive probe restricted to a fresh copy of the local test fixture."""
|
||||
from pathlib import Path
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import shutil
|
||||
|
||||
from cbb_refresh_verify.native_repair import RepairTarget, run_native_repair
|
||||
|
||||
|
||||
def main():
|
||||
base = Path(__file__).resolve().parents[1]
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--output', default='icdb-corruption-probe-20261028-r2')
|
||||
parser.add_argument('--without-history', action='store_true',
|
||||
help='Exclude historical ProjectBackup/cdbback data from the new test copy')
|
||||
args = parser.parse_args()
|
||||
source = base / 'diagnostics/icdb-auto-recovery-20261027/project-copy'
|
||||
root = (base/'diagnostics'/args.output).resolve()
|
||||
if root.parent != (base/'diagnostics').resolve():
|
||||
raise ValueError('Output must be a new direct child of the diagnostics directory')
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
project_dir = root / 'project'
|
||||
if any(p.is_symlink() or p.is_junction() for p in source.rglob('*')):
|
||||
raise RuntimeError('Fixture contains links')
|
||||
shutil.copytree(source, project_dir, ignore=(
|
||||
shutil.ignore_patterns('ProjectBackup', 'cdbback') if args.without_history else None))
|
||||
if args.without_history:
|
||||
storage = project_dir/'ProjectBackup'
|
||||
(storage/'backups').mkdir(parents=True)
|
||||
shutil.copy2(source/'ProjectBackup/ProjectBackup.cfg', storage/'ProjectBackup.cfg')
|
||||
if list(project_dir.rglob('*.zip')):
|
||||
raise RuntimeError('Historical archives remain in the no-history test copy')
|
||||
database = project_dir / 'database/icdb.dat'
|
||||
original = database.read_bytes()
|
||||
damaged = bytes(64) + original[64:]
|
||||
database.write_bytes(damaged)
|
||||
record = {'source_sha256': hashlib.sha256(original).hexdigest(),
|
||||
'damaged_sha256': hashlib.sha256(damaged).hexdigest(),
|
||||
'damage': 'first 64 bytes zeroed in isolated copy',
|
||||
'without_historical_backups': args.without_history}
|
||||
(root/'damage.json').write_text(json.dumps(record, indent=2), encoding='utf-8')
|
||||
result = run_native_repair(RepairTarget(project_dir/'IP101GR.prj', database.parent),
|
||||
root/'repair', timeout_seconds=180)
|
||||
result['database_sha256_after'] = hashlib.sha256(database.read_bytes()).hexdigest()
|
||||
result['damaged_bytes_unchanged'] = result['database_sha256_after'] == record['damaged_sha256']
|
||||
(root/'probe-result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
print(json.dumps(result, ensure_ascii=False))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,147 @@
|
||||
"""Real damaged-open / popup recovery / reopened-target comparison.
|
||||
|
||||
All damage and recovery are confined to a fresh diagnostics fixture. The popup
|
||||
uses the production backend and native verifier without injected substitutes.
|
||||
"""
|
||||
from dataclasses import asdict
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
from cbb_refresh_verify.backup_recovery import _manifest
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, run_verify, read_library_flag_if_available
|
||||
|
||||
|
||||
def write(path, value):
|
||||
path.write_text(json.dumps(value, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('phase', choices=('setup', 'open-before', 'restore', 'compare'))
|
||||
args = parser.parse_args()
|
||||
base = Path(__file__).resolve().parents[1]
|
||||
root = base/'diagnostics/icdb-backup-dialog-live-20261028'
|
||||
library = root/'library/mentor_lib.lmc'
|
||||
source = base/'diagnostics/icdb-auto-recovery-20261027/verify-library'
|
||||
project = library.parent/'ReusableBlocksLP/IP101GR/Work/IP101GR.prj'
|
||||
if args.phase == 'setup':
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
if any(p.is_symlink() or p.is_junction() for p in source.rglob('*')):
|
||||
raise ValueError('Source contains links')
|
||||
copied = subprocess.run(['robocopy', str(source), str(library.parent), '/E', '/COPY:DAT',
|
||||
'/DCOPY:T', '/MT:8', '/R:1', '/W:1', '/NFL', '/NDL', '/NJH', '/NJS', '/NP'],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
(root/'copy.log').write_bytes(copied.stdout)
|
||||
if copied.returncode > 7:
|
||||
raise RuntimeError(f'Fixture copy failed: {copied.returncode}')
|
||||
# Remove historical backups only in this new, resolved fixture Work.
|
||||
for path in sorted(project.parent.rglob('*'), reverse=True):
|
||||
if path.is_dir() and path.name in {'ProjectBackup', 'cdbback'}:
|
||||
resolved = path.resolve()
|
||||
if not resolved.is_relative_to((root/'library').resolve()):
|
||||
raise ValueError('History cleanup escaped test fixture')
|
||||
shutil.rmtree(resolved)
|
||||
database = project.parent/'database/icdb.dat'
|
||||
healthy = database.read_bytes()
|
||||
if healthy[:64] == bytes(64):
|
||||
raise ValueError('Fixture baseline is not healthy')
|
||||
database.write_bytes(bytes(64) + healthy[64:])
|
||||
write(root/'source-before.json', _manifest(source))
|
||||
write(root/'setup.json', {'project': project, 'database': database,
|
||||
'damage': 'First 64 bytes zeroed in isolated Work database',
|
||||
'historical_backups_removed': True})
|
||||
print('SETUP_DONE', flush=True)
|
||||
return
|
||||
request = VerifyRequest(library, 'IP101GR', root/args.phase, timeout_seconds=300, auto_repair=False)
|
||||
if args.phase == 'open-before':
|
||||
result = run_verify(request, progress=lambda e: print(json.dumps(e, ensure_ascii=True), flush=True),
|
||||
flag_reader=read_library_flag_if_available)
|
||||
write(root/'open-before.json', asdict(result))
|
||||
log = project.parent/'PCB/LogFiles/PCBLogFile.txt'
|
||||
if log.is_file():
|
||||
(root/'open-before-PCBLogFile.txt').write_bytes(log.read_bytes())
|
||||
print(json.dumps(asdict(result), default=str, ensure_ascii=True), flush=True)
|
||||
return
|
||||
if args.phase == 'restore':
|
||||
before = json.loads((root/'open-before.json').read_text('utf-8'))
|
||||
log = (root/'open-before-PCBLogFile.txt').read_text('utf-8', errors='replace')
|
||||
if before['status'] != 'FAIL' or 'Database cannot be loaded' not in log:
|
||||
raise ValueError('Actual damaged target opening failure must be confirmed first')
|
||||
from PySide6.QtCore import QTimer, Qt
|
||||
from PySide6.QtTest import QTest
|
||||
from PySide6.QtWidgets import QApplication
|
||||
from cbb_refresh_verify.application import configure_qt_application
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
app = QApplication([]); configure_qt_application(app)
|
||||
window = MainWindow(data_root=root/'data'); window.resize(1060, 860)
|
||||
window.show(); window.lmc_edit.setText(str(library))
|
||||
window.libspec_edit.clear(); window.dms_edit.clear()
|
||||
state = {'phase': 'loading'}
|
||||
def tick():
|
||||
if state['phase'] == 'loading' and not window.busy:
|
||||
if not window.loaded_path:
|
||||
raise RuntimeError(window.status_label.text())
|
||||
state['phase'] = 'restoring'
|
||||
QTest.mouseClick(window.backup_restore_button, Qt.LeftButton)
|
||||
dialog = window.backup_dialog
|
||||
def progress(event):
|
||||
print(json.dumps(event, ensure_ascii=True, default=str), flush=True)
|
||||
if event.get('stage') == 'validate_source':
|
||||
candidates = list((root/'data/evidence').glob('backup-restore-*/recovery-request.json'))
|
||||
current = max(candidates, key=lambda p: p.stat().st_mtime_ns)
|
||||
config = json.loads(current.read_text('utf-8'))
|
||||
flag = read_library_flag_if_available(Path(config['validation_library']), 'IP101GR')
|
||||
write(current.parent/'source-before-native-flag.json', asdict(flag))
|
||||
dialog.signals.progress.connect(progress)
|
||||
dialog.source_edit.setText(str(source/'ReusableBlocksLP/IP101GR/Work/IP101GR.prj'))
|
||||
dialog.grab().save(str(root/'dialog-before.png'))
|
||||
def done(name, result):
|
||||
write(root/'restore-ui-result.json', asdict(result))
|
||||
state['phase'] = 'finishing'
|
||||
QTimer.singleShot(200, finish)
|
||||
dialog.completed.connect(done)
|
||||
QTest.mouseClick(dialog.restore_button, Qt.LeftButton)
|
||||
def finish():
|
||||
timer.stop()
|
||||
dialog = window.backup_dialog
|
||||
dialog.grab().save(str(root/'dialog-after.png'))
|
||||
window.grab().save(str(root/'window-after.png'))
|
||||
write(root/'ui-state.json', {'status': dialog.status_label.text(),
|
||||
'target_ready': 'IP101GR' in window.ready_names, 'busy': dialog.busy})
|
||||
dialog.reject(); window.close(); app.quit()
|
||||
timer=QTimer(); timer.setInterval(100); timer.timeout.connect(tick)
|
||||
window.load_library(); timer.start(); app.exec()
|
||||
print((root/'restore-ui-result.json').read_text('utf-8'), flush=True)
|
||||
return
|
||||
before = json.loads((root/'open-before.json').read_text('utf-8'))
|
||||
restored = json.loads((root/'restore-ui-result.json').read_text('utf-8'))
|
||||
record = json.loads(Path(restored['evidence_path']).read_text('utf-8'))
|
||||
final = record['result']
|
||||
retained = Path(record.get('retained_work', ''))
|
||||
after_evidence = Path(final['evidence_path']).parent
|
||||
events = [json.loads(line) for line in (after_evidence/'events.jsonl').read_text('utf-8').splitlines()]
|
||||
native_log = project.parent/'PCB/LogFiles/PCBLogFile.txt'
|
||||
text = native_log.read_text('utf-8', errors='replace')
|
||||
checks = {
|
||||
'real_open_failed_before': before['status'] == 'FAIL' and before['error_category'] == 'MENTOR_DATABASE_LOAD_FAILED',
|
||||
'source_copy_verified': record['source_validation']['status'] == 'PASS'
|
||||
and record['source_validation']['library_flag'] == 'GREEN',
|
||||
'same_target_opened_after': any(e.get('stage') == 'identity_verified'
|
||||
and e.get('project') == str(project) for e in events),
|
||||
'restored_target_passed': final['status'] == 'PASS' and final['library_flag'] == 'GREEN'
|
||||
and final['safe_to_continue'],
|
||||
'old_damaged_work_retained': (retained/'database/icdb.dat').read_bytes()[:64] == bytes(64),
|
||||
'original_source_unchanged': _manifest(source) == json.loads((root/'source-before.json').read_text('utf-8')),
|
||||
'load_error_absent_after': 'Database cannot be loaded' not in text,
|
||||
}
|
||||
write(root/'comparison.json', {'checks': checks, 'passed': all(checks.values())})
|
||||
print(json.dumps(checks), flush=True)
|
||||
if not all(checks.values()):
|
||||
raise RuntimeError('Real recovery comparison failed')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Exercise the real Qt button and native verifier on a fresh local fixture copy.
|
||||
|
||||
Run with PYTHONPATH=src. No verifier/reader stubs or synthetic errors are used.
|
||||
The source library is fingerprinted before and after the run.
|
||||
"""
|
||||
from pathlib import Path
|
||||
import hashlib
|
||||
import json
|
||||
import shutil
|
||||
import time
|
||||
|
||||
from PySide6.QtCore import QTimer, Qt
|
||||
from PySide6.QtTest import QTest
|
||||
from PySide6.QtWidgets import QApplication, QMessageBox
|
||||
|
||||
from cbb_refresh_verify.application import configure_qt_application
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
|
||||
|
||||
def fingerprint(root):
|
||||
result = {}
|
||||
for path in root.rglob('*'):
|
||||
if path.is_symlink() or path.is_junction():
|
||||
raise ValueError('Test source contains links')
|
||||
if path.is_file():
|
||||
with path.open('rb') as stream:
|
||||
result[str(path.relative_to(root))] = hashlib.file_digest(stream, 'sha256').hexdigest()
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
base = Path(__file__).resolve().parents[1]
|
||||
source = base/'diagnostics/icdb-auto-recovery-20261027/verify-library'
|
||||
root = base/'diagnostics/icdb-button-live-20261028'
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
before = fingerprint(source)
|
||||
(root/'source-before.json').write_text(json.dumps(before), encoding='utf-8')
|
||||
library = root/'library'
|
||||
shutil.copytree(source, library)
|
||||
app = QApplication([])
|
||||
configure_qt_application(app)
|
||||
window = MainWindow(data_root=root/'data')
|
||||
window.resize(1060, 820)
|
||||
window.show()
|
||||
window.libspec_edit.clear()
|
||||
window.dms_edit.clear()
|
||||
window.lmc_edit.setText(str(library/'mentor_lib.lmc'))
|
||||
state = {'phase': 'loading', 'started': time.monotonic()}
|
||||
|
||||
def event(record):
|
||||
if record.get('stage') in {'icdb_check_start', 'native_repair_start', 'PASS', 'FAIL', 'BLOCKED', 'TIMEOUT'}:
|
||||
print(json.dumps(record, ensure_ascii=True), flush=True)
|
||||
window.signals.event.connect(event)
|
||||
|
||||
def tick():
|
||||
for widget in app.topLevelWidgets():
|
||||
if isinstance(widget, QMessageBox) and widget.isVisible() and widget.windowTitle() == '全库 ICDB 检查与修复':
|
||||
widget.grab().save(str(root/'confirmation.png'))
|
||||
state['confirmation'] = widget.text()
|
||||
QTest.mouseClick(widget.button(QMessageBox.Yes), Qt.LeftButton)
|
||||
if state['phase'] == 'loading' and not window.busy:
|
||||
if not window.loaded_path:
|
||||
state.update(phase='load_failed', detail=window.status_label.text())
|
||||
finish()
|
||||
return
|
||||
state.update(phase='running', rows=window.cbb_table.rowCount(),
|
||||
ready_before=len(window.ready_names), preflight_skipped=len(window.skipped_names))
|
||||
window.grab().save(str(root/'loaded.png'))
|
||||
QTimer.singleShot(0, lambda: QTest.mouseClick(window.icdb_repair_button, Qt.LeftButton))
|
||||
elif state['phase'] == 'running' and not window.busy and hasattr(window, 'summary'):
|
||||
state.update(phase='finished', detail=window.status_label.text(), ready_after=len(window.ready_names))
|
||||
window.grab().save(str(root/'finished.png'))
|
||||
window.summary.grab().save(str(root/'summary.png'))
|
||||
finish()
|
||||
elif time.monotonic()-state['started'] > 900 and not window.stop.is_set():
|
||||
window.request_stop()
|
||||
|
||||
def finish():
|
||||
timer.stop()
|
||||
after = fingerprint(source)
|
||||
state.update(source_unchanged=before == after, source_file_count=len(before),
|
||||
elapsed_seconds=time.monotonic()-state['started'])
|
||||
(root/'outcome.json').write_text(json.dumps(state, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
print(json.dumps(state, ensure_ascii=True), flush=True)
|
||||
if hasattr(window, 'summary'):
|
||||
window.summary.close()
|
||||
window.close()
|
||||
app.quit()
|
||||
|
||||
timer = QTimer()
|
||||
timer.setInterval(100)
|
||||
timer.timeout.connect(tick)
|
||||
window.load_library()
|
||||
timer.start()
|
||||
app.exec()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,136 @@
|
||||
"""Real before/repair/after test, confined to a newly cloned local fixture.
|
||||
|
||||
Run each phase separately so the actual native failure can be reviewed before
|
||||
repair. The opening phases use the production verifier with auto repair off.
|
||||
"""
|
||||
import argparse
|
||||
from dataclasses import asdict
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
|
||||
from cbb_refresh_verify.native_repair import RepairTarget, run_native_repair
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, preflight, run_verify, read_library_flag_if_available
|
||||
|
||||
|
||||
def write(path, record):
|
||||
path.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
|
||||
|
||||
def require_released():
|
||||
from cbb_refresh_verify.mentor_adapter import pcb_processes, _process_details
|
||||
if pcb_processes() or _process_details('LibraryManager.exe'):
|
||||
raise RuntimeError('Native PCB or Library Manager remains open; stop before repair/reopen')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('phase', choices=('setup', 'open-before', 'repair', 'open-after', 'compare'))
|
||||
parser.add_argument('--output', default='icdb-open-repair-reopen-20261028')
|
||||
args = parser.parse_args()
|
||||
base = Path(__file__).resolve().parents[1]
|
||||
root = (base/'diagnostics'/args.output).resolve()
|
||||
if root.parent != (base/'diagnostics').resolve():
|
||||
raise ValueError('Output must be a direct child of diagnostics')
|
||||
require_released()
|
||||
if args.phase == 'setup':
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
source = base/'diagnostics/icdb-auto-recovery-20261027/verify-library'
|
||||
if any(p.is_symlink() or p.is_junction() for p in source.rglob('*')):
|
||||
raise ValueError('Source contains directory/file links')
|
||||
command = ['robocopy', str(source), str(root/'library'), '/E', '/COPY:DAT', '/DCOPY:T',
|
||||
'/MT:8', '/R:1', '/W:1', '/NFL', '/NDL', '/NJH', '/NJS', '/NP']
|
||||
copied = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
(root/'copy.log').write_bytes(copied.stdout)
|
||||
if copied.returncode > 7:
|
||||
raise RuntimeError(f'Fixture copy failed: {copied.returncode}')
|
||||
request = VerifyRequest(root/'library/mentor_lib.lmc', 'IP101GR', root/'preflight')
|
||||
project, pcb = preflight(request)
|
||||
database = project.parent/'database/icdb.dat'
|
||||
original = database.read_bytes()
|
||||
if not original or original[:64] == bytes(64):
|
||||
raise ValueError('Source is not a healthy baseline')
|
||||
(root/'original-icdb.dat').write_bytes(original)
|
||||
damaged = bytes(64)+original[64:]
|
||||
database.write_bytes(damaged)
|
||||
write(root/'setup.json', {'source': source, 'library': request.lmc_file,
|
||||
'project': project, 'pcb': pcb, 'database': database,
|
||||
'source_sha256': hashlib.sha256(original).hexdigest(),
|
||||
'damaged_sha256': hashlib.sha256(damaged).hexdigest(),
|
||||
'damage': 'Zero first 64 bytes in the new isolated Work database',
|
||||
'auto_repair_during_open': False})
|
||||
print('SETUP_DONE', flush=True)
|
||||
return
|
||||
config = json.loads((root/'setup.json').read_text('utf-8'))
|
||||
project, database = Path(config['project']), Path(config['database'])
|
||||
if not project.resolve().is_relative_to(root) or not database.resolve().is_relative_to(root):
|
||||
raise ValueError('Target escaped the isolated test directory')
|
||||
if args.phase == 'compare':
|
||||
before = json.loads((root/'open-before.json').read_text('utf-8'))
|
||||
after = json.loads((root/'open-after.json').read_text('utf-8'))
|
||||
repair = json.loads((root/'repair/repair-result.json').read_text('utf-8'))
|
||||
before_job = json.loads(Path(before['evidence_path']).with_name('request.json').read_text('utf-8'))
|
||||
after_job = json.loads(Path(after['evidence_path']).with_name('request.json').read_text('utf-8'))
|
||||
before_log = (root/'open-before-PCBLogFile.txt').read_text('utf-8', errors='replace')
|
||||
after_log = (root/'open-after-PCBLogFile.txt').read_text('utf-8', errors='replace')
|
||||
events = [json.loads(line) for line in
|
||||
Path(after['evidence_path']).with_name('events.jsonl').read_text('utf-8').splitlines()]
|
||||
source_database = Path(config['source'])/'ReusableBlocksLP/IP101GR/Work/database/icdb.dat'
|
||||
checks = {
|
||||
'same_project': before_job['project'] == after_job['project'] == str(project),
|
||||
'same_database_repaired': repair['database'] == str(database.parent),
|
||||
'native_icdb_error_confirmed_before': before['status'] == 'FAIL'
|
||||
and str(database) in before_log and 'iCDB Project Backup' in before_log,
|
||||
'identity_verified_after': any(e.get('stage') == 'identity_verified'
|
||||
and e.get('project') == str(project) for e in events),
|
||||
'opening_error_absent_after': 'Database cannot be loaded' not in after_log and not after['stderr'],
|
||||
'verification_passed_after': after['status'] == 'PASS' and after['safe_to_continue']
|
||||
and after['library_flag'] == 'GREEN',
|
||||
'original_source_database_unchanged': hashlib.sha256(source_database.read_bytes()).hexdigest()
|
||||
== config['source_sha256'],
|
||||
}
|
||||
result = {'checks': checks, 'comparison_passed': all(checks.values()),
|
||||
'before_error_category': before['error_category'],
|
||||
'native_repair_status': repair['status'],
|
||||
'restored_from_backup': repair.get('evidence', {}).get('restored_from_backup', ''),
|
||||
'after_library_flag': after['library_flag_detail'],
|
||||
'server_session_loss_reproduced': 'last server session' in before_log.casefold()}
|
||||
write(root/'comparison.json', result)
|
||||
print(json.dumps(result, ensure_ascii=True), flush=True)
|
||||
if not result['comparison_passed']:
|
||||
raise RuntimeError('Before/repair/reopen comparison did not pass every check')
|
||||
return
|
||||
from cbb_refresh_verify.icdb_ownership import unreleased_addresses
|
||||
addresses = unreleased_addresses(config['library'], project, config['pcb'])
|
||||
if addresses:
|
||||
raise RuntimeError(f'Database ownership not released: {addresses}')
|
||||
if args.phase == 'repair':
|
||||
before = json.loads((root/'open-before.json').read_text('utf-8'))
|
||||
if before['status'] in {'PASS', 'TIMEOUT'}:
|
||||
raise RuntimeError('No confirmed opening failure available for a repair comparison')
|
||||
native_error = (root/'open-before-PCBLogFile.txt').read_text('utf-8', errors='replace')
|
||||
if str(database).casefold() not in native_error.casefold() or 'iCDB Project Backup' not in native_error:
|
||||
raise RuntimeError('Native log does not confirm an iCDB repair request for the damaged target')
|
||||
result = run_native_repair(RepairTarget(project, database.parent), root/'repair', timeout_seconds=240)
|
||||
write(root/'repair-summary.json', result)
|
||||
else:
|
||||
if args.phase == 'open-after':
|
||||
repaired = json.loads((root/'repair-summary.json').read_text('utf-8'))
|
||||
if repaired['status'] not in {'REPAIRED', 'RESTORED_REVIEW_REQUIRED'}:
|
||||
raise RuntimeError('Repair did not reach a completed native result')
|
||||
request = VerifyRequest(Path(config['library']), 'IP101GR', root/args.phase,
|
||||
timeout_seconds=300, auto_repair=False)
|
||||
def progress(event):
|
||||
print(json.dumps(event, ensure_ascii=True, default=str), flush=True)
|
||||
result = asdict(run_verify(request, progress=progress,
|
||||
flag_reader=read_library_flag_if_available))
|
||||
write(root/(args.phase+'.json'), result)
|
||||
native_log = Path(config['pcb']).parent/'LogFiles/PCBLogFile.txt'
|
||||
if native_log.is_file():
|
||||
(root/(args.phase+'-PCBLogFile.txt')).write_bytes(native_log.read_bytes())
|
||||
print(json.dumps(result, ensure_ascii=True, default=str), flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user