fix: isolate ICDB checks and recover scoped native sessions

This commit is contained in:
阿白叔 committed 2026-11-05 21:52:38 +08:00
1 parent 1acfbd6374
commit 1bd7589fef
82 files changed
+7770 -1583

No files matched your search

@@ -0,0 +1,37 @@
# 后台 ICDB 检查与原生任务排队
## 用户约定
ICDB 检查保持界面可用;校验、库管理、恢复、原生版本窗口等操作提交后串行排队,检查每完成一个 CBB 让出原生执行机会。检查与校验相互独立;检查不自动 Repair、Restore,不改变权威 READY 状态。
## 已确认的原因与修改
- 旧检查占用 MainWindow 的整批 busy 标志,缓存筛选及其他入口随整批禁用。改为独立 IcdbCheckTask、独立取消信号和捕获的工程/库快照;NativeQueue 每项准入,可取消等待,暂停检查不挡后续任务。
- 全局 PCB COM/ROT 可能等待其他工程的模态窗口。检查改为本任务完整 PCB 参数/PID、当前私有桌面、目标 MDI 和本次原生日志联合取证;正式校验继续使用原 COM 集成接口。
- PCB 参数匹配原为子串匹配,会把 `.pcb.backup` 或 `-ReusableCircuitOther` 误认。现按完整命令行参数匹配。
- DialogAgent 在读取子控件/UIA 之前按本任务 PID 范围过滤,执行前重新确认 HWND/PID/class;不读取或点击同桌面的无关弹窗。
- PCB 的临时启动窗口、普通/Reused Block 标题转换及 WM_CLOSE 后 IE 辅助窗口的短暂变化,不能解释为 ICDB 损坏。关闭前必须有目标加载证据;关闭后等待本任务退出,未知窗不点击、不重新发 WM_CLOSE。持续未知窗至期限结束才保留现场并报告。
- 诊断跳过无关集成历史日志,日志读取限定内容和两秒读取期限;超时结束该项,不反复堆积读线程。历史目录较多、历史日志较大不能直接判工程异常。
- ProcessWatch 用已打开的进程句柄观察退出,避免 COM 等待阶段每次轮询都启动 WMI 辅助进程。
- 原生退出后才产生完整 PCB Job Name 的缓冲日志必须在关闭后再核对。PASS 同时要求新 PCB 成功日志、新数据库路径和 PCB PID 装载日志,以及完整 PCB Job Name。
- 失败性质和实际释放事实分别记录。错误包含 ICDB 才标明 ICDB 错误;读写确认、版本选择和未确认原因单独显示。单项失败且释放已确认时继续下一项。
- 后台切库时只更新匹配的库清单;旧工程检查建议在后续管理操作后失效。旧库结果的释放入口不能释放当前另一个库。
- 网络映射由全部活跃 lease 登记保护,包括读取线程尚未交付给界面的 lease;拥有者结束时向仍在使用者转移映射。网络收尾需离开 Qt 线程,避免慢网络同步冻结界面。
## 原生验证范围
Windows 11、Mentor EE7.9.5。实验仅使用 `C:/Temp/CbbBackground-*` 中的完整隔离库;原始六个主要文件记录 SHA-256,来源保持不变。故意修改副本 `icdb.dat` 文件头得到 `SingleFile: ERROR ... in old, unsupported format`,用于验证当前错误分类和失败后的安全退出,不能代替现场失联故障验收。
`diagnostics/icdb-authority-20261105/validate_background_native.py` 运行应用的 `run_verify(maintenance=True)` 完整检查入口,在每次工作桌面放置另一个 PID 的真实模态窗口。要求损坏项报告明确 ICDB 错误并安全退出,下一正常项完成打开检查;无关弹窗在本任务结束前保持未操作;不得执行自动修复。
原始日志及结果在 `diagnostics/icdb-authority-20261105/background-native-result.json` 和对应 C:/Temp 证据目录。冻结包的 BUILD.json、VALIDATION.json 分别记录源码哈希与本包验收证据。
源代码完整链路于 19:16—19:17 通过:损坏项 FAIL / ICDB_REPAIR_REQUIRED / safe_to_continue=true,耗时 62.062 秒;下一正常项 CHECKED / safe_to_continue=true,耗时 26.704 秒。两个其他 PID 的模态窗口均保持未操作,来源六文件哈希不变。此记录另存 `background-native-source-result.json`;最终候选包必须再次通过自身冻结执行器的原生验证。
`validate_background_gui.py` 额外通过真实检查按钮提交检查,再提交权威状态读取并测试排队期间缓存筛选。其单行目录 PENDING 状态为测试注入,原生打开、退出和后续状态读取均实际执行;这不构成现场真实库状态的验收。
r9 冻结执行器实测通过同一批次:故障项 68.531 秒,安全释放;正常项 33.313 秒,CHECKED 且安全释放,其中启动 17.093 秒、关闭 8.703 秒;两个无关弹窗未操作。界面实测 34.703 秒,检查和状态读取顺序为 check-start → check-end → status-start → status-end,排队期间缓存筛选可用,50ms 事件循环计时器最大间隔 79ms,后续原生状态为 READY。该时间只是本机当前样本结果,不作跨样本速度提升比例。
## 保留限制
检查仍需实际启动 Mentor。去掉 COM/WMI 和无关日志开销,不等于消除原生程序启动和数据库加载时间。日志或文档身份不足时报告未确认,不推断 ICDB 损坏。实际未释放的当前任务会停止后续原生写入并提供释放入口,界面缓存操作继续可用。ICDB 自动修复和现场服务器会话失联恢复未验收;本候选包不关闭工单、不作为正式发布。
@@ -1,5 +1,7 @@
# 阻塞范围收紧记录
> 后续审查已修正本记录中“切库清除旧标志”和“只检查 Work”的不足。当前行为与验证请以 [全链路审计](2026-11-05-blocking-lock-audit.md) 为准:状态按库保存,释放包含共享数据库并绑定原任务范围。
本轮处理的是“一个 CBB 出问题后把整批和其他库一起停掉”的误阻塞。
## 已收紧
@@ -0,0 +1,132 @@
# 全链路阻塞与锁审计(2026-11-05)
本次目标:枚举从按钮到批次结束的阻塞条件;消除把单项失败、历史记录或无关进程当成本任务仍占用的情况;用实际运行核对退出和下一项继续。
这是一份开发与验证记录,不把维护说明加入产品 UI。ICDB 批量检查不执行自动 Repair,不把普通打开失败推定为 ICDB 损坏。真正仍在运行的同库写会话继续串行;不能通过删除 Mentor 地址/锁文件、强杀数据库进程来制造“已释放”。
## 已复现的现场链路
旧候选包对 IP101GR 打开检查时,原生 PCB 显示完整的 `Continue to open the design with read/write access permission?` 问题。程序报 `DATABASE_RECOVERY_REQUIRED`,启动收尾函数却只接受 ICDB/数据库加载失败类别,返回 `STARTUP_FAILURE_UNCONFIRMED`,未取消打开。PCB 60544 和启动器 63824 留在本任务私有桌面,导致后续会话不释放。
2026-11-05 使用修改后的真实退出函数,核对本次进程、工程参数、私有桌面和精确原生全文后点击 **No (7)**;两个进程均退出。6 个主要文件(LMC、PRJ、PCB、工程数据库、Layout、PCB工程配置)前后 SHA-256 一致。原有两个 Project Backup 会话没有被终止,没有执行 Repair/Restore。
证据:`diagnostics/icdb-authority-20261105/field-readwrite-cleanup/result.json`。旧候选包原始 evidence 保留,不覆盖。
## 逐阶段清单
“保留”表示正在执行或归属无法确认时仍需要该条件;“修正”表示本次已定位误阻塞并实施回归修复。最终运行结果在文末登记,不能仅凭表格认为现场验收成功。
|序号|阶段 / 代码入口|触发条件及原有影响|处理|
|---|---|---|---|
|1|UI `busy` / ServiceWorker|真实任务执行中禁用启动、编辑路径、关闭;任务异常后必须恢复按钮|保留串行;检查 finished/failure 收尾|
|2|UI 已加载路径|输入路径改变但仍使用旧清单|保留身份检查,不启动旧目标|
|3|UI 选择 / 筛选|隐藏项、READY、缺工程进入检查队列|保留:隐藏项取消勾选;READY 即使选中也排除检查;管理选择独立|
|4|UI 状态刷新|检查结束后条目仍按旧状态禁用或勾选|刷新受影响条目,保留搜索;READY 清旧勾选|
|5|UI management unsafe|切换库清掉唯一的未释放标志,返回原库失去状态|修正:按库保存;A 不影响 B,回到 A 仍保留真实未释放事实|
|6|UI 原生版本会话|正在运行的同库版本窗口与写任务冲突|保留;其它库和清单筛选独立|
|7|版本窗口返回|启动器退出但结果缺失/身份错/子会话未确认|保留当前会话,不因窗口消失虚报恢复成功;释放入口可独立重查|
|8|全局原生 mutex|真实另一个校验/管理/恢复正在使用共享原生通道|保留实际串行;不把互斥失败持久化成本任务数据库 owner|
|9|库协调 OS handle|上传工具/本程序对同库持有独占句柄|保留真实冲突;不能用删文件绕过|
|10|协调文件创建|权限/路径/网络错误也被统一报“有锁”|修正:区分 sharing/lock violation 和访问错误;启动前失败可重试|
|11|历史 checkpoint|同库旧记录、pending、损坏 JSON 一律阻止管理|提供有证据的释放/归档;归属完全丢失时不能证明未知分析副本,列明未确认原因,不擅删|
|12|retained 管理 lease|失败时保留的句柄没有可再次检查的 release probe|修正可重查的精确 operation / scope;未退出仍保留|
|13|显式释放 scope|全机 retained exporter/worker 阻止另一库释放|修正按 operation / library 范围,不采用同名即归属|
|14|释放范围|只检查目标 Work,遗漏共享符号库;或遍历全部库和快照|修正目标库 / 目标 CBB / 实际共享路径,保留共享库互斥|
|15|owner 匹配|原始字符串包含 Work 会误匹配 WorkOld,job.json worker 又漏匹配|精确参数/目录边界及记录的 operation 归属;无法读取归属单列未确认|
|16|PID 重用|旧 PID 恰巧分给另一进程,形成永久旧 owner|新记录绑定创建时间/句柄,旧格式需独立释放证据|
|17|工程 preflight|缺 PRJ/PCB、无工具、非法参数|保留单项失败;未启动会话不保留执行锁、不停其它项|
|18|post-repair proof|非法凭证在任何 worker 创建前被标为 unsafe|修正为启动前拒绝,下一项可继续|
|19|content-check timeout|只读辅助进程超时与真实原生会话混淆|必须区分子进程是否退出,不以 timeout 字样推断 owner|
|20|DesktopWorker 构造|CreateDesktop/CreateJob/CreateProcess 失败,未创建 worker 仍被标 unsafe|修正句柄回滚并返回显式 no-process 证据;已部分启动仍未确认|
|21|原生读写确认|要求确认无其他用户,原程序不 Yes 也不 No,留下 PCB|修正:仅精确全文、PID、工程、桌面一致时 No;等待退出,绝不自动 Yes|
|22|PCB 历史版本选择|有两个版本,需要用户选择;不能代选|记录候选版本;扫描不执行历史回退,取消已识别的本次打开后收尾|
|23|其它已知 fatal|确认错误后仍存在空 PCB|正常关闭本任务空壳;不能要求先判为 ICDB 才允许收尾|
|24|未知弹窗 / 文档|不能证明是空壳、存在文档、非本进程、不同桌面|保留,不盲点按钮;显式给出未释放窗口证据|
|25|PCB 先退出|common 启动器延迟退出|等待 PCB 与启动器,不在任一仍活着时说释放|
|26|No 后旧地址|尝试打开被明确拒绝,但旧地址让本任务空壳不能退出|修正:取消本次打开不要求先取得外部数据库;保留地址,不声称数据库无人占用|
|27|COM 无文档 / ROT|COM 指向他人会话或 ActiveDocument 为空|只依据本任务进程/路径/桌面关闭空壳,不借全局 ROT 关闭他人工程|
|28|保存 / Close 超时|COM 仍可能在写;第二个 close helper 会重复操作|保留已有 helper,观察其释放,不强杀 PCB/数据库或重复保存|
|29|取消 / 主窗口关闭|取消发生在启动、校验、保存任意阶段|合作停止本任务,禁止新项;收尾确认后可再次启动|
|30|会话释放证据|worker 已退出,但 native/automation client 仍在|保留真实未释放;只有独立退出证明才能提升安全状态|
|31|可选报告/性能记录|释放成功后写报告失败又变成 unsafe|修正:报告失败保留告警,不覆盖执行结果和退出事实|
|32|progress / 批次报告|回调/磁盘写入异常终止循环,UI generic failure 挂锁|修正隔离展示错误,保留已完成结果与释放事实|
|33|批次单项 FAIL/BLOCKED/TIMEOUT|某项失败但已正常退出,被错误扩大成全批失败|依据实际 release 继续;用户取消/真正未知会话仍停止同通道任务|
|34|管理预检查|desktop/mapping/capability/source 失败,尚未原生激活即 unsafe|修正显式 activation-start 状态;未启动可重试|
|35|管理 catalog 失败|返回 success=false/safe=true 被异常丢掉 release|修正操作成功与会话释放分别传播|
|36|管理检查点恢复|catalog_before 中断就阻止所有 CBB,和实际 remove 中断混同|区分 mutation 前失败和已发生修改;后者保留数据恢复要求|
|37|无关 LM/PCB/viewdraw|全机任意同名进程阻止目标库检查/恢复/导出|绑定目标工程、库和共享数据库;COM 激活若无法证明隔离仍单独说明|
|38|工程内容导出|收养所有同名 iCDB server / exporter,等待无关进程永不退出|修正仅本次后代和精确分析副本 owner;保留实际后代|
|39|导出副本回收|一个库的 retained export 挡住另一个库|修正 registry 库/副本身份,配合数据库独占读取|
|40|sAddress.adr|存在即被称为“有人占用”|存在只表示需原生确认;保留文件,NOT_IN_USE 只能由严格绑定的 inspection 得出|
|41|备份恢复异常|源路径/清单预检失败也报 unsafe、UI 挂锁|修正沿用是否实际启动/释放事实;目标改动与内容冲突仍阻止安装|
|42|批量状态读取协议|A 响应不匹配就把未查的 B 等都标 MALFORMED|修正重启桥接并逐项读取,不伪造后续状态|
|43|状态缓存身份|只凭字节/名称复用另一库的结果|修正规则版本 + canonical library / block type 身份|
|44|映射关闭|先置 closed,再取消映射;失败以后不能重试|修正确认成功才关闭;保留用户/被改指向的映射|
|45|无关盘枚举失败|另一个盘查询失败阻止目标 share 复用|修正独立确认目标盘;未知盘只作为不可用盘符|
|46|真实地址/共享库占用|同目标 / 实际共享数据库活动 writer|保留实际写互斥;普通错误不得扩展成永久禁用其它独立功能|
|47|未知执行异常|无法知道是否已经启动/改数据|保持未确认;修已知 prelaunch/reporting 根因,不能把一切异常默认放行|
|48|退出后 cscript owner 查询|3 秒包含 helper/Python/COM 启动,现场实际约 4.75 秒,误报查询超时|真实隔离批次复现;改为统一 10 秒有上限查询,超时仍是未确认而非确认占用|
|49|导出全进程 WMI|直接 WMI 卡住会让 supervision 长期保留 worker|移入有上限只读 helper,保留所有名称的真实后代;启动前查询失败不会制造 native owner|
|50|操作后内容/状态失败|移除/新增已返回安全退出,但后续读取失败又丢失释放结论|内容恢复状态与进程释放分别保留,不能用 mutated 标签代替活动会话证明|
|51|启动前证据磁盘错误|目录/request/worker.log 创建失败,被通用异常当原生会话未释放|明确尚未创建 worker 的失败可继续;runtime 指纹写入为可选,不掩盖真实结果|
|52|证据查看器|打开证据路径失败进入通用 worker 错误处理,错误保留映射|只显示查看失败,不创建数据库未释放状态|
|53|维护库归属|目标 RepairTarget 可证明所属库,却仍全局检查 retained worker/export|从原 PRJ 的唯一有效 CentralLibrary 解析身份,复用统一归属检查;普通扫描保持独立|
|54|同库路径别名|同库 drive/UNC/大小写别名被当成另一个库,或 UI resolve 访问失联网络|使用已有 lease 映射和本地规范键,不在控件更新中访问网络|
|55|同盘符多个 retained 库|A 释放后所有权转到当前 A,关闭 A 会断掉仍有 B 会话使用的盘符|关闭前把所有权交给同盘符/共享路径的 retained B;B 未释放不取消映射|
|56|目录输入的库身份|原始输入为目录,释放同目录任意 LMC 都可能误匹配|读取时绑定选中的实际 LMC;未绑定目录不接受 sibling LMC 释放证明|
|57|已释放但待数据恢复|同目标正确阻止重放,却把旧 safe=true 改成 false,进而停止独立目标|保留同目标数据恢复要求,同时保留已释放事实,独立目标可继续|
|58|真实 EDIF 参数形式|程序发出 `-icdb <目录>`,归属解析器只认 `-icdb=<目录>`|兼容实际分离参数并按完整目录边界匹配,不把明确无关的导出器当未知 owner|
|59|Add 的 pre-capture 准备|原始库引用/清单/工程对齐失败发生在导出前,却继承 unsafe|准备阶段失败明确未启动;实际 capture 后始终沿用真实释放证据|
|60|最后一笔导出进程记录|native 和数据库已释放,但 finally 中的 process.json 写入异常覆盖结果|最终报告 I/O 保留真实 release;无退出证明的分支仍保留 owner|
|61|冻结代理启动与 PCB 门禁|代理十秒未就绪可误报,工作进程又只等十五秒|代理启动 30 秒、工作进程门禁 45 秒共享常量;日常 IPC 仍为 2 秒,监听以 0.2 秒轮询响应取消|
|62|连接握手/门禁取消竞态|用户在 handshake 时取消,父进程仍可能先放行 PCB|认证后、门禁放行前、worker 执行原生前都再次检查取消;等待门禁时也响应取消|
|63|残留释放入口|只显示存在进程即拒绝,没有正常关闭、保存提示或确认后结束的路径|增加精确名单、逐项关闭、保存选择、独立强制确认、重新证明与继续未执行项;未知归属也显示供定位|
|64|释放后的全库证明|对普通输出逐个重复 resolve,同一副本 23,218 条记录导致原生退出后仍等待约 97 秒|一次规范库路径;复用已拒绝链接的扫描 metadata,先筛原生数据库候选再独占读取;同一库证明约 6.9 秒|
|65|管理启动前历史阻止|旧记录让新任务尚未启动便失败,却被 LibraryBusy 类型强制改成 unsafe,UI 永久禁用;扫描也不显示记录和无 JSON 的 retained lease|新任务在互斥内先做完整复核,归档旧记录并恢复句柄/映射;未启动不制造 owner,UI 旧标志转为重查要求,历史/内存记录可见;同目标未恢复修改仍禁止重放|
## 执行与验收顺序
1. 保存现场弹窗、PID、request、release 和主要文件 hash;正常取消本次遗留打开。
2. 各域先写失败回归,分别修空壳/弹窗、UI/批次、库协调/管理、导出/恢复、网络/状态桥接。
3. 统一串行运行全量测试,避免测试之间竞争真实 Windows named mutex。
4. 在新隔离库运行应用检查入口:故障/提示项 → 正常项;确认无自动 Repair,无遗留本次 native owner,原来源文件保持不变。
5. 新目录生成候选冻结包;校验冻结源码一致、启动/退出、x86 桥接、冻结检查链路,再打开供用户测试。
## 验证记录(持续更新)
- 现场遗留读写确认:真实 No 取消,PCB 60544 / launcher 63824 退出,6 项 hash 一致。
- startup / desktop / native repair 相关回归:194 passed(后续统一全量再次覆盖)。
- 第一轮真实副本批次:**未通过**。成功复现原始读写确认;新取消路径正常退出,但随后 cscript owner helper 的 3 秒超时将结果重新标为 unsafe,后两项 SKIPPED。原库 6 项 hash 一致。保留 `blocking-live/result.json` 与完整原始记录。该误阻塞已新增回归修正,原副本重跑结果见下一条。
- 修正后的真实副本批次:**通过退出与继续验收**。`blocking-live/retry1/result.json`:现场副本 `FAIL / DATABASE_SAFETY_STOP / safe=true`(No 正常取消);故意损坏副本 `FAIL / ICDB_REPAIR_REQUIRED / safe=true`;健康副本 `CHECKED / safe=true`。三项各自 `session-release.json` 均为 worker 已退出且 remaining_pids=[]。源 6 项 hash 一致,无 Repair/Restore/保存集成。此结果证明失败项能够退出并继续,不证明故障内容已修复。
- 第一轮统一全量:1452 passed / 3 failed。三项为旧测试的相对路径归属 fixture、缺 library 的内部请求 fixture,以及新增的应用 JSON 回收 callsite 未纳入静态审计白名单。已限定修正 fixture 和 JSON callsite;未放宽未知原生归属、未允许删除 Mentor 文件。审查额外发现的 55–60 项已补回归并纳入下述复跑。
- 统一全量复跑:**1479 passed**,1 条既有 pywinauto STA warning;`blocking-final-pytest.xml`。编译与 diff 检查通过。
- 后续冻结实测:现场副本 FAIL 退出后继续;损坏样本遇到 `DIALOG_AGENT_START_TIMEOUT`,同样已释放并继续;健康样本 CHECKED。该结果只通过退出/继续验收,损坏样本本轮未进入 PCB,不能算内容检查通过。由此补充 61–62 的启动/取消修正。
- 启动修正增量:**239 passed**,含代理、worker 门禁、普通验证、批次、保存/退出、修复凭证与 UI 回归;`startup-delta-pytest.xml`。本机另有独立 pytest 使用相同产品 mutex,增量回归使用专有测试 mutex 名,产品互斥名不变。
- **最终 r3 冻结链路通过**:`blocking-live/frozen-r3/result.json` 中现场副本 `FAIL / DATABASE_SAFETY_STOP / safe=true` → 损坏样本 `FAIL / ICDB_REPAIR_REQUIRED / safe=true` → 健康样本 `CHECKED / safe=true`。三项实际启动 PCB,三项本次会话均退出、没有未执行项、没有 Repair/Restore,原库 6 项主要文件 hash 一致。
- 冻结验证使用与包内字节码逐项比对一致的控制层,worker、弹窗代理、原生日志和进程探测均运行实际冻结 exe;没有把源码子进程冒充冻结子进程。24 个 Python 模块及 2 个 VBS 内容一致;实际 GUI 启动/退出、x86 codec、包内 VC runtime 加载、完整进程快照 helper 通过。
- 最终独立只读复核:已报告的 P2 修正后无剩余 P1/P2;后续启动预算/取消竞态也单独复核通过。当前实测环境为 Windows 11 / Mentor EE7.9.5,Windows 10 未实机验收。
- 候选包目录:`dist/blocking-locks-20261105-candidate-r3/CbbRefreshVerify-Background`。`BUILD.json` 记录源码基线/实际文件 hash,`VALIDATION.json` 记录全量与增量测试和原生链路结果;仍是候选包,不合并发布、不关闭工单。
本次不宣称 ICDB 自动修复已通过验收。生产库的其它原生 Project Backup 会话保留;它们连接失败不等于数据库损坏,也不等于可强夺写权限。
## 确认释放会话(后续增量)
用户反馈 r3“释放检查”遇到两个 Project Backup 进程只返回不能解除,没有关闭入口。本次增加“释放残留会话…”:先列当前库关联进程和工程,确认后逐项正常关闭。已加载 PCB 允许正常关闭;标准保存问题由用户明确选择保存、放弃或取消。已知启动确认可正常取消。单项未退出继续处理其它项;全部退出后再证明库文件可独占读取,归档本应用旧 checkpoint,解除保留句柄与映射,恢复按钮。
正常关闭无法完成时,提供独立的“强制结束所列会话…”警告确认,默认取消,列出项目、进程和 PID,明确未保存修改丢失及写入中断风险。仅对已证明属于当前库、重新核对 command/creation/image/进程句柄身份的列表发送结束请求;不是全机按进程名清理。此显式功能不改变普通检查/关闭/取消任务的合作退出策略,不删除 Mentor 地址、数据库或锁文件。
不安全批次结果可直接进入释放;取得当前库释放证明后,重新显示结果框,用户可点击“继续未执行项”。只恢复原检查/校验中 SKIPPED 的条目,重新套用当前可见性及非 READY 资格。失败项不自动重放,库增删改不自动重放。
原生实际验证:`diagnostics/icdb-authority-20261105/confirmed-release-b00c3b7c/result.json`。在 `C:/Temp/CbbBlocking-15cb2f99/healthy` 一致副本运行真实 Qt 确认按钮,正常关闭 Project Backup PID60404,监督 worker PID13008 随后退出,full release proof 通过;6 个主要文件 SHA-256 前后一致,未执行 Repair/Restore。截图 `preview.png` / `released.png` 保留。初次试验遇到不存在的 pywin32 查询 API,已改用 Windows QueryFullProcessImageNameW 并增加真实句柄回归后重跑通过;初次证据不覆盖。
当前边界:跨机器会话、不可读归属、操作系统拒绝权限或原生数据库地址未释放,不能通过本机结束请求制造成功。最终证明失败会保留原始原因与证据。强制结束仅证明进程结束,不证明工程数据完好;ICDB 自动修复仍未验收。标准 PCB 保存提示有精确消息/按钮与 UI 单元验证,尚无真实未保存 PCB 工程的端到端保存验收。
验证记录:统一全量 **1509 passed**(`residual-release-pytest.xml`,328.25 秒,1 条既有 STA warning);最新释放/未知归属/确认名单竞态/界面继续入口/库协调/文件保护增量 **83 passed**(16.42 秒)。未知归属进程现在显示为诊断行;只有预览时和执行时都证明属于当前库的项才可以关闭,执行时恢复可读不能扩大用户确认过的名单。代码复核发现的结果框隐藏及确认名单竞态均补先失败的回归后修正,最后只读复核未发现剩余 P1/P2。编译与 diff 空白检查通过。
r4 冻结增量的第一次实际试验没有通过 100 秒测试门槛:原生 PID25260 / worker PID61916 已正常退出,旧 checkpoint 已完整归档且 release proof 成功,但全库证明对 23,218 条记录反复路径解析约 97 秒,测试控制器已提前超时。证据 `confirmed-release-dfb29e88` 保留,不能算该轮通过。新增失败回归后修正了普通文件的多余解析,同一副本实际 proof **6.906 秒 / 12 个独占文件**;最新涉及库释放、维护范围、网络映射和 UI 的回归 **105 passed**(17.09 秒)。新的 r5 候选需再次执行完整冻结释放链路。
**r5 冻结链路已通过**:`confirmed-release-99f3f4e0/result.json`。真实 Qt 确认入口关闭 Project Backup PID48080,包内监督 worker PID23040 退出,旧 checkpoint 归档并清除,12 个原生文件独占读取证明通过,6 个主要文件 hash 一致。整个试验约 27 秒(含启动后等待 15 秒、列表查询、关闭及复查),原生退出至释放完成约 6 秒。随后立即使用同一 r5 exe 运行真实 PCB 检查,`next-check-result.json` 返回 `CHECKED / safe=true`,实际打开正常并退出、remaining_pids=[]、没有 Repair/Restore,证明清理后可以继续后续任务。此检查耗时约 100 秒,其中 PCB 启动约 86 秒;不能把该启动耗时称为释放卡死。
r5 包内 26 个 Python 模块及 2 个 VBS 与当前源码一致,实际主窗口启动/退出、x86 codec、包内 runtime 和冻结进程探测 helper 均通过。性能改动经独立只读复核,未发现新 P1/P2。只交付候选包,不关闭工单或宣称 ICDB 数据已自动修复。
r6 用户中心库误阻止的根因、源日志、修正和冻结管理链路见 `2026-11-05-management-phantom-blocker.md`。统一全量 1518 passed;完整原生移除与重新读目录验证通过。历史记录本身不再被直接当作活跃会话,管理入口允许重新复核;生产库工程未改动。
@@ -0,0 +1,47 @@
# 导出身份比对与备份恢复:阻塞审计及修正
本记录只覆盖 `native_engineering_identity.py`、`native_export_guard.py`、`backup_recovery.py`。没有启动原生工具,没有修改生产库、Mentor 地址、锁或数据库文件。完整应用现场验证由主任务单独记录。
## 从前到后的阻塞点
| 阶段/触发条件 | 修正后行为 | 保留原因/证据 |
|---|---|---|
| 输入工程身份、库引用、工程完整性或独立目录不符 | 本项拒绝;没有创建原生会话时可继续下一项 | 防止替换错误工程;不是占用锁 |
| EE7.9.5 导出程序缺失或二进制版本未通过能力探测 | 本项拒绝;未启动即不宣称会话未释放 | 不猜测原生接口 |
| CBB 操作互斥量实际被其它操作持有 | 不并行启动;保留原有串行约束 | 原生 COM/共享库任务串行运行 |
| 库协调句柄确实独占或已有未释放任务 | 保留互斥 | 句柄/释放证明由 `library_lock` 负责 |
| 机器上存在其它工程的 PCB、LM 或原理图进程 | 不再单凭进程名阻塞恢复 | 完整工程/数据库/库路径参数匹配;`WorkOld` 不匹配 `Work` |
| 有原生进程但没有可识别的工程参数 | 本项返回 `TARGET_NATIVE_OWNER_UNCONFIRMED` | 尚不能通过进程名称判断它打开的文档;不称为确认占用 |
| 进程参数明确属于目标工程或目标库 | 本项返回 `TARGET_NATIVE_OWNER_ACTIVE` | 不抢占用户或其它任务会话 |
| 目标/共享数据库仍有 `sAddress.adr` | 本项返回地址状态未确认;保留地址原件 | 地址存在不等于活动占用;目前该写入入口仍需原生判断 |
| 目标 LMC、工程/PCB 的 `icdb.dat` 或 Layout 无法独占读取 | 拒绝安装 | 补上实际文件句柄探测,包含共享 `__ReusableBlocksSymb` |
| 源、目标、验证副本内容在复制/验证期间变化 | 本项拒绝覆盖;已证明释放时不制造未释放状态 | 保留原文件与清单 |
| 其它工程的 iCDB 导出器/服务器存在 | 不再全机拒绝导出,也不收养为当前任务后代 | 仅追踪本次后代和命令明确指向本次分析副本的原生进程 |
| 当前导出器或后代仍活跃/句柄未释放 | 保留导出任务及它所属库的排除 | 不杀原生进程;库 A 的已绑定记录不排除 B |
| 创建导出进程遭明确 FileNotFound/Permission/CreateProcess 拒绝 | 返回失败且本任务会话安全;可重试 | `native_started=False` 及 `export-launch=false` 绑定证明 |
| 导出构造异常但可能已经启动原生进程 | 保持未确认,不能因没有返回 Python 对象就放行 | 明确区分未启动和部分启动 |
| 导出前完整进程树查询超时/失败 | 本项失败且明确未启动;释放本任务未启动的 guard,可继续独立项 | 不把查询失败视为空进程列表或数据库无人占用 |
| 已启动导出后的完整进程树查询超时/失败 | 10 秒以内返回未确认,保留本次进程与已记录句柄 | 不无限等待 WMI,也不强杀原生进程 |
| 导出器已退出,但输出错误或不完整 | 内容失败与释放结论分开 | 不因业务失败制造活动会话 |
| 导出后的汇总报告写入失败 | 保留已确认释放;报告写入错误另行返回 | 先完成释放证明及清除本次导出 guard,再写可选汇总 |
| 恢复开始前检查失败或恢复结果报告写入失败 | 保留 `release_confirmed`;没有原生会话则允许其它独立项继续 | 不再统一返回 `safe_to_continue=False` |
| 程序重启后导出 PID 被其它进程复用 | 新记录比对 creation identity 与 executable;不同身份不当成旧 owner | 旧记录没有创建身份时仍需新鲜独立证明 |
| 正常导出 checkpoint 存在 | 重新核对库、lease、token、独立副本、进程身份、地址和数据库句柄 | 不把其它同名服务器当作当前副本 owner |
| 应用 checkpoint 损坏 | 默认拒绝;显式释放入口持有完整库/原生证明时可使用 `allow_unknown_checkpoint` | 此路径仍要求全机已知导出 owner 和 retained registry 均空;调用者必须归档未变化的原字节 |
## 回归验证
首次新增的六项失败测试均先观察到旧实现失败,然后实现修正。领域测试曾串行运行 `81 passed in 62.78s`;后续增加了未知启动、损坏 checkpoint 显式释放、无 PID 证明、报告 I/O 及 PID 复用的正反测试,新增子集 `12 passed`,汇总报告 I/O 正反 `2 passed`。主任务须再进行最终全量运行;多进程并行运行这些测试会争用产品全局 named mutex,不能把相互争用的结果当成业务失败。
全进程树查询现已移入现有 `--owner-probe` 只读 helper,使用版本化 snapshot 协议并保留 PID、parent、未知可执行文件名称、命令、路径和创建身份。上限统一为 10 秒,为 helper 启动及 WMI 查询保留余量;超时只结束读取进程的 helper。回归覆盖启动前与启动后的查询超时、未知后代、PID 0 系统项、协议版本、重复 PID、缺少创建身份等。新增相关子集 `17 passed`。实际本机只读 helper 返回 457 个进程(包括非 `.exe` 系统名称),457 项创建身份,耗时 4.75 秒;该数值是一次观察值,并非上限,没有启动 Mentor。
最后补充了 `capture_identity` 的启动前异常边界:来源校验、当前 lease 证明、证据目录范围、导出器能力检查、目录创建或 mutex 创建失败,均返回明确未启动的安全失败,不把被调用者异常变成活动会话。六项对应的失败测试先观察到旧实现抛异常,再验证修正;与查询预算、已启动失败的正反测试合并为 `11 passed`。这些测试没有取得产品全局 mutex。
最后一次只读复审补齐了进程诊断记录写入边界:已证明导出进程/后代退出且副本数据库已释放后,最终 `*-process.json` 写入失败只附加报告警告,不能抹去释放事实;仍有活跃原生进程时保持未确认。首个 `export-launch.json` 在任何启动前写入失败,则返回明确未启动;仅凭本进程当前 lease、匹配的一次性 token、独立副本 owner 和零 process records,清除此任务尚未启动的 guard。缺少日志不能由重启后的无身份记录借用此通路。三项报告异常回归先失败,修正后与未释放正反测试合计 `7 passed`,未使用产品全局 mutex。
## 仍需明确保留的验收缺口
- 没有在真实 Mentor 环境验证本轮恢复/导出功能。上述证据为回归测试及只读代码审查。
- 恢复写入入口中的旧地址,需要原生 inspection 确认;本次没有自动删地址,也没有仅凭文件不存在就确认释放。
- 无工程参数的外部原生会话仍是未确认;不能自动 Attach 到全局 COM 并操作他人的文档。
- 当前没有解决所有原生内容失败;本轮不表示 ICDB 自动修复或现场问题验收成功。
@@ -0,0 +1,124 @@
# ICDB 判定接口与本次检查失败的只读调查
日期:2026-11-05。基于 `1acfbd6`。本轮新增离线元数据探测脚本,读取安装的
类型库、PE 导出和既有日志;没有改变应用判定、打开工程、调用 Repair/Restore、
删除地址文件或终止原生进程。自动修复仍未验收通过。
## 结论
“打不开 PCB 并出现弹窗”是有价值的直接观察,但一般数据库打开失败、服务器
连接失败、版本恢复选择,都不能单独等同于 ICDB 数据损坏。现阶段最可靠的
ICDB 维护依据,是**本次打开的同一工程/数据库发出的明确原生诊断**。
尚未找到已验证、无需打开数据库、能直接预测 PCB 将发生 ICDB 故障的独立接口。
底层确有原生错误类别和错误码,但只有导出名称,没有取得对应 SDK 声明和
完整枚举,也没有建立真实故障与数字的映射;不能猜测参数调用后接入产品。
## 1. 已解析的 PCB 原生类型库
前一轮探测对 PCB 启动器执行 LoadTypeLib 失败,并不意味着安装缺少类型库。
本轮按 32 位注册表定位到:
```text
HKCR\TypeLib\{EDEDED00-D5F6-4B04-8FE7-EDEDEDEDED00}\1.0\0\win32
D:\Mentor\7.9.5EE\SDD_HOME\wg\win32\bin\XtremeDesignSessionWG.exe
```
成功读取 `MGCPCB (AutoActive series) Type Library`,共 614 个类型。没有创建
COM 实例,也没有连接正在运行的 PCB。
| 元数据 | 实际声明 | 能证明什么 |
| --- | --- | --- |
| `IMGCPCBApplication.OpenDocument(filename)` | 返回 Document,没有 ICDB 专用返回码声明,`scodeArray` 为空 | 可作为实际打开尝试;不能据声明直接解码 ICDB 故障 |
| `ActiveDocument` | 返回当前 Document | 空文档只表示尚未取得文档,不说明失败原因 |
| `IMGCPCBError` | `ErrorString`、`Severity`,没有 ErrorCode 属性 | 提供文字/严重级别;不是统一的打开错误码接口 |
| `EPcbErrorSeverity` | `epcbWarning=0`、`epcbError=1`、`epcbFatalError=2` | 是错误严重级别,不是三种 ICDB 健康状态 |
| `Errors` 集合使用方 | CopperBalancing、DFFSliverAnalysis、IDFImport、DXFImport | 不能把这些集合当成 PCB 启动错误队列 |
| 应用事件 | 打开前/打开后事件;`OnOpenDocument` 标志为 Open=1、Reload=2 | 没有声明 ICDB 打开失败事件和专用数字码 |
类型库没有专用错误表,不代表运行时绝不会返回 HRESULT/EXCEPINFO。真正执行
调用时仍应记录 HRESULT、EXCEPINFO 的 wCode/scode/source/description 和调用阶段,
但尚未实测的 HRESULT 不能赋予“数据库损坏”的含义。
## 2. 底层 iCDB 候选接口
静态解析 `iCDBPI.dll`、`iCDBDiagPI.dll`、`iCDBCAPI.dll`,三者均为 x86 PE。
`diagnostics/icdb-authority-20261105/metadata.json` 保存实际文件 SHA-256、
原始符号名称、类型库说明及常量,避免跨版本混用。
| 接口线索 | 已证实内容 | 未证实内容 |
| --- | --- | --- |
| `Result_get_iErrorCategory` / `Result_get_iErrorCode` / `Result_get_sResult` | 原生 Result 有错误类别、编号、文字 | 对象布局、调用约定、错误枚举及故障映射 |
| `CDBPI_IsOpenCDB` / `IsOpenCDBEx` | 存在数据库打开状态查询符号 | 未打开、不可达、损坏之间如何区分;不是已验证的健康检查 |
| `PIMgr_GetStatus` | C++ 对应声明返回/填写 `ESynchStatus` | 不能把同步状态当 ICDB 健康状态 |
| `Diag.GetProjectStatus` / `GetActiveIssues` / `GetStatus` | 存在诊断、工作区及项目状态接口 | 结构体、枚举、初始化方式、查询范围及副作用 |
| `Diagnostic.Check` / `CDBPI.IsDatabaseAvailable` | 存在相关诊断符号 | 参数语义、是否只读、超时/释放行为;未执行 |
导出名的存在不证明这是厂商支持的外部自动化 API。不能用 ctypes 默认 int
返回类型和猜测指针去调用这些接口;尤其不能在主界面线程内调用。
## 3. 本次“全部失败”的具体证据
旧记录:`diagnostics/live-open-20261105/evidence/icdb-check-1738989c1dae/`,
原生 PID `63480`,目标为 IP101GR 的 `Work/database`。
这次补读该 PID 的原生进程日志:
```text
D:\Mentor\7.9.5EE\SDD_HOME\standard\iCDB\ProjectBackup\Logs\
2026-11-05 08.08.40-63480\2026.11.05.iCDBProjectBackup.txt
```
日志从 08:08:41 读取该数据库的 ServerProperties,依次尝试所记录的多个地址
和主机名,端口 10002。08:09:14 报 `Network: ERROR: Could not connect to IP [...]`
后,再次读取同一数据库的服务器属性并继续重试。调查时读到的后续日志中仍有
相同循环。摘录和来源保存在 `diagnostics/icdb-authority-20261105/network-evidence.json`。
因此,这一次没有取得 Output 的直接线索是**原生工具正在反复连接数据库记录的
服务器地址**,不是仅仅窗口标题不匹配。此前允许通用标题的修改不足以解决它。
尚不能仅凭这些日志确定是服务器已退出、地址过时、监听配置或防火墙问题,
也不能证明数据库内容损坏,更不能据此确认目标无人占用。
同批另两项缺少唯一 PRJ,不能把它们与此连接失败合并成一个损坏结论。
本轮没有再次启动原库检查,也没有强制结束 PID 60000/63480。
## 4. 检测规则的遗漏与证据等级
安装 DLL 包含以下明确的原生维护消息:失联、整理中断、升级中断、重要操作
中断、跨机失联、自动修复中断,均带厂商的 Repair project 指引。
把这六段原生模板传给当前 `parse_session_loss`,只有跨机失联模板被识别。
这确认了规则覆盖范围偏窄;**模板存在不证明此次工程触发了这些错误**。
| 本轮实际证据 | 合理结论 |
| --- | --- |
| 同一数据库的完整失联/不一致/检查失败原生诊断 | 明确 ICDB 异常,记录子类、数据库路径、会话及证据 |
| 服务器地址重试、连接失败、一般 Could not run server | 连接/启动失败,尚不能认定损坏或无人占用 |
| `problem encountered while opening ...` / `loading ... failed` | 一般打开失败,还需具体数据库日志/弹窗 |
| Database Restoration 的手动/自动保存版本选项 | 需要用户选择版本,不自动判为损坏或代选 |
| Project Backup 未占用、备份目录已加载 | 占用/备份信息,不保证 PCB 完整打开或设计健康 |
| 无 Output、超时、未知错误 | 无法确认,不能推成健康或需要 Repair |
| READY/PENDING、地址文件、历史日志 | 均不能单独决定本次 ICDB 是否有问题 |
## 5. 建议的接入顺序与验证缺口
普通检验继续实际打开工程;同步捕获本次弹窗、增量日志和同一工程身份。
打开成功就继续普通检验,不以 Project Backup 预检作为其前置门槛;打开失败
仅在有明确 ICDB 原生诊断时标注 ICDB 子类,其他错误分别记录。检查结果不应
禁用普通检验或原生版本管理,也不据单项诊断设置永久的全库失败标志。
独立批量检查仍只做用户明确启动的检查。原生日志补采应绑定启动时间/PID、
目标路径和读取预算,保持增量及有界;网络日志中的“连接失败”不等同会话释放。
如果新检查不能退出,应显示实际滞留目标和原生连接阶段,不能靠伪造释放证明
继续对同一目标执行写操作。
下一轮动态探测应使用完整隔离库,分别记录正常打开、会话失联、文件损坏、
连接不可达、版本恢复选择的 PCB/原生证据;只运行诊断打开,不执行 Repair/Restore。
提取真实 HRESULT/EXCEPINFO 与原生文字的对应关系,并验证取消与正常关闭。
即使没有数字码,已确认的原生消息仍可用于分类;不能为等一个状态码阻塞普通流程。
本轮只验证元数据提取、既有日志关联和解析器覆盖范围,没有执行新的 PCB 打开
对照,没有验证底层 C API、解决原生网络重试或修复 ICDB。没有生成新应用候选包。
后续经用户授权完成了真实损坏/正常/缺文件对照,见
[损坏弹窗与错误码实测](2026-11-05-icdb-corruption-error-code-probe.md)。取得了 PCB
COM 两层错误码,但损坏与缺文件返回相同值,仍不能把该码当 ICDB 健康状态。
@@ -0,0 +1,102 @@
# ICDB 损坏弹窗与错误码实测
2026-11-05,Mentor EE7.9.5,PCB `2013.0.543.380`。按用户授权,只在新建的
`C:\Temp\CbbCodeProbe-7f1b0307` 内制造损坏。应用判定代码没有修改,没有执行
Repair、Restore、版本回退、集成或保存。
## 实测结论
成功触发数据库打开失败弹窗,并取得 `OpenDocument` 的 HRESULT 和 EXCEPINFO。
但**数据库损坏与文件不存在返回完全相同的两层错误码**,因此这两个数字不能
单独用于判断 ICDB 异常,更不能单凭它们自动请求 Repair。
| 对照 | `OpenDocument` 结果 | 外层 HRESULT | 内层 EXCEPINFO.scode | 原生证据 |
| --- | --- | --- | --- | --- |
| 未修改副本 | 返回正确工程文档 | 没有失败异常 | 不适用 | 工程身份匹配,数据库 GUARD OK,连接成功 |
| 损坏 `icdb.dat` 前 64 字节 | 失败,ActiveDocument 为空 | `0x80020009` / `-2147352567` | `0x8004021E` / `-2147220962` | 数据库文件打开失败弹窗;本轮日志明确指出损坏副本的数据库格式不受支持,并建议 Repair |
| 不存在的 PCB 路径 | 失败,ActiveDocument 为空 | `0x80020009` / `-2147352567` | `0x8004021E` / `-2147220962` | 文件不存在弹窗,没有数据库损坏依据 |
两次失败的 EXCEPINFO 全部字段相同:
```text
wCode: 0
source: Expedition PCB - Reusable Block
description: Failed to open document.
helpFile: ...\hssd\win32\bin\mgcpcbautomation.hlp
helpContext: 10187
scode: -2147220962 (0x8004021E)
```
外层 `0x80020009` 是 COM 调度异常。内层码在本次对照中表示通用的
`Failed to open document.`,尚不能赋予它 ICDB 专用含义。这里取得的是 PCB
自动化接口的错误,**没有调用未验证的 iCDB C API,也没有取得其内部 Result
错误枚举的可靠映射**。
正常、损坏、缺文件三种情况在等待 COM 注册时,都出现过
`0x800401E3` / `-2147221021`。正常副本随后成功打开,故此码也不能视为 ICDB
故障;它记录的是当时 COM 对象尚不可取得。
## 复现条件与证据
种子来自已有的隔离测试库:
`diagnostics/icdb-auto-recovery-20261027/verify-library`,不使用生产库作破坏对象。
完整复制得到 control 和 damaged 两份库,每份 21,029 个文件、557,187,955 字节。
复制后核对 LMC、PRJ、DXP、icdb.dat、PCB、LayoutDB 六个主要文件的 SHA-256。
仅在 damaged 副本清零工程 `database/icdb.dat` 的前 64 字节,其余字节保留。
正常副本和损坏副本在启动前彼此独立。
通过同一个原生入口启动空白 Reusable Block PCB,然后在专用私有桌面执行
`app.OpenDocument(绝对PCB路径)`。调用前核对 COM 主窗口 PID 的启动器父子关系,
记录 InstanceGuid,确认没有已有文档,且 PRJ/PCB 工程配置的 CentralLibrary
均解析到该副本自己的 LMC。没有创建生产库 COM 写会话。
正常对照 PID 15536,10:16:44 开始 OpenDocument,10:17:06 返回正确文档。
随后授权匹配的关闭助手执行 `doc.Close False`,未保存,正常退出。
损坏对照 PID 63384,10:18:10 开始 OpenDocument,10:18:51 取得异常。
弹窗原文:
```text
Problem encountered while opening one of the database files.
See PCBLogFile.txt for details. Loading of the job has failed.
Do you wish to view the log file now?
```
截图:
`diagnostics/icdb-authority-20261105/live-damaged/database-failure-75566502.png`。
先记录弹窗,再点击“否”关闭可选的查看日志提示,未发送修复指令。
本轮 `PCBLogFile.txt` 增量为:
```text
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 [C:\Temp\CbbCodeProbe-7f1b0307\damaged\
ReusableBlocksLP\IP101GR\Work\database\icdb.dat] in old, unsupported format)
```
“旧格式”是原生对被清零文件头的诊断文字;本试验没有证明实际存在版本过旧。
iCDBManager 同一 PID 的日志也指向该损坏副本。原生层先重复读取损坏数据库后
才显示弹窗;COM 异常在确认弹窗后返回,故不能只等 COM 异常而省略限时的
独立弹窗/日志观察。
缺文件对照最终 PID 61776,记录为 `live-missing-3174b7/com-result.json`,
10:25:08 发起调用,10:25:10 取得异常。首轮缺文件试验的单按钮提示未被助手
识别,助手按 180 秒上限停止,原生会话保留;之后核对窗口 PID 和明确的缺文件
文字,确认原生按钮 ID 为 2,正常确认提示并关闭空白 PCB。修正试验助手后重跑
取得上述完整错误码。该超时不是 ICDB 故障证据,首轮记录保留。
## 验证结果与边界
`diagnostics/icdb-authority-20261105/verify_live_probe.py` 对真实记录执行断言,
汇总保存在 `live-comparison.json`:正常文档身份、两种失败完全相同的 EXCEPINFO、
损坏弹窗与数据库路径、来源六文件哈希、损坏文件未被修复、测试进程退出及
已有原生会话保留均核对通过。三个最终试验都正常结束,前台窗口句柄前后相同。
没有剩余测试 PCB 或 iCDBNetServer;既有 Project Backup PID 60000、63480 保留。
检测可使用“实际打开失败 + 本次明确 ICDB 诊断 + 数据库目标一致”的组合证据。
文件不存在、一般打开失败和 COM 注册等待要分别记录。明确的单项故障只应
成为该项备注,不据此永久禁用普通检验或版本管理。
本次验证了**文件头损坏**,没有复现现场“先前服务器会话失联”,也没有验证自动
修复、READY 或完整产品维护入口。没有运行/宣称全量应用测试;本轮只增加
调查脚本与记录。不能将本次复现算作现场 ICDB 修复通过。
@@ -0,0 +1,86 @@
# ICDB 扫描按实际打开错误内容分类
## 当前行为
批量检查复用 PCB 诊断打开路径,不再先启动 Project Backup 检查所有条目。
诊断请求固定关闭自动修复、原生 Verify 菜单及恢复确认;只打开、核对目标并
正常关闭,不执行集成、保存、Repair 或 Restore。普通检验和原生版本管理
继续使用独立入口,界面既有 READY 排除规则保留。
只有实际打开错误中包含 iCDB 内容,或本轮原生日志明确给出 iCDB 错误,才
将该条目标为 ICDB 问题。通用错误码、工具内部类别名称、路径中出现 icdb、
成功输出以及历史错误均不能单独决定分类。没有 iCDB 具体原因的一般打开
失败仍保留原始错误和未确认结果,不显示成数据库损坏或健康。
正常打开只返回 CHECKED,不推定 CBB READY。单项失败在实际会话已释放后
继续后续项;未确认释放或用户取消时仍保留真实状态。没有增加永久的故障
锁,也没有把扫描结果作为普通检验或版本管理的前置条件。
## 重复扫描的证据
实机发现同一工程连续打开失败时,PCBLogFile.txt 可能逐字相同。仅比较内容
会把本次故障误当成旧日志。补充采集当前 PCB PID 对应的 iCDBManager 日志,
核对日志创建/修改时间、PID 头和目标数据库路径后,保留具体 ERROR 内容。
采集在既有的限时只读助手中执行,保留 16 KiB 头和最多 128 KiB 尾的读取
预算,不在 Qt 线程中等待日志,不递归扫描整个库,也不调用修复接口。
文件日志仍采用启动前检查点及有界增量读取;未知基线、无法确认历史的日志
重写,不提升为本轮证据。扫描只读取本次诊断子目录的错误证据,不能引用
上一次操作的证据目录来标记当前条目。
## 实际验证
使用上一轮完整复制的两个隔离库,通过应用的 `run_verify(maintenance=True)`
及 `run_batch` 执行真实生产调度路径。请求故意带旧 `auto_repair=True`,内部
诊断仍强制关闭修复。
- 损坏副本:保留清零前 64 字节的 icdb.dat。原生再次弹数据库打开失败提示;
扫描读取本轮 PID 3060 的日志,返回 FAIL / ICDB_REPAIR_REQUIRED,并保留
`in old, unsupported format`、目标数据库路径及日志证据。
- 随后正常副本:原生 PID 28948 打开正确工程,正常关闭,返回 CHECKED。
- 两项均 `safe_to_continue=true`、`library_flag=UNKNOWN`、恢复尝试为空;没有
请求 Project Backup、Repair、Restore、集成或保存。测试 PCB 和数据库服务器
均退出;没有终止已有 Project Backup 会话。
证据:`diagnostics/icdb-authority-20261105/product-scan-comparison.json`,以及
相邻的 `product-scan-damaged` 和 `product-scan-control`。首次复测暴露的重复日志
漏判记录也保留,没有清空故障日志来让测试通过。
回归覆盖实际损坏报文、未知 iCDB 错误内容、相同错误码的非 ICDB 失败、正常
输出、旧日志残留、日志重写、旧操作证据隔离、当前/其他 PID、日志新旧、
其他数据库路径、取消、会话未释放、失败后继续,以及原有界面/报告行为。
最终测试命令为 `python -X utf8 -m pytest -q`,1326 项通过,436.32 秒,保留一条
既有 pywinauto STA 提示;详细结果保存在同目录 `pytest-scan-utf8.xml`。
系统默认 GBK 模式的一次运行中,既有测试用默认编码读 UTF-8 报告失败;该项
在 UTF-8 模式下单独复测通过,未因此修改原生修复逻辑。
冻结候选包构建及启动/正常退出冒烟通过,16 个关键 Python 模块和两个 VBS
与源码一致,包内 x86 运行库及 Unicode/UNC 协议检查通过。使用冻结程序的
限时日志采集入口回放当前 PID 证据,确认能保留实际 ICDB 原因;未在冻结包中
再次执行整条原生扫描批次。独立候选包不覆盖此前的程序目录,未合并发布。
只读独立复核未发现必要 P1/P2,指出批量报告残留旧的 Project Backup 流程名称。
随后仅将该报告名称改为实际的 PCB 打开检查,并增加报告输出断言,104 项相关
回归通过;同步重建并复核最终 r2 候选包。完整回归记录保留在上述 XML,文案
修正后的相关回归记录为 `pytest-scan-post-review.xml`。
## 文件头损坏与版本管理
“版本历史与备份…”打开原生 iCDB Project Backup,入口只核对 LMC 和唯一 PRJ,
不以 icdb.dat 文件头可读作为准入条件。有效的完整历史备份包含数据库文件,
原生 Restore 可以替换损坏状态,而不是计算或补写丢失的 64 字节。
这份隔离样本的备份配置为相对 `./backups`,列出的多个历史 ZIP 中包含
`database/icdb.dat`。文件头非零仅是清单检查,不能代替原生验证备份有效性。
清单保存在 `version-backup-inventory.json`。
已有同类隔离实测中,原生 Repair 从已有备份恢复了该文件头损坏,随后重新
打开并验证成功;没有有效备份的对照则明确失败。见
[此前有备份的恢复记录](../releases/2026-10-28-icdb-open-repair-reopen-validation.md)
及[无备份的失败记录](../releases/2026-10-28-icdb-no-history-repair-validation.md)。
那次是 Repair 调用原生备份恢复,不等同于本轮已通过当前版本管理按钮执行
Restore;本轮没有选择恢复版本或发起恢复。
恢复会回到所选备份的内容版本,应保留原始损坏副本,恢复后重新打开并单独
检验。PCB 启动时“最后手动保存/自动保存”的选择不应直接视作工程 iCDB 已
恢复。没有有效恢复源时不能承诺恢复。现场会话失联和自动修复仍未验收通过。
@@ -0,0 +1,58 @@
# 管理、协调租约与状态扫描阻塞审计
审计范围:`library_lock.py`、`management.py`、`native_management.py`、`management_transaction.py`、`native_backup.py`、`network_path.py`、`batch_status.py`、`status_bridge.py`、`owner_probe.py`、`native_export_guard.py`、`icdb_ownership.py`、`native_occupancy.py`。本文仅说明这些边界,原生窗口清理、普通校验入口与界面由本轮其他修改覆盖。
本轮没有在该审计任务中启动 Mentor、Repair、Restore,未修改现场中心库,也没有删除原生地址、锁或数据库文件。测试使用临时文件和替代原生边界。
## 已落实的误阻塞修正
| 入口或条件 | 原误阻塞 | 修正及验证重点 |
| --- | --- | --- |
| `native_management.NativeOperationError`、`management.run_management.invoke` | 原生目录失败已退出,异常丢失释放事实 | 操作成功与释放证据独立传播;已退出目录失败可以重新取得协调锁 |
| `management_worker_main` | 桌面、映射、能力或来源校验在原生启动前失败,却报告未释放 | 原生启动前保持 safe;进入实际原生边界前才变为未确认 |
| `NativeManagementAdapter._worker`、`_launch_worker` | 请求准备或桌面构造失败制造假会话 | 准备失败为 safe;构造失败仅消费明确的 process_created=false、worker_exited=true、remaining_pids=[]、safe=true 协议;部分启动和缺证据仍未确认 |
| `NativeManagementAdapter._add_transaction` | Add 已退出后,路径、引用对齐、被动占用检查失败再次保留库锁 | 仅实际启动下一原生调用前重置释放状态;异常在租约退出前传播已知释放事实 |
| `NativeManagementAdapter.prepare_add_transaction` | 来源缺 CentralLibrary、staged/asset/lease 校验失败制造假未释放 | 原生 capture 前的完整准备块返回 safe=true 失败;capture 后准备事务记录失败保留 capture 的实际释放事实 |
| `management_worker_main` 的 CleanWGLib remove | wait 已退出但输出文件读取失败,或明确创建拒绝仍 unsafe | wait 确认退出立即记录 safe=true 与 exit_code;FileNotFoundError/PermissionError 的创建拒绝为 safe,未知创建错误和 timeout 保持未确认 |
| `management.run_management` | 移除后被动检查异常、报告写入异常把已释放改成未释放 | 保留释放事实;报告存储错误写入返回 detail;不重复抛出最终报告异常 |
| `management.RECOVERY_REQUIRED` | 目标数据恢复要求被混同为原生会话仍存活 | 可同时返回 RECOVERY_REQUIRED 和 safe=true;同目标禁止自动重放,明确不同目标不被旧数据恢复记录误挡 |
| `management._may_have_mutated` | 末阶段看似 prewrite 即忽略曾经写入的 history | 仅完整且一致的 prewrite history 可确认未写入;缺失、混合历史保持未知 |
| `LibraryLock.__enter__` | retained checkpoint 租约永远不再检查回调 | 同一租约 release probe 证明退出后,可清理身份相符且无未决 export/pending 的应用检查点并重新取得锁 |
| `NativeManagementAdapter.release_confirmed` | 超时 worker 后来正常返回仍永久保留 | 按库跟踪原 worker;只有它已退出且其独立结果 safe=true 才回收;缺失/损坏/unsafe 结果不推定释放 |
| `LibraryLock`、`release_retained_library` 的 CreateFile | 权限、网络、路径失败统一称 competing writer | 仅 Windows sharing/lock violation 32/33 分类为 LibraryBusy,其余保留实际错误 |
| `has_blocker` | 完整快照中的异库 pending 一律阻止 | 主记录与 pending 采用一致规则:只有完整、有效且明确搬迁的源记录可忽略 |
| `release_retained_library` | torn、malformed 应用检查点没有恢复路线 | 显式释放强制全库 fresh owner/native-file/export proof,复核原字节未变化,先归档再清应用 JSON;未知 proof 标记不足时拒绝归档 |
| `release_retained_library` 的范围 | 选择 Target 却清除全库旧检查点,可能漏 Other 真实会话 | 只有检查点和进程内 owner 都绑定相同 block_name 才使用所选目标范围;旧无 scope、范围不符、损坏记录一律 full proof |
| `_prove_library_released` 的目录范围 | 目标释放仍扫描所有 CBB,且漏共享 SymbolLibs | 已绑定目标时仅扫描目标 Work、共享 SymbolLibs 和所选 LMC;共享地址与数据库始终参与证明 |
| `_prove_library_released` 的 retained registry | 库 A 的 worker 阻止库 B | 新 worker 绑定 cbb_library;明确异库可排除;旧无 scope registry 保留未确认 |
| `_owner_matches_scope` | Work 子串匹配 WorkOld,worker 只带 job 路径时反而漏检 | Windows argv 精确路径边界;读取有大小限制的 worker 请求以绑定 library/project/database;没有可判定路径时不推定无关 |
| `_owner_matches_scope` 的 EDIF 命令 | 不识别产品实际 `-icdb <database>`,异库 exporter 被当全局未知 owner | 同时支持实际位置参数与 `-icdb=`、`-project=`,带空格路径和大小写按 Windows argv 解析,目录前缀相似不当作同库 |
| full known library process proof | 全机同名原生程序都被称本库占用 | 命令行明确指向异库可排除;无范围证据仍保持未确认;损坏检查点 full recovery 不使用范围排除 |
| `_persist_blocker`、`_recorded_supervisor_alive` | maintenance 只凭旧 PID 存在,PID 复用会误挡 | 新 version=1 记录附 machine 和 supervisor_created;只有进程创建身份匹配才称原监督进程仍在;旧身份缺失需额外 fresh native-idle 和原独占/目标 guards |
| maintenance 新 owner | retained 后缺 release_probe | 保存明确的 release callback,复用协调租约的再证明路径 |
| `BatchStatusSession.scan` | 单个请求/CBB/path mismatch 把后续未查询项全部标错 | 错配记录只属于当前项;支持 server 时先 restart 再逐项查询;已确认基础设施不可用仍批量标 UNAVAILABLE |
| `fingerprint_library`、batch record | 相同字节/时间的另一库或另一 block_type 复用旧 READY/PENDING | cache 绑定绝对库路径和 block_type;不 resolve 映射路径,不递归扫描工程负载 |
| `LibraryPathLease.close` | 映射查询或取消一次失败后 closed 永久为 true | 成功后才结束租约,瞬时失败可再次 close |
| `release_after_recovery`、`release_retained_path_leases` | retained mapping 永久不释放,旧界面丢引用后也不可回收 | 核对 RELEASE_CONFIRMED 与该库 display/mentor/已知 UNC 身份;registry 保留旧对象;共享当前映射先转移所有权再释放旧租约 |
| `WindowsWNetBackend.mapping`、`mappings` | 无关盘符读取失败让目标映射不可用 | 目标盘直接查询;全枚举跳过不可查盘但把它保留为已占用,避免抢占未知盘符 |
对应集中回归:`tests/test_management_retry_blockers.py`。既有管理、事务、租约、网络路径、状态扫描、导出与维护测试也纳入定向回归。
## 仍保留的真实互斥和未确认边界
| 条件 | 当前行为与可用路线 |
| --- | --- |
| uploader 或真实同库任务仍持有独占协调句柄 | 继续互斥;不删除 `.cbb-upload.lock`,文件中的旧 PID 文本不作为当前持锁证据 |
| 全局 `Local\\CbbRefreshVerify795` mutex | 仍序列化本应用原生 COM/管理操作。尚未证明所有 Mentor 全局状态可按库并发,未直接删掉此保护 |
| `native_management._catalog_com` 已有 LibraryManager.exe | 仍在 COM 激活前保守拒绝。尚无证据证明本机 DispatchEx 不会复用已有服务、Quit 不会关闭他人窗口;本轮仅修为 prelaunch safe,不声称已支持并存 |
| `_catalog_native` 激活后无 native_pids、原生 PID/退出状态缺失 | 仍未确认,可能 COM 已启动不可追踪服务。新请求不自动把空 PID 列表视为已释放;提供显式 fresh release 路线 |
| `native_management._wait_native_exit` | 运行时仍以已观察 PID 等待退出,未新增 Library Manager 全部后代的 JobObject/creation-time 追踪;超时转显式释放证明 |
| 旧无创建身份的外部/导出进程记录 | 不能把活 PID 直接证明是旧进程,也不能无条件忽略;新记录创建身份由本轮相应 exporter 修改提供。无法独立确认时保留现场与明确未确认原因 |
| `sAddress.adr` 仍存在 | 地址存在本身不证明当前活跃,但显式“释放”也不冒充原生 NOT_IN_USE。应走既有 inspection-only 的精确 project/database 原生占用检查;本任务不手删地址,也不自动 Repair/Restore |
| `owner_probe` 超时、访问受限、空 command、请求文件不可读 | 不把它称已确认活跃;也不猜已退出。明确异库证据可以排除;范围不明时保留未确认,并允许独立只读检查 |
| export ownership 元数据有效但副本目录/数据库缺失,或 token/lease 不匹配 | 仍拒绝自动清理,因为无法证明原副本的原生写入已结束;未知元数据恢复通路不能覆盖掉已知但尚未证明释放的 export copy |
| native backup 的自身 JobObject/子进程仍活动或查询失败 | `native_backup.py` 已使用 checkpoint=false;等待其自身会话树退出,不因主窗口/launcher 先退就推定全部释放,不 terminate/kill 原生事务 |
| 二进制 hash、flow、ASCII removal contract、来源/目标/证据隔离、Add nonce 和 duplicated lease handle | 继续作为能力或身份校验;它们不是 native busy。失败不授权绕过未验证原生写入路径 |
| 损坏检查点的全局 fresh-proof 路线 | 为弥补未知 scope,可能要求全机已知 native/app workers 均退出;这是显式未知记录恢复的限制,不适用于具有明确异库范围的普通重试 |
维护入口的 PID 创建身份、原生文件独占和 fresh guards 是不同证据:创建身份匹配只说明原监督进程仍在;创建身份不同/旧 schema 缺失只消除错误 PID 归属判断,不能替代原生或数据库释放证明。
@@ -0,0 +1,37 @@
# 中心库无活跃会话却被阻止(r5 现场反馈)
## 现场证据
用户运行 `blocking-locks-20261105-candidate-r5` 后反馈中心库操作仍提示上次未安全释放,扫描看不到活跃会话。
- r5 `data/logs/application.log` 中,15:54(北京时间)的 IP101GR PCB 检查因原生读写确认退出;相应 `session-release.json` 为 `safe_to_continue=true`、`worker_exited=true`、`remaining_pids=[]`。这次打开失败不等于释放失败。
- 随后版本工具 `native-backup-6bc7146a0eee/result.json` 为 `CLOSED / safe_to_continue=true / native_exit_code=0`,原生 PID60660 已退出。
- 中心库仍存在 11 月 4 日的 `.mentor_lib.cbb-upload.blocked.json`:PID57076、lease `eae80a06f23e4664b00c70b6bf81b01d`、`OWNER_RELEASE_UNCONFIRMED`。这是一条旧操作记录,并非当前进程存在的证据。
- 本轮只读实查:关联进程列表为空,协调文件可独占读取,库内没有 `sAddress.adr`,原有完整释放证明也通过。未为诊断删除源库任何记录或工程文件。
## 根因
`run_management` 在 `LibraryLock.__enter__` 因历史记录拒绝时尚未启动任何原生会话,但异常收尾用 `isinstance(error, LibraryBusy)` 强制把结果改成 unsafe。互斥通道尚忙也返回相同 unsafe。UI 随后把这一结果记为按库保留的 unsafe 标志,直接禁用后续管理;此时没有 PID 可供扫描。失败发生在创建本次 checkpoint 之前,也没有对应的管理尝试日志。
原残留扫描只列进程,不列历史 JSON 或 `checkpoint=False` 的进程内 retained lease,进一步造成“明明没有会话但永远不能继续”的表现。
## 修正
1. 有旧 JSON、同库进程内 retained lease、旧管理标志或未确认释放的历史修改时,在操作互斥范围内、任何新原生调用前执行已有完整释放证明。证明通过才归档本应用旧记录并释放本应用句柄;真实 owner、地址、共享访问冲突仍拒绝。未知归属不假定无人占用。
2. 管理尝试先记录 `release_check`;未启动的新操作被拒绝时保留本次 `safe=true`,不制造新的原生 owner。已启动原生后仍沿用实际退出证据,不能把超时统一放行。
3. UI 的旧 unsafe 标志成为重新复核要求,不再直接禁用管理。匹配当前库的证明且本批结果全部安全后,先恢复 retained 路径 lease,再清旧标志。映射恢复失败保留重试状态。
4. 扫描明确显示历史记录日期与内存保留记录,和活跃进程分开。空进程列表不再隐藏历史阻止原因。
5. 同目标历史修改未完成仍保留“需恢复,不能重放”;会话已释放与数据是否恢复分别处理。没有修改的 BLOCKED 结果不再触发额外原生目录读取。
6. 管理结果记录操作类型、CBB、状态、安全状态、原因及证据位置,包含启动前拒绝。
## 验证
- 按现场旧记录结构新增回归,初次 4 项均失败;修正后通过。覆盖无 owner 继续、真实地址保留、执行通道忙不制造 owner、空列表显示历史记录。
- 后续覆盖无 JSON 的 retained Verify 锁、匹配证明后恢复路径 lease、UI 可以再次发起复核。相关 **119 passed**。
- 实际隔离 UI 链路 `diagnostics/icdb-authority-20261105/legacy-management-a1c609ec/result.json`:从已验证原生 CRUD fixture 创建新隔离副本,预置相同旧记录与 UI unsafe 状态,通过应用“移除 CBB”确认入口完成复核、旧记录原字节归档、原生移除 IP101GR、重新读取目录确认缺失、UI 与路径 lease 恢复。**REMOVED / safe=true,59.016 秒,原始 fixture 全文件哈希不变**。生产库工程与旧记录未改动。
- 统一全量 **1518 passed**(`management-release-pytest.xml`,268.10 秒,1 条既有 STA warning);编译与 diff 检查通过。
- **r6 冻结复验通过**:`legacy-management-b3a65315/result.json`。控制层源码与包内字节码一致,真实管理 worker 与进程探测运行 r6 exe;相同 UI 链路返回 `REMOVED / safe=true`,原生目录再次确认缺失,旧记录归档、保留映射与 UI 标志恢复,原 fixture 未修改;96.313 秒。包内 26 个 Python 模块及 2 个 VBS、实际 GUI 启停、x86 codec 和包内 runtime 加载通过。
- 独立复核确认没有新增 P1/P2。候选包为 `dist/blocking-locks-20261105-candidate-r6/CbbRefreshVerify-Background`。
生产库的旧记录保留到用户下一次实际管理操作或确认释放时,由同一完整证明流程复核、归档。没有直接删除源库锁文件,也没有替用户重放增删改。不把本次阻塞修复算作 ICDB 数据自动修复验收。
@@ -0,0 +1,27 @@
# r9 补充审查:4 个已复现问题
本次只审查和构造回归复现,未修改产品源码、未启动 Mentor、未操作真实库。源码与 r9 BUILD.json 的 SHA-256 比对另行确认。先前 1,564 项测试通过没有覆盖以下组合场景。
复现:`python -X utf8 -m pytest -q diagnostics/icdb-authority-20261105/test_r9_remaining_review.py --junitxml=diagnostics/icdb-authority-20261105/r9-review.xml`。四项均在预期断言处失败,收尾完成,无残留测试线程。
1. **P2:停止整队列不会取消后台检查。** 后台 Beta 检查期间提交前台任务,再点击“取消当前任务并停止队列”。`request_stop()` 在 busy=true 时只设置前台 stop,没有设置 IcdbCheckTask.stop。前台任务取消后后台继续执行 Gamma。实测 calls=['Beta', 'Gamma'],期望仅 ['Beta']。位置:`ui/main_window.py:1484`。
2. **P2:排队项目变成 READY 后仍重复检查。** 检查启动时捕获目标列表,后续权威刷新将 Gamma 更新为 READY,恢复执行时只检查 invalidated,未重新核对 READY 操作资格。实测 Gamma 仍进入 verifier。用户要求 READY 即使被勾选也应排除。位置:`ui/icdb_check_task.py:66`、`ui/main_window.py:781`。
3. **P2:释放失败被误显示成读写确认。** 注入 `DATABASE_SAFETY_STOP` 和原始错误 `PCB_RELEASE_UNCONFIRMED: launcher still running`,表格固定显示“未确认:需处理读写确认”。该分类也包含普通会话释放失败,不能据此断定存在读写提示。位置:`ui/main_window.py:819`。
4. **P3:普通校验排队时隐藏后台检查进度。** 检查 Beta 仍运行时提交普通 Gamma 校验;`_run(maintenance=False)` 无条件隐藏 icdb_progress 和 icdb_phase,后台任务仍活跃。实测 isHidden=true。位置:`ui/main_window.py:1215`。
建议先修 1、2,再修 3、4;把这四个复现并入正式 tests 后复测。没有依据将这些结果扩大成数据库损坏或“所有释放又失败”。
仍保留此前验收缺口:ICDB 自动修复、现场服务器失联完整恢复以及 Windows 10 关键流程没有因本轮检查验收而变成通过。
## r10 修复
- “停止队列”同时取消后台 ICDB 任务和当前前台队列;独立“停止检查”仍只取消 ICDB 检查。
- 每项在真正取得原生执行机会后检查最新 READY 集合。权威状态读取在让出队列之前发布状态,不等待界面回调;重新加载和局部状态刷新均按库匹配,其他库的同名 CBB 不受影响。
- 仅本次错误明确含 read/write access permission 时显示读写确认;释放未确认和其他安全状态分别显示,原始错误保留在提示中。
- 普通校验排队时保留活跃 ICDB 任务的进度条和阶段标签。
4 个原始复现已转入 `tests/test_background_queue_regressions.py`,修复前均红,修复后均绿;另补权威状态在 Qt 回调前发布、跨库同名隔离以及不同安全原因显示,共 10 个回归。第一轮相关 49 项通过。产品源码仅修改 `ui/main_window.py`、`ui/icdb_check_task.py`;具体最终测试、冻结和原生验收见 r10 的 VALIDATION.json。
最终全量 1,574 项通过(0 失败、0 错误、0 跳过);有既有 pywinauto STA COM 初始化提示 1 条。编译和 diff 检查通过。r10 冻结包 37 个模块/脚本与源码比对、GUI 启动冒烟和包内 x86 状态桥运行库加载检查通过。
r10 实际冻结执行器隔离批次通过:损坏项 FAIL / ICDB_REPAIR_REQUIRED / safe=true(68.563 秒),随后正常项 CHECKED / safe=true(26.812 秒);无关的同桌面模态窗口未被操作,来源六文件哈希不变。实际界面按钮检查及排队状态读取通过,顺序 check-start → check-end → status-start → status-end,筛选可用,最终 READY,50ms 定时器最大间隔 79ms,整体 23.656 秒。目录行的初始 PENDING 为测试注入,原生打开和状态读取实际执行;不冒充现场服务器失联恢复验收。
@@ -0,0 +1,104 @@
{
"candidate": "r10",
"candidate_base_commit": "1acfbd637452e8f64d96981044194661dfb4ec1b",
"python_full_suite": {
"name": "pytest",
"errors": "0",
"failures": "0",
"skipped": "0",
"tests": "1574",
"time": "207.493",
"timestamp": "2026-11-05T21:09:55.032807+08:00",
"hostname": "Abs"
},
"frozen_modules_and_scripts_compared": 37,
"native_check_statuses": [
"FAIL / ICDB_REPAIR_REQUIRED / released",
"CHECKED / released"
],
"native_check_seconds": [
68.563,
26.812
],
"foreign_same_desktop_modals_untouched": true,
"source_primary_files_unchanged": true,
"gui_native_queue_order": [
"check-start",
"check-end",
"status-start",
"status-end"
],
"gui_cached_filter_available": true,
"gui_max_timer_gap_seconds": 0.079,
"automatic_icdb_repair_acceptance": false,
"field_server_session_loss_acceptance": false,
"tested_environment": "Windows 11 / Mentor EE7.9.5",
"candidate_code_sha256": {
"src\\cbb_refresh_verify\\application.py": "babeefe4ddc2fb78b4af3a0413f60cb351eeca57f6f2862e2bc1b8a94de59f32",
"src\\cbb_refresh_verify\\backup_recovery.py": "ea7bb90151e825efb6163e43ed344b62407565005f0a0459c423ba2205539373",
"src\\cbb_refresh_verify\\batch_status.py": "918c80dd4f75146baa6b7616ce415ac89ee28f6c1f46b2bfda77bacc998b5b62",
"src\\cbb_refresh_verify\\close_failed_block.vbs": "e5f0d9678e07a239e96e634b90cdc761de52fc136311fae31484fa38d6fd9781",
"src\\cbb_refresh_verify\\com_runtime.py": "f0f3802d16c791a8bf9dfef44a94917072bfbe8fb3a927d76be8ef0341c7ed35",
"src\\cbb_refresh_verify\\content_check.py": "8239c17b6e2452917ed573a7deb3e4c8583eaef91bf3630eff5f424d2e398aa9",
"src\\cbb_refresh_verify\\defaults.py": "19aaec03822468b18099f358a5aac3433346ff81345b58a896545bf508840e3d",
"src\\cbb_refresh_verify\\desktop_worker.py": "3b79dcab16b67b68550957c09c9de1ba08d85109356274c8cff4d0ae02dec3fb",
"src\\cbb_refresh_verify\\diagnostic_open.py": "d6c49ffe5a9969d95edbbc896d47a7e100f248380a287f99ba7bb8b92dbb3072",
"src\\cbb_refresh_verify\\dialog_agent.py": "6a9b9802912afc4dd670ee9b28845d97bd64fe55fd577815ef729850fcff78db",
"src\\cbb_refresh_verify\\dialog_models.py": "f68cee01d927b48364276d3c1f79e63ce8af231878db0b907d292780cfbafb56",
"src\\cbb_refresh_verify\\dialog_probe.py": "d61f0c955c961980c9635940eefb9c7947c1fe90b4b6fd844720df41ddb0aa0e",
"src\\cbb_refresh_verify\\dialog_rules.py": "3189b476f7ff4678b59212ed7b4081164e038a7cf7662cd895f9019cb3c1edc5",
"src\\cbb_refresh_verify\\dialog_supervisor.py": "15c415c2d2e916f0b64ba04416164ff3b50996d70b22d3b4ac2226b5547ac247",
"src\\cbb_refresh_verify\\failed_session.py": "7e6f7fa5ebcf2d49f00c5efc5f5d26412704b68e9122e476ac1f672009a98c09",
"src\\cbb_refresh_verify\\icdb_maintenance.py": "6ab1d469b09e92641ff60a4b6acd4cc13d40cc200a6fae880e864181526994c4",
"src\\cbb_refresh_verify\\icdb_ownership.py": "7bf2342c1efa34a2ebcbf27964a40a490220f7146fbb6ef957b8d8dd1e511f03",
"src\\cbb_refresh_verify\\icdb_recovery.py": "358c0e9a004f1e0b2127513286e1dffd94b0180a8b7314aa9655462f570b6360",
"src\\cbb_refresh_verify\\integration.py": "86669ad73ae1d2d724d406c3644bb0c297236a4ffdc4b66117930ec660613e88",
"src\\cbb_refresh_verify\\library_lock.py": "315b3cba411713aea870b0a773b3cb986be73b85b88732d8cd82862f22f650db",
"src\\cbb_refresh_verify\\library_manager_verify.py": "71e16a8581ad44b646be78b480f9812d596a6d162b3eec03d078bf4ea4503195",
"src\\cbb_refresh_verify\\library_reader.py": "47d58d1518ba7e69c22b41db2d34c0a61e2cc1362cf28414f0dae43d4a011753",
"src\\cbb_refresh_verify\\maintenance_entry.py": "3b93c25f69d7a961facd8004e20e155a84cc18f06d57b741a9055786ed058990",
"src\\cbb_refresh_verify\\management.py": "64e6fc1382092f1d2154052ec456b9c511934d3f2b231a163be28dca27eb3873",
"src\\cbb_refresh_verify\\management_transaction.py": "32c53caaead05d5db3a31d3e30721e65434e5837095dc0a7812f825603aa37a0",
"src\\cbb_refresh_verify\\mentor_adapter.py": "26a73074ef162d240c80747e371f357c8ec87b00cdbbd3509acc4ac58e68e955",
"src\\cbb_refresh_verify\\mentor_paths.py": "ba48c0003b3eacc88906497b2e43b299ca4536a9f7dae44bbacf6a6ec694c116",
"src\\cbb_refresh_verify\\models.py": "9e8209a8b006df923f1d07ccab64bbcfe5d674c4fc5f5426bfbc9b11da4ee105",
"src\\cbb_refresh_verify\\native_add_failures.py": "192202732423dc624c5548c75e80bb20973e5087c21d4798c9637f25b6de7e94",
"src\\cbb_refresh_verify\\native_backup.py": "7276fa3d9298e85550151cf0c7191d84bd08a18a72df5d854a3c38f1a88b001d",
"src\\cbb_refresh_verify\\native_engineering_identity.py": "a241bc3be7dc5a922ce728f1febd3684e79e6e45935589c80a4b1a5933e05df5",
"src\\cbb_refresh_verify\\native_export_guard.py": "5120c13e4ef1255d4c61316eb40005c4039c23e41edca4437bd0ef28b543a959",
"src\\cbb_refresh_verify\\native_management.py": "bdb42f0bbbc964e56cfe08f66804a03dfc73157eb98911531c9d4c8f940c7595",
"src\\cbb_refresh_verify\\native_occupancy.py": "7d686079d12217382adace6361cd343d977b91075e3b157cf410ca92d07ffc49",
"src\\cbb_refresh_verify\\native_repair.py": "65d3e5b28b1f375402b002f56527dafbcc79e21cea66822e3bc26332d59e76d7",
"src\\cbb_refresh_verify\\native_repair_ui.py": "3eb2af181befea94af8b3b3131edff09589f340bcba777abbe3b67fd31b80393",
"src\\cbb_refresh_verify\\network_path.py": "6b5b159502d42a46bf4ee26d5698fd2c6498b359088e2138eae46e9065b13c6f",
"src\\cbb_refresh_verify\\operation_queue.py": "57ad9da8aeedd9cd75a8c5b5fda44044bcb457670a4a323e9e8bb247198074ab",
"src\\cbb_refresh_verify\\owner_probe.py": "8dcfd60246cb2979db34d02e0a2d605c334b6f7e2666f22daa2f50c0408bc903",
"src\\cbb_refresh_verify\\pcb_bridge.py": "745ef117e984021e8a3d9087a2783c1efe19bed97edcfbcfae7703a291cc6eaa",
"src\\cbb_refresh_verify\\pcb_restore_prompt.py": "5342612ad666e3faf5a9a4ce4710e9af458d357c44037135f2837dad436d5cf7",
"src\\cbb_refresh_verify\\post_repair_consent.py": "3b1eaff1db58fd311a9dbc5fdcd850911b04cc5afd4e747f70a62b721b8fafa4",
"src\\cbb_refresh_verify\\post_verify_diagnostics.py": "2442de485bc0d6a34c4b2d9b10bc5f323ea38f9ff66674119a50f5c6043bac63",
"src\\cbb_refresh_verify\\reimport_recovery.py": "66b2ba8f8c2ec9ae8c386d6b1aa74a1fbb6bfb509c4df0a262f8d7a0ca48469e",
"src\\cbb_refresh_verify\\residual_sessions.py": "5fa8a0f87d6aa4c7e0dec4ab8eb1d649d20fdbea35016fbd65fefefd42607eef",
"src\\cbb_refresh_verify\\runtime_logging.py": "44ac25430a89c5ffc9e48e88c227e88052f4c815365e8556f67918f3edaba39e",
"src\\cbb_refresh_verify\\runtime_timing.py": "c4ed4057b5000e0b86ccc7dd1fe2004d6e0dae29f397fec77b01ad41a81c6367",
"src\\cbb_refresh_verify\\save_verified_block.vbs": "2ede0a1297e44533cd6333b775cdd70c86b5f2aaaaa18ccd6df158379ef1fd31",
"src\\cbb_refresh_verify\\scoped_manifest.py": "54cf4288e7850e220b394ae971ea98785eba631f80784a3f9354209e649f6498",
"src\\cbb_refresh_verify\\status_bridge.py": "05d627d02934a14f5de409472c291d37565a95b836f9c130111aae80612e9196",
"src\\cbb_refresh_verify\\verify_diagnostics.py": "041b77d7d49e4c86338ed5e5741d0d27a2c42bc358f4da54b3efe64af847e9b8",
"src\\cbb_refresh_verify\\verify_probe.py": "3ed743df49d195c74f5045985ab23491d103ee2c42c07b558c1e037176402ed2",
"src\\cbb_refresh_verify\\verify_runner.py": "bb5bae2616e3440a6adc083a2a6941176205f574fde077cf89a6121077833fed",
"src\\cbb_refresh_verify\\__init__.py": "be4027b9b6c4659a7f7d499a14f75ec84c410bd9a706d9ffd9f97d44a52b3806",
"src\\cbb_refresh_verify\\__main__.py": "dc4be034a0e43d4b659a9300137c29a6a6b7a32b7eaef02b6dff9a88c41305c0",
"src\\cbb_refresh_verify\\assets\\dialog_rules.v1.json": "3911080fc799a819662e225a7ad73e883abd8dcc9936cc30ec1b2d6d51a03c6e",
"src\\cbb_refresh_verify\\ui\\backup_notice.py": "1a3fa5ac18a267907faf95f36d61b9f08bcbb939eef4d480dd06af1391a81599",
"src\\cbb_refresh_verify\\ui\\backup_recovery_dialog.py": "621ea2efea85367b0c9bb9790040860a1ac001f40f8834289cb67c96ea0648d1",
"src\\cbb_refresh_verify\\ui\\icdb_check_task.py": "03e6416ee3d2fa11ecedb624a558516eeb56e634f1f22e41e57e10db6a13d656",
"src\\cbb_refresh_verify\\ui\\main_window.py": "4526f59b7f26374ff119d45453b278780a51ef7632eb1611c4e06a205cd30795",
"src\\cbb_refresh_verify\\ui\\management_dialog.py": "27d920284e508f19fae71b04a3b3d81a251559d52c4cc8d691c41186686572b7",
"src\\cbb_refresh_verify\\ui\\pcb_restore_dialog.py": "b7228d828df235ac51502dc68760d28c4896af02c780e9806e11c32dba7f62da",
"src\\cbb_refresh_verify\\ui\\residual_release_dialog.py": "515aaa236ad6a0bee9e5383c6c9d093baeb8bc7dc98f1b978b1197ce672c6764",
"src\\cbb_refresh_verify\\ui\\workers.py": "4cc9360a4820ded27a7d5ef709fc9f04b1d4804cb1e7a614f6c357ed547da888",
"src\\cbb_refresh_verify\\ui\\__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
},
"scope_note": "Candidate icons are local workspace assets and are excluded from this change. Native evidence files and engineering copies remain local."
}
+53 -9
View File
@@ -29,14 +29,55 @@ def _paths(root):
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 和原理图编辑器,再恢复备份。')
from .mentor_adapter import _process_details
from .icdb_ownership import database_directories, unreleased_addresses
from contextlib import ExitStack
import win32con, win32file
directories = database_directories(library, project, pcb)
scope = (Path(project).parent, *directories)
for name in ('ExpeditionPCB.exe', 'LibraryManager.exe', 'viewdraw.exe',
'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe'):
for pid, executable, command, parent in _process_details(name):
match = _native_scope_match(command, roots=scope, files=(library, project, pcb))
if match is True:
raise ValueError(f'TARGET_NATIVE_OWNER_ACTIVE: 目标工程或共享库有原生会话,PID={pid}')
if match is None:
raise ValueError(f'TARGET_NATIVE_OWNER_UNCONFIRMED: 无法确认原生会话所属工程,PID={pid}')
addresses = unreleased_addresses(library, project, pcb)
if addresses:
raise ValueError(f'数据库会话尚未释放,停止恢复:{addresses}')
raise ValueError(f'TARGET_DATABASE_ADDRESS_UNCONFIRMED: 地址记录需原生确认;未据此判定正在占用:{addresses}')
with ExitStack() as stack:
paths = [Path(library), Path(pcb).parent/'Layout/LayoutDB.lyt',
*(directory/'icdb.dat' for directory in directories)]
for path in dict.fromkeys(paths):
if path.is_file():
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
win32con.OPEN_EXISTING, 0, None)
stack.callback(handle.Close)
def _native_scope_match(command, *, roots=(), files=()):
"""Match complete native path arguments; a missing identity remains unknown."""
from .failed_session import _command_args
if not command:
return None
expected_files = {Path(path).resolve() for path in files}
expected_roots = tuple(Path(path).resolve() for path in roots)
identities = []
for arg in _command_args(command)[1:]:
value = arg.split('=', 1)[-1].strip('"')
path = Path(value)
if not path.is_absolute():
continue
key = arg.split('=', 1)[0].lstrip('-/').casefold() if '=' in arg else ''
if (path.suffix.casefold() in ('.prj', '.pcb', '.lmc')
or path.name.casefold() in ('database', '__reusableblockssymb', 'icdb.dat')
or key in ('project', 'icdb', 'database', 'db', 'lmc', 'prj', 'pcb')):
identities.append(path.resolve())
if not identities:
return None
return any(path in expected_files or any(path == root or path.is_relative_to(root)
for root in expected_roots) for path in identities)
def _project_text(path):
@@ -140,9 +181,12 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
return report
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')
try:
root.mkdir(parents=True, exist_ok=True)
report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
except OSError as error:
return replace(result, stderr=(result.stderr+'\n恢复报告写入失败:'+str(error)).strip())
return replace(result, evidence_path=report)
from .com_runtime import initialize_com, uninitialize_com
initialized = initialize_com()
@@ -239,7 +283,7 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
return save(final)
except Exception as error:
return save(VerifyResult('FAIL' if installed else 'BLOCKED', stderr=str(error),
safe_to_continue=False))
safe_to_continue=release_confirmed))
finally:
try:
if library_lock is not None:
+12 -6
View File
@@ -48,6 +48,7 @@ class BatchStatusRecord:
detail: str = ""
fingerprint: str = ""
timestamp: str = ""
block_type: str = "RULP"
@property
def completed(self) -> bool:
@@ -109,6 +110,8 @@ def fingerprint_library(lmc: Path) -> LmcFingerprint:
# digest so a changed central library can never reuse an old status row.
content_sha256 = _sha256_file(path)
combined = hashlib.sha256()
combined.update(str(path).casefold().encode('utf-8', errors='surrogatepass'))
combined.update(b'\0')
combined.update(content_sha256.encode("ascii"))
combined.update(b"\0" + str(stat.st_size).encode("ascii"))
combined.update(b"\0" + str(stat.st_mtime_ns).encode("ascii") + b"\0")
@@ -165,6 +168,7 @@ class BatchCheckpoint:
detail=str(payload.get("detail") or ""),
fingerprint=str(payload.get("fingerprint") or ""),
timestamp=str(payload.get("timestamp") or ""),
block_type=str(payload.get("block_type") or "RULP"),
)
return cls(records)
@@ -212,7 +216,7 @@ class BatchStatusSession:
for index, name in enumerate(names):
previous = checkpoint.get(name)
if (previous and previous.status in {'READY', 'PENDING'}
and previous.fingerprint == fingerprint.sha256):
and previous.fingerprint == fingerprint.sha256 and previous.block_type == block_type):
results.append(previous)
continue
try:
@@ -224,11 +228,12 @@ class BatchStatusSession:
block_name=name, status=result.status, source=result.source,
raw_status=result.raw_status, detail=result.detail,
fingerprint=fingerprint.sha256, timestamp=_now(),
block_type=block_type,
)
except BridgeUnavailable as error:
record = BatchStatusRecord(block_name=name, status="UNAVAILABLE",
detail=str(error), fingerprint=fingerprint.sha256,
timestamp=_now())
timestamp=_now(), block_type=block_type)
except BridgeResponseError as error:
message = str(error)
lower_message = message.casefold()
@@ -242,15 +247,15 @@ class BatchStatusSession:
else "UNAVAILABLE" if unavailable
else "MALFORMED" if malformed else "ERROR")
record = BatchStatusRecord(block_name=name, status=status, detail=message,
fingerprint=fingerprint.sha256, timestamp=_now())
if server is not None and status not in {"UNAVAILABLE", "MALFORMED"}:
fingerprint=fingerprint.sha256, timestamp=_now(), block_type=block_type)
if server is not None and status != "UNAVAILABLE":
try:
server.restart()
except BridgeUnavailable:
pass
except Exception as error: # bridge failures isolate one CBB
record = BatchStatusRecord(block_name=name, status="ERROR", detail=str(error),
fingerprint=fingerprint.sha256, timestamp=_now())
fingerprint=fingerprint.sha256, timestamp=_now(), block_type=block_type)
if server is not None:
try:
server.restart()
@@ -258,7 +263,7 @@ class BatchStatusSession:
pass
checkpoint.append(checkpoint_path, record)
results.append(record)
if record.status in {"UNAVAILABLE", "MALFORMED"} and index + 1 < len(names):
if record.status == "UNAVAILABLE" and index + 1 < len(names):
# Startup/protocol failures cannot be repaired by retrying
# the same request contract for every CBB.
# Preserve a terminal record for each remaining name so a
@@ -267,6 +272,7 @@ class BatchStatusSession:
unavailable_record = BatchStatusRecord(
block_name=remaining, status=record.status, detail=record.detail,
fingerprint=fingerprint.sha256, timestamp=_now(),
block_type=block_type,
)
checkpoint.append(checkpoint_path, unavailable_record)
results.append(unavailable_record)
+2
View File
@@ -1,3 +1,5 @@
"""Shared execution defaults for GUI, batch requests and integration."""
DEFAULT_TIMEOUT_SECONDS = 600
DIALOG_AGENT_STARTUP_TIMEOUT_SECONDS = 30
WORKER_START_GATE_TIMEOUT_SECONDS = DIALOG_AGENT_STARTUP_TIMEOUT_SECONDS + 15
+51 -19
View File
@@ -74,27 +74,58 @@ 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)
# Track the session, but never use kill-on-close: PCB descendants can
# still own a database transaction when the Python supervisor exits.
self.job = win32job.CreateJobObject(None, '')
self.job_bound = False
self.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:
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
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 as error:
# No worker means no native descendants; a partial launch is uncertain.
error.worker_launch_release = {
'process_created': self.handle is not None,
'safe_to_continue': self.handle is None,
'worker_exited': self.handle is None,
'remaining_pids': [self.pid] if self.handle is not None else []}
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()
@@ -131,7 +162,8 @@ class DesktopWorker:
expected = {Path(__file__).with_name(name).resolve()
for name in ('save_verified_block.vbs', 'close_failed_block.vbs')}
clients = []
for row in query_owners(('cscript.exe',), timeout_seconds=3):
# Interpreter + COM startup measures ~4.75s on the field machine.
for row in query_owners(('cscript.exe',), timeout_seconds=10):
if row.get('parent') != self.pid:
continue
args = _command_args(row['command'])
+377
View File
@@ -0,0 +1,377 @@
"""Process-bound native opening check. This module never acquires PCB COM.
The caller must bind the newly launched PCB command/PID before entering here.
The check runs in that worker's private desktop and closes only that session.
"""
from __future__ import annotations
import ctypes
from ctypes import wintypes
import heapq
import hashlib
import ntpath
import os
from pathlib import Path
import re
import time
from .dialog_probe import _bounded_call
from .icdb_recovery import LOG_LIMIT, _read_log_delta, decode_log, snapshot_log
_MAX_WINDOWS = 128
_MAX_CHILDREN = 512
def _normal(path):
return ntpath.normcase(ntpath.normpath(str(path).strip()))
def _log_paths(job):
project = Path(job['project']).parent
pcb_logs = Path(job['pcb']).parent / 'LogFiles'
paths = [pcb_logs / 'PCBAutoSave.txt', pcb_logs / 'PCBLogFile.txt']
directory = project / 'database/cdbsvr/log'
directories = []
if directory.is_dir():
with os.scandir(directory) as entries:
directories = [entry.name for entry in entries if entry.is_dir(follow_symlinks=False)]
# Native names start with sortable YYYY-MM-DD HH.MM.SS. Only the
# newest candidates can describe this launch; history is never read.
for name in heapq.nlargest(8, directories):
with os.scandir(directory / name) as entries:
logs = heapq.nlargest(2, (entry.name for entry in entries
if entry.is_file(follow_symlinks=False) and entry.name.casefold().endswith('.txt')))
paths.extend(directory / name / log for log in logs)
return paths, directories
def _read_logs(job):
result = {}
paths, directories = _log_paths(job)
for path in paths:
try:
record = snapshot_log(path)
record['mtime_ns'] = path.stat().st_mtime_ns
result[str(path)] = record
except FileNotFoundError:
result[str(path)] = snapshot_log(path)
return {'files': result, 'server_directories': directories}
def _read_bounded(function):
try:
return _bounded_call(function, 2)
except TimeoutError as error:
# Abort this check, rather than spawning another reader while a slow
# share still owns the previous read. The daemon performs reads only.
raise RuntimeError('DIAGNOSTIC_LOG_READ_TIMEOUT: native evidence unavailable') from error
def snapshot_diagnostic_logs(job) -> dict:
"""Capture pre-launch content, so copied/stale successes cannot pass.
Store the returned JSON-compatible value at job['diagnostic_log_baseline'].
File count, content size and the entire read are bounded; failure is closed.
"""
started = time.time_ns()
return {'started_ns': started, **_read_bounded(lambda: _read_logs(job))}
def _fresh_logs_read(job, baseline):
paths, _ = _log_paths(job)
fresh = {}
for path in paths:
try:
stat = path.stat()
old = baseline['files'].get(str(path))
if stat.st_mtime_ns < baseline['started_ns']:
continue
if old is None:
if path.parent.name in baseline.get('server_directories', ()):
continue # An unobserved historical file cannot prove freshness.
old = {'size': 0, 'tail_sha256': hashlib.sha256(b'').hexdigest()}
if 'error' in old or (old.get('mtime_ns') == stat.st_mtime_ns and old['size'] == stat.st_size):
continue
content, appended = _read_log_delta(path, old)
if not content and stat.st_mtime_ns != old.get('mtime_ns') and stat.st_size == old['size']:
# Native PCBLog is overwritten with the same successful text.
content, _ = _read_log_delta(path, {'size': 0, 'tail_sha256': ''})
if not appended and old.get('truncated') and stat.st_size > LOG_LIMIT:
continue
fresh[str(path)] = decode_log(content)
except FileNotFoundError:
continue
return fresh
def _fresh_logs(job, baseline):
return _read_bounded(lambda: _fresh_logs_read(job, baseline))
def _load_evidence(job, pcb_pid, baseline, *, require_autosave=True):
logs = _fresh_logs(job, baseline)
project = Path(job['project']).parent
pcb = str(Path(job['pcb']))
auto_path = str(Path(job['pcb']).parent / 'LogFiles/PCBAutoSave.txt')
read_path = str(Path(job['pcb']).parent / 'LogFiles/PCBLogFile.txt')
names = re.findall(r'^Job Name:\s*(.*?)\s*$', logs.get(auto_path, ''), re.M | re.I)
if names and any(_normal(name) != _normal(pcb) for name in names):
return None
if require_autosave and not names:
return None
if 'Successfully read the design.' not in logs.get(read_path, ''):
return None
database = _normal(project / 'database')
project_file = _normal(job['project'])
for path, text in logs.items():
if path in (auto_path, read_path):
continue
db_paths = re.findall(r'S: Project path \[(.*?)\]', text)
prj_paths = re.findall(r'S: Project file \(\.prj\) path \[(.*?)\]', text)
startup = re.search(r'S: Started up automatically by iCDB Client: [^\r\n]*:' + str(pcb_pid) + r'\s*$', text, re.M)
client = re.search(r'Registered sub-client \[Expedition PCB[^\r\n]*process ID \[' + str(pcb_pid) + r'\]', text)
if (db_paths and all(_normal(value) == database for value in db_paths)
and prj_paths and all(_normal(value) == project_file for value in prj_paths)
and startup and client and 'S: iCDB database loaded' in text):
return {'pcb': pcb, 'pcb_pid': pcb_pid,
'logs': [auto_path, read_path, path], 'document_identity': 'native_path_pid_logs'}
return None
def _target_frame(windows, name):
matches = []
for window in windows:
if not window['class'].casefold().startswith('afx:') or not window.get('enabled', True):
continue
if not window['text'].casefold().startswith('expedition pcb') or f'[{name}]'.casefold() not in window['text'].casefold():
continue
mdi = {child['hwnd'] for child in window['children'] if child['class'].casefold() == 'mdiclient'}
if any(child.get('parent') in mdi and child['text'].casefold() == name.casefold()
and child['class'].casefold().startswith('afx:') for child in window['children']):
matches.append(window)
return matches[0] if len(matches) == 1 else None
def _windows(pid):
"""Read only this PID on the current desktop; cap text and enumeration work."""
import win32api
import win32gui
import win32process
user = ctypes.WinDLL('user32', use_last_error=True)
user.GetWindowTextW.argtypes = [wintypes.HWND, wintypes.LPWSTR, ctypes.c_int]
user.GetWindowTextW.restype = ctypes.c_int
deadline = time.monotonic() + .2
def text(hwnd):
if time.monotonic() >= deadline:
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
buffer = ctypes.create_unicode_buffer(4096)
# All handles belong to the external PCB PID: GetWindowText reads
# Windows' cached caption without sending a message to its UI thread.
# Custom controls may have no cached text; missing evidence fails closed.
user.GetWindowTextW(hwnd, buffer, len(buffer))
return buffer.value
result = []
seen = 0
def collect(hwnd, _):
nonlocal seen
if win32process.GetWindowThreadProcessId(hwnd)[1] != pid or not win32gui.IsWindowVisible(hwnd):
return True
seen += 1
if seen > _MAX_WINDOWS:
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: too many windows')
cls = win32gui.GetClassName(hwnd)
if cls.casefold() in {'uac_inputindicatoroverlaywnd', 'uac input indicator'}:
return True
children = []
count = 0
def child(ch, _):
nonlocal count
count += 1
if count > _MAX_CHILDREN:
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: too many controls')
child_cls = win32gui.GetClassName(ch)
if (cls == '#32770' or cls.casefold() == 'internet explorer_hidden'
or child_cls.casefold() == 'mdiclient' or child_cls.casefold().startswith('afx:')):
children.append({'hwnd': ch, 'class': child_cls, 'text': text(ch),
'id': win32gui.GetDlgCtrlID(ch), 'parent': win32gui.GetParent(ch),
'enabled': bool(win32gui.IsWindowEnabled(ch))})
return True
if cls == '#32770' or cls.casefold().startswith('afx:') or cls.casefold() == 'internet explorer_hidden':
win32gui.EnumChildWindows(hwnd, child, None)
caption = text(hwnd)
if cls.casefold() == 'internet explorer_hidden' and not caption and not children:
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
if left == right and top == bottom:
return True
result.append({'hwnd': hwnd, 'pid': pid, 'class': cls, 'text': caption,
'enabled': bool(win32gui.IsWindowEnabled(hwnd)), 'children': children,
'owner': int(win32gui.GetWindow(hwnd, 4) or 0)})
return True
win32api.SetLastError(0)
try:
win32gui.EnumWindows(collect, None)
except Exception as error:
if getattr(error, 'winerror', None) not in (0, 2) or result:
raise
return result
class _NativeSession:
def __init__(self, job, pid, launcher):
if launcher is None or not callable(getattr(launcher, 'poll', None)):
raise RuntimeError('DIAGNOSTIC_LAUNCHER_REQUIRED: retain the exact launch process handle')
import win32api
import win32service
import win32con
import win32event
import win32gui
self.handles = []
self.pid = int(pid)
self.launcher = launcher
self.gui = win32gui
self.event = win32event
desktop = win32service.GetThreadDesktop(win32api.GetCurrentThreadId())
actual = win32service.GetUserObjectInformation(desktop, 2)
expected = str(job.get('desktop', '')).split('\\')[-1]
if not expected or expected.casefold() in {'default', 'winlogon', 'disconnect'} or actual.casefold() != expected.casefold():
raise RuntimeError('DIAGNOSTIC_DESKTOP_MISMATCH')
self.pcb = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid))
self.handles.append(self.pcb)
def pcb_alive(self):
return self.event.WaitForSingleObject(self.pcb, 0) != 0
def launcher_alive(self):
return self.launcher.poll() is None
def post(self, hwnd, message):
import win32process
if not self.pcb_alive() or win32process.GetWindowThreadProcessId(hwnd)[1] != self.pid:
raise RuntimeError('DIAGNOSTIC_STALE_WINDOW: target window identity changed')
self.gui.PostMessage(hwnd, message, 0, 0)
def close(self):
for handle in self.handles:
handle.Close()
self.handles.clear()
def _cancel(job):
if job.get('cancel_signal') and Path(job['cancel_signal']).exists():
raise RuntimeError('VERIFY_CANCELLED: native opening check cancelled')
def _pending_prompt(windows, *, closing=False, frame=None, session=None, clicked=None, pcb_name=''):
for window in windows:
known_titles = {'expedition pcb', 'expedition pcb - reusable block'}
if (window['class'].casefold().startswith('afx:') and not window.get('owner')
and (window['text'].casefold() in known_titles
or frame is not None and window['hwnd'] == frame['hwnd'])):
continue
text = ' '.join([window['text'], *(child['text'] for child in window['children'])]).casefold()
if not closing and ('select the desired product license' in text or window['text'] == 'Tip of the Day'):
return True # The caller's PID-scoped DialogAgent owns startup choices.
standard_save = (pcb_name and window['class'] == '#32770' and window['text'] == 'Expedition PCB'
and any(child['class'].casefold() == 'static'
and child['text'].strip().casefold() == f'Save changes to {pcb_name}?'.casefold()
for child in window['children'])
and {child.get('id') for child in window['children'] if child['class'].casefold() == 'button'} == {2, 6, 7})
if closing and window.get('owner') == frame['hwnd'] and (standard_save or 'do you want to save the reusable block cell' in text):
targets = [child for child in window['children'] if child['class'].casefold() == 'button'
and child.get('id') == 7 and child['text'].replace('&', '').strip().casefold() in {'no', '否(n)', '否'}
and child.get('enabled', True)]
if len(targets) == 1:
if window['hwnd'] not in clicked:
session.post(targets[0]['hwnd'], 0xF5)
clicked.add(window['hwnd'])
continue
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: unknown native prompt preserved; '
f"pid={window['pid']} hwnd={window['hwnd']} class={window['class']!r} "
f"title={window['text']!r}")
return False
def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=None) -> dict:
"""Prove this launch opened its native document, close it, and prove exit.
PASS describes opening only; it does not imply refresh READY or unoccupied
shared library data. Unknown prompts are preserved for existing recovery.
"""
baseline = job.get('diagnostic_log_baseline')
if not isinstance(baseline, dict) or 'started_ns' not in baseline or 'files' not in baseline:
raise RuntimeError('DIAGNOSTIC_BASELINE_REQUIRED: snapshot before native launch')
session = _NativeSession(job, int(pcb_pid), launcher_process)
try:
deadline = time.monotonic() + max(.01, float(job['timeout']))
while True:
_cancel(job)
error = _read_bounded(error_reader) if error_reader else None
if error:
raise RuntimeError(str(error))
if not session.pcb_alive():
raise RuntimeError('DIAGNOSTIC_OPEN_UNCONFIRMED: target PCB exited before loaded evidence')
windows = _windows(int(pcb_pid))
if any(window['pid'] != int(pcb_pid) for window in windows):
raise RuntimeError('DIAGNOSTIC_PID_MISMATCH')
frame = _target_frame(windows, Path(job['pcb']).stem)
# Startup splash/control windows are transient. Unknown windows
# veto closing only once the target actually has loaded evidence.
pending = (_pending_prompt(windows, frame=frame, pcb_name=Path(job['pcb']).name)
if frame is not None else True)
evidence = (_load_evidence(job, int(pcb_pid), baseline, require_autosave=False)
if frame is not None and not pending else None)
if evidence is not None:
emit(stage='diagnostic_native_loaded_candidate', **evidence)
break
if time.monotonic() >= deadline:
raise RuntimeError('DIAGNOSTIC_OPEN_UNCONFIRMED: current target MDI/path/PID logs missing')
time.sleep(.1)
# Re-read the exact PID/frame before the sole close request.
_cancel(job)
windows = _windows(int(pcb_pid))
_pending_prompt(windows, frame=frame, pcb_name=Path(job['pcb']).name)
current = _target_frame(windows, Path(job['pcb']).stem)
if current is None or current['hwnd'] != frame['hwnd']:
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: target frame changed')
session.post(frame['hwnd'], 0x10)
emit(stage='diagnostic_wm_close', pcb_pid=int(pcb_pid), hwnd=frame['hwnd'])
close_deadline = time.monotonic() + 30
clicked = set()
last_pending = ''
while session.pcb_alive() or session.launcher_alive():
# Cancellation stops future work; an accepted normal close must
# still finish before its queue slot can be safely released.
windows = _windows(int(pcb_pid)) if session.pcb_alive() else []
previous_clicks = set(clicked)
pending_error = None
try:
_pending_prompt(windows, closing=True, frame=frame, session=session, clicked=clicked,
pcb_name=Path(job['pcb']).name)
except RuntimeError as error:
if not str(error).startswith('DIAGNOSTIC_CLOSE_UNCONFIRMED: unknown native prompt'):
raise
# WM_CLOSE creates and destroys native windows asynchronously;
# preserve them and wait for populated controls or process exit.
pending_error = error
if str(error) != last_pending:
last_pending = str(error)
emit(stage='diagnostic_close_pending', detail=last_pending)
for hwnd in clicked - previous_clicks:
emit(stage='diagnostic_save_no', pcb_pid=int(pcb_pid), hwnd=hwnd)
if time.monotonic() >= close_deadline:
if pending_error is not None:
raise pending_error
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: target PCB/launcher still running')
time.sleep(.1)
# EE7.9 buffers PCBAutoSave's complete Job Name until normal close.
# The candidate permits closing only; PASS still needs that exact path.
evidence = _load_evidence(job, int(pcb_pid), baseline)
if evidence is None:
raise RuntimeError('DIAGNOSTIC_OPEN_UNCONFIRMED: fresh full PCB path not confirmed after close')
emit(stage='diagnostic_native_open_confirmed', **evidence)
return {'status': 'PASS', 'check': 'native_open_only', 'native_processes_exited': True,
'library_ownership': 'NOT_ASSESSED', 'pcb_pid': int(pcb_pid),
'launcher_pid': getattr(launcher_process, 'pid', None), 'evidence': evidence,
'detail': 'Native target opened and its PCB/launcher exited; refresh readiness and shared library ownership not assessed.'}
finally:
session.close()
+39 -10
View File
@@ -324,6 +324,7 @@ class DialogAgentServer:
self.channel = JsonChannel(connection)
self.channel.send({"token": paths.token}, timeout_seconds=10)
self.observed_snapshots = {}
self.observed_pids = None
@classmethod
def from_config(cls, config_path: Path, *, probe, executor=None):
@@ -348,7 +349,15 @@ class DialogAgentServer:
try:
phase = DialogPhase(request["phase"])
if request.get("op") == "observe":
snapshots = self.probe.capture(phase)
pids = request.get('pids')
if pids is not None and (not isinstance(pids, list)
or any(type(pid) is not int or pid <= 0 for pid in pids)):
raise ValueError('DIALOG_PID_SCOPE_INVALID')
self.observed_pids = set(pids) if pids is not None else None
snapshots = (self.probe.capture(phase, pids=self.observed_pids) if pids is not None
else self.probe.capture(phase))
if pids is not None:
snapshots = tuple(w for w in snapshots if w.pid in self.observed_pids)
self.observed_snapshots.clear()
for snapshot in snapshots:
self.observed_snapshots[stable_fingerprint(snapshot)] = snapshot
@@ -356,7 +365,9 @@ class DialogAgentServer:
return
if request.get("op") == "perform":
expected = self.observed_snapshots.get(request.get("fingerprint"))
if expected is None:
if (expected is None or int(request.get('hwnd', 0)) != expected.hwnd
or self.observed_pids is not None and (expected.pid not in self.observed_pids
or not self.probe.matches_identity(expected))):
self._response(request, result=ActionResult(
error_code="STALE_DIALOG",
detail="窗口已消失或指纹已变化",
@@ -422,27 +433,45 @@ class DialogAgentClient:
raise
@classmethod
def start(cls, worker, root: Path, *, timeout_seconds: float = 2.0):
def start(cls, worker, root: Path, *, timeout_seconds: float = 2.0, stop=None):
client = cls.prepare(root, desktop_name=worker.name, timeout_seconds=timeout_seconds)
try:
from .defaults import DIALOG_AGENT_STARTUP_TIMEOUT_SECONDS
worker.start_companion(dialog_agent_command(client.paths.config))
client._accept(timeout_seconds=10.0)
# Frozen imports can exceed ten seconds; PCB remains behind its gate.
client._accept(timeout_seconds=DIALOG_AGENT_STARTUP_TIMEOUT_SECONDS, stop=stop)
return client
except Exception:
client.close()
raise
def _accept(self, timeout_seconds):
def _accept(self, timeout_seconds, *, stop=None):
if self.channel is not None:
return
self.listener.settimeout(timeout_seconds)
connection, _address = self.listener.accept()
deadline = time.monotonic() + timeout_seconds
while True:
if stop is not None and stop.is_set():
raise InterruptedError('VERIFY_CANCELLED: 弹窗代理启动已取消,未启动 PCB')
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError('dialog-agent startup timed out')
self.listener.settimeout(min(.2, remaining))
try:
connection, _address = self.listener.accept()
break
except socket.timeout:
continue
channel = JsonChannel(connection)
try:
connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
hello = channel.receive(timeout_seconds)
remaining = deadline - time.monotonic()
if remaining <= 0:
raise TimeoutError('dialog-agent handshake timed out')
hello = channel.receive(min(self.timeout_seconds, remaining))
if hello.get("token") != self.paths.token:
raise DialogAgentProtocolError("dialog-agent authentication failed")
if stop is not None and stop.is_set():
raise InterruptedError('VERIFY_CANCELLED: 弹窗代理启动已取消,未启动 PCB')
self.channel = channel
except Exception:
channel.close()
@@ -473,8 +502,8 @@ class DialogAgentClient:
raise DialogAgentProtocolError(f"{response['error_code']}: {response.get('detail', '')}")
return response.get("result")
def observe(self, phase: DialogPhase) -> tuple[WindowSnapshot, ...]:
result = self._request("observe", phase)
def observe(self, phase: DialogPhase, *, pids=None) -> tuple[WindowSnapshot, ...]:
result = self._request("observe", phase, **({'pids': sorted(pids)} if pids is not None else {}))
return tuple(WindowSnapshot.from_record(item) for item in (result or ()))
def perform(self, *, hwnd: int, fingerprint: str, action: dict,
+15 -1
View File
@@ -265,9 +265,23 @@ class WindowProbe:
except Exception:
return None
def capture(self, phase: DialogPhase) -> tuple[WindowSnapshot, ...]:
def matches_identity(self, snapshot):
try:
return (self.win32gui.IsWindowVisible(snapshot.hwnd)
and self.win32process.GetWindowThreadProcessId(snapshot.hwnd)[1] == snapshot.pid
and self.win32gui.GetClassName(snapshot.hwnd) == snapshot.window_class)
except Exception:
return False
def capture(self, phase: DialogPhase, *, pids=None) -> tuple[WindowSnapshot, ...]:
snapshots = []
for hwnd in self._handles():
if pids is not None:
try:
if self.win32process.GetWindowThreadProcessId(hwnd)[1] not in pids:
continue
except Exception:
continue
try:
window_class = str(self.win32gui.GetClassName(hwnd) or "")
except Exception:
+6 -2
View File
@@ -100,11 +100,15 @@ class DialogBackend(Protocol):
class AgentDialogBackend:
"""Adapt the private-desktop JSONL client to the supervisor backend."""
def __init__(self, client) -> None:
def __init__(self, client, *, pids=None) -> None:
self.client = client
self.pids = pids
def observe(self, phase: DialogPhase):
return self.client.observe(phase)
if self.pids is None:
return self.client.observe(phase)
pids = set(self.pids())
return tuple(w for w in self.client.observe(phase, pids=pids) if w.pid in pids)
def perform(self, snapshot: WindowSnapshot, action: dict, phase: DialogPhase) -> ActionResult:
return self.client.perform(
+63 -22
View File
@@ -24,13 +24,42 @@ def _command_args(command):
kernel.LocalFree(ctypes.cast(args, wintypes.HLOCAL))
def _startup_snapshot(hwnd, pcb_pid):
import win32gui, win32process
from .dialog_models import ControlSnapshot, DialogPhase, WindowSnapshot
if win32process.GetWindowThreadProcessId(hwnd)[1] != pcb_pid:
raise ValueError('STARTUP_WINDOW_OWNER_CHANGED')
children = []
win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(child), None)
controls = tuple(ControlSnapshot(child, win32gui.GetDlgCtrlID(child) & 0xffff,
win32gui.GetClassName(child), win32gui.GetWindowText(child),
enabled=bool(win32gui.IsWindowEnabled(child))) for child in children)
return WindowSnapshot(hwnd, pcb_pid, 'ExpeditionPCB.exe', 'pcb',
win32gui.GetClassName(hwnd), win32gui.GetWindowText(hwnd), DialogPhase.PCB_STARTING, controls)
def _cancel_target(snapshot):
from .post_repair_consent import exact_prompt
from .dialog_rules import pcb_version_choice
if exact_prompt(snapshot):
return 'read_write_access', next(c for c in snapshot.controls if c.control_id == 7)
if pcb_version_choice(snapshot) is not None:
buttons = [c for c in snapshot.controls if c.class_name == 'Button']
cancel = [c for c in buttons if c.control_id == 2 and c.name == 'Cancel' and c.enabled]
if (len(cancel) == 1 and {c.control_id for c in buttons} == {6212, 6213, 6206, 1, 2}
and any(c.control_id == 6211 and c.name == '* Cancelling will load no design.'
for c in snapshot.controls)):
return 'pcb_version_choice', cancel[0]
return None
def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: None,
timeout_seconds=20, launcher_process=None):
"""Normally close our empty PCB after a diagnosed database startup failure.
No COM attach: there is no document, and the global ROT can name another
session. Unknown windows, live database addresses and other desktops are
retained. WM_CLOSE is cooperative; no save prompt is accepted or killed.
No COM attach: the global ROT can name another session. Only the exact
read/write question is declined; no ownership is taken or version restored.
Unknown windows and documents are retained. WM_CLOSE is cooperative.
"""
import win32api, win32con, win32gui, win32process
from .mentor_adapter import _process_details
@@ -43,23 +72,6 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
if launcher_process is not None:
result['launcher_pid'] = launcher_process.pid
try:
diagnosed = False
diagnosis_path = root/'diagnostics/icdb-recovery.json'
if diagnosis_path.is_file():
diagnosis = json.loads(diagnosis_path.read_text('utf-8'))
diagnosed = diagnosis.get('error_code') == 'ICDB_REPAIR_REQUIRED'
dialogs_path = root/'dialogs.jsonl'
if not diagnosed and dialogs_path.is_file():
for line in dialogs_path.read_text('utf-8').splitlines():
try:
dialog = json.loads(line)
except ValueError:
continue
if (dialog.get('pid') == pcb_pid and dialog.get('phase') == 'PCB_STARTING'
and dialog.get('error_code') == 'MENTOR_DATABASE_LOAD_FAILED'):
diagnosed = True
if not diagnosed:
raise ValueError('STARTUP_FAILURE_UNCONFIRMED')
desktop = job.get('desktop', '')
if (job.get('interactive_input') or not desktop.startswith('CbbVerify_')
or _current_desktop_name() != desktop):
@@ -69,14 +81,20 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
expected = os.path.normcase(os.path.abspath(job['pcb']))
deadline = time.monotonic() + max(0, timeout_seconds)
sent = False
declined_hwnd = None
declined_kind = None
cancelled_open = False
while True:
records = [r for r in _process_details('ExpeditionPCB.exe') if r[0] == pcb_pid]
if not records:
if declined_hwnd is not None:
cancelled_open = True
result['cancelled_prompt'] = declined_kind
addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])
launcher_exited = launcher_process is None or launcher_process.poll() is not None
result['release'] = {'pcb_exited': True, 'launcher_exited': launcher_exited,
'addresses': addresses,
'safe_to_continue': launcher_exited and not addresses}
'safe_to_continue': launcher_exited and (cancelled_open or not addresses)}
if result['release']['safe_to_continue']:
result['status'] = 'CLOSED'
result.pop('detail', None)
@@ -100,6 +118,29 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
windows.append(hwnd)
win32api.SetLastError(0)
win32gui.EnumWindows(observe, None)
if declined_hwnd is not None and declined_hwnd not in windows:
cancelled_open = True
result['cancelled_prompt'] = declined_kind
if declined_hwnd is None:
for hwnd in windows:
if win32gui.GetClassName(hwnd) != '#32770':
continue
snapshot = _startup_snapshot(hwnd, pcb_pid)
target = _cancel_target(snapshot)
if target is None:
continue
# Re-read immediately before declining; never select a version.
current = _startup_snapshot(hwnd, pcb_pid)
if _cancel_target(current) != target or current != snapshot:
raise ValueError('STARTUP_CANCEL_PROMPT_CHANGED')
declined_kind, cancel = target
win32gui.PostMessage(cancel.handle, win32con.BM_CLICK, 0, 0)
declined_hwnd = hwnd
emit(stage='startup_open_declined', pcb_pid=pcb_pid, hwnd=hwnd,
control_id=cancel.control_id, prompt=declined_kind)
break
if declined_hwnd is not None:
continue
main = [w for w in windows if win32gui.GetClassName(w).startswith('Afx:')
and win32gui.GetWindowText(w).strip() in (
'Expedition PCB', 'Expedition PCB - Reusable Block')
@@ -131,7 +172,7 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
win32api.SetLastError(0)
win32gui.EnumChildWindows(main[0], child, None)
addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])
if len(main) == 1 and not unknown and not documents and not addresses:
if len(main) == 1 and not unknown and not documents and (cancelled_open or not addresses):
win32gui.PostMessage(main[0], win32con.WM_CLOSE, 0, 0)
sent = True
emit(stage='startup_close_requested', pcb_pid=pcb_pid, hwnd=main[0])
+33 -1
View File
@@ -17,6 +17,37 @@ _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)
_ICDB_MESSAGE = re.compile(r'(?<![A-Za-z0-9_])icdb(?![A-Za-z0-9_.\\/])', re.I)
_FAILURE_MESSAGE = re.compile(
r'\b(?:error|failed|failure|unable|cannot|could\s+not|lost|corrupt(?:ed|ion)?|'
r'inconsistent|unsupported|refused|denied)\b|失败|错误|异常|无法|损坏|失联|拒绝|未完成', re.I)
def _icdb_error_paragraphs(text, *, error_message=False):
text = str(text or '').replace('\r', '')
for record in _session_losses(text):
yield record['native_message'].strip()
for paragraph in text.splitlines():
# A pathname or our own category label is not native error content.
message = re.sub(r'\[[^\]\n]*[\\/][^\]\n]*\]', '', paragraph)
if _ICDB_MESSAGE.search(message) and (error_message or _FAILURE_MESSAGE.search(message)):
yield paragraph.strip()
def icdb_error_content(text, *, error_message=False):
"""Extract failure paragraphs mentioning iCDB; codes/healthy text do not qualify."""
return '\n\n'.join(dict.fromkeys(_icdb_error_paragraphs(text, error_message=error_message)))
def read_icdb_error(path, baseline):
"""Retain only new native error content, with the same bounded freshness rules."""
data, appended = _read_log_delta(path, baseline)
if not appended and (not baseline or baseline.get('truncated') or 'icdb_errors' not in baseline):
return ''
previous = baseline.get('icdb_errors', [])
return '\n\n'.join(message for message in _icdb_error_paragraphs(decode_log(data))
if appended or ' '.join(message.split()) not in previous)
def parse_session_loss(text):
"""Keep the native target verbatim; never infer a local repair target."""
@@ -68,9 +99,10 @@ def snapshot_log(path):
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,
'icdb_errors': [' '.join(message.split()) for message in _icdb_error_paragraphs(decode_log(tail))],
'session_losses': losses}
except FileNotFoundError:
return {'size': 0, 'tail_sha256': hashlib.sha256(b'').hexdigest()}
return {'size': 0, 'tail_sha256': hashlib.sha256(b'').hexdigest(), 'icdb_errors': []}
except OSError as error:
return {'error': str(error)}
+165 -32
View File
@@ -17,6 +17,16 @@ class LibraryBusy(OSError):
pass
def inspect_residual_sessions(library, *, block_names=()):
from .residual_sessions import inspect_residual_sessions as inspect
return inspect(library, block_names=block_names)
def release_residual_sessions(plan, evidence_dir, *, stop=None, force=False):
from .residual_sessions import release_residual_sessions as release
return release(plan, evidence_dir, stop=stop, force=force)
def _key(path):
return str(Path(path).resolve()).casefold()
@@ -53,32 +63,48 @@ def checkpoint_record(path):
def has_blocker(library):
path = blocker_path(library)
if path.with_suffix('.pending').exists():
return True
if not path.exists():
return False
record = checkpoint_record(path)
# Complete snapshots retain every file, including their source lease
# checkpoint. Only a valid relocated snapshot record can be ignored.
return record is None or _key(record['library']) == _key(library)
for marker in (path, path.with_suffix('.pending')):
if marker.exists():
record = checkpoint_record(marker)
if record is None or _key(record['library']) == _key(library):
return True
return False
def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id):
def has_retained_library(library):
"""Include leases retained without a JSON checkpoint, such as PCB checks."""
path = Path(library).resolve()
key = _key(path.with_name('.' + path.stem + '.cbb-upload.lock'))
with _guard:
owner = _owners.get(key)
return bool(owner and owner['retained'])
def _persist_blocker(library, status='OWNER_RELEASE_UNCONFIRMED', *, lease_id, block_name=None):
import win32api, win32process
path = blocker_path(library)
pending = path.with_suffix('.pending')
previous = checkpoint_record(path)
export_record = ({'export_analysis': previous['export_analysis']}
if previous and previous['lease_id'] == lease_id and 'export_analysis' in previous else {})
if block_name is not None:
export_record['block_name'] = block_name
elif previous and previous['lease_id'] == lease_id and 'block_name' in previous:
export_record['block_name'] = previous['block_name']
with pending.open('w', encoding='utf-8') as stream:
json.dump({**export_record, 'protocol': 'cbb-refresh-verify-library-lease', 'version': 1,
'lease_id': lease_id, 'library': str(library), 'status': status,
'machine': socket.gethostname(),
'supervisor_created': win32process.GetProcessTimes(win32api.GetCurrentProcess())['CreationTime'].isoformat(),
'pid': os.getpid(), 'time': datetime.now(timezone.utc).isoformat()}, stream)
stream.flush(); os.fsync(stream.fileno())
pending.replace(path)
class LibraryLock:
def __init__(self, library, *, safe_to_release=None, checkpoint=True):
def __init__(self, library, *, safe_to_release=None, checkpoint=True, block_name=None):
library = Path(library).resolve()
self.library = library
self.path = library.with_name('.' + library.stem + '.cbb-upload.lock')
@@ -86,6 +112,7 @@ class LibraryLock:
self.entered = False
self.safe_to_release = safe_to_release
self.checkpoint = checkpoint
self.block_name = block_name
self.maintenance_guard = None
self.maintenance_mutex = None
@@ -105,6 +132,13 @@ class LibraryLock:
except Exception:
released = False
if released:
if owner.get('checkpoint', True):
marker = blocker_path(self.library)
record = checkpoint_record(marker)
if (record is None or record['lease_id'] != owner['lease_id']
or 'export_analysis' in record or marker.with_suffix('.pending').exists()):
raise LibraryBusy('协调检查点身份或导出会话尚未确认,保持库锁。')
marker.unlink()
owner['handle'].Close()
del _owners[self.key]
owner = None
@@ -126,6 +160,8 @@ class LibraryLock:
win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None,
win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None)
except Exception as error:
if getattr(error, 'winerror', None) not in (32, 33):
raise
raise LibraryBusy(f'中心库已锁定或无法创建协调锁:{self.path}: {error}') from error
try:
data = (f'machine={socket.gethostname()}; pid={os.getpid()}; '
@@ -140,14 +176,16 @@ class LibraryLock:
import pywintypes
win32file.SetFileTime(handle, None, None, pywintypes.Time(datetime.now()))
if self.checkpoint:
_persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id)
_persist_blocker(self.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id,
block_name=self.block_name)
except Exception:
handle.Close()
raise
_owners[self.key] = {'thread': threading.get_ident(), 'depth': 1,
'handle': handle, 'retained': False, 'lease_id': lease_id,
'checkpoint': self.checkpoint,
'release_probe': self.safe_to_release if not self.checkpoint else None}
'block_name': self.block_name,
'release_probe': self.safe_to_release}
self.entered = True
return self
@@ -241,6 +279,67 @@ def _supervisor_alive(pid):
handle.Close()
def _recorded_supervisor_alive(record):
"""None means legacy/unknown identity; the native/file guard still applies."""
if record.get('machine') != socket.gethostname() or not record.get('supervisor_created'):
return None
import pywintypes, win32api, win32con, win32process
try:
handle = win32api.OpenProcess(win32con.SYNCHRONIZE | 0x1000, False, record['pid'])
except pywintypes.error as error:
return False if error.winerror == 87 else None
try:
return (win32process.GetProcessTimes(handle)['CreationTime'].isoformat() == record['supervisor_created']
and _supervisor_alive(record['pid']))
finally:
handle.Close()
def _owner_matches_scope(item, library, roots):
"""False requires an explicit other project; a missing path stays unknown."""
import ntpath
from .failed_session import _command_args
command = str(item.get('command') or '')
if not command:
return None
try:
args = _command_args(command)
except (OSError, ValueError):
return None
candidates = []
worker_flags = {'--verify-worker', '--repair-worker', '--management-worker', '--native-backup-worker'}
for index, arg in enumerate(args[1:], 1):
if arg.casefold() in worker_flags and index + 1 < len(args):
try:
request = Path(args[index + 1])
if request.stat().st_size > 1024 * 1024:
return None
record = json.loads(request.read_text(encoding='utf-8'))
for key in ('library', 'lmc_file', 'project', 'database'):
if isinstance(record.get(key), str):
candidates.append(record[key])
except (OSError, ValueError, TypeError, AttributeError):
return None
value = arg.split('=', 1)[1] if arg.startswith('-') and '=' in arg else arg
if (ntpath.isabs(value) and (ntpath.splitext(value)[1].casefold() in ('.prj', '.pcb', '.lmc')
or arg.casefold().startswith(('-project=', '-icdb='))
or args[index - 1].casefold() in ('-icdb', '-project', '-p'))):
candidates.append(value)
normalized_roots = [ntpath.normcase(ntpath.normpath(str(root))) for root in roots]
expected_library = ntpath.normcase(ntpath.normpath(str(library)))
for candidate in candidates:
path = ntpath.normcase(ntpath.normpath(candidate))
if path == expected_library:
return True
for root in normalized_roots:
try:
if ntpath.commonpath((path, root)) == root:
return True
except ValueError:
pass
return False if candidates else None
def _enter_maintenance(lease):
"""Transfer only a valid known lease, after project admission under lock."""
import win32con, win32event, win32file
@@ -266,13 +365,16 @@ def _enter_maintenance(lease):
raise LibraryBusy('本进程库锁仍在使用或检查点身份不符')
handle = existing['handle']
else:
if record is not None and record['pid'] != os.getpid() and _supervisor_alive(record['pid']):
if record is not None and record['pid'] != os.getpid() and _recorded_supervisor_alive(record) is True:
raise LibraryBusy('原监督进程仍存在,不能接续其库锁')
handle = win32file.CreateFile(str(lease.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
0, None, win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None)
# All target/process/address/exclusive-file checks finish BEFORE
# any old checkpoint replacement. Keep the exact sidecar open.
from .native_export_guard import prove_export_copies_released
if record is not None and _recorded_supervisor_alive(record) is None:
from .maintenance_entry import assert_native_idle
assert_native_idle()
with prove_export_copies_released(lease.library), lease.maintenance_guard() as proof:
if (marker.read_bytes() if marker.exists() else None) != old:
raise LibraryBusy('维护检查期间检查点变化')
@@ -285,7 +387,8 @@ def _enter_maintenance(lease):
lease_id = uuid4().hex
_persist_blocker(lease.library, 'OPERATION_IN_PROGRESS', lease_id=lease_id)
_owners[lease.key] = {'thread': threading.get_ident(), 'depth': 1, 'handle': handle,
'retained': False, 'lease_id': lease_id}
'retained': False, 'lease_id': lease_id, 'checkpoint': True,
'release_probe': lease.safe_to_release, 'block_name': None}
lease.maintenance_proof = proof
lease.entered = True
lease.maintenance_mutex = mutex
@@ -315,16 +418,20 @@ def owned_snapshot(path):
@contextmanager
def _prove_library_released(library, block_name=None):
def _prove_library_released(library, block_name=None, *, allow_unknown_checkpoint=False):
"""Hold all native database read handles during explicit lease release."""
import stat
import win32con, win32file
from .owner_probe import query_owners
from .scoped_manifest import scan_tree
from .native_management import _retained_workers
from .native_export_guard import assert_retained_exports_idle, prove_export_copies_released
from .native_engineering_identity import _OWNER_NAMES
assert_retained_exports_idle()
if any(worker.poll() is None for worker in _retained_workers):
library = Path(library).resolve()
assert_retained_exports_idle(library)
if any(worker.poll() is None and (allow_unknown_checkpoint
or not getattr(worker, 'cbb_library', None) or _key(worker.cbb_library) == _key(library))
for worker in _retained_workers):
raise LibraryBusy('原生管理工作进程尚未返回,不能解除协调阻止。')
from .com_runtime import initialize_com, uninitialize_com
initialized = initialize_com()
@@ -334,43 +441,58 @@ def _prove_library_released(library, block_name=None):
'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe',
'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES))
scope_root = (Path(library).resolve().parent/'ReusableBlocksLP'/block_name/'Work').resolve() if block_name else None
shared_root = Path(library).resolve().parent/'SymbolLibs'
if allow_unknown_checkpoint and scope_root is not None:
raise LibraryBusy('未知检查点必须执行完整库释放证明。')
def check_owners():
owners = []
for item in query_owners(names):
if item['pid'] == os.getpid():
continue
if scope_root is not None and item.get('command'):
command = str(item['command']).casefold().replace('/', '\\')
if str(scope_root).casefold().replace('/', '\\') not in command:
if not allow_unknown_checkpoint:
roots = (scope_root, shared_root) if scope_root is not None else (Path(library).parent,)
if _owner_matches_scope(item, library, roots) is False:
continue
automation = item['name'].casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe')
command = item['command'].casefold()
if not automation or not command or any(flag in command for flag in (
'--verify-worker', '--repair-worker', '--management-worker')):
'--verify-worker', '--repair-worker', '--management-worker', '--native-backup-worker')):
owners.append(item)
if owners:
raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}')
return owners
check_owners()
entries = scan_tree(Path(library).parent)
if scope_root is None:
entries = scan_tree(Path(library).parent)
else:
entries = {Path(library): Path(library).lstat()}
for root in (scope_root, shared_root):
if root.exists():
entries.update(scan_tree(root))
addresses = [str(path) for path in entries if path.name.casefold() == 'saddress.adr'
and (scope_root is None or path.resolve().is_relative_to(scope_root))]
and (scope_root is None or path.is_relative_to(scope_root)
or path.is_relative_to(shared_root))]
if addresses:
raise LibraryBusy(f'库中仍有数据库地址记录,不能自动解除:{addresses}')
native = [path for path in entries if path.is_file() and (
path.suffix.casefold() == '.lmc'
# scan_tree already rejects links; only native candidates need file opens.
native = [path for path, metadata in entries.items() if stat.S_ISREG(metadata.st_mode) and (
path == library
or (path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')
and (scope_root is None or path.resolve().is_relative_to(scope_root))))]
if Path(library).resolve() not in native:
and (scope_root is None or path.is_relative_to(scope_root)
or path.is_relative_to(shared_root))))]
if library not in native:
raise LibraryBusy('目标库身份缺失,不能解除协调阻止。')
with ExitStack() as stack:
export_copies = stack.enter_context(prove_export_copies_released(library))
export_proof = (prove_export_copies_released(library, allow_unknown_checkpoint=True)
if allow_unknown_checkpoint else prove_export_copies_released(library))
export_copies = stack.enter_context(export_proof)
for path in native:
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
stack.callback(handle.Close)
owners = check_owners()
yield {'native_owners': owners, 'addresses': addresses, 'export_copies': export_copies,
'unknown_checkpoint_recovery': allow_unknown_checkpoint,
'exclusive_files': [str(path) for path in native]}
finally:
uninitialize_com(initialized)
@@ -405,17 +527,28 @@ def release_retained_library(library, evidence_dir, *, block_name=None):
win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None,
win32con.OPEN_ALWAYS, win32con.FILE_ATTRIBUTE_NORMAL, None)
except Exception as error:
if getattr(error, 'winerror', None) not in (32, 33):
raise
raise LibraryBusy('其他进程仍持有库锁,不能解除。') from error
proof_context = (_prove_library_released(library, block_name=block_name)
if block_name else _prove_library_released(library))
marker = blocker_path(library)
checkpoints = (marker, marker.with_suffix('.pending'))
before = {path: path.read_bytes() if path.exists() else None for path in checkpoints}
unknown = any(data is not None and checkpoint_record(path) is None for path, data in before.items())
records = [checkpoint_record(path) for path, data in before.items() if data is not None]
scoped = (block_name if block_name and all(record and record.get('block_name') == block_name
for record in records) and (owner is None or owner.get('block_name') == block_name) else None)
proof_context = (_prove_library_released(library, allow_unknown_checkpoint=True) if unknown else
_prove_library_released(library, block_name=scoped)
if scoped else _prove_library_released(library))
with proof_context as proof:
marker = blocker_path(library)
checkpoints = (marker, marker.with_suffix('.pending'))
if any(path.exists() and checkpoint_record(path) is None for path in checkpoints):
raise LibraryBusy('协调检查点格式不明或已损坏,保留原文件供人工审查。')
if unknown and proof.get('unknown_checkpoint_recovery') is not True:
raise LibraryBusy('未知检查点缺少完整库释放证明,保留原文件。')
if any((path.read_bytes() if path.exists() else None) != data for path, data in before.items()):
raise LibraryBusy('释放检查期间协调检查点变化,保留原文件。')
root = evidence/('release-' + uuid4().hex)
root.mkdir(parents=True, exist_ok=False)
record = {'library': str(library), 'status': 'RELEASE_CONFIRMED', **proof,
'unknown_checkpoint_archived': unknown,
'data_recovery_confirmed': False, 'evidence_path': str(root/'release.json')}
(root/'release.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
for index, checkpoint in enumerate(checkpoints):
+64 -67
View File
@@ -15,12 +15,29 @@ from uuid import uuid4
def assert_native_idle(target=None):
from .owner_probe import query_owners
from .failed_session import _command_args
from .native_management import _retained_workers
from .native_export_guard import assert_retained_exports_idle
from .native_engineering_identity import _OWNER_NAMES
assert_retained_exports_idle()
if any(worker.poll() is None for worker in _retained_workers):
from .library_lock import _key, _owner_matches_scope
library = None
roots = ()
if target is not None:
from .backup_recovery import _project_text
roots = (Path(target.project).parent, Path(target.database))
try:
lines = re.findall(r'^KEY CentralLibrary\b[^\r\n]*', _project_text(target.project), re.M)
references = [re.fullmatch(r'KEY CentralLibrary "([^"\r\n]+)"\s*', line) for line in lines]
if not references or any(match is None for match in references):
raise ValueError('CentralLibrary references are missing or invalid.')
libraries = {(Path(target.project).parent/match[1]).resolve(strict=True) for match in references}
if len(libraries) == 1 and all(path.suffix.casefold() == '.lmc' and path.is_file() for path in libraries):
library = libraries.pop()
roots += (library.parent/'SymbolLibs',)
except (OSError, ValueError):
pass # Missing/ambiguous original references cannot narrow retained ownership.
assert_retained_exports_idle(library)
if any(worker.poll() is None and (library is None or not getattr(worker, 'cbb_library', None)
or _key(worker.cbb_library) == _key(library)) for worker in _retained_workers):
raise ValueError('MAINTENANCE_OWNER_ACTIVE: 管理工作进程尚未返回')
names = ('ExpeditionPCB.exe', 'LibraryManager.exe', 'viewdraw.exe', 'iCDBProjectBackup.exe',
'ReuseBlockImporter.exe', 'CleanWGLib.exe', 'cscript.exe', 'wscript.exe',
@@ -32,18 +49,10 @@ def assert_native_idle(target=None):
name, command = item['name'].casefold(), item['command'].casefold()
automation = name in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe')
if not automation or not command or any(flag in command for flag in (
'--verify-worker', '--repair-worker', '--management-worker')):
'--verify-worker', '--repair-worker', '--management-worker', '--native-backup-worker')):
owners.append(item)
if target is not None:
expected = os.path.normcase(os.path.abspath(str(target.project)))
scoped = []
for item in owners:
command = str(item.get('command') or '')
if not command:
scoped.append(item)
elif expected in [os.path.normcase(os.path.abspath(arg)) for arg in _command_args(command)[1:]]:
scoped.append(item)
owners = scoped
owners = [item for item in owners if _owner_matches_scope(item, library, roots) is not False]
if owners:
raise ValueError(f'MAINTENANCE_OWNER_ACTIVE: 请正常关闭原生程序/操作助手:{owners}')
@@ -215,13 +224,9 @@ def maintenance_target_guard(request, expected=None, *, stop=None):
def run_icdb_check(request, *, progress=None, stop=None):
"""Inspect through Project Backup; never launch PCB, Repair or Restore."""
from .verify_runner import VerifyResult
from .library_lock import LibraryLock, LibraryBusy
from .native_repair import run_native_repair, RepairTarget
from .native_backup import native_backup_project
from .backup_recovery import _project_text
from .native_occupancy import confirmed_inspection
"""Open PCB diagnostically and classify this attempt's native error content."""
from .verify_runner import VerifyResult, run_verify
from .icdb_recovery import icdb_error_content
root = Path(request.evidence_dir).resolve()/('icdb-check-'+uuid4().hex[:12])
library_root = Path(request.lmc_file).resolve().parent
if root.is_relative_to(library_root) or library_root.is_relative_to(root):
@@ -229,10 +234,7 @@ def run_icdb_check(request, *, progress=None, stop=None):
stderr='检查记录目录需位于中心库之外。')
record = {'operation': 'icdb_batch_check', 'automatic_repair': False,
'library': str(request.lmc_file), 'block': request.block_name}
released = True
def check():
if stop is not None and stop.is_set():
raise InterruptedError('已取消检查。')
started = False
def save(result):
report = root/'result.json'
recorded = replace(result, evidence_path=report)
@@ -245,51 +247,46 @@ def run_icdb_check(request, *, progress=None, stop=None):
return result
return recorded
try:
check()
root.mkdir(parents=True, exist_ok=False)
with LibraryLock(request.lmc_file, checkpoint=False, safe_to_release=lambda: released):
project = native_backup_project(request.lmc_file, request.block_name)
values = re.findall(r'^KEY iCDBDir "([^"\r\n]*)"', _project_text(project), re.M)
if (len(values) != 1 or not values[0] or PureWindowsPath(values[0]).drive
or PureWindowsPath(values[0]).root
or (project.parent/values[0]).resolve() != (project.parent/'database').resolve()):
raise ValueError('无法确认工程的 ICDB 数据库路径。')
target = RepairTarget(project, project.parent/'database')
record['target'] = asdict(target)
check()
if progress:
progress({'stage': 'icdb_ownership_check', 'block': request.block_name,
'detail': '正在检查 ICDB 状态'})
released = False
inspection = run_native_repair(target, root/'native-inspection',
mentor_bin=request.mentor_bin, timeout_seconds=min(30, request.timeout_seconds),
progress=progress, inspection_only=True, stop=stop)
record['native_inspection'] = inspection
released = (inspection.get('release', {}).get('native_exited') is True
or inspection.get('native_started') is False
and inspection.get('native_pid') is None and inspection.get('worker_pid') is None)
if ((stop is not None and stop.is_set())
or inspection.get('status') in ('CANCELLED', 'CANCELLED_UNRELEASED')):
return save(VerifyResult('CANCELLED', safe_to_continue=released, stderr='已取消 ICDB 检查。'))
if not confirmed_inspection(target, inspection):
return save(VerifyResult('FAIL', safe_to_continue=released,
error_category='ICDB_INSPECTION_UNCONFIRMED',
stderr='无法确认 ICDB 状态。' + str(inspection.get('detail', ''))))
if inspection['status'] == 'IN_USE':
return save(VerifyResult('BLOCKED', safe_to_continue=released,
error_category='ICDB_DATABASE_IN_USE', stderr='工程正在使用,暂未完成 ICDB 检查。'))
if inspection['repair_required']:
return save(VerifyResult('FAIL', safe_to_continue=released,
error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。'))
return save(VerifyResult('CHECKED', safe_to_continue=released,
stdout='ICDB 检查未发现异常,无需修复。'))
except InterruptedError:
return save(VerifyResult('CANCELLED', safe_to_continue=released, stderr='已取消 ICDB 检查。'))
except LibraryBusy as error:
return save(VerifyResult('BLOCKED', safe_to_continue=False,
error_category='LIBRARY_IN_USE', stderr=str(error)))
if stop is not None and stop.is_set():
return save(VerifyResult('CANCELLED', stderr='已取消 ICDB 检查。'))
if progress:
progress({'stage': 'icdb_startup_check', 'block': request.block_name,
'detail': '正在打开工程并检查本次错误信息'})
started = True
startup = run_verify(replace(request, maintenance=False, auto_repair=False,
diagnostic_only=True, native_lm_verify=False, post_repair_proof=None,
evidence_dir=root/'pcb-open'), progress=progress, stop=stop)
record['native_open'] = asdict(startup)
if startup.status == 'CANCELLED' or stop is not None and stop.is_set():
return save(replace(startup, status='CANCELLED', recovery_attempts=()))
if startup.status == 'PASS' and startup.safe_to_continue:
return save(replace(startup, status='CHECKED', library_flag='UNKNOWN',
stdout='工程打开正常,未发现 ICDB 错误;未执行 CBB 校验。', recovery_attempts=()))
content = icdb_error_content(startup.stderr, error_message=True)
if (startup.evidence_path
and startup.evidence_path.resolve().is_relative_to(root/'pcb-open')):
try:
saved = json.loads(startup.evidence_path.with_name('diagnostics').joinpath(
'icdb-error-content.json').read_text('utf-8'))
native = icdb_error_content(saved.get('native_message', ''))
if native:
content = '\n'.join(dict.fromkeys(filter(None, (content, native))))
record['native_error'] = saved
except (OSError, ValueError, AttributeError):
pass
if content:
return save(replace(startup, status='FAIL', library_flag='UNKNOWN',
error_category='ICDB_REPAIR_REQUIRED',
stderr='发现 ICDB 错误,未执行修复。\n'+content, recovery_attempts=()))
category = startup.error_category
if not category or category.startswith('ICDB_'):
category = 'ICDB_INSPECTION_UNCONFIRMED'
return save(replace(startup, status='FAIL' if startup.status=='PASS' else startup.status,
error_category=category, library_flag='UNKNOWN', recovery_attempts=(),
stderr=startup.stderr or '工程打开或释放未完成,尚无法确认 ICDB 状态。'))
except Exception as error:
return save(VerifyResult('FAIL', safe_to_continue=released,
return save(VerifyResult('FAIL', safe_to_continue=not started,
error_category='ICDB_INSPECTION_UNCONFIRMED', stderr=f'无法确认 ICDB 状态:{error}'))
+60 -24
View File
@@ -31,6 +31,7 @@ class ManagementRequest:
mapped_drive: str = ''
mapped_remote_root: str = ''
transaction_token: str = ''
check_previous_release: bool = False
@dataclass(frozen=True)
@@ -71,6 +72,7 @@ class ManagementResult:
safe_to_continue: bool
detail: str = ''
evidence_path: Path | None = None
release_proof: dict | None = None
def validate_name(value):
@@ -150,13 +152,22 @@ def unfinished_operations(root):
for path in sorted(Path(root).glob('*/checkpoint.json')):
try:
record = json.loads(path.read_text(encoding='utf-8'))
if record.get('stage') != 'complete' or record.get('safe_to_continue') is False:
if (record.get('stage') != 'complete' or record.get('safe_to_continue') is False
or record.get('status') == 'RECOVERY_REQUIRED'):
results.append({**record, 'evidence_path': str(path)})
except (OSError, ValueError):
results.append({'status': 'CHECKPOINT_UNREADABLE', 'evidence_path': str(path)})
return results
def _may_have_mutated(record):
prewrite = {'preflight', 'release_check', 'catalog_before', 'baseline', 'stage_source', 'prepare_identity'}
history = record.get('history')
return (record.get('stage') not in prewrite | {'complete'} or record.get('status') == 'RECOVERY_REQUIRED'
or not isinstance(history, list) or not history
or any(not isinstance(item, dict) or item.get('stage') not in prewrite for item in history))
def run_management(request, *, adapter=None, verifier=None, idle_check=None, progress=None):
from .verify_runner import VerifyRequest, run_verify
from .backup_recovery import _idle_check
@@ -174,6 +185,17 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
owner_safe = True
mutex = None
acquired = False
release_proof = None
def invoke(callback, *args):
nonlocal owner_safe
owner_safe = False
try:
result = callback(*args)
except Exception as error:
owner_safe = getattr(error, 'safe_to_continue', False) is True
raise
owner_safe = result.safe_to_continue
return result
def checkpoint(next_stage, **details):
nonlocal stage
stage = next_stage
@@ -185,9 +207,12 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
progress({'stage': stage, 'block': request.name, 'operation': request.operation, **details})
def finish(status, detail='', safe=True):
result = ManagementResult(request.operation, request.library, request.name, request.partition,
request.source, request.auto_verify, 'complete', status, safe, detail, report)
request.source, request.auto_verify, 'complete', status, safe, detail, report, release_proof)
if report:
_write_json(report, {**asdict(result), 'history': history, 'backup_policy': 'manual'})
try:
_write_json(report, {**asdict(result), 'history': history, 'backup_policy': 'manual'})
except OSError as error:
result = replace(result, detail=f'{detail}\n操作记录写入失败:{error}'.strip())
return result
from .com_runtime import initialize_com, uninitialize_com
initialized = initialize_com()
@@ -207,9 +232,13 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
capability = adapter.capabilities().get(request.operation, Capability(False, '未验证的原生接口'))
if not capability.supported:
return finish('UNSUPPORTED', capability.reason)
unfinished_target = None
for unfinished in unfinished_operations(evidence):
if unfinished.get('library') and Path(unfinished['library']).resolve() == canonical_library:
return finish('BLOCKED', f'存在未完成/需恢复任务,禁止自动重放:{unfinished["evidence_path"]}', False)
if (unfinished.get('library') and Path(unfinished['library']).resolve() == canonical_library
and _may_have_mutated(unfinished) and (not unfinished.get('name')
or str(unfinished['name']).casefold() == request.name.casefold())):
unfinished_target = unfinished
break
source = pcb = None
if request.operation in ('add', 'update'):
if request.source is None:
@@ -219,16 +248,24 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
or evidence.is_relative_to(source.parent)
or source.parent.is_relative_to(evidence)):
raise ValueError('来源工程、目标库与操作证据必须使用独立目录。')
root = evidence/('management-' + uuid4().hex)
root.mkdir(parents=True, exist_ok=False)
report = root/'checkpoint.json'
checkpoint('release_check')
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
if not acquired:
return finish('BLOCKED', '已有 CBB 校验/管理/恢复任务运行。', False)
return finish('BLOCKED', '已有 CBB 校验/管理/恢复任务运行;本次未启动。')
from .library_lock import has_blocker, has_retained_library, release_retained_library
if (has_blocker(library) or has_retained_library(library) or request.check_previous_release
or unfinished_target and unfinished_target.get('safe_to_continue') is not True):
release_proof = release_retained_library(library, root/'previous-release')
if unfinished_target:
return finish('BLOCKED', f'存在未完成/需恢复任务,禁止自动重放:{unfinished_target["evidence_path"]}')
if hasattr(adapter, 'bind_library_mapping'):
adapter.bind_library_mapping(request.mapped_drive, request.mapped_remote_root)
with LibraryLock(library, safe_to_release=lambda: owner_safe):
root = evidence/('management-' + uuid4().hex)
root.mkdir(parents=True, exist_ok=False)
report = root/'checkpoint.json'
with LibraryLock(library, block_name=request.name, safe_to_release=lambda: owner_safe or (
callable(getattr(adapter, 'release_confirmed', None)) and adapter.release_confirmed(library))):
work = library.parent/'ReusableBlocksLP'/request.name/'Work'
target_project, target_pcb = work/(request.name+'.prj'), work/'PCB/pcb.pcb'
if request.operation != 'add':
@@ -244,7 +281,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
idle_check(library, source, pcb)
checkpoint('catalog_before')
owner_safe = False
before = adapter.catalog(library, root/'before')
before = invoke(adapter.catalog, library, root/'before')
owner_safe = before.safe_to_continue
if not owner_safe or Path(before.library).resolve() != canonical_library or before.flow != 1:
return finish('BLOCKED', '原生目录身份/流程或所有者释放未确认。仅支持已验证 DxDesigner 流程。', owner_safe)
@@ -285,32 +322,29 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
if request.operation in ('remove', 'update'):
checkpoint('native_remove')
mutated = True; owner_safe = False
native = adapter.remove(request, root/'remove')
native = invoke(adapter.remove, request, root/'remove')
owner_safe = native.safe_to_continue
if not owner_safe or not native.success:
return finish('RECOVERY_REQUIRED', native.detail, False)
owner_safe = False
return finish('RECOVERY_REQUIRED', native.detail, owner_safe)
idle_check(library, target_project, target_pcb)
owner_safe = False
after = adapter.catalog(library, root/'after-remove')
after = invoke(adapter.catalog, library, root/'after-remove')
owner_safe = after.safe_to_continue
if (not owner_safe or Path(after.library).resolve() != canonical_library or after.flow != 1
or any(e.name == request.name and e.partition == request.partition for e in after.entries)):
return finish('RECOVERY_REQUIRED', '原生移除后目录未确认目标缺失。', False)
return finish('RECOVERY_REQUIRED', '原生移除后目录未确认目标缺失。', owner_safe)
removed = True
checkpoint('removed')
if request.operation == 'remove':
return finish('REMOVED', '已由原生操作移除,重新读取目录确认不存在;应用未创建备份。')
checkpoint('native_add')
mutated = True; owner_safe = False
native = adapter.add(staged, root/'add')
native = invoke(adapter.add, staged, root/'add')
owner_safe = native.safe_to_continue
if not owner_safe or not native.success:
return finish('RECOVERY_REQUIRED' if removed or not owner_safe else 'UPLOAD_FAILED',
native.detail, owner_safe and not removed)
owner_safe = False
native.detail, owner_safe)
idle_check(library, target_project, target_pcb)
after = adapter.catalog(library, root/'after-add')
after = invoke(adapter.catalog, library, root/'after-add')
owner_safe = after.safe_to_continue
matches = [entry for entry in after.entries if entry.name == request.name
and entry.partition == request.partition]
@@ -319,7 +353,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
len(matches) == 1 and matches[0].engineering_identity == identity)
if (not owner_safe or Path(after.library).resolve() != canonical_library or after.flow != 1
or len(matches) != 1 or not proof_matches):
return finish('RECOVERY_REQUIRED', '原生新增后名称、分区或来源工程内容未确认一致。', False)
return finish('RECOVERY_REQUIRED', '原生新增后名称、分区或来源工程内容未确认一致。', owner_safe)
uploaded = True
checkpoint('uploaded')
if not request.auto_verify:
@@ -336,12 +370,14 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
verified.stderr or verified.library_flag_detail, owner_safe)
except Exception as error:
if uploaded:
return finish('UPLOADED_VERIFY_FAILED', str(error), False)
return finish('UPLOADED_VERIFY_FAILED', str(error), owner_safe)
status = 'RECOVERY_REQUIRED' if mutated else ('UPLOAD_FAILED' if request.operation in ('add', 'update') else 'BLOCKED')
# Bad input is never described as a native upload attempt.
if root is None and isinstance(error, ValueError):
status = 'BLOCKED'
return finish(status, str(error), not mutated and owner_safe and not isinstance(error, LibraryBusy))
if stage == 'release_check' and not mutated:
status = 'BLOCKED'
return finish(status, str(error), owner_safe)
finally:
if mutex is not None:
if acquired:
+57 -41
View File
@@ -236,12 +236,13 @@ def pcb_processes():
def is_reusable_circuit_pcb(exe, command, expected_pcb):
"""Match the ReusableCircuit session for this CBB by argument, not install layout."""
from .failed_session import _command_args
exe = os.path.normcase(str(exe or ''))
command = os.path.normcase(str(command or ''))
args = _command_args(str(command or ''))
expected = os.path.normcase(os.path.abspath(str(expected_pcb)))
return (os.path.basename(exe) == 'expeditionpcb.exe'
and '-reusablecircuit' in command
and expected in command)
and '-reusablecircuit' in {arg.casefold() for arg in args[1:]}
and expected in {os.path.normcase(os.path.abspath(arg)) for arg in args[1:]})
def select_reusable_circuit_pcb(candidates):
@@ -636,7 +637,7 @@ def execute_job(job, emit):
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'):
for name in (() if job.get('diagnostic_only') else ('ForwardAnnotation.txt', 'MsgBoxResponse.log', 'PartPkg.log')):
path = integration_log_dir / name
try:
baseline = path.read_bytes()
@@ -663,6 +664,8 @@ def execute_job(job, emit):
# 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)
if job.get('diagnostic_only'):
return startup
integration = pcb_log_reports_integration_error(job['pcb'], offsets=integration_offsets)
if 'ICDB_REPAIR_REQUIRED' in startup:
return startup
@@ -692,6 +695,8 @@ def execute_job(job, emit):
reference = match[2]
if not reference or (config.parent/reference).resolve() != library:
raise ValueError(f'DIAGNOSTIC_LIBRARY_MISMATCH: {config}: {reference}')
from .diagnostic_open import snapshot_diagnostic_logs
job['diagnostic_log_baseline'] = snapshot_diagnostic_logs(job)
else:
ensure_pcb_central_library(
job['pcb'],
@@ -708,6 +713,7 @@ def execute_job(job, emit):
# The successful background runs use the normal ReusableCircuit
# launcher. Keep the experimental Library Manager menu route opt-in
# for diagnostics only; it is not the production trigger on EE7.9.
pcb_started_ns = time.time_ns()
if bool(job.get('native_lm_verify', False)):
from .library_manager_verify import launch_library_verify
emit(stage='lm_launch', library=job['lmc_file'], block=job['block_name'])
@@ -749,46 +755,51 @@ def execute_job(job, emit):
parent_pid=worker_parent_pid or lm_pid, command=worker_command,
binding='commandline' if is_reusable_circuit_pcb(
worker_executable, worker_command, job['pcb']) else 'launcher_child')
try:
from .mentor_paths import discover_sdd_home
home = discover_sdd_home(job.get('mentor_bin'))
wdir = os.environ.get('WDIR', '').split(';')[0].strip()
if wdir or home:
log_root = Path(os.path.expandvars(wdir)) if wdir else home/'standard'
(root/'diagnostics/native-client-context.json').write_text(json.dumps({
'native_pid': worker_pid, 'started_ns': pcb_started_ns,
'client_log_root': str(log_root/'iCDB/Client/Logs'),
'database': str(Path(job['project']).parent/'database')},
ensure_ascii=False), encoding='utf-8')
except (OSError, ValueError):
pass
break
time.sleep(.2)
if worker_pid is None:
raise RuntimeError('ReusableCircuit common 启动后未观察到 wg PCB 子进程')
app, doc, pi = wait_for_target_document(
job,
lambda: win32com.client.GetActiveObject('MGCPCB.Application'),
process_alive=lambda: any(candidate_pid in {lm_pid, worker_pid, worker_parent_pid}
for candidate_pid, _, _ in pcb_processes()),
emit=emit,
error_reader=native_error,
)
identity_verified = True
if job.get('diagnostic_only'):
# The diagnostic opens only to establish current native evidence.
# Do not refresh integration, save/commit Cert, or edit PRJ paths.
if detail := native_error():
raise RuntimeError(detail)
pi = doc = app = None
gc.collect()
from .failed_session import close_failed_document
diagnostic_close_attempted = True
closed = close_failed_document(job, emit=emit)
if closed.get('status') != 'CLOSED':
raise RuntimeError(f'DIAGNOSTIC_CLOSE_UNCONFIRMED: {closed}')
release_deadline = time.monotonic() + 30
while True:
released = cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid,
expected_pcb=job['pcb'], baseline_pids=pcb_before, emit=emit)
if (released and released.get('safe_to_continue')
and not unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])):
break
if time.monotonic() >= release_deadline:
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: 原生会话或数据库仍占用')
time.sleep(.2)
if detail := native_error():
# Global PCB COM can wait on another design's modal window.
# Opening checks use only the newly bound process and fresh logs.
from .diagnostic_open import check_target_window
def diagnostic_emit(**event):
nonlocal diagnostic_close_attempted
if event.get('stage') == 'diagnostic_wm_close':
diagnostic_close_attempted = True
emit(**event)
checked = check_target_window(job, worker_pid, emit=diagnostic_emit,
error_reader=native_error, launcher_process=launcher_process)
released = cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid,
expected_pcb=job['pcb'], baseline_pids=pcb_before, emit=emit)
if (checked.get('status') != 'PASS' or not checked.get('native_processes_exited')
or not released or not released.get('safe_to_continue')):
raise RuntimeError(f'DIAGNOSTIC_CLOSE_UNCONFIRMED: {checked}; {released}')
from .diagnostic_open import _read_bounded
if detail := _read_bounded(native_error):
raise RuntimeError(detail)
completed = True
emit(stage='diagnostic_complete', status='PASS')
return {'status': 'PASS', 'stdout': 'ICDB 启动检查通过;未执行集成或保存,已关闭工程。'}
return {'status': 'PASS', 'stdout': 'ICDB 启动检查通过;工程及启动器已退出。'}
from .pcb_bridge import ProcessWatch
with ProcessWatch((lm_pid, worker_pid, worker_parent_pid)) as watch:
app, doc, pi = wait_for_target_document(
job, lambda: win32com.client.GetActiveObject('MGCPCB.Application'),
process_alive=watch.alive, emit=emit, 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
@@ -877,8 +888,8 @@ def execute_job(job, emit):
if helper is not None and helper.poll() is None:
emit(stage='save_helper_retained', helper_pid=helper.pid)
elif diagnostic_close_attempted:
# A timed-out close helper may still own an in-flight native Close.
# Observe release below, but never start a second close helper.
# The process-bound checker already sent its sole WM_CLOSE.
# Preserve an unknown prompt; never fall back to global COM Close.
pass
elif not completed and identity_verified:
try:
@@ -994,11 +1005,16 @@ def worker_main(request_path):
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job.get('cancel_signal'), stop_clients=True)
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
from .defaults import WORKER_START_GATE_TIMEOUT_SECONDS
gate_deadline = time.monotonic() + WORKER_START_GATE_TIMEOUT_SECONDS
while not start_gate.is_file():
if job.get('cancel_signal') and Path(job['cancel_signal']).exists():
raise RuntimeError('VERIFY_CANCELLED: 启动已取消,未启动 PCB')
if time.monotonic() >= gate_deadline:
raise RuntimeError('DIALOG_AGENT_START_TIMEOUT: 弹窗代理未就绪,未启动 PCB')
time.sleep(.05)
if job.get('cancel_signal') and Path(job['cancel_signal']).exists():
raise RuntimeError('VERIFY_CANCELLED: 启动已取消,未启动 PCB')
outcome = execute_job(job, emit)
except Exception as error:
traceback.print_exc()
@@ -1444,6 +1460,8 @@ def read_library_flag(library, name, *, timeout_seconds=90, log_path=None):
import win32con
import win32process
import win32service
from pywinauto import Desktop
from pywinauto.controls.common_controls import TreeViewWrapper
executable = library_manager_path()
if not executable.is_file():
@@ -1452,8 +1470,6 @@ def read_library_flag(library, name, *, timeout_seconds=90, log_path=None):
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
handle = None
pid = None
@@ -265,14 +265,8 @@ def validate_export_output(kind, code, report, log):
def _process_snapshot():
import win32com.client
service = win32com.client.GetObject('winmgmts:')
try:
return [{'pid': int(p.ProcessId), 'parent': int(p.ParentProcessId or 0),
'name': str(p.Name), 'executable': str(p.ExecutablePath or '')}
for p in service.ExecQuery('SELECT ProcessId, ParentProcessId, Name, ExecutablePath FROM Win32_Process')]
finally:
service = None
from .owner_probe import query_process_snapshot
return query_process_snapshot(timeout_seconds=10)
def _database_released(project_root):
@@ -295,9 +289,20 @@ def _run_exports(project, root, mentor_home, timeout_seconds):
"""Observe exact exporter/descendant handles; never kill a database owner."""
import win32api, win32con, win32event, pywintypes
root = Path(root)
_write_json(root/'export-launch.json', {'started': False})
before = _process_snapshot()
if any(p['name'].casefold() in _OWNER_NAMES for p in before):
try:
_write_json(root/'export-launch.json', {'started': False})
except OSError as error:
return {'success': False, 'safe_to_continue': True, 'native_started': False,
'detail': str(error)}
diagnostic_warnings = []
try:
before = _process_snapshot()
except Exception as error:
return {'success': False, 'safe_to_continue': True, 'native_started': False,
'detail': str(error)}
from .backup_recovery import _native_scope_match
if any(p['name'].casefold() in _OWNER_NAMES and _native_scope_match(
p.get('command', ''), roots=(project.parent,)) is True for p in before):
return {'success': False, 'safe_to_continue': False, 'detail': 'An existing iCDB owner prevents isolated identity export.'}
if not _database_released(project.parent):
return {'success': False, 'safe_to_continue': False, 'detail': 'Analysis copy has unreleased native addresses or DB handles.'}
@@ -318,14 +323,20 @@ def _run_exports(project, root, mentor_home, timeout_seconds):
for kind, command, report, log in commands:
process = None
handles = {}
identities = {}
released = False
record = {'command': command, 'kind': kind, 'success': False, 'safe_to_continue': False}
launch_attempted = False
record = {'command': command, 'kind': kind, 'success': False,
'safe_to_continue': False, 'native_started': False}
try:
deadline = time.monotonic()+float(timeout_seconds)
with (root/(kind+'-stdout.txt')).open('wb') as output:
_write_json(root/'export-launch.json', {'started': True})
launch_attempted = True
record['native_started'] = None
process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT,
creationflags=subprocess.CREATE_NO_WINDOW)
record['native_started'] = True
record['pid'] = process.pid
descendants = {process.pid}
_write_json(root/(kind+'-process.json'), record)
@@ -336,13 +347,19 @@ def _run_exports(project, root, mentor_home, timeout_seconds):
previous = len(descendants)
descendants.update(p['pid'] for p in snapshot if p['parent'] in descendants)
changed = len(descendants) != previous
owners = [p for p in snapshot if p['pid'] in descendants or p['name'].casefold() in _OWNER_NAMES]
owners = [p for p in snapshot if p['pid'] in descendants or (
p['name'].casefold() in _OWNER_NAMES and _native_scope_match(
p.get('command', ''), roots=(project.parent,)) is True)]
for owner in owners:
if owner['pid'] not in handles:
try: handles[owner['pid']] = win32api.OpenProcess(win32con.SYNCHRONIZE, False, owner['pid'])
except pywintypes.error as error:
if error.winerror != 87: raise
record['observed_pids'] = sorted(descendants | handles.keys())
identities.update({owner['pid']:
{key: owner.get(key, '') for key in ('pid', 'created', 'executable')}
for owner in owners})
record['process_identities'] = list(identities.values())
if (process.poll() is not None and not owners
and all(win32event.WaitForSingleObject(h, 0) == win32con.WAIT_OBJECT_0 for h in handles.values())):
released = _database_released(project.parent)
@@ -355,15 +372,37 @@ def _run_exports(project, root, mentor_home, timeout_seconds):
validate_export_output(kind, process.returncode, report, log)
record.update(success=True, detail='Native export complete and owners released.')
except Exception as error:
creation_rejected = (isinstance(error, (FileNotFoundError, PermissionError))
or getattr(error, 'winerror', None) in (2, 3, 5, 193, 206, 740))
if process is None and (not launch_attempted or creation_rejected):
# Only a known CreateProcess rejection proves that no native owner started.
released = True
record['native_started'] = False
_write_json(root/'export-launch.json', {'started': False})
record.update(detail=str(error), safe_to_continue=released)
return record
finally:
if process is not None and not released:
_retained_exports.append((process, handles))
import json
owner_path = root/'export-owner.json'
try:
owner = json.loads(owner_path.read_text('utf-8')) if owner_path.is_file() else {}
except (ValueError, OSError):
owner = {} # Preserve unknown ownership; never lose the live handles.
_retained_exports.append((process, handles, {
'library': owner.get('library'), 'project': str(project), 'analysis_root': str(root)}))
else:
for handle in handles.values(): handle.Close()
_write_json(root/(kind+'-process.json'), record)
return {'success': True, 'safe_to_continue': True, 'detail': 'All three native exports completed and released.'}
try:
_write_json(root/(kind+'-process.json'), record)
except OSError as error:
if not released:
raise
warning = 'Export diagnostic write failed: '+str(error)
diagnostic_warnings.append(warning)
record['detail'] = (record.get('detail', '')+'\n'+warning).strip()
return {'success': True, 'safe_to_continue': True,
'detail': '\n'.join(['All three native exports completed and released.', *diagnostic_warnings])}
def capture_identity(source, evidence_root, *, expected_library, owner_library, mentor_home,
@@ -374,18 +413,22 @@ def capture_identity(source, evidence_root, *, expected_library, owner_library,
from .scoped_manifest import copy_verified
from .native_export_guard import begin_export_analysis, finish_export_analysis
import win32event, win32con
source, pcb, top = source_project(source)
root = Path(evidence_root).absolute()
library = Path(owner_library)
lease = LibraryLock(library)
if owned_snapshot(lease.path) is None:
raise ValueError('Native identity capture requires a current-thread transaction library lease.')
if (root.resolve().is_relative_to(source.parent) or source.parent.is_relative_to(root.resolve())
or root.resolve().is_relative_to(library.parent.resolve())):
raise ValueError('Native identity evidence must be independent of source and library.')
check_exporters(mentor_home)
root.mkdir(parents=True, exist_ok=False)
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
root = Path(evidence_root)
try:
root = root.absolute()
source, pcb, top = source_project(source)
library = Path(owner_library)
lease = LibraryLock(library)
if owned_snapshot(lease.path) is None:
raise ValueError('Native identity capture requires a current-thread transaction library lease.')
if (root.resolve().is_relative_to(source.parent) or source.parent.is_relative_to(root.resolve())
or root.resolve().is_relative_to(library.parent.resolve())):
raise ValueError('Native identity evidence must be independent of source and library.')
check_exporters(mentor_home)
root.mkdir(parents=True, exist_ok=False)
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
except Exception as error:
return IdentityOutcome(None, True, str(error), root)
acquired = False
owner_safe = True
outcome = IdentityOutcome(None, True, 'Identity capture did not start.', root)
@@ -403,10 +446,11 @@ def capture_identity(source, evidence_root, *, expected_library, owner_library,
export_token = begin_export_analysis(library, root, clone)
owner_safe = False
native = _run_exports(clone, root, mentor_home, timeout_seconds)
_write_json(root/'native-outcome.json', native)
if native.get('safe_to_continue') is True:
finish_export_analysis(library, export_token)
finish_export_analysis(library, export_token,
native_not_started=native.get('native_started') is False)
owner_safe = True
_write_json(root/'native-outcome.json', native)
if not owner_safe or not native.get('success'):
outcome = IdentityOutcome(None, owner_safe, native.get('detail', 'Native export failed.'), root)
else:
@@ -423,6 +467,10 @@ def capture_identity(source, evidence_root, *, expected_library, owner_library,
finally:
if acquired: win32event.ReleaseMutex(mutex)
mutex.Close()
_write_json(root/'identity.json', {'identity': outcome.identity, 'safe_to_continue': outcome.safe_to_continue,
'detail': outcome.detail, 'policy': POLICY})
try:
_write_json(root/'identity.json', {'identity': outcome.identity, 'safe_to_continue': outcome.safe_to_continue,
'detail': outcome.detail, 'policy': POLICY})
except OSError as error:
outcome = IdentityOutcome(outcome.identity, outcome.safe_to_continue,
outcome.detail+'\nIdentity summary write failed: '+str(error), root)
return outcome
+53 -12
View File
@@ -7,11 +7,16 @@ from uuid import uuid4
from .management import _write_json
def assert_retained_exports_idle():
def assert_retained_exports_idle(library=None):
import win32con, win32event
from .native_engineering_identity import _retained_exports
from .library_lock import LibraryBusy
for process, handles in _retained_exports:
for entry in _retained_exports:
process, handles = entry[:2]
scope = entry[2] if len(entry) > 2 else {}
if (library is not None and scope.get('library')
and Path(scope['library']).resolve() != Path(library).resolve()):
continue
if process.poll() is None or any(win32event.WaitForSingleObject(handle, 0) != win32con.WAIT_OBJECT_0
for handle in handles.values()):
raise LibraryBusy('Native identity exporter or descendant has not released.')
@@ -40,26 +45,33 @@ def begin_export_analysis(library, root, project):
@contextmanager
def prove_export_copies_released(library):
def prove_export_copies_released(library, *, allow_unknown_checkpoint=False, unstarted_token=''):
"""Fresh process and exclusive-copy proof, including after interpreter exit."""
import win32file, win32con, pywintypes
from .library_lock import blocker_path, checkpoint_record, _supervisor_alive, LibraryBusy
from .native_engineering_identity import _process_snapshot, _OWNER_NAMES
from .scoped_manifest import scan_tree
assert_retained_exports_idle()
library = Path(library).resolve()
assert_retained_exports_idle(library)
marker = blocker_path(library)
checkpoint = checkpoint_record(marker)
if marker.exists() and checkpoint is None:
raise LibraryBusy('Invalid library checkpoint cannot release export ownership.')
if not allow_unknown_checkpoint:
raise LibraryBusy('Invalid library checkpoint cannot release export ownership.')
# Only the explicit release path supplies full-library/native proof and
# archives the unchanged metadata while its coordination handle is held.
assert_retained_exports_idle()
if any(p['name'].casefold() in _OWNER_NAMES for p in _process_snapshot()):
raise LibraryBusy('Unknown export checkpoint still has a possible native identity owner.')
yield []
return
if checkpoint is None or 'export_analysis' not in checkpoint:
yield []
return
records = checkpoint['export_analysis']
if not isinstance(records, list) or not records:
raise LibraryBusy('Invalid export-analysis ownership record.')
owners = [p for p in _process_snapshot() if p['name'].casefold() in _OWNER_NAMES]
if owners: raise LibraryBusy(f'Native identity owners remain active: {owners}')
snapshot = None
copies = []
with ExitStack() as stack:
for record in records:
@@ -78,15 +90,43 @@ def prove_export_copies_released(library):
processes = [root/(kind+'-process.json') for kind in ('ascii', 'csv', 'edif')
if (root/(kind+'-process.json')).exists()]
if not processes:
launch = json.loads((root/'export-launch.json').read_text(encoding='utf-8'))
if launch != {'started': False}:
launch_path = root/'export-launch.json'
launch = json.loads(launch_path.read_text('utf-8')) if launch_path.exists() else None
unstarted_current_lease = False
if unstarted_token and record['token'] == unstarted_token:
# In-memory no-start proof may clear its own guard even
# when creating the first launch record failed.
from .management_transaction import current_lease
current = current_lease(library)
unstarted_current_lease = (record['lease_id'] == current['lease_id']
and record['supervisor_pid'] == current['supervisor_pid'])
if launch != {'started': False} and not (launch is None and unstarted_current_lease):
raise ValueError('Export launch has no confirmed process record.')
else:
if snapshot is None:
snapshot = _process_snapshot()
from .backup_recovery import _native_scope_match
if any(p['name'].casefold() in _OWNER_NAMES and _native_scope_match(
p.get('command', ''), roots=(project.parent,)) is True for p in snapshot):
raise LibraryBusy('A native identity owner is bound to this analysis copy.')
for path in processes:
process = json.loads(path.read_text(encoding='utf-8'))
if ('pid' not in process and process.get('safe_to_continue') is True
and process.get('native_started') is False
and json.loads((root/'export-launch.json').read_text('utf-8')) == {'started': False}):
continue # Failed CreateProcess: no native ownership was acquired.
pids = process.get('observed_pids', []) + [process['pid']]
if any(not isinstance(pid, int) or pid <= 0 for pid in pids):
raise ValueError('Invalid export process identity.')
if any(_supervisor_alive(pid) for pid in pids):
identities = {p['pid']: p for p in process.get('process_identities', [])}
def original_alive(pid):
recorded = identities.get(pid, {})
current = next((p for p in snapshot if p['pid'] == pid), None)
if recorded.get('created') and current and current.get('created'):
return (recorded['created'] == current['created']
and recorded.get('executable') == current.get('executable'))
return any(p['pid'] == pid for p in snapshot) or _supervisor_alive(pid)
if any(original_alive(pid) for pid in pids):
raise LibraryBusy('A recorded export process or descendant remains alive.')
entries = scan_tree(root/'project')
if any(path.name.casefold() == 'saddress.adr' for path in entries):
@@ -105,9 +145,10 @@ def prove_export_copies_released(library):
yield copies
def finish_export_analysis(library, token):
def finish_export_analysis(library, token, *, native_not_started=False):
from .library_lock import _guard, blocker_path, checkpoint_record, LibraryBusy
with _guard, prove_export_copies_released(library):
with _guard, prove_export_copies_released(library,
unstarted_token=token if native_not_started else ''):
marker = blocker_path(library)
checkpoint = checkpoint_record(marker)
records = checkpoint.get('export_analysis', []) if checkpoint else []
+135 -52
View File
@@ -21,6 +21,12 @@ from .scoped_manifest import hash_file
from .network_path import absolute_preserving_drive
_retained_workers = []
class NativeOperationError(RuntimeError):
def __init__(self, detail, *, safe_to_continue):
super().__init__(detail)
self.safe_to_continue = safe_to_continue
# Coordinator-owned Create/Add/conflict/update/remove/backup-reimport evidence:
# diagnostics/issue2-native-crud-20261103/engine-e2e/cycle-result.json
_PRODUCTION_ADD_VALIDATED = True
@@ -62,6 +68,28 @@ class NativeManagementAdapter:
self.timeout_seconds = timeout_seconds
self.mapped_drive = ''
self.mapped_remote_root = ''
self._pending_workers = {}
def release_confirmed(self, library):
"""Reap only this adapter's timed-out worker after its own release result."""
key = str(Path(library).resolve()).casefold()
pending = self._pending_workers.get(key)
if pending is None:
return False
worker, evidence = pending
if worker.poll() is None:
return False
try:
result = json.loads((evidence/'result.json').read_text(encoding='utf-8'))
if result.get('safe_to_continue') is not True:
return False
except (OSError, ValueError, TypeError, AttributeError):
return False
worker.close()
if worker in _retained_workers:
_retained_workers.remove(worker)
del self._pending_workers[key]
return True
def bind_library_mapping(self, drive, remote_root):
self.mapped_drive = drive or ''
@@ -105,14 +133,18 @@ class NativeManagementAdapter:
}
def _worker(self, job, evidence):
evidence = Path(evidence)
evidence.mkdir(parents=True, exist_ok=False)
desktop = 'CbbManagement_' + uuid4().hex[:12]
timeout = float(job.get('timeout_seconds', self.timeout_seconds))
job = {**job, 'desktop': desktop, 'mentor_home': str(self.home or ''),
'mapped_drive': job.get('mapped_drive', self.mapped_drive),
'mapped_remote_root': job.get('mapped_remote_root', self.mapped_remote_root),
'timeout_seconds': timeout, 'supervisor_pid': os.getpid()}
try:
evidence = Path(evidence)
evidence.mkdir(parents=True, exist_ok=False)
desktop = 'CbbManagement_' + uuid4().hex[:12]
timeout = float(job.get('timeout_seconds', self.timeout_seconds))
job = {**job, 'desktop': desktop, 'mentor_home': str(self.home or ''),
'mapped_drive': job.get('mapped_drive', self.mapped_drive),
'mapped_remote_root': job.get('mapped_remote_root', self.mapped_remote_root),
'timeout_seconds': timeout, 'supervisor_pid': os.getpid()}
except Exception as error:
error.safe_to_continue = True
raise
if job['operation'] == 'add':
import win32con, win32event
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
@@ -158,6 +190,23 @@ class NativeManagementAdapter:
return self._launch_worker(job, evidence)
def _launch_worker(self, job, evidence):
state = {'attempted': False, 'worker': None, 'released': False}
try:
return self._launch_worker_observed(job, evidence, state)
except Exception as error:
proof = getattr(error, 'worker_launch_release', None)
no_launch = (isinstance(proof, dict) and proof.get('safe_to_continue') is True
and proof.get('process_created') is False and proof.get('worker_exited') is True
and proof.get('remaining_pids') == [])
error.safe_to_continue = (not state['attempted'] or state['released']
or (state['worker'] is None and no_launch))
if state['worker'] is not None and not error.safe_to_continue:
if state['worker'] not in _retained_workers:
_retained_workers.append(state['worker'])
self._pending_workers[str(Path(job['library']).resolve()).casefold()] = (state['worker'], evidence)
raise
def _launch_worker_observed(self, job, evidence, state):
from .desktop_worker import DesktopWorker
request_path = evidence/'request.json'
desktop, timeout = job['desktop'], job['timeout_seconds']
@@ -168,13 +217,17 @@ class NativeManagementAdapter:
_write_json(request_path, job)
command = ([sys.executable] if getattr(sys, 'frozen', False)
else [sys.executable, '-m', 'cbb_refresh_verify'])
state['attempted'] = True
worker = (self.worker_factory or DesktopWorker)(command + ['--management-worker', str(request_path)], desktop)
state['worker'] = worker
worker.cbb_library = str(Path(job['library']).resolve())
while worker.poll() is None and time.monotonic() < deadline:
time.sleep(.1)
if worker.poll() is None:
# DesktopWorker.close() requests a kill without a cancellation file.
# Keep this exact worker alive until its native call returns normally.
_retained_workers.append(worker)
self._pending_workers[str(Path(job['library']).resolve()).casefold()] = (worker, evidence)
if add_job:
(evidence/'cancel-native-add.signal').touch()
return {'success': False, 'safe_to_continue': False,
@@ -183,6 +236,7 @@ class NativeManagementAdapter:
result = json.loads((evidence/'result.json').read_text(encoding='utf-8'))
if not isinstance(result.get('safe_to_continue'), bool):
raise ValueError('原生释放记录缺失')
state['released'] = result['safe_to_continue']
return result
except (OSError, ValueError) as error:
return {'success': False, 'safe_to_continue': False, 'detail': str(error)}
@@ -192,7 +246,8 @@ class NativeManagementAdapter:
def catalog(self, library, evidence):
result = self._worker({'operation': 'catalog', 'library': str(absolute_preserving_drive(library))}, evidence)
if not result.get('success') or not result.get('safe_to_continue'):
raise RuntimeError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'))
raise NativeOperationError(result.get('detail', '原生目录复读未确认,禁止文件夹回退。'),
safe_to_continue=result.get('safe_to_continue') is True)
return Catalog(Path(result['library']), int(result['flow']),
tuple(CatalogEntry(**entry) for entry in result['entries']), result['safe_to_continue'])
@@ -228,48 +283,65 @@ class NativeManagementAdapter:
from .management_transaction import current_lease, prepare_transaction
from .management import source_project
from .backup_recovery import _align_library
if not self.capabilities()['add'].supported:
raise ValueError(self.capabilities()['add'].reason)
try:
if not self.capabilities()['add'].supported:
raise ValueError(self.capabilities()['add'].reason)
validate_native_copy_paths(request.source, request.library, request.name)
except ValueError as error:
current_lease(request.library)
original_assets = asset_identity(original_source, project_library(original_source))
staged, pcb, _ = source_project(request.source, request.top_level_block)
if staged.parent != Path(root).resolve()/'source':
raise ValueError('Only the engine staged source can be aligned for native Add.')
_align_library(staged, pcb, request.library)
if asset_identity(staged, request.library) != original_assets:
raise ValueError('Staged engineering assets differ from the original source.')
except Exception as error:
return request, IdentityOutcome(None, True, str(error), Path(root))
current_lease(request.library)
original_assets = asset_identity(original_source, project_library(original_source))
staged, pcb, _ = source_project(request.source, request.top_level_block)
if staged.parent != Path(root).resolve()/'source':
raise ValueError('Only the engine staged source can be aligned for native Add.')
_align_library(staged, pcb, request.library)
if asset_identity(staged, request.library) != original_assets:
raise ValueError('Staged engineering assets differ from the original source.')
proof = capture_identity(staged, Path(root)/'source-identity', expected_library=request.library,
owner_library=request.library, mentor_home=self.home, timeout_seconds=request.timeout_seconds)
if proof.identity is None or not proof.safe_to_continue:
return request, proof
token = prepare_transaction(request, root, proof.identity, mentor_home=self.home,
fixture=getattr(self, '_fixture_authority', None))
try:
token = prepare_transaction(request, root, proof.identity, mentor_home=self.home,
fixture=getattr(self, '_fixture_authority', None))
except Exception as error:
return request, IdentityOutcome(None, proof.safe_to_continue, str(error), Path(root))
return replace(request, transaction_token=token), proof
def _add_transaction(self, request, evidence):
from contextlib import contextmanager
from .management_transaction import validate_transaction
from .library_lock import LibraryLock
from .native_engineering_identity import capture_identity, project_library
from .management import source_project
from .backup_recovery import _align_library, _idle_check
job = {'operation': 'add', 'authority_kind': 'management', 'transaction_nonce': request.transaction_token,
'library': str(absolute_preserving_drive(request.library)), 'source': str(request.source),
'name': request.name, 'partition': request.partition, 'top_level_block': request.top_level_block,
'source_identity': engineering_identity(request.source), 'supervisor_pid': os.getpid(),
'mentor_home': str(self.home), 'timeout_seconds': request.timeout_seconds,
'mapped_drive': request.mapped_drive, 'mapped_remote_root': request.mapped_remote_root}
prepared = validate_transaction(job, evidence.parent)
owner_safe = False
with LibraryLock(request.library, safe_to_release=lambda: owner_safe):
try:
job = {'operation': 'add', 'authority_kind': 'management', 'transaction_nonce': request.transaction_token,
'library': str(absolute_preserving_drive(request.library)), 'source': str(request.source),
'name': request.name, 'partition': request.partition, 'top_level_block': request.top_level_block,
'source_identity': engineering_identity(request.source), 'supervisor_pid': os.getpid(),
'mentor_home': str(self.home), 'timeout_seconds': request.timeout_seconds,
'mapped_drive': request.mapped_drive, 'mapped_remote_root': request.mapped_remote_root}
prepared = validate_transaction(job, evidence.parent)
except Exception as error:
error.safe_to_continue = True
raise
owner_safe = True
@contextmanager
def preserve_release():
nonlocal owner_safe
try:
yield
except Exception as error:
owner_safe = getattr(error, 'safe_to_continue', owner_safe) is True
error.safe_to_continue = owner_safe
raise
with LibraryLock(request.library, safe_to_release=lambda: owner_safe), preserve_release():
owner_safe = False
result = self._worker(job, evidence)
owner_safe = result.get('safe_to_continue') is True
if not owner_safe or not result.get('success'):
return NativeOutcome(False, owner_safe, result.get('detail', 'Native Add failed.'), evidence_path=evidence)
owner_safe = False
# The native editor may leave PCB/pcb.prj relative to the staged
# location. Correct only documented library-reference tokens after
# its owner has exited, then reopen the native catalog independently.
@@ -277,12 +349,10 @@ class NativeManagementAdapter:
work = Path(request.library).parent/'ReusableBlocksLP'/request.name/'Work'
projects = list(work.glob('*.prj'))
if len(projects) != 1:
owner_safe = False
return NativeOutcome(True, False, 'Registered engineering project is not unique.',
return NativeOutcome(True, owner_safe, 'Registered engineering project is not unique.',
identity_matches=False, evidence_path=evidence)
target, pcb, _ = source_project(projects[0], request.top_level_block)
_idle_check(request.library, target, pcb)
owner_safe = False
from .maintenance_entry import maintenance_project_files
project_files = maintenance_project_files(target, pcb)
before_refs = {str(p): hash_file(p) for p in project_files}
@@ -291,13 +361,13 @@ class NativeManagementAdapter:
raise ValueError('Registered project references do not resolve to the exact target library.')
_write_json(evidence/'reference-alignment.json', {'before': before_refs,
'after': {str(p): hash_file(p) for p in project_files}, 'library': job['library']})
owner_safe = False
catalog = self.catalog(request.library, evidence/'reopen-aligned')
owner_safe = catalog.safe_to_continue
entries = [entry for entry in catalog.entries if entry.name == request.name and entry.partition == request.partition]
if (not owner_safe or Path(catalog.library).resolve() != Path(request.library).resolve()
or catalog.flow != 1 or len(entries) != 1):
owner_safe = False
return NativeOutcome(True, False, 'Aligned target did not reopen with the exact catalog identity.',
return NativeOutcome(True, owner_safe, 'Aligned target did not reopen with the exact catalog identity.',
identity_matches=False, evidence_path=evidence)
owner_safe = False
proof = capture_identity(target, evidence/'registered-identity', expected_library=request.library,
@@ -906,17 +976,21 @@ def _catalog_native(job, root):
def _catalog_com(job, root):
import pythoncom, win32com.client, win32con, win32gui, win32process
from .mentor_adapter import _process_details
if job['operation'] == 'add':
if job.get('authority_kind') == 'management':
from .management_transaction import validate_transaction
validate_transaction(job, root.parent)
else:
validate_owned_fixture_add(job, root)
desktop, name = _current_desktop()
if name != job['desktop'] or not name.startswith('CbbManagement_'):
raise RuntimeError('拒绝在非本次私有桌面读取原生目录。')
if _process_details('LibraryManager.exe'):
raise RuntimeError('已有 Library Manager 运行,请关闭后重试。')
try:
if job['operation'] == 'add':
if job.get('authority_kind') == 'management':
from .management_transaction import validate_transaction
validate_transaction(job, root.parent)
else:
validate_owned_fixture_add(job, root)
desktop, name = _current_desktop()
if name != job['desktop'] or not name.startswith('CbbManagement_'):
raise RuntimeError('拒绝在非本次私有桌面读取原生目录。')
if _process_details('LibraryManager.exe'):
raise RuntimeError('已有 Library Manager 运行,请关闭后重试。')
except Exception as error:
error.safe_to_continue = True
raise
stop = threading.Event()
refused = []
observed = []
@@ -1059,7 +1133,7 @@ def management_worker_main(request_path):
request_path = Path(request_path)
root = request_path.parent
job = json.loads(request_path.read_text(encoding='utf-8'))
result = {'success': False, 'safe_to_continue': False}
result = {'success': False, 'safe_to_continue': True}
try:
_, desktop = _current_desktop()
if desktop != job['desktop'] or not desktop.startswith('CbbManagement_'):
@@ -1077,6 +1151,7 @@ def management_worker_main(request_path):
raise ValueError('Native Add requires Logical-Physical.')
if engineering_identity(job['source']) != job.get('source_identity'):
raise ValueError('Staged source engineering identity changed before native Add.')
result['safe_to_continue'] = False
result = _catalog_native(job, root)
elif job['operation'] == 'remove':
adapter = NativeManagementAdapter(mentor_home=job['mentor_home'])
@@ -1090,8 +1165,13 @@ def management_worker_main(request_path):
command = [str(adapter.home/'lm/win32/bin/CleanWGLib.exe'), str(job['library']), 'F4', str(input_path)]
_write_json(root/'command.json', command)
with (root/'native-output.txt').open('wb') as output:
process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT,
creationflags=subprocess.CREATE_NO_WINDOW)
result['safe_to_continue'] = False
try:
process = subprocess.Popen(command, stdout=output, stderr=subprocess.STDOUT,
creationflags=subprocess.CREATE_NO_WINDOW)
except (FileNotFoundError, PermissionError):
result['safe_to_continue'] = True
raise
_write_json(root/'native-process.json', {'pid': process.pid, 'command': command})
# Never terminate this native database owner on timeout.
try:
@@ -1099,12 +1179,15 @@ def management_worker_main(request_path):
except subprocess.TimeoutExpired:
result.update(detail=f'TIMEOUT: CleanWGLib PID {process.pid} 尚未释放')
else:
result.update(safe_to_continue=True, exit_code=code)
text = (root/'native-output.txt').read_bytes().decode('mbcs', errors='replace')
result.update(success=removal_output_ok(code, text, job['name'], job['partition']),
safe_to_continue=True, detail=text, exit_code=code)
detail=text)
else:
raise ValueError('未验证的原生管理操作')
except Exception as error:
if getattr(error, 'safe_to_continue', None) is True:
result['safe_to_continue'] = True
result['detail'] = str(error)
result['exception'] = _exception_details(error)
finally:
+101 -16
View File
@@ -9,6 +9,20 @@ import ntpath
import os
from pathlib import Path
from typing import Protocol
from functools import wraps
from threading import RLock
from weakref import WeakValueDictionary
_retained_path_leases = {}
_live_path_leases = WeakValueDictionary()
_path_lease_guard = RLock()
def _lease_locked(method):
@wraps(method)
def locked(*args, **kwargs):
with _path_lease_guard: return method(*args, **kwargs)
return locked
class NetworkPathError(RuntimeError):
@@ -45,23 +59,35 @@ class WindowsWNetBackend:
def __init__(self) -> None:
self._mpr = ctypes.WinDLL("mpr", use_last_error=True)
self._kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
self._unavailable_drives = set()
def mapping(self, drive: str) -> str | None:
size = wintypes.DWORD(32768)
buffer = ctypes.create_unicode_buffer(size.value)
error = self._mpr.WNetGetConnectionW(drive, buffer, ctypes.byref(size))
if error == 0:
return buffer.value
if error == self.ERROR_NOT_CONNECTED:
return None
raise OSError(error, f"WNetGetConnectionW({drive})")
def mappings(self) -> dict[str, str]:
result: dict[str, str] = {}
self._unavailable_drives = set()
for code in range(ord("D"), ord("Z") + 1):
drive = f"{chr(code)}:"
size = wintypes.DWORD(32768)
buffer = ctypes.create_unicode_buffer(size.value)
error = self._mpr.WNetGetConnectionW(drive, buffer, ctypes.byref(size))
if error == 0:
result[drive] = buffer.value
elif error != self.ERROR_NOT_CONNECTED:
raise OSError(error, f"WNetGetConnectionW({drive})")
try:
remote = self.mapping(drive)
except OSError:
self._unavailable_drives.add(drive)
continue
if remote:
result[drive] = remote
return result
def used_drives(self) -> set[str]:
mask = int(self._kernel32.GetLogicalDrives())
return {
return getattr(self, '_unavailable_drives', set()) | {
f"{chr(ord('A') + index)}:"
for index in range(26)
if mask & (1 << index)
@@ -88,6 +114,14 @@ def _same_remote(left: str, right: str) -> bool:
return ntpath.normcase(ntpath.normpath(left)) == ntpath.normcase(ntpath.normpath(right))
def _mapping(backend, drive):
direct = getattr(backend, 'mapping', None)
if callable(direct):
return direct(drive)
return next((value for key, value in backend.mappings().items()
if str(key).upper().rstrip('\\/') == drive.upper()), None)
def ensure_mapped_drive_visible(drive: str, remote_root: str, *,
backend: WNetBackend | None = None) -> dict[str, object]:
"""Ensure a worker process can see the mapped drive used by Mentor.
@@ -108,13 +142,11 @@ def ensure_mapped_drive_visible(drive: str, remote_root: str, *,
normalized_drive = raw_drive.upper()
backend = backend or WindowsWNetBackend()
try:
mappings = backend.mappings()
current = _mapping(backend, normalized_drive)
except OSError as error:
raise NetworkPathError(
f"NETWORK_UNAVAILABLE: 无法读取网络盘映射 {normalized_drive}: {error}"
) from error
current = next((value for key, value in mappings.items()
if str(key).upper().rstrip('\\/') == normalized_drive), None)
if current:
if _same_remote(current, raw_remote):
return {'drive': normalized_drive, 'remote_root': current, 'created': False}
@@ -180,7 +212,11 @@ class LibraryPathLease:
_closed: bool = False
_retained_for_recovery: bool = False
def __post_init__(self):
with _path_lease_guard: _live_path_leases[id(self)] = self
@classmethod
@_lease_locked
def acquire(cls, path, *, backend: WNetBackend | None = None) -> "LibraryPathLease":
raw = _normalize_input_path(path)
if not raw.startswith("\\\\"):
@@ -189,8 +225,7 @@ class LibraryPathLease:
if drive:
try:
backend = backend or WindowsWNetBackend()
remote = next((value for key, value in backend.mappings().items()
if str(key).upper().rstrip('\\/') == drive.upper()), '')
remote = _mapping(backend, drive.upper()) or ''
except (OSError, AttributeError):
remote = ''
return cls(local, local, drive.upper(), remote, False, backend)
@@ -225,10 +260,31 @@ class LibraryPathLease:
f"NETWORK_UNAVAILABLE: 无法挂载中心库 {remote_root}: {error}"
) from error
@_lease_locked
def retain_for_recovery(self) -> None:
"""Keep the Mentor drive available to a retained native session."""
self._retained_for_recovery = True
_retained_path_leases[id(self)] = self
@_lease_locked
def release_after_recovery(self, proof) -> bool:
"""Restore normal mapping lifetime after this library's release proof."""
if not isinstance(proof, dict) or proof.get('status') != 'RELEASE_CONFIRMED':
return False
candidates = [str(path) for path in (self.display_path, self.mentor_path)
if str(path).casefold().endswith('.lmc')]
drive, tail = ntpath.splitdrive(str(self.mentor_path))
if (self.remote_root and drive.upper() == self.drive.upper()
and str(self.mentor_path).casefold().endswith('.lmc')):
candidates.append(self.remote_root + tail)
library = str(proof.get('library') or '')
if not any(_same_remote(library, candidate) for candidate in candidates):
return False
self._retained_for_recovery = False
_retained_path_leases.pop(id(self), None)
return True
@_lease_locked
def transfer_ownership(self, other) -> None:
"""Keep a shared mapping alive for the currently loaded library."""
if (isinstance(other, LibraryPathLease) and not other._closed
@@ -237,16 +293,28 @@ class LibraryPathLease:
other.created, other._backend = True, self._backend
self.created = False
@_lease_locked
def close(self) -> None:
if self._closed:
return
self._closed = True
if self._retained_for_recovery or not self.created or self._backend is None or not self.drive:
if self._retained_for_recovery:
return
if not self.created or self._backend is None or not self.drive:
self._closed = True
return
for live in tuple(_live_path_leases.values()):
same_backend = (live._backend is self._backend or
isinstance(self._backend, WindowsWNetBackend) and isinstance(live._backend, WindowsWNetBackend))
if live is not self and not live._closed and same_backend:
self.transfer_ownership(live)
if not self.created:
self._closed = True
return
try:
current = self._backend.mappings().get(self.drive)
current = _mapping(self._backend, self.drive)
if current and _same_remote(current, self.remote_root):
self._backend.cancel_mapping(self.drive)
self._closed = True
except OSError as error:
raise NetworkPathError(
f"NETWORK_UNAVAILABLE: 释放临时盘符 {self.drive} 失败: {error}"
@@ -257,3 +325,20 @@ class LibraryPathLease:
def __exit__(self, exc_type, exc_value, traceback) -> None:
self.close()
def release_retained_path_leases(proof, *, current_lease=None) -> int:
"""Recover matching leases even after the UI dropped its old reference."""
released = 0
for lease in tuple(_retained_path_leases.values()):
if not lease.release_after_recovery(proof):
continue
if lease is not current_lease:
lease.transfer_ownership(current_lease)
try:
lease.close()
except NetworkPathError:
_retained_path_leases[id(lease)] = lease
raise
released += 1
return released
+44
View File
@@ -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()
+37 -10
View File
@@ -11,9 +11,20 @@ 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({'names': names}), encoding='utf-8')
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:
@@ -27,13 +38,20 @@ def query_owners(names, *, timeout_seconds=10):
try:
record = json.loads(job.with_name('result.json').read_text('utf-8'))
rows = record['owners']
if result.returncode != 0 or record['names'] != list(names) or not isinstance(rows, list):
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 or row['name'].casefold() not in {n.casefold() for n in names}
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
@@ -43,19 +61,28 @@ def owner_probe_main(job):
import pythoncom
import win32com.client
job = Path(job)
names = json.loads(job.read_text('utf-8'))['names']
if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names):
return 1
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 FROM Win32_Process WHERE ' + ' OR '.join(
f"Name='{name}'" for name in names)
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)} for item in service.ExecQuery(query)]
'parent': int(item.ParentProcessId or 0),
**({'created': str(item.CreationDate or '')} if full_snapshot else {})}
for item in service.ExecQuery(query)]
service = None
job.with_name('result.json').write_text(json.dumps({'names': names, 'owners': rows}), encoding='utf-8')
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()
+29
View File
@@ -91,6 +91,35 @@ def require_document_identity(document, job):
return integration
class ProcessWatch:
"""Hold this task's native handles; repeated COM attach polls never run WMI."""
def __init__(self, pids):
self.pids = set(int(pid) for pid in pids if pid)
self.handles = None
def __enter__(self):
return self
def alive(self):
import pywintypes, win32api, win32con, win32event
if self.handles is None:
self.handles = []
try:
for pid in self.pids:
try:
self.handles.append(win32api.OpenProcess(win32con.SYNCHRONIZE, False, pid))
except pywintypes.error as error:
if error.winerror != 87: raise
except Exception:
self.__exit__(None, None, None)
raise
return any(win32event.WaitForSingleObject(h, 0) != win32con.WAIT_OBJECT_0 for h in self.handles)
def __exit__(self, *_):
for handle in self.handles or (): handle.Close()
self.handles = []
def wait_for_target_document(job, app_provider, *, process_alive, emit=lambda **event: None,
error_reader=None, sleep=time.sleep):
"""Wait for the exact launched document; the parent process owns timeout."""
+379
View File
@@ -0,0 +1,379 @@
"""Preview and cooperatively close the exact native sessions a user confirms."""
from contextlib import contextmanager
import json
import os
from pathlib import Path
import re
import time
from uuid import uuid4
from .owner_probe import query_process_snapshot
from .native_engineering_identity import _OWNER_NAMES
_NAMES = {'expeditionpcb.exe', 'librarymanager.exe', 'icdbprojectbackup.exe',
'viewdraw.exe', 'icdbnetserver.exe', 'icdbserver.exe',
'reuseblockimporter.exe', 'cleanwglib.exe', 'cscript.exe', 'wscript.exe'} | _OWNER_NAMES
_WORKER_NAMES = {'python.exe', 'pythonw.exe', 'cbbrefreshverify.exe'}
_WORKER_FLAGS = ('--verify-worker', '--repair-worker', '--management-worker', '--native-backup-worker')
@contextmanager
def _process_handle(pid, *, terminate=False):
import win32api, win32con
handle = win32api.OpenProcess(win32con.SYNCHRONIZE | 0x1000 | (1 if terminate else 0), False, int(pid))
try:
yield handle
finally:
handle.Close()
def _handle_identity(handle):
import ctypes
from ctypes import wintypes
import win32process
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
kernel.QueryFullProcessImageNameW.argtypes = [wintypes.HANDLE, wintypes.DWORD,
wintypes.LPWSTR, ctypes.POINTER(wintypes.DWORD)]
kernel.QueryFullProcessImageNameW.restype = wintypes.BOOL
buffer = ctypes.create_unicode_buffer(32768)
length = wintypes.DWORD(len(buffer))
if not kernel.QueryFullProcessImageNameW(int(handle), 0, buffer, ctypes.byref(length)):
raise ctypes.WinError(ctypes.get_last_error())
return {'created_handle': win32process.GetProcessTimes(handle)['CreationTime'].isoformat(),
'image': buffer.value}
def _process_identity(row):
with _process_handle(row['pid']) as handle:
return _handle_identity(handle)
def _process_windows(pids):
if not pids: return {}
import win32api, win32con, win32gui, win32process, win32service
from .native_repair_ui import _native_output
result = {pid: [] for pid in pids}
observed = set()
def capture(hwnd, _=None):
pid = win32process.GetWindowThreadProcessId(hwnd)[1]
if pid not in result or hwnd in observed or not win32gui.IsWindowVisible(hwnd):
return
observed.add(hwnd)
children, documents = [], []
def child(handle, _):
kind = win32gui.GetClassName(handle)
if kind == 'MDIClient' and win32gui.GetWindow(handle, win32con.GW_CHILD):
documents.append(handle)
if win32gui.IsWindowVisible(handle):
children.append({'hwnd': int(handle), 'class': kind,
'id': win32gui.GetDlgCtrlID(handle) & 0xffff,
'text': win32gui.GetWindowText(handle), 'enabled': bool(win32gui.IsWindowEnabled(handle))})
win32api.SetLastError(0)
win32gui.EnumChildWindows(hwnd, child, None)
window = {'hwnd': int(hwnd), 'title': win32gui.GetWindowText(hwnd),
'class': win32gui.GetClassName(hwnd), 'enabled': bool(win32gui.IsWindowEnabled(hwnd)),
'children': children, 'documents': documents, 'rect': list(win32gui.GetWindowRect(hwnd))}
if any(c['class'].casefold().startswith('richedit') for c in children):
window['output'] = _native_output([window])
result[pid].append(window)
win32api.SetLastError(0)
def incomplete():
for pid in result:
result[pid].append({'hwnd': 0, 'title': '窗口状态读取不完整', 'class': 'UNCONFIRMED',
'enabled': False, 'children': []})
try:
win32gui.EnumWindows(capture, None)
except Exception:
incomplete()
try:
desktops = win32service.GetProcessWindowStation().EnumDesktops()
except Exception:
incomplete()
return result
for name in desktops:
if name != 'Default' and not name.startswith(('CbbVerify_', 'CbbRepair_', 'CbbManagement_', 'CbbStatusRead_', 'CbbFlagRead_')):
continue
desktop = None
try:
desktop = win32service.OpenDesktop(name, 0, False, win32con.DESKTOP_READOBJECTS)
for hwnd in desktop.EnumDesktopWindows(): capture(int(hwnd))
except Exception:
incomplete()
finally:
if desktop is not None: desktop.CloseDesktop()
return result
def _project_name(row, library):
from .failed_session import _command_args
for arg in _command_args(row['command'])[1:]:
try:
relative = Path(arg.split('=', 1)[-1]).relative_to(library.parent/'ReusableBlocksLP')
if len(relative.parts) >= 2 and relative.parts[1].casefold() == 'work':
return relative.parts[0]
except ValueError:
continue
return '当前库关联会话'
def close_target(name, windows):
"""No save/restore approval and no close against an unknown modal/window."""
main = [w for w in windows if w['class'].startswith('Afx:') and (
name.casefold() == 'icdbprojectbackup.exe' and w['title'].startswith('iCDB Project Backup')
or name.casefold() == 'librarymanager.exe' and w['title'].startswith('Library Manager')
or name.casefold() == 'expeditionpcb.exe' and 'Expedition PCB' in w['title'])]
if len(main) != 1:
return None, '未确认空闲主窗口;有工程时请先在原生窗口保存并关闭'
window = main[0]
for other in windows:
if other is window: continue
benign = (not other['title'] and other['class'] in
('UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator'))
benign = benign or (other['class'] == 'Internet Explorer_Hidden' and not other['title']
and not other.get('children') and other.get('rect', [1, 1, 2, 2])[0:2] == other.get('rect', [1, 1, 2, 2])[2:4])
if not benign: return None, '存在原生弹窗,请先处理该窗口后重新检查'
if not window['enabled']:
return None, '原生程序正在工作,请稍后重新检查'
if any(c['class'].casefold() == 'msctls_progress32' for c in window['children']):
return None, '原生操作尚未结束'
if name.casefold() == 'icdbprojectbackup.exe':
output = window.get('output')
if not isinstance(output, str): return None, '原生输出尚无法确认,请重新检查'
active = list(re.finditer(r'(?i)repairing|restoring|backing up|creating (?:a )?backup', output))
last_load = output.rfind('Backup information loaded correctly.')
failure = output.rfind('Repair project error [')
if active and max(last_load, failure) < active[-1].start():
return None, '备份、恢复或修复需要在原生窗口确认结束后再释放'
return window, ('将正常关闭工程;未保存时等待确认' if window.get('documents') else '可正常关闭')
def _save_question(windows):
for window in windows:
text = '\n'.join(c['text'] for c in window['children'] if c['class'] == 'Static')
buttons = {c['id']: c for c in window['children'] if c['class'] == 'Button' and c['enabled']}
if (window['class'] == '#32770' and 'Expedition' in window['title']
and re.search(r'\bsave\b', text, re.I) and re.search(r'changes|modified', text, re.I)
and set(buttons) == {6, 7, 2}):
return {'hwnd': window['hwnd'], 'text': text, 'buttons': buttons}
return None
def inspect_residual_sessions(library, *, block_names=()):
from .library_lock import _owner_matches_scope, blocker_path, checkpoint_record, has_retained_library
library = Path(library).resolve()
if library.suffix.casefold() != '.lmc' or not library.is_file():
raise ValueError('请选择有效的中心库 .lmc 文件')
blocks = tuple(dict.fromkeys(str(name) for name in block_names))
if any(not name or name in ('.', '..') or any(c in name for c in '\\/:*?"<>|') for name in blocks):
raise ValueError('残留检查的工程名称无效')
roots = tuple(library.parent/'ReusableBlocksLP'/b/'Work' for b in blocks) + (library.parent/'SymbolLibs',)
if not blocks: roots = (library.parent,)
candidates = []
for row in query_process_snapshot():
if row['pid'] == os.getpid(): continue
native = row['name'].casefold() in _NAMES
worker = (row['name'].casefold() in _WORKER_NAMES
and any(flag in row['command'].casefold() for flag in _WORKER_FLAGS))
if not (native or worker): continue
scope = _owner_matches_scope(row, library, roots)
if scope is not False:
candidates.append({**row, 'scope_confirmed': scope is True})
windows = _process_windows({r['pid'] for r in candidates})
sessions = []
for row in candidates:
item = dict(row)
try:
item.update(_process_identity(row))
target, detail = close_target(row['name'], windows.get(row['pid'], [])) if row['scope_confirmed'] else (
None, '归属未确认,请在原生窗口确认工程并关闭后重新检查')
item.update(can_close=target is not None, detail=detail)
except Exception as error:
item.update(can_close=False, detail=f'进程已变化或暂时无法确认:{error}')
item['project'] = _project_name(row, library) if row['scope_confirmed'] else '归属未确认'
item['prompt'] = _save_question(windows.get(row['pid'], [])) if row['scope_confirmed'] else None
sessions.append(item)
previous = []
marker = blocker_path(library)
for path in (marker, marker.with_suffix('.pending')):
if path.exists():
record = checkpoint_record(path)
previous.append({'path': str(path), 'recognized': record is not None,
'status': record.get('status', 'UNRECOGNIZED') if record else 'UNRECOGNIZED',
'time': record.get('time', '') if record else '',
'pid': record.get('pid') if record else None})
if has_retained_library(library):
previous.append({'path': str(library), 'status': 'RETAINED_IN_MEMORY', 'time': '',
'pid': os.getpid(), 'kind': 'in_memory'})
return {'library': str(library), 'block_names': list(blocks), 'sessions': sessions,
'previous_records': previous}
def _request_close(row, *, stop=None):
import win32con, win32event, win32gui, win32process
with _process_handle(row['pid']) as handle:
if win32event.WaitForSingleObject(handle, 0) == win32con.WAIT_OBJECT_0:
return {'release_status': 'RELEASED', 'detail': '已退出'}
identity = _handle_identity(handle)
if identity != {key: row.get(key) for key in ('created_handle', 'image')}:
return {'release_status': 'IDENTITY_CHANGED', 'detail': '进程身份已变化,未关闭'}
windows = _process_windows({row['pid']}).get(row['pid'], [])
if row['name'].casefold() == 'expeditionpcb.exe':
from .failed_session import _startup_snapshot, _cancel_target
for window in windows:
if window['class'] != '#32770': continue
target = _cancel_target(_startup_snapshot(window['hwnd'], row['pid']))
if target:
win32gui.PostMessage(target[1].handle, win32con.BM_CLICK, 0, 0)
deadline = time.monotonic()+3
while time.monotonic() < deadline:
if win32event.WaitForSingleObject(handle, 100) == win32con.WAIT_OBJECT_0:
return {'release_status': 'RELEASED', 'detail': '已取消启动并退出'}
if stop is not None and stop.is_set():
return {'release_status': 'CANCELLED', 'detail': '已停止释放'}
windows = _process_windows({row['pid']}).get(row['pid'], [])
if close_target(row['name'], windows)[0] is not None: break
break
target, detail = close_target(row['name'], windows)
if target is None: return {'release_status': 'WAITING', 'detail': detail}
if stop is not None and stop.is_set():
return {'release_status': 'CANCELLED', 'detail': '已停止释放'}
if win32process.GetWindowThreadProcessId(target['hwnd'])[1] != row['pid']:
return {'release_status': 'IDENTITY_CHANGED', 'detail': '窗口归属已变化,未关闭'}
win32gui.PostMessage(target['hwnd'], win32con.WM_CLOSE, 0, 0)
deadline = time.monotonic()+3
while time.monotonic() < deadline:
if win32event.WaitForSingleObject(handle, 100) == win32con.WAIT_OBJECT_0:
return {'release_status': 'RELEASED', 'detail': '已正常退出'}
if stop is not None and stop.is_set(): break
return {'release_status': 'WAITING', 'detail': '已请求正常关闭;请处理原生窗口,完成后重新检查'}
def _force_close(row, *, stop=None):
"""Separate UI consent permits ending only this exact, scoped process handle."""
import win32api, win32con, win32event
if stop is not None and stop.is_set():
return {'release_status': 'CANCELLED', 'detail': '已停止释放'}
with _process_handle(row['pid'], terminate=True) as handle:
if win32event.WaitForSingleObject(handle, 0) == win32con.WAIT_OBJECT_0:
return {'release_status': 'RELEASED', 'detail': '已退出'}
if _handle_identity(handle) != {k: row.get(k) for k in ('created_handle', 'image')}:
return {'release_status': 'IDENTITY_CHANGED', 'detail': '进程身份已变化,未结束'}
win32api.TerminateProcess(handle, 1)
exited = win32event.WaitForSingleObject(handle, 3000) == win32con.WAIT_OBJECT_0
return {'release_status': 'RELEASED' if exited else 'WAITING',
'detail': '已强制结束,工程需重新检查' if exited else '已请求结束,等待退出'}
def _finish_library_release(library, evidence):
from .library_lock import release_retained_library
return release_retained_library(library, evidence)
def _release_residual_sessions(plan, evidence_dir, *, stop=None, force=False):
library = Path(plan['library']).resolve()
root = Path(evidence_dir).resolve()/('sessions-'+uuid4().hex[:12])
if root.is_relative_to(library.parent) or library.parent.is_relative_to(root):
raise ValueError('释放记录需位于中心库之外')
root.mkdir(parents=True)
result = {'status': 'PARTIAL', 'library': str(library), 'sessions': [], 'force': force,
'evidence_path': str(root/'result.json'), 'previous_records': plan.get('previous_records', [])}
fresh = {r['pid']: r for r in query_process_snapshot()}
# Close native children first, so their supervisors can finish and drop leases.
ordered = sorted(plan['sessions'], key=lambda r: r['name'].casefold() in _WORKER_NAMES)
for original in ordered:
current = fresh.get(original['pid'])
outcome = {'release_status': 'RELEASED', 'detail': '已退出'}
if stop is not None and stop.is_set():
outcome = {'release_status': 'CANCELLED', 'detail': '已停止释放'}
elif current is not None:
from .library_lock import _owner_matches_scope
scoped = _owner_matches_scope(current, library, (library.parent,)) is True
if original.get('scope_confirmed') is not True or not scoped or original['pid'] == os.getpid():
outcome = {'release_status': 'WAITING', 'detail': '未在确认名单中证明属于当前库,未关闭;请重新检查'}
elif any(current.get(key) != original.get(key) for key in ('created', 'executable', 'command', 'name')):
outcome = {'release_status': 'IDENTITY_CHANGED', 'detail': '确认后进程身份已变化,未关闭'}
else:
try: outcome = (_force_close if force else _request_close)(original, stop=stop)
except Exception as error:
outcome = {'release_status': 'WAITING', 'detail': str(error)}
result['sessions'].append({**original, **outcome})
by_pid = {r['pid']: r for r in result['sessions']}
result['sessions'] = [by_pid[r['pid']] for r in plan['sessions']]
# Launchers may exit only after the GUI child processed its close request.
after = {r['pid']: r for r in query_process_snapshot()}
remaining_windows = _process_windows({r['pid'] for r in result['sessions']
if r['release_status'] == 'WAITING' and r['pid'] in after})
for row in result['sessions']:
if row['release_status'] == 'WAITING' and row['pid'] not in after:
row.update(release_status='RELEASED', detail='已退出')
elif row['release_status'] == 'WAITING':
row['prompt'] = _save_question(remaining_windows.get(row['pid'], []))
if all(row['release_status'] == 'RELEASED' for row in result['sessions']) and not (stop and stop.is_set()):
try:
result['proof'] = _finish_library_release(library, root/'library-proof')
proof = result['proof']
if (proof.get('status') != 'RELEASE_CONFIRMED'
or Path(proof.get('library', '')).resolve() != library):
raise ValueError('释放证明与当前库不一致')
result['status'] = 'RELEASE_CONFIRMED'
except Exception as error:
result['detail'] = str(error)
try:
(root/'result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), 'utf-8')
except OSError as error:
result['evidence_error'] = str(error)
return result
def release_residual_sessions(plan, evidence_dir, *, stop=None, force=False):
import win32con, win32event
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = False
try:
deadline = time.monotonic()+15
while time.monotonic() < deadline and not (stop and stop.is_set()):
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
if acquired: break
time.sleep(.2)
if not acquired:
return {'status': 'PARTIAL', 'library': plan['library'], 'sessions': [
{**r, 'release_status': 'WAITING', 'detail': '其它批次尚在执行;请停止该批次后重试'}
for r in plan['sessions']], 'detail': '未取得执行通道,未关闭任何进程。'}
return _release_residual_sessions(plan, evidence_dir, stop=stop, force=force)
finally:
if acquired: win32event.ReleaseMutex(mutex)
mutex.Close()
def answer_save_question(row, choice):
"""Only the exact displayed save question receives an explicit user answer."""
import win32con, win32event, win32gui
if choice not in (6, 7, 2): raise ValueError('无效的保存选择')
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
if not acquired:
mutex.Close()
raise ValueError('其它批次正在执行,请稍后处理保存提示')
try:
_answer_save_question(row, choice)
finally:
win32event.ReleaseMutex(mutex)
mutex.Close()
def _answer_save_question(row, choice):
import win32con, win32gui
fresh = next((r for r in query_process_snapshot() if r['pid'] == row['pid']), None)
if fresh is None or any(fresh.get(k) != row.get(k) for k in ('created', 'command', 'executable')):
raise ValueError('进程已变化,请重新检查')
with _process_handle(row['pid']) as handle:
if _handle_identity(handle) != {k: row.get(k) for k in ('created_handle', 'image')}:
raise ValueError('进程已变化,请重新检查')
prompt = _save_question(_process_windows({row['pid']}).get(row['pid'], []))
if not prompt or prompt != row.get('prompt'):
raise ValueError('原生提示已变化,请重新检查')
button = prompt['buttons'][choice]
import win32process
if win32process.GetWindowThreadProcessId(button['hwnd'])[1] != row['pid']:
raise ValueError('保存按钮归属已变化')
win32gui.PostMessage(button['hwnd'], win32con.BM_CLICK, 0, 0)
+5
View File
@@ -58,6 +58,11 @@ class RunTimings:
elif stage == 'identity_verified':
self.end('pcb_startup', now)
self.begin('integration', now)
elif stage == 'diagnostic_native_loaded_candidate':
self.end('pcb_startup', now)
self.begin('diagnostic_close', now)
elif stage == 'diagnostic_complete':
self.end('diagnostic_close', now)
elif stage == 'integration_action':
key = ACTION_KEYS.get(event.get('action'))
if key:
@@ -1,5 +1,6 @@
"""Small, asynchronous entry point for recovery from a healthy CBB project."""
from pathlib import Path
from threading import Event
from PySide6.QtCore import QObject, QThreadPool, Signal
from PySide6.QtWidgets import (QComboBox, QDialog, QFileDialog, QFormLayout,
@@ -23,6 +24,8 @@ class BackupRecoveryDialog(QDialog):
self.resize(620, 230)
self.request_factory = request_factory
self.recoverer = recoverer
self.native_queue = None
self.stop = Event()
self.busy = False
self.pool = QThreadPool(self)
self.pool.setMaxThreadCount(1)
@@ -38,6 +41,9 @@ class BackupRecoveryDialog(QDialog):
self.restore_button.clicked.connect(self._restore)
self.close_button = QPushButton('关闭')
self.close_button.clicked.connect(self.reject)
self.cancel_queue_button = QPushButton('取消排队')
self.cancel_queue_button.setEnabled(False)
self.cancel_queue_button.clicked.connect(self.stop.set)
self.status_label = QLabel(MANUAL_BACKUP_NOTICE + ' 所选备份先在隔离库中验证;安装时旧 Work 仅改名保留。')
self.status_label.setWordWrap(True)
layout = QFormLayout(self)
@@ -50,6 +56,7 @@ class BackupRecoveryDialog(QDialog):
buttons = QHBoxLayout()
buttons.addStretch()
buttons.addWidget(self.restore_button)
buttons.addWidget(self.cancel_queue_button)
buttons.addWidget(self.close_button)
layout.addRow(buttons)
@@ -84,11 +91,24 @@ class BackupRecoveryDialog(QDialog):
QMessageBox.Yes | QMessageBox.No, QMessageBox.No) != QMessageBox.Yes:
return
self.busy = True
self.stop.clear()
self._set_enabled(False)
self.status_label.setText('正在准备恢复…')
self.started.emit(name)
self.worker = ServiceWorker(lambda: recoverer(
request, source, progress=self.signals.progress.emit))
def execute():
action = lambda: recoverer(request, source, progress=self.signals.progress.emit)
if self.native_queue is None: return action()
from ..operation_queue import QueueCancelled
def restore():
self.signals.progress.emit({'stage': 'queue_acquired'})
return action()
try:
return self.native_queue.run(restore, stop=self.stop, waiting=lambda: self.signals.progress.emit(
{'stage': 'waiting_native_slot', 'detail': '已排队,等待当前检查结束'}))
except QueueCancelled as error:
from ..verify_runner import VerifyResult
return VerifyResult('CANCELLED', stderr=str(error))
self.worker = ServiceWorker(execute)
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)
@@ -101,6 +121,8 @@ class BackupRecoveryDialog(QDialog):
def _progress(self, event):
if isinstance(event, dict):
if event.get('stage') in ('waiting_native_slot', 'queue_acquired'):
self.cancel_queue_button.setEnabled(event['stage'] == 'waiting_native_slot')
message = event.get('message') or event.get('detail')
if message:
self.status_label.setText(str(message))
@@ -121,6 +143,7 @@ class BackupRecoveryDialog(QDialog):
def _finished(self):
self.busy = False
self.cancel_queue_button.setEnabled(False)
self._set_enabled(True)
def reject(self):
@@ -0,0 +1,115 @@
"""A check batch owns its snapshot and yields the native queue after each CBB."""
from threading import Event
from PySide6.QtCore import QObject, QThreadPool, Signal
from .workers import ServiceWorker
from ..operation_queue import QueueCancelled
from ..verify_runner import VerifyResult, run_verify, write_batch_report
from ..icdb_maintenance import write_maintenance_report
class IcdbCheckTask(QObject):
event = Signal(object)
item_done = Signal(str, object)
finished = Signal(object)
def __init__(self, requests, queue, report_root, *, verifier=None, path_lease=None, parent=None):
super().__init__(parent)
self.requests = tuple(requests)
self.queue, self.report_root = queue, report_root
self.path_lease = path_lease
self.verifier = verifier or (lambda req: run_verify(req, progress=self.event.emit, stop=self.stop))
self.results = []
self.stop = Event()
self.active = False
self.paused = False
self.invalidated = set()
self.ready_names = set()
self.running = False
self.report = None
self.report_error = ''
self.pool = QThreadPool(self); self.pool.setMaxThreadCount(1)
def start(self):
self.active = True
self._next()
def cancel(self):
self.stop.set(); self.queue.wake()
if not self.running and self.active: self._next()
def pause(self):
self.paused = True
def invalidate(self, names):
self.invalidated.update(names)
def update_statuses(self, statuses):
self.ready_names = (self.ready_names - statuses.keys()) | {
name for name, status in statuses.items() if status == 'READY'}
def resume(self):
self.paused = False; self.queue.wake()
if not self.running and self.active: self._next()
def _next(self):
if self.running or not self.active: return
if self.stop.is_set() or self.results and not self.results[-1].safe_to_continue:
self.results.extend(VerifyResult('SKIPPED', stderr='队列已停止,未操作此 CBB')
for _ in self.requests[len(self.results):])
if len(self.results) == len(self.requests):
self._report(); return
if self.paused: return
request = self.requests[len(self.results)]
def check():
try:
def execute():
if request.block_name in self.ready_names:
return VerifyResult('SKIPPED', stderr='工程已为 READY,跳过 ICDB 检查。')
if request.block_name in self.invalidated:
return VerifyResult('SKIPPED', stderr='目标已有后续管理操作,请重新检查。')
self.event.emit({'stage': 'icdb_check_start', 'block': request.block_name})
return self.verifier(request)
return self.queue.run(execute, stop=self.stop, ready=lambda: not self.paused,
waiting=lambda: self.event.emit({'stage': 'waiting_native_slot', 'block': request.block_name,
'detail': '已排队,等待当前原生操作结束'}))
except QueueCancelled as error:
return VerifyResult('CANCELLED', stderr=str(error))
except Exception as error:
return VerifyResult('FAIL', stderr=str(error), safe_to_continue=False,
error_category='ICDB_INSPECTION_UNCONFIRMED')
self.running = True
self.worker = ServiceWorker(check)
self.worker.signals.succeeded.connect(self._record)
self.worker.signals.finished.connect(self._item_finished)
self.pool.start(self.worker)
def _record(self, result):
name = self.requests[len(self.results)].block_name
self.results.append(result)
if result.status in ('CANCELLED', 'QUEUE_STOP'): self.stop.set()
self.item_done.emit(name, result)
def _item_finished(self):
self.running = False
self._next()
def _report(self):
self.running = True
def write():
try:
report = write_batch_report(self.requests, self.results, self.report_root)
write_maintenance_report(self.requests, self.results, report, ())
return report, ''
except Exception as error:
return None, str(error)
self.worker = ServiceWorker(write)
self.worker.signals.succeeded.connect(self._report_ready)
self.worker.signals.finished.connect(self._complete)
self.pool.start(self.worker)
def _report_ready(self, payload):
self.report, self.report_error = payload
def _complete(self):
self.running = self.active = False
self.finished.emit(self)
+476 -83
View File
@@ -1,6 +1,7 @@
from pathlib import Path
from threading import Event
import os
import ntpath
import time
from collections import Counter
from dataclasses import replace
@@ -20,6 +21,26 @@ def _path_key(value):
if not raw:
return ''
return os.path.normcase(os.path.normpath(os.path.abspath(raw)))
def _library_path_keys(value, lease=None):
"""Use known mapping aliases without resolving unavailable network paths."""
if not value:
return set()
keys = {_path_key(value)}
mentor = getattr(lease, 'mentor_path', None)
display = getattr(lease, 'display_path', None)
if mentor and display:
for source, alias in ((mentor, display), (display, mentor)):
if _path_key(value) == _path_key(source):
keys.add(_path_key(alias))
elif _path_key(Path(value).parent) == _path_key(source):
keys.add(_path_key(Path(alias)/Path(value).name))
drive, tail = ntpath.splitdrive(str(value))
if (getattr(lease, 'remote_root', '') and drive
and drive.upper() == getattr(lease, 'drive', '').upper()):
keys.add(_path_key(lease.remote_root + tail))
return keys
try:
from dx_productivity.trial_task_center.ui.workers import ServiceWorker
except ImportError:
@@ -75,6 +96,7 @@ class MainWindow(QWidget):
self.management_capabilities = self.management_adapter.capabilities()
self._management_unsafe = False
self._management_unsafe_library = None
self._management_unsafe_libraries = set()
self._management_operation_library = None
self._maintenance_recovery_view = False
self._reload_after_maintenance = False
@@ -105,6 +127,16 @@ class MainWindow(QWidget):
p, names, mapping_store=self.data_root/'image-status-map.json'),
)
self.pool = QThreadPool(self); self.pool.setMaxThreadCount(1)
self._lease_pending = 0
self._lease_workers = set()
self._mapping_busy = 0
self._mapping_auto_idle = False
self._mapping_finish_waiters = []
self._closing = self._close_ready = False
from cbb_refresh_verify.operation_queue import NativeQueue
self.native_queue = NativeQueue()
self.icdb_task = None
self.icdb_tasks = []
self.busy = False; self.loaded_path = None; self.stop = Event(); self.ready_names = set()
self._row_by_name = {}
self._records_by_name = {}
@@ -236,6 +268,9 @@ class MainWindow(QWidget):
maintenance_layout.addWidget(self.native_backup_button)
maintenance_row = QHBoxLayout()
maintenance_row.addWidget(self.icdb_repair_button)
self.icdb_stop_button = QPushButton('停止检查')
self.icdb_stop_button.clicked.connect(self._stop_icdb_check)
maintenance_row.addWidget(self.icdb_stop_button)
maintenance_row.addWidget(self.backup_restore_button)
maintenance_row.addWidget(self.reimport_button)
maintenance_layout.addLayout(maintenance_row)
@@ -264,7 +299,7 @@ class MainWindow(QWidget):
management_layout.insertWidget(0, self.management_capability_label)
self.unfinished_label = QLabel(); self.unfinished_label.setWordWrap(True)
management_layout.addWidget(self.unfinished_label)
self.release_library_button = QPushButton('检查占用…')
self.release_library_button = QPushButton('释放残留会话…')
self.release_library_button.clicked.connect(self.check_library_release)
management_buttons.addWidget(self.release_library_button)
self.section_stack.addWidget(management)
@@ -293,20 +328,62 @@ class MainWindow(QWidget):
self._records_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()
if not self._icdb_active():
self.icdb_progress.hide(); self.icdb_phase.hide()
def _release_library_lease(self):
lease, self.library_lease = self.library_lease, None
if self._native_backup_session is not None and self._native_backup_session.path_lease is lease:
return # The visible native session still needs this mapping.
if self._icdb_active() and self.icdb_task.path_lease is lease:
return # This background task still uses its captured mapping.
if lease is None:
return
try:
lease.close()
self._close_path_lease(lease)
except Exception as error:
if self.logging_context:
self.logging_context.logger.warning('library_lease_release_failed error=%s', error)
def _close_path_lease(self, lease):
self._lease_action(lease.close, hold_busy=self.busy)
def _retain_path_lease(self, lease, *, hold_busy=False):
if lease is not None:
self._lease_action(lease.retain_for_recovery, hold_busy=hold_busy)
def _lease_action(self, action, completed=None, failed=None, *, hold_busy=False):
# The serial pool orders retain before every subsequent close.
self._lease_pending += 1
if hold_busy:
if not self._mapping_busy and not self.busy:
self._mapping_auto_idle = True
self._mapping_busy += 1
self.busy = True
self._update()
worker = ServiceWorker(action)
self._lease_workers.add(worker)
if completed is not None: worker.signals.succeeded.connect(completed)
worker.signals.failed.connect(failed or self._display_error)
def finished():
self._lease_workers.discard(worker)
self._lease_pending -= 1
if hold_busy: self._mapping_busy -= 1
if not self._mapping_busy:
callbacks, self._mapping_finish_waiters = self._mapping_finish_waiters, []
auto_idle, self._mapping_auto_idle = self._mapping_auto_idle, False
for callback in callbacks: callback()
if auto_idle and not callbacks: self._finished()
self._update()
worker.signals.finished.connect(finished)
self.pool.start(worker)
def _defer_mapping_finish(self, callback):
if not self._mapping_busy: return False
if callback not in self._mapping_finish_waiters:
self._mapping_finish_waiters.append(callback)
return True
def _update(self):
# The refresh flow needs only the library and a CBB; LibSpec/DMS config
# are optional and only enable the extra contents check.
@@ -318,7 +395,8 @@ class MainWindow(QWidget):
ready = ready and not self._native_backup_same_library()
self.batch_button.setEnabled(ready and bool(self._eligible_names()))
self.selected_batch_button.setEnabled(ready and bool(self._eligible_names(checked_only=True)))
self.icdb_repair_button.setEnabled(ready and bool(self._maintenance_names()))
self.icdb_repair_button.setEnabled(ready and not self._icdb_active() and bool(self._maintenance_names()))
self.icdb_stop_button.setEnabled(self._icdb_active() and not self.icdb_task.stop.is_set())
self.backup_restore_button.setEnabled(ready and bool(
self._eligible_names(checked_only=bool(self.selected_records()))))
self.release_library_button.setEnabled(not self.busy and bool(self.lmc_edit.text().strip()))
@@ -331,19 +409,18 @@ class MainWindow(QWidget):
reasons.append(reason)
self.management_capability_label.setText('\n'.join(dict.fromkeys(reasons)))
current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text()) if self.lmc_edit.text().strip() else None
notice = ('上次管理操作尚未确认安全释放,请检查该操作的结果。'
if (self._management_unsafe and self._management_unsafe_library is not None
and current_library == _path_key(self._management_unsafe_library)) else '')
notice = ('上次操作结果待复查,执行前将重新检查占用。'
if self._management_library_unsafe(current_library) else '')
self.library_notice.setText(notice)
self.library_notice.setVisible(bool(notice))
for widget in (self.lmc_edit, self.browse_button, self.load_button, self.cbb_table,
self.timeout_spin, self.advanced_toggle, self.advanced_panel,
self.search_edit, self.status_filter, self.select_visible_button, self.clear_selection_button):
for widget in (self.lmc_edit, self.browse_button, self.load_button,
self.timeout_spin, self.advanced_toggle, self.advanced_panel):
widget.setEnabled(not self.busy)
counts = self.visible_counts()
self.counts_label.setText(f'可见 {counts["visible"]} / {len(self._records_by_name)} · '
f'READY {counts["ready"]} · 异常/待确认 {counts["error"]}')
self.stop_button.setEnabled(self.busy and self.loaded_path is not None and not self.stop.is_set())
self.stop_button.setEnabled((self.busy and self.loaded_path is not None and not self.stop.is_set())
or self._icdb_active() and not self.icdb_task.stop.is_set())
self.stop_button.setText('取消当前任务并停止队列')
available = bool(self._reimport_offers) and not self.busy and self.management_capabilities['update'].supported
self.reimport_button.setVisible(bool(self._reimport_offers))
@@ -351,6 +428,13 @@ class MainWindow(QWidget):
self.reimport_button.setToolTip(self.management_capabilities['update'].reason)
if self._reimport_summary_action is not None:
self._reimport_summary_action.setEnabled(available)
if getattr(self, '_residual_release_action', None) is not None:
self._residual_release_action.setEnabled(not self.busy)
if getattr(self, '_resume_batch_action', None) is not None:
pending = getattr(self, '_resume_batch', None)
same = bool(pending and _library_path_keys(pending['library'], self.library_lease)
& _library_path_keys(self.mentor_lmc_path or self.loaded_path, self.library_lease))
self._resume_batch_action.setEnabled(not self.busy and same and pending['released'])
def _show_unfinished_management(self):
from cbb_refresh_verify.management import unfinished_operations
@@ -364,48 +448,79 @@ class MainWindow(QWidget):
def check_library_release(self):
if self.busy or not self.lmc_edit.text().strip():
return
if QMessageBox.question(self, '检查原生会话释放',
'检查所有原生编辑器/管理进程、库内数据库地址记录及数据库独占可读性。'
'\n全部通过后归档阻止记录并释放本应用保留的库锁。'
'\n数据恢复结果和未完成管理任务仍需单独审查。继续?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No) != QMessageBox.Yes:
return
from cbb_refresh_verify.library_lock import release_retained_library
from .residual_release_dialog import ResidualReleaseDialog
library = Path(self.mentor_lmc_path or self.lmc_edit.text().strip())
if library.is_dir():
library = select_library_file(library)
def completed(result):
current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text().strip())
if current_library == _path_key(library):
self._management_unsafe = False
self._management_unsafe_library = None
session = self._native_backup_session
if session is not None and session.library == library and session.process.poll() is not None:
self._release_native_backup_session(session)
message = f'原生会话释放检查通过。请审查未完成任务后重新读取中心库。\n{result["evidence_path"]}'
self.status_label.setText(message); self.output.append(message)
block_name = None
if len(self._batch_requests) == 1:
block_name = self._batch_requests[0].block_name
elif len(self.selected_records()) == 1:
block_name = self.selected_records()[0].name
import inspect
if 'block_name' in inspect.signature(release_retained_library).parameters:
action = lambda: release_retained_library(library, self.data_root/'release-checks',
block_name=block_name)
else:
action = lambda: release_retained_library(library, self.data_root/'release-checks')
self._start(action, completed)
self.release_dialog = ResidualReleaseDialog(library, self.data_root/'release-checks', self)
self.release_dialog.native_queue = self.native_queue
self.release_dialog.before_release.connect(self._stop_icdb_check)
self.release_dialog.released.connect(lambda result: self._complete_library_release(self.release_dialog.library, result))
self.release_dialog.finished.connect(self._release_dialog_finished)
self.busy = True; self._update()
self.release_dialog.show()
def _release_dialog_finished(self, _=None):
if self._defer_mapping_finish(self._release_dialog_finished): return
self._finished()
pending = getattr(self, '_resume_batch', None)
if pending and pending['released']:
self.summary.setText('会话已释放,可以继续未执行项。')
self.summary.show()
def _complete_library_release(self, library, result):
from cbb_refresh_verify.network_path import release_retained_path_leases
try:
if (not isinstance(result, dict) or result.get('status') != 'RELEASE_CONFIRMED'
or not (_library_path_keys(result.get('library'), self.library_lease)
& _library_path_keys(library, self.library_lease))):
raise ValueError('RELEASE_PROOF_UNCONFIRMED: 释放证明未确认或不属于当前中心库。')
except Exception as error:
self._failed(error)
return
lease = self.library_lease
def release():
try:
release_retained_path_leases(result, current_lease=lease)
except Exception:
if lease is not None: lease.retain_for_recovery()
raise
self._lease_action(release, lambda _: self._library_release_ready(library, result),
self._display_error, hold_busy=True)
def _library_release_ready(self, library, result):
current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text().strip())
if current_library == _path_key(library):
self._management_unsafe = False
self._management_unsafe_library = None
self._management_unsafe_libraries.discard(_path_key(library))
session = self._native_backup_session
if self._native_backup_matches_library(library) and session.process.poll() is not None:
self._release_native_backup_session(session)
pending = getattr(self, '_resume_batch', None)
if pending and (_library_path_keys(pending['library'], self.library_lease)
& _library_path_keys(library, self.library_lease)):
pending['released'] = True
message = f'相关会话已释放,可以重试未完成的项目。\n{result["evidence_path"]}'
self.status_label.setText(message); self.output.append(message)
def _resume_released_batch(self):
pending = getattr(self, '_resume_batch', None)
if self.busy or not pending or not pending['released']: return
if not (_library_path_keys(pending['library'], self.library_lease)
& _library_path_keys(self.mentor_lmc_path or self.loaded_path, self.library_lease)):
return
self._resume_batch = None
self._run(pending['names'], maintenance=pending['maintenance'])
def _management_library_unsafe(self, library):
if self._management_unsafe and self._management_unsafe_library is not None:
self._management_unsafe_libraries.add(_path_key(self._management_unsafe_library))
return _path_key(library) in self._management_unsafe_libraries
def _management_block_reason(self, operation):
if self.busy:
return '当前任务尚未结束,请等待后再执行库管理操作。'
if self._native_backup_same_library():
return '该库的版本与备份窗口仍在使用,关闭后可执行写入操作。'
current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text()) if self.lmc_edit.text().strip() else None
if (self._management_unsafe and self._management_unsafe_library is not None
and current_library == _path_key(self._management_unsafe_library)):
return '上次管理操作的会话尚未安全释放,请先查看异常证据并执行释放检查。'
capability = self.management_capabilities[operation]
if not capability.supported:
return capability.reason
@@ -441,8 +556,12 @@ class MainWindow(QWidget):
self.stop.clear()
self._management_is_recovery = bool(recovery_from)
self._management_operation_library = _path_key(requests[0].library) if requests else None
requests = [replace(request, check_previous_release=True) if self._management_library_unsafe(request.library)
else request for request in requests]
self.management_dialog = ManagementDialog(requests, self, adapter=self.management_adapter,
recovery_from=recovery_from)
self.management_dialog.native_queue = self.native_queue
self.management_dialog.before_execute.connect(lambda names: self._invalidate_check_targets(requests[0].library, names))
self._management_results = []
self.management_dialog.completed.connect(self._management_completed)
self.management_dialog.finished.connect(self._management_closed)
@@ -471,8 +590,41 @@ class MainWindow(QWidget):
def _management_completed(self, results):
self._management_results = results
if (getattr(self, '_management_is_recovery', False) and results
proofs = [result.release_proof for result in results if isinstance(getattr(result, 'release_proof', None), dict)
and result.release_proof.get('status') == 'RELEASE_CONFIRMED'
and bool(_library_path_keys(result.release_proof.get('library'), self.library_lease)
& _library_path_keys(self._management_operation_library, self.library_lease))
]
safe = all(result.safe_to_continue for result in results)
lease = self.library_lease
if proofs and safe:
from cbb_refresh_verify.network_path import release_retained_path_leases
def release():
try:
release_retained_path_leases(proofs[-1], current_lease=lease)
except Exception:
if lease is not None: lease.retain_for_recovery()
raise
def failed(error):
self._management_unsafe = True
self._management_unsafe_library = self._management_operation_library
self._management_unsafe_libraries.add(_path_key(self._management_operation_library))
self._display_error(error)
self._management_results_ready(results, proofs, False)
self._lease_action(release, lambda _: self._management_results_ready(results, proofs, True),
failed, hold_busy=True)
elif not safe and lease is not None:
self._lease_action(lease.retain_for_recovery,
lambda _: self._management_results_ready(results, proofs, True),
lambda error: (self._display_error(error), self._management_results_ready(results, proofs, False)),
hold_busy=True)
else:
self._management_results_ready(results, proofs, True)
def _management_results_ready(self, results, proofs, mapping_released):
if (mapping_released and (getattr(self, '_management_is_recovery', False) or proofs) and results
and all(result.safe_to_continue for result in results)):
self._management_unsafe_libraries.discard(_path_key(self._management_operation_library))
self._management_unsafe = False
self._management_unsafe_library = None
self._maintenance_recovery_view = False
@@ -482,20 +634,25 @@ class MainWindow(QWidget):
for result in results:
self.output.append(f'{result.name}:{RESULT_LABELS.get(result.status, result.status)} '
f'{result.detail}\n{result.evidence_path or ""}')
if self.logging_context:
self.logging_context.logger.info('management_result operation=%s block=%s status=%s safe=%s evidence=%s detail=%s',
result.operation, result.name, result.status, result.safe_to_continue, result.evidence_path, result.detail)
if getattr(self, '_management_is_recovery', False) and result.status == 'UPLOADED_VERIFIED':
self.output.append('副本恢复校验通过,版本待核对。')
if any(not result.safe_to_continue for result in results):
self._management_unsafe = True
self._management_unsafe_library = self._management_operation_library
if self.library_lease:
self.library_lease.retain_for_recovery()
self._management_unsafe_libraries.add(_path_key(self._management_operation_library))
self.status_label.setText('管理操作需要人工确认恢复/释放;已停止写入,未再次读取原生目录。')
self._show_unfinished_management()
def _management_closed(self, *_):
if self._defer_mapping_finish(self._management_closed): return
from PySide6.QtCore import QTimer
self.busy = False; self._update()
if self._management_results and not self._management_unsafe:
changed = any(result.status in {'REMOVED', 'UPLOADED', 'UPLOADED_VERIFIED', 'UPLOADED_VERIFY_FAILED', 'RECOVERY_REQUIRED'}
for result in self._management_results)
if changed and not self._management_library_unsafe(self.mentor_lmc_path or self.loaded_path):
# Whole catalog reload is necessary for inserted/deleted identities;
# existing local search/filter controls are intentionally retained.
QTimer.singleShot(0, self.load_library)
@@ -574,23 +731,177 @@ class MainWindow(QWidget):
if path: self.lmc_edit.setText(path); self.load_library()
def _start(self, action, callback):
self.stop.clear()
self.busy = True; self._update()
self.worker = ServiceWorker(action)
self.worker = ServiceWorker(self._queued_action(action))
self.worker.signals.succeeded.connect(callback)
self.worker.signals.failed.connect(self._failed)
self.worker.signals.finished.connect(self._finished)
self.pool.start(self.worker)
def _queued_action(self, action):
return lambda: self.native_queue.run(action, stop=self.stop,
waiting=lambda: self.signals.event.emit({'stage': 'waiting_native_slot',
'detail': '已排队,等待当前原生操作结束'}))
def _icdb_active(self):
return self.icdb_task is not None and self.icdb_task.active
def _check_matches_library(self, library):
return self._icdb_active() and bool(_library_path_keys(library, self.library_lease)
& _library_path_keys(self.icdb_task.requests[0].lmc_file, self.icdb_task.path_lease))
def _invalidate_check_targets(self, library, names):
names = set(names)
for task in self.icdb_tasks:
if not (_library_path_keys(library, self.library_lease)
& _library_path_keys(task.requests[0].lmc_file, task.path_lease)): continue
task.invalidate(names)
if hasattr(task, 'summary'):
prefix = '已更新的工程:'+', '.join(sorted(task.invalidated))+'。下列为此前检查记录。\n'
task.summary.setDetailedText(prefix+task.summary.detailedText())
if names & getattr(task, 'recovery_names', set()):
task.recovery_action.setEnabled(False)
task.recovery_action.setToolTip('工程已有后续管理操作,请重新检查。')
self._reimport_offers = tuple(offer for offer in self._reimport_offers if offer[0].block_name not in names
or not (_library_path_keys(library, self.library_lease) & _library_path_keys(offer[0].lmc_file)))
def _stop_icdb_check(self):
if self._icdb_active():
self.icdb_task.cancel()
self.icdb_phase.setText('正在停止检查,等待本项退出…')
self._update()
def _publish_check_statuses(self, library, statuses, lease=None):
for task in tuple(self.icdb_tasks):
if task.active and (_library_path_keys(library, lease or self.library_lease)
& _library_path_keys(task.requests[0].lmc_file, task.path_lease)):
task.update_statuses(statuses)
def _start_icdb_check(self, names):
if self.busy or self._icdb_active() or not self.loaded_path or self._native_backup_same_library(): return
eligible = set(self._maintenance_names())
names = [name for name in names if name in eligible]
if not names: return
from .icdb_check_task import IcdbCheckTask
requests = tuple(replace(self._request(name), maintenance=True, auto_repair=False) for name in names)
task = IcdbCheckTask(requests, self.native_queue, self.data_root/'reports',
verifier=self.verifier, path_lease=self.library_lease, parent=self)
task.records = {name: self._records_by_name[name] for name in names}
self.icdb_task = task
self.icdb_tasks.append(task)
task.event.connect(lambda event: self._icdb_event(task, event))
task.item_done.connect(lambda name, result: self._icdb_item_done(task, name, result))
task.finished.connect(self._icdb_done)
self.icdb_progress.setVisible(True); self.icdb_phase.setVisible(True)
self.icdb_progress.setRange(0, len(requests)); self.icdb_progress.setValue(0)
self.icdb_progress.setFormat('已处理 %v / %m 个 CBB')
self.icdb_phase.setText('准备后台检查…')
task.start(); self._update()
def _icdb_event(self, task, event):
name, stage = str(event.get('block', '')), str(event.get('stage', ''))
detail = str(event.get('detail') or stage)
self.icdb_phase.setText(f'{name} · '+({'icdb_check_start': '正在检查',
'icdb_startup_check': '正在打开工程', 'waiting_native_slot': '已排队,等待执行'}.get(stage, detail)))
if not self.busy: self.status_label.setText('后台 ICDB 检查:'+self.icdb_phase.text())
if self.logging_context:
self.logging_context.logger.info('icdb_background block=%s stage=%s detail=%s', name, stage, detail)
def _icdb_item_done(self, task, name, result):
self.icdb_progress.setValue(len(task.results))
if not (_library_path_keys(task.requests[0].lmc_file, task.path_lease)
& _library_path_keys(self.mentor_lmc_path or self.loaded_path, self.library_lease)):
return
row = self._row_by_name.get(name)
if row is None: return
if not result.safe_to_continue: self._defer_status_refresh([name])
if name in task.invalidated:
text = '内容已更新,待重新检查'
elif result.status == 'CHECKED': text = '未发现 ICDB 错误'
elif result.error_category == 'ICDB_REPAIR_REQUIRED': text = '发现 ICDB 错误'
elif result.status == 'CANCELLED': text = '检查已取消'
elif result.status == 'SKIPPED':
text = '已为 READY,跳过检查' if name in task.ready_names else '未检查'
elif result.error_category == 'DATABASE_SAFETY_STOP':
if 'read/write access permission' in result.stderr.casefold():
text = '未确认:需处理读写确认'
elif not result.safe_to_continue:
text = '未确认:会话释放未确认'
else:
text = '未确认:会话或数据库状态待确认'
elif result.error_category == 'PCB_VERSION_CHOICE_REQUIRED': text = '未确认:需选择工程版本'
else: text = '未确认:'+(result.stderr.splitlines()[0][:120] if result.stderr else result.status)
cell = self.cbb_table.item(row, 3)
cell.setText(text); cell.setToolTip(result.stderr or result.stdout)
cell.setBackground(QColor('#E8F5E9' if result.status == 'CHECKED' and name not in task.invalidated
else '#FFEBEE' if result.error_category == 'ICDB_REPAIR_REQUIRED' else '#FFF8E1'))
def _icdb_done(self, task):
from cbb_refresh_verify.icdb_maintenance import maintenance_counts
counts = maintenance_counts(task.results)
lease = task.path_lease
if lease is not None:
if any(not result.safe_to_continue for result in task.results): self._retain_path_lease(lease)
held_by_native = self._native_backup_session is not None and self._native_backup_session.path_lease is lease
if lease is not self.library_lease and not held_by_native:
self._close_path_lease(lease)
for request, result in zip(task.requests, task.results):
if result.status == 'SKIPPED': self._icdb_item_done(task, request.block_name, result)
text = (f'ICDB 检查结束:未发现异常 {counts["checked_without_repair"]},发现问题 {counts["issues_found"]},'
f'占用 {counts["in_use"]},无法确认 {counts["unconfirmed"]},取消 {counts["cancelled"]},未执行 {counts["skipped"]}')
from cbb_refresh_verify.reimport_recovery import offers_reimport
offers = tuple((req, task.records[req.block_name], result) for req, result in zip(task.requests, task.results)
if req.block_name not in task.invalidated and offers_reimport(result))
same_library = bool(_library_path_keys(task.requests[0].lmc_file, task.path_lease)
& _library_path_keys(self.mentor_lmc_path or self.loaded_path, self.library_lease))
if offers: text += '\n可选择工程副本恢复。'
if same_library and not self.busy: self._reimport_offers = offers
self.icdb_phase.setText(text)
if not self.busy: self.status_label.setText(text)
self.output.append(text+'\n'+str(task.report or task.report_error))
summary = QMessageBox(self); self.icdb_summary = summary
task.summary = summary
if not self.busy: self.summary = summary
summary.setWindowTitle('ICDB 批量检查结果'); summary.setText(text)
summary.setDetailedText(str(task.report or task.report_error)+'\n'+'\n'.join(
req.block_name+':'+('内容已更新,以下为旧检查结果:' if req.block_name in task.invalidated else '')
+(result.stderr or result.stdout or result.status)
for req, result in zip(task.requests, task.results)))
summary.setTextFormat(Qt.PlainText); summary.setAttribute(Qt.WA_ShowWithoutActivating)
if any(not result.safe_to_continue for result in task.results):
def release_task():
if not (_library_path_keys(task.requests[0].lmc_file, task.path_lease)
& _library_path_keys(self.mentor_lmc_path or self.loaded_path, self.library_lease)):
self.status_label.setText(f'请切回 {task.requests[0].lmc_file} 后释放该检查的残留。')
return
self.check_library_release()
summary.addButton('查看残留并释放…', QMessageBox.ActionRole).clicked.connect(release_task)
if offers:
action = summary.addButton('选择副本恢复…', QMessageBox.ActionRole)
task.recovery_action = action
task.recovery_names = {req.block_name for req, _, _ in offers}
def recover():
if task.recovery_names & task.invalidated: return
if self.busy or not (_library_path_keys(task.requests[0].lmc_file, task.path_lease)
& _library_path_keys(self.mentor_lmc_path or self.loaded_path, self.library_lease)): return
self._reimport_offers = offers
self.reimport_failed()
action.clicked.connect(recover)
summary.show(); self._update()
def load_library(self):
if self.busy or not self.lmc_edit.text(): return
self._maintenance_recovery_view = False
self._global_icdb_run = False
if self._management_unsafe_library is not None:
self._management_library_unsafe(self._management_unsafe_library)
requested_library = _path_key(self.lmc_edit.text())
if requested_library != _path_key(self._management_unsafe_library):
self._management_unsafe = False
self._management_unsafe_library = None
self.icdb_progress.hide(); self.icdb_phase.hide()
if not self._icdb_active():
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._records_by_name = {}
@@ -607,6 +918,8 @@ class MainWindow(QWidget):
lease = self.path_lease_factory(Path(path))
mentor_path = (select_library_file(lease.mentor_path)
if lease.mentor_path.is_dir() else lease.mentor_path)
if isinstance(lease, LibraryPathLease):
lease.mentor_path = mentor_path
if self.logging_context:
self.logging_context.logger.info(
'library_path_lease display=%s mentor=%s drive=%s remote=%s created=%s',
@@ -614,6 +927,9 @@ class MainWindow(QWidget):
lease.remote_root or '-', lease.created)
recovery_view = False
result = self.reader(mentor_path)
if not result.error:
self._publish_check_statuses(mentor_path,
{record.name: record.status for record in result.records}, lease)
if self.logging_context:
statuses = Counter(record.status for record in result.records)
unknown = [record.name for record in result.records if record.status in {'UNKNOWN', 'UNAVAILABLE'}]
@@ -636,10 +952,10 @@ class MainWindow(QWidget):
def loaded(payload):
result, skip_reasons, lease, mentor_path, recovery_view = payload
if path != self.lmc_edit.text():
lease.close()
self._close_path_lease(lease)
return
if result.error:
lease.close()
self._close_path_lease(lease)
self.status_label.setText(result.error); return
self.library_lease = lease
self._maintenance_recovery_view = recovery_view
@@ -747,8 +1063,13 @@ class MainWindow(QWidget):
self._run(names, maintenance=True)
def _native_backup_same_library(self):
return self._native_backup_matches_library(self.mentor_lmc_path or self.lmc_edit.text())
def _native_backup_matches_library(self, library, lease=None):
session = self._native_backup_session
return session is not None and Path(self.mentor_lmc_path or self.lmc_edit.text()) == session.library
return session is not None and bool(
_library_path_keys(library, lease or self.library_lease)
& _library_path_keys(session.library, getattr(session, 'path_lease', None)))
def open_native_backup(self):
if self.busy or not self.loaded_path or self._native_backup_session is not None:
@@ -763,6 +1084,9 @@ class MainWindow(QWidget):
self._global_icdb_run = False
self._reimport_offers = ()
self.status_label.setText(f'正在打开 {request.block_name} 的版本与备份…')
paused_check = self.icdb_task if self._check_matches_library(request.lmc_file) else None
if paused_check is not None:
paused_check.pause(); paused_check.invalidate([request.block_name])
def opened(session):
session.path_lease = self.library_lease
self._native_backup_session = session
@@ -770,30 +1094,33 @@ class MainWindow(QWidget):
self.status_label.setText(f'正在打开 {session.block} 的原生备份窗口,请在其中选择版本。')
self.output.append(f'{session.block} · 原生版本与备份\n{session.job}')
self.busy = True; self._update()
self.worker = ServiceWorker(lambda: launcher(request))
self.stop.clear()
self.worker = ServiceWorker(self._queued_action(lambda: launcher(request)))
self.worker.signals.succeeded.connect(opened)
self.worker.signals.failed.connect(lambda error: self.status_label.setText(f'无法打开版本与备份:{error}'))
def failed(error):
self.status_label.setText(f'无法打开版本与备份:{error}')
if paused_check is not None: paused_check.resume()
self.worker.signals.failed.connect(failed)
self.worker.signals.finished.connect(self._finished)
self.pool.start(self.worker)
def _release_native_backup_session(self, session):
self._native_backup_session = None
if session.path_lease is not None and session.path_lease is not self.library_lease:
transfer = getattr(session.path_lease, 'transfer_ownership', None)
if transfer is not None:
transfer(self.library_lease)
session.path_lease.close()
self._close_path_lease(session.path_lease)
if self._icdb_active(): self.icdb_task.resume()
def _poll_native_backup(self):
session = self._native_backup_session
if session is None or self.busy or session.process.poll() is None:
return
self.native_backup_timer.stop()
same_library = Path(self.mentor_lmc_path or self.lmc_edit.text()) == session.library
same_library = self._native_backup_same_library()
def read_result():
import json
result = json.loads(session.job.with_name('result.json').read_text('utf-8'))
if Path(result['library']) != session.library or result['block'] != session.block:
if (not self._native_backup_matches_library(result['library'], getattr(session, 'path_lease', None))
or result['block'] != session.block):
raise ValueError('原生会话返回的工程身份不一致。')
refresh = (self._read_affected_records((session.block,))
if result.get('safe_to_continue') is True and same_library else None)
@@ -816,7 +1143,8 @@ class MainWindow(QWidget):
self.status_label.setText('原生窗口退出尚未确认,请关闭窗口后检查释放。')
self.output.append(f'{session.block} · 退出尚未确认:{error}\n{session.job}')
self.busy = True; self._update()
self.worker = ServiceWorker(read_result)
self.stop.clear()
self.worker = ServiceWorker(self._queued_action(read_result))
self.worker.signals.succeeded.connect(completed)
self.worker.signals.failed.connect(unconfirmed)
self.worker.signals.finished.connect(self._finished)
@@ -830,6 +1158,7 @@ class MainWindow(QWidget):
if not names:
return
self.backup_dialog = BackupRecoveryDialog(names, self._backup_request, self)
self.backup_dialog.native_queue = self.native_queue
row = self.cbb_table.currentRow()
if row >= 0 and self.cbb_table.item(row, 0).text() in names:
self.backup_dialog.block_combo.setCurrentText(self.cbb_table.item(row, 0).text())
@@ -847,6 +1176,7 @@ class MainWindow(QWidget):
return self._request(name)
def _backup_started(self, name):
self._invalidate_check_targets(self.mentor_lmc_path or self.loaded_path, [name])
self.busy = True
self._global_icdb_run = False
self.ready_names.discard(name)
@@ -864,7 +1194,7 @@ class MainWindow(QWidget):
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()
self._retain_path_lease(self.library_lease, hold_busy=True)
row = self._row_by_name.get(name)
if row is not None and result.status != 'PASS':
self.cbb_table.item(row, 2).setText('未知 / 需确认')
@@ -881,6 +1211,7 @@ class MainWindow(QWidget):
self._finished()
def _run(self, names, *, maintenance=False):
if maintenance: return self._start_icdb_check(names)
if self.busy or not self.loaded_path: return
if self._native_backup_same_library():
self.status_label.setText('请先关闭该库的版本与备份窗口。')
@@ -897,8 +1228,8 @@ class MainWindow(QWidget):
self._reimport_offers = ()
self._global_icdb_run = maintenance
self._icdb_completed = 0
self.icdb_progress.setVisible(maintenance)
self.icdb_phase.setVisible(maintenance)
self.icdb_progress.setVisible(maintenance or self._icdb_active())
self.icdb_phase.setVisible(maintenance or self._icdb_active())
if maintenance:
self.icdb_progress.setRange(0, len(requests))
self.icdb_progress.setValue(0)
@@ -922,10 +1253,14 @@ class MainWindow(QWidget):
progress=lambda name, result: self.signals.event.emit({'block': name, 'stage': result.status,
'error_category': result.error_category, 'evidence': str(result.evidence_path or ''),
'detail': result.stderr or result.stdout}))
report = write_batch_report(requests, results, self.data_root/'reports')
if maintenance:
from cbb_refresh_verify.icdb_maintenance import write_maintenance_report
write_maintenance_report(requests, results, report, ())
report = None
try:
report = write_batch_report(requests, results, self.data_root/'reports')
if maintenance:
from cbb_refresh_verify.icdb_maintenance import write_maintenance_report
write_maintenance_report(requests, results, report, ())
except Exception as error:
self.signals.event.emit({'stage': 'report_failed', 'detail': str(error)})
affected = [req.block_name for req, result in zip(requests, results) if result.status != 'SKIPPED']
safe = all(result.safe_to_continue for result in results)
refresh = self._read_affected_records(affected) if safe else None
@@ -940,6 +1275,8 @@ class MainWindow(QWidget):
path = Path(self.mentor_lmc_path or self.loaded_path)
try:
statuses = self.status_refresher(tuple(names), path)
# Publish before yielding the native queue, ahead of Qt callbacks.
self._publish_check_statuses(path, statuses)
reasons = {name: reason for name in names
if (reason := preflight_skip_reason(path, name))}
return tuple(names), statuses, reasons, ''
@@ -950,6 +1287,8 @@ class MainWindow(QWidget):
def _apply_refreshed_records(self, payload):
names, statuses, reasons, error = payload
self._publish_check_statuses(self.mentor_lmc_path or self.loaded_path,
{name: statuses.get(name, 'UNKNOWN') for name in names})
self.cbb_table.blockSignals(True)
try:
for name in names:
@@ -1061,6 +1400,8 @@ class MainWindow(QWidget):
def _batch_done(self, outcome):
results, report = outcome[:2]
failure_report = failure_report_path(report) if report is not None else '未写入'
report = report if report is not None else '未写入'
from cbb_refresh_verify.reimport_recovery import offers_reimport
self._reimport_offers = tuple((req, self._batch_records[req.block_name], result)
for req, result in zip(self._batch_requests, results)
@@ -1072,7 +1413,7 @@ class MainWindow(QWidget):
else:
self._apply_refreshed_records(refresh)
if self.library_lease is not None and any(not result.safe_to_continue for result in results):
self.library_lease.retain_for_recovery()
self._retain_path_lease(self.library_lease, hold_busy=True)
for result in results:
if not result.safe_to_continue and result.evidence_path:
self.recovery_path = result.evidence_path.parent
@@ -1083,7 +1424,7 @@ class MainWindow(QWidget):
repaired = sum(r.status == 'REPAIRED' for r in results)
failed = len(results)-passed-skipped-checked-repaired
text = (f'已结束:成功 {passed},失败 {failed},预检跳过 {len(self.skipped_names)},'
f'队列未执行 {skipped}。\n完整日志:{report}\n仅失败日志:{failure_report_path(report)}')
f'队列未执行 {skipped}。\n完整日志:{report}\n仅失败日志:{failure_report}')
if self._global_icdb_run:
from cbb_refresh_verify.icdb_maintenance import maintenance_counts
counts = maintenance_counts(results)
@@ -1091,7 +1432,7 @@ class MainWindow(QWidget):
f'未发现异常 {counts["checked_without_repair"]},发现问题 {counts["issues_found"]},'
f'占用 {counts["in_use"]},无法确认 {counts["unconfirmed"]},'
f'取消 {counts["cancelled"]},队列未执行 {skipped}\n'
f'完整日志:{report}\n仅失败日志:{failure_report_path(report)}')
f'完整日志:{report}\n仅失败日志:{failure_report}')
self.icdb_phase.setText('批量检查已结束')
if any(not result.safe_to_continue for result in results):
text += '\n未确认数据库或会话安全,已停止剩余校验。请查看恢复现场。'
@@ -1124,6 +1465,21 @@ class MainWindow(QWidget):
self.summary.setDetailedText(text)
self.summary.setIcon(QMessageBox.Warning if failed else QMessageBox.Information)
self._reimport_summary_action = None
self._residual_release_action = None
self._resume_batch_action = None
self._resume_batch = None
if any(not result.safe_to_continue for result in results):
self._residual_release_action = self.summary.addButton('查看残留并释放…', QMessageBox.ActionRole)
self._residual_release_action.clicked.connect(self.check_library_release)
self._residual_release_action.setEnabled(False)
names = [req.block_name for req, result in zip(self._batch_requests, results)
if result.status == 'SKIPPED']
if names:
self._resume_batch = {'library': self._batch_requests[0].lmc_file, 'names': names,
'maintenance': self._global_icdb_run, 'released': False}
self._resume_batch_action = self.summary.addButton('继续未执行项', QMessageBox.ActionRole)
self._resume_batch_action.clicked.connect(self._resume_released_batch)
self._resume_batch_action.setEnabled(False)
if self._reimport_offers:
self.summary.setStandardButtons(QMessageBox.Close)
self._reimport_summary_action = self.summary.addButton('选择副本恢复…', QMessageBox.ActionRole)
@@ -1146,6 +1502,9 @@ class MainWindow(QWidget):
self.version_dialog.show()
def request_stop(self):
if self._icdb_active():
self._stop_icdb_check()
if not self.busy: return
if self.version_dialog is not None:
self.version_dialog.reject()
management = getattr(self, 'management_dialog', None)
@@ -1155,12 +1514,17 @@ class MainWindow(QWidget):
'已请求取消;检查将停止,原生写入只等待安全结束,不强制终止。'); self._update()
def _failed(self, error):
if self.library_lease is not None:
self.library_lease.retain_for_recovery()
from cbb_refresh_verify.operation_queue import QueueCancelled
if self.library_lease is not None and not isinstance(error, QueueCancelled):
self._retain_path_lease(self.library_lease, hold_busy=True)
self._display_error(error)
def _display_error(self, error):
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)
def _finished(self):
if self._defer_mapping_finish(self._finished): return
if self.version_dialog is not None:
self.version_dialog.reject()
self.version_dialog = None
@@ -1176,16 +1540,45 @@ class MainWindow(QWidget):
def recover(self):
if not self.recovery_path: return
try: os.startfile(str(self.recovery_path))
except Exception as error: self._failed(error)
except Exception as error: self._display_error(error)
def closeEvent(self, event):
if self.busy:
if self._close_ready:
event.accept()
elif self._closing:
event.ignore()
elif self._icdb_active():
self._stop_icdb_check(); event.ignore()
elif self.busy:
self.request_stop(); event.ignore()
else:
self._closing = True
self.busy = True; self._update()
retained = None
if self._native_backup_session is not None:
self.native_backup_timer.stop()
lease = self._native_backup_session.path_lease
if lease is not None:
lease.retain_for_recovery()
self._release_library_lease()
event.accept()
retained = self._native_backup_session.path_lease
lease, self.library_lease = self.library_lease, None
def release():
if retained is not None: retained.retain_for_recovery()
if lease is not None and lease is not retained: lease.close()
self._lease_action(release)
self._close_when_idle()
event.ignore()
def _close_when_idle(self):
owners = [self, *self.icdb_tasks]
for name in ('management_dialog', 'backup_dialog', 'release_dialog'):
owners.append(getattr(self, name, None))
active = False
for owner in owners:
try:
pool = getattr(owner, 'pool', None)
active = active or bool(pool is not None and pool.activeThreadCount())
except RuntimeError:
pass # Qt may already have deleted a closed dialog's pool.
if self._lease_pending or active:
QTimer.singleShot(20, self._close_when_idle)
return
self._close_ready = True
self.close()
+17 -2
View File
@@ -34,6 +34,7 @@ class ManagementSignals(QObject):
class ManagementDialog(QDialog):
completed = Signal(object)
before_execute = Signal(object)
def __init__(self, requests, parent=None, *, adapter=None, runner=None, recovery_from=None):
super().__init__(parent)
@@ -41,6 +42,7 @@ class ManagementDialog(QDialog):
self.template = self.templates[0]
self.operation = self.template.operation
self.adapter = adapter
self.native_queue = None
self.recovery_from = dict(recovery_from or {})
self.runner = runner or run_management_batch
if self.recovery_from and runner is None:
@@ -160,8 +162,20 @@ class ManagementDialog(QDialog):
return
self.busy = True; self.executed = True
self._enabled(False)
self.worker = ServiceWorker(lambda: self.runner(requests, adapter=self.adapter, stop=self.stop,
progress=self.signals.progress.emit))
self.before_execute.emit([r.name for r in requests])
def execute():
action = lambda: self.runner(requests, adapter=self.adapter, stop=self.stop,
progress=self.signals.progress.emit)
if self.native_queue is None: return action()
from ..operation_queue import QueueCancelled
try:
return self.native_queue.run(action, stop=self.stop, waiting=lambda: self.signals.progress.emit(
{'stage': 'waiting_native_slot', 'detail': '已排队,等待当前检查结束'}))
except QueueCancelled as error:
from ..management import ManagementResult
return [ManagementResult(r.operation, r.library, r.name, r.partition, r.source, r.auto_verify,
'complete', 'BLOCKED', True, str(error)) for r in requests]
self.worker = ServiceWorker(execute)
self.worker.signals.succeeded.connect(self._result)
self.worker.signals.failed.connect(self._failed)
self.worker.signals.finished.connect(self._finished)
@@ -176,6 +190,7 @@ class ManagementDialog(QDialog):
def _progress(self, event):
stage = str(event.get('stage', ''))
text = f'{event.get("block", "")} {STAGE_LABELS.get(stage, RESULT_LABELS.get(stage, "正在处理"))}'
if stage == 'waiting_native_slot': text = str(event.get('detail', '已排队,等待执行'))
self.output.append(str(event))
if 'copied_files' in event:
text += f' {event["copied_files"]}/{event.get("total_files", "?")}'
@@ -0,0 +1,178 @@
"""User-confirmed, asynchronous native-session release."""
from pathlib import Path
from threading import Event
from PySide6.QtCore import QThreadPool, Signal
from PySide6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
QTableWidget, QTableWidgetItem, QHeaderView, QMessageBox)
from .workers import ServiceWorker
class ResidualReleaseDialog(QDialog):
released = Signal(object)
before_release = Signal()
def __init__(self, library, evidence_dir, parent=None):
super().__init__(parent)
self.library, self.evidence_dir = Path(library), Path(evidence_dir)
self.plan = None
self.native_queue = None
self.busy = False
self.stop = Event()
self._refresh_after = False
self.pool = QThreadPool(self)
self.setWindowTitle('释放残留会话')
self.setModal(True)
self.resize(760, 350)
layout = QVBoxLayout(self)
self.message = QLabel('正在检查当前库关联的会话…')
self.message.setWordWrap(True)
layout.addWidget(self.message)
self.table = QTableWidget(0, 4)
self.table.setHorizontalHeaderLabels(['工程', '程序', 'PID', '状态'])
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
self.table.horizontalHeader().setSectionResizeMode(3, QHeaderView.Stretch)
self.table.setEditTriggers(QTableWidget.NoEditTriggers)
layout.addWidget(self.table)
buttons = QHBoxLayout()
self.refresh_button = QPushButton('重新检查')
self.confirm_button = QPushButton('确认释放')
self.prompt_button = QPushButton('处理保存提示…')
self.force_button = QPushButton('强制结束所列会话…')
self.force_button.setEnabled(False)
self.prompt_button.setEnabled(False)
self.close_button = QPushButton('关闭')
for button in (self.refresh_button, self.confirm_button, self.prompt_button, self.force_button, self.close_button): buttons.addWidget(button)
layout.addLayout(buttons)
self.refresh_button.clicked.connect(self.refresh)
self.confirm_button.clicked.connect(self.confirm_release)
self.close_button.clicked.connect(self.close)
self.prompt_button.clicked.connect(self.answer_prompt)
self.force_button.clicked.connect(self.force_release)
self.table.itemSelectionChanged.connect(self._update_prompt)
self.refresh()
def _start(self, action, completed):
self.busy = True
self.confirm_button.setEnabled(False)
self.refresh_button.setEnabled(False)
self.prompt_button.setEnabled(False)
self.force_button.setEnabled(False)
self.close_button.setText('停止释放')
self.worker = ServiceWorker(action)
self.worker.signals.succeeded.connect(completed)
self.worker.signals.failed.connect(lambda error: self.message.setText(str(error)))
self.worker.signals.finished.connect(self._finished)
self.pool.start(self.worker)
def _finished(self):
self.busy = False
self.refresh_button.setEnabled(True)
self.confirm_button.setEnabled(self.plan is not None)
self.force_button.setEnabled(bool(self.plan and any(
r.get('scope_confirmed', True) for r in self.plan['sessions'])))
self.close_button.setText('关闭')
self._update_prompt()
if self._refresh_after:
self._refresh_after = False
self.refresh()
def _rows(self, sessions):
self.table.setRowCount(len(sessions))
for index, session in enumerate(sessions):
for column, value in enumerate((session.get('project', ''), session['name'],
session['pid'], session.get('detail', ''))):
self.table.setItem(index, column, QTableWidgetItem(str(value)))
if sessions: self.table.setCurrentCell(0, 0)
def _update_prompt(self):
row = self.table.currentRow()
rows = self.plan['sessions'] if self.plan else []
self.prompt_button.setEnabled(not self.busy and 0 <= row < len(rows) and bool(rows[row].get('prompt')))
def answer_prompt(self):
if self.busy or not self.plan: return
row = self.table.currentRow()
if row < 0: return
session = self.plan['sessions'][row]
prompt = session.get('prompt')
if not prompt: return
choice = QMessageBox.question(self, '关闭工程前保存', prompt['text']+
'\n\n是:保存后关闭;否:放弃未保存更改;取消:保留会话。',
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.Cancel)
code = {QMessageBox.Yes: 6, QMessageBox.No: 7, QMessageBox.Cancel: 2}[choice]
from cbb_refresh_verify.residual_sessions import answer_save_question
self._start(lambda: answer_save_question(session, code),
lambda _: setattr(self, '_refresh_after', True))
def refresh(self):
if self.busy: return
from cbb_refresh_verify.library_lock import inspect_residual_sessions
self.plan = None
self.message.setText('正在检查当前库关联的会话…')
def ready(plan):
self.library = Path(plan['library'])
self.plan = plan
self._rows(plan['sessions'])
text = ('确认后逐项正常关闭下列会话;有未保存更改时,可选择保存或保留。'
if plan['sessions'] else '未发现活跃会话。')
records = plan.get('previous_records', [])
if records:
dates = ', '.join(sorted({r['time'][:10] for r in records if r.get('time')}))
text += f'\n有 {len(records)} 条历史操作记录'+(f'({dates})' if dates else '')+',确认后将复核并清理。'
elif not plan['sessions']:
text += '确认后将复查库占用状态。'
self.message.setText(text)
def inspect():
from cbb_refresh_verify.network_path import select_library_file
library = self.library if self.library.suffix.casefold() == '.lmc' else select_library_file(self.library)
return inspect_residual_sessions(library)
self._start(inspect, ready)
def force_release(self):
if self.busy or not self.plan or not self.plan['sessions']: return
targets = [r for r in self.plan['sessions'] if r.get('scope_confirmed', True)]
if not targets: return
names = '\n'.join(f'{r.get("project", "")} — {r["name"]} (PID {r["pid"]})' for r in targets)
answer = QMessageBox.warning(self, '强制结束会话',
'将强制结束以下进程,未保存的更改会丢失,正在写入的工程可能需要恢复。\n\n'+names+
'\n\n仍要结束这些进程吗?', QMessageBox.Yes | QMessageBox.Cancel, QMessageBox.Cancel)
if answer == QMessageBox.Yes: self.confirm_release(force=True)
def confirm_release(self, checked=False, *, force=False):
if self.busy or self.plan is None: return
from cbb_refresh_verify.library_lock import release_residual_sessions
self.stop.clear()
plan = self.plan
self.before_release.emit()
self.message.setText('正在逐项释放,请稍候…')
def done(result):
self.plan = {**plan, 'sessions': result['sessions']}
self._rows(result['sessions'])
count = sum(row['release_status'] == 'RELEASED' for row in result['sessions'])
if result['status'] == 'RELEASE_CONFIRMED':
self.plan = None
self.message.setText(f'已释放 {count} 个会话,旧占用记录已解除。')
self.released.emit(result['proof'])
else:
self.message.setText(f'已释放 {count} 个会话,其余会话或库状态仍待确认。'
+ ('\n'+result['detail'] if result.get('detail') else '请处理原生窗口后重新检查。'))
def execute():
action = lambda: release_residual_sessions(plan, self.evidence_dir, stop=self.stop, force=force)
return self.native_queue.run(action, stop=self.stop) if self.native_queue is not None else action()
self._start(execute, done)
def closeEvent(self, event):
if self.busy:
self.stop.set()
self.message.setText('正在停止释放,等待本次操作返回…')
event.ignore()
else:
super().closeEvent(event)
def reject(self):
if self.busy:
self.stop.set()
else:
super().reject()
+52 -1
View File
@@ -7,6 +7,7 @@ once after cleanup if the worker was killed while a native file was open.
from pathlib import Path
import json
import os
import re
import stat
import tempfile
import subprocess
@@ -124,7 +125,7 @@ def collect_native_failure_logs(pcb_path, evidence_root, *, max_bytes=NATIVE_LOG
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
from .icdb_recovery import read_log_failure, read_icdb_error
root = Path(evidence_root)/'diagnostics'
try:
baseline = json.loads((root/'startup-log-baseline.json').read_text('utf-8'))
@@ -135,6 +136,9 @@ def _collect_icdb_recovery(source, evidence_root):
_, recovery = read_log_failure(source, checkpoint)
if recovery:
_write_record(root/'icdb-recovery.json', {**recovery, 'source': str(source)})
message = read_icdb_error(source, checkpoint)
if message:
_write_record(root/'icdb-error-content.json', {'native_message': message, 'source': str(source)})
except (OSError, ValueError, KeyError, TypeError):
pass # Unknown freshness must not replace the original failure.
@@ -143,9 +147,56 @@ def _collect_icdb_recovery(source, evidence_root):
def failure_logs_main(request_path):
job = json.loads(Path(request_path).read_text(encoding='utf-8'))
collect_native_failure_logs(job['pcb'], job['root'])
collect_native_client_error(job['root'])
return 0
def collect_native_client_error(evidence_root):
"""Read only this PCB PID's new iCDB manager log in the bounded log helper."""
root = Path(evidence_root)/'diagnostics'
try:
context = json.loads((root/'native-client-context.json').read_text('utf-8'))
pid, started = context['native_pid'], context['started_ns']
if type(pid) is not int or pid <= 0 or type(started) is not int:
return
target = str(context['database']).replace('/', '\\').casefold()
if not target:
return
from .icdb_recovery import decode_log
for directory in Path(context['client_log_root']).glob(f'*-{pid}'):
for source in directory.glob('*.iCDBManager.txt'):
with source.open('rb') as stream:
metadata = os.fstat(stream.fileno())
if (metadata.st_mtime_ns < started
or getattr(metadata, 'st_birthtime_ns', started) < started):
continue
header = stream.read(16*1024)
stream.seek(0, os.SEEK_END)
size = stream.tell()
stream.seek(max(0, size-NATIVE_LOG_MAX_BYTES))
tail = stream.read(NATIVE_LOG_MAX_BYTES)
if not re.search(rf'\bPID\s*\[{pid}\]', decode_log(header)):
continue
# The native iCDB logger supplies the domain; a pathname in a
# generic PCB error alone never establishes this identity.
lines = decode_log(tail).splitlines()
if size > NATIVE_LOG_MAX_BYTES:
lines = lines[1:] # The first bounded-tail line may start mid-message.
message = next((line for line in lines
if re.search(r'\bERROR\s*:', line, re.I)
and target in line.replace('/', '\\').casefold()), '')
if message:
target_file = root/'native-logs/current-icdb-manager.txt'
target_file.parent.mkdir(parents=True, exist_ok=True)
target_file.write_bytes(tail)
_write_record(root/'icdb-error-content.json', {'native_message': 'iCDB: '+message,
'source': str(source), 'native_pid': pid, 'started_ns': started,
'database': context['database'], 'evidence': str(target_file)})
return
except (OSError, ValueError, KeyError, TypeError):
pass # A missing/ambiguous native log must not replace the open failure.
def capture_failure_logs_with_timeout(request_path, *, timeout_seconds=8):
"""Post-cleanup retry in a killable helper, not a potentially stuck SMB thread."""
request_path = Path(request_path)
+81 -35
View File
@@ -14,6 +14,7 @@ native menu invocation happens on the worker's private desktop.
from dataclasses import asdict, dataclass, field, replace
from pathlib import Path
import json
import logging
import os
import platform
import re
@@ -32,6 +33,22 @@ ERROR_TEXT = re.compile(r'failed|error|cannot connect|bad data', re.IGNORECASE)
INVALID_NAME_CHARS = '\\/:*?"<>|'
def _notify_progress(callback, *args):
"""A failed observer cannot change native execution or release facts."""
if callback is not None:
try:
callback(*args)
except Exception:
logging.getLogger(__name__).warning('Progress notification failed', exc_info=True)
def _write_optional_evidence(path, value):
try:
path.write_text(json.dumps(value, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
except OSError:
logging.getLogger(__name__).warning('Could not write diagnostic evidence %s', path, exc_info=True)
@dataclass(frozen=True)
class VerifyRequest:
lmc_file: Path
@@ -511,7 +528,7 @@ def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline, l
except OSError:
pass
if progress:
progress(event)
_notify_progress(progress, event)
return cleanup_verify_processes(
lm_pid,
@@ -545,11 +562,12 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
if not acquired:
return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。',
safe_to_continue=False)
safe_to_continue=True, error_category='NATIVE_CHANNEL_BUSY')
if request.maintenance:
from .maintenance_entry import run_icdb_check
return run_icdb_check(request, progress=progress, stop=stop)
from .library_lock import LibraryLock, LibraryBusy
import pywintypes
if Path(request.lmc_file).is_file():
try:
library_lock = LibraryLock(request.lmc_file, checkpoint=False,
@@ -557,6 +575,8 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
library_lock.__enter__()
except LibraryBusy as error:
return VerifyResult('FAIL', stderr=str(error), error_category='LIBRARY_IN_USE')
except (OSError, pywintypes.error) as error:
return VerifyResult('FAIL', stderr=str(error), error_category='LIBRARY_ACCESS_FAILED')
release_confirmed = False
result = _run_verify_serialized(
request, progress=progress, flag_reader=flag_reader,
@@ -726,23 +746,24 @@ def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_
error_stage=(result.error_stage or (unfinished[-1] if unfinished else 'execution')
if result.status not in {"PASS", "SKIPPED"} else ""))
if result.evidence_path:
result.evidence_path.write_text(json.dumps(
{'block_name': request.block_name, **asdict(result)},
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
result.evidence_path.with_name('performance.json').write_text(json.dumps({
_write_optional_evidence(result.evidence_path, {'block_name': request.block_name, **asdict(result)})
_write_optional_evidence(result.evidence_path.with_name('performance.json'), {
'block_name': request.block_name, 'status': result.status,
'unit': 'seconds', 'timings_seconds': durations,
'incomplete_timing_phases': unfinished,
'clock': 'monotonic; save helper subphases: VBScript Timer (midnight adjusted)',
'notes': 'phase totals overlap action/call subphases; skipped phases are absent',
}, ensure_ascii=False, indent=2), encoding='utf-8')
})
if progress:
progress({'stage': 'performance', 'block': request.block_name,
_notify_progress(progress, {'stage': 'performance', 'block': request.block_name,
'timings_seconds': durations, 'incomplete_timing_phases': unfinished,
'detail': ' '.join(f'{key}={value:.3f}s' for key, value in durations.items())})
if request.logging_context:
request.logging_context.logger.info('operation=cbb_verify block=%s status=%s flag=%s evidence=%s error=%s timings_seconds=%s',
request.block_name, result.status, result.library_flag, result.evidence_path, result.stderr, durations)
try:
request.logging_context.logger.info('operation=cbb_verify block=%s status=%s flag=%s evidence=%s error=%s timings_seconds=%s',
request.block_name, result.status, result.library_flag, result.evidence_path, result.stderr, durations)
except Exception:
logging.getLogger(__name__).warning('Operation logging failed', exc_info=True)
return result
@@ -756,12 +777,14 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
not depend on the host's port reservations.
"""
root = Path(request.evidence_dir).resolve()/('verify-' + uuid4().hex[:12])
root.mkdir(parents=True, exist_ok=False)
try:
root.mkdir(parents=True, exist_ok=False)
except OSError as error:
return VerifyResult('FAIL', stderr=f'无法创建操作记录目录:{error}')
fingerprint = runtime_fingerprint(request.mentor_bin)
(root/'runtime.json').write_text(
json.dumps(fingerprint, ensure_ascii=False, indent=2), encoding='utf-8')
_write_optional_evidence(root/'runtime.json', fingerprint)
if progress:
progress({'stage': 'runtime_fingerprint', 'block': request.block_name,
_notify_progress(progress, {'stage': 'runtime_fingerprint', 'block': request.block_name,
**fingerprint})
result_path = root/'result.json'
runner = process_runner or (lambda command, timeout, cwd:
@@ -797,7 +820,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
return finish(VerifyResult('BLOCKED', stderr=str(error)))
command = build_content_check_command(request, executable)
if progress:
progress({'stage': 'content_check_start', 'block': request.block_name,
_notify_progress(progress, {'stage': 'content_check_start', 'block': request.block_name,
'detail': ' '.join(command)})
timing.begin('content_check')
try:
@@ -819,7 +842,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
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'})
_notify_progress(progress, {'stage': 'content_check_done', 'block': request.block_name, 'status': 'PASS'})
# Sampling cannot establish that the whole native port range is unusable.
from .mentor_adapter import icdb_ports_bindable
@@ -827,7 +850,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
ports_ok, ports_detail = check_ports()
if not ports_ok:
if progress:
progress({'stage': 'port_warning', 'block': request.block_name, 'detail': ports_detail})
_notify_progress(progress, {'stage': 'port_warning', 'block': request.block_name, 'detail': ports_detail})
if request.strict_port_check:
return finish(VerifyResult('BLOCKED', stderr=ports_detail))
@@ -844,14 +867,17 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
'cancel_signal': str(root/'cancel-worker.signal'),
'start_gate': str(root/'start-worker.signal')}
consent = None
if request.post_repair_proof is not None:
from .post_repair_consent import PostRepairProof, ConsentController, exact_prompt
if not isinstance(request.post_repair_proof, PostRepairProof):
return finish(VerifyResult('BLOCKED', stderr='POST_REPAIR_PROOF_REQUIRED', safe_to_continue=False))
job['post_repair_consent'] = request.post_repair_proof.for_job(job)
consent = ConsentController(request.post_repair_proof, job)
job_path = root/'request.json'
job_path.write_text(json.dumps(job, ensure_ascii=False), encoding='utf-8')
try:
if request.post_repair_proof is not None:
from .post_repair_consent import PostRepairProof, ConsentController, exact_prompt
if not isinstance(request.post_repair_proof, PostRepairProof):
return finish(VerifyResult('BLOCKED', stderr='POST_REPAIR_PROOF_REQUIRED'))
job['post_repair_consent'] = request.post_repair_proof.for_job(job)
consent = ConsentController(request.post_repair_proof, job)
job_path.write_text(json.dumps(job, ensure_ascii=False), encoding='utf-8')
except Exception as error:
return finish(VerifyResult('FAIL', stderr=str(error)))
events = root/'events.jsonl'
offset = 0
target_pid = None
@@ -871,10 +897,13 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
lock_status = win32event.WaitForSingleObject(mutex, 0)
if lock_status not in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED):
mutex.Close()
return finish(VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。', safe_to_continue=False))
return finish(VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行;本次未启动。',
safe_to_continue=True, error_category='NATIVE_CHANNEL_BUSY'))
worker_launch_attempted = False
try:
timing.begin('worker_setup')
with (root/'worker.log').open('w', encoding='utf-8') as log:
worker_launch_attempted = True
if request.interactive_input:
process = DesktopWorker(
worker_command(job_path), None, interactive_input=True)
@@ -892,9 +921,10 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
if hasattr(process, 'start_companion'):
from .dialog_agent import DialogAgentClient
from .dialog_rules import default_rules_path, load_rules
dialog_agent = DialogAgentClient.start(process, root/'dialog-agent')
dialog_agent = DialogAgentClient.start(process, root/'dialog-agent', stop=stop)
dialog_supervisor = DialogSupervisor(
AgentDialogBackend(dialog_agent),
AgentDialogBackend(dialog_agent, pids=lambda: {
int(pid) for pid in (target_pid, lm_pid, launcher_pid) if pid}),
rules=load_rules(default_rules_path()),
)
else:
@@ -907,6 +937,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
desktop=process.desktop,
pids=lambda: ({value for value in (target_pid, lm_pid) if value is not None}),
)
if stop is not None and stop.is_set():
raise InterruptedError('VERIFY_CANCELLED: 启动已取消,未启动 PCB')
Path(job['start_gate']).touch()
while process.poll() is None:
if stop is not None and stop.is_set():
@@ -947,7 +979,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
0.1, float(request.timeout_seconds))
deadline.touch()
last_activity_key = activity_key
if progress: progress(event)
if progress: _notify_progress(progress, event)
offset = stream.tell()
if verify_invoked and target_pid is None and time.monotonic() - last_observation >= .5:
from .post_verify_diagnostics import observe_post_verify
@@ -1051,7 +1083,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
if dispatch_failure is not None:
status, detail = dispatch_failure
if progress:
progress({
_notify_progress(progress, {
'stage': 'verify_dispatch_failed',
'block': request.block_name,
'detail': detail,
@@ -1070,7 +1102,21 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
except Exception as error:
if 'process' in locals() and process.poll() is None:
_stop_worker(process)
return finish(VerifyResult('FAIL', stderr=str(error), safe_to_continue=False))
launch_release = getattr(error, 'worker_launch_release', None) if 'process' not in locals() else None
if not worker_launch_attempted:
launch_release = {'process_created': False, 'worker_exited': True,
'safe_to_continue': True, 'remaining_pids': []}
no_launch = (isinstance(launch_release, dict)
and launch_release.get('process_created') is False
and launch_release.get('worker_exited') is True
and launch_release.get('safe_to_continue') is True
and launch_release.get('remaining_pids') == [])
if no_launch:
_write_optional_evidence(root/'session-release.json', {
**launch_release, 'block': request.block_name, 'library': str(request.lmc_file),
'worker_pid': None})
status = 'CANCELLED' if stop is not None and stop.is_set() else 'FAIL'
return finish(VerifyResult(status, stderr=str(error), safe_to_continue=no_launch))
finally:
close_errors = []
worker_exited = False
@@ -1178,16 +1224,16 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
def record_attempt(record):
bridge_records.append(record)
if progress:
progress({'stage': 'library_flag_probe', 'block': request.block_name, **record})
_notify_progress(progress, {'stage': 'library_flag_probe', 'block': request.block_name, **record})
try:
probe = read_library_flag_with_retry(
flag_reader or read_library_flag_if_available, Path(request.lmc_file),
request.block_name, on_attempt=record_attempt)
result = apply_post_verify_status(result, probe)
result = replace(result, bridge_attempts=tuple(bridge_records))
(root/'bridge-status.json').write_text(json.dumps(bridge_records, ensure_ascii=False, indent=2), encoding='utf-8')
_write_optional_evidence(root/'bridge-status.json', bridge_records)
if progress:
progress({'stage': 'library_flag', 'block': request.block_name,
_notify_progress(progress, {'stage': 'library_flag', 'block': request.block_name,
'library_flag': result.library_flag, 'detail': result.library_flag_detail})
finally:
timing.end('library_flag')
@@ -1216,7 +1262,7 @@ def run_batch(requests, max_workers=1, verifier=None, *, stop=None, progress=Non
queue_stop = queue_stop or result.status in {'QUEUE_STOP', 'CANCELLED'} or not result.safe_to_continue
if queue_stop and stop is not None:
stop.set()
if progress: progress(request.block_name, result)
if progress: _notify_progress(progress, request.block_name, result)
return results
@@ -1273,7 +1319,7 @@ def write_batch_report(requests, results, root):
lines = ['CBB Verify 批量结果',
'流程: LMC 原生 Verify / ReusableCircuit PCB -> Project Integration -> Save/Close -> IsVerifiedReuseBlock READY(金标准)']
if requests and all(req.maintenance for req in requests):
lines = ['ICDB 批量检查结果', '流程: 原生 Project Backup 加载检查;不执行 Repair、Restore 或 CBB 校验。']
lines = ['ICDB 批量检查结果', '流程: 原生 PCB 打开检查;不执行 Repair、Restore 或 CBB 校验。']
for req, result in zip(requests, results):
detail = _report_field(result.stderr or result.stdout.strip()[:200])
seconds = ' '.join(f'{key}={value:.3f}s' +
+87
View File
@@ -0,0 +1,87 @@
from threading import Event, Thread
import time
def test_native_queue_is_fifo_and_waiting_cancel_never_runs_action():
from cbb_refresh_verify.operation_queue import NativeQueue, QueueCancelled
queue = NativeQueue()
holding, release, waiting, cancelled = Event(), Event(), Event(), Event()
order = []
first = Thread(target=lambda: queue.run(lambda: (holding.set(), release.wait(3), order.append('first'))))
first.start(); assert holding.wait(1)
def second():
try: queue.run(lambda: order.append('cancelled'), stop=cancelled, waiting=waiting.set)
except QueueCancelled: pass
second_thread = Thread(target=second); second_thread.start(); assert waiting.wait(1)
cancelled.set(); second_thread.join(1); assert not second_thread.is_alive()
third = Thread(target=lambda: queue.run(lambda: order.append('third')))
third.start(); release.set(); first.join(2); third.join(2)
assert order == ['first', 'third']
def test_background_scan_keeps_ui_usable_and_yields_between_projects(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
started, release = Event(), Event()
order = []
def verify(request):
order.append(request.block_name)
if request.block_name == 'Beta':
started.set(); release.wait(3)
return VerifyResult('CHECKED')
window = load_window(qtbot, tmp_path, monkeypatch, verifier=verify)
window._run(['Beta', 'Gamma'], maintenance=True)
try:
qtbot.waitUntil(started.is_set)
assert not window.busy and window.search_edit.isEnabled() and window.load_button.isEnabled()
window.search_edit.setText('Gamma')
assert [r.name for r in window.visible_records()] == ['Gamma']
window._start(lambda: order.append('foreground'), lambda _: None)
qtbot.wait(30)
assert order == ['Beta']
release.set()
qtbot.waitUntil(lambda: not window.icdb_task.active and not window.busy)
assert order == ['Beta', 'foreground', 'Gamma']
assert window.search_edit.text() == 'Gamma'
assert window.icdb_repair_button.isEnabled()
finally:
release.set()
def test_unconfirmed_open_is_amber_and_does_not_claim_icdb_damage(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: VerifyResult('FAIL',
error_category='DATABASE_SAFETY_STOP', stderr='Continue to open read/write?'))
window._run(['Beta'], maintenance=True)
qtbot.waitUntil(lambda: not window.icdb_task.active)
cell = window.cbb_table.item(window._row_by_name['Beta'], 3)
assert '未确认' in cell.text() and 'ICDB 错误' not in cell.text()
assert cell.background().color().name() == '#fff8e1'
def test_completed_old_library_result_does_not_release_new_library(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
from pathlib import Path
window = load_window(qtbot, tmp_path, monkeypatch, verifier=lambda _: VerifyResult('FAIL', safe_to_continue=False))
window._run(['Beta'], maintenance=True)
qtbot.waitUntil(lambda: not window._icdb_active())
summary = window.icdb_summary
window.lmc_edit.setText('other.lmc')
next(button for button in summary.buttons() if '释放' in button.text()).click()
assert getattr(window, 'release_dialog', None) is None
assert '切' in window.status_label.text()
def test_later_management_invalidates_old_recovery_offer(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from test_reimport_recovery import failed_repair
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: failed_repair(tmp_path/'old/result.json', safe=True))
window._run(['Beta'], maintenance=True)
qtbot.waitUntil(lambda: not window._icdb_active())
action = next(b for b in window.icdb_summary.buttons() if '副本' in b.text())
assert action.isEnabled()
window._invalidate_check_targets('test.lmc', ['Beta'])
assert not action.isEnabled() and not window._reimport_offers
+122
View File
@@ -0,0 +1,122 @@
"""Background check and foreground queue interaction regressions."""
from threading import Event
import pytest
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
def background(window, qtbot):
started, release = Event(), Event()
calls=[]
def verify(request):
calls.append(request.block_name)
if request.block_name=='Beta':
started.set()
assert release.wait(5)
return VerifyResult('CHECKED')
window.verifier=verify
window._run(['Beta','Gamma'],maintenance=True)
qtbot.waitUntil(started.is_set)
return release,calls
def finish(window, qtbot, release):
release.set()
window.icdb_task.resume()
qtbot.waitUntil(lambda:not window._icdb_active() and not window.busy)
qtbot.waitUntil(lambda:window.pool.activeThreadCount()==0)
def test_stop_whole_queue_does_not_resume_background_checks(qtbot,tmp_path,monkeypatch):
window=load_window(qtbot,tmp_path,monkeypatch)
release,calls=background(window,qtbot)
try:
window._start(lambda:None,lambda _:None)
qtbot.waitUntil(lambda:bool(window.native_queue.pending))
window.stop_button.click()
release.set()
qtbot.waitUntil(lambda:not window._icdb_active() and not window.busy)
assert calls==['Beta'],f'Cancelled queue still ran: {calls}'
finally:finish(window,qtbot,release)
def test_check_progress_survives_queued_verify(qtbot,tmp_path,monkeypatch):
window=load_window(qtbot,tmp_path,monkeypatch,
status_refresher=lambda names,path:{name:'PENDING' for name in names})
release,calls=background(window,qtbot)
try:
assert not window.icdb_progress.isHidden()
window._run(['Gamma'])
assert not window.icdb_progress.isHidden(),'Queued verify hid the active check progress'
finally:finish(window,qtbot,release)
def test_release_failure_is_not_labelled_as_read_write_prompt(qtbot,tmp_path,monkeypatch):
window=load_window(qtbot,tmp_path,monkeypatch,verifier=lambda request:VerifyResult(
'FAIL',error_category='DATABASE_SAFETY_STOP',safe_to_continue=False,
stderr='PCB_RELEASE_UNCONFIRMED: launcher still running'))
window._run(['Beta'],maintenance=True)
qtbot.waitUntil(lambda:not window._icdb_active())
cell=window.cbb_table.item(window._row_by_name['Beta'],3)
assert '读写确认' not in cell.text(),cell.text()
def test_pending_check_skips_a_target_that_has_become_ready(qtbot,tmp_path,monkeypatch):
window=load_window(qtbot,tmp_path,monkeypatch)
release,calls=background(window,qtbot)
try:
window.icdb_task.pause()
release.set()
qtbot.waitUntil(lambda:len(window.icdb_task.results)==1 and not window.icdb_task.running)
window._apply_refreshed_records((('Gamma',),{'Gamma':'READY'},{},''))
assert 'Gamma' in window.ready_names
window.icdb_task.resume()
qtbot.waitUntil(lambda:not window._icdb_active())
assert calls==['Beta'],f'Already READY target was opened again: {calls}'
finally:finish(window,qtbot,release)
def test_worker_refresh_excludes_ready_before_any_gui_result_callback(qtbot,tmp_path,monkeypatch):
window=load_window(qtbot,tmp_path,monkeypatch,
status_refresher=lambda names,path:{name:'READY' for name in names})
release,calls=background(window,qtbot)
try:
window._start(lambda:window._read_affected_records(('Gamma',)),lambda payload:None)
qtbot.waitUntil(lambda:bool(window.native_queue.pending))
release.set()
qtbot.waitUntil(lambda:not window._icdb_active() and not window.busy)
assert window._records_by_name['Gamma'].status=='UNKNOWN'
assert calls==['Beta']
assert window.icdb_task.results[1].status=='SKIPPED'
assert 'READY' in window.icdb_task.results[1].stderr
finally:finish(window,qtbot,release)
def test_another_library_ready_status_cannot_skip_this_library_target(qtbot,tmp_path,monkeypatch):
window=load_window(qtbot,tmp_path,monkeypatch)
release,calls=background(window,qtbot)
try:
window._publish_check_statuses('other.lmc',{'Gamma':'READY'})
release.set()
qtbot.waitUntil(lambda:not window._icdb_active())
assert calls==['Beta','Gamma']
finally:finish(window,qtbot,release)
@pytest.mark.parametrize('detail,safe,expected',[
('Continue to open the design with read/write access permission?',True,'读写确认'),
('PCB_RELEASE_UNCONFIRMED: launcher still running',False,'释放未确认'),
('ICDB_RELEASE_UNCONFIRMED: database still occupied',False,'释放未确认'),
('PCB_SESSION_ACTIVE: target already open',True,'会话或数据库状态待确认'),
])
def test_safety_stop_label_tracks_actual_reason(qtbot,tmp_path,monkeypatch,detail,safe,expected):
window=load_window(qtbot,tmp_path,monkeypatch,verifier=lambda request:VerifyResult(
'FAIL',error_category='DATABASE_SAFETY_STOP',safe_to_continue=safe,stderr=detail))
window._run(['Beta'],maintenance=True)
qtbot.waitUntil(lambda:not window._icdb_active())
cell=window.cbb_table.item(window._row_by_name['Beta'],3)
assert expected in cell.text()
assert cell.toolTip()==detail
assert cell.background().color().name()=='#fff8e1'
+54
View File
@@ -152,6 +152,60 @@ def test_idle_guard_queries_supported_native_process_names(recovery_files, monke
assert any("Name='viewdraw.exe'" in query for query in queries)
def test_idle_guard_ignores_unrelated_project_and_prefix_sibling(recovery_files, monkeypatch):
from cbb_refresh_verify.backup_recovery import _idle_check
from cbb_refresh_verify import mentor_adapter
request, original, _ = recovery_files
unrelated = original.parent.with_name(original.parent.name+'Old')/'PCB/A.pcb'
monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: [(123, '', str(unrelated))])
monkeypatch.setattr(mentor_adapter, '_process_details', lambda name:
[(123, 'ExpeditionPCB.exe', f'ExpeditionPCB.exe "{unrelated}" -ReusableCircuit', 0)]
if name == 'ExpeditionPCB.exe' else [])
_idle_check(request.lmc_file, original, original.parent/'PCB/A.pcb')
def test_idle_guard_rejects_exact_target_process(recovery_files, monkeypatch):
from cbb_refresh_verify.backup_recovery import _idle_check
from cbb_refresh_verify import mentor_adapter
request, original, _ = recovery_files
target = original.parent/'PCB/A.pcb'
monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: [])
monkeypatch.setattr(mentor_adapter, '_process_details', lambda name:
[(123, 'ExpeditionPCB.exe', f'ExpeditionPCB.exe "{target}" -ReusableCircuit', 0)]
if name == 'ExpeditionPCB.exe' else [])
with pytest.raises(ValueError, match='TARGET_NATIVE_OWNER_ACTIVE'):
_idle_check(request.lmc_file, original, target)
def test_prelaunch_recovery_rejection_has_no_unreleased_session(recovery_files):
from cbb_refresh_verify.backup_recovery import recover_from_backup
request, _, source = recovery_files
def reject(*args): raise ValueError('target unavailable before launch')
result = recover_from_backup(request, source, idle_check=reject,
verifier=lambda *a, **k: pytest.fail('prelaunch rejection must not start native'))
assert result.status == 'BLOCKED'
assert result.safe_to_continue
def test_report_write_failure_does_not_lose_prelaunch_release_fact(recovery_files, monkeypatch):
from cbb_refresh_verify.backup_recovery import recover_from_backup
import win32event
from uuid import uuid4
create_mutex = win32event.CreateMutex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
create_mutex(security, owned, name+'-test-'+uuid4().hex))
request, _, source = recovery_files
original_write = Path.write_text
def write(path, *args, **kwargs):
if path.name == 'recovery-result.json': raise OSError('report disk unavailable')
return original_write(path, *args, **kwargs)
monkeypatch.setattr(Path, 'write_text', write)
def reject(*args): raise ValueError('prelaunch unavailable')
result = recover_from_backup(request, source, idle_check=reject)
assert result.status == 'BLOCKED' and result.safe_to_continue
assert 'report disk unavailable' in result.stderr
def test_copy_refuses_directory_junction_before_copying(tmp_path):
import subprocess
from cbb_refresh_verify.backup_recovery import _copy_verified
+2 -2
View File
@@ -99,7 +99,7 @@ def test_timeout_isolated_and_later_item_continues(tmp_path):
assert records[1].status == "PENDING"
def test_protocol_path_mismatch_stops_repeating_same_global_failure(tmp_path):
def test_protocol_path_mismatch_records_each_queried_item(tmp_path):
class BrokenProtocolBridge:
def __init__(self):
self.requests = []
@@ -116,7 +116,7 @@ def test_protocol_path_mismatch_stops_repeating_same_global_failure(tmp_path):
['A', 'B', 'C'], lmc, tmp_path / 'scan.jsonl'
)
assert bridge.requests == ['A']
assert bridge.requests == ['A', 'B', 'C']
assert [record.status for record in records] == ['MALFORMED'] * 3
+91
View File
@@ -0,0 +1,91 @@
import pytest
def test_constructor_releases_private_desktop_when_process_launch_fails(monkeypatch):
import cbb_refresh_verify.desktop_worker as module
class Handle:
def __init__(self):
self.closed = False
def Close(self):
self.closed = True
def CloseDesktop(self):
self.closed = True
desktop = Handle()
job = Handle()
monkeypatch.setattr(module.win32service, 'CreateDesktop', lambda *args: desktop)
monkeypatch.setattr(module.win32job, 'CreateJobObject', lambda *args: job)
monkeypatch.setattr(module.win32process, 'STARTUPINFO', lambda: type('Startup', (), {})())
monkeypatch.setattr(module.win32process, 'CreateProcess',
lambda *args: (_ for _ in ()).throw(RuntimeError('launch failed')))
with pytest.raises(RuntimeError, match='launch failed') as caught:
module.DesktopWorker(['worker.exe'], 'PrivateDesk')
assert desktop.closed is True
assert job.closed is True
assert caught.value.worker_launch_release == {
'process_created': False, 'safe_to_continue': True, 'worker_exited': True, 'remaining_pids': []}
def test_partial_launch_cleanup_never_claims_native_descendants_released(monkeypatch):
import cbb_refresh_verify.desktop_worker as module
class Handle:
def Close(self): pass
def CloseDesktop(self): pass
class Thread:
def Close(self): raise OSError('thread handle cleanup failed')
monkeypatch.setattr(module.win32service, 'CreateDesktop', lambda *args: Handle())
monkeypatch.setattr(module.win32job, 'CreateJobObject', lambda *args: Handle())
monkeypatch.setattr(module.win32process, 'STARTUPINFO', lambda: type('Startup', (), {})())
monkeypatch.setattr(module.win32process, 'CreateProcess', lambda *args: (Handle(), Thread(), 123, 456))
terminated = []
monkeypatch.setattr(module.win32process, 'TerminateProcess', lambda *args: terminated.append(args))
with pytest.raises(OSError, match='thread handle cleanup failed') as caught:
module.DesktopWorker(['worker.exe'], 'PrivateDesk')
assert caught.value.worker_launch_release == {
'process_created': True, 'safe_to_continue': False, 'worker_exited': False, 'remaining_pids': [123]}
assert len(terminated) == 1
def test_release_probe_allows_measured_owner_helper_startup_time(monkeypatch):
import cbb_refresh_verify.desktop_worker as module
from cbb_refresh_verify import owner_probe
worker = module.DesktopWorker.__new__(module.DesktopWorker)
worker.pid = 123
worker._closed = True
worker._exit_code = 1
def query(names, *, timeout_seconds):
# Real helper measured ~4.75s including interpreter + COM startup.
if timeout_seconds < 5:
raise RuntimeError('OWNER_QUERY_TIMEOUT')
return []
monkeypatch.setattr(owner_probe, 'query_owners', query)
assert worker.automation_release_state() == {'safe_to_continue': True, 'remaining_pids': []}
def test_library_flag_loads_ui_dependencies_before_creating_private_desktop(monkeypatch):
import builtins
import win32service
import cbb_refresh_verify.mentor_adapter as module
created = []
original_import = builtins.__import__
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(win32service, 'CreateDesktop',
lambda *args: created.append(args))
with pytest.raises(ImportError, match='pywinauto unavailable'):
module.read_library_flag('library.lmc', 'Block')
assert created == []
+338
View File
@@ -0,0 +1,338 @@
import importlib
import importlib.util
from pathlib import Path
import pytest
@pytest.fixture
def diagnostic():
assert importlib.util.find_spec('cbb_refresh_verify.diagnostic_open') is not None, 'diagnostic open module is missing'
return importlib.import_module('cbb_refresh_verify.diagnostic_open')
@pytest.fixture
def job(tmp_path):
project = tmp_path / 'IP101GR'
pcb = project / 'PCB/IP101GR.pcb'
pcb.parent.mkdir(parents=True)
pcb.write_bytes(b'pcb')
return {'project': str(project / 'IP101GR.prj'), 'pcb': str(pcb), 'root': str(tmp_path / 'evidence'),
'desktop': 'CbbDiagnosticTest', 'timeout': .03}
def write_logs(job, *, pid=42, foreign=False, success=True):
project = Path(job['project']).parent
logdir = Path(job['pcb']).parent / 'LogFiles'
logdir.mkdir(parents=True, exist_ok=True)
pcb = str(Path(job['pcb'])) if not foreign else str(project.parent / 'other/PCB/IP101GR.pcb')
(logdir / 'PCBAutoSave.txt').write_text('Job Name: ' + pcb + '\n', encoding='utf-8')
(logdir / 'PCBLogFile.txt').write_text('Successfully read the design.\n' if success else 'Loading\n', encoding='utf-8')
database = str(project / 'database') if not foreign else str(project.parent / 'other/database')
logs = project / 'database/cdbsvr/log/current'
logs.mkdir(parents=True, exist_ok=True)
(logs / 'current.txt').write_text(
f'S: Project path [{database}] on file system [NTFS]\n'
f'S: Project file (.prj) path [{project / "IP101GR.prj"}] on file system [NTFS]\n'
f'S: Started up automatically by iCDB Client: ABS:ABS:{pid}\n'
'S: iCDB database loaded\n'
f'SC[455]: Registered sub-client [Expedition PCB_123] with ID [1] for session ID [455] host [Abs] user [ABS] process ID [{pid}]\n',
encoding='utf-8')
def test_old_success_logs_are_not_current_open_evidence(diagnostic, job):
write_logs(job)
baseline = diagnostic.snapshot_diagnostic_logs(job)
assert diagnostic._load_evidence(job, 42, baseline) is None
@pytest.mark.parametrize('pid,foreign,success', [(99, False, True), (42, True, True), (42, False, False)])
def test_foreign_pid_path_or_incomplete_load_cannot_pass(diagnostic, job, pid, foreign, success):
baseline = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job, pid=pid, foreign=foreign, success=success)
assert diagnostic._load_evidence(job, 42, baseline) is None
def test_current_full_path_and_pcb_pid_logs_prove_open_only(diagnostic, job):
baseline = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
evidence = diagnostic._load_evidence(job, 42, baseline)
assert evidence['pcb'] == job['pcb']
assert evidence['pcb_pid'] == 42
def test_old_prefix_in_appended_log_cannot_supply_target_identity(diagnostic, job):
write_logs(job)
baseline = diagnostic.snapshot_diagnostic_logs(job)
for path in Path(job['project']).parent.rglob('*.txt'):
with path.open('a', encoding='utf-8') as output:
output.write('unrelated new record\n')
assert diagnostic._load_evidence(job, 42, baseline) is None
def frame(job, *, title='IP101GR'):
return {'hwnd': 10, 'pid': 42, 'class': 'Afx:123',
'text': f'Expedition PCB - Reusable Block - [{title}]', 'enabled': True,
'children': [{'hwnd': 11, 'class': 'MDIClient', 'text': '', 'parent': 10},
{'hwnd': 12, 'class': 'Afx:document', 'text': title, 'parent': 11}]}
def test_target_title_without_target_mdi_does_not_pass(diagnostic, job):
window = frame(job)
window['children'][1]['text'] = 'another_project'
assert diagnostic._target_frame([window], Path(job['pcb']).stem) is None
class Session:
instances = []
def __init__(self, job, pid, launcher):
self.alive = True
self.closed = False
self.posted = []
self.instances.append(self)
def pcb_alive(self):
return self.alive
def launcher_alive(self):
return self.alive
def post(self, hwnd, message):
self.posted.append((hwnd, message))
self.alive = False
def close(self):
self.closed = True
def test_unknown_prompt_is_preserved_and_never_closed(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job),
{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': 'Unknown question', 'children': []}])
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert Session.instances[-1].posted == []
assert Session.instances[-1].closed
def test_success_posts_one_exact_close_and_reports_only_open_check(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
outcome = diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert outcome['status'] == 'PASS'
assert outcome['check'] == 'native_open_only'
assert outcome['native_processes_exited'] is True
assert outcome['library_ownership'] == 'NOT_ASSESSED'
assert Session.instances[-1].posted == [(10, 0x10)]
assert Session.instances[-1].closed
def test_transient_startup_window_cannot_abort_before_target_is_loaded(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
calls = []
def windows(pid):
calls.append(True)
if len(calls) == 1:
return [{'hwnd': 20, 'pid': 42, 'class': 'VendorSplash', 'text': '', 'children': []}]
return [frame(job)] if Session.instances[-1].alive else []
monkeypatch.setattr(diagnostic, '_windows', windows)
job['timeout'] = 1
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
assert Session.instances[-1].posted == [(10, 0x10)]
def test_native_caption_variant_uses_proven_target_mdi(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
target = frame(job)
target['text'] = 'Expedition PCB - [IP101GR]'
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [target] if Session.instances[-1].alive else [])
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
def test_cancel_does_not_wait_for_open_timeout(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
cancel = Path(job['root']).parent / 'cancel'
cancel.touch()
job['cancel_signal'] = str(cancel)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [])
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED'):
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert Session.instances[-1].posted == []
assert Session.instances[-1].closed
def test_cancel_after_close_request_still_waits_for_own_launcher_exit(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
cancel = Path(job['root']).parent / 'cancel'
job['cancel_signal'] = str(cancel)
class DelayedLauncher(Session):
polls = 0
def post(self, hwnd, message):
super().post(hwnd, message)
cancel.touch()
def launcher_alive(self):
self.polls += 1
return self.polls < 3
monkeypatch.setattr(diagnostic, '_NativeSession', DelayedLauncher)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
assert Session.instances[-1].polls >= 3
@pytest.mark.parametrize('owner,window_class', [(10, 'VendorModal'), (0, 'VendorModal'),
(0, 'Internet Explorer_Hidden')])
def test_unknown_custom_owned_window_blocks_close(diagnostic, job, monkeypatch, owner, window_class):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job),
{'hwnd': 20, 'pid': 42, 'class': window_class, 'text': 'Unclassified prompt',
'owner': owner, 'enabled': True, 'children': []}])
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert Session.instances[-1].posted == []
def test_empty_desktop_resets_stale_win32_last_error(diagnostic, monkeypatch):
import win32api
import win32gui
import pywintypes
seen = []
def empty(callback, context):
seen.append(win32api.GetLastError())
raise pywintypes.error(0, 'EnumWindows', 'empty desktop')
monkeypatch.setattr(win32gui, 'EnumWindows', empty)
win32api.SetLastError(170)
assert diagnostic._windows(42) == []
assert seen == [0]
def test_missing_launcher_cannot_claim_verified_native_release(diagnostic, job):
with pytest.raises(RuntimeError, match='DIAGNOSTIC_LAUNCHER_REQUIRED'):
diagnostic._NativeSession(job, 42, None)
def test_buffered_full_pcb_path_must_be_verified_after_normal_close(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
autosave = Path(job['pcb']).parent / 'LogFiles/PCBAutoSave.txt'
autosave.write_text('', encoding='utf-8')
class BufferedSession(Session):
def post(self, hwnd, message):
super().post(hwnd, message)
autosave.write_text('Job Name: ' + job['pcb'] + '\n', encoding='utf-8')
monkeypatch.setattr(diagnostic, '_NativeSession', BufferedSession)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
outcome = diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert outcome['status'] == 'PASS'
assert Session.instances[-1].posted == [(10, 0x10)]
def test_large_unrelated_history_does_not_block_current_evidence(diagnostic, job):
logs = Path(job['project']).parent / 'database/cdbsvr/log'
for index in range(270):
directory = logs / f'2000-01-01 00.00.00-{index:05}'
directory.mkdir(parents=True)
(directory / '2000.01.01.txt').write_bytes(b'old unrelated history\n' * 14000)
baseline = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
assert diagnostic._load_evidence(job, 42, baseline) is not None
def test_pcb_logs_follow_actual_pcb_directory(diagnostic, job):
pcb = Path(job['pcb']).parent.parent / 'AlternativePCB/IP101GR.pcb'
pcb.parent.mkdir()
pcb.write_bytes(b'pcb')
job['pcb'] = str(pcb)
baseline = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
assert diagnostic._load_evidence(job, 42, baseline)['pcb'] == str(pcb)
def test_only_exact_owned_save_prompt_gets_no_after_diagnostic_close(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
class SaveSession(Session):
closing = False
def post(self, hwnd, message):
self.posted.append((hwnd, message))
if message == 0x10:
self.closing = True
else:
self.alive = False
closing_scans = []
def windows(pid):
state = Session.instances[-1]
if not state.closing:
return [frame(job)]
closing_scans.append(True)
if len(closing_scans) == 1:
return [{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': '',
'owner': 10, 'children': []}]
return [{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': 'Expedition PCB',
'owner': 10, 'children': [
{'hwnd': 30, 'class': 'Static', 'text': 'Do you want to save the reusable block cell?', 'id': -1},
{'hwnd': 31, 'class': 'Button', 'text': '&No', 'id': 7}]}]
monkeypatch.setattr(diagnostic, '_NativeSession', SaveSession)
monkeypatch.setattr(diagnostic, '_windows', windows)
outcome = diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert outcome['status'] == 'PASS'
assert Session.instances[-1].posted == [(10, 0x10), (31, 0xF5)]
def test_no_pass_when_normal_close_never_flushes_full_pcb_path(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
(Path(job['pcb']).parent / 'LogFiles/PCBAutoSave.txt').write_bytes(b'')
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
with pytest.raises(RuntimeError, match='fresh full PCB path not confirmed after close'):
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert Session.instances[-1].posted == [(10, 0x10)]
@pytest.mark.parametrize('filename,accepted', [('IP101GR.pcb', True), ('Foreign.pcb', False)])
def test_standard_save_question_requires_exact_target_filename(diagnostic, job, monkeypatch, filename, accepted):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
class SaveSession(Session):
closing = False
def post(self, hwnd, message):
self.posted.append((hwnd, message))
if message == 0x10:
self.closing = True
else:
self.alive = False
def windows(pid):
if not Session.instances[-1].closing:
return [frame(job)]
return [{'hwnd': 20, 'pid': 42, 'class': '#32770', 'text': 'Expedition PCB', 'owner': 10,
'children': [{'hwnd': 30, 'class': 'Static', 'text': f'Save changes to {filename}?', 'id': -1},
{'hwnd': 31, 'class': 'Button', 'text': '否(&N)', 'id': 7},
{'hwnd': 32, 'class': 'Button', 'text': '是(&Y)', 'id': 6},
{'hwnd': 33, 'class': 'Button', 'text': '取消', 'id': 2}]}]
monkeypatch.setattr(diagnostic, '_NativeSession', SaveSession)
monkeypatch.setattr(diagnostic, '_windows', windows)
if accepted:
assert diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)['status'] == 'PASS'
assert Session.instances[-1].posted == [(10, 0x10), (31, 0xF5)]
else:
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
diagnostic.check_target_window(job, 42, emit=lambda **event: None, error_reader=lambda: None)
assert Session.instances[-1].posted == [(10, 0x10)]
def test_recycled_window_from_another_pid_never_receives_close(diagnostic, monkeypatch):
import win32process
session = diagnostic._NativeSession.__new__(diagnostic._NativeSession)
session.pid = 42
session.pcb_alive = lambda: True
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 99))
with pytest.raises(RuntimeError, match='DIAGNOSTIC_STALE_WINDOW'):
session.post(10, 0x10)
+74 -1
View File
@@ -412,6 +412,79 @@ def test_agent_start_failure_releases_listener(tmp_path):
DialogAgentClient.start(FailedWorker(), tmp_path)
def _slow_start_client(tmp_path, monkeypatch, *, delay, stop=None):
import socket
from types import SimpleNamespace
from cbb_refresh_verify import dialog_agent as module
client = DialogAgentClient.prepare(tmp_path, desktop_name='test')
client.listener.close()
elapsed = [0.0]
class Listener:
closed = False
def settimeout(self, timeout): self.timeout = timeout
def accept(self):
if elapsed[0] < delay:
elapsed[0] += self.timeout
if stop is not None and elapsed[0] >= .4: stop.set()
raise socket.timeout()
return SimpleNamespace(setsockopt=lambda *a: None), None
def close(self): self.closed = True
class Channel:
def __init__(self, connection): pass
def receive(self, timeout): return {'token': client.paths.token}
def send(self, *a): pass
def close(self): pass
client.listener = Listener()
monkeypatch.setattr(module, 'JsonChannel', Channel)
monkeypatch.setattr(module.time, 'monotonic', lambda: elapsed[0])
monkeypatch.setattr(DialogAgentClient, 'prepare', classmethod(lambda cls, *a, **k: client))
worker = SimpleNamespace(name='test', start_companion=lambda command: None)
return client, worker, elapsed
def test_frozen_agent_can_initialize_after_the_old_ten_second_deadline(tmp_path, monkeypatch):
client, worker, elapsed = _slow_start_client(tmp_path, monkeypatch, delay=12)
assert DialogAgentClient.start(worker, tmp_path) is client
assert elapsed[0] >= 12 and client.channel is not None
assert client.timeout_seconds == 2 # Normal IPC does not inherit the startup budget.
def test_agent_startup_wait_responds_to_cancel_without_starting_pcb(tmp_path, monkeypatch):
import pytest
from threading import Event
stop = Event()
client, worker, elapsed = _slow_start_client(tmp_path, monkeypatch, delay=100, stop=stop)
with pytest.raises(InterruptedError, match='VERIFY_CANCELLED'):
DialogAgentClient.start(worker, tmp_path, stop=stop)
assert elapsed[0] < 1 and client.listener.closed
def test_agent_startup_still_has_an_absolute_deadline(tmp_path, monkeypatch):
import pytest
client, worker, elapsed = _slow_start_client(tmp_path, monkeypatch, delay=100)
with pytest.raises(TimeoutError):
DialogAgentClient.start(worker, tmp_path)
assert 29 <= elapsed[0] <= 31 and client.listener.closed
def test_cancellation_during_authenticated_handshake_never_releases_startup(tmp_path, monkeypatch):
import pytest
from threading import Event
from cbb_refresh_verify import dialog_agent as module
stop = Event()
client, worker, _ = _slow_start_client(tmp_path, monkeypatch, delay=0)
class Channel:
def __init__(self, connection): pass
def receive(self, timeout):
stop.set()
return {'token': client.paths.token}
def close(self): pass
monkeypatch.setattr(module, 'JsonChannel', Channel)
with pytest.raises(InterruptedError, match='VERIFY_CANCELLED'):
DialogAgentClient.start(worker, tmp_path, stop=stop)
assert client.channel is None and client.listener.closed
def test_memory_channel_timeout_is_bounded_and_partial_packets_survive():
import socket
import pytest
@@ -436,7 +509,7 @@ def test_loopback_connection_rejects_wrong_agent_token(tmp_path):
import socket
import pytest
from cbb_refresh_verify.dialog_agent import JsonChannel, DialogAgentProtocolError
client = DialogAgentClient.prepare(tmp_path, desktop_name='test', timeout_seconds=.3)
client = DialogAgentClient.prepare(tmp_path, desktop_name='test', timeout_seconds=2)
wrong = JsonChannel(socket.create_connection(('127.0.0.1', client.paths.port), timeout=1))
try:
wrong.send({'token': 'not-the-agent'})
+12
View File
@@ -86,6 +86,18 @@ def test_probe_keeps_visible_non_dialog_classes():
assert snapshots[0].process_name == "proc-1010.exe"
def test_pid_scope_excludes_foreign_dialog_before_child_or_uia_read():
gui = FakeGui({10: {'class_name': '#32770', 'text': 'owned'},
20: {'class_name': '#32770', 'text': 'other project'}})
class Uia:
def controls(self, hwnd):
assert hwnd == 10, 'Foreign project was probed'
return ()
probe = make_probe(gui, (20, 10), Uia())
assert [w.hwnd for w in probe.capture(PHASE, pids={1010})] == [10]
assert probe.capture(PHASE, pids=set()) == ()
def test_disappearing_window_is_skipped_without_hiding_other_windows():
gui = FakeGui({
10: {"class_name": "GXWND", "text": "gone"},
+31
View File
@@ -0,0 +1,31 @@
import json
from types import SimpleNamespace
import pytest
@pytest.mark.parametrize('cancel,gate_present', [(False, False), (True, False), (True, True)])
def test_worker_waits_for_cold_agent_but_cancellation_never_opens_pcb(tmp_path, monkeypatch, cancel, gate_present):
from cbb_refresh_verify import mentor_adapter as adapter
import pythoncom
gate, stop = tmp_path/'start.signal', tmp_path/'cancel.signal'
if gate_present: gate.touch()
if cancel: stop.touch()
clock, opened = [0.0], []
def advance(seconds):
clock[0] += seconds
if not cancel and clock[0] >= 20: gate.touch()
monkeypatch.setattr(adapter, 'time', SimpleNamespace(monotonic=lambda: clock[0], sleep=advance,
strftime=lambda *_: 'test'))
monkeypatch.setattr(pythoncom, 'CoInitialize', lambda: None)
monkeypatch.setattr(pythoncom, 'CoUninitialize', lambda: None)
monkeypatch.setattr(adapter, 'execute_job', lambda *args: opened.append(True) or {'status': 'PASS'})
job = tmp_path/'request.json'
job.write_text(json.dumps({'root': str(tmp_path), 'block_name': 'fixture',
'start_gate': str(gate), 'cancel_signal': str(stop)}), encoding='utf-8')
result = adapter.worker_main(job)
assert result == (1 if cancel else 0)
assert opened == ([] if cancel else [True])
if cancel:
outcome = json.loads((tmp_path/'outcome.json').read_text('utf-8'))
assert 'VERIFY_CANCELLED' in outcome['stderr']
+2 -1
View File
@@ -72,7 +72,8 @@ def test_serial_guard_covers_preflight_and_content_check(tmp_path, monkeypatch):
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 second.safe_to_continue
assert second.error_category == 'NATIVE_CHANNEL_BUSY'
assert calls == ['first']
finally:
release.set()
+95 -4
View File
@@ -50,7 +50,7 @@ def test_failed_startup_normally_closes_owned_empty_pcb(tmp_path, monkeypatch, t
@pytest.mark.parametrize('obstacle', ['baseline', 'wrong_path', 'path_prefix', 'desktop', 'address',
'dialog', 'document_title', 'mdi_document', 'disabled', 'no_diagnosis'])
'dialog', 'document_title', 'mdi_document', 'disabled'])
def test_failed_startup_preserves_unconfirmed_or_busy_session(tmp_path, monkeypatch, obstacle):
import win32gui
from cbb_refresh_verify import native_repair_ui
@@ -79,6 +79,97 @@ def test_failed_startup_preserves_unconfirmed_or_busy_session(tmp_path, monkeypa
assert not messages
def test_owned_empty_shell_closes_without_an_icdb_diagnosis(tmp_path, monkeypatch):
job, records, messages = startup_fixture(tmp_path, monkeypatch)
(tmp_path/'diagnostics/icdb-recovery.json').unlink()
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
assert result['status'] == 'CLOSED' and not records and len(messages) == 1
def read_write_fixture(tmp_path, monkeypatch):
import win32gui, win32con
from cbb_refresh_verify.post_repair_consent import PROMPT_TEXT
job, records, messages = startup_fixture(tmp_path, monkeypatch)
(tmp_path/'diagnostics/icdb-recovery.json').unlink()
visible = {100, 200}
classes = {100: 'Afx:main', 200: '#32770', 201: 'Button', 202: 'Button', 203: 'Static'}
names = {100: 'Expedition PCB', 200: 'Expedition PCB', 201: 'Yes', 202: 'No', 203: PROMPT_TEXT}
identifiers = {201: 6, 202: 7, 203: 65535}
monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(h, arg) for h in tuple(visible)])
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, fn, arg:
[fn(c, arg) for c in (201, 202, 203)] if h == 200 else None)
monkeypatch.setattr(win32gui, 'GetClassName', classes.__getitem__)
monkeypatch.setattr(win32gui, 'GetWindowText', names.__getitem__)
monkeypatch.setattr(win32gui, 'GetDlgCtrlID', identifiers.__getitem__)
monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda h: h != 100 or 200 not in visible)
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda h: h in visible or h in identifiers)
def post(hwnd, message, *_):
messages.append((hwnd, message))
if hwnd == 202 and message == win32con.BM_CLICK:
visible.remove(200)
elif hwnd == 100 and message == win32con.WM_CLOSE:
records.clear()
monkeypatch.setattr(win32gui, 'PostMessage', post)
return job, records, messages, names, visible
def test_captured_read_write_question_is_declined_and_own_empty_shell_exits(tmp_path, monkeypatch):
import win32con
job, records, messages, _, _ = read_write_fixture(tmp_path, monkeypatch)
# A pre-existing address must not prevent cancelling our own attempted open.
address = tmp_path/'database/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
address.write_text('existing foreign or stale address')
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
assert result['status'] == 'CLOSED' and result['release']['safe_to_continue']
assert messages == [(202, win32con.BM_CLICK), (100, win32con.WM_CLOSE)]
assert not records and address.read_text() == 'existing foreign or stale address'
assert result['cancelled_prompt'] == 'read_write_access'
@pytest.mark.parametrize('obstacle', ['text', 'foreign_pid', 'baseline', 'wrong_path', 'desktop', 'stuck'])
def test_read_write_cancel_never_accepts_unconfirmed_or_unclosed_prompt(tmp_path, monkeypatch, obstacle):
import win32gui, win32process, win32con
from cbb_refresh_verify import native_repair_ui
job, records, messages, names, _ = read_write_fixture(tmp_path, monkeypatch)
if obstacle == 'text': names[203] += ' Different recovery operation.'
if obstacle == 'foreign_pid':
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda h: (1, 99 if h == 200 else 12))
if obstacle == 'wrong_path': records[0] = (12, records[0][1], 'other.pcb -ReusableCircuit', 11)
if obstacle == 'desktop': monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'Default')
if obstacle == 'stuck':
monkeypatch.setattr(win32gui, 'PostMessage', lambda h, m, *_: messages.append((h, m)))
result = failed_session.close_failed_startup(job, 12,
baseline_pids=(12,) if obstacle == 'baseline' else (), timeout_seconds=0)
assert result['status'] == 'RETAINED' and records
assert messages == ([(202, win32con.BM_CLICK)] if obstacle == 'stuck' else [])
def test_version_choice_is_cancelled_without_selecting_or_confirming_a_version(tmp_path, monkeypatch):
import win32gui, win32con
from test_pcb_version_choice import captured
job, records, messages, _, visible = read_write_fixture(tmp_path, monkeypatch)
snapshot = captured()
children = [c for c in snapshot.controls if c.handle and c.handle != snapshot.hwnd]
mapping = {300+i: c for i, c in enumerate(children)}
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, fn, arg:
[fn(c, arg) for c in mapping] if h == 200 else None)
monkeypatch.setattr(win32gui, 'GetClassName', lambda h: mapping[h].class_name if h in mapping else
'#32770' if h == 200 else 'Afx:main')
monkeypatch.setattr(win32gui, 'GetWindowText', lambda h: mapping[h].name if h in mapping else
'Database Restoration' if h == 200 else 'Expedition PCB')
monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda h: mapping[h].control_id)
cancel = next(h for h, c in mapping.items() if c.control_id == 2)
def post(h, m, *_):
messages.append((h, m))
if h == cancel: visible.remove(200)
elif h == 100: records.clear()
monkeypatch.setattr(win32gui, 'PostMessage', post)
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
assert result['status'] == 'CLOSED' and result['cancelled_prompt'] == 'pcb_version_choice'
assert messages == [(cancel, win32con.BM_CLICK), (100, win32con.WM_CLOSE)]
def test_requested_close_without_exit_is_not_reported_closed(tmp_path, monkeypatch):
import win32gui
job, records, messages = startup_fixture(tmp_path, monkeypatch)
@@ -169,13 +260,13 @@ def test_failed_startup_accepts_current_owned_database_failure_dialog(tmp_path,
@pytest.mark.parametrize('pid,phase', [(99, 'PCB_STARTING'), (12, 'SAVING')])
def test_failed_startup_rejects_unrelated_failure_dialog(tmp_path, monkeypatch, pid, phase):
def test_unrelated_failure_record_is_not_needed_to_close_own_empty_shell(tmp_path, monkeypatch, pid, phase):
job, records, messages = startup_fixture(tmp_path, monkeypatch)
(tmp_path/'diagnostics/icdb-recovery.json').unlink()
(tmp_path/'dialogs.jsonl').write_text(json.dumps({
'pid': pid, 'phase': phase, 'error_code': 'MENTOR_DATABASE_LOAD_FAILED'})+'\n')
assert failed_session.close_failed_startup(job, 12, timeout_seconds=0)['status'] == 'RETAINED'
assert not messages
assert failed_session.close_failed_startup(job, 12, timeout_seconds=0)['status'] == 'CLOSED'
assert len(messages) == 1 and not records
@pytest.mark.parametrize('exits', [True, False])
+159 -194
View File
@@ -1,194 +1,159 @@
"""Batch ICDB inspection never opens PCB or dispatches Repair."""
from dataclasses import replace
import json
import pytest
from test_maintenance_entry import case, no_native_launch
from test_maintenance_diagnosis import inspection
@pytest.mark.parametrize('scenario,status,category,safe', [
('healthy', 'CHECKED', '', True),
('fault', 'FAIL', 'ICDB_REPAIR_REQUIRED', True),
('busy', 'BLOCKED', 'ICDB_DATABASE_IN_USE', True),
('unknown', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True),
('unexpected_repair', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True),
('missing_exit_proof', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', False),
('not_launched', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', True),
('unreleased', 'FAIL', 'ICDB_INSPECTION_UNCONFIRMED', False),
('cancelled', 'CANCELLED', '', True),
])
def test_batch_check_only_loads_native_inspection(case, monkeypatch, scenario, status, category, safe):
from cbb_refresh_verify import verify_runner as runner, native_repair, maintenance_entry as entry
from test_native_occupancy import output
request, _, address = case
calls = []
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('check opened PCB'))
monkeypatch.setattr(entry, 'assert_native_idle', lambda: pytest.fail('check used host-wide owner gate'))
def native(target, root, **kw):
assert kw['inspection_only'] is True and not kw.get('maintenance_context')
calls.append('inspect')
record = inspection(target, fault=scenario == 'fault')
if scenario == 'busy':
record.update(status='IN_USE', output=output(target.project, target.database, busy=True))
if scenario in ('unknown', 'unreleased'):
record.update(status='UNCONFIRMED', output='No conclusive native response')
if scenario == 'unreleased': record['release']['native_exited'] = False
if scenario == 'cancelled': record.update(status='CANCELLED')
if scenario == 'unexpected_repair': record.update(status='REPAIRED', repair_requested=True)
if scenario == 'missing_exit_proof': return {}
if scenario == 'not_launched': return {'status': 'BLOCKED', 'native_started': False}
return record
monkeypatch.setattr(native_repair, 'run_native_repair', native)
result = runner.run_verify(replace(request, auto_repair=True))
assert calls == ['inspect']
assert (result.status, result.error_category, result.safe_to_continue) == (status, category, safe)
assert result.library_flag == 'UNKNOWN' and result.recovery_attempts == ()
assert address.read_bytes() == b'old server address - preserved'
record = json.loads(result.evidence_path.read_text('utf-8'))
assert record['operation'] == 'icdb_batch_check' and record['automatic_repair'] is False
@pytest.mark.parametrize('case_type', ['no_layout', 'no_pcb', 'no_icdb', 'ambiguous_repair_targets'])
def test_inspection_does_not_require_repair_admission(case, monkeypatch, case_type):
from cbb_refresh_verify import verify_runner as runner, native_repair
request, project, address = case
if case_type == 'no_layout': (project.parent/'PCB/Layout/LayoutDB.lyt').unlink()
if case_type == 'no_pcb': (project.parent/'PCB/IP101GR.pcb').unlink()
if case_type == 'no_icdb': (project.parent/'database/icdb.dat').unlink()
if case_type == 'ambiguous_repair_targets':
address.unlink()
(project.parent/'PCB/pcb.prj').write_text('KEY iCDBDir "database"\n')
db = project.parent/'PCB/database'; db.mkdir(); (db/'icdb.dat').touch()
calls = []
def native(target, *a, **kw):
assert kw['inspection_only'] is True and target.project == project
calls.append('inspect')
return inspection(target)
monkeypatch.setattr(native_repair, 'run_native_repair', native)
result = runner.run_verify(request)
assert calls == ['inspect']
assert result.recovery_attempts == ()
@pytest.mark.parametrize('before_launch', [True, False])
def test_cancellation_never_dispatches_repair_or_reports_a_clean_check(case, monkeypatch, before_launch):
from threading import Event
from cbb_refresh_verify import verify_runner as runner, native_repair
request, _, _ = case
stop = Event(); calls = []
if before_launch: stop.set()
def native(target, *a, **kw):
assert kw['inspection_only'] is True
calls.append('inspect'); stop.set()
return inspection(target)
monkeypatch.setattr(native_repair, 'run_native_repair', native)
result = runner.run_verify(request, stop=stop)
assert result.status == 'CANCELLED' and result.safe_to_continue
assert calls == ([] if before_launch else ['inspect'])
def test_released_icdb_fault_continues_the_check_batch(case, monkeypatch):
from cbb_refresh_verify import verify_runner as runner, native_repair
request, _, _ = case
calls = []
def native(target, root, **kw):
assert kw['inspection_only'] is True
calls.append('inspect')
return inspection(target, fault=len(calls) == 1)
monkeypatch.setattr(native_repair, 'run_native_repair', native)
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('check opened PCB'))
results = runner.run_batch([request, request])
assert calls == ['inspect', 'inspect']
assert [result.status for result in results] == ['FAIL', 'CHECKED']
def test_inspection_ignores_other_pcb_and_skips_exclusive_repair_probe(tmp_path, monkeypatch):
import win32con
from test_native_repair import repair_worker_fixture
from test_native_occupancy import output
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: [(999, 'other.pcb', '')])
monkeypatch.setattr('cbb_refresh_verify.native_repair.check_repair_access',
lambda *a: pytest.fail('inspection used exclusive Repair admission'))
monkeypatch.setattr(ui, '_repair_command', lambda *a: pytest.fail('inspection resolved Repair command'))
monkeypatch.setattr(ui, '_native_output', lambda *_: output(data['project'], data['database'], busy=True))
assert ui.repair_worker_main(job) == 0
assert actions == [(100, win32con.WM_CLOSE)]
def test_ui_check_summary_reports_findings_and_never_claims_repair(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
from PySide6.QtWidgets import QMessageBox
requests, prompts = [], []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (requests.append(req) or VerifyResult('FAIL',
error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (prompts.append(a[2]) or QMessageBox.Yes))
window.search_edit.setText('Beta')
window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy)
assert requests[0].maintenance and not requests[0].auto_repair
assert '批量检查' in window.icdb_repair_button.text()
assert not any('手动备份' in text for text in prompts)
assert window.summary.windowTitle() == 'ICDB 批量检查结果'
assert '发现问题 1' in window.summary.text()
assert '修复并校验通过' not in window.summary.detailedText()
@pytest.mark.parametrize('category,label', [('ICDB_DATABASE_IN_USE', '占用 1'),
('ICDB_INSPECTION_UNCONFIRMED', '无法确认 1')])
def test_busy_or_unknown_inspection_is_not_labelled_as_database_damage(qtbot, tmp_path, monkeypatch, category, label):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: VerifyResult('FAIL', error_category=category, stderr='暂未完成检查。'),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
window.search_edit.setText('Beta')
window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window.busy)
assert label in window.summary.text()
assert 'ICDB 问题' not in window.cbb_table.item(window._row_by_name['Beta'], 3).text()
def test_missing_pcb_is_skipped_from_icdb_batch_check_and_restore(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.models import CbbRecord
from cbb_refresh_verify.verify_runner import VerifyResult
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
records=(CbbRecord('Missing', 'Power', status='PENDING'),),
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('CHECKED')),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
assert not window.icdb_repair_button.isEnabled()
assert not window.batch_button.isEnabled() and not window.backup_restore_button.isEnabled()
assert calls == []
def test_missing_project_rows_are_excluded_from_icdb_batch_check(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.models import CbbRecord
from cbb_refresh_verify.verify_runner import VerifyResult
window = load_window(qtbot, tmp_path, monkeypatch,
records=(CbbRecord('Missing', 'Power', status='PENDING'),
CbbRecord('Good', 'Power', status='PENDING')),
verifier=lambda req: VerifyResult('CHECKED'),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
window.skipped_names.add('Missing')
window._update()
assert window._maintenance_names() == ['Good']
def test_check_reports_describe_inspection_without_pcb_verification(case, tmp_path):
from cbb_refresh_verify.verify_runner import write_batch_report, failure_report_path, VerifyResult
request, _, _ = case
report = write_batch_report([request], [VerifyResult('FAIL',
error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')], tmp_path/'reports')
full = report.read_text('utf-8-sig')
failed = failure_report_path(report).read_text('utf-8-sig')
assert 'ICDB 批量检查' in full and 'Project Integration' not in full
assert 'ICDB 检查异常' in failed and '发现问题: 1' in failed
assert 'ICDB已修复待校验' not in failed
"""Batch ICDB checks open diagnostically without dispatching Repair."""
from dataclasses import replace
import json
import pytest
from test_maintenance_entry import case, no_native_launch
@pytest.mark.parametrize('scenario,status,category,safe', [
('healthy', 'CHECKED', '', True),
('fault', 'FAIL', 'ICDB_REPAIR_REQUIRED', True),
('generic', 'FAIL', 'MENTOR_DATABASE_LOAD_FAILED', True),
('unreleased', 'FAIL', 'MENTOR_DATABASE_LOAD_FAILED', False),
('cancelled', 'CANCELLED', '', True),
])
def test_batch_check_opens_pcb_without_repair(case, monkeypatch, scenario, status, category, safe):
from cbb_refresh_verify import verify_runner as runner, native_repair, maintenance_entry as entry
from test_icdb_open_check import NATIVE_ERROR
request, _, address = case
calls = []
def open_pcb(probe, **kwargs):
assert probe.diagnostic_only and not probe.maintenance and not probe.auto_repair
calls.append('open')
return runner.VerifyResult('PASS' if scenario=='healthy' else 'CANCELLED' if scenario=='cancelled' else 'FAIL',
stderr=NATIVE_ERROR if scenario=='fault' else '' if scenario in ('healthy','cancelled') else 'Failed to open document.',
error_category='' if scenario in ('healthy','cancelled') else 'MENTOR_DATABASE_LOAD_FAILED',
safe_to_continue=scenario!='unreleased')
monkeypatch.setattr(runner,'_run_verify_serialized',open_pcb)
monkeypatch.setattr(native_repair,'run_native_repair',lambda *a,**k:pytest.fail('scan requested Project Backup'))
monkeypatch.setattr(entry,'assert_native_idle',lambda:pytest.fail('scan used host-wide owner gate'))
result=runner.run_verify(replace(request,auto_repair=True))
assert calls==['open']
assert (result.status,result.error_category,result.safe_to_continue)==(status,category,safe)
assert result.library_flag=='UNKNOWN' and result.recovery_attempts==()
assert address.read_bytes()==b'old server address - preserved'
record=json.loads(result.evidence_path.read_text('utf-8'))
assert record['operation']=='icdb_batch_check' and record['automatic_repair'] is False
@pytest.mark.parametrize('before_launch',[True,False])
def test_cancellation_never_dispatches_repair_or_reports_a_clean_check(case,monkeypatch,before_launch):
from threading import Event
from cbb_refresh_verify import verify_runner as runner
request,_,_=case
stop=Event();calls=[]
if before_launch:stop.set()
def open_pcb(probe,**kwargs):
calls.append('open');stop.set()
return runner.VerifyResult('PASS')
monkeypatch.setattr(runner,'_run_verify_serialized',open_pcb)
result=runner.run_verify(request,stop=stop)
assert result.status=='CANCELLED' and result.safe_to_continue
assert calls==([] if before_launch else ['open'])
def test_released_icdb_fault_continues_the_check_batch(case,monkeypatch):
from cbb_refresh_verify import verify_runner as runner
from test_icdb_open_check import NATIVE_ERROR
request,_,_=case
calls=[]
def open_pcb(probe,**kwargs):
calls.append('open')
return runner.VerifyResult('FAIL',stderr=NATIVE_ERROR) if len(calls)==1 else runner.VerifyResult('PASS')
monkeypatch.setattr(runner,'_run_verify_serialized',open_pcb)
results=runner.run_batch([request,request])
assert calls==['open','open']
assert [result.status for result in results]==['FAIL','CHECKED']
def test_inspection_ignores_other_pcb_and_skips_exclusive_repair_probe(tmp_path, monkeypatch):
import win32con
from test_native_repair import repair_worker_fixture
from test_native_occupancy import output
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: [(999, 'other.pcb', '')])
monkeypatch.setattr('cbb_refresh_verify.native_repair.check_repair_access',
lambda *a: pytest.fail('inspection used exclusive Repair admission'))
monkeypatch.setattr(ui, '_repair_command', lambda *a: pytest.fail('inspection resolved Repair command'))
monkeypatch.setattr(ui, '_native_output', lambda *_: output(data['project'], data['database'], busy=True))
assert ui.repair_worker_main(job) == 0
assert actions == [(100, win32con.WM_CLOSE)]
def test_ui_check_summary_reports_findings_and_never_claims_repair(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
from PySide6.QtWidgets import QMessageBox
requests, prompts = [], []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (requests.append(req) or VerifyResult('FAIL',
error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (prompts.append(a[2]) or QMessageBox.Yes))
window.search_edit.setText('Beta')
window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window._icdb_active())
assert requests[0].maintenance and not requests[0].auto_repair
assert '批量检查' in window.icdb_repair_button.text()
assert not any('手动备份' in text for text in prompts)
assert window.summary.windowTitle() == 'ICDB 批量检查结果'
assert '发现问题 1' in window.summary.text()
assert '修复并校验通过' not in window.summary.detailedText()
@pytest.mark.parametrize('category,label', [('ICDB_DATABASE_IN_USE', '占用 1'),
('ICDB_INSPECTION_UNCONFIRMED', '无法确认 1')])
def test_busy_or_unknown_inspection_is_not_labelled_as_database_damage(qtbot, tmp_path, monkeypatch, category, label):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: VerifyResult('FAIL', error_category=category, stderr='暂未完成检查。'),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
window.search_edit.setText('Beta')
window.icdb_repair_button.click(); qtbot.waitUntil(lambda: not window._icdb_active())
assert label in window.summary.text()
assert 'ICDB 问题' not in window.cbb_table.item(window._row_by_name['Beta'], 3).text()
def test_missing_pcb_is_skipped_from_icdb_batch_check_and_restore(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.models import CbbRecord
from cbb_refresh_verify.verify_runner import VerifyResult
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
records=(CbbRecord('Missing', 'Power', status='PENDING'),),
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('CHECKED')),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
assert not window.icdb_repair_button.isEnabled()
assert not window.batch_button.isEnabled() and not window.backup_restore_button.isEnabled()
assert calls == []
def test_missing_project_rows_are_excluded_from_icdb_batch_check(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.models import CbbRecord
from cbb_refresh_verify.verify_runner import VerifyResult
window = load_window(qtbot, tmp_path, monkeypatch,
records=(CbbRecord('Missing', 'Power', status='PENDING'),
CbbRecord('Good', 'Power', status='PENDING')),
verifier=lambda req: VerifyResult('CHECKED'),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
window.skipped_names.add('Missing')
window._update()
assert window._maintenance_names() == ['Good']
def test_check_reports_describe_inspection_without_pcb_verification(case, tmp_path):
from cbb_refresh_verify.verify_runner import write_batch_report, failure_report_path, VerifyResult
request, _, _ = case
report = write_batch_report([request], [VerifyResult('FAIL',
error_category='ICDB_REPAIR_REQUIRED', stderr='发现 ICDB 问题,未执行修复。')], tmp_path/'reports')
full = report.read_text('utf-8-sig')
failed = failure_report_path(report).read_text('utf-8-sig')
assert 'ICDB 批量检查' in full and 'Project Integration' not in full
assert 'PCB 打开检查' in full and 'Project Backup 加载检查' not in full
assert 'ICDB 检查异常' in failed and '发现问题: 1' in failed
assert 'ICDB已修复待校验' not in failed
+133
View File
@@ -0,0 +1,133 @@
"""PCB open errors identify ICDB findings by native content, never by code alone."""
from dataclasses import replace
import json
import pytest
from test_maintenance_entry import case, no_native_launch
NATIVE_ERROR = (
'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 [C:\\isolated\\Work\\database\\icdb.dat] in old, unsupported format)')
@pytest.mark.parametrize('message,expected', [
(NATIVE_ERROR, True),
('Unable to open iCDB connection.', True),
('iCDB database check FAILED', True),
('iCDB database check PASSED', False),
('0x8004021E: Failed to open document.', False),
('ICDB_REPAIR_REQUIRED: Failed to open document.', False),
('Cannot open [C:\\icdb\\icdb.dat].', False),
('PCB file was not found. Evidence: C:\\icdb\\result.json', False),
])
def test_error_content_requires_an_icdb_failure_message(message, expected):
from cbb_refresh_verify.icdb_recovery import icdb_error_content
assert bool(icdb_error_content(message)) is expected
def test_unknown_icdb_description_in_an_actual_open_error_is_retained():
from cbb_refresh_verify.icdb_recovery import icdb_error_content
assert icdb_error_content('iCDB connection: E_NEW_NATIVE_CODE', error_message=True)
@pytest.mark.parametrize('mode', ['append', 'rewrite', 'retained', 'unchanged'])
def test_native_error_content_is_from_this_open_attempt(tmp_path, mode):
from cbb_refresh_verify.icdb_recovery import snapshot_log, read_icdb_error
path = tmp_path/'PCBLogFile.txt'
previous = 'old header\n'+NATIVE_ERROR+'\n' if mode in ('retained','unchanged') else 'old header\n'
path.write_text(previous, 'utf-8')
baseline = snapshot_log(path)
if mode == 'append': path.write_text(previous+NATIVE_ERROR, 'utf-8')
elif mode == 'rewrite': path.write_text('new header\n'+NATIVE_ERROR, 'utf-8')
elif mode == 'retained': path.write_text('new header\n'+NATIVE_ERROR+'\nNormal startup', 'utf-8')
assert bool(read_icdb_error(path, baseline)) is (mode in ('append','rewrite'))
@pytest.mark.parametrize('failure,category', [(NATIVE_ERROR,'ICDB_REPAIR_REQUIRED'),
('0x8004021E: Failed to open document.','MENTOR_DATABASE_LOAD_FAILED')])
def test_batch_scan_opens_without_repair_and_classifies_native_content(case, monkeypatch, failure, category):
from cbb_refresh_verify import maintenance_entry as entry, verify_runner as runner, native_repair
request, _, address = case
probes=[]
monkeypatch.setattr(native_repair,'run_native_repair',lambda *a,**k:pytest.fail('scan dispatched Project Backup'))
def diagnose(probe, **kw):
probes.append(probe)
return runner.VerifyResult('FAIL',stderr=failure,error_category='MENTOR_DATABASE_LOAD_FAILED')
monkeypatch.setattr(runner,'run_verify',diagnose)
result=entry.run_icdb_check(replace(request,auto_repair=True))
assert len(probes)==1 and probes[0].diagnostic_only and not probes[0].maintenance
assert not probes[0].auto_repair and not probes[0].native_lm_verify
assert result.status=='FAIL' and result.error_category==category and result.safe_to_continue
assert result.library_flag=='UNKNOWN' and result.recovery_attempts==()
assert address.read_bytes()==b'old server address - preserved'
def test_scan_uses_the_current_open_evidence_even_when_popup_is_generic(case, monkeypatch):
from cbb_refresh_verify import maintenance_entry as entry, verify_runner as runner
request, _, _=case
def diagnose(probe, **kw):
root=probe.evidence_dir/'verify-current'; (root/'diagnostics').mkdir(parents=True)
(root/'diagnostics/icdb-error-content.json').write_text(json.dumps(
{'native_message':NATIVE_ERROR,'source':'PCBLogFile.txt'}),'utf-8')
result=root/'result.json'; result.write_text('{}','utf-8')
return runner.VerifyResult('FAIL',stderr='Problem encountered while opening one of the database files.',
evidence_path=result,error_category='MENTOR_DATABASE_LOAD_FAILED')
monkeypatch.setattr(runner,'run_verify',diagnose)
result=entry.run_icdb_check(request)
assert result.error_category=='ICDB_REPAIR_REQUIRED' and NATIVE_ERROR in result.stderr
def test_scan_does_not_reuse_an_older_operation_error_evidence(case, monkeypatch):
from cbb_refresh_verify import maintenance_entry as entry, verify_runner as runner
request,_,_=case
root=request.evidence_dir/'older-operation';(root/'diagnostics').mkdir(parents=True)
(root/'diagnostics/icdb-error-content.json').write_text(json.dumps({'native_message':NATIVE_ERROR}),'utf-8')
result=root/'result.json';result.write_text('{}','utf-8')
monkeypatch.setattr(runner,'run_verify',lambda *a,**k:runner.VerifyResult('FAIL',
stderr='Failed to open document.',error_category='MENTOR_DATABASE_LOAD_FAILED',evidence_path=result))
scanned=entry.run_icdb_check(request)
assert scanned.error_category=='MENTOR_DATABASE_LOAD_FAILED'
def test_error_capture_retains_the_new_native_corruption_detail(tmp_path):
from cbb_refresh_verify.icdb_recovery import snapshot_log
from cbb_refresh_verify.verify_diagnostics import collect_native_failure_logs
pcb=tmp_path/'PCB/CBB.pcb'; log=pcb.parent/'LogFiles/PCBLogFile.txt'
log.parent.mkdir(parents=True)
evidence=tmp_path/'evidence'; (evidence/'diagnostics').mkdir(parents=True)
baseline=snapshot_log(log)
(evidence/'diagnostics/startup-log-baseline.json').write_text(json.dumps(
{'source':str(log),'baseline':baseline}),'utf-8')
log.write_text(NATIVE_ERROR,'utf-8')
collect_native_failure_logs(pcb,evidence)
record=json.loads((evidence/'diagnostics/icdb-error-content.json').read_text('utf-8'))
assert record['source']==str(log) and record['native_message']==NATIVE_ERROR
@pytest.mark.parametrize('scenario', ['current', 'other_pid', 'old', 'other_database'])
def test_repeated_failure_uses_only_this_pcb_process_icdb_log(tmp_path, scenario):
import os,time
from cbb_refresh_verify.verify_diagnostics import collect_native_client_error
root=tmp_path/'evidence'; (root/'diagnostics').mkdir(parents=True)
logs=tmp_path/'Client/Logs'
pid=123
started=time.time_ns()-1_000_000_000
folder=logs/('2026-11-05 12.00.00-'+str(321 if scenario=='other_pid' else pid))
folder.mkdir(parents=True)
source=folder/'2026.11.05.iCDBManager.txt'
database=tmp_path/'Work/database'
message=f'SingleFile: ERROR: Database [{database}/icdb.dat] in old, unsupported format'
if scenario=='other_database': message=message.replace(str(database),'C:/other/database')
source.write_text(f'PID [{pid}]\n'+message,'utf-8')
if scenario=='old':os.utime(source,ns=(started-1_000_000_000,started-1_000_000_000))
(root/'diagnostics/native-client-context.json').write_text(json.dumps(
{'native_pid':pid,'started_ns':started,'client_log_root':str(logs),'database':str(database)}),'utf-8')
collect_native_client_error(root)
result=root/'diagnostics/icdb-error-content.json'
assert result.exists() is (scenario=='current')
if result.exists():
record=json.loads(result.read_text('utf-8'))
assert record['native_pid']==pid and 'unsupported format' in record['native_message']
+11 -13
View File
@@ -75,11 +75,15 @@ def test_loading_a_different_library_clears_old_management_block(qtbot, tmp_path
def test_targeted_native_idle_ignores_unrelated_native_command(tmp_path, monkeypatch):
from cbb_refresh_verify import maintenance_entry as entry
from cbb_refresh_verify.native_repair import RepairTarget
project = tmp_path/'target.prj'; project.touch()
work = tmp_path/'target/Work'; work.mkdir(parents=True)
library = tmp_path/'library.lmc'; library.touch()
project = work/'target.prj'
project.write_text(f'KEY CentralLibrary "{library}"\n', encoding='utf-8')
other_pcb = tmp_path/'other/Work/PCB/other.pcb'
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda names: [
{'pid': 10, 'name': 'ExpeditionPCB.exe', 'command': 'ExpeditionPCB.exe other.pcb'},
{'pid': 10, 'name': 'ExpeditionPCB.exe', 'command': f'ExpeditionPCB.exe "{other_pcb}"'},
])
entry.assert_native_idle(RepairTarget(project, tmp_path/'database'))
entry.assert_native_idle(RepairTarget(project, work/'database'))
def test_maintenance_guard_does_not_scan_other_cbb_projects(case, monkeypatch):
@@ -148,20 +152,14 @@ def test_live_writer_keeps_execution_lock_without_a_permanent_diagnostic_blocker
if owner: owner['handle'].Close()
def test_healthy_native_inspection_with_old_address_does_not_lock_management(case, monkeypatch):
def test_healthy_open_with_old_address_does_not_lock_management(case, monkeypatch):
from cbb_refresh_verify import native_repair, verify_runner as runner, library_lock as locks
from test_native_occupancy import output
request, project, address = case
def inspect(target, *a, **kw):
assert kw['inspection_only']
return {'inspection_only': True, 'repair_requested': False, 'repair_required': False,
'status': 'NOT_IN_USE', 'native_pid': 777, 'native_exit_code': 0,
'project': str(target.project), 'database': str(target.database),
'output': output(target.project, target.database), 'release': {'native_exited': True}}
monkeypatch.setattr(native_repair, 'run_native_repair', inspect)
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **kw: runner.VerifyResult('PASS'))
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **kw: pytest.fail('scan requested Project Backup'))
result = runner.run_verify(request)
assert result.status == 'CHECKED' and result.safe_to_continue and address.exists()
assert not result.stderr and '无需修复' in result.stdout
assert not result.stderr and '未发现 ICDB 错误' in result.stdout
assert not locks.blocker_path(request.lmc_file).exists()
with locks.LibraryLock(request.lmc_file): pass
+19 -1
View File
@@ -134,6 +134,24 @@ def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkey
assert proof['exclusive_files'] == [str(library)]
def test_release_does_not_resolve_every_unrelated_generated_file(tmp_path, monkeypatch):
from pathlib import Path
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.owner_probe as probe
monkeypatch.setattr(probe, 'query_owners', lambda *_: [])
library = tmp_path/'lib.lmc'; library.touch()
outputs = tmp_path/'generated'; outputs.mkdir()
for index in range(100): (outputs/f'{index}.log').touch()
database = tmp_path/'database/icdb.dat'; database.parent.mkdir(); database.touch()
resolve = Path.resolve
def guarded(path, *a, **kw):
assert path.parent != outputs, 'Release resolved unrelated generated content'
return resolve(path, *a, **kw)
monkeypatch.setattr(Path, 'resolve', guarded)
with locks._prove_library_released(library) as proof:
assert set(proof['exclusive_files']) == {str(library), str(database)}
def test_scoped_release_ignores_another_cbb_address(tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.owner_probe as probe
@@ -261,7 +279,7 @@ def test_arbitrary_guard_filename_is_preserved_and_primary(tmp_path, monkeypatch
assert marker.name in manifest['files']
from contextlib import contextmanager
@contextmanager
def proof(*a): yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
def proof(*a, **kw): yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
monkeypatch.setattr(locks, '_prove_library_released', proof)
with pytest.raises(locks.LibraryBusy):
locks.release_retained_library(library, tmp_path/'release')
+1 -1
View File
@@ -280,7 +280,7 @@ def test_takeover_preserves_old_checkpoint_until_every_guard_passes(case, monkey
if change == 'foreign_library': record['library'] = str(library.parent.parent/'foreign/mentor.lmc')
marker.write_text('broken json' if change == 'malformed' else json.dumps(record), encoding='utf-8')
old = marker.read_bytes()
monkeypatch.setattr(locks, '_supervisor_alive', lambda pid: change == 'live_foreign')
monkeypatch.setattr(locks, '_recorded_supervisor_alive', lambda record: change == 'live_foreign')
if change == 'guard_failure':
monkeypatch.setattr(entry, 'assert_native_idle', lambda: (_ for _ in ()).throw(ValueError('guard failed')))
archive = request.evidence_dir/'takeover'
+93
View File
@@ -0,0 +1,93 @@
"""Ownership scoping only: no native process or execution mutex is started."""
import json
from types import SimpleNamespace
import pytest
@pytest.fixture
def scope(tmp_path, monkeypatch):
from cbb_refresh_verify.native_repair import RepairTarget
from cbb_refresh_verify import native_management, native_engineering_identity
library = tmp_path/'a/lib.lmc'
library.parent.mkdir()
library.touch()
other = tmp_path/'b/lib.lmc'
other.parent.mkdir()
other.touch()
project = tmp_path/'work/main.prj'
project.parent.mkdir()
project.write_text(f'KEY CentralLibrary "{library}"\n')
target = RepairTarget(project, project.parent/'database')
monkeypatch.setattr(native_management, '_retained_workers', [])
monkeypatch.setattr(native_engineering_identity, '_retained_exports', [])
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k: [])
return target, library, other
@pytest.mark.parametrize('kind', ['management', 'export'])
@pytest.mark.parametrize('ownership', ['same', 'other', 'unknown'])
def test_retained_native_ownership_is_scoped_only_with_proven_library(scope, monkeypatch, kind, ownership):
from cbb_refresh_verify import maintenance_entry as entry
target, library, other = scope
claimed = library if ownership == 'same' else other if ownership == 'other' else None
worker = SimpleNamespace(poll=lambda: None, cbb_library=claimed)
if kind == 'management':
monkeypatch.setattr('cbb_refresh_verify.native_management._retained_workers', [worker])
else:
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._retained_exports',
[(worker, {}, {'library': str(claimed)} if claimed else {})])
if ownership == 'other':
entry.assert_native_idle(target)
else:
with pytest.raises(Exception, match='(OWNER_ACTIVE|exporter)'):
entry.assert_native_idle(target)
@pytest.mark.parametrize('reference', ['missing', 'conflicting', 'invalid', 'empty_extra'])
def test_unproven_library_does_not_exclude_retained_workers(scope, monkeypatch, reference):
from cbb_refresh_verify import maintenance_entry as entry
target, library, other = scope
target.project.write_text('' if reference == 'missing' else
f'KEY CentralLibrary "{library}"\nKEY CentralLibrary "{other}"' if reference == 'conflicting' else
f'KEY CentralLibrary "{library}"\nKEY CentralLibrary ""' if reference == 'empty_extra' else
'KEY CentralLibrary "missing.lmc"')
monkeypatch.setattr('cbb_refresh_verify.native_management._retained_workers',
[SimpleNamespace(poll=lambda: None, cbb_library=other)])
with pytest.raises(ValueError, match='OWNER_ACTIVE'):
entry.assert_native_idle(target)
@pytest.mark.parametrize('source', ['project', 'pcb', 'database', 'lmc', 'shared', 'job', 'unknown', 'other'])
def test_native_commands_include_target_and_shared_library_ownership(scope, monkeypatch, tmp_path, source):
from cbb_refresh_verify import maintenance_entry as entry
target, library, other = scope
job = tmp_path/'request.json'
job.write_text(json.dumps({'library': str(library)}))
command = {
'project': f'ExpeditionPCB.exe "{target.project}"',
'pcb': f'ExpeditionPCB.exe "{target.project.parent / "PCB/board.pcb"}"',
'database': f'iCDBProjectBackup.exe -icdb="{target.database}"',
'lmc': f'LibraryManager.exe "{library}"',
'shared': f'viewdraw.exe -project="{library.parent / "SymbolLibs/database"}"',
'job': f'python.exe --management-worker "{job}"',
'unknown': 'ExpeditionPCB.exe',
'other': f'LibraryManager.exe "{other}"',
}[source]
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k:
[{'pid': 987654, 'name': 'python.exe' if source == 'job' else 'ExpeditionPCB.exe',
'command': command}])
if source == 'other':
entry.assert_native_idle(target)
else:
with pytest.raises(ValueError, match='OWNER_ACTIVE'):
entry.assert_native_idle(target)
def test_no_target_keeps_global_retained_owner_guard(scope, monkeypatch):
from cbb_refresh_verify import maintenance_entry as entry
_, _, other = scope
monkeypatch.setattr('cbb_refresh_verify.native_management._retained_workers',
[SimpleNamespace(poll=lambda: None, cbb_library=other)])
with pytest.raises(ValueError, match='OWNER_ACTIVE'):
entry.assert_native_idle()
+55 -23
View File
@@ -31,12 +31,28 @@ def test_diagnostic_worker_closes_without_integration_save_or_config_edit(tmp_pa
def forbidden(*args, **kwargs):
pytest.fail('diagnosis attempted integration/save/config rewrite')
monkeypatch.setattr(adapter, 'ensure_pcb_central_library', forbidden)
monkeypatch.setattr(adapter, 'pcb_log_reports_integration_error', forbidden)
original_read = Path.read_bytes
def read_bytes(path):
if path.name in ('ForwardAnnotation.txt', 'MsgBoxResponse.log', 'PartPkg.log'):
pytest.fail('opening check read unrelated integration history')
return original_read(path)
monkeypatch.setattr(Path, 'read_bytes', read_bytes)
monkeypatch.setattr('cbb_refresh_verify.integration.refresh_integration', forbidden)
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.launch_save_helper', forbidden)
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', forbidden)
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document', forbidden)
closes = []
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document',
lambda job, **kw: closes.append(job) or {'status': 'CLOSED'})
def check(job, pid, **kw):
assert pid == 2 and kw['launcher_process'].pid == 1
assert job['diagnostic_log_baseline']['started_ns']
closes.append(job)
kw['emit'](stage='diagnostic_wm_close')
return {'status': 'PASS', 'native_processes_exited': True}
monkeypatch.setattr('cbb_refresh_verify.diagnostic_open.check_target_window', check)
result = execute()
from cbb_refresh_verify.verify_runner import VerifyResult
assert VerifyResult(**result).status == 'PASS'
assert result['status'] == 'PASS'
assert len(closes) == 1
assert not any(e['stage'] in ('save_start', 'saved_and_closed') for e in events)
@@ -47,8 +63,13 @@ def test_diagnostic_close_failure_never_returns_healthy(tmp_path, monkeypatch):
job, events, execute = diagnostic_worker(tmp_path, monkeypatch)
job['diagnostic_only'] = True
closes = []
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document',
lambda *a, **kw: closes.append(a) or {'status': 'RETAINED'})
def check(*a, **kw):
closes.append(a)
kw['emit'](stage='diagnostic_wm_close')
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED')
monkeypatch.setattr('cbb_refresh_verify.diagnostic_open.check_target_window', check)
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_startup',
lambda *a, **kw: pytest.fail('must not send another close after WM_CLOSE'))
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
execute()
assert len(closes) == 1
@@ -71,6 +92,8 @@ def diagnostic_worker(tmp_path, monkeypatch):
job, events, execute = exercise_worker(tmp_path, monkeypatch)
(Path(job['pcb']).parent/'pcb.prj').write_text(
'KEY CentralLibrary "' + job['lmc_file'] + '"\n', encoding='utf-8')
monkeypatch.setattr('cbb_refresh_verify.diagnostic_open.check_target_window',
lambda *a, **kw: {'status': 'PASS', 'native_processes_exited': True})
return job, events, execute
@@ -98,19 +121,14 @@ def test_diagnostic_runner_skips_importer_and_ready_verification(tmp_path, monke
assert captured['diagnostic_only'] is True
def test_diagnostic_waits_for_normal_exit_after_close_returns(tmp_path, monkeypatch):
def test_diagnostic_cannot_pass_with_unconfirmed_process_exit(tmp_path, monkeypatch):
from cbb_refresh_verify import mentor_adapter as adapter
job, events, execute = diagnostic_worker(tmp_path, monkeypatch)
job['diagnostic_only'] = True
observations = []
def release(*a, **kw):
observations.append(True)
return {'safe_to_continue': len(observations) > 1}
monkeypatch.setattr(adapter, 'cleanup_verify_processes', release)
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document',
lambda *a, **kw: {'status': 'CLOSED'})
assert execute()['status'] == 'PASS'
assert len(observations) >= 2
monkeypatch.setattr(adapter, 'cleanup_verify_processes',
lambda *a, **kw: {'safe_to_continue': False, 'remaining_pids': [2]})
with pytest.raises(RuntimeError, match='DIAGNOSTIC_CLOSE_UNCONFIRMED'):
execute()
@pytest.mark.parametrize('when', ['document_ready', 'closing'])
@@ -123,17 +141,31 @@ def test_diagnostic_checks_late_startup_errors_before_declaring_healthy(tmp_path
source.parent.mkdir(exist_ok=True)
source.write_text("The last server session on different server machine lost the connection to the database. "
f"Machine:[fixture] Path:[{Path(job['project']).parent/'database'}] session:[current]", encoding='utf-8')
def document(*a, **kw):
if when == 'document_ready': late_fault()
return None, None, object()
closes = []
def close(*a, **kw):
def check(*a, **kw):
if when == 'document_ready':
late_fault()
raise RuntimeError(kw['error_reader']())
closes.append(True)
if when == 'closing': late_fault()
return {'status': 'CLOSED'}
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', document)
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document', close)
kw['emit'](stage='diagnostic_wm_close')
late_fault()
return {'status': 'PASS', 'native_processes_exited': True}
monkeypatch.setattr('cbb_refresh_verify.diagnostic_open.check_target_window', check)
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_startup',
lambda *a, **kw: {'status': 'CLOSED'})
with pytest.raises(RuntimeError, match='ICDB_REPAIR_REQUIRED'):
execute()
assert len(closes) == 1
assert len(closes) == (1 if when == 'closing' else 0)
assert not any(e['stage'] == 'diagnostic_complete' for e in events)
def test_diagnostic_snapshot_is_before_launch(tmp_path, monkeypatch):
from cbb_refresh_verify import mentor_adapter as adapter
job, events, execute = diagnostic_worker(tmp_path, monkeypatch)
job['diagnostic_only'] = True
def launch(*a):
assert job.get('diagnostic_log_baseline', {}).get('started_ns')
from types import SimpleNamespace
return SimpleNamespace(pid=1)
monkeypatch.setattr(adapter, 'launch_pcb_process', launch)
assert execute()['status'] == 'PASS'
+115 -115
View File
@@ -1,115 +1,115 @@
import json
from pathlib import Path
import pytest
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QMessageBox
def test_only_confirmed_maintenance_sets_explicit_flag(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req) or VerifyResult('FAIL', safe_to_continue=True)),
status_refresher=lambda names, path: {name: 'PENDING' for name in names})
window.search_edit.setText('Beta')
window.verify_all(); qtbot.waitUntil(lambda: not window.busy)
assert calls and calls[-1].maintenance is False
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.No)
window.repair_icdb_all(); assert len(calls) == 1
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy)
assert len(calls) == 2 and calls[-1].maintenance is True
@pytest.mark.parametrize('marker_kind', ['valid', 'foreign', 'malformed'])
def test_old_checkpoint_does_not_force_maintenance_or_disable_verify(qtbot, tmp_path, monkeypatch, marker_kind):
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.verify_runner import VerifyResult
from cbb_refresh_verify.models import LibraryReadResult, CbbRecord
import cbb_refresh_verify.ui.main_window as ui
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch()
(library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True)
with LibraryLock(library, safe_to_release=lambda: False) as lock: pass
with _guard:
owner = _owners.pop(lock.key, None)
if owner: owner['handle'].Close()
marker = blocker_path(library)
if marker_kind == 'malformed': marker.write_text('{}')
if marker_kind == 'foreign':
record = json.loads(marker.read_text()); record['library'] = str(tmp_path/'foreign/mentor.lmc')
marker.write_text(json.dumps(record))
monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None)
calls = []
window = MainWindow(reader=lambda *a: LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),)),
verifier=lambda req: (calls.append(req) or VerifyResult('BLOCKED', safe_to_continue=False)),
status_refresher=lambda *a: {'IP101GR': 'PENDING'}, data_root=tmp_path/'data')
qtbot.addWidget(window)
window.lmc_edit.setText(str(library)); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert window.release_library_button.isEnabled()
assert window.visible_records()[0].name == 'IP101GR'
assert window.visible_records()[0].status == 'PENDING'
assert not window._maintenance_recovery_view
window.select_visible_button.click()
assert window.icdb_repair_button.isEnabled()
assert window.batch_button.isEnabled() and window.selected_batch_button.isEnabled()
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window.verify_selected(); qtbot.waitUntil(lambda: not window.busy)
assert len(calls) == 1 and calls[0].maintenance is False
assert window.selected_batch_button.isEnabled()
window.repair_icdb_all(); qtbot.waitUntil(lambda: not window.busy)
assert len(calls) == 2 and calls[1].maintenance is True
assert marker.exists()
def test_maintenance_summary_exposes_first_failure_reason(qtbot, tmp_path):
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.verify_runner import VerifyResult
window = MainWindow(data_root=tmp_path)
qtbot.addWidget(window)
window._global_icdb_run = True
detail = 'MAINTENANCE_TARGET_UNCONFIRMED: PCB 引用与当前工程不一致'
window._batch_done(([VerifyResult('BLOCKED', stderr=detail, safe_to_continue=False)], tmp_path/'batch.log'))
assert detail in window.summary.detailedText()
assert detail in window.output.toPlainText()
assert detail not in window.summary.text()
assert str(tmp_path) not in window.summary.text()
assert detail not in window.status_label.text()
assert '无法确认 1' in window.summary.text()
def test_safe_maintenance_reloads_authoritative_catalog_after_worker_finishes(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.models import LibraryReadResult, CbbRecord
from cbb_refresh_verify.verify_runner import VerifyResult
import cbb_refresh_verify.ui.main_window as ui
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch()
(library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True)
with LibraryLock(library, safe_to_release=lambda: False) as lease: pass
with _guard:
owner = _owners.pop(lease.key, None)
if owner: owner['handle'].Close()
reads = []
def reader(path):
reads.append(path)
return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),))
def verify(req):
assert req.maintenance
blocker_path(library).unlink() # Simulate normal cleanup at the injected boundary.
return VerifyResult('PASS', library_flag='GREEN')
monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None)
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window = MainWindow(reader=reader, verifier=verify, data_root=tmp_path/'data',
status_refresher=lambda *a: {'IP101GR': 'READY'})
qtbot.addWidget(window)
window.lmc_edit.setText(str(library)); window.load_library(); qtbot.waitUntil(lambda: not window.busy)
assert len(reads) == 1
window.search_edit.setText('IP101')
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'READY'
assert not window._maintenance_recovery_view and window.search_edit.text() == 'IP101'
import json
from pathlib import Path
import pytest
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QMessageBox
def test_only_confirmed_maintenance_sets_explicit_flag(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req) or VerifyResult('FAIL', safe_to_continue=True)),
status_refresher=lambda names, path: {name: 'PENDING' for name in names})
window.search_edit.setText('Beta')
window.verify_all(); qtbot.waitUntil(lambda: not window.busy)
assert calls and calls[-1].maintenance is False
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.No)
window.repair_icdb_all(); assert len(calls) == 1
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window.repair_icdb_all(); qtbot.waitUntil(lambda: not window._icdb_active())
assert len(calls) == 2 and calls[-1].maintenance is True
@pytest.mark.parametrize('marker_kind', ['valid', 'foreign', 'malformed'])
def test_old_checkpoint_does_not_force_maintenance_or_disable_verify(qtbot, tmp_path, monkeypatch, marker_kind):
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.verify_runner import VerifyResult
from cbb_refresh_verify.models import LibraryReadResult, CbbRecord
import cbb_refresh_verify.ui.main_window as ui
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch()
(library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True)
with LibraryLock(library, safe_to_release=lambda: False) as lock: pass
with _guard:
owner = _owners.pop(lock.key, None)
if owner: owner['handle'].Close()
marker = blocker_path(library)
if marker_kind == 'malformed': marker.write_text('{}')
if marker_kind == 'foreign':
record = json.loads(marker.read_text()); record['library'] = str(tmp_path/'foreign/mentor.lmc')
marker.write_text(json.dumps(record))
monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None)
calls = []
window = MainWindow(reader=lambda *a: LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),)),
verifier=lambda req: (calls.append(req) or VerifyResult('BLOCKED', safe_to_continue=False)),
status_refresher=lambda *a: {'IP101GR': 'PENDING'}, data_root=tmp_path/'data')
qtbot.addWidget(window)
window.lmc_edit.setText(str(library)); window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert window.release_library_button.isEnabled()
assert window.visible_records()[0].name == 'IP101GR'
assert window.visible_records()[0].status == 'PENDING'
assert not window._maintenance_recovery_view
window.select_visible_button.click()
assert window.icdb_repair_button.isEnabled()
assert window.batch_button.isEnabled() and window.selected_batch_button.isEnabled()
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window.verify_selected(); qtbot.waitUntil(lambda: not window.busy)
assert len(calls) == 1 and calls[0].maintenance is False
assert window.selected_batch_button.isEnabled()
window.repair_icdb_all(); qtbot.waitUntil(lambda: not window._icdb_active())
assert len(calls) == 2 and calls[1].maintenance is True
assert marker.exists()
def test_maintenance_summary_exposes_first_failure_reason(qtbot, tmp_path):
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.verify_runner import VerifyResult
window = MainWindow(data_root=tmp_path)
qtbot.addWidget(window)
window._global_icdb_run = True
detail = 'MAINTENANCE_TARGET_UNCONFIRMED: PCB 引用与当前工程不一致'
window._batch_done(([VerifyResult('BLOCKED', stderr=detail, safe_to_continue=False)], tmp_path/'batch.log'))
assert detail in window.summary.detailedText()
assert detail in window.output.toPlainText()
assert detail not in window.summary.text()
assert str(tmp_path) not in window.summary.text()
assert detail not in window.status_label.text()
assert '无法确认 1' in window.summary.text()
def test_open_check_preserves_verify_state_without_starting_an_extra_catalog_read(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path, _owners, _guard
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.models import LibraryReadResult, CbbRecord
from cbb_refresh_verify.verify_runner import VerifyResult
import cbb_refresh_verify.ui.main_window as ui
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.touch()
(library.parent/'ReusableBlocksLP/IP101GR').mkdir(parents=True)
with LibraryLock(library, safe_to_release=lambda: False) as lease: pass
with _guard:
owner = _owners.pop(lease.key, None)
if owner: owner['handle'].Close()
reads = []
def reader(path):
reads.append(path)
return LibraryReadResult((CbbRecord('IP101GR', 'Reusable Blocks', status='PENDING'),))
def verify(req):
assert req.maintenance
blocker_path(library).unlink() # Simulate normal cleanup at the injected boundary.
return VerifyResult('CHECKED')
monkeypatch.setattr(ui, 'preflight_skip_reason', lambda *a: None)
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window = MainWindow(reader=reader, verifier=verify, data_root=tmp_path/'data',
status_refresher=lambda *a: pytest.fail('An open check must not start a second native status read'))
qtbot.addWidget(window)
window.lmc_edit.setText(str(library)); window.load_library(); qtbot.waitUntil(lambda: not window.busy)
assert len(reads) == 1
window.search_edit.setText('IP101')
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window._icdb_active())
assert window.visible_records()[0].status == 'PENDING' and len(reads) == 1
assert not window._maintenance_recovery_view and window.search_edit.text() == 'IP101'
+3 -3
View File
@@ -162,7 +162,7 @@ def test_update_failed_readd_keeps_removed_checkpoint_and_baseline(setup):
request, adapter, source = setup
adapter.fail_add = True
result = execute(replace(request, operation='update', source=source), adapter)
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
assert adapter.records == []
history = json.loads(result.evidence_path.read_text(encoding='utf-8'))['history']
assert 'removed' in [x['stage'] for x in history]
@@ -177,7 +177,7 @@ def test_lying_native_output_does_not_report_success(setup, operation):
request = replace(request, operation=operation, source=source,
name='New' if operation == 'add' else 'Block')
result = execute(request, adapter)
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
def test_unreleased_native_owner_prevents_postread_and_verification(setup):
@@ -265,7 +265,7 @@ def test_replaced_source_bytes_fail_identity_even_with_same_name(setup):
return result
adapter.add = wrong_source
result = execute(replace(request, operation='add', name='New', source=source), adapter)
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
def test_update_success_requires_same_identity_and_no_implicit_partition_move(setup):
+4 -2
View File
@@ -107,7 +107,8 @@ def test_native_registered_but_asset_mismatch_retains_recovery(transaction, corr
root, request, state = transaction
state['corrupt'] = corrupt
result = execute(root, request)
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
assert execute(root, request).status == 'BLOCKED'
def test_update_readd_failure_preserves_baseline_and_removed_checkpoint(transaction):
@@ -115,7 +116,8 @@ def test_update_readd_failure_preserves_baseline_and_removed_checkpoint(transact
assert execute(root, request).status == 'UPLOADED'
state['fail_add'] = True
result = execute(root, replace(request, operation='update'))
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
assert execute(root, replace(request, operation='update')).status == 'BLOCKED'
assert (result.evidence_path.parent/'library-baseline-manifest.json').exists()
assert not (result.evidence_path.parent/'library-before').exists()
assert 'removed' in [entry['stage'] for entry in json.loads(result.evidence_path.read_text())['history']]
+585
View File
@@ -0,0 +1,585 @@
"""Retry boundaries use fake native outcomes and temporary libraries only."""
import json
import os
from pathlib import Path
from types import SimpleNamespace
import pytest
from test_management import setup, execute
from test_management_native_transaction import transaction
@pytest.fixture
def isolated_mutex(monkeypatch):
import win32event
from uuid import uuid4
original = win32event.CreateMutex
suffix = uuid4().hex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
original(security, owned, name + suffix))
def test_released_catalog_failure_does_not_retain_management_lease(setup, monkeypatch):
from cbb_refresh_verify.native_management import NativeManagementAdapter
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path
request, _, _ = setup
adapter = NativeManagementAdapter(mentor_home=request.library.parent)
monkeypatch.setattr(adapter, 'capabilities', lambda: {'remove': SimpleNamespace(supported=True)})
monkeypatch.setattr(adapter, '_worker', lambda *_: {
'success': False, 'safe_to_continue': True, 'detail': 'catalog read failed after exit'})
result = execute(request, adapter)
assert result.safe_to_continue
assert not blocker_path(request.library).exists()
with LibraryLock(request.library):
pass
@pytest.mark.parametrize('busy', [False, True])
def test_legacy_blocker_is_rechecked_before_management_without_inventing_new_owner(setup, monkeypatch, isolated_mutex, busy):
import cbb_refresh_verify.library_lock as locks
request, adapter, _ = setup
marker = locks.blocker_path(request.library)
legacy = {'protocol': 'cbb-refresh-verify-library-lease', 'version': 1,
'lease_id': 'eae80a06f23e4664b00c70b6bf81b01d', 'library': str(request.library),
'status': 'OWNER_RELEASE_UNCONFIRMED', 'pid': 57076, 'time': '2026-11-04T09:43:50.830958+00:00'}
marker.write_text(json.dumps(legacy), encoding='utf-8')
before = marker.read_bytes()
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
address = request.library.parent/'database/cdbsvr/sAddress.adr'
if busy:
address.parent.mkdir(parents=True); address.write_bytes(b'actual address')
result = execute(request, adapter)
assert result.safe_to_continue, 'No new native session was started by a blocked release check'
assert result.evidence_path and result.evidence_path.is_file()
if busy:
assert result.status == 'BLOCKED' and adapter.calls == []
assert marker.read_bytes() == before and address.read_bytes() == b'actual address'
assert '地址' in result.detail
else:
assert result.status == 'REMOVED' and adapter.calls == ['remove']
assert result.release_proof['status'] == 'RELEASE_CONFIRMED'
archive = Path(result.release_proof['evidence_path']).with_name('blocker-before-0.json')
assert archive.read_bytes() == before and not marker.exists()
def test_busy_execution_channel_does_not_create_unreleased_management_owner(setup, monkeypatch):
import win32con, win32event
request, adapter, _ = setup
monkeypatch.setattr(win32event, 'WaitForSingleObject', lambda *_: win32con.WAIT_TIMEOUT)
result = execute(request, adapter)
assert result.status == 'BLOCKED' and result.safe_to_continue and not adapter.calls
assert result.evidence_path.is_file()
def test_management_rechecks_retained_verify_lease_without_checkpoint(setup, monkeypatch, isolated_mutex):
import cbb_refresh_verify.library_lock as locks
from cbb_refresh_verify.residual_sessions import inspect_residual_sessions
request, adapter, _ = setup
with locks.LibraryLock(request.library, checkpoint=False, safe_to_release=lambda: False):
pass
assert not locks.blocker_path(request.library).exists()
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
monkeypatch.setattr('cbb_refresh_verify.residual_sessions.query_process_snapshot', lambda: [])
preview = inspect_residual_sessions(request.library)
assert preview['sessions'] == [] and any(r.get('kind') == 'in_memory' for r in preview['previous_records'])
result = execute(request, adapter)
assert result.status == 'REMOVED' and result.release_proof['status'] == 'RELEASE_CONFIRMED'
assert not locks.has_retained_library(request.library)
def test_management_worker_prelaunch_failure_is_safe(tmp_path, monkeypatch):
import cbb_refresh_verify.native_management as native
job = tmp_path/'request.json'
job.write_text(json.dumps({'operation': 'catalog', 'desktop': 'CbbManagement_test',
'library': str(tmp_path/'lib.lmc')}), encoding='utf-8')
monkeypatch.setattr(native, '_current_desktop', lambda: (None, 'wrong desktop'))
assert native.management_worker_main(job) == 2
result = json.loads((tmp_path/'result.json').read_text('utf-8'))
assert result['safe_to_continue'] is True
@pytest.mark.parametrize('code,busy', [(5, False), (32, True), (33, True)])
def test_coordination_open_error_preserves_access_failure(tmp_path, monkeypatch, code, busy):
import pywintypes
import win32file
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
def denied(*_):
raise pywintypes.error(code, 'CreateFile', 'test failure')
monkeypatch.setattr(win32file, 'CreateFile', denied)
with pytest.raises((OSError, pywintypes.error)) as error:
with LibraryLock(tmp_path/'lib.lmc'):
pass
assert isinstance(error.value, LibraryBusy) is busy
def test_retained_checkpoint_lease_can_reprobe_exact_release(tmp_path):
import cbb_refresh_verify.library_lock as locks
library = tmp_path/'lib.lmc'; library.touch()
released = False
lease = locks.LibraryLock(library, safe_to_release=lambda: released)
try:
with lease:
pass
with pytest.raises(locks.LibraryBusy):
with locks.LibraryLock(library):
pass
released = True
with locks.LibraryLock(library):
pass
assert not locks.blocker_path(library).exists()
finally:
owner = locks._owners.pop(lease.key, None)
if owner:
owner['handle'].Close()
@pytest.mark.parametrize('failure', ['enumerate', 'cancel'])
def test_owned_mapping_close_retries_after_transient_failure(monkeypatch, failure):
from test_network_path import FakeWNet
from cbb_refresh_verify.network_path import LibraryPathLease, NetworkPathError
backend = FakeWNet()
lease = LibraryPathLease.acquire(r'\\server\share\lib.lmc', backend=backend)
method = 'mappings' if failure == 'enumerate' else 'cancel_mapping'
original = getattr(backend, method)
def broken(*_):
raise OSError('temporary disconnect')
monkeypatch.setattr(backend, method, broken)
with pytest.raises(NetworkPathError):
lease.close()
monkeypatch.setattr(backend, method, original)
lease.close()
assert backend.cancelled == [lease.drive]
def test_batch_mismatch_isolated_to_item(tmp_path):
from cbb_refresh_verify.batch_status import BatchStatusSession
from cbb_refresh_verify.status_bridge import BridgeResponseError, BridgeResult
library = tmp_path/'lib.lmc'; library.touch()
class Bridge:
def __init__(self): self.calls = []; self.restarts = 0
def start(self): pass
def close(self): pass
def restart(self): self.restarts += 1
def query(self, library, name, **kwargs):
self.calls.append(name)
if name == 'Bad': raise BridgeResponseError('CBB mismatch')
return BridgeResult('PENDING', 'test')
bridge = Bridge()
results = BatchStatusSession(bridge).scan(['Bad', 'Good'], library, tmp_path/'scan.jsonl')
assert [result.status for result in results] == ['MALFORMED', 'PENDING']
assert bridge.calls == ['Bad', 'Good'] and bridge.restarts == 1
def test_batch_cache_binds_exact_library_identity(tmp_path):
from cbb_refresh_verify.batch_status import fingerprint_library
first = tmp_path/'a/lib.lmc'; first.parent.mkdir(); first.write_bytes(b'same')
second = tmp_path/'b/lib.lmc'; second.parent.mkdir(); second.write_bytes(b'same')
stat = first.stat(); os.utime(second, ns=(stat.st_atime_ns, stat.st_mtime_ns))
assert fingerprint_library(first).sha256 != fingerprint_library(second).sha256
def test_timed_out_management_worker_releases_only_after_its_result(tmp_path, monkeypatch):
import cbb_refresh_verify.native_management as native
state = {'exit': None}
worker = SimpleNamespace(poll=lambda: state['exit'], close=lambda: None)
adapter = native.NativeManagementAdapter(mentor_home=tmp_path, worker_factory=lambda *_: worker)
library = tmp_path/'lib.lmc'; library.touch()
evidence = tmp_path/'run'; evidence.mkdir()
result = adapter._launch_worker({'library': str(library), 'operation': 'catalog',
'desktop': 'CbbManagement_test', 'timeout_seconds': 0}, evidence)
try:
assert result['safe_to_continue'] is False
assert adapter.release_confirmed(library) is False
state['exit'] = 2
assert adapter.release_confirmed(library) is False
(evidence/'result.json').write_text(json.dumps({'success': False, 'safe_to_continue': True}))
assert adapter.release_confirmed(tmp_path/'other.lmc') is False
assert adapter.release_confirmed(library) is True
assert worker not in native._retained_workers
finally:
if worker in native._retained_workers:
native._retained_workers.remove(worker)
def test_existing_library_manager_refusal_preserves_prelaunch_release(tmp_path, monkeypatch):
import cbb_refresh_verify.native_management as native
monkeypatch.setattr(native, '_current_desktop', lambda: (None, 'CbbManagement_test'))
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter._process_details', lambda *_: [(123, 'other')])
with pytest.raises(RuntimeError) as error:
native._catalog_native({'operation': 'catalog', 'desktop': 'CbbManagement_test'}, tmp_path)
assert error.value.safe_to_continue is True
@pytest.mark.parametrize('stage,expected', [('catalog_before', 'REMOVED'), ('native_remove', 'BLOCKED')])
def test_prewrite_failure_evidence_does_not_permanently_prevent_retry(setup, stage, expected):
request, adapter, _ = setup
checkpoint = request.evidence_dir/'interrupted/checkpoint.json'
checkpoint.parent.mkdir(parents=True)
checkpoint.write_text(json.dumps({'library': str(request.library), 'stage': 'complete',
'status': 'BLOCKED', 'safe_to_continue': False, 'history': [{'stage': stage}]}), encoding='utf-8')
result = execute(request, adapter)
assert result.status == expected
def test_scoped_release_includes_shared_database_addresses(tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
library = tmp_path/'lib.lmc'; library.touch()
address = tmp_path/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True); address.write_bytes(b'preserve shared native address')
with pytest.raises(locks.LibraryBusy):
with locks._prove_library_released(library, block_name='Target'):
pass
assert address.read_bytes() == b'preserve shared native address'
def test_valid_relocated_pending_checkpoint_does_not_block_snapshot(tmp_path):
import cbb_refresh_verify.library_lock as locks
library = tmp_path/'copy/lib.lmc'; library.parent.mkdir(); library.touch()
pending = locks.blocker_path(library).with_suffix('.pending')
pending.write_text(json.dumps({'protocol': 'cbb-refresh-verify-library-lease', 'version': 1,
'library': str(tmp_path/'source/lib.lmc'), 'pid': os.getpid(),
'status': 'OPERATION_IN_PROGRESS', 'lease_id': 'a'*32, 'time': '2026-11-05'}))
assert not locks.has_blocker(library)
@pytest.mark.parametrize('content', ['{torn json', '{"unrecognized":"metadata"}'])
def test_unknown_checkpoint_release_archives_only_after_full_fresh_proof(tmp_path, monkeypatch, content):
import cbb_refresh_verify.library_lock as locks
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: [])
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
marker = locks.blocker_path(library); marker.write_text(content)
result = locks.release_retained_library(library, tmp_path/'evidence', block_name='Target')
assert result['status'] == 'RELEASE_CONFIRMED'
assert Path(result['evidence_path']).with_name('blocker-before-0.json').read_text() == content
assert not marker.exists()
assert library.with_name('.lib.cbb-upload.lock').exists()
def test_unknown_checkpoint_cannot_use_selected_target_to_ignore_other_owners(tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: [])
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
marker = locks.blocker_path(library); marker.write_bytes(b'{torn checkpoint')
address = library.parent/'ReusableBlocksLP/Other/Work/database/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True); address.write_bytes(b'preserve owner record')
with pytest.raises(locks.LibraryBusy):
locks.release_retained_library(library, tmp_path/'evidence', block_name='Target')
assert marker.read_bytes() == b'{torn checkpoint'
assert address.read_bytes() == b'preserve owner record'
def test_status_cache_does_not_reuse_a_different_block_type(tmp_path):
from cbb_refresh_verify.batch_status import BatchStatusSession
from cbb_refresh_verify.status_bridge import BridgeResult
library = tmp_path/'lib.lmc'; library.touch()
calls = []
class Bridge:
def query(self, library, name, **kwargs):
calls.append(kwargs['block_type'])
return BridgeResult('READY', 'test')
session = BatchStatusSession(Bridge())
session.scan(['Block'], library, tmp_path/'scan.jsonl', block_type='RULP')
session.scan(['Block'], library, tmp_path/'scan.jsonl', block_type='Other')
assert calls == ['RULP', 'Other']
@pytest.mark.parametrize('case,relevant', [('other', False), ('prefix', False),
('target', True), ('shared', True), ('worker_target', True), ('worker_other', False)])
def test_release_owner_scope_uses_exact_paths_and_bound_worker_request(tmp_path, monkeypatch, case, relevant):
import cbb_refresh_verify.library_lock as locks
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
target = library.parent/'ReusableBlocksLP/Target/Work'
locations = {'other': tmp_path/'other/Other.prj', 'prefix': target.with_name('WorkOld')/'Other.prj',
'target': target/'Target.prj', 'shared': library.parent/'SymbolLibs/shared/database'}
if case.startswith('worker_'):
job = tmp_path/'worker.json'
job.write_text(json.dumps({'library': str(library if case == 'worker_target' else tmp_path/'other/lib.lmc')}))
name, command = 'python.exe', f'python.exe --management-worker "{job}"'
else:
name, command = 'ExpeditionPCB.exe', f'ExpeditionPCB.exe -project="{locations[case]}"'
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [
{'pid': 123456, 'name': name, 'command': command, 'parent': 0}])
if relevant:
with pytest.raises(locks.LibraryBusy):
with locks._prove_library_released(library, block_name='Target'):
pass
else:
with locks._prove_library_released(library, block_name='Target'):
pass
def test_release_ignores_retained_management_worker_for_other_library(tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.native_management as native
library = tmp_path/'lib.lmc'; library.touch()
other = SimpleNamespace(poll=lambda: None, cbb_library=str(tmp_path/'other.lmc'))
monkeypatch.setattr(native, '_retained_workers', [other])
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
with locks._prove_library_released(library):
pass
def test_retained_mapping_can_close_after_exact_release_proof():
from test_network_path import FakeWNet
from cbb_refresh_verify.network_path import LibraryPathLease
backend = FakeWNet()
lease = LibraryPathLease.acquire(r'\\server\share\lib.lmc', backend=backend)
lease.retain_for_recovery()
lease.close()
assert backend.cancelled == []
assert not lease.release_after_recovery({'status': 'RELEASE_CONFIRMED', 'library': r'\\other\share\lib.lmc'})
assert lease.release_after_recovery({'status': 'RELEASE_CONFIRMED', 'library': r'\\server\share\lib.lmc'})
lease.close()
assert backend.cancelled == [lease.drive]
def test_target_mapping_query_ignores_unrelated_enumeration_failure():
from test_network_path import FakeWNet
from cbb_refresh_verify.network_path import ensure_mapped_drive_visible
class Backend(FakeWNet):
def mapping(self, drive):
assert drive == 'S:'
return r'\\server\share'
def mappings(self):
raise OSError('unrelated drive enumeration failed')
result = ensure_mapped_drive_visible('S:', r'\\server\share', backend=Backend())
assert result['created'] is False
def test_windows_mapping_enumeration_reserves_unavailable_drive():
from cbb_refresh_verify.network_path import WindowsWNetBackend
backend = WindowsWNetBackend.__new__(WindowsWNetBackend)
def connection(drive, buffer, size):
if drive == 'D:': return 5
if drive == 'Y:':
buffer.value = r'\\server\share'
return 0
return 2250
backend._mpr = SimpleNamespace(WNetGetConnectionW=connection)
backend._kernel32 = SimpleNamespace(GetLogicalDrives=lambda: 0)
assert backend.mappings() == {'Y:': r'\\server\share'}
assert 'D:' in backend.used_drives()
def test_postnative_idle_failure_preserves_known_release(setup, isolated_mutex):
from cbb_refresh_verify.management import run_management
from cbb_refresh_verify.library_lock import LibraryLock
request, adapter, _ = setup
checks = []
def idle(*_):
checks.append(1)
if len(checks) == 3:
raise OSError('passive owner query unavailable after native exit')
result = run_management(request, adapter=adapter, idle_check=idle)
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
with LibraryLock(request.library):
pass
def test_final_report_failure_does_not_discard_release_result(setup, monkeypatch, isolated_mutex):
import cbb_refresh_verify.management as management
request, adapter, _ = setup
original = management._write_json
def write(path, record):
if record.get('stage') == 'complete':
raise OSError('evidence volume unavailable')
return original(path, record)
monkeypatch.setattr(management, '_write_json', write)
result = execute(request, adapter)
assert result.status == 'REMOVED' and result.safe_to_continue
assert 'evidence volume unavailable' in result.detail
def test_legacy_checkpoint_cannot_clear_other_target_owner(tmp_path, monkeypatch, isolated_mutex):
import cbb_refresh_verify.library_lock as locks
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
marker = locks.blocker_path(library)
locks._persist_blocker(library, lease_id='b'*32)
original = marker.read_bytes()
address = library.parent/'ReusableBlocksLP/Other/Work/database/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True); address.write_bytes(b'keep actual owner address')
with pytest.raises(locks.LibraryBusy):
locks.release_retained_library(library, tmp_path/'evidence', block_name='Target')
assert marker.read_bytes() == original
assert address.read_bytes() == b'keep actual owner address'
def test_release_proof_recovers_old_mapping_lease_and_transfers_shared_drive():
import cbb_refresh_verify.network_path as network
from test_network_path import FakeWNet
backend = FakeWNet()
old = network.LibraryPathLease.acquire(r'\\server\share\A\lib.lmc', backend=backend)
current = network.LibraryPathLease.acquire(r'\\server\share\B\lib.lmc', backend=backend)
old.retain_for_recovery(); old.close()
proof = {'status': 'RELEASE_CONFIRMED', 'library': r'\\server\share\A\lib.lmc'}
assert network.release_retained_path_leases(proof, current_lease=current) == 1
assert backend.cancelled == [] and current.created
current.close()
assert backend.cancelled == [current.drive]
def test_clean_failed_mutation_keeps_recovery_requirement_and_released_owner(setup, isolated_mutex):
from dataclasses import replace
from cbb_refresh_verify.management import run_management
request, adapter, source = setup
adapter.fail_add = True
failed = execute(replace(request, operation='update', source=source, auto_verify=False), adapter)
assert failed.status == 'RECOVERY_REQUIRED' and failed.safe_to_continue
adapter.fail_add = False
same = execute(replace(request, operation='add', source=source, auto_verify=False), adapter)
assert same.status == 'BLOCKED'
other = execute(replace(request, operation='add', name='Independent', source=source, auto_verify=False), adapter)
assert other.status == 'UPLOADED' and other.safe_to_continue
@pytest.mark.parametrize('history', [None, [], [{'stage': 'native_remove'}, {'stage': 'prepare_identity'}]])
def test_prewrite_stage_alone_does_not_override_unknown_or_mutated_history(history):
from cbb_refresh_verify.management import _may_have_mutated
record = {'stage': 'prepare_identity', 'history': history, 'status': 'RUNNING'}
assert _may_have_mutated(record)
def test_library_checkpoint_binds_supervisor_creation_identity(tmp_path):
import cbb_refresh_verify.library_lock as locks
library = tmp_path/'lib.lmc'; library.touch()
locks._persist_blocker(library, lease_id='c'*32)
record = json.loads(locks.blocker_path(library).read_text())
assert record.get('supervisor_created')
assert locks._recorded_supervisor_alive(record) is True
assert locks._recorded_supervisor_alive({**record, 'supervisor_created': 'other process'}) is False
legacy = {key: value for key, value in record.items() if key not in ('machine', 'supervisor_created')}
assert locks._recorded_supervisor_alive(legacy) is None
@pytest.mark.parametrize('proof,safe', [
({'safe_to_continue': True, 'process_created': False, 'worker_exited': True, 'remaining_pids': []}, True),
({'safe_to_continue': True, 'process_created': True, 'worker_exited': True, 'remaining_pids': []}, False),
({'safe_to_continue': True, 'process_created': False, 'worker_exited': False, 'remaining_pids': []}, False),
(None, False)])
def test_management_worker_launch_exception_uses_explicit_release_protocol(tmp_path, proof, safe):
from cbb_refresh_verify.native_management import NativeManagementAdapter
error = OSError('private desktop creation failed')
error.worker_launch_release = proof
def fail(*_): raise error
adapter = NativeManagementAdapter(mentor_home=tmp_path, worker_factory=fail)
with pytest.raises(OSError) as caught:
adapter._launch_worker({'library': str(tmp_path/'lib.lmc'), 'operation': 'catalog',
'desktop': 'CbbManagement_test', 'timeout_seconds': 1}, tmp_path)
assert getattr(caught.value, 'safe_to_continue', None) is safe
def test_native_add_passive_postcheck_failure_preserves_release(transaction, monkeypatch, isolated_mutex):
from test_management_native_transaction import execute as execute_transaction
root, request, state = transaction
def unavailable(*_): raise OSError('passive target check unavailable')
monkeypatch.setattr('cbb_refresh_verify.backup_recovery._idle_check', unavailable)
result = execute_transaction(root, request)
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
@pytest.mark.parametrize('case,safe', [('exit_output_error', True), ('popen_missing', True),
('popen_denied', True), ('popen_unknown', False), ('timeout', False)])
def test_remove_worker_preserves_exit_and_known_creation_rejection(tmp_path, monkeypatch, case, safe):
import cbb_refresh_verify.native_management as native
job = tmp_path/'request.json'
job.write_text(json.dumps({'operation': 'remove', 'desktop': 'CbbManagement_test',
'mentor_home': str(tmp_path), 'library': str(tmp_path/'lib.lmc'), 'name': 'Block',
'partition': 'Logical-Physical', 'timeout_seconds': 1}), encoding='utf-8')
monkeypatch.setattr(native, '_current_desktop', lambda: (None, 'CbbManagement_test'))
monkeypatch.setattr(native.NativeManagementAdapter, 'capabilities', lambda _: {
'remove': SimpleNamespace(supported=True)})
observed = {'waited': False}
def wait(timeout):
if case == 'timeout':
raise native.subprocess.TimeoutExpired('fake CleanWGLib', timeout)
observed['waited'] = True
return 0
def popen(*args, **kwargs):
failures = {'popen_missing': FileNotFoundError('native binary disappeared'),
'popen_denied': PermissionError('native creation denied'),
'popen_unknown': OSError('unconfirmed process creation')}
if case in failures:
raise failures[case]
return SimpleNamespace(pid=123456, wait=wait)
monkeypatch.setattr(native.subprocess, 'Popen', popen)
original_read = Path.read_bytes
def read(path):
if path.name == 'native-output.txt' and case == 'exit_output_error':
assert observed['waited']
raise OSError('native output storage unavailable after exit')
return original_read(path)
monkeypatch.setattr(Path, 'read_bytes', read)
assert native.management_worker_main(job) == 2
result = json.loads((tmp_path/'result.json').read_text('utf-8'))
assert result['safe_to_continue'] is safe
if case == 'exit_output_error':
assert result['exit_code'] == 0 and 'storage unavailable' in result['detail']
def test_add_source_missing_central_library_fails_before_capture_without_retaining(setup, monkeypatch, isolated_mutex):
from dataclasses import replace
import cbb_refresh_verify.native_management as native
from cbb_refresh_verify.management import Catalog
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path
request, _, source = setup
adapter = native.NativeManagementAdapter(mentor_home=source.parent)
monkeypatch.setattr(adapter, 'capabilities', lambda: {'add': SimpleNamespace(supported=True)})
monkeypatch.setattr(adapter, 'catalog', lambda library, evidence: Catalog(library, 1, ()))
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity.capture_identity',
lambda *_a, **_kw: pytest.fail('incomplete source reached native capture'))
result = execute(replace(request, operation='add', name='New', source=source, auto_verify=False), adapter)
assert result.status == 'UPLOAD_FAILED' and result.safe_to_continue
assert 'CentralLibrary' in result.detail
assert not blocker_path(request.library).exists()
with LibraryLock(request.library):
pass
def test_prepared_transaction_storage_failure_preserves_capture_release(transaction, monkeypatch, isolated_mutex):
from test_management_native_transaction import execute as execute_transaction
root, request, _ = transaction
def unavailable(*_a, **_kw): raise OSError('prepared transaction storage unavailable')
monkeypatch.setattr('cbb_refresh_verify.management_transaction.prepare_transaction', unavailable)
result = execute_transaction(root, request)
assert result.status == 'UPLOAD_FAILED' and result.safe_to_continue
assert 'prepared transaction storage unavailable' in result.detail
@pytest.mark.parametrize('flag,relevant', [('-icdb', False), ('-icdb=', False),
('-ICDB', True), ('-icdb=', True)])
def test_release_owner_scope_parses_actual_edif_database_argument(tmp_path, flag, relevant):
from cbb_refresh_verify.library_lock import _owner_matches_scope
library = tmp_path/'Target/lib.lmc'
database = library.parent/'Work/database'
selected = database if relevant else tmp_path/'Other project/database'
argument = flag + ('' if flag.endswith('=') else ' ') + f'"{selected}"'
item = {'command': f'edifExporter.exe -schematic {argument} -isnap DxD'}
assert _owner_matches_scope(item, library, (database,)) is relevant
def test_release_owner_scope_does_not_match_edif_database_prefix(tmp_path):
from cbb_refresh_verify.library_lock import _owner_matches_scope
database = tmp_path/'Target/database'
item = {'command': f'edifExporter.exe -schematic -icdb "{database}_old" -isnap DxD'}
assert _owner_matches_scope(item, tmp_path/'Target/lib.lmc', (database,)) is False
def test_same_target_recovery_gate_preserves_release_and_batch_continues(setup, isolated_mutex):
from dataclasses import replace
from cbb_refresh_verify.management import run_management_batch
request, adapter, source = setup
adapter.fail_add = True
failed = execute(replace(request, operation='update', source=source, auto_verify=False), adapter)
assert failed.status == 'RECOVERY_REQUIRED' and failed.safe_to_continue
adapter.fail_add = False
results = run_management_batch([
replace(request, operation='add', source=source, auto_verify=False),
replace(request, operation='add', name='Independent', source=source, auto_verify=False)],
adapter=adapter, idle_check=lambda *_: None)
assert [result.status for result in results] == ['BLOCKED', 'UPLOADED']
assert all(result.safe_to_continue for result in results)
+41 -3
View File
@@ -80,6 +80,37 @@ def test_restart_shows_unfinished_without_starting_worker(qtbot, tmp_path):
assert 'Block' in window.output.toPlainText()
def test_previous_unsafe_result_allows_retry_and_requires_matching_release_proof(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.management import Capability, ManagementResult
adapter = SimpleNamespace(capabilities=lambda: {op: Capability(True, 'available')
for op in ('add', 'update', 'remove')})
window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter)
library = Path(window.mentor_lmc_path or window.loaded_path)
from cbb_refresh_verify.network_path import LibraryPathLease
current = LibraryPathLease(library, library)
current.retain_for_recovery()
window.library_lease = current
window._management_unsafe = True
window._management_unsafe_library = library
window.select_visible_button.click()
assert window.remove_button.isEnabled(), 'A historical flag must not disable the fresh release check'
window.remove_button.click()
requests = window.management_dialog.requests()
assert requests and all(r.check_previous_release for r in requests)
result = ManagementResult('remove', library, 'Beta', 'Logical-Physical', None,
False, 'complete', 'BLOCKED', True, 'prelaunch failure')
window._management_completed([result])
assert window._management_library_unsafe(library), 'An input failure does not prove the previous session released'
assert current._retained_for_recovery
proof = {'status': 'RELEASE_CONFIRMED', 'library': str(library), 'evidence_path': 'proof.json'}
window._management_completed([replace(result, release_proof=proof)])
qtbot.waitUntil(lambda: window._mapping_busy == 0)
assert not window._management_library_unsafe(library)
assert not current._retained_for_recovery
window.management_dialog.reject()
def test_historical_record_does_not_prevent_catalog_load_and_offers_checked_release(qtbot, tmp_path, monkeypatch):
import pytest
import cbb_refresh_verify.library_lock as locks
@@ -95,9 +126,16 @@ def test_historical_record_does_not_prevent_catalog_load_and_offers_checked_rele
assert window.release_library_button.isEnabled()
assert locks.blocker_path(library).read_text() == '{}'
calls = []
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
monkeypatch.setattr(locks, 'release_retained_library', lambda *a:
(calls.append(a) or {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'proof.json'}))
monkeypatch.setattr(locks, 'inspect_residual_sessions', lambda *a:
{'library': str(library), 'sessions': []})
monkeypatch.setattr(locks, 'release_residual_sessions', lambda *a, **kw:
(calls.append(a) or {'status': 'RELEASE_CONFIRMED', 'sessions': [], 'proof':
{'status': 'RELEASE_CONFIRMED', 'library': str(library), 'evidence_path': 'proof.json'}}))
window.release_library_button.click()
qtbot.waitUntil(lambda: not window.release_dialog.busy)
assert not calls
window.release_dialog.confirm_button.click()
qtbot.waitUntil(lambda: not window.release_dialog.busy)
window.release_dialog.close()
qtbot.waitUntil(lambda: not window.busy)
assert len(calls) == 1 and 'proof.json' in window.output.toPlainText()
+2 -1
View File
@@ -46,7 +46,8 @@ def test_add_timeout_publishes_cancellation_without_killing_native_owner(tmp_pat
def poll(self): return None
def close(self): pytest.fail('live worker must not be killed')
adapter=native.NativeManagementAdapter(worker_factory=Running)
result=adapter._launch_worker({'operation':'add','desktop':'CbbManagement_test','timeout_seconds':0},tmp_path)
result=adapter._launch_worker({'operation':'add','desktop':'CbbManagement_test','timeout_seconds':0,
'library':str(tmp_path/'library.lmc')},tmp_path)
assert not result['safe_to_continue'] and (tmp_path/'cancel-native-add.signal').exists()
assert len(native._retained_workers)==1
+2 -1
View File
@@ -163,6 +163,7 @@ def test_closing_app_keeps_manual_native_window_and_mapping(qtbot, tmp_path, mon
window.library_lease = lease
window._native_backup_session = session
window.show(); window.close()
qtbot.waitUntil(lambda: not window.isVisible())
assert retained == [True] and session.process.poll() is None
@@ -214,7 +215,7 @@ def test_confirmed_native_close_transfers_mapping_to_current_library(qtbot, tmp_
assert window._native_backup_session is None
assert backend.cancelled == [] and current.created
window.close()
assert backend.cancelled == [current.drive]
qtbot.waitUntil(lambda: backend.cancelled == [current.drive])
def test_native_mapping_ownership_transfers_to_new_library_on_same_share():
+235
View File
@@ -200,6 +200,241 @@ def test_export_timeout_retains_process_without_kill(project, tmp_path, monkeypa
assert (root/'ascii-process.json').is_file()
def test_export_does_not_adopt_unrelated_existing_native_owner(project, tmp_path, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
unrelated = {'pid': 888888, 'parent': 0, 'name': 'iCDBNetServer.exe',
'executable': 'native', 'command': 'iCDBNetServer.exe C:\\other\\database'}
monkeypatch.setattr(identity, '_process_snapshot', lambda: [unrelated])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(identity, 'validate_export_output', lambda *_: None)
started = []
class Finished:
pid = 987654
returncode = 0
def __init__(self, command, **kwargs): started.append(command)
def poll(self): return 0
monkeypatch.setattr(identity.subprocess, 'Popen', Finished)
result = identity._run_exports(source, root, tmp_path, 1)
assert result['success'] and result['safe_to_continue']
assert len(started) == 3
def test_export_spawn_failure_does_not_manufacture_native_ownership(project, tmp_path, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
def fail(*a, **k): raise FileNotFoundError('missing exporter')
monkeypatch.setattr(identity.subprocess, 'Popen', fail)
result = identity._run_exports(source, root, tmp_path, 1)
assert not result['success'] and result['safe_to_continue']
assert 'missing exporter' in result['detail']
def test_unconfirmed_export_constructor_failure_retains_ownership(project, tmp_path, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
def fail(*a, **k): raise RuntimeError('uncertain partially-created process')
monkeypatch.setattr(identity.subprocess, 'Popen', fail)
result = identity._run_exports(source, root, tmp_path, 1)
assert not result['success'] and not result['safe_to_continue']
@pytest.mark.parametrize('summary', ['native-outcome.json', 'identity.json'])
def test_export_summary_write_failure_preserves_confirmed_release(project, tmp_path, monkeypatch, summary):
import cbb_refresh_verify.native_engineering_identity as identity
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path
import win32event
from uuid import uuid4
create_mutex = win32event.CreateMutex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
create_mutex(security, owned, name+'-test-'+uuid4().hex))
source, library = project
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
def exports(clone, root, *_):
(root/'export-launch.json').write_text('{"started": false}')
return {'success': False, 'safe_to_continue': True, 'detail': 'export rejected before start'}
monkeypatch.setattr(identity, '_run_exports', exports)
original_write = identity._write_json
def write(path, record):
if path.name == summary: raise OSError('summary unavailable')
return original_write(path, record)
monkeypatch.setattr(identity, '_write_json', write)
with LibraryLock(library):
result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert 'summary unavailable' in result.detail
assert not blocker_path(library).exists()
def test_export_snapshot_uses_bounded_read_only_helper(monkeypatch):
from cbb_refresh_verify import native_engineering_identity as identity, owner_probe
rows = [{'pid': 42, 'parent': 10, 'name': 'unknown-child.exe'}]
calls = []
def query(**kwargs):
calls.append(kwargs)
return rows
monkeypatch.setattr(owner_probe, 'query_process_snapshot', query)
assert identity._process_snapshot() == rows
assert calls == [{'timeout_seconds': 10}]
@pytest.mark.parametrize('after_launch', [False, True])
def test_export_snapshot_timeout_preserves_launch_extent(project, tmp_path, monkeypatch, after_launch):
from cbb_refresh_verify import native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
calls = []
def snapshot():
calls.append('snapshot')
if after_launch and len(calls) == 1: return []
raise RuntimeError('OWNER_QUERY_TIMEOUT: WMI timed out')
monkeypatch.setattr(identity, '_process_snapshot', snapshot)
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(identity, '_retained_exports', [])
launched = []
class Running:
pid = 987654
def __init__(self, *a, **k): launched.append(self)
def poll(self): return None
monkeypatch.setattr(identity.subprocess, 'Popen', Running)
result = identity._run_exports(source, root, tmp_path, 1)
assert not result['success']
assert result['safe_to_continue'] is (not after_launch)
assert result['native_started'] is after_launch
assert 'OWNER_QUERY_TIMEOUT' in result['detail']
assert bool(launched) is after_launch
assert bool(identity._retained_exports) is after_launch
def test_prelaunch_snapshot_timeout_releases_only_own_unstarted_guard(project, tmp_path, monkeypatch):
from cbb_refresh_verify import native_engineering_identity as identity
from cbb_refresh_verify.library_lock import LibraryLock, blocker_path
import win32event
from uuid import uuid4
source, library = project
create_mutex = win32event.CreateMutex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
create_mutex(security, owned, name+'-test-'+uuid4().hex))
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
calls = []
def snapshot():
calls.append('query')
raise RuntimeError('OWNER_QUERY_TIMEOUT: process tree unavailable')
monkeypatch.setattr(identity, '_process_snapshot', snapshot)
with LibraryLock(library):
result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert 'OWNER_QUERY_TIMEOUT' in result.detail
assert calls == ['query']
assert not blocker_path(library).exists()
@pytest.mark.parametrize('failure', ['source', 'lease', 'scope', 'exporters', 'directory', 'mutex'])
def test_capture_prevalidation_failure_explicitly_reports_no_native_session(project, tmp_path, monkeypatch, failure):
from cbb_refresh_verify import native_engineering_identity as identity, library_lock
import win32event
source, library = project
root = tmp_path/'analysis'
def reject(*args, **kwargs): raise OSError(f'prevalidation {failure} unavailable')
monkeypatch.setattr(library_lock, 'owned_snapshot', lambda _: b'owned lease')
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
monkeypatch.setattr(identity, '_run_exports', lambda *a, **k: pytest.fail('native export must not start'))
if failure == 'source': monkeypatch.setattr(identity, 'source_project', reject)
if failure == 'lease': monkeypatch.setattr(library_lock, 'owned_snapshot', lambda _: None)
if failure == 'scope': root = source.parent/'analysis'
if failure == 'exporters': monkeypatch.setattr(identity, 'check_exporters', reject)
if failure == 'directory':
mkdir = Path.mkdir
def create(path, *args, **kwargs):
if path == root: return reject()
return mkdir(path, *args, **kwargs)
monkeypatch.setattr(Path, 'mkdir', create)
if failure == 'mutex': monkeypatch.setattr(win32event, 'CreateMutex', reject)
result = identity.capture_identity(source, root, expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert result.detail
@pytest.mark.parametrize('failure', ['initial_launch_record', 'final_process_record'])
def test_export_record_io_preserves_known_launch_extent(project, tmp_path, monkeypatch, failure):
from cbb_refresh_verify import native_engineering_identity as identity, library_lock
import win32event
from uuid import uuid4
source, library = project
create_mutex = win32event.CreateMutex
monkeypatch.setattr(win32event, 'CreateMutex', lambda security, owned, name:
create_mutex(security, owned, name+'-test-'+uuid4().hex))
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(library_lock, '_supervisor_alive', lambda _: False)
launched = []
class Finished:
pid = 987654
returncode = 0
def __init__(self, *args, **kwargs): launched.append(self)
def poll(self): return 0
monkeypatch.setattr(identity.subprocess, 'Popen', Finished)
def content_failure(*args): raise ValueError('native export content rejected')
monkeypatch.setattr(identity, 'validate_export_output', content_failure)
write = identity._write_json
process_writes = []
def fail(path, record):
if path.name == 'export-launch.json' and failure == 'initial_launch_record':
raise OSError('launch metadata unavailable')
if path.name == 'ascii-process.json':
process_writes.append(record.copy())
if failure == 'final_process_record' and len(process_writes) == 2:
raise OSError('final process metadata unavailable')
return write(path, record)
monkeypatch.setattr(identity, '_write_json', fail)
with library_lock.LibraryLock(library):
result = identity.capture_identity(source, tmp_path/'analysis', expected_library=library,
owner_library=library, mentor_home=tmp_path)
assert result.identity is None and result.safe_to_continue
assert 'metadata unavailable' in result.detail
assert bool(launched) is (failure == 'final_process_record')
assert not library_lock.blocker_path(library).exists()
with library_lock.LibraryLock(library): pass
def test_successful_export_keeps_release_when_final_diagnostic_write_fails(project, tmp_path, monkeypatch):
from cbb_refresh_verify import native_engineering_identity as identity
source, _ = project
root = tmp_path/'exports'; root.mkdir()
monkeypatch.setattr(identity, '_process_snapshot', lambda: [])
monkeypatch.setattr(identity, '_database_released', lambda _: True)
monkeypatch.setattr(identity, 'validate_export_output', lambda *_: None)
class Finished:
pid = 987654
returncode = 0
def __init__(self, *args, **kwargs): pass
def poll(self): return 0
monkeypatch.setattr(identity.subprocess, 'Popen', Finished)
write = identity._write_json
counts = {}
def fail(path, record):
if path.name.endswith('-process.json'):
counts[path] = counts.get(path, 0)+1
if counts[path] == 2: raise OSError('final metadata unavailable')
return write(path, record)
monkeypatch.setattr(identity, '_write_json', fail)
result = identity._run_exports(source, root, tmp_path, 1)
assert result['success'] and result['safe_to_continue']
assert 'final metadata unavailable' in result['detail']
def edif_report(timestamp=b'2026 11 3 11 40 56'):
return (b'(edif DxD (edifVersion 2 0 0) (status (written (timestamp '+timestamp+b')'
b' (program "DxDesigner Edif Exporter"))) (library Board1 (cell Top'
+80
View File
@@ -116,3 +116,83 @@ def test_successful_export_clears_only_its_bound_guard_after_fresh_copy_proof(tm
finish_export_analysis(library, token)
assert 'export_analysis' not in json.loads(marker.read_text())
assert not marker.exists()
def test_unrelated_same_name_server_does_not_prevent_copy_release(export_case, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
from cbb_refresh_verify.native_export_guard import prove_export_copies_released
library, root, *_ = export_case
monkeypatch.setattr(identity, '_process_snapshot', lambda: [
{'pid': 777777, 'name': 'iCDBNetServer.exe', 'parent': 0, 'executable': 'native',
'command': 'iCDBNetServer.exe C:\\other\\database'}])
with prove_export_copies_released(library) as copies:
assert copies == [str(root/'project')]
def test_retained_export_registry_is_scoped_to_its_library(tmp_path, monkeypatch):
import cbb_refresh_verify.native_engineering_identity as identity
from cbb_refresh_verify.native_export_guard import assert_retained_exports_idle
from cbb_refresh_verify.library_lock import LibraryBusy
library_a, library_b = tmp_path/'a.lmc', tmp_path/'b.lmc'
monkeypatch.setattr(identity, '_retained_exports', [
(SimpleNamespace(poll=lambda: None), {}, {'library': str(library_a.resolve())})])
assert_retained_exports_idle(library_b)
with pytest.raises(LibraryBusy): assert_retained_exports_idle(library_a)
@pytest.mark.parametrize('created,blocked', [('new-process', False), ('original-process', True)])
def test_recorded_export_pid_reuse_requires_matching_creation_identity(export_case, monkeypatch, created, blocked):
import cbb_refresh_verify.native_engineering_identity as identity
import cbb_refresh_verify.library_lock as locks
from cbb_refresh_verify.native_export_guard import prove_export_copies_released
library, root, *_ = export_case
path = root/'ascii-process.json'
record = json.loads(path.read_text('utf-8'))
record['process_identities'] = [{'pid': 123456, 'created': 'original-process', 'executable': 'native'}]
path.write_text(json.dumps(record), encoding='utf-8')
monkeypatch.setattr(locks, '_supervisor_alive', lambda _: True)
monkeypatch.setattr(identity, '_process_snapshot', lambda: [
{'pid': 123456, 'name': 'unrelated.exe', 'parent': 0,
'executable': 'native', 'created': created, 'command': ''}])
if blocked:
with pytest.raises(locks.LibraryBusy):
with prove_export_copies_released(library): pass
else:
with prove_export_copies_released(library): pass
@pytest.mark.parametrize('active', [False, True])
def test_unknown_checkpoint_requires_explicit_full_proof_and_no_native_export_owner(tmp_path, monkeypatch, active):
import cbb_refresh_verify.native_engineering_identity as identity
import cbb_refresh_verify.library_lock as locks
from cbb_refresh_verify.native_export_guard import prove_export_copies_released
library = tmp_path/'lib.lmc'; library.touch()
locks.blocker_path(library).write_text('{torn metadata')
monkeypatch.setattr(identity, '_retained_exports', [])
monkeypatch.setattr(identity, '_process_snapshot', lambda: [
{'pid': 12345, 'name': 'iCDBNetServer.exe', 'parent': 0}] if active else [])
with pytest.raises(locks.LibraryBusy):
with prove_export_copies_released(library): pass
if active:
with pytest.raises(locks.LibraryBusy):
with prove_export_copies_released(library, allow_unknown_checkpoint=True): pass
else:
with prove_export_copies_released(library, allow_unknown_checkpoint=True) as copies:
assert copies == []
assert locks.blocker_path(library).read_text() == '{torn metadata'
@pytest.mark.parametrize('no_start_proven', [False, True])
def test_missing_pid_requires_bound_no_start_evidence(export_case, no_start_proven):
from cbb_refresh_verify.native_export_guard import prove_export_copies_released
from cbb_refresh_verify.library_lock import LibraryBusy
library, root, *_ = export_case
record = {'success': False, 'safe_to_continue': True}
if no_start_proven: record['native_started'] = False
(root/'ascii-process.json').write_text(json.dumps(record), encoding='utf-8')
(root/'export-launch.json').write_text(json.dumps({'started': not no_start_proven}), encoding='utf-8')
if no_start_proven:
with prove_export_copies_released(library): pass
else:
with pytest.raises(LibraryBusy):
with prove_export_copies_released(library): pass
+191 -191
View File
@@ -1,191 +1,191 @@
import json
from pathlib import Path
import pytest
def output(project, database, *, fault=False, busy=False):
text = f'Loading project file [{project}].\nChecking if project is in use.\n'
if fault:
text += (f'Project database [{database}] is inconsistent. It has been manually copied while the iCDB Server was running. '
"Please use iCDB Project Backup 'Repair project' functionality to fix this issue.\n")
return text + ('Project is in use.\n' if busy else
"Project is not in use. Full iCDB Project Backup's functionality is available.\nBackup information loaded correctly.\n")
@pytest.mark.parametrize('fault,busy,status,repair', [
(False, False, 'NOT_IN_USE', False), (True, False, 'NOT_IN_USE', True),
(True, True, 'IN_USE', False), (False, True, 'IN_USE', False)])
def test_native_ownership_and_repair_need_are_separate(tmp_path, fault, busy, status, repair):
from cbb_refresh_verify.native_occupancy import classify_inspection
project, database = tmp_path/'A.prj', tmp_path/'database'
result = classify_inspection(project, database, output(project, database, fault=fault, busy=busy))
assert result['status'] == status
assert result['repair_required'] is repair
@pytest.mark.parametrize('case', ['progress', 'wrong_project', 'wrong_database', 'stale_verdict', 'generic_repair'])
def test_incomplete_or_foreign_output_cannot_authorize_repair(tmp_path, case):
from cbb_refresh_verify.native_occupancy import classify_inspection
project, database = tmp_path/'A.prj', tmp_path/'database'
text = output(project, database, fault=True)
if case == 'progress': text = f'Loading project file [{project}].\nChecking if project is in use.'
if case == 'wrong_project': text = output(tmp_path/'Other.prj', database, fault=True)
if case == 'wrong_database': text = output(project, tmp_path/'other', fault=True)
if case == 'stale_verdict': text += 'Checking if project is in use.\n'
if case == 'generic_repair': text = output(project, database) + 'Please try to repair the project!'
assert classify_inspection(project, database, text)['repair_required'] is False
@pytest.mark.parametrize('fault,busy', [(False, False), (True, False), (False, True)])
def test_inspection_mode_only_observes_and_closes_its_tool(tmp_path, monkeypatch, fault, busy):
import win32con
from test_native_repair import repair_worker_fixture
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr(ui, '_repair_command', lambda *a: pytest.fail('inspection resolved Repair command'))
monkeypatch.setattr(ui, '_native_output', lambda *_: output(data['project'], data['database'], fault=fault, busy=busy))
assert ui.repair_worker_main(job) == 0
result = json.loads((root/'repair-result.json').read_text('utf-8'))
assert result['status'] == ('IN_USE' if busy else 'NOT_IN_USE')
assert result['release']['native_exited'] is True
assert result['repair_required'] is (fault and not busy)
assert actions == [(100, win32con.WM_CLOSE)]
def test_generic_project_backup_shell_is_accepted_as_the_single_target_window(tmp_path, monkeypatch):
import json
from test_native_repair import repair_worker_fixture
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr(ui, '_native_output', lambda windows: (
f'Loading project file [{data["project"]}].\nChecking if project is in use.\n'
"Project is not in use. Full iCDB Project Backup's functionality is available.\n"
'Backup information loaded correctly.'))
assert ui.repair_worker_main(job) == 0
def test_only_check_never_counts_as_repair_success():
from cbb_refresh_verify.icdb_maintenance import maintenance_counts
from cbb_refresh_verify.verify_runner import VerifyResult
counts = maintenance_counts([VerifyResult('CHECKED', safe_to_continue=True)])
assert counts['passed'] == counts['repaired'] == 0
assert counts['checked_without_repair'] == 1
def test_cancelled_probe_returns_within_grace_without_killing_native(tmp_path, monkeypatch):
from threading import Event
from types import SimpleNamespace
from cbb_refresh_verify import native_repair as native
from cbb_refresh_verify.native_repair import RepairTarget
stop = Event(); ticks = [0.0]; actions = []
def sleep(seconds):
ticks[0] += seconds
stop.set()
monkeypatch.setattr(native, 'time', SimpleNamespace(monotonic=lambda: ticks[0], sleep=sleep))
class Worker:
pid = 123
def __init__(self, *a): pass
def bind_cancellation(self, p): pass
def poll(self): return None
def request_stop(self): actions.append('cancel')
def close(self): actions.append('close_handles')
def kill(self): pytest.fail('native owner must not be killed')
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
result = native.run_native_repair(RepairTarget(tmp_path/'A.prj', tmp_path/'database'),
tmp_path/'evidence', timeout_seconds=600, inspection_only=True, stop=stop)
assert ticks[0] < 6
assert result['status'] == 'CANCELLED_UNRELEASED'
assert result['release']['safe_to_continue'] is False
assert actions[-1] == 'close_handles'
def test_ui_keeps_processing_events_and_passes_cancel_to_native_task(qtbot, tmp_path, monkeypatch):
from threading import Event
from PySide6.QtCore import QTimer
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
entered = Event(); cancelled = Event(); ticks = []
def verify(req, *, stop, **kw):
entered.set()
if stop.wait(3): cancelled.set()
return VerifyResult('CANCELLED')
monkeypatch.setattr('cbb_refresh_verify.verify_runner.run_verify', verify)
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
window.search_edit.setText('Beta')
timer = QTimer(window); timer.setInterval(10); timer.timeout.connect(lambda: ticks.append(True)); timer.start()
window.repair_icdb_all()
qtbot.waitUntil(entered.is_set)
qtbot.waitUntil(lambda: len(ticks) >= 3)
assert window.busy and window.stop_button.isEnabled()
window.stop_button.click()
qtbot.waitUntil(lambda: not window.busy)
assert cancelled.is_set()
@pytest.mark.parametrize('extra', ['foreign_session', 'extra_error'])
def test_foreign_or_additional_fault_is_never_a_clean_inspection(tmp_path, extra):
from cbb_refresh_verify.native_occupancy import classify_inspection
project, database = tmp_path/'A.prj', tmp_path/'database'
text = output(project, database, fault=extra == 'extra_error')
if extra == 'foreign_session':
text += ('The last server session on different server machine lost the connection to the database. '
'Machine:[SERVER] Path:[C:/Other/Work/database] session:[old]')
else:
text += 'ERROR: shared symbol database unavailable'
assert classify_inspection(project, database, text)['status'] == 'UNCONFIRMED'
def test_unconfirmed_inspection_closes_known_main_at_timeout(tmp_path, monkeypatch):
import win32con
from test_native_repair import repair_worker_fixture
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr(ui, '_native_output', lambda *_: 'Unrecognized response')
assert ui.repair_worker_main(job) == 1
result = json.loads((root/'repair-result.json').read_text('utf-8'))
assert result['status'] == 'UNCONFIRMED'
assert result['release']['native_exited'] is True
assert actions == [(100, win32con.WM_CLOSE)]
def test_directory_scan_honors_cancel_between_entries(tmp_path):
from cbb_refresh_verify.scoped_manifest import scan_tree
for i in range(10): (tmp_path/f'{i}.dat').touch()
calls = []
def cancel():
calls.append(True)
if len(calls) == 3: raise RuntimeError('CANCELLED')
with pytest.raises(RuntimeError, match='CANCELLED'):
scan_tree(tmp_path, check=cancel)
assert len(calls) == 3
def test_cancel_during_final_guard_cannot_launch_native_tool(tmp_path, monkeypatch):
from contextlib import contextmanager
from test_native_repair import repair_worker_fixture
from cbb_refresh_verify.native_repair import RepairTarget
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8'))
data['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
job.write_text(json.dumps(data), encoding='utf-8')
calls = []
@contextmanager
def guard(*a, **kw):
calls.append(True)
yield {'target': RepairTarget(Path(data['project']), Path(data['database']))}
Path(data['cancel_signal']).touch()
monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard)
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('late native launch'))
assert ui.repair_worker_main(job) == 1
assert len(calls) == 1 and not actions
def test_verified_cbb_refusal_is_not_hidden_by_secondary_release_failure():
from cbb_refresh_verify.verify_runner import classify_failure_category
detail = ('BackAnnotate: Back Annotation is not allowed on verified (or once verified) Reusable Blocks.'
'\nPCB_RELEASE_UNCONFIRMED: not released')
assert classify_failure_category(detail) == 'CBB_REIMPORT_REQUIRED'
import json
from pathlib import Path
import pytest
def output(project, database, *, fault=False, busy=False):
text = f'Loading project file [{project}].\nChecking if project is in use.\n'
if fault:
text += (f'Project database [{database}] is inconsistent. It has been manually copied while the iCDB Server was running. '
"Please use iCDB Project Backup 'Repair project' functionality to fix this issue.\n")
return text + ('Project is in use.\n' if busy else
"Project is not in use. Full iCDB Project Backup's functionality is available.\nBackup information loaded correctly.\n")
@pytest.mark.parametrize('fault,busy,status,repair', [
(False, False, 'NOT_IN_USE', False), (True, False, 'NOT_IN_USE', True),
(True, True, 'IN_USE', False), (False, True, 'IN_USE', False)])
def test_native_ownership_and_repair_need_are_separate(tmp_path, fault, busy, status, repair):
from cbb_refresh_verify.native_occupancy import classify_inspection
project, database = tmp_path/'A.prj', tmp_path/'database'
result = classify_inspection(project, database, output(project, database, fault=fault, busy=busy))
assert result['status'] == status
assert result['repair_required'] is repair
@pytest.mark.parametrize('case', ['progress', 'wrong_project', 'wrong_database', 'stale_verdict', 'generic_repair'])
def test_incomplete_or_foreign_output_cannot_authorize_repair(tmp_path, case):
from cbb_refresh_verify.native_occupancy import classify_inspection
project, database = tmp_path/'A.prj', tmp_path/'database'
text = output(project, database, fault=True)
if case == 'progress': text = f'Loading project file [{project}].\nChecking if project is in use.'
if case == 'wrong_project': text = output(tmp_path/'Other.prj', database, fault=True)
if case == 'wrong_database': text = output(project, tmp_path/'other', fault=True)
if case == 'stale_verdict': text += 'Checking if project is in use.\n'
if case == 'generic_repair': text = output(project, database) + 'Please try to repair the project!'
assert classify_inspection(project, database, text)['repair_required'] is False
@pytest.mark.parametrize('fault,busy', [(False, False), (True, False), (False, True)])
def test_inspection_mode_only_observes_and_closes_its_tool(tmp_path, monkeypatch, fault, busy):
import win32con
from test_native_repair import repair_worker_fixture
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr(ui, '_repair_command', lambda *a: pytest.fail('inspection resolved Repair command'))
monkeypatch.setattr(ui, '_native_output', lambda *_: output(data['project'], data['database'], fault=fault, busy=busy))
assert ui.repair_worker_main(job) == 0
result = json.loads((root/'repair-result.json').read_text('utf-8'))
assert result['status'] == ('IN_USE' if busy else 'NOT_IN_USE')
assert result['release']['native_exited'] is True
assert result['repair_required'] is (fault and not busy)
assert actions == [(100, win32con.WM_CLOSE)]
def test_generic_project_backup_shell_is_accepted_as_the_single_target_window(tmp_path, monkeypatch):
import json
from test_native_repair import repair_worker_fixture
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr(ui, '_native_output', lambda windows: (
f'Loading project file [{data["project"]}].\nChecking if project is in use.\n'
"Project is not in use. Full iCDB Project Backup's functionality is available.\n"
'Backup information loaded correctly.'))
assert ui.repair_worker_main(job) == 0
def test_only_check_never_counts_as_repair_success():
from cbb_refresh_verify.icdb_maintenance import maintenance_counts
from cbb_refresh_verify.verify_runner import VerifyResult
counts = maintenance_counts([VerifyResult('CHECKED', safe_to_continue=True)])
assert counts['passed'] == counts['repaired'] == 0
assert counts['checked_without_repair'] == 1
def test_cancelled_probe_returns_within_grace_without_killing_native(tmp_path, monkeypatch):
from threading import Event
from types import SimpleNamespace
from cbb_refresh_verify import native_repair as native
from cbb_refresh_verify.native_repair import RepairTarget
stop = Event(); ticks = [0.0]; actions = []
def sleep(seconds):
ticks[0] += seconds
stop.set()
monkeypatch.setattr(native, 'time', SimpleNamespace(monotonic=lambda: ticks[0], sleep=sleep))
class Worker:
pid = 123
def __init__(self, *a): pass
def bind_cancellation(self, p): pass
def poll(self): return None
def request_stop(self): actions.append('cancel')
def close(self): actions.append('close_handles')
def kill(self): pytest.fail('native owner must not be killed')
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
result = native.run_native_repair(RepairTarget(tmp_path/'A.prj', tmp_path/'database'),
tmp_path/'evidence', timeout_seconds=600, inspection_only=True, stop=stop)
assert ticks[0] < 6
assert result['status'] == 'CANCELLED_UNRELEASED'
assert result['release']['safe_to_continue'] is False
assert actions[-1] == 'close_handles'
def test_ui_keeps_processing_events_and_passes_cancel_to_native_task(qtbot, tmp_path, monkeypatch):
from threading import Event
from PySide6.QtCore import QTimer
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
entered = Event(); cancelled = Event(); ticks = []
def verify(req, *, stop, **kw):
entered.set()
if stop.wait(3): cancelled.set()
return VerifyResult('CANCELLED')
monkeypatch.setattr('cbb_refresh_verify.verify_runner.run_verify', verify)
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
window.search_edit.setText('Beta')
timer = QTimer(window); timer.setInterval(10); timer.timeout.connect(lambda: ticks.append(True)); timer.start()
window.repair_icdb_all()
qtbot.waitUntil(entered.is_set)
qtbot.waitUntil(lambda: len(ticks) >= 3)
assert not window.busy and window._icdb_active() and window.stop_button.isEnabled()
window.stop_button.click()
qtbot.waitUntil(lambda: not window._icdb_active())
assert cancelled.is_set()
@pytest.mark.parametrize('extra', ['foreign_session', 'extra_error'])
def test_foreign_or_additional_fault_is_never_a_clean_inspection(tmp_path, extra):
from cbb_refresh_verify.native_occupancy import classify_inspection
project, database = tmp_path/'A.prj', tmp_path/'database'
text = output(project, database, fault=extra == 'extra_error')
if extra == 'foreign_session':
text += ('The last server session on different server machine lost the connection to the database. '
'Machine:[SERVER] Path:[C:/Other/Work/database] session:[old]')
else:
text += 'ERROR: shared symbol database unavailable'
assert classify_inspection(project, database, text)['status'] == 'UNCONFIRMED'
def test_unconfirmed_inspection_closes_known_main_at_timeout(tmp_path, monkeypatch):
import win32con
from test_native_repair import repair_worker_fixture
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8')); data['inspection_only'] = True
job.write_text(json.dumps(data), encoding='utf-8')
monkeypatch.setattr(ui, '_native_output', lambda *_: 'Unrecognized response')
assert ui.repair_worker_main(job) == 1
result = json.loads((root/'repair-result.json').read_text('utf-8'))
assert result['status'] == 'UNCONFIRMED'
assert result['release']['native_exited'] is True
assert actions == [(100, win32con.WM_CLOSE)]
def test_directory_scan_honors_cancel_between_entries(tmp_path):
from cbb_refresh_verify.scoped_manifest import scan_tree
for i in range(10): (tmp_path/f'{i}.dat').touch()
calls = []
def cancel():
calls.append(True)
if len(calls) == 3: raise RuntimeError('CANCELLED')
with pytest.raises(RuntimeError, match='CANCELLED'):
scan_tree(tmp_path, check=cancel)
assert len(calls) == 3
def test_cancel_during_final_guard_cannot_launch_native_tool(tmp_path, monkeypatch):
from contextlib import contextmanager
from test_native_repair import repair_worker_fixture
from cbb_refresh_verify.native_repair import RepairTarget
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
data = json.loads(job.read_text('utf-8'))
data['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
job.write_text(json.dumps(data), encoding='utf-8')
calls = []
@contextmanager
def guard(*a, **kw):
calls.append(True)
yield {'target': RepairTarget(Path(data['project']), Path(data['database']))}
Path(data['cancel_signal']).touch()
monkeypatch.setattr('cbb_refresh_verify.maintenance_entry.maintenance_target_guard', guard)
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('late native launch'))
assert ui.repair_worker_main(job) == 1
assert len(calls) == 1 and not actions
def test_verified_cbb_refusal_is_not_hidden_by_secondary_release_failure():
from cbb_refresh_verify.verify_runner import classify_failure_category
detail = ('BackAnnotate: Back Annotation is not allowed on verified (or once verified) Reusable Blocks.'
'\nPCB_RELEASE_UNCONFIRMED: not released')
assert classify_failure_category(detail) == 'CBB_REIMPORT_REQUIRED'
+1 -1
View File
@@ -446,7 +446,7 @@ def test_repair_without_application_backup_still_rejects_busy_database(tmp_path,
try:
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('busy database launched'))
assert ui.repair_worker_main(job) == 1
assert 'REPAIR_DATABASE_IN_USE' in json.loads((root/'repair-result.json').read_text())['detail']
assert 'REPAIR_DATABASE_IN_USE' in json.loads((root/'repair-result.json').read_text(encoding='utf-8'))['detail']
finally:
handle.Close()
+65
View File
@@ -0,0 +1,65 @@
from cbb_refresh_verify import network_path as network
from test_network_path import FakeWNet
def test_releasing_a_keeps_shared_mapping_until_retained_b_releases(monkeypatch):
monkeypatch.setattr(network, '_retained_path_leases', {})
backend = FakeWNet()
original_a = network.LibraryPathLease.acquire(r'\\server\share\A\lib.lmc', backend=backend)
b = network.LibraryPathLease.acquire(r'\\server\share\B\lib.lmc', backend=backend)
original_a.retain_for_recovery(); b.retain_for_recovery()
current_a = network.LibraryPathLease.acquire(r'\\server\share\A\lib.lmc', backend=backend)
network.release_retained_path_leases(
{'status': 'RELEASE_CONFIRMED', 'library': str(original_a.mentor_path)}, current_lease=current_a)
current_a.close()
assert backend.cancelled == [] and b._retained_for_recovery and b.created
network.release_retained_path_leases({'status': 'RELEASE_CONFIRMED', 'library': str(b.mentor_path)})
assert backend.cancelled == [original_a.drive]
def test_unbound_directory_cannot_accept_an_arbitrary_sibling_library_proof(monkeypatch):
monkeypatch.setattr(network, '_retained_path_leases', {})
lease = network.LibraryPathLease.acquire(r'\\server\share\Folder', backend=FakeWNet())
lease.retain_for_recovery()
proof = {'status': 'RELEASE_CONFIRMED', 'library': r'\\server\share\Folder\other.lmc'}
assert not lease.release_after_recovery(proof)
# The library reader binds the selected LMC before retaining a loaded lease.
lease.mentor_path = lease.mentor_path/'chosen.lmc'
assert not lease.release_after_recovery(proof)
proof['library'] = r'\\server\share\Folder\chosen.lmc'
assert lease.release_after_recovery(proof)
def test_closing_scanner_lease_preserves_an_unpublished_live_reader_lease():
backend = FakeWNet()
scanner = network.LibraryPathLease.acquire(r'\\server\share\A\lib.lmc', backend=backend)
reader = network.LibraryPathLease.acquire(r'\\server\share\B\lib.lmc', backend=backend)
scanner.close()
assert not backend.cancelled and reader.created
reader.close()
assert backend.cancelled == [scanner.drive]
def test_acquire_and_close_do_not_race_before_new_reader_is_registered():
from threading import Event, Thread
entered, release = Event(), Event()
class Backend(FakeWNet):
block = False
def mappings(self):
snapshot = super().mappings()
if self.block:
entered.set(); assert release.wait(3)
return snapshot
backend = Backend()
scanner = network.LibraryPathLease.acquire(r'\\server\share\A\lib.lmc', backend=backend)
backend.block = True
leases = []
reader = Thread(target=lambda: leases.append(network.LibraryPathLease.acquire(
r'\\server\share\B\lib.lmc', backend=backend)))
closer = Thread(target=scanner.close)
reader.start(); assert entered.wait(1)
closer.start(); release.set()
reader.join(2); closer.join(2)
assert leases and not backend.cancelled and leases[0].created
leases[0].close()
assert backend.cancelled == [scanner.drive]
+69
View File
@@ -21,3 +21,72 @@ def test_malformed_wmi_response_is_not_empty_owner_list(monkeypatch):
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'
+242 -242
View File
@@ -1,242 +1,242 @@
from dataclasses import replace
from pathlib import Path
from threading import Event
from types import SimpleNamespace
import pytest
from PySide6.QtCore import QTimer
from PySide6.QtWidgets import QMessageBox
from cbb_refresh_verify.management import Capability, ManagementRequest, ManagementResult
from cbb_refresh_verify.verify_runner import VerifyResult
def failed_repair(path, *, safe=False):
return VerifyResult('FAIL', error_category='CBB_REIMPORT_REQUIRED',
stderr='Back Annotation is not allowed on verified (or once verified) Reusable Blocks.',
evidence_path=path, safe_to_continue=safe,
recovery_attempts=({'native_status': 'REPAIRED', 'status': 'REPAIR_VALIDATION_FAILED',
'validation_passed': False},))
def recovery_window(qtbot, tmp_path, monkeypatch, result=None):
from test_ui_filters import load_window
adapter = SimpleNamespace(capabilities=lambda: {op: Capability(True, 'available')
for op in ('add', 'update', 'remove')})
result = result or failed_repair(tmp_path/'maintenance/result.json')
window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter,
verifier=lambda _: result, status_refresher=lambda names, path: {n: 'PENDING' for n in names})
window.search_edit.setText('Beta')
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window.busy)
return window, result
def test_failed_repair_offers_copy_import_bound_to_failed_target(qtbot, tmp_path, monkeypatch):
window, failed = recovery_window(qtbot, tmp_path, monkeypatch)
assert '副本' in window.summary.text()
action = next(b for b in window.summary.buttons() if '副本' in b.text())
window.search_edit.setText('Alpha')
window.select_visible_button.click()
action.click()
dialog = window.management_dialog
requests = dialog.requests()
assert len(requests) == 1 and requests[0].name == 'Beta'
assert requests[0].partition == 'Power' and requests[0].operation == 'update'
assert requests[0].source is None and requests[0].auto_verify
assert '修复未完成' in dialog.status_label.text()
assert '手动备份' in dialog.status_label.text()
assert str(failed.evidence_path) in dialog.output.toPlainText()
dialog.reject()
assert failed.status == 'FAIL'
assert window.reimport_button.isEnabled()
def test_declining_reimport_confirmation_does_not_check_release_or_write(qtbot, tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
window, _ = recovery_window(qtbot, tmp_path, monkeypatch)
window.summary.close()
window.reimport_button.click()
dialog = window.management_dialog
source = tmp_path/'copy/test.prj'; source.parent.mkdir(); source.touch()
dialog.add_sources([source])
monkeypatch.setattr(locks, 'release_retained_library', lambda *a: pytest.fail('unconfirmed release'))
confirmations = []
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **kw:
confirmations.append(a[2]) or QMessageBox.No)
dialog.execute_button.click()
assert not dialog.busy and not dialog.executed
assert 'Beta' in confirmations[0] and str(source) in confirmations[0]
assert '副本' in confirmations[0] and '先移除' in confirmations[0]
dialog.reject()
@pytest.mark.parametrize('result', [VerifyResult('CHECKED'), VerifyResult('PASS', library_flag='GREEN'),
VerifyResult('BLOCKED', error_category='ICDB_DATABASE_IN_USE', safe_to_continue=False),
VerifyResult('FAIL', stderr='unknown error')])
def test_no_reimport_offer_for_unconfirmed_diagnosis_or_success(qtbot, tmp_path, monkeypatch, result):
window, _ = recovery_window(qtbot, tmp_path, monkeypatch, result)
assert not window.reimport_button.isVisible()
assert not any('副本' in b.text() for b in window.summary.buttons())
def test_changing_library_invalidates_old_reimport_offer(qtbot, tmp_path, monkeypatch):
window, _ = recovery_window(qtbot, tmp_path, monkeypatch)
old_action = next(b for b in window.summary.buttons() if '副本' in b.text())
window.lmc_edit.setText('another.lmc')
old_action.click()
assert not getattr(window, 'management_dialog', None)
assert not window.reimport_button.isVisible()
def request(tmp_path):
library = tmp_path/'lib/mentor.lmc'; library.parent.mkdir(); library.touch()
source = tmp_path/'copy/Block.prj'; source.parent.mkdir(); source.touch()
return ManagementRequest('update', library, 'Block', source=source, evidence_dir=tmp_path/'management')
def test_release_failure_preserves_failure_and_never_dispatches_management(tmp_path, monkeypatch):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
req = request(tmp_path)
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
def denied(*a): raise locks.LibraryBusy('PCB still active: 123')
monkeypatch.setattr(locks, 'release_retained_library', denied)
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: pytest.fail('unsafe write'))
results = run_reimport_recovery([req], previous_evidence={'Block': tmp_path/'old/result.json'},
evidence_dir=tmp_path/'reimport')
assert results[0].status == 'BLOCKED' and not results[0].safe_to_continue
assert '123' in results[0].detail
import json
record = json.loads(results[0].evidence_path.read_text('utf-8'))
assert record['previous_evidence']['Block'] == str(tmp_path/'old/result.json')
assert record['stage'] == 'release_failed'
def test_cancel_during_release_prevents_import(tmp_path, monkeypatch):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
req = request(tmp_path); stop = Event()
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
def release(*a):
stop.set()
return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'}
monkeypatch.setattr(locks, 'release_retained_library', release)
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: pytest.fail('cancelled write'))
results = run_reimport_recovery([req], previous_evidence={'Block': 'old.json'},
evidence_dir=tmp_path/'reimport', stop=stop)
assert results == []
@pytest.mark.parametrize('uploaded_status', ['UPLOADED', 'UPLOADED_VERIFY_FAILED', 'UPLOADED_VERIFIED'])
def test_copy_recovery_keeps_native_management_outcome_and_original_evidence(tmp_path, monkeypatch, uploaded_status):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
import json
req = request(tmp_path)
old = tmp_path/'old.json'; old.write_text('{"status":"FAIL"}', encoding='utf-8')
before = old.read_bytes()
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
monkeypatch.setattr(locks, 'release_retained_library', lambda *a:
{'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'})
expected = ManagementResult('update', req.library, req.name, req.partition, req.source,
True, 'complete', uploaded_status, True)
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: [expected])
results = run_reimport_recovery([req], previous_evidence={'Block': str(old)},
evidence_dir=tmp_path/'reimport')
assert results == [expected] and old.read_bytes() == before
record = json.loads(next((tmp_path/'reimport').glob('*/recovery.json')).read_text('utf-8'))
assert record['original_repair_succeeded'] is False
assert record['results'][0]['status'] == uploaded_status and record['version_review_required']
def test_incomplete_copy_never_releases_or_removes_target(tmp_path, monkeypatch):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
req = request(tmp_path) # Empty PRJ is deliberately incomplete.
monkeypatch.setattr(locks, 'release_retained_library', lambda *a: pytest.fail('incomplete source'))
results = run_reimport_recovery([req], previous_evidence={'Block': 'old.json'},
evidence_dir=tmp_path/'reimport')
assert results[0].status == 'BLOCKED' and results[0].stage == 'preflight'
def test_recovery_uses_background_release_then_existing_management(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
req = request(tmp_path); entered, finish = Event(), Event(); calls = []
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
def release(*a):
entered.set(); assert finish.wait(5)
calls.append('release')
return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'}
monkeypatch.setattr(locks, 'release_retained_library', release)
def run(items, **kw):
calls.append('management')
return [ManagementResult('update', req.library, req.name, req.partition, req.source,
True, 'complete', 'UPLOADED_VERIFIED', True)]
monkeypatch.setattr(management, 'run_management_batch', run)
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **kw: QMessageBox.Yes)
dialog = ManagementDialog([req], recovery_from={'Block': 'old.json'})
qtbot.addWidget(dialog)
ticks = []
timer = QTimer(); timer.setInterval(10); timer.timeout.connect(lambda: ticks.append(1)); timer.start()
try:
dialog.execute_button.click()
qtbot.waitUntil(entered.is_set)
qtbot.waitUntil(lambda: len(ticks) >= 3)
assert calls == [] and dialog.busy
finally:
finish.set()
qtbot.waitUntil(lambda: not dialog.busy)
timer.stop()
assert calls == ['release', 'management']
assert '版本待核对' in dialog.status_label.text()
def test_recovery_dialog_keeps_internal_details_in_optional_log(qtbot, tmp_path):
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
req = request(tmp_path)
dialog = ManagementDialog([req], recovery_from={'Block': 'old-result.json'})
qtbot.addWidget(dialog); dialog.show()
assert dialog.output.isHidden()
assert not any(word in dialog.status_label.text() for word in ('原生', '证据', '检查点', '记录保留'))
detail = 'OWNER_QUERY_TIMEOUT: diagnostic internals'
result = ManagementResult('update', req.library, req.name, req.partition, req.source,
True, 'release_check', 'BLOCKED', False, detail, tmp_path/'recovery.json')
dialog._result([result])
assert detail not in dialog.status_label.text()
assert '日志' in dialog.status_label.text()
dialog.logs_button.click()
assert dialog.output.isVisible() and detail in dialog.output.toPlainText()
def test_main_stop_during_recovery_release_cancels_import(qtbot, tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
window, _ = recovery_window(qtbot, tmp_path, monkeypatch)
window.summary.close(); window.reimport_button.click()
dialog = window.management_dialog
source = tmp_path/'copy/source.prj'; source.parent.mkdir(); source.touch()
dialog.add_sources([source])
monkeypatch.setattr(management, 'source_project', lambda *a: (source, source, 'Beta'))
entered, finish = Event(), Event()
def release(*args):
entered.set(); assert finish.wait(5)
return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'}
monkeypatch.setattr(locks, 'release_retained_library', release)
dispatched = []
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: dispatched.append(a) or [])
dialog.execute_button.click()
try:
qtbot.waitUntil(entered.is_set)
window.stop_button.click()
assert dialog.stop.is_set()
finally:
finish.set(); qtbot.waitUntil(lambda: not dialog.busy)
dialog.reject()
assert dispatched == []
from dataclasses import replace
from pathlib import Path
from threading import Event
from types import SimpleNamespace
import pytest
from PySide6.QtCore import QTimer
from PySide6.QtWidgets import QMessageBox
from cbb_refresh_verify.management import Capability, ManagementRequest, ManagementResult
from cbb_refresh_verify.verify_runner import VerifyResult
def failed_repair(path, *, safe=False):
return VerifyResult('FAIL', error_category='CBB_REIMPORT_REQUIRED',
stderr='Back Annotation is not allowed on verified (or once verified) Reusable Blocks.',
evidence_path=path, safe_to_continue=safe,
recovery_attempts=({'native_status': 'REPAIRED', 'status': 'REPAIR_VALIDATION_FAILED',
'validation_passed': False},))
def recovery_window(qtbot, tmp_path, monkeypatch, result=None):
from test_ui_filters import load_window
adapter = SimpleNamespace(capabilities=lambda: {op: Capability(True, 'available')
for op in ('add', 'update', 'remove')})
result = result or failed_repair(tmp_path/'maintenance/result.json')
window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter,
verifier=lambda _: result, status_refresher=lambda names, path: {n: 'PENDING' for n in names})
window.search_edit.setText('Beta')
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window._icdb_active())
return window, result
def test_failed_repair_offers_copy_import_bound_to_failed_target(qtbot, tmp_path, monkeypatch):
window, failed = recovery_window(qtbot, tmp_path, monkeypatch)
assert '副本' in window.summary.text()
action = next(b for b in window.summary.buttons() if '副本' in b.text())
window.search_edit.setText('Alpha')
window.select_visible_button.click()
action.click()
dialog = window.management_dialog
requests = dialog.requests()
assert len(requests) == 1 and requests[0].name == 'Beta'
assert requests[0].partition == 'Power' and requests[0].operation == 'update'
assert requests[0].source is None and requests[0].auto_verify
assert '修复未完成' in dialog.status_label.text()
assert '手动备份' in dialog.status_label.text()
assert str(failed.evidence_path) in dialog.output.toPlainText()
dialog.reject()
assert failed.status == 'FAIL'
assert window.reimport_button.isEnabled()
def test_declining_reimport_confirmation_does_not_check_release_or_write(qtbot, tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
window, _ = recovery_window(qtbot, tmp_path, monkeypatch)
window.summary.close()
window.reimport_button.click()
dialog = window.management_dialog
source = tmp_path/'copy/test.prj'; source.parent.mkdir(); source.touch()
dialog.add_sources([source])
monkeypatch.setattr(locks, 'release_retained_library', lambda *a: pytest.fail('unconfirmed release'))
confirmations = []
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **kw:
confirmations.append(a[2]) or QMessageBox.No)
dialog.execute_button.click()
assert not dialog.busy and not dialog.executed
assert 'Beta' in confirmations[0] and str(source) in confirmations[0]
assert '副本' in confirmations[0] and '先移除' in confirmations[0]
dialog.reject()
@pytest.mark.parametrize('result', [VerifyResult('CHECKED'), VerifyResult('PASS', library_flag='GREEN'),
VerifyResult('BLOCKED', error_category='ICDB_DATABASE_IN_USE', safe_to_continue=False),
VerifyResult('FAIL', stderr='unknown error')])
def test_no_reimport_offer_for_unconfirmed_diagnosis_or_success(qtbot, tmp_path, monkeypatch, result):
window, _ = recovery_window(qtbot, tmp_path, monkeypatch, result)
assert not window.reimport_button.isVisible()
assert not any('副本' in b.text() for b in window.summary.buttons())
def test_changing_library_invalidates_old_reimport_offer(qtbot, tmp_path, monkeypatch):
window, _ = recovery_window(qtbot, tmp_path, monkeypatch)
old_action = next(b for b in window.summary.buttons() if '副本' in b.text())
window.lmc_edit.setText('another.lmc')
old_action.click()
assert not getattr(window, 'management_dialog', None)
assert not window.reimport_button.isVisible()
def request(tmp_path):
library = tmp_path/'lib/mentor.lmc'; library.parent.mkdir(); library.touch()
source = tmp_path/'copy/Block.prj'; source.parent.mkdir(); source.touch()
return ManagementRequest('update', library, 'Block', source=source, evidence_dir=tmp_path/'management')
def test_release_failure_preserves_failure_and_never_dispatches_management(tmp_path, monkeypatch):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
req = request(tmp_path)
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
def denied(*a): raise locks.LibraryBusy('PCB still active: 123')
monkeypatch.setattr(locks, 'release_retained_library', denied)
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: pytest.fail('unsafe write'))
results = run_reimport_recovery([req], previous_evidence={'Block': tmp_path/'old/result.json'},
evidence_dir=tmp_path/'reimport')
assert results[0].status == 'BLOCKED' and not results[0].safe_to_continue
assert '123' in results[0].detail
import json
record = json.loads(results[0].evidence_path.read_text('utf-8'))
assert record['previous_evidence']['Block'] == str(tmp_path/'old/result.json')
assert record['stage'] == 'release_failed'
def test_cancel_during_release_prevents_import(tmp_path, monkeypatch):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
req = request(tmp_path); stop = Event()
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
def release(*a):
stop.set()
return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'}
monkeypatch.setattr(locks, 'release_retained_library', release)
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: pytest.fail('cancelled write'))
results = run_reimport_recovery([req], previous_evidence={'Block': 'old.json'},
evidence_dir=tmp_path/'reimport', stop=stop)
assert results == []
@pytest.mark.parametrize('uploaded_status', ['UPLOADED', 'UPLOADED_VERIFY_FAILED', 'UPLOADED_VERIFIED'])
def test_copy_recovery_keeps_native_management_outcome_and_original_evidence(tmp_path, monkeypatch, uploaded_status):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
import json
req = request(tmp_path)
old = tmp_path/'old.json'; old.write_text('{"status":"FAIL"}', encoding='utf-8')
before = old.read_bytes()
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
monkeypatch.setattr(locks, 'release_retained_library', lambda *a:
{'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'})
expected = ManagementResult('update', req.library, req.name, req.partition, req.source,
True, 'complete', uploaded_status, True)
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: [expected])
results = run_reimport_recovery([req], previous_evidence={'Block': str(old)},
evidence_dir=tmp_path/'reimport')
assert results == [expected] and old.read_bytes() == before
record = json.loads(next((tmp_path/'reimport').glob('*/recovery.json')).read_text('utf-8'))
assert record['original_repair_succeeded'] is False
assert record['results'][0]['status'] == uploaded_status and record['version_review_required']
def test_incomplete_copy_never_releases_or_removes_target(tmp_path, monkeypatch):
from cbb_refresh_verify.reimport_recovery import run_reimport_recovery
import cbb_refresh_verify.library_lock as locks
req = request(tmp_path) # Empty PRJ is deliberately incomplete.
monkeypatch.setattr(locks, 'release_retained_library', lambda *a: pytest.fail('incomplete source'))
results = run_reimport_recovery([req], previous_evidence={'Block': 'old.json'},
evidence_dir=tmp_path/'reimport')
assert results[0].status == 'BLOCKED' and results[0].stage == 'preflight'
def test_recovery_uses_background_release_then_existing_management(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
req = request(tmp_path); entered, finish = Event(), Event(); calls = []
monkeypatch.setattr(management, 'source_project', lambda *a: (req.source, req.source, 'Block'))
def release(*a):
entered.set(); assert finish.wait(5)
calls.append('release')
return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'}
monkeypatch.setattr(locks, 'release_retained_library', release)
def run(items, **kw):
calls.append('management')
return [ManagementResult('update', req.library, req.name, req.partition, req.source,
True, 'complete', 'UPLOADED_VERIFIED', True)]
monkeypatch.setattr(management, 'run_management_batch', run)
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **kw: QMessageBox.Yes)
dialog = ManagementDialog([req], recovery_from={'Block': 'old.json'})
qtbot.addWidget(dialog)
ticks = []
timer = QTimer(); timer.setInterval(10); timer.timeout.connect(lambda: ticks.append(1)); timer.start()
try:
dialog.execute_button.click()
qtbot.waitUntil(entered.is_set)
qtbot.waitUntil(lambda: len(ticks) >= 3)
assert calls == [] and dialog.busy
finally:
finish.set()
qtbot.waitUntil(lambda: not dialog.busy)
timer.stop()
assert calls == ['release', 'management']
assert '版本待核对' in dialog.status_label.text()
def test_recovery_dialog_keeps_internal_details_in_optional_log(qtbot, tmp_path):
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
req = request(tmp_path)
dialog = ManagementDialog([req], recovery_from={'Block': 'old-result.json'})
qtbot.addWidget(dialog); dialog.show()
assert dialog.output.isHidden()
assert not any(word in dialog.status_label.text() for word in ('原生', '证据', '检查点', '记录保留'))
detail = 'OWNER_QUERY_TIMEOUT: diagnostic internals'
result = ManagementResult('update', req.library, req.name, req.partition, req.source,
True, 'release_check', 'BLOCKED', False, detail, tmp_path/'recovery.json')
dialog._result([result])
assert detail not in dialog.status_label.text()
assert '日志' in dialog.status_label.text()
dialog.logs_button.click()
assert dialog.output.isVisible() and detail in dialog.output.toPlainText()
def test_main_stop_during_recovery_release_cancels_import(qtbot, tmp_path, monkeypatch):
import cbb_refresh_verify.library_lock as locks
import cbb_refresh_verify.management as management
window, _ = recovery_window(qtbot, tmp_path, monkeypatch)
window.summary.close(); window.reimport_button.click()
dialog = window.management_dialog
source = tmp_path/'copy/source.prj'; source.parent.mkdir(); source.touch()
dialog.add_sources([source])
monkeypatch.setattr(management, 'source_project', lambda *a: (source, source, 'Beta'))
entered, finish = Event(), Event()
def release(*args):
entered.set(); assert finish.wait(5)
return {'status': 'RELEASE_CONFIRMED', 'evidence_path': 'release.json'}
monkeypatch.setattr(locks, 'release_retained_library', release)
dispatched = []
monkeypatch.setattr(management, 'run_management_batch', lambda *a, **kw: dispatched.append(a) or [])
dialog.execute_button.click()
try:
qtbot.waitUntil(entered.is_set)
window.stop_button.click()
assert dialog.stop.is_set()
finally:
finish.set(); qtbot.waitUntil(lambda: not dialog.busy)
dialog.reject()
assert dispatched == []
+130
View File
@@ -0,0 +1,130 @@
from pathlib import Path
from PySide6.QtCore import Qt
def test_preview_lists_residuals_and_does_not_close_before_confirmation(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify import library_lock
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
plan = {'library': str(tmp_path/'lib.lmc'), 'sessions': [
{'project': 'A', 'name': 'iCDBProjectBackup.exe', 'pid': 10, 'detail': '可正常关闭'}]}
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda *_: plan)
calls = []
monkeypatch.setattr(library_lock, 'release_residual_sessions', lambda *a, **k: calls.append(a))
dialog = ResidualReleaseDialog(tmp_path/'lib.lmc', tmp_path/'evidence')
qtbot.addWidget(dialog); dialog.show()
qtbot.waitUntil(lambda: not dialog.busy)
assert dialog.table.item(0, 0).text() == 'A'
assert dialog.table.item(0, 2).text() == '10'
assert not calls
dialog.close()
assert not calls
def test_empty_process_list_explains_historical_records_without_claiming_activity(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify import library_lock
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda *_: {
'library': str(tmp_path/'lib.lmc'), 'sessions': [], 'previous_records': [
{'path': 'old-record.json', 'time': '2026-11-04T09:43:50+00:00', 'pid': 57076}]})
dialog = ResidualReleaseDialog(tmp_path/'lib.lmc', tmp_path/'evidence')
qtbot.addWidget(dialog)
qtbot.waitUntil(lambda: not dialog.busy)
assert '未发现活跃会话' in dialog.message.text()
assert '历史操作记录' in dialog.message.text() and '2026-11-04' in dialog.message.text()
assert dialog.confirm_button.isEnabled() and not dialog.force_button.isEnabled()
dialog.close()
def test_confirm_releases_full_plan_and_only_emits_actual_library_proof(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify import library_lock
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
plan = {'library': str(tmp_path/'lib.lmc'), 'sessions': [
{'project': name, 'name': 'iCDBProjectBackup.exe', 'pid': pid, 'detail': '可正常关闭'}
for name, pid in [('A', 10), ('B', 20)]]}
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda *_: plan)
proof = {'status': 'RELEASE_CONFIRMED', 'library': plan['library'], 'evidence_path': 'proof.json'}
calls = []
def release(actual, *a, **kw):
calls.append(actual)
return {'status': 'RELEASE_CONFIRMED', 'proof': proof, 'sessions': [
{**row, 'release_status': 'RELEASED', 'detail': '已正常退出'} for row in actual['sessions']]}
monkeypatch.setattr(library_lock, 'release_residual_sessions', release)
dialog = ResidualReleaseDialog(tmp_path/'lib.lmc', tmp_path/'evidence')
qtbot.addWidget(dialog); dialog.show()
released = []
dialog.released.connect(released.append)
qtbot.waitUntil(lambda: not dialog.busy)
qtbot.mouseClick(dialog.confirm_button, Qt.LeftButton)
qtbot.waitUntil(lambda: not dialog.busy)
assert calls == [plan] and released == [proof]
assert '已释放 2' in dialog.message.text()
assert not dialog.confirm_button.isEnabled()
def test_main_window_opens_confirmation_list_and_returns_to_idle_when_closed(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify import library_lock
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda library, **kw:
{'library': str(library), 'sessions': []})
window = load_window(qtbot, tmp_path, monkeypatch)
window.check_library_release()
dialog = getattr(window, 'release_dialog', None)
assert dialog is not None, 'Release entry must preview sessions before cleanup'
qtbot.waitUntil(lambda: not dialog.busy)
assert window.busy and dialog.confirm_button.isEnabled()
dialog.close()
qtbot.waitUntil(lambda: not window.busy)
def test_force_close_is_explicit_and_cancel_does_not_release(qtbot, tmp_path, monkeypatch):
from PySide6.QtWidgets import QMessageBox
from cbb_refresh_verify import library_lock
from cbb_refresh_verify.ui.residual_release_dialog import ResidualReleaseDialog
plan = {'library': str(tmp_path/'lib.lmc'), 'sessions': [
{'project': 'A', 'name': 'ExpeditionPCB.exe', 'pid': 10, 'detail': '窗口无响应'}]}
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda *_: plan)
calls = []
def release(actual, *a, **kw):
calls.append(kw['force'])
return {'status': 'PARTIAL', 'sessions': [{**plan['sessions'][0], 'release_status': 'WAITING'}]}
monkeypatch.setattr(library_lock, 'release_residual_sessions', release)
dialog = ResidualReleaseDialog(tmp_path/'lib.lmc', tmp_path/'evidence')
qtbot.addWidget(dialog)
qtbot.waitUntil(lambda: not dialog.busy)
monkeypatch.setattr(QMessageBox, 'warning', lambda *a: QMessageBox.Cancel)
dialog.force_button.click()
assert not calls
monkeypatch.setattr(QMessageBox, 'warning', lambda *a: QMessageBox.Yes)
dialog.force_button.click()
qtbot.waitUntil(lambda: not dialog.busy)
assert calls == [True]
def test_released_batch_can_resume_only_its_unexecuted_items(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.verify_runner import VerifyResult
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: VerifyResult('FAIL', stderr='release unconfirmed', safe_to_continue=False))
window._run(['Beta', 'Gamma'])
qtbot.waitUntil(lambda: not window.busy)
action = getattr(window, '_resume_batch_action', None)
assert action is not None and not action.isEnabled()
library = Path(window.mentor_lmc_path or window.loaded_path)
from cbb_refresh_verify import library_lock
monkeypatch.setattr(library_lock, 'inspect_residual_sessions', lambda *_:
{'library': str(library), 'sessions': []})
monkeypatch.setattr(library_lock, 'release_residual_sessions', lambda *a, **kw:
{'status': 'RELEASE_CONFIRMED', 'sessions': [], 'proof': {'status': 'RELEASE_CONFIRMED',
'library': str(library), 'evidence_path': str(tmp_path/'proof.json')}})
window._residual_release_action.click()
qtbot.waitUntil(lambda: not window.release_dialog.busy)
window.release_dialog.confirm_button.click()
qtbot.waitUntil(lambda: not window.release_dialog.busy)
window.release_dialog.close()
qtbot.waitUntil(lambda: not window.busy)
assert window.summary.isVisible()
assert action.isEnabled()
calls = []
monkeypatch.setattr(window, '_run', lambda names, **kw: calls.append((names, kw)))
action.click()
assert calls == [(['Gamma'], {'maintenance': False})]
+229
View File
@@ -0,0 +1,229 @@
from pathlib import Path
from types import SimpleNamespace
import pytest
def api():
from cbb_refresh_verify import library_lock
inspect = getattr(library_lock, 'inspect_residual_sessions', None)
release = getattr(library_lock, 'release_residual_sessions', None)
assert callable(inspect) and callable(release), 'Missing confirmed residual-session release workflow'
return inspect, release
def row(pid, project, *, name='iCDBProjectBackup.exe'):
return {'pid': pid, 'name': name, 'parent': 1, 'created': f'created-{pid}',
'executable': 'C:/Mentor/'+name, 'command': f'{name} -p "{project}"'}
def test_preview_limits_to_selected_projects_and_keeps_process_identity(tmp_path, monkeypatch):
inspect, _ = api()
from cbb_refresh_verify import residual_sessions as sessions
library = tmp_path/'lib.lmc'; library.touch()
a = library.parent/'ReusableBlocksLP/A/Work/A.prj'
b = library.parent/'ReusableBlocksLP/B/Work/B.prj'
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [row(10, a), row(20, b)])
monkeypatch.setattr(sessions, '_process_identity', lambda r: {'created_handle': r['created'], 'image': r['executable']})
monkeypatch.setattr(sessions, '_process_windows', lambda pids: {p: [] for p in pids})
result = inspect(library, block_names=('A',))
assert [r['pid'] for r in result['sessions']] == [10]
assert result['sessions'][0]['created'] == 'created-10'
def test_unknown_scope_is_visible_but_never_closed_even_with_force(tmp_path, monkeypatch):
from cbb_refresh_verify import residual_sessions as sessions
library = tmp_path/'lib.lmc'; library.touch()
process = row(10, ''); process['command'] = 'LibraryManager.exe'
process['name'] = 'LibraryManager.exe'
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [process])
monkeypatch.setattr(sessions, '_process_windows', lambda _: {})
monkeypatch.setattr(sessions, '_process_identity', lambda r: {'created_handle': 'old', 'image': r['executable']})
plan = sessions.inspect_residual_sessions(library)
assert plan['sessions'] and plan['sessions'][0]['scope_confirmed'] is False
monkeypatch.setattr(sessions, '_request_close', lambda *a, **kw: pytest.fail('closed unknown scope'))
monkeypatch.setattr(sessions, '_force_close', lambda *a, **kw: pytest.fail('ended unknown scope'))
for force in (False, True):
result = sessions.release_residual_sessions(plan, tmp_path.parent/'release-evidence'/tmp_path.name, force=force)
assert result['status'] == 'PARTIAL'
# A request file becoming readable must not expand the user's consent list.
monkeypatch.setattr('cbb_refresh_verify.library_lock._owner_matches_scope', lambda *a: True)
result = sessions.release_residual_sessions(plan, tmp_path.parent/'release-evidence'/tmp_path.name, force=True)
assert result['status'] == 'PARTIAL'
@pytest.mark.parametrize('name,title,children,expected', [
('iCDBProjectBackup.exe', 'iCDB Project Backup - test', [], True),
('ExpeditionPCB.exe', 'Expedition PCB - Reusable Block', [], True),
('ExpeditionPCB.exe', 'Design.pcb - Expedition PCB', [], True),
('iCDBProjectBackup.exe', 'iCDB Project Backup - test', [{'class': 'msctls_progress32', 'text': ''}], False),
])
def test_only_idle_supported_native_windows_are_offered_for_close(name, title, children, expected):
api()
from cbb_refresh_verify.residual_sessions import close_target
windows = [{'hwnd': 100, 'class': 'Afx:main', 'title': title, 'enabled': True,
'children': children, 'documents': [], 'output': 'Backup information loaded correctly.'}]
target, reason = close_target(name, windows)
assert bool(target) is expected
if not expected: assert reason
def test_confirmation_rechecks_pid_reuse_and_never_posts_to_replacement(tmp_path, monkeypatch):
inspect, release = api()
from cbb_refresh_verify import residual_sessions as sessions
library = tmp_path/'lib.lmc'; library.touch()
process = row(10, tmp_path/'ReusableBlocksLP/A/Work/A.prj')
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [process.copy()])
monkeypatch.setattr(sessions, '_process_identity', lambda r: {'created_handle': 'old', 'image': r['executable']})
monkeypatch.setattr(sessions, '_process_windows', lambda _: {10: []})
plan = inspect(library)
process['created'] = 'replacement'
monkeypatch.setattr(sessions, '_request_close', lambda *_: pytest.fail('closed replacement PID'))
monkeypatch.setattr(sessions, '_finish_library_release', lambda *_: {'status': 'RELEASE_CONFIRMED'})
result = release(plan, tmp_path.parent/'release-evidence'/tmp_path.name)
assert result['sessions'][0]['release_status'] == 'IDENTITY_CHANGED'
assert result['status'] == 'PARTIAL'
def test_close_requests_cover_whole_preview_and_failures_remain_per_item(tmp_path, monkeypatch):
inspect, release = api()
from cbb_refresh_verify import residual_sessions as sessions
library = tmp_path/'lib.lmc'; library.touch()
rows = [row(p, tmp_path/f'ReusableBlocksLP/{p}/Work/{p}.prj') for p in (10, 20)]
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: rows)
monkeypatch.setattr(sessions, '_process_identity', lambda r: {'created_handle': r['created'], 'image': r['executable']})
monkeypatch.setattr(sessions, '_process_windows', lambda _: {})
calls = []
monkeypatch.setattr(sessions, '_request_close', lambda r, *_, **kw: calls.append(r['pid']) or
{'release_status': 'RELEASED' if r['pid'] == 10 else 'WAITING', 'detail': 'native window pending'})
monkeypatch.setattr(sessions, '_finish_library_release', lambda *_: pytest.fail('partial release cleared library marker'))
result = release(inspect(library), tmp_path.parent/'release-evidence'/tmp_path.name)
assert calls == [10, 20]
assert result['status'] == 'PARTIAL'
assert [r['release_status'] for r in result['sessions']] == ['RELEASED', 'WAITING']
def test_no_remaining_processes_rechecks_and_clears_old_metadata(tmp_path, monkeypatch):
inspect, release = api()
from cbb_refresh_verify import residual_sessions as sessions
library = tmp_path/'lib.lmc'; library.touch()
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [])
monkeypatch.setattr(sessions, '_process_windows', lambda _: {})
proof = {'status': 'RELEASE_CONFIRMED', 'library': str(library), 'evidence_path': 'proof.json'}
monkeypatch.setattr(sessions, '_finish_library_release', lambda *_: proof)
result = release(inspect(library), tmp_path.parent/'release-evidence'/tmp_path.name)
assert result['status'] == 'RELEASE_CONFIRMED' and result['proof'] == proof
def test_empty_session_scan_still_exposes_historical_operation_record(tmp_path, monkeypatch):
import json
from cbb_refresh_verify import residual_sessions as sessions
from cbb_refresh_verify.library_lock import blocker_path
library = tmp_path/'lib.lmc'; library.touch()
marker = blocker_path(library)
marker.write_text(json.dumps({'protocol': 'cbb-refresh-verify-library-lease', 'version': 1,
'library': str(library), 'lease_id': 'a'*32, 'pid': 57076,
'status': 'OWNER_RELEASE_UNCONFIRMED', 'time': '2026-11-04T09:43:50+00:00'}))
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [])
result = sessions.inspect_residual_sessions(library)
assert result['sessions'] == []
assert result['previous_records'][0]['path'] == str(marker)
assert result['previous_records'][0]['time'].startswith('2026-11-04')
assert marker.exists()
@pytest.mark.parametrize('output', [None, 'Repairing project\nRepair backup created successfully.',
'Repairing project\nBackup information loaded correctly.\nRestoring project'])
def test_backup_intermediate_success_does_not_allow_cooperative_close(output):
from cbb_refresh_verify.residual_sessions import close_target
window = {'hwnd': 10, 'class': 'Afx:main', 'title': 'iCDB Project Backup - test',
'children': [], 'enabled': True, 'output': output}
assert close_target('iCDBProjectBackup.exe', [window])[0] is None
def test_force_requires_separate_explicit_argument_and_rechecks_identity(tmp_path, monkeypatch):
from cbb_refresh_verify import residual_sessions as sessions
library = tmp_path/'lib.lmc'; library.touch()
process = row(10, tmp_path/'ReusableBlocksLP/A/Work/A.prj')
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [process.copy()])
monkeypatch.setattr(sessions, '_process_identity', lambda r: {'created_handle': 'old', 'image': r['executable']})
monkeypatch.setattr(sessions, '_process_windows', lambda _: {})
monkeypatch.setattr(sessions, '_request_close', lambda *a, **k: {'release_status': 'WAITING', 'detail': 'hung'})
forced = []
monkeypatch.setattr(sessions, '_force_close', lambda r, **kw: forced.append(r['pid']) or
{'release_status': 'WAITING', 'detail': 'exiting'})
plan = sessions.inspect_residual_sessions(library)
evidence = tmp_path.parent/'release-evidence'/tmp_path.name
sessions.release_residual_sessions(plan, evidence)
assert not forced
sessions.release_residual_sessions(plan, evidence, force=True)
assert forced == [10]
process['created'] = 'reused'
sessions.release_residual_sessions(plan, evidence, force=True)
assert forced == [10]
def test_current_process_handle_identity_uses_real_windows_api():
import os
from cbb_refresh_verify.residual_sessions import _process_identity
identity = _process_identity({'pid': os.getpid()})
assert identity['created_handle'] and Path(identity['image']).is_file()
def test_force_checks_exact_handle_before_ending_owned_test_helper():
import subprocess
import sys
from cbb_refresh_verify.residual_sessions import _process_identity, _force_close
child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'],
creationflags=subprocess.CREATE_NO_WINDOW)
try:
process = {'pid': child.pid, **_process_identity({'pid': child.pid})}
changed = {**process, 'created_handle': 'different process'}
assert _force_close(changed)['release_status'] == 'IDENTITY_CHANGED'
assert child.poll() is None
assert _force_close(process)['release_status'] == 'RELEASED'
assert child.wait(3) == 1
finally:
if child.poll() is None:
child.terminate(); child.wait(3) # This test's sleep-only helper.
def test_partial_desktop_enumeration_never_offers_normal_close(monkeypatch):
import win32gui, win32service
from cbb_refresh_verify.residual_sessions import _process_windows, close_target
def failed(*_): raise OSError('desktop disappeared')
monkeypatch.setattr(win32gui, 'EnumWindows', failed)
monkeypatch.setattr(win32service, 'GetProcessWindowStation', lambda: SimpleNamespace(EnumDesktops=lambda: []))
windows = _process_windows({10})[10]
assert any(w['class'] == 'UNCONFIRMED' for w in windows)
windows.append({'hwnd': 100, 'class': 'Afx:main', 'title': 'Design - Expedition PCB',
'children': [], 'enabled': True})
assert close_target('ExpeditionPCB.exe', windows)[0] is None
@pytest.mark.parametrize('choice', [6, 7, 2])
def test_save_answers_require_current_exact_prompt_and_process(monkeypatch, choice):
import win32con, win32gui, win32process
from contextlib import nullcontext
from cbb_refresh_verify import residual_sessions as sessions
process = row(10, 'C:/Lib/ReusableBlocksLP/A/Work/A.prj')
process.update(created_handle='creation', image='pcb.exe')
dialog = {'hwnd': 100, 'class': '#32770', 'title': 'Expedition PCB', 'children': [
{'class': 'Static', 'text': 'Save changes to the modified design?'},
*[{'class': 'Button', 'text': text, 'enabled': True, 'id': code, 'hwnd': code}
for code, text in ((6, 'Yes'), (7, 'No'), (2, 'Cancel'))]]}
windows = [dialog]
process['prompt'] = sessions._save_question(windows)
monkeypatch.setattr(sessions, 'query_process_snapshot', lambda: [process])
monkeypatch.setattr(sessions, '_process_handle', lambda _: nullcontext(123))
monkeypatch.setattr(sessions, '_handle_identity', lambda _: {'created_handle': 'creation', 'image': 'pcb.exe'})
monkeypatch.setattr(sessions, '_process_windows', lambda _: {10: windows})
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda _: (1, 10))
clicked = []
monkeypatch.setattr(win32gui, 'PostMessage', lambda *a: clicked.append(a))
sessions._answer_save_question(process, choice)
assert clicked == [(choice, win32con.BM_CLICK, 0, 0)]
dialog['children'][0]['text'] = 'Save changes to another modified design?'
with pytest.raises(ValueError, match='提示已变化'):
sessions._answer_save_question(process, choice)
assert len(clicked) == 1
+11
View File
@@ -12,6 +12,17 @@ def test_phase_duration_uses_worker_monotonic_time_not_delayed_poll_time():
assert timer.snapshot()[0]['pcb_startup'] == 132.0
def test_opening_check_ends_startup_and_close_without_starting_integration():
timer = RunTimings(clock=lambda: 50.0)
timer.record({'stage': 'pcb_launch', 'monotonic_seconds': 10})
timer.record({'stage': 'diagnostic_native_loaded_candidate', 'monotonic_seconds': 30})
timer.record({'stage': 'diagnostic_complete', 'monotonic_seconds': 38})
values, unfinished = timer.snapshot()
assert values['pcb_startup'] == 20
assert values['diagnostic_close'] == 8
assert 'integration' not in values and not unfinished
def test_action_and_save_subtimings_accumulate_real_durations():
timer = RunTimings(clock=lambda: 1000.0)
timer.record({'stage': 'integration_action_complete', 'action': 'ForwardAnnotate',
+3 -2
View File
@@ -112,7 +112,8 @@ def _without_authorized_checkpoint_cleanup(text):
"""Exempt exact protocol-checkpoint callsites, never the whole module."""
tree = ast.parse(text)
parents = {child: parent for parent in ast.walk(tree) for child in ast.iter_child_nodes(parent)}
allowed = {('LibraryLock', '_exit_owned'): 'marker', ('release_retained_library',): 'checkpoint'}
allowed = {('LibraryLock', '_exit_owned'): 'marker', ('LibraryLock', '__enter__'): 'marker',
('release_retained_library',): 'checkpoint'}
used, spans = set(), []
for call in ast.walk(tree):
if not (isinstance(call, ast.Call) and isinstance(call.func, ast.Attribute)
@@ -126,7 +127,7 @@ def _without_authorized_checkpoint_cleanup(text):
scope = tuple(scope)
if scope in used or allowed.get(scope) != call.func.value.id:
continue
expected = ([] if scope == ('release_retained_library',) else [('missing_ok', True)])
expected = ([('missing_ok', True)] if scope == ('LibraryLock', '_exit_owned') else [])
keywords = [(k.arg, k.value.value if isinstance(k.value, ast.Constant) else None) for k in call.keywords]
if keywords == expected and call.lineno == call.end_lineno:
spans.append((call.lineno - 1, call.col_offset, call.end_col_offset))
+46
View File
@@ -0,0 +1,46 @@
"""Foreign PCB windows must not become this scan's evidence or cleanup targets."""
from dataclasses import replace
import os
from pathlib import Path
import pytest
@pytest.mark.parametrize('suffix,flag', [('.backup', '-ReusableCircuit'), ('', '-ReusableCircuitOther')])
def test_reusable_pcb_binding_requires_exact_path_and_switch(tmp_path, suffix, flag):
from cbb_refresh_verify.mentor_adapter import is_reusable_circuit_pcb
pcb = tmp_path/'Board.pcb'
assert not is_reusable_circuit_pcb('C:/Mentor/ExpeditionPCB.exe',
f'ExpeditionPCB.exe "{pcb}{suffix}" {flag}', pcb)
def test_agent_roundtrip_applies_pid_scope_and_rejects_window_changed_to_foreign_owner(tmp_path):
from test_dialog_agent import running_protocol, snapshot, FakeExecutor
from cbb_refresh_verify.dialog_models import DialogPhase, stable_fingerprint
class Probe:
value = snapshot()
scopes = []
def capture(self, phase, *, pids=None):
self.scopes.append(pids)
return (self.value,) if self.value.pid in pids else ()
def matches_identity(self, expected):
return self.value.pid == expected.pid
probe, executor = Probe(), FakeExecutor()
client, _, thread = running_protocol(tmp_path, probe, executor)
try:
assert client.observe(DialogPhase.PCB_STARTING, pids={99}) == ()
owned = client.observe(DialogPhase.PCB_STARTING, pids={20})[0]
probe.value = replace(probe.value, pid=99)
result = client.perform(hwnd=owned.hwnd, fingerprint=stable_fingerprint(owned),
action={'action': 'confirm', 'channels': ['win32_button_click']}, phase=DialogPhase.PCB_STARTING)
assert result.error_code == 'STALE_DIALOG' and not executor.calls
finally:
client.close(); thread.join(2)
def test_process_watch_polls_bound_handles_without_wmi(monkeypatch):
from cbb_refresh_verify import owner_probe
from cbb_refresh_verify.pcb_bridge import ProcessWatch
monkeypatch.setattr(owner_probe, 'query_owners', lambda *a, **kw: pytest.fail('Polled WMI during COM attach'))
with ProcessWatch({os.getpid()}) as watch:
assert all(watch.alive() for _ in range(20))
+12 -12
View File
@@ -47,7 +47,7 @@ def test_global_icdb_action_confirms_and_excludes_ready_rows(qtbot, tmp_path, mo
assert calls == []
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
window.icdb_repair_button.click()
qtbot.waitUntil(lambda: not window.busy)
qtbot.waitUntil(lambda: not window._icdb_active())
assert calls == ['PENDING']
def test_global_icdb_action_disabled_without_library_or_while_busy(qtbot):
@@ -72,11 +72,11 @@ def test_global_icdb_failure_preserves_untargeted_ready_and_stops_queue(qtbot, t
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)
qtbot.waitUntil(lambda: not window._icdb_active())
assert calls == ['B']
assert 'A' in window.ready_names
assert window.cbb_table.item(1, 2).text() == '未知 / 需确认'
assert window.cbb_table.item(2, 3).text().startswith('SKIPPED')
assert window.cbb_table.item(2, 3).text() == '未检查'
def test_global_icdb_progress_and_check_summary(qtbot, tmp_path, monkeypatch):
from PySide6.QtWidgets import QMessageBox
@@ -92,7 +92,7 @@ def test_global_icdb_progress_and_check_summary(qtbot, tmp_path, monkeypatch):
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)
qtbot.waitUntil(lambda: not window._icdb_active())
assert 'lib.lmc' in confirmations[0]
assert '非 READY' in confirmations[0]
assert window.icdb_progress.value() == 1
@@ -284,7 +284,7 @@ def test_unc_library_uses_mentor_path_for_read_and_verify_and_releases_on_close(
assert calls == [lease.mentor_path]
assert window._request('CBB1').lmc_file == lease.mentor_path
window.close()
assert lease.closed
qtbot.waitUntil(lambda: lease.closed)
def test_advanced_options_are_copied_to_verify_request(qtbot, tmp_path):
@@ -353,12 +353,12 @@ def test_ui_default_timeout_matches_request_and_labels_inactivity(qtbot):
assert window.timeout_spin.value() == 600
assert window.timeout_spin.value() == VerifyRequest(Path('lib.lmc'), 'CBB1', Path('data')).timeout_seconds
assert '无进展' in window.timeout_spin.suffix()
def test_batch_summary_exposes_failures_only_report(qtbot, tmp_path):
window = MainWindow(reader=fake_reader, data_root=tmp_path)
qtbot.addWidget(window)
report=tmp_path/'reports/batch-20260929-test.log'
window._batch_done(([VerifyResult('FAIL',stderr='Mentor error')],report))
def test_batch_summary_exposes_failures_only_report(qtbot, tmp_path):
window = MainWindow(reader=fake_reader, data_root=tmp_path)
qtbot.addWidget(window)
report=tmp_path/'reports/batch-20260929-test.log'
window._batch_done(([VerifyResult('FAIL',stderr='Mentor error')],report))
assert '仅失败日志' in window.summary.detailedText()
assert str(report.with_name('failures-20260929-test.log')) in window.summary.detailedText()
+308
View File
@@ -0,0 +1,308 @@
"""No native applications or shared execution mutex are used by these regressions."""
import json
from dataclasses import replace
from pathlib import Path
from types import SimpleNamespace
import pytest
from cbb_refresh_verify import verify_runner as runner
from cbb_refresh_verify.status_bridge import BridgeResult
from test_verify_runner import fixture, request, PORTS_OK
@pytest.fixture
def isolated_verify(tmp_path, monkeypatch):
import win32event
fixture(tmp_path)
monkeypatch.setattr(win32event, 'CreateMutex', lambda *_: SimpleNamespace(Close=lambda: None))
monkeypatch.setattr(win32event, 'WaitForSingleObject', lambda *_: 0)
monkeypatch.setattr(win32event, 'ReleaseMutex', lambda *_: None)
monkeypatch.setattr(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 *_: None)
monkeypatch.setattr(runner, 'read_library_flag_if_available',
lambda *_: BridgeResult('READY', 'IsVerifiedReuseBlock', 1))
return request(tmp_path)
def test_invalid_proof_is_safe_before_worker_launch(isolated_verify, monkeypatch):
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker',
lambda *_: pytest.fail('invalid proof launched a worker'))
req = replace(isolated_verify, post_repair_proof=object())
results = runner.run_batch([req, req], verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK))
assert [r.status for r in results] == ['BLOCKED', 'BLOCKED']
assert all(r.safe_to_continue for r in results)
@pytest.mark.parametrize('failure', ['directory', 'runtime', 'request', 'worker_log'])
def test_prelaunch_disk_failure_does_not_stop_independent_items(isolated_verify, monkeypatch, failure):
class FinishedWorker:
desktop = None
returncode = 0
def __init__(self, command, *_a, **_k):
Path(command[-1]).with_name('outcome.json').write_text('{"status":"PASS"}')
def poll(self): return 0
def close(self): pass
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', FinishedWorker)
mkdir, write, open_file = Path.mkdir, Path.write_text, Path.open
def make(path, *a, **k):
if failure == 'directory' and path.name.startswith('verify-'):
raise OSError('evidence volume unavailable')
return mkdir(path, *a, **k)
def save(path, *a, **k):
if path.name == {'runtime': 'runtime.json', 'request': 'request.json'}.get(failure):
raise OSError('evidence volume unavailable')
return write(path, *a, **k)
def opening(path, *a, **k):
if failure == 'worker_log' and path.name == 'worker.log':
raise OSError('evidence volume unavailable')
return open_file(path, *a, **k)
monkeypatch.setattr(Path, 'mkdir', make)
monkeypatch.setattr(Path, 'write_text', save)
monkeypatch.setattr(Path, 'open', opening)
from cbb_refresh_verify import library_lock as locks
try:
results = runner.run_batch([isolated_verify, isolated_verify],
verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK))
assert all(r.safe_to_continue for r in results)
assert [r.status for r in results] == (['PASS', 'PASS'] if failure == 'runtime' else ['FAIL', 'FAIL'])
assert locks.LibraryLock(isolated_verify.lmc_file).key not in locks._owners
finally:
owner = locks._owners.pop(locks.LibraryLock(isolated_verify.lmc_file).key, None)
if owner: owner['handle'].Close()
def test_coordination_permission_failure_does_not_manufacture_native_owner(isolated_verify, monkeypatch):
from cbb_refresh_verify.library_lock import LibraryLock
def denied(_): raise PermissionError('coordination directory is read-only')
monkeypatch.setattr(LibraryLock, '__enter__', denied)
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker',
lambda *_: pytest.fail('permission failure launched native work'))
results = runner.run_batch([isolated_verify, isolated_verify],
verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK))
assert [r.status for r in results] == ['FAIL', 'FAIL']
assert all(r.safe_to_continue and r.error_category == 'LIBRARY_ACCESS_FAILED' for r in results)
@pytest.mark.parametrize('launch_state', ['never_created', 'unknown', 'partially_created'])
def test_constructor_release_evidence_controls_batch_continuation(isolated_verify, monkeypatch, launch_state):
proven_rollback = launch_state == 'never_created'
launches = []
def fail_launch(*_a, **_k):
launches.append(True)
error = RuntimeError('worker launch failed')
if launch_state != 'unknown':
error.worker_launch_release = {'safe_to_continue': True, 'worker_exited': True,
'remaining_pids': [], 'process_created': not proven_rollback}
raise error
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', fail_launch)
from cbb_refresh_verify import library_lock as locks
try:
results = runner.run_batch([isolated_verify, isolated_verify],
verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK))
assert len(launches) == (2 if proven_rollback else 1)
assert results[0].safe_to_continue is proven_rollback
if proven_rollback:
proof = json.loads(results[0].evidence_path.with_name('session-release.json').read_text('utf-8'))
assert proof['worker_exited'] and proof['safe_to_continue']
assert locks.LibraryLock(isolated_verify.lmc_file).key not in locks._owners
finally:
with locks._guard:
owner = locks._owners.pop(locks.LibraryLock(isolated_verify.lmc_file).key, None)
if owner:
owner['handle'].Close()
@pytest.mark.parametrize('filename', ['result.json', 'performance.json', 'bridge-status.json'])
def test_optional_evidence_write_failure_preserves_released_result(isolated_verify, monkeypatch, filename):
class FinishedWorker:
desktop = None
returncode = 0
def __init__(self, command, *_a, **_k):
Path(command[-1]).with_name('outcome.json').write_text('{"status":"PASS"}')
def poll(self): return 0
def close(self): pass
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', FinishedWorker)
original = Path.write_text
def write(path, *a, **k):
if path.name == filename:
raise OSError('report disk unavailable')
return original(path, *a, **k)
monkeypatch.setattr(Path, 'write_text', write)
results = runner.run_batch([isolated_verify, isolated_verify],
verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK))
assert [r.status for r in results] == ['PASS', 'PASS']
assert all(r.safe_to_continue for r in results)
def test_progress_failure_does_not_stop_safe_batch(isolated_verify):
def failed_progress(*_):
raise RuntimeError('view was closed')
results = runner.run_batch([isolated_verify, isolated_verify],
verifier=lambda _: runner.VerifyResult('FAIL', safe_to_continue=True), progress=failed_progress)
assert len(results) == 2 and all(r.safe_to_continue for r in results)
def test_performance_progress_failure_preserves_release(isolated_verify, monkeypatch):
root = isolated_verify.evidence_dir
root.mkdir()
result = runner.VerifyResult('FAIL', safe_to_continue=True, evidence_path=root/'result.json')
monkeypatch.setattr(runner, '_run_verify', lambda *a, **k: result)
def progress(event):
if event['stage'] == 'performance':
raise RuntimeError('view was closed')
actual = runner._run_verify_serialized(isolated_verify, progress=progress)
assert actual.safe_to_continue and actual.status == 'FAIL'
def test_performance_logger_failure_preserves_release(isolated_verify, monkeypatch):
def unavailable_log(*_a, **_k):
raise OSError('log destination unavailable')
req = replace(isolated_verify, logging_context=SimpleNamespace(logger=SimpleNamespace(info=unavailable_log)))
monkeypatch.setattr(runner, '_run_verify', lambda *a, **k: runner.VerifyResult('FAIL', safe_to_continue=True))
result = runner._run_verify_serialized(req)
assert result.safe_to_continue and result.status == 'FAIL'
def test_management_unsafe_survives_switching_back_to_library(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.management import Capability
adapter = SimpleNamespace(capabilities=lambda: {op: Capability(True, 'available')
for op in ('add', 'update', 'remove')})
window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter)
window.path_lease_factory = lambda path: SimpleNamespace(mentor_path=path, close=lambda: None,
retain_for_recovery=lambda: None)
library_a = tmp_path/'a.lmc'
library_b = tmp_path/'b.lmc'
window._management_unsafe = True
window._management_unsafe_library = library_a
for library, blocked in [(library_b, False), (library_a, True)]:
window.lmc_edit.setText(str(library))
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert window._management_library_unsafe(library) is blocked
assert not window._management_block_reason('add')
assert window.batch_button.isEnabled()
@pytest.mark.parametrize('released', [True, False])
def test_batch_report_failure_preserves_ui_outcomes(qtbot, tmp_path, monkeypatch, released):
from test_ui_filters import load_window
import cbb_refresh_verify.ui.main_window as ui
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: runner.VerifyResult('FAIL', stderr='item failed', safe_to_continue=released),
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
retained = []
window.library_lease.retain_for_recovery = lambda: retained.append(True)
monkeypatch.setattr(ui, 'write_batch_report', lambda *_: (_ for _ in ()).throw(OSError('report unavailable')))
window._run(['Beta', 'Gamma'])
qtbot.waitUntil(lambda: not window.busy)
assert bool(retained) is not released
assert 'report unavailable' in window.output.toPlainText()
assert 'item failed' in window.output.toPlainText()
assert window.batch_button.isEnabled()
def test_release_check_clears_only_its_own_management_library(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
from cbb_refresh_verify.management import Capability
adapter = SimpleNamespace(capabilities=lambda: {op: Capability(True, 'available')
for op in ('add', 'update', 'remove')})
window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter)
window.path_lease_factory = lambda path: SimpleNamespace(mentor_path=path, close=lambda: None,
retain_for_recovery=lambda: None)
library_a, library_b = tmp_path/'a.lmc', tmp_path/'b.lmc'
for library in (library_a, library_b):
window._management_unsafe = True
window._management_unsafe_library = library
window._update()
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library',
lambda *_: {'status': 'RELEASE_CONFIRMED', 'library': str(library_a),
'evidence_path': str(tmp_path/'release.json')})
window.lmc_edit.setText(str(library_a))
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
window._complete_library_release(library_a, {'status': 'RELEASE_CONFIRMED',
'library': str(library_a), 'evidence_path': str(tmp_path/'release.json')})
qtbot.waitUntil(lambda: not window.busy)
assert not window._management_block_reason('add')
window.lmc_edit.setText(str(library_b))
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
assert window._management_library_unsafe(library_b)
assert not window._management_block_reason('add')
@pytest.mark.parametrize('alias', ['absolute', 'mapped'])
def test_native_backup_session_matches_library_alias_without_network_resolution(qtbot, tmp_path, monkeypatch, alias):
from test_ui_filters import load_window
window = load_window(qtbot, tmp_path, monkeypatch)
if alias == 'absolute':
current = Path.cwd()/'test.lmc'
session_library = Path('test.lmc')
session_lease = None
else:
current = Path('T:/folder/lib.lmc')
session_library = Path('S:/folder/lib.lmc')
window.library_lease = SimpleNamespace(mentor_path=current, display_path=current,
drive='T:', remote_root=r'\\server\share', close=lambda: None)
session_lease = SimpleNamespace(mentor_path=session_library, display_path=session_library,
drive='S:', remote_root=r'\\server\share')
window.mentor_lmc_path = current
window._native_backup_session = SimpleNamespace(library=session_library, path_lease=session_lease)
try:
with monkeypatch.context() as context:
context.setattr(Path, 'resolve', lambda *_a, **_k: pytest.fail('UI touched network resolution'))
assert window._native_backup_same_library()
finally:
window._native_backup_session = None
@pytest.mark.parametrize('proof_kind', ['valid', 'foreign', 'invalid', 'lease_error'])
def test_release_check_restores_current_mapping_only_after_matching_proof(qtbot, tmp_path, monkeypatch, proof_kind):
from test_ui_filters import load_window
from cbb_refresh_verify import network_path
window = load_window(qtbot, tmp_path, monkeypatch)
library = Path('test.lmc').absolute()
current = network_path.LibraryPathLease(library, library)
current.retain_for_recovery()
window.library_lease = current
window.mentor_lmc_path = library
window._management_unsafe = True
window._management_unsafe_library = library
session = SimpleNamespace(library=library, path_lease=current, process=SimpleNamespace(poll=lambda: 0))
window._native_backup_session = session
proof = {'status': 'UNCONFIRMED' if proof_kind == 'invalid' else 'RELEASE_CONFIRMED',
'library': str(tmp_path/'foreign.lmc') if proof_kind == 'foreign' else str(library),
'evidence_path': str(tmp_path/'release.json')}
monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library', lambda *_: proof)
if proof_kind == 'lease_error':
def failed_release(*_a, **_k):
raise network_path.NetworkPathError('mapping release failed')
monkeypatch.setattr(network_path, 'release_retained_path_leases', failed_release)
try:
window._complete_library_release(library, proof)
qtbot.waitUntil(lambda: window._mapping_busy == 0)
qtbot.waitUntil(lambda: window._lease_pending == 0)
assert current._retained_for_recovery is (proof_kind != 'valid')
assert not current._closed
assert window._management_unsafe is (proof_kind != 'valid')
assert (window._native_backup_session is session) is (proof_kind != 'valid')
finally:
window._native_backup_session = None
network_path._retained_path_leases.pop(id(current), None)
def test_opening_evidence_failure_does_not_retain_library(qtbot, tmp_path, monkeypatch):
from test_ui_filters import load_window
window = load_window(qtbot, tmp_path, monkeypatch)
retained = []
window.library_lease.retain_for_recovery = lambda: retained.append(True)
window.recovery_path = tmp_path
def cannot_open(*_):
raise OSError('file viewer unavailable')
monkeypatch.setattr('os.startfile', cannot_open)
window.recover()
assert not retained
assert 'file viewer unavailable' in window.output.toPlainText()
+291 -291
View File
@@ -1,291 +1,291 @@
"""Local catalog scope and authoritative status refresh regression tests."""
import json
import time
from threading import Event
from pathlib import Path
from types import SimpleNamespace
import pytest
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QMessageBox
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.verify_runner import VerifyResult
MIXED = (
CbbRecord('Alpha', 'Power', status='READY'),
CbbRecord('Beta', 'Power', status='PENDING'),
CbbRecord('Gamma', 'Analog', status='UNKNOWN'),
CbbRecord('Delta', 'Analog', status='ERROR'),
CbbRecord('Missing', 'Power', status='READY'),
)
def load_window(qtbot, tmp_path, monkeypatch, records=MIXED, **kwargs):
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
import cbb_refresh_verify.ui.main_window as ui
monkeypatch.setattr(ui, 'preflight_skip_reason',
lambda _, name: 'missing project' if name == 'Missing' else None)
lease = SimpleNamespace(mentor_path=Path('test.lmc'), close=lambda: None,
retain_for_recovery=lambda: None)
window = MainWindow(reader=lambda _: LibraryReadResult(records), data_root=tmp_path,
path_lease_factory=lambda _: lease, **kwargs)
qtbot.addWidget(window)
window.lmc_edit.setText('test.lmc')
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
return window
def names(records):
return [record.name for record in records]
def test_search_and_filters_use_cached_catalog_and_count_visible_states(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.reader = lambda _: pytest.fail('filter reread Mentor')
assert window.visible_counts() == {'visible': 5, 'ready': 2, 'error': 3}
expected = {
'ready': ['Alpha', 'Missing'], 'non_ready': ['Beta', 'Gamma', 'Delta'],
'pending': ['Beta'], 'errors': ['Gamma', 'Delta'], 'missing': ['Missing'],
}
for mode, wanted in expected.items():
window.status_filter.setCurrentIndex(window.status_filter.findData(mode))
assert names(window.visible_records()) == wanted
window.status_filter.setCurrentIndex(window.status_filter.findData('all'))
window.search_edit.setText(' ANALOG ')
assert names(window.visible_records()) == ['Gamma', 'Delta']
assert window.visible_counts() == {'visible': 2, 'ready': 0, 'error': 2}
window.search_edit.setText('aLpH')
assert names(window.visible_records()) == ['Alpha']
assert '1' in window.counts_label.text()
def test_management_selection_includes_ready_and_missing_but_hiding_clears_checks(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.select_visible_button.click()
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Gamma', 'Delta', 'Missing']
assert window.selected_records()[0].logical_type == 'LP'
window.search_edit.setText('power')
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing']
window.search_edit.clear()
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing']
window.clear_selection_button.click()
assert window.selected_records() == ()
def test_verify_all_and_selected_only_target_visible_non_ready_with_projects(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=lambda names, _: {name: 'READY' for name in names})
window.search_edit.setText('power')
window.select_visible_button.click()
window.verify_selected()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta']
window.search_edit.setText('analog')
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta', 'Gamma', 'Delta']
def test_maintenance_confirmation_progress_and_report_use_visible_checked_subset(qtbot, tmp_path, monkeypatch):
calls, confirmations, refreshes = [], [], []
def refresh(targets, _):
refreshes.append(tuple(targets))
return {name: 'PENDING' for name in targets}
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=refresh)
window.search_edit.setText('analog')
window.cbb_table.item(3, 0).setCheckState(Qt.Checked)
monkeypatch.setattr(QMessageBox, 'question', lambda *args, **_: (
confirmations.append(args[2]) or QMessageBox.Yes))
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Delta']
assert refreshes == [('Delta',)]
assert '1' in confirmations[0] and '非 READY' in confirmations[0]
assert window.icdb_progress.maximum() == 1 and window.icdb_progress.value() == 1
report = json.loads(next((tmp_path/'reports').glob('icdb-*.json')).read_text(encoding='utf-8'))
assert report['includes_ready'] is False
assert report['target_names'] == ['Delta']
assert report['preflight_skipped'] == []
assert report['counts']['total'] == 1
assert window.search_edit.text() == 'analog'
assert window.cbb_table.item(3, 3).text() == 'PASS'
assert names(window.visible_records()) == ['Gamma', 'Delta']
assert window.visible_records()[1].status == 'PENDING'
def test_maintenance_defaults_to_visible_eligible_and_ready_only_checks_do_not_fallback(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=lambda targets, _: {name: 'PENDING' for name in targets})
monkeypatch.setattr(QMessageBox, 'question', lambda *_, **__: QMessageBox.Yes)
window.cbb_table.item(0, 0).setCheckState(Qt.Checked)
window.repair_icdb_all()
assert not window.busy and calls == []
window.clear_selection_button.click()
window.search_edit.setText('power')
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta']
def test_recovery_defaults_to_visible_checked_non_ready_and_skips_ready(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.search_edit.setText('power')
window.restore_backup()
assert [window.backup_dialog.block_combo.itemText(i)
for i in range(window.backup_dialog.block_combo.count())] == ['Beta']
window.backup_dialog.reject()
window.search_edit.clear()
window.cbb_table.item(3, 0).setCheckState(Qt.Checked)
window.restore_backup()
assert window.backup_dialog.block_combo.currentText() == 'Delta'
assert window.backup_dialog.block_combo.count() == 1
window.backup_dialog.reject()
def test_action_pass_does_not_promote_pending_without_authoritative_ready(qtbot, tmp_path, monkeypatch):
entered = []
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda targets, _: (entered.extend(targets) or {'Beta': 'READY'}))
window.status_filter.setCurrentIndex(window.status_filter.findData('pending'))
window.cbb_table.item(1, 0).setCheckState(Qt.Checked)
window._progress({'block': 'Beta', 'stage': 'PASS'})
assert names(window.visible_records()) == ['Beta']
assert window.visible_records()[0].status == 'PENDING'
window.refresh_affected_records(['Beta'])
qtbot.waitUntil(lambda: not window.busy)
assert entered == ['Beta']
assert window.visible_records() == ()
assert window.selected_records() == ()
assert window.status_filter.currentData() == 'pending'
window.status_filter.setCurrentIndex(window.status_filter.findData('ready'))
assert names(window.visible_records()) == ['Alpha', 'Beta', 'Missing']
assert window.cbb_table.item(1, 3).text() == 'PASS'
def test_refresh_failure_demotes_affected_without_losing_other_statuses(qtbot, tmp_path, monkeypatch):
def fail(*_):
raise OSError('bridge unavailable')
window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=fail)
window.refresh_affected_records(['Alpha'])
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'UNKNOWN'
assert window.visible_records()[1].status == 'PENDING'
assert 'Alpha' not in window.ready_names
assert 'bridge unavailable' in window.output.toPlainText()
def test_unsafe_result_defers_native_refresh_and_retains_action_evidence(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: VerifyResult('FAIL', stderr='owner release unconfirmed',
safe_to_continue=False, evidence_path=tmp_path/'evidence.json'),
status_refresher=lambda *_: pytest.fail('unsafe owner was reopened'))
window.search_edit.setText('beta')
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'UNKNOWN'
assert window.cbb_table.item(1, 3).text() == 'FAIL: owner release unconfirmed'
assert str(tmp_path/'evidence.json') in window.output.toPlainText()
assert '暂缓原生状态复核' in window.cbb_table.item(1, 2).toolTip()
def test_refresh_callback_uses_only_known_affected_records_and_preserves_logical_type(qtbot, tmp_path, monkeypatch):
observed, callbacks = [], []
def refresh(targets, path):
observed.append((targets, path))
return {'X': 'READY'}
window = load_window(qtbot, tmp_path, monkeypatch,
records=(CbbRecord('X', 'Display label', logical_type='RULP', status='PENDING'),),
status_refresher=refresh)
assert window.refresh_affected_records(['unknown']) is False
assert window.refresh_affected_records(['X', 'X', 'unknown'], callbacks.append) is True
assert window.refresh_affected_records(['X']) is False
qtbot.waitUntil(lambda: not window.busy)
assert observed == [(('X',), Path('test.lmc'))]
assert callbacks == [(CbbRecord('X', 'Display label', logical_type='RULP', status='READY'),)]
def test_backup_completion_refreshes_status_without_dialog_close_unlocking_pending_refresh(qtbot, tmp_path, monkeypatch):
entered, release = Event(), Event()
def refresh(targets, _):
entered.set()
release.wait(3)
return {name: 'PENDING' for name in targets}
window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=refresh)
window.search_edit.setText('beta')
window.restore_backup()
window.backup_dialog.started.emit('Beta')
window.backup_dialog.completed.emit('Beta', VerifyResult('PASS'))
try:
qtbot.waitUntil(entered.is_set)
window.backup_dialog.reject()
assert window.busy
finally:
release.set()
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'PENDING'
assert window.cbb_table.item(1, 3).text() == 'PASS'
def test_missing_project_keeps_explicit_authoritative_status(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
assert 'Ready' in window.cbb_table.item(4, 2).text()
assert '缺少工程' in window.cbb_table.item(4, 2).text()
def test_open_backup_dialog_cannot_restore_target_that_became_ready(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda targets, _: {name: 'READY' for name in targets})
window.search_edit.setText('beta')
window.restore_backup()
dialog = window.backup_dialog
source = tmp_path/'Beta.prj'
source.touch()
dialog.source_edit.setText(str(source))
dialog.recoverer = lambda *_, **__: (calls.append('restore') or VerifyResult('PASS'))
window.refresh_affected_records(['Beta'])
qtbot.waitUntil(lambda: not window.busy)
dialog.restore_button.click()
qtbot.waitUntil(lambda: not dialog.busy)
assert calls == []
assert '非 READY' in dialog.status_label.text()
dialog.reject()
@pytest.mark.parametrize('status', ['UNKNOWN', 'UNAVAILABLE', 'ERROR', 'TIMEOUT', 'MALFORMED', 'CRASHED',
'UNRECOGNIZED'])
def test_unconfirmed_statuses_are_non_ready_and_error_filter_candidates(qtbot, tmp_path, monkeypatch, status):
window = load_window(qtbot, tmp_path, monkeypatch, records=(CbbRecord('X', 'LP', status=status),))
window.status_filter.setCurrentIndex(window.status_filter.findData('non_ready'))
assert names(window.visible_records()) == ['X']
window.status_filter.setCurrentIndex(window.status_filter.findData('errors'))
assert names(window.visible_records()) == ['X']
@pytest.mark.parametrize('size', [381, 3000])
def test_large_catalog_local_filter_and_selection_remain_responsive(qtbot, tmp_path, monkeypatch, size):
records = tuple(CbbRecord(f'CBB{i:04}', 'Power' if i % 2 else 'Analog',
status='READY' if i % 3 == 0 else 'PENDING') for i in range(size))
start = time.perf_counter()
window = load_window(qtbot, tmp_path, monkeypatch, records=records)
loaded = time.perf_counter() - start
window.reader = lambda _: pytest.fail('local actions reread Mentor')
start = time.perf_counter()
window.search_edit.setText('power')
window.select_visible_button.click()
window.status_filter.setCurrentIndex(window.status_filter.findData('pending'))
window.clear_selection_button.click()
elapsed = time.perf_counter() - start
print(f'catalog rows={size} load={loaded:.3f}s local_actions={elapsed:.3f}s')
assert elapsed < 2.0
assert window.selected_records() == ()
"""Local catalog scope and authoritative status refresh regression tests."""
import json
import time
from threading import Event
from pathlib import Path
from types import SimpleNamespace
import pytest
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QMessageBox
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
from cbb_refresh_verify.ui.main_window import MainWindow
from cbb_refresh_verify.verify_runner import VerifyResult
MIXED = (
CbbRecord('Alpha', 'Power', status='READY'),
CbbRecord('Beta', 'Power', status='PENDING'),
CbbRecord('Gamma', 'Analog', status='UNKNOWN'),
CbbRecord('Delta', 'Analog', status='ERROR'),
CbbRecord('Missing', 'Power', status='READY'),
)
def load_window(qtbot, tmp_path, monkeypatch, records=MIXED, **kwargs):
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
import cbb_refresh_verify.ui.main_window as ui
monkeypatch.setattr(ui, 'preflight_skip_reason',
lambda _, name: 'missing project' if name == 'Missing' else None)
lease = SimpleNamespace(mentor_path=Path('test.lmc'), close=lambda: None,
retain_for_recovery=lambda: None)
window = MainWindow(reader=lambda _: LibraryReadResult(records), data_root=tmp_path,
path_lease_factory=lambda _: lease, **kwargs)
qtbot.addWidget(window)
window.lmc_edit.setText('test.lmc')
window.load_library()
qtbot.waitUntil(lambda: not window.busy)
return window
def names(records):
return [record.name for record in records]
def test_search_and_filters_use_cached_catalog_and_count_visible_states(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.reader = lambda _: pytest.fail('filter reread Mentor')
assert window.visible_counts() == {'visible': 5, 'ready': 2, 'error': 3}
expected = {
'ready': ['Alpha', 'Missing'], 'non_ready': ['Beta', 'Gamma', 'Delta'],
'pending': ['Beta'], 'errors': ['Gamma', 'Delta'], 'missing': ['Missing'],
}
for mode, wanted in expected.items():
window.status_filter.setCurrentIndex(window.status_filter.findData(mode))
assert names(window.visible_records()) == wanted
window.status_filter.setCurrentIndex(window.status_filter.findData('all'))
window.search_edit.setText(' ANALOG ')
assert names(window.visible_records()) == ['Gamma', 'Delta']
assert window.visible_counts() == {'visible': 2, 'ready': 0, 'error': 2}
window.search_edit.setText('aLpH')
assert names(window.visible_records()) == ['Alpha']
assert '1' in window.counts_label.text()
def test_management_selection_includes_ready_and_missing_but_hiding_clears_checks(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.select_visible_button.click()
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Gamma', 'Delta', 'Missing']
assert window.selected_records()[0].logical_type == 'LP'
window.search_edit.setText('power')
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing']
window.search_edit.clear()
assert names(window.selected_records()) == ['Alpha', 'Beta', 'Missing']
window.clear_selection_button.click()
assert window.selected_records() == ()
def test_verify_all_and_selected_only_target_visible_non_ready_with_projects(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=lambda names, _: {name: 'READY' for name in names})
window.search_edit.setText('power')
window.select_visible_button.click()
window.verify_selected()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta']
window.search_edit.setText('analog')
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert calls == ['Beta', 'Gamma', 'Delta']
def test_maintenance_confirmation_progress_and_report_use_visible_checked_subset(qtbot, tmp_path, monkeypatch):
calls, confirmations, refreshes = [], [], []
def refresh(targets, _):
refreshes.append(tuple(targets))
return {name: 'PENDING' for name in targets}
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('CHECKED')),
status_refresher=refresh)
window.search_edit.setText('analog')
window.cbb_table.item(3, 0).setCheckState(Qt.Checked)
monkeypatch.setattr(QMessageBox, 'question', lambda *args, **_: (
confirmations.append(args[2]) or QMessageBox.Yes))
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window._icdb_active())
assert calls == ['Delta']
assert refreshes == []
assert '1' in confirmations[0] and '非 READY' in confirmations[0]
assert window.icdb_progress.maximum() == 1 and window.icdb_progress.value() == 1
report = json.loads(next((tmp_path/'reports').glob('icdb-*.json')).read_text(encoding='utf-8'))
assert report['includes_ready'] is False
assert report['target_names'] == ['Delta']
assert report['preflight_skipped'] == []
assert report['counts']['total'] == 1
assert window.search_edit.text() == 'analog'
assert window.cbb_table.item(3, 3).text() == '未发现 ICDB 错误'
assert names(window.visible_records()) == ['Gamma', 'Delta']
assert window.visible_records()[1].status == 'ERROR'
def test_maintenance_defaults_to_visible_eligible_and_ready_only_checks_do_not_fallback(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda req: (calls.append(req.block_name) or VerifyResult('PASS')),
status_refresher=lambda targets, _: {name: 'PENDING' for name in targets})
monkeypatch.setattr(QMessageBox, 'question', lambda *_, **__: QMessageBox.Yes)
window.cbb_table.item(0, 0).setCheckState(Qt.Checked)
window.repair_icdb_all()
assert not window.busy and calls == []
window.clear_selection_button.click()
window.search_edit.setText('power')
window.repair_icdb_all()
qtbot.waitUntil(lambda: not window._icdb_active())
assert calls == ['Beta']
def test_recovery_defaults_to_visible_checked_non_ready_and_skips_ready(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
window.search_edit.setText('power')
window.restore_backup()
assert [window.backup_dialog.block_combo.itemText(i)
for i in range(window.backup_dialog.block_combo.count())] == ['Beta']
window.backup_dialog.reject()
window.search_edit.clear()
window.cbb_table.item(3, 0).setCheckState(Qt.Checked)
window.restore_backup()
assert window.backup_dialog.block_combo.currentText() == 'Delta'
assert window.backup_dialog.block_combo.count() == 1
window.backup_dialog.reject()
def test_action_pass_does_not_promote_pending_without_authoritative_ready(qtbot, tmp_path, monkeypatch):
entered = []
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda targets, _: (entered.extend(targets) or {'Beta': 'READY'}))
window.status_filter.setCurrentIndex(window.status_filter.findData('pending'))
window.cbb_table.item(1, 0).setCheckState(Qt.Checked)
window._progress({'block': 'Beta', 'stage': 'PASS'})
assert names(window.visible_records()) == ['Beta']
assert window.visible_records()[0].status == 'PENDING'
window.refresh_affected_records(['Beta'])
qtbot.waitUntil(lambda: not window.busy)
assert entered == ['Beta']
assert window.visible_records() == ()
assert window.selected_records() == ()
assert window.status_filter.currentData() == 'pending'
window.status_filter.setCurrentIndex(window.status_filter.findData('ready'))
assert names(window.visible_records()) == ['Alpha', 'Beta', 'Missing']
assert window.cbb_table.item(1, 3).text() == 'PASS'
def test_refresh_failure_demotes_affected_without_losing_other_statuses(qtbot, tmp_path, monkeypatch):
def fail(*_):
raise OSError('bridge unavailable')
window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=fail)
window.refresh_affected_records(['Alpha'])
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'UNKNOWN'
assert window.visible_records()[1].status == 'PENDING'
assert 'Alpha' not in window.ready_names
assert 'bridge unavailable' in window.output.toPlainText()
def test_unsafe_result_defers_native_refresh_and_retains_action_evidence(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: VerifyResult('FAIL', stderr='owner release unconfirmed',
safe_to_continue=False, evidence_path=tmp_path/'evidence.json'),
status_refresher=lambda *_: pytest.fail('unsafe owner was reopened'))
window.search_edit.setText('beta')
window.verify_all()
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'UNKNOWN'
assert window.cbb_table.item(1, 3).text() == 'FAIL: owner release unconfirmed'
assert str(tmp_path/'evidence.json') in window.output.toPlainText()
assert '暂缓原生状态复核' in window.cbb_table.item(1, 2).toolTip()
def test_refresh_callback_uses_only_known_affected_records_and_preserves_logical_type(qtbot, tmp_path, monkeypatch):
observed, callbacks = [], []
def refresh(targets, path):
observed.append((targets, path))
return {'X': 'READY'}
window = load_window(qtbot, tmp_path, monkeypatch,
records=(CbbRecord('X', 'Display label', logical_type='RULP', status='PENDING'),),
status_refresher=refresh)
assert window.refresh_affected_records(['unknown']) is False
assert window.refresh_affected_records(['X', 'X', 'unknown'], callbacks.append) is True
assert window.refresh_affected_records(['X']) is False
qtbot.waitUntil(lambda: not window.busy)
assert observed == [(('X',), Path('test.lmc'))]
assert callbacks == [(CbbRecord('X', 'Display label', logical_type='RULP', status='READY'),)]
def test_backup_completion_refreshes_status_without_dialog_close_unlocking_pending_refresh(qtbot, tmp_path, monkeypatch):
entered, release = Event(), Event()
def refresh(targets, _):
entered.set()
release.wait(3)
return {name: 'PENDING' for name in targets}
window = load_window(qtbot, tmp_path, monkeypatch, status_refresher=refresh)
window.search_edit.setText('beta')
window.restore_backup()
window.backup_dialog.started.emit('Beta')
window.backup_dialog.completed.emit('Beta', VerifyResult('PASS'))
try:
qtbot.waitUntil(entered.is_set)
window.backup_dialog.reject()
assert window.busy
finally:
release.set()
qtbot.waitUntil(lambda: not window.busy)
assert window.visible_records()[0].status == 'PENDING'
assert window.cbb_table.item(1, 3).text() == 'PASS'
def test_missing_project_keeps_explicit_authoritative_status(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
assert 'Ready' in window.cbb_table.item(4, 2).text()
assert '缺少工程' in window.cbb_table.item(4, 2).text()
def test_open_backup_dialog_cannot_restore_target_that_became_ready(qtbot, tmp_path, monkeypatch):
calls = []
window = load_window(qtbot, tmp_path, monkeypatch,
status_refresher=lambda targets, _: {name: 'READY' for name in targets})
window.search_edit.setText('beta')
window.restore_backup()
dialog = window.backup_dialog
source = tmp_path/'Beta.prj'
source.touch()
dialog.source_edit.setText(str(source))
dialog.recoverer = lambda *_, **__: (calls.append('restore') or VerifyResult('PASS'))
window.refresh_affected_records(['Beta'])
qtbot.waitUntil(lambda: not window.busy)
dialog.restore_button.click()
qtbot.waitUntil(lambda: not dialog.busy)
assert calls == []
assert '非 READY' in dialog.status_label.text()
dialog.reject()
@pytest.mark.parametrize('status', ['UNKNOWN', 'UNAVAILABLE', 'ERROR', 'TIMEOUT', 'MALFORMED', 'CRASHED',
'UNRECOGNIZED'])
def test_unconfirmed_statuses_are_non_ready_and_error_filter_candidates(qtbot, tmp_path, monkeypatch, status):
window = load_window(qtbot, tmp_path, monkeypatch, records=(CbbRecord('X', 'LP', status=status),))
window.status_filter.setCurrentIndex(window.status_filter.findData('non_ready'))
assert names(window.visible_records()) == ['X']
window.status_filter.setCurrentIndex(window.status_filter.findData('errors'))
assert names(window.visible_records()) == ['X']
@pytest.mark.parametrize('size', [381, 3000])
def test_large_catalog_local_filter_and_selection_remain_responsive(qtbot, tmp_path, monkeypatch, size):
records = tuple(CbbRecord(f'CBB{i:04}', 'Power' if i % 2 else 'Analog',
status='READY' if i % 3 == 0 else 'PENDING') for i in range(size))
start = time.perf_counter()
window = load_window(qtbot, tmp_path, monkeypatch, records=records)
loaded = time.perf_counter() - start
window.reader = lambda _: pytest.fail('local actions reread Mentor')
start = time.perf_counter()
window.search_edit.setText('power')
window.select_visible_button.click()
window.status_filter.setCurrentIndex(window.status_filter.findData('pending'))
window.clear_selection_button.click()
elapsed = time.perf_counter() - start
print(f'catalog rows={size} load={loaded:.3f}s local_actions={elapsed:.3f}s')
assert elapsed < 2.0
assert window.selected_records() == ()
+166
View File
@@ -0,0 +1,166 @@
"""Lease calls may wait on WNet; the Qt event loop must never wait for them."""
from pathlib import Path
from threading import Event, get_ident
from types import SimpleNamespace
from PySide6.QtCore import QTimer
from test_ui_filters import load_window
def slow_lease(main_thread):
entered, release, closed = Event(), Event(), Event()
calls = []
def retain():
assert get_ident() != main_thread, 'retain blocked the Qt thread'
calls.append('retain')
entered.set()
assert release.wait(3)
def close():
assert get_ident() != main_thread, 'close blocked the Qt thread'
calls.append('close')
closed.set()
lease = SimpleNamespace(mentor_path=Path('test.lmc'), retain_for_recovery=retain, close=close)
return lease, entered, release, closed, calls
def test_unsafe_background_retains_before_a_later_cross_library_close(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify.verify_runner import VerifyResult
window = load_window(qtbot, tmp_path, monkeypatch,
verifier=lambda _: VerifyResult('FAIL', safe_to_continue=False))
lease, entered, release, closed, calls = slow_lease(get_ident())
window.library_lease = lease
try:
window._run(['Beta'], maintenance=True)
qtbot.waitUntil(entered.is_set)
ticks = []
QTimer.singleShot(0, lambda: ticks.append(True))
window.lmc_edit.setText('other.lmc')
qtbot.waitUntil(lambda: bool(ticks))
assert window.search_edit.isEnabled() and not closed.is_set()
release.set()
qtbot.waitUntil(closed.is_set)
assert calls == ['retain', 'close']
finally:
release.set()
qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0)
def test_management_dialog_finish_waits_for_mapping_cleanup_without_freezing_filters(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify.management import ManagementResult
window = load_window(qtbot, tmp_path, monkeypatch)
library = Path(window.mentor_lmc_path)
window._management_operation_library = library
window._management_unsafe = True
window._management_unsafe_library = library
window.busy = True
entered, release = Event(), Event()
main_thread = get_ident()
def restore(*args, **kwargs):
assert get_ident() != main_thread, 'mapping release blocked the Qt thread'
entered.set()
assert release.wait(3)
monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases', restore)
proof = {'status': 'RELEASE_CONFIRMED', 'library': str(library)}
result = ManagementResult('remove', library, 'Beta', 'Logical-Physical', None, False,
'complete', 'BLOCKED', True, release_proof=proof)
try:
window._management_completed([result])
window._management_closed()
qtbot.waitUntil(entered.is_set)
assert window.busy and window.search_edit.isEnabled() and window.status_filter.isEnabled()
assert window._management_library_unsafe(library)
release.set()
qtbot.waitUntil(lambda: not window.busy)
assert not window._management_library_unsafe(library)
finally:
release.set()
qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0)
def test_release_dialog_finish_cannot_clear_busy_before_mapping_cleanup(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
library = Path(window.mentor_lmc_path)
entered, release = Event(), Event()
main_thread = get_ident()
def restore(*args, **kwargs):
assert get_ident() != main_thread, 'mapping release blocked the Qt thread'
entered.set()
assert release.wait(3)
monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases', restore)
window.busy = True
try:
window._complete_library_release(library, {'status': 'RELEASE_CONFIRMED',
'library': str(library), 'evidence_path': 'proof.json'})
window._release_dialog_finished(0)
qtbot.waitUntil(entered.is_set)
assert window.busy and window.search_edit.isEnabled()
release.set()
qtbot.waitUntil(lambda: not window.busy)
finally:
release.set()
qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0)
def test_window_close_waits_asynchronously_for_retain_and_all_pool_work(qtbot, tmp_path, monkeypatch):
window = load_window(qtbot, tmp_path, monkeypatch)
lease, entered, release, closed, calls = slow_lease(get_ident())
window.library_lease = SimpleNamespace(mentor_path=Path('other.lmc'), close=lease.close)
window._native_backup_session = SimpleNamespace(path_lease=lease, library=Path('test.lmc'))
window.show()
try:
window.close()
qtbot.waitUntil(entered.is_set)
ticks = []
QTimer.singleShot(0, lambda: ticks.append(True))
qtbot.waitUntil(lambda: bool(ticks))
assert window.isVisible() and not closed.is_set()
release.set()
qtbot.waitUntil(lambda: not window.isVisible())
assert calls == ['retain', 'close']
assert window.pool.activeThreadCount() == 0
finally:
release.set()
qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0)
window._native_backup_session = None
def test_duplicate_dialog_finish_schedules_only_one_catalog_reload(qtbot, tmp_path, monkeypatch):
from cbb_refresh_verify.management import ManagementResult
window = load_window(qtbot, tmp_path, monkeypatch)
library = Path(window.mentor_lmc_path)
window._management_operation_library = library
window.busy = True
entered, release = Event(), Event()
reloads = []
monkeypatch.setattr(window, 'load_library', lambda: reloads.append(True))
def restore(*args, **kwargs):
entered.set()
assert release.wait(3)
monkeypatch.setattr('cbb_refresh_verify.network_path.release_retained_path_leases', restore)
result = ManagementResult('remove', library, 'Beta', 'Logical-Physical', None, False,
'complete', 'REMOVED', True, release_proof={'status': 'RELEASE_CONFIRMED', 'library': str(library)})
try:
window._management_completed([result])
window._management_closed(); window._management_closed()
qtbot.waitUntil(entered.is_set)
release.set()
qtbot.waitUntil(lambda: not window.busy)
qtbot.waitUntil(lambda: bool(reloads))
assert reloads == [True]
finally:
release.set()
qtbot.waitUntil(lambda: window.pool.activeThreadCount() == 0)
def test_window_close_tolerates_fake_and_deleted_dialog_pools(qtbot, tmp_path, monkeypatch):
from PySide6.QtCore import QThreadPool
from shiboken6 import delete
window = load_window(qtbot, tmp_path, monkeypatch)
deleted_pool = QThreadPool()
delete(deleted_pool)
window.release_dialog = SimpleNamespace()
window.management_dialog = SimpleNamespace(pool=deleted_pool)
window.show()
window.close()
qtbot.waitUntil(lambda: not window.isVisible())