Compare commits
21
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0fca6abb4e | ||
|
|
342a7266c7 | ||
|
|
e8a1714d95 | ||
|
|
e73f9c6edc | ||
|
|
b7baa24b5e | ||
|
|
3c1998f2ef | ||
|
|
807aa26baf | ||
|
|
8c2c60fab1 | ||
|
|
5b22787418 | ||
|
|
679f33b8cc | ||
|
|
746e5b5145 | ||
|
|
8eefecd9f0 | ||
|
|
d8156c57f0 | ||
|
|
959399b974 | ||
|
|
46dae81190 | ||
|
|
282be7be7f | ||
|
|
da67de026f | ||
|
|
9e230e1a46 | ||
|
|
a56fd17b60 | ||
|
|
766d67cb57 | ||
|
|
311d0dd9a4 |
No files matched your search
@@ -0,0 +1,47 @@
|
||||
# Issue 5:私有桌面泄露
|
||||
|
||||
## 现象
|
||||
|
||||
代码与回归测试确认:原生 worker 创建私有 Windows Desktop 后,如果 Job Object 创建或 worker 进程启动失败,构造函数直接抛出异常,已创建的 Desktop 和 Job 句柄没有释放。重复失败存在累积私有桌面资源的风险;本次未对现场所有桌面异常作归因。
|
||||
|
||||
状态读取路径还有一个相同的边界:`read_library_flag` 在进入资源清理保护前加载 UI 依赖;依赖加载异常时,刚创建的私有桌面无法执行 `CloseDesktop`。
|
||||
|
||||
## 根因
|
||||
|
||||
1. `DesktopWorker.__init__` 的资源申请跨越多个可能失败的步骤,但没有事务式回滚。
|
||||
2. 进程句柄、Job 句柄和 Desktop 句柄的释放顺序没有覆盖构造阶段异常。
|
||||
3. `read_library_flag` 的依赖导入位于 `CreateDesktop` 之后、`try/finally` 之前。
|
||||
4. `read_library_flag` 和 `read_library_tree_image_map` 都在清理保护外先创建 Desktop,再执行 `Path(library)` 等准备逻辑;准备阶段抛异常时也会泄漏。
|
||||
|
||||
## 解决方案
|
||||
|
||||
- 在 `DesktopWorker` 构造开始时初始化资源字段,并把 Desktop、Job、worker 创建放入同一异常边界。
|
||||
- 构造失败时按 worker、Job、Desktop 顺序回滚;已启动的 worker 先终止,避免留下无父级的原生进程。
|
||||
- 在状态读取创建 Desktop 前完成 UI 依赖加载,确保依赖异常不会产生未受保护的 Desktop。
|
||||
- 两处状态读取均在准备逻辑完成后、`try/finally` 内创建 Desktop,清理时检查资源是否存在。
|
||||
- 测试中分离 Desktop、Job、Process、Thread 的接口,断言失败后关闭顺序;覆盖四个构造失败点与进程创建后的终止分支。
|
||||
|
||||
## 影响范围
|
||||
|
||||
- 覆盖原生 worker 构造失败,以及状态读取准备和启动阶段的资源清理路径。
|
||||
- 正常任务的私有桌面、Job Object、进程树和数据库释放策略不变。
|
||||
- 不会切换用户输入桌面,也不会改变中心库写入策略。
|
||||
|
||||
## 验证
|
||||
|
||||
- 修复分支完整 `python -m pytest -q --tb=short`:480 passed。
|
||||
- 同环境、同工作树切换到 `origin/main`(`39985f7`)完整运行相同命令:471 passed。两次运行依次执行,均仅有一条 pywinauto STA 警告。
|
||||
- `tests/test_desktop_worker.py`:9 passed。两处状态读取准备阶段泄漏的新增测试在修复前 2 failed,修复后通过。
|
||||
- 变异验证:把回滚中的 `self.job.Close()` 故意改为 `self.job.CloseDesktop()`,测试产生 2 failed;恢复正确代码后通过。
|
||||
- `python -m compileall -q src tests`:通过。
|
||||
- `git -c core.whitespace=cr-at-eol diff origin/main HEAD --check`:通过。命令显式识别 Windows CRLF;仓库 `.gitattributes` 为 `* -text`,保留源文件换行,不依赖本机 `core.whitespace` 配置。
|
||||
|
||||
此前 #7 将一次运行中的 `test_backup_recovery.py::test_invalid_source_does_not_touch_target` 失败称为既有基线问题,缺少同环境对照,撤回该归因。本次该用例在 main 与修复分支均通过。审查者在旧 WIP 基线测得的 19 个失败不属于本次从 main 独立提交后的测试范围。
|
||||
|
||||
## 合并范围修正
|
||||
|
||||
原 PR #7 以 ICDB WIP 分支为父提交,却以 main 为合并目标,实际携带 59 个提交、118 个文件。现从 main 单独 cherry-pick 桌面修复并补充上述审查修正;相对 main 只有本工单、两个源码文件和一个测试文件,共 4 个文件。独立代码复审未发现待处理问题。
|
||||
|
||||
## 分支
|
||||
|
||||
`codex/desktop-leak-main`
|
||||
@@ -0,0 +1,7 @@
|
||||
# PR #10 comment 309
|
||||
|
||||
Release proof now records the requested CBB, effective BLOCK/LIBRARY proof scope, identities/statuses of archived checkpoints and retained owners, and `reconciled_foreign_blockers`. For example, a BlockA operation which safely reconciles BlockB's historical checkpoint explicitly lists BlockB and the full-library proof. `data_recovery_confirmed` remains false. No extra gate, automatic backup or native action was added.
|
||||
|
||||
Three regressions first failed on missing evidence fields. After the fix, 94 backend/lease/release regressions passed; compileall and git diff --check passed. The tests use real temporary file/lease handles with the process-query boundary replaced; no production library was opened. Full regression is run again at the integrated chain tip. Real Mentor/shared-network acceptance and a rebuilt package remain outstanding.
|
||||
|
||||
Review: https://git.simgreek.com/Skyworth_Digital_Hardware/cbb-refresh-verify-795/pulls/10#issuecomment-309
|
||||
@@ -0,0 +1,38 @@
|
||||
# PR11 review 345 follow-up
|
||||
|
||||
Repeated CBB verification and ICDB check runs replace the previous result dialog
|
||||
for the same operation. Closing a result retires its Qt widgets and action
|
||||
references, while the check task keeps its requests, results, invalidated targets,
|
||||
and recovery context. A result for the other operation stays available.
|
||||
|
||||
During application shutdown, the title and status show that background work and
|
||||
path release are pending. After approximately 120 seconds a second notice says
|
||||
the application is still waiting. This is a notice interval, not a deadline;
|
||||
shutdown still waits for existing workers and never forces native tools to stop.
|
||||
|
||||
The batch CLI accepts an optional positive `--limit`:
|
||||
|
||||
```powershell
|
||||
python -m cbb_refresh_verify --batch-lmc 'Z:\library\library.lmc' --limit 2
|
||||
```
|
||||
|
||||
The limit selects the first requested names in their existing order. With no
|
||||
`--block`, the existing non-READY selection supplies those names; repeated
|
||||
`--block` options retain their explicit order. Omitting `--limit` retains the
|
||||
existing all-requested behavior. `batch_selection` logs selected, total, and
|
||||
remaining counts. On batch exceptions or unconfirmed native release,
|
||||
`batch_drive_retained` identifies the drive, remote share, display library and
|
||||
Mentor path, and directs the operator to the confirmed-release entry. It never
|
||||
disconnects a mapping while a native writer may still be active.
|
||||
|
||||
Management change counts explicitly cover the compared engineering files only.
|
||||
|
||||
Removed from the standalone PR11 head: all five `if False` blocks for the later
|
||||
release flow, and `_reload_after_maintenance` assignments and its unused reload
|
||||
branch. The hidden release control and existing method signatures remain for
|
||||
the later PR12 implementation. When integrating forward, preserve PR12's live
|
||||
release/resume behavior and retire its dialog-owned action references as well.
|
||||
Preserve PR13's asynchronous close and lease guards when adding the wait notice.
|
||||
|
||||
Validation uses the isolated offscreen review wrapper with fake native boundaries;
|
||||
it does not open Mentor, kill native processes, invoke Repair, or back up a library.
|
||||
@@ -0,0 +1,13 @@
|
||||
# 独立审查 1:只读进程探测与原生任务准入队列
|
||||
|
||||
基于 main `a56fd17`,不叠加 PR #3 的历史提交。
|
||||
|
||||
**行为变化:** 增加有界、版本化、只读 owner-probe helper 和可取消的 FIFO 原生任务队列。应用仅新增隐藏的只读 helper 分派;现有 UI、验证和原生写操作保持原来的入口。
|
||||
|
||||
**失败安全:** 查询超时或协议/身份异常返回未确认;暂停或取消的队列票据不会运行其动作,也不阻止后面的可执行任务。不启动或终止 Mentor 进程。
|
||||
|
||||
**回滚方式:** 尚未合入下游时撤销本提交即可;下游接入后须按依赖从上层向下回退。
|
||||
|
||||
完整回归:491 passed,1 条既有 pywinauto STA 提示。编译和 diff 检查通过。
|
||||
|
||||
与原评论建议的差别:`management_transaction` 直接导入管理模型和库锁;`scoped_manifest` 的协调记录与独占快照依赖库锁协议。这两者在后端审查中一起交付,避免构造导入不完整的“纯工具”基线。
|
||||
@@ -0,0 +1,20 @@
|
||||
# Native backend review unit
|
||||
|
||||
Base: codex/review-infrastructure. This unit integrates ICDB open diagnostics, scoped content manifests, native CBB transactions and the library-release protocol; it does not expose the new UI or residual termination action. Verification and ICDB checks remain separate, READY items are excluded from maintenance, and a released single-item failure can continue independent targets.
|
||||
|
||||
Failure safety: no automatic Repair/version choice, no server termination, no direct LMC edits or native lock-file deletion. Native writes are serialized, uncertain ownership retains evidence and mappings, and unfinished writes are shown without replay.
|
||||
|
||||
Dependency rationale: library release imports native engineering identity and retained management workers; engineering identity imports management source models and scoped manifests. Separating these into nominally independent utilities would leave runtime imports unresolved. This PR therefore keeps the backend dependency closure together; UI and user-confirmed termination remain separate later PRs.
|
||||
|
||||
Rollback: before dependents are merged, revert this commit. After merging dependents, revert in reverse order or use the previous candidate package after closing the current app. Code rollback does not undo a native transaction; use a consistent manual backup or explicitly selected native history on an isolated copy.
|
||||
|
||||
Validation: full Python suite 1,376 passed (one existing pywinauto COM-threading warning); compileall and git diff --check passed. Native field recovery, Windows 10 and cross-machine shared-library acceptance remain pending.
|
||||
|
||||
Review comment 344 follow-up (2026-11-09):
|
||||
|
||||
- Production status scans continue to query fresh bridge results. The low-level session now also defaults to fresh scans; explicit checkpoint reuse is documented as a controlled read-only option with no production resume promise.
|
||||
- `library-changes.json` declares `rule_version`, the selected `scope`, excluded directory categories and suffixes, positional runtime rules, runtime filenames/prefixes, and recognized coordination checkpoints. Names, suffixes, prefixes and native database locations come from the same constants used by semantic classification. These are differences in participating engineering files; excluded content is not proven unchanged, and unrelated CBB content is not checked.
|
||||
- Removed the tests-only `pcb_bridge.execution_outcome` helper. Production release safety continues to come from observed release state and remaining clients.
|
||||
- Unlocked primary snapshots hash the copy stream, then independently hash the final source and destination. Source reads decrease from three to two. Rejected the suggestion to remove/threshold the final source hash: size and mtime cannot detect same-size, same-timestamp source edits. Locked LMC/icdb streams and owned lock snapshots retain their existing behavior; no source-rehash skip flag is added.
|
||||
|
||||
Validation for this follow-up: 287 targeted cases passed through the isolated mutex/PYTHONPATH wrapper (`test_batch_status`, `test_batch_status_faults`, `test_review_backend_followup`, `test_scoped_manifest`, `test_pcb_bridge`, `test_management_retry_blockers`, `test_backup_recovery`, `test_library_lock`, `test_management`). AST parsing, `git diff --check`, and original newline-style checks passed. No real Mentor, COM automation, shared library or process control was exercised; no full-suite run was requested.
|
||||
@@ -0,0 +1,9 @@
|
||||
# Application integration review unit
|
||||
|
||||
Base: codex/review-native-backend. Behavior: verification, ICDB batch checking and CBB management have separate tabs; background checks yield between items, keep browsing responsive and let other native tasks queue. READY targets are excluded at execution, status refresh is scoped, and manual version/copy decisions remain explicit.
|
||||
|
||||
Failure safety: a released item failure can continue independent targets; uncertain native ownership retains evidence and mappings. There is no residual-session release backend in this review stage: its button and result actions are hidden, and its callback is inert. The next PR restores the complete release entry together with its tested backend, rather than exposing a button with a missing import. No automatic Repair or native version selection is added.
|
||||
|
||||
Rollback: revert this UI commit before its dependent release PR; after dependents, revert in reverse order or close the current program and use the prior candidate. This does not roll back library writes.
|
||||
|
||||
Validation: 1,545 passed (one existing pywinauto COM-threading warning), compileall and diff checks passed. Release-specific tests are introduced in the dependent PR, never discarded from the final tree. Windows 10, real shared-library concurrency and field ICDB recovery remain pending.
|
||||
@@ -0,0 +1,9 @@
|
||||
# 评论 275:占用复核与变更报告
|
||||
|
||||
校验和管理共用历史释放记录复核:旧 JSON/内存租约触发实际进程、地址、独占文件和导出副本证明,不直接视为真实占用,不结束进程或执行 Repair。确认释放后归档旧记录并继续;失败且本项尚未启动时保留原记录,结果不会把本项标成未释放。校验结果兼容新增 release_proof,管理首次原生调用前保存释放经过;进程释放不等于历史数据已恢复。
|
||||
|
||||
写后仅对原先的目标 Work/Cert、中心库配置和共享依赖范围生成 SHA-256 差分,报告新增、删除、修改路径,并保存写后清单。范围沿用原有 ManifestScope.library,不增加整库备份或其他 CBB 内容哈希,不声称已证明所有其他 CBB 未被波及。已释放的部分失败也保留差分;仍有原生所有者时不为报告读取数据库。报告失败单独标为 UNCONFIRMED 并返回原因,不伪造未完成事务、不制造永久占用。
|
||||
|
||||
PASS 不再重复计入 unconfirmed。未改变实际原生写操作、人工备份政策或服务器保护,也没有恢复自动修复。
|
||||
|
||||
回归覆盖历史记录无占用/真实地址占用、首次原生调用前留证、目标及共享依赖差分、失败后差分、报告保存失败后可继续。完整回归和限制以最终验证记录及 PR 描述为准;真实 Mentor、Win10、跨机器共享库尚未因此取得新验收结论。
|
||||
@@ -0,0 +1,7 @@
|
||||
# 评论 275/283:结果口径接入
|
||||
|
||||
管理进度显示历史占用复核;结果分别呈现已确认的原生动作、变更清单完整性和历史会话释放事实。单项 RECOVERY_REQUIRED 但 safe_to_continue=True 时,不再声称整个队列停止;记录失败不伪装成原生上传/移除失败。
|
||||
|
||||
独立操作回归更新为实际历史记录归档流程:先证明当前占用已释放、保留归档字节,再进行普通验证;不自动 Repair,失败后不遗留新的占用锁。
|
||||
|
||||
本阶段相关 UI/独立操作/后端回归 29 passed。后端完整回归与最终集成回归另见验证记录。没有执行真实 Mentor 写操作;回退按依赖逆序,不代表恢复原生事务。
|
||||
@@ -1,5 +1,7 @@
|
||||
# CBB Batch Status Bridge Implementation Plan
|
||||
|
||||
Current behavior (2026-11-09): production status scans always query the bridge fresh and overwrite checkpoint evidence. `BatchStatusSession.scan` defaults to `resume=False`; `resume=True` remains an explicit low-level option for controlled read-only scans, not an exposed production resume feature. Its lightweight fingerprint cannot detect every native payload/database write, so saved READY/PENDING rows must not authorize production actions.
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Read 180 to 3000 CBB Verify states through one resilient x86 Mentor bridge session with checkpoint recovery and authoritative `READY/PENDING` results.
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# Latest reviews for PR #10/#11/#12
|
||||
|
||||
Scope: user selected only the 2026-11-09 comments #309/#310/#313. Earlier deep-review backlog stays out of scope. Preserve ICDB automatic Repair OFF, serial writes, current icon and independent-library admission. No merge to main, issue closure or release.
|
||||
|
||||
## PR #10
|
||||
|
||||
- [ ] Reproduce BlockA releasing historical BlockB checkpoints and retained owners without naming BlockB in proof.
|
||||
- [ ] Record requested block, actual proof scope, checkpoint identities and foreign names in existing release proof. Preserve original checkpoint bytes and data_recovery_confirmed=False; no new blocking gate.
|
||||
- [ ] Run backend/lease regressions, compile/diff checks, commit on codex/review-native-backend.
|
||||
|
||||
## PR #11/#12
|
||||
|
||||
- [ ] Forward-merge backend into UI, then UI into residual branch; no history rewriting.
|
||||
- [ ] Test one unreadable process/thread not affecting another, without fabricated modal windows; cover preview and action-time rechecks.
|
||||
- [ ] Enumerate only target-process threads/windows, store incomplete-read metadata per PID. Keep unknown/active writers protected rather than adding a blind force override.
|
||||
- [ ] Persist window identities and at most a 4 KiB output excerpt, preserve full preview in memory, stop duplicating activity output.
|
||||
- [ ] Carry exact preview block scope into native Cancel and recheck identity/scope before clicking.
|
||||
- [ ] Run residual/UI regressions, compile/diff checks, commit on residual branch.
|
||||
|
||||
## Chain tip
|
||||
|
||||
- [ ] Merge residual fixes into PR #13, preserving ca39fb3 and icon.
|
||||
- [ ] Address #11's line-ending note once at the tip: application.py to LF; pin it and main_window.py in EditorConfig. Preserve all other file endings and the global * -text rule.
|
||||
- [ ] Independent review, integrated full suite, compile/diff checks, validation records and fast-forward pushes to existing PR branches.
|
||||
- [ ] Update PR descriptions with precise results and no new package/native acceptance. Leave #3 as historical umbrella, without merging or closing it.
|
||||
@@ -1,5 +1,7 @@
|
||||
# CBB 批量状态桥设计
|
||||
|
||||
Current behavior (2026-11-09): production status scans always query the bridge fresh and overwrite checkpoint evidence. `BatchStatusSession.scan` defaults to `resume=False`; `resume=True` remains an explicit low-level option for controlled read-only scans, not an exposed production resume feature. Its lightweight fingerprint cannot detect every native payload/database write, so saved READY/PENDING rows must not authorize production actions.
|
||||
|
||||
## 目标
|
||||
|
||||
支持对 180 到 3000 个 CBB 进行 Mentor authoritative Verify 状态读取,要求单条失败隔离、过程可恢复、结果可审计,并避免现有“一条 CBB 启动一个桥进程”的启动开销。
|
||||
|
||||
@@ -1,8 +1,23 @@
|
||||
from pathlib import Path
|
||||
import os
|
||||
import struct
|
||||
project_root = Path(SPECPATH).parent
|
||||
assets_root = project_root / 'src' / 'cbb_refresh_verify' / 'assets'
|
||||
bridge_path = project_root / 'tools' / 'status_bridge' / 'bin' / 'CbbStatusBridge32.exe'
|
||||
verify_probe_path = project_root / 'tools' / 'status_bridge' / 'bin' / 'VerifyProbe32.exe'
|
||||
# App-local x86 runtime belongs beside the x86 bridge, separate from Qt/Python.
|
||||
bridge_runtime = []
|
||||
if bridge_path.is_file():
|
||||
runtime_root = Path(os.environ.get('WINDIR', r'C:\Windows')) / 'SysWOW64'
|
||||
for name in ('mfc140.dll', 'msvcp140.dll', 'vcruntime140.dll', 'concrt140.dll'):
|
||||
runtime = runtime_root / name
|
||||
if not runtime.is_file():
|
||||
raise RuntimeError(f'Install the Microsoft VC++ 2015-2022 x86 runtime before packaging: {runtime}')
|
||||
data = runtime.read_bytes()
|
||||
pe_offset = struct.unpack_from('<I', data, 0x3c)[0]
|
||||
if data[pe_offset:pe_offset+4] != b'PE\x00\x00' or struct.unpack_from('<H', data, pe_offset+4)[0] != 0x14c:
|
||||
raise RuntimeError(f'Expected an x86 bridge runtime: {runtime}')
|
||||
bridge_runtime.append((str(runtime), 'tools/status_bridge/bin'))
|
||||
a = Analysis(
|
||||
[str(project_root / 'src' / 'cbb_refresh_verify' / '__main__.py')],
|
||||
pathex=[str(project_root / 'src'), r'F:\Sync Tool Skyworth\Work Maps\dxdesigner-part-replacement\src'],
|
||||
@@ -12,13 +27,13 @@ a = Analysis(
|
||||
[(str(verify_probe_path), 'tools/status_bridge/bin')]
|
||||
if verify_probe_path.is_file() else []
|
||||
),
|
||||
datas=[(str(project_root / 'src' / 'cbb_refresh_verify' / 'save_verified_block.vbs'), 'cbb_refresh_verify'),
|
||||
datas=bridge_runtime + [(str(project_root / 'src' / 'cbb_refresh_verify' / 'save_verified_block.vbs'), 'cbb_refresh_verify'),
|
||||
(str(project_root / 'src' / 'cbb_refresh_verify' / 'close_failed_block.vbs'), 'cbb_refresh_verify'),
|
||||
(str(assets_root / 'cbb_refresh_verify.ico'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'cbb_refresh_verify.png'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'cbb_refresh_verify.svg'), 'cbb_refresh_verify/assets'),
|
||||
(str(assets_root / 'dialog_rules.v1.json'), 'cbb_refresh_verify/assets')],
|
||||
hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'],
|
||||
hiddenimports=['win32com.client','win32com.client.dynamic','pythoncom','pywintypes','win32timezone','win32job','pywinauto','pywinauto.uia_defines','pywinauto.controls.uiawrapper','pywinauto.win32_element_info','cbb_refresh_verify.dialog_agent','cbb_refresh_verify.dialog_probe','cbb_refresh_verify.dialog_rules','cbb_refresh_verify.ui.main_window','cbb_refresh_verify.ui.management_dialog','cbb_refresh_verify.management','cbb_refresh_verify.native_management','cbb_refresh_verify.library_lock','cbb_refresh_verify.content_check','cbb_refresh_verify.failed_session','cbb_refresh_verify.icdb_ownership','cbb_refresh_verify.native_repair','cbb_refresh_verify.native_repair_ui','dx_productivity.trial_task_center.logging_setup','dx_productivity.trial_task_center.errors','dx_productivity.trial_task_center.retention'],
|
||||
excludes=['PySide6.QtDesigner','PySide6.QtMultimedia','PySide6.QtMultimediaWidgets','PySide6.QtOpenGL','PySide6.QtOpenGLWidgets','PySide6.QtPdf','PySide6.QtPdfWidgets','PySide6.QtQml','PySide6.QtQuick','PySide6.QtQuickWidgets','PySide6.QtWebChannel','PySide6.QtWebEngineCore','PySide6.QtWebEngineWidgets'],
|
||||
)
|
||||
excluded_qt_binaries = {'icudt78.dll','icuuc.dll','qpdf.dll','qt6pdf.dll','qt6qml.dll','qt6qmlmeta.dll','qt6qmlmodels.dll','qt6qmlworkerscript.dll','qt6quick.dll','qt6quickwidgets.dll'}
|
||||
|
||||
@@ -27,6 +27,8 @@ def configure_qt_application(app):
|
||||
}
|
||||
for role, color in colors.items():
|
||||
palette.setColor(role, QColor(color))
|
||||
for role in (QPalette.ColorRole.WindowText, QPalette.ColorRole.Text, QPalette.ColorRole.ButtonText):
|
||||
palette.setColor(QPalette.ColorGroup.Disabled, role, QColor('#858b94'))
|
||||
app.setPalette(palette)
|
||||
app.setStyleSheet(
|
||||
'QWidget { color: #202124; }'
|
||||
@@ -34,6 +36,7 @@ def configure_qt_application(app):
|
||||
'background: #ffffff; color: #202124; selection-color: #202124; }'
|
||||
'QHeaderView::section { background: #e9edf2; color: #202124; }'
|
||||
'QPushButton, QToolButton { color: #202124; }'
|
||||
'QPushButton:disabled, QToolButton:disabled { color: #858b94; background: #e3e6eb; }'
|
||||
)
|
||||
return app
|
||||
|
||||
@@ -43,19 +46,42 @@ def resolve_data_root(cli_value: Path | None) -> Path:
|
||||
local = os.environ.get("LOCALAPPDATA")
|
||||
return (Path(local) / "CbbRefreshVerify" if local else Path.home() / "AppData" / "Local" / "CbbRefreshVerify").resolve()
|
||||
|
||||
def _positive_int(value):
|
||||
try:
|
||||
number = int(value)
|
||||
except ValueError:
|
||||
raise argparse.ArgumentTypeError('must be a positive integer') from None
|
||||
if number <= 0:
|
||||
raise argparse.ArgumentTypeError('must be a positive integer')
|
||||
return number
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="CbbRefreshVerify")
|
||||
parser.add_argument('--verify-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--repair-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--owner-probe', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--native-backup-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--management-worker', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dialog-agent', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--collect-failure-logs', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--batch-lmc', type=Path, help=argparse.SUPPRESS)
|
||||
parser.add_argument('--block', action='append', default=[], help=argparse.SUPPRESS)
|
||||
parser.add_argument('--limit', type=_positive_int, help='Maximum requested CBB names for --batch-lmc (default: all)')
|
||||
parser.add_argument('--libspec', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument('--dms-login-config', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument('--mentor-bin', default='', help=argparse.SUPPRESS)
|
||||
parser.add_argument("--smoke-test", action="store_true", help=argparse.SUPPRESS); parser.add_argument("--data-root", type=Path, help=argparse.SUPPRESS)
|
||||
args = parser.parse_args(list(argv) if argv is not None else None)
|
||||
if args.owner_probe:
|
||||
from .owner_probe import owner_probe_main
|
||||
return owner_probe_main(args.owner_probe)
|
||||
if args.native_backup_worker:
|
||||
from .native_backup import native_backup_worker_main
|
||||
return native_backup_worker_main(args.native_backup_worker)
|
||||
if args.management_worker:
|
||||
from .native_management import management_worker_main
|
||||
return management_worker_main(args.management_worker)
|
||||
if args.repair_worker:
|
||||
from .native_repair_ui import repair_worker_main
|
||||
return repair_worker_main(args.repair_worker)
|
||||
@@ -95,6 +121,11 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
if library.error: raise RuntimeError(library.error)
|
||||
names = [record.name for record in library.records if record.status != 'READY']
|
||||
if not names: raise RuntimeError('中心库未读取到需要校验的 CBB')
|
||||
total = len(names)
|
||||
if args.limit is not None:
|
||||
names = names[:args.limit]
|
||||
logger.info('batch_selection selected=%s total=%s remaining=%s limit=%s',
|
||||
len(names), total, total - len(names), args.limit or 'all')
|
||||
requests = [VerifyRequest(mentor_path, name, context.root/'evidence',
|
||||
libspec=args.libspec, dms_login_config=args.dms_login_config,
|
||||
mentor_bin=args.mentor_bin, logging_context=context,
|
||||
@@ -102,13 +133,21 @@ def main(argv: Sequence[str] | None = None) -> int:
|
||||
mapped_remote_root=lease.remote_root,
|
||||
native_lm_verify=native_lm_verify_enabled())
|
||||
for name in names]
|
||||
def retain_mapping(reason):
|
||||
lease.retain_for_recovery()
|
||||
logger.warning(
|
||||
'batch_drive_retained drive=%s remote=%s display=%s mentor=%s reason=%s '
|
||||
'guidance=请打开此中心库的“释放残留会话…”入口,确认原生会话安全释放后再释放映射;'
|
||||
'原生写入仍可能运行时请保留映射。',
|
||||
lease.drive or '-', lease.remote_root or '-', lease.display_path,
|
||||
mentor_path, reason)
|
||||
try:
|
||||
results = run_batch(requests)
|
||||
except Exception:
|
||||
lease.retain_for_recovery()
|
||||
retain_mapping('batch_exception')
|
||||
raise
|
||||
if any(not result.safe_to_continue for result in results):
|
||||
lease.retain_for_recovery()
|
||||
retain_mapping('native_release_unconfirmed')
|
||||
report = write_batch_report(requests, results, context.root/'reports')
|
||||
logger.info('batch_report path=%s', report)
|
||||
logger.info('failure_report path=%s', failure_report_path(report))
|
||||
|
||||
@@ -3,12 +3,8 @@
|
||||
This restores project data for an existing library entry. It is not a native
|
||||
cross-library import and does not delete library records or shared symbols.
|
||||
"""
|
||||
from contextlib import ExitStack
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from dataclasses import asdict, replace
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
@@ -16,114 +12,72 @@ import stat
|
||||
from uuid import uuid4
|
||||
|
||||
from .verify_runner import VerifyResult, preflight
|
||||
from .scoped_manifest import (ManifestScope, build_manifest, copy_verified,
|
||||
hash_file as _hash, scan_tree as _scan)
|
||||
|
||||
|
||||
def _hash(path):
|
||||
digest = hashlib.sha256()
|
||||
with path.open('rb') as stream:
|
||||
for data in iter(lambda: stream.read(1024 * 1024), b''):
|
||||
digest.update(data)
|
||||
return digest.hexdigest()
|
||||
def _manifest(root, *, scope=None, exclude=None, progress=None):
|
||||
return build_manifest(root, scope=scope, exclude=exclude, progress=progress, hash_function=_hash)
|
||||
|
||||
|
||||
def _scan(root):
|
||||
if root.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError(f'不支持通过目录链接恢复:{root}')
|
||||
entries = {}
|
||||
folders = [root]
|
||||
while folders:
|
||||
with os.scandir(folders.pop()) as scanned:
|
||||
for entry in scanned:
|
||||
metadata = entry.stat(follow_symlinks=False)
|
||||
if metadata.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError(f'目录含文件或目录链接,停止恢复:{entry.path}')
|
||||
path = Path(entry.path)
|
||||
entries[path] = metadata
|
||||
if stat.S_ISDIR(metadata.st_mode):
|
||||
folders.append(path)
|
||||
return dict(sorted(entries.items()))
|
||||
def _copy_verified(source, destination, *, scope=None, progress=None):
|
||||
return copy_verified(source, destination, scope=scope, progress=progress, hash_function=_hash)
|
||||
|
||||
|
||||
def _paths(root):
|
||||
return list(_scan(root))
|
||||
|
||||
|
||||
def _manifest(root, *, exclude=None):
|
||||
entries = _scan(root)
|
||||
paths = [path for path in entries
|
||||
if exclude is None or not path.is_relative_to(exclude)]
|
||||
def entry(path):
|
||||
return str(path.relative_to(root)), _hash(path) if stat.S_ISREG(entries[path].st_mode) else None
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
return dict(pool.map(entry, paths))
|
||||
|
||||
|
||||
def _copy_verified(source, destination, *, progress=None):
|
||||
"""Copy every file, refusing links and changed files; lock native databases."""
|
||||
import win32con, win32file
|
||||
entries = _scan(source)
|
||||
paths = list(entries)
|
||||
destination.mkdir(parents=True, exist_ok=False)
|
||||
record = {}
|
||||
with ExitStack() as stack:
|
||||
locks = {}
|
||||
for path in paths:
|
||||
if stat.S_ISREG(entries[path].st_mode) and (path.name.casefold() == 'icdb.dat' or path.suffix.casefold() == '.lmc'):
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
locks[path] = handle
|
||||
files = []
|
||||
for path in paths:
|
||||
relative = str(path.relative_to(source))
|
||||
copied = destination / relative
|
||||
if stat.S_ISDIR(entries[path].st_mode):
|
||||
copied.mkdir(parents=True, exist_ok=True)
|
||||
record[relative] = None
|
||||
continue
|
||||
files.append(path)
|
||||
def copy_file(path):
|
||||
relative = str(path.relative_to(source))
|
||||
copied = destination / relative
|
||||
before = entries[path]
|
||||
copied.parent.mkdir(parents=True, exist_ok=True)
|
||||
if path in locks:
|
||||
with copied.open('wb') as stream:
|
||||
while True:
|
||||
_, data = win32file.ReadFile(locks[path], 1024 * 1024)
|
||||
if not data:
|
||||
break
|
||||
stream.write(data)
|
||||
shutil.copystat(path, copied)
|
||||
digest = _hash(copied)
|
||||
else:
|
||||
digest = _hash(path)
|
||||
shutil.copy2(path, copied)
|
||||
if _hash(copied) != digest:
|
||||
raise OSError(f'副本校验失败:{path}')
|
||||
after = path.stat()
|
||||
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
|
||||
raise OSError(f'复制期间文件变化:{path}')
|
||||
return relative, digest
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
for count, (relative, digest) in enumerate(pool.map(copy_file, files), 1):
|
||||
record[relative] = digest
|
||||
if progress and (count % 256 == 0 or count == len(files)):
|
||||
progress(count, len(files))
|
||||
if _manifest(source) != record:
|
||||
raise OSError(f'复制期间目录内容变化:{source}')
|
||||
return record
|
||||
|
||||
|
||||
def _idle_check(library, project, pcb):
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
if pcb_processes() or any(_process_details(name) for name in (
|
||||
'LibraryManager.exe', 'viewdraw.exe')):
|
||||
raise ValueError('请先正常关闭 PCB、Library Manager 和原理图编辑器,再恢复备份。')
|
||||
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):
|
||||
@@ -173,17 +127,18 @@ def _source_check(source, name, original):
|
||||
|
||||
|
||||
def _align_library(project, pcb, library):
|
||||
from .mentor_adapter import ensure_project_central_library, ensure_pcb_central_library
|
||||
for path in (project, pcb.parent/'pcb.prj'):
|
||||
from .mentor_adapter import ensure_project_central_library
|
||||
from .maintenance_entry import maintenance_project_files
|
||||
projects = maintenance_project_files(project, pcb)
|
||||
for path in projects:
|
||||
if not re.findall(r'^KEY CentralLibrary "([^"]*)"', _project_text(path), re.M):
|
||||
raise ValueError('备份工程缺少 CentralLibrary 引用。')
|
||||
def already_references(path):
|
||||
return all((path.parent/value).resolve() == Path(library).resolve()
|
||||
for value in re.findall(r'^KEY CentralLibrary "([^"]*)"', _project_text(path), re.M))
|
||||
if not already_references(project):
|
||||
ensure_project_central_library(project, library)
|
||||
if not already_references(pcb.parent/'pcb.prj'):
|
||||
ensure_pcb_central_library(pcb, library)
|
||||
for path in projects:
|
||||
if not already_references(path):
|
||||
ensure_project_central_library(path, library)
|
||||
|
||||
|
||||
def recover_from_backup(request, source, *, progress=None, verifier=None, idle_check=None):
|
||||
@@ -209,25 +164,40 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
return VerifyResult('BLOCKED', stderr='本机验证目录不能位于目标库或备份工程内。')
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
library_lock = None
|
||||
release_confirmed = True
|
||||
installed = False
|
||||
record = {'operation': 'same_cbb_complete_project_restore', 'source': str(source),
|
||||
'library': str(library), 'block': request.block_name, 'installed': False,
|
||||
'validation_library': str(validation_dir/library.name)}
|
||||
def emit(stage, message):
|
||||
'validation_library': str(validation_dir/library.name), 'backup_policy': 'manual'}
|
||||
def emit(stage, message, **details):
|
||||
if progress:
|
||||
progress({'stage': stage, 'block': request.block_name, 'message': message})
|
||||
progress({'stage': stage, 'block': request.block_name, 'message': message, **details})
|
||||
def copy_progress(stage, message):
|
||||
def report(event):
|
||||
details = {key: value for key, value in event.items() if key != 'stage'}
|
||||
emit(stage, f'{message} {event["copied_files"]} / {event["total_files"]}',
|
||||
manifest_stage=event['stage'], **details)
|
||||
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)
|
||||
pythoncom.CoInitialize()
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (
|
||||
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
return VerifyResult('BLOCKED', stderr='已有 CBB 任务运行,请等待结束。', safe_to_continue=False)
|
||||
from .library_lock import LibraryLock
|
||||
library_lock = LibraryLock(library, safe_to_release=lambda: release_confirmed)
|
||||
library_lock.__enter__()
|
||||
if source.is_relative_to(library_root):
|
||||
raise ValueError('请选择目标中心库之外的独立备份工程。')
|
||||
project, pcb = preflight(replace(request, lmc_file=library))
|
||||
@@ -239,19 +209,17 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
library_size = sum(info.st_size for info in _scan(library_root).values() if stat.S_ISREG(info.st_mode))
|
||||
source_size = sum(info.st_size for info in _scan(source.parent).values() if stat.S_ISREG(info.st_mode))
|
||||
same_drive = root.anchor.casefold() == validation_dir.anchor.casefold()
|
||||
required = library_size * (2 if same_drive else 1) + source_size * 3
|
||||
required = (library_size + source_size * 2) if same_drive else 0
|
||||
if (shutil.disk_usage(root).free < required
|
||||
or shutil.disk_usage(validation_base).free < library_size + source_size * 2):
|
||||
raise ValueError('磁盘空间不足:需要容纳中心库回退副本和验证副本。')
|
||||
or shutil.disk_usage(validation_base).free < library_size + source_size * 2
|
||||
or shutil.disk_usage(library_root).free < source_size):
|
||||
raise ValueError('磁盘空间不足:需要容纳隔离验证库和待安装工程。')
|
||||
(root/'recovery-request.json').write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
emit('backup', '正在保留完整中心库副本…')
|
||||
original_manifest = _copy_verified(library_root, root/'library-before', progress=lambda n, total:
|
||||
emit('backup', f'正在保留中心库副本:{n} / {total} 个文件…'))
|
||||
(root/'library-before-manifest.json').write_text(json.dumps(original_manifest, indent=2), encoding='utf-8')
|
||||
emit('prepare_source', '正在准备备份验证副本…')
|
||||
source_manifest = _copy_verified(source.parent, root/'source-before')
|
||||
_copy_verified(root/'library-before', validation_dir, progress=lambda n, total:
|
||||
emit('prepare_source', f'正在准备验证库:{n} / {total} 个文件…'))
|
||||
emit('prepare_source', '正在准备隔离验证库;应用不自动创建回退备份…')
|
||||
library_scope = ManifestScope.library(request.block_name, library.name)
|
||||
original_manifest = _copy_verified(library_root, validation_dir, scope=library_scope,
|
||||
progress=copy_progress('prepare_source', '正在准备验证库:'))
|
||||
(root/'library-baseline-manifest.json').write_text(json.dumps(original_manifest, indent=2), encoding='utf-8')
|
||||
staged_library = validation_dir/library.name
|
||||
staged_work = staged_library.parent/'ReusableBlocksLP'/request.block_name/'Work'
|
||||
# Both paths are newly created private copies; originals are retained.
|
||||
@@ -259,7 +227,9 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
if not resolved_work.is_relative_to(validation_dir.resolve()):
|
||||
raise ValueError('验证副本工程路径越界。')
|
||||
shutil.rmtree(resolved_work)
|
||||
_copy_verified(root/'source-before', staged_work)
|
||||
source_manifest = _copy_verified(source.parent, staged_work,
|
||||
progress=copy_progress('prepare_source', '正在准备备份验证工程:'))
|
||||
(root/'source-manifest.json').write_text(json.dumps(source_manifest, indent=2), encoding='utf-8')
|
||||
staged_project = staged_work/source.name
|
||||
staged_pcb = staged_work/source_pcb.relative_to(source.parent)
|
||||
_align_library(staged_project, staged_pcb, staged_library)
|
||||
@@ -269,7 +239,9 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
native_lm_verify=False, interactive_input=False)
|
||||
preflight(validation)
|
||||
emit('validate_source', '正在副本中打开备份工程并执行原生校验…')
|
||||
release_confirmed = False
|
||||
source_result = verifier(validation, progress=progress, flag_reader=read_library_flag_if_available)
|
||||
release_confirmed = source_result.safe_to_continue
|
||||
record['source_validation'] = asdict(source_result)
|
||||
if (source_result.status != 'PASS' or source_result.library_flag != 'GREEN'
|
||||
or not source_result.safe_to_continue):
|
||||
@@ -277,15 +249,17 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
'备份校验未通过,目标未更改。\n' + (source_result.stderr or source_result.library_flag_detail)))
|
||||
idle_check(library, project, pcb)
|
||||
idle_check(staged_library, staged_project, staged_pcb)
|
||||
if _manifest(source.parent) != source_manifest:
|
||||
raise ValueError('备份来源在验证期间发生变化,目标未更改。')
|
||||
token = root.name.removeprefix('backup-restore-')
|
||||
work = project.parent.resolve()
|
||||
incoming = work.with_name('Work.restore-' + token)
|
||||
retained = work.with_name('Work.before-restore-' + token)
|
||||
_copy_verified(staged_work, incoming)
|
||||
_copy_verified(staged_work, incoming,
|
||||
progress=copy_progress('prepare_source', '正在准备恢复工程:'))
|
||||
_align_library(incoming/source.name, incoming/source_pcb.relative_to(source.parent), library)
|
||||
if _manifest(library_root, exclude=incoming.resolve()) != original_manifest:
|
||||
idle_check(library, source, source_pcb)
|
||||
if _manifest(source.parent) != source_manifest:
|
||||
raise ValueError('备份来源在验证期间发生变化,目标未更改。')
|
||||
if _manifest(library_root, scope=library_scope, exclude=incoming.resolve()) != original_manifest:
|
||||
raise ValueError('目标中心库在验证期间发生变化,停止恢复。')
|
||||
emit('install', '备份已通过验证,正在保留原工程并恢复…')
|
||||
work.rename(retained)
|
||||
@@ -297,19 +271,25 @@ def recover_from_backup(request, source, *, progress=None, verifier=None, idle_c
|
||||
installed = True
|
||||
record['retained_work'] = str(retained)
|
||||
emit('verify_target', '正在重新打开恢复后的目标工程并校验…')
|
||||
release_confirmed = False
|
||||
final = verifier(replace(request, lmc_file=library, evidence_dir=root/'target-verify',
|
||||
auto_repair=False, native_lm_verify=False, interactive_input=False),
|
||||
progress=progress, flag_reader=read_library_flag_if_available)
|
||||
release_confirmed = final.safe_to_continue
|
||||
if final.status == 'PASS' and (final.library_flag != 'GREEN' or not final.safe_to_continue):
|
||||
final = replace(final, status='FAIL', stderr='目标工程恢复后的 Ready 状态未确认。')
|
||||
if final.status != 'PASS':
|
||||
final = replace(final, stderr=(final.stderr + '\n原工程和完整库副本已保留,请查看恢复记录。').strip())
|
||||
final = replace(final, stderr=(final.stderr + '\n原 Work 已改名保留;应用未创建完整库备份,请查看恢复记录并使用手动备份。').strip())
|
||||
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:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
pythoncom.CoUninitialize()
|
||||
try:
|
||||
if library_lock is not None:
|
||||
library_lock.__exit__(None, None, None)
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
uninitialize_com(initialized)
|
||||
@@ -48,6 +48,7 @@ class BatchStatusRecord:
|
||||
detail: str = ""
|
||||
fingerprint: str = ""
|
||||
timestamp: str = ""
|
||||
block_type: str = "RULP"
|
||||
|
||||
@property
|
||||
def completed(self) -> bool:
|
||||
@@ -106,9 +107,11 @@ def fingerprint_library(lmc: Path) -> LmcFingerprint:
|
||||
# Metadata alone is not a safe checkpoint key: on network shares and on
|
||||
# file systems with coarse timestamp resolution, two different LMC
|
||||
# contents can have the same size and mtime. Include the actual file
|
||||
# digest so a changed central library can never reuse an old status row.
|
||||
# digest to invalidate changed LMC bytes; payload edits remain outside this key.
|
||||
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)
|
||||
|
||||
@@ -182,7 +186,12 @@ def _now() -> str:
|
||||
|
||||
|
||||
class BatchStatusSession:
|
||||
"""Supervised serial bridge session with checkpoint recovery."""
|
||||
"""Supervised serial bridge session; fresh status queries by default.
|
||||
|
||||
Checkpoints record scan evidence. Explicit ``resume=True`` is a low-level
|
||||
opt-in for controlled read-only scans, not a production freshness proof:
|
||||
the lightweight fingerprint does not cover native payload/database edits.
|
||||
"""
|
||||
|
||||
def __init__(self, bridge: StatusBridgeClient, *, timeout_seconds: float = 10.0,
|
||||
rotate_every: int = 500) -> None:
|
||||
@@ -192,7 +201,7 @@ class BatchStatusSession:
|
||||
self.restart_count = 0
|
||||
|
||||
def scan(self, names: Iterable[str], lmc_file: Path, checkpoint_path: Path,
|
||||
*, block_type: str = "RULP", resume: bool = True) -> list[BatchStatusRecord]:
|
||||
*, block_type: str = "RULP", resume: bool = False) -> list[BatchStatusRecord]:
|
||||
fingerprint = fingerprint_library(Path(lmc_file))
|
||||
if resume:
|
||||
checkpoint = BatchCheckpoint.load(checkpoint_path)
|
||||
@@ -211,7 +220,8 @@ class BatchStatusSession:
|
||||
queried = 0
|
||||
for index, name in enumerate(names):
|
||||
previous = checkpoint.get(name)
|
||||
if previous and previous.fingerprint == fingerprint.sha256:
|
||||
if (previous and previous.status in {'READY', 'PENDING'}
|
||||
and previous.fingerprint == fingerprint.sha256 and previous.block_type == block_type):
|
||||
results.append(previous)
|
||||
continue
|
||||
try:
|
||||
@@ -223,15 +233,18 @@ 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()
|
||||
unavailable = any(marker in message for marker in ("LMCDLL70", "私有 MFC", "未找到可用"))
|
||||
unavailable = any(marker in message for marker in ("LMCDLL70", "私有 MFC", "未找到可用")) or any(
|
||||
code in lower_message for code in ('0xc0000135', '-1073741515', '3221225781',
|
||||
'0xc000007b', '-1073741701', '3221225595'))
|
||||
malformed = (any(marker in message for marker in (
|
||||
"路径不匹配", "请求标识不匹配", "CBB 标识不匹配",
|
||||
)) or "mismatch" in lower_message)
|
||||
@@ -239,15 +252,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()
|
||||
@@ -255,7 +268,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
|
||||
@@ -264,6 +277,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)
|
||||
@@ -282,6 +296,8 @@ def authoritative_status_map(names: Iterable[str], lmc_file: Path, checkpoint_pa
|
||||
timeout_seconds: float = 10.0) -> dict[str, str]:
|
||||
"""Return only bridge-backed statuses for an optional UI integration.
|
||||
|
||||
Production always queries fresh statuses, including after native writes;
|
||||
saved READY/PENDING rows are never reused here.
|
||||
A missing bridge is represented as ``UNAVAILABLE``/``UNKNOWN`` by callers;
|
||||
it is never promoted to ``READY``.
|
||||
"""
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
"""Balanced COM initialization without tearing down an existing MTA caller."""
|
||||
|
||||
|
||||
def initialize_com():
|
||||
import pythoncom
|
||||
try:
|
||||
pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
|
||||
except pythoncom.com_error as error:
|
||||
if error.hresult != -2147417850: # RPC_E_CHANGED_MODE
|
||||
raise
|
||||
return False # Existing apartment is usable; no reference was added.
|
||||
return True
|
||||
|
||||
|
||||
def uninitialize_com(initialized):
|
||||
if initialized:
|
||||
import pythoncom
|
||||
pythoncom.CoUninitialize()
|
||||
@@ -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
|
||||
@@ -22,7 +22,7 @@ def launch_save_helper(command, **kwargs):
|
||||
return helper
|
||||
|
||||
|
||||
def start_supervisor_watchdog(supervisor_pid, *, cancel_path=None):
|
||||
def start_supervisor_watchdog(supervisor_pid, *, cancel_path=None, stop_clients=False):
|
||||
"""A crashed supervisor must not leave unsupervised automation writing.
|
||||
|
||||
Stop only this Python worker and its registered automation clients. Native
|
||||
@@ -38,7 +38,9 @@ def start_supervisor_watchdog(supervisor_pid, *, cancel_path=None):
|
||||
# Let native calls finish before the worker closes its exact
|
||||
# document. Killing a pending automation client can abandon it.
|
||||
Path(cancel_path).touch()
|
||||
return
|
||||
if not stop_clients:
|
||||
return
|
||||
threading.Event().wait(5)
|
||||
with _automation_clients_lock:
|
||||
for helper in _automation_clients:
|
||||
try:
|
||||
@@ -48,7 +50,7 @@ def start_supervisor_watchdog(supervisor_pid, *, cancel_path=None):
|
||||
pass
|
||||
finally:
|
||||
parent.Close()
|
||||
if cancel_path is None:
|
||||
if cancel_path is None or stop_clients:
|
||||
os._exit(75)
|
||||
|
||||
thread = threading.Thread(target=watch, name='supervisor-watchdog', daemon=True)
|
||||
@@ -72,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()
|
||||
@@ -118,9 +151,39 @@ class DesktopWorker:
|
||||
return int(pid)
|
||||
|
||||
def poll(self):
|
||||
if getattr(self, '_closed', False):
|
||||
return self._exit_code
|
||||
code = win32process.GetExitCodeProcess(self.handle)
|
||||
return None if code == 259 else code
|
||||
|
||||
def _save_clients(self):
|
||||
from .owner_probe import query_owners
|
||||
from .failed_session import _command_args
|
||||
expected = {Path(__file__).with_name(name).resolve()
|
||||
for name in ('save_verified_block.vbs', 'close_failed_block.vbs')}
|
||||
clients = []
|
||||
# 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'])
|
||||
if any(Path(arg).resolve() in expected for arg in args[1:] if arg.casefold().endswith('.vbs')):
|
||||
clients.append(row['pid'])
|
||||
return clients
|
||||
|
||||
def automation_release_state(self):
|
||||
try:
|
||||
remaining = self._save_clients()
|
||||
return {'safe_to_continue': self.poll() is not None and not remaining,
|
||||
'remaining_pids': remaining}
|
||||
except Exception as error:
|
||||
return {'safe_to_continue': False, 'remaining_pids': [], 'error': str(error)}
|
||||
|
||||
def stop_automation_clients(self):
|
||||
from .mentor_adapter import _terminate_pid
|
||||
for pid in self._save_clients():
|
||||
_terminate_pid(pid)
|
||||
|
||||
@property
|
||||
def returncode(self):
|
||||
return self.poll()
|
||||
@@ -132,12 +195,9 @@ class DesktopWorker:
|
||||
except Exception:
|
||||
pass
|
||||
# The worker is now stopped and cannot spawn another save helper.
|
||||
from .mentor_adapter import _process_details, _terminate_pid
|
||||
try:
|
||||
for pid, _exe, command, parent in _process_details('cscript.exe'):
|
||||
if (int(parent or 0) == self.pid
|
||||
and 'save_verified_block.vbs' in str(command).casefold()):
|
||||
_terminate_pid(pid) # single automation process, no /T
|
||||
self.wait(timeout=5)
|
||||
self.stop_automation_clients()
|
||||
except Exception:
|
||||
pass
|
||||
for companion in self.companions:
|
||||
@@ -162,6 +222,8 @@ class DesktopWorker:
|
||||
return self.poll()
|
||||
|
||||
def close(self):
|
||||
if getattr(self, '_closed', False):
|
||||
return
|
||||
# Releasing Python resources must not kill native database owners.
|
||||
try:
|
||||
if self.poll() is None:
|
||||
@@ -177,7 +239,9 @@ class DesktopWorker:
|
||||
pass
|
||||
self.companions.clear()
|
||||
self.companion_pids.clear()
|
||||
self._exit_code = self.poll()
|
||||
self.handle.Close()
|
||||
self._closed = True
|
||||
# Native processes keep their desktop alive if manual recovery is needed.
|
||||
self.job.Close()
|
||||
if self.desktop is not None:
|
||||
|
||||
@@ -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()
|
||||
@@ -236,6 +236,22 @@ class DialogActionExecutor:
|
||||
def perform(self, snapshot: WindowSnapshot, action: dict) -> ActionResult:
|
||||
channels = tuple(action.get("channels", ()))
|
||||
action_name = action.get("action")
|
||||
if action_name == 'user_selected_pcb_version':
|
||||
try:
|
||||
from .pcb_restore_prompt import validate_action_window
|
||||
if channels != ('win32_button_click',):
|
||||
raise ValueError('PCB_VERSION_ACTION_CHANNEL_MISMATCH')
|
||||
validate_action_window(snapshot, action)
|
||||
except Exception as error:
|
||||
return ActionResult(error_code='PCB_VERSION_CHOICE_REQUIRED', detail=str(error))
|
||||
if action_name == 'post_repair_local_read_write':
|
||||
try:
|
||||
from .post_repair_consent import validate_action_window
|
||||
validate_action_window(snapshot, action)
|
||||
if channels != ('win32_button_click',) or action.get('target') != {'control_id': 6}:
|
||||
raise ValueError('POST_REPAIR_ACTION_MISMATCH')
|
||||
except Exception as error:
|
||||
return ActionResult(error_code='DATABASE_RECOVERY_REQUIRED', detail=str(error))
|
||||
if action_name == "accept_license":
|
||||
target = self._license_target(snapshot)
|
||||
elif action_name == "restore_last_saved":
|
||||
@@ -308,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):
|
||||
@@ -332,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
|
||||
@@ -340,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="窗口已消失或指纹已变化",
|
||||
@@ -406,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()
|
||||
@@ -457,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,
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, fields, replace
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Any, Sequence
|
||||
@@ -185,6 +186,39 @@ def _decision(rule: DialogRule) -> RuleDecision:
|
||||
)
|
||||
|
||||
|
||||
def pcb_version_choice(snapshot):
|
||||
"""Describe the captured native choice; never select or approve a version."""
|
||||
if (snapshot.title != 'Database Restoration' or snapshot.window_class != '#32770'
|
||||
or snapshot.process_name.casefold() != 'expeditionpcb.exe' or snapshot.process_role != 'pcb'
|
||||
or snapshot.phase != DialogPhase.PCB_STARTING):
|
||||
return None
|
||||
explanation = ('The system has detected this design abnormally terminated the last time it was used. '
|
||||
'The user has to decide what database (if any) to associate with the design.')
|
||||
if not any(c.control_id == 6208 and ' '.join(c.name.split()) == explanation for c in snapshot.controls):
|
||||
return None
|
||||
candidates = []
|
||||
for identifier, kind, prefix in ((6213, 'last_user_saved', 'Load the last user saved database dated '),
|
||||
(6206, 'autosave', 'Load the autosave database dated ')):
|
||||
controls = [c for c in snapshot.controls if c.control_id == identifier and c.class_name == 'Button']
|
||||
if len(controls) != 1: return None
|
||||
control = controls[0]
|
||||
match = re.fullmatch(re.escape(prefix) + r'(.+)\.', ' '.join(control.name.split()))
|
||||
if not match: return None
|
||||
candidates.append({'kind': kind, 'control_id': identifier, 'label': control.name,
|
||||
'date': match.group(1), 'enabled': control.enabled,
|
||||
'selected': control.selected or control.checked})
|
||||
return {'candidates': candidates, 'automatic_selection': False}
|
||||
|
||||
|
||||
def archive_version_choice(snapshot, root):
|
||||
choice = pcb_version_choice(snapshot)
|
||||
if choice is None: raise ValueError('Not the recognized native PCB version choice')
|
||||
path = Path(root)/'pcb-version-choice.json'
|
||||
path.write_text(json.dumps({**choice, 'snapshot': snapshot.to_record()},
|
||||
ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
return path
|
||||
|
||||
|
||||
def decide(snapshot: WindowSnapshot, rules: Sequence[DialogRule]) -> RuleDecision:
|
||||
# Windows Text Services Framework language-bar window exposed by Mentor's
|
||||
# process is not a Mentor modal: it has no controls and must not abort a run.
|
||||
@@ -197,6 +231,12 @@ def decide(snapshot: WindowSnapshot, rules: Sequence[DialogRule]) -> RuleDecisio
|
||||
if parse_session_loss(combined):
|
||||
return RuleDecision(error_code=ERROR_CODE,
|
||||
detail=' | '.join(snapshot.texts) or snapshot.title)
|
||||
choice = pcb_version_choice(snapshot)
|
||||
if choice is not None:
|
||||
labels = {'last_user_saved': '最后用户保存', 'autosave': '自动保存'}
|
||||
detail = ';'.join(f'{labels[c["kind"]]}:{c["date"]}' for c in choice['candidates'])
|
||||
return RuleDecision(error_code='PCB_VERSION_CHOICE_REQUIRED',
|
||||
detail=f'PCB需选择恢复版本,尚未校验通过。候选版本:{detail}。未自动选择或确认任何版本。')
|
||||
# These prompts require knowledge of database ownership or a recovery
|
||||
# choice. A generic Yes/restore rule cannot establish either fact.
|
||||
if any(marker in combined for marker in (
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -5,6 +5,195 @@ from pathlib import Path
|
||||
import time
|
||||
|
||||
|
||||
def _command_args(command):
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
shell = ctypes.WinDLL('shell32', use_last_error=True)
|
||||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
shell.CommandLineToArgvW.argtypes = [wintypes.LPCWSTR, ctypes.POINTER(ctypes.c_int)]
|
||||
shell.CommandLineToArgvW.restype = ctypes.POINTER(wintypes.LPWSTR)
|
||||
kernel.LocalFree.argtypes = [wintypes.HLOCAL]
|
||||
kernel.LocalFree.restype = wintypes.HLOCAL
|
||||
count = ctypes.c_int()
|
||||
args = shell.CommandLineToArgvW(command, ctypes.byref(count))
|
||||
if not args:
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
try:
|
||||
return list(args[:count.value])
|
||||
finally:
|
||||
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: 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
|
||||
from .native_repair_ui import _current_desktop_name
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
root = Path(job['root'])
|
||||
result = {'status': 'RETAINED', 'pcb_pid': pcb_pid}
|
||||
result['release'] = {'pcb_exited': False, 'launcher_exited': launcher_process is None,
|
||||
'safe_to_continue': False}
|
||||
if launcher_process is not None:
|
||||
result['launcher_pid'] = launcher_process.pid
|
||||
try:
|
||||
desktop = job.get('desktop', '')
|
||||
if (job.get('interactive_input') or not desktop.startswith('CbbVerify_')
|
||||
or _current_desktop_name() != desktop):
|
||||
raise ValueError('STARTUP_DESKTOP_UNCONFIRMED')
|
||||
if not pcb_pid or pcb_pid in baseline_pids:
|
||||
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
|
||||
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 (cancelled_open or not addresses)}
|
||||
if result['release']['safe_to_continue']:
|
||||
result['status'] = 'CLOSED'
|
||||
result.pop('detail', None)
|
||||
break
|
||||
result['detail'] = 'STARTUP_RELEASE_UNCONFIRMED: 等待启动器退出和数据库地址释放'
|
||||
if time.monotonic() >= deadline:
|
||||
break
|
||||
time.sleep(.2)
|
||||
continue
|
||||
if not sent:
|
||||
_, executable, command, _ = records[0]
|
||||
args = _command_args(command)
|
||||
if (Path(executable).name.casefold() != 'expeditionpcb.exe'
|
||||
or '-reusablecircuit' not in [a.casefold() for a in args]
|
||||
or expected not in [os.path.normcase(os.path.abspath(a)) for a in args[1:]]):
|
||||
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
|
||||
windows = []
|
||||
def observe(hwnd, _):
|
||||
if (win32process.GetWindowThreadProcessId(hwnd)[1] == pcb_pid
|
||||
and win32gui.IsWindowVisible(hwnd)):
|
||||
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')
|
||||
and win32gui.IsWindowEnabled(w)]
|
||||
observed = [{'hwnd': w, 'class': win32gui.GetClassName(w),
|
||||
'title': win32gui.GetWindowText(w), 'enabled': bool(win32gui.IsWindowEnabled(w))}
|
||||
for w in windows]
|
||||
benign = [w for w in observed if w['title'] == '' and w['class'] in (
|
||||
'UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator')]
|
||||
for window in observed:
|
||||
if window['class'] == 'Internet Explorer_Hidden' and window['title'] == '':
|
||||
window['rect'] = list(win32gui.GetWindowRect(window['hwnd']))
|
||||
window['children'] = []
|
||||
win32api.SetLastError(0)
|
||||
win32gui.EnumChildWindows(window['hwnd'],
|
||||
lambda hwnd, _: window['children'].append(int(hwnd)), None)
|
||||
left, top, right, bottom = window['rect']
|
||||
if left == right and top == bottom and not window['children']:
|
||||
benign.append(window)
|
||||
unknown = [w for w in observed if w['hwnd'] not in main and w not in benign]
|
||||
result.update(owned_windows=observed, unknown_windows=unknown,
|
||||
benign_auxiliary_windows=benign)
|
||||
documents = []
|
||||
def child(hwnd, _):
|
||||
if (win32gui.GetClassName(hwnd) == 'MDIClient'
|
||||
and win32gui.GetWindow(hwnd, win32con.GW_CHILD)):
|
||||
documents.append(hwnd)
|
||||
if len(main) == 1:
|
||||
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 (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])
|
||||
# Observe exit even when the caller requested no extra wait.
|
||||
continue
|
||||
result['detail'] = 'STARTUP_IDLE_UNCONFIRMED: 等待空闲窗口和数据库释放'
|
||||
# After the single owned WM_CLOSE, WMI may lose the executable/
|
||||
# command fields before removing the PID. Only observe exit here;
|
||||
# never issue another close or treat missing fields as release.
|
||||
if time.monotonic() >= deadline:
|
||||
if sent:
|
||||
result['detail'] = 'STARTUP_EXIT_UNCONFIRMED: 已请求正常关闭,但 PCB 尚未退出'
|
||||
break
|
||||
time.sleep(.2)
|
||||
except Exception as error:
|
||||
result['detail'] = str(error)
|
||||
(root/'startup-close.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
emit(stage='startup_close_result', **result)
|
||||
return result
|
||||
|
||||
|
||||
def close_failed_document(job, *, emit=lambda **event: None, timeout_seconds=30):
|
||||
from .desktop_worker import launch_save_helper
|
||||
root = Path(job['root'])
|
||||
|
||||
@@ -1,22 +1,41 @@
|
||||
"""Reports for the explicit full-library maintenance action."""
|
||||
"""Reports for the explicit visible non-READY maintenance action."""
|
||||
from dataclasses import asdict
|
||||
import json
|
||||
|
||||
|
||||
def maintenance_counts(results):
|
||||
attempts = [attempt for result in results for attempt in result.recovery_attempts]
|
||||
repaired = sum(attempt.get('status') == 'REPAIRED' for attempt in attempts)
|
||||
repaired = sum(attempt.get('status') == 'REPAIRED'
|
||||
and attempt.get('native_status') == 'REPAIRED'
|
||||
and attempt.get('validation_passed') is True
|
||||
and result.status == 'PASS' and result.library_flag == 'GREEN'
|
||||
and result.safe_to_continue
|
||||
for result in results for attempt in result.recovery_attempts)
|
||||
review = sum(attempt.get('status') == 'RESTORED_REVIEW_REQUIRED' for attempt in attempts)
|
||||
version_choice = sum(attempt.get('status') == 'PCB_VERSION_CHOICE_REQUIRED'
|
||||
and attempt.get('native_status') == 'REPAIRED'
|
||||
and attempt.get('validation_passed') is False for attempt in attempts)
|
||||
pending = sum(attempt.get('status') == 'REPAIRED_UNVERIFIED'
|
||||
and attempt.get('native_status') == 'REPAIRED' for attempt in attempts)
|
||||
return {'total': len(results), 'passed': sum(r.status == 'PASS' for r in results),
|
||||
'checked_without_repair': sum(r.status == 'CHECKED' for r in results),
|
||||
'issues_found': sum(r.error_category == 'ICDB_REPAIR_REQUIRED' for r in results),
|
||||
'in_use': sum(r.error_category == 'ICDB_DATABASE_IN_USE' for r in results),
|
||||
'unconfirmed': sum(r.status not in ('PASS', 'CHECKED', 'SKIPPED', 'CANCELLED')
|
||||
and r.error_category not in ('ICDB_REPAIR_REQUIRED', 'ICDB_DATABASE_IN_USE') for r in results),
|
||||
'cancelled': sum(r.status == 'CANCELLED' for r in results),
|
||||
'skipped': sum(r.status == 'SKIPPED' for r in results),
|
||||
'repaired': repaired, 'review_required': review,
|
||||
'repair_unconfirmed': len(attempts)-repaired-review}
|
||||
'version_choice_required': version_choice,
|
||||
'repaired_unverified': pending,
|
||||
'repair_unconfirmed': len(attempts)-repaired-review-version_choice-pending}
|
||||
|
||||
|
||||
def write_maintenance_report(requests, results, batch_report, skipped_names):
|
||||
path = batch_report.with_name(batch_report.name.replace('batch-', 'icdb-', 1)).with_suffix('.json')
|
||||
record = {'operation': 'full_library_icdb_check_and_repair',
|
||||
'library': str(requests[0].lmc_file), 'includes_ready': True,
|
||||
record = {'operation': 'visible_non_ready_icdb_batch_check', 'automatic_repair': False,
|
||||
'library': str(requests[0].lmc_file), 'includes_ready': False,
|
||||
'target_names': [request.block_name for request in requests],
|
||||
'shared_database_repair_supported': False,
|
||||
'preflight_skipped': sorted(skipped_names), 'counts': maintenance_counts(results),
|
||||
'results': [{'block': req.block_name, **asdict(result)}
|
||||
|
||||
@@ -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)}
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time
|
||||
deadline = ActivityDeadline(timeout_seconds, clock=clock) if timeout_seconds is not None else None
|
||||
in_flight = {}
|
||||
executed = set()
|
||||
protected_green = set()
|
||||
action_history = []
|
||||
green_entry = None
|
||||
previous_state = None
|
||||
@@ -65,6 +66,8 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time
|
||||
# NoCES=3 is recorded as unsupported, never claimed green.
|
||||
if status not in (1, 2):
|
||||
raise IntegrationError(f'{name}: unsupported status {status}')
|
||||
if status != 2:
|
||||
protected_green.discard(name)
|
||||
for name in tuple(in_flight):
|
||||
if state[name] == 2:
|
||||
started, call_elapsed = in_flight.pop(name)
|
||||
@@ -74,11 +77,14 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time
|
||||
# When all four lamps start green, mirror the manual refresh: one
|
||||
# ForwardAnnotate, then only repair statuses that actually become Required.
|
||||
# Non-green entry keeps the previously validated four-action route.
|
||||
refreshed = ('ForwardAnnotation' in executed if green_entry
|
||||
else len(executed) == len(ACTIONS))
|
||||
checked = executed | protected_green
|
||||
refreshed = ('ForwardAnnotation' in checked if green_entry
|
||||
else len(checked) == len(ACTIONS))
|
||||
if refreshed and not in_flight and all(status == 2 for status in state.values()):
|
||||
emit(stage='integration_complete', statuses=state, actions=action_history,
|
||||
policy='green_first_only' if green_entry else 'required_full_sequence')
|
||||
policy='green_first_only' if green_entry else
|
||||
'required_with_native_gates' if protected_green else 'required_full_sequence',
|
||||
skipped_synchronized=sorted(protected_green))
|
||||
return state
|
||||
if deadline is not None and deadline.expired():
|
||||
raise IntegrationError('Project Integration timeout')
|
||||
@@ -87,14 +93,19 @@ def refresh_integration(pi, *, emit=lambda **event: None, settle_seconds=5, time
|
||||
continue
|
||||
action_started = False
|
||||
for name, method in ACTIONS:
|
||||
if state[name] == 2 and (name in executed or
|
||||
if state[name] == 2 and (name in checked or
|
||||
(green_entry and name != 'ForwardAnnotation')):
|
||||
continue
|
||||
allowed = bool(value(pi, 'Is' + name + 'Allowed'))
|
||||
# A green item still has to receive one Verify refresh attempt.
|
||||
# Mentor may report the capability as false for that item; this
|
||||
# flag is a gate only for a genuinely Required (status=1) item.
|
||||
if state[name] != 2 and not allowed:
|
||||
# Restored, previously verified CBBs can forbid a synchronized
|
||||
# action even while another action is Required. Do not invoke a
|
||||
# native-prohibited method or misreport the skip as a performed call.
|
||||
if state[name] == 2 and not allowed:
|
||||
protected_green.add(name)
|
||||
emit(stage='integration_action_skipped', action=method, initial_status=2,
|
||||
reason='synchronized_native_action_disallowed')
|
||||
continue
|
||||
if not allowed:
|
||||
emit(stage='integration_action_unavailable', action=method,
|
||||
initial_status=state[name], reason='mentor_not_allowed')
|
||||
raise IntegrationError(f'{name}: required but not allowed')
|
||||
|
||||
@@ -0,0 +1,606 @@
|
||||
"""Uploader-compatible exclusive sidecar lease. Metadata never proves ownership."""
|
||||
from datetime import datetime, timezone
|
||||
from contextlib import contextmanager, ExitStack
|
||||
import json
|
||||
import re
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import threading
|
||||
from uuid import uuid4
|
||||
|
||||
_guard = threading.RLock()
|
||||
_owners = {}
|
||||
|
||||
|
||||
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()
|
||||
|
||||
|
||||
def blocker_path(library):
|
||||
library = Path(library).resolve()
|
||||
return library.with_name('.' + library.stem + '.cbb-upload.blocked.json')
|
||||
|
||||
|
||||
def checkpoint_record(path):
|
||||
"""Recognize only our reserved filename and complete versioned protocol."""
|
||||
path = Path(path)
|
||||
if not (path.name.endswith('.cbb-upload.blocked.json') or path.name.endswith('.cbb-upload.blocked.pending')):
|
||||
return None
|
||||
try:
|
||||
import stat
|
||||
if path.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
return None
|
||||
record = json.loads(path.read_text(encoding='utf-8'))
|
||||
library = Path(record['library'])
|
||||
expected = '.' + library.stem + '.cbb-upload.blocked'
|
||||
if (record.get('protocol') != 'cbb-refresh-verify-library-lease' or record.get('version') != 1
|
||||
or not library.is_absolute() or library.suffix.casefold() != '.lmc'
|
||||
or path.name not in (expected+'.json', expected+'.pending')
|
||||
or not re.fullmatch('[0-9a-f]{32}', str(record.get('lease_id', '')))
|
||||
or not isinstance(record.get('pid'), int) or record['pid'] <= 0
|
||||
or record.get('status') not in ('OPERATION_IN_PROGRESS', 'OWNER_RELEASE_UNCONFIRMED')
|
||||
or not isinstance(record.get('time'), str)):
|
||||
return None
|
||||
return record
|
||||
except (OSError, ValueError, TypeError, KeyError):
|
||||
return None
|
||||
|
||||
|
||||
def has_blocker(library):
|
||||
path = blocker_path(library)
|
||||
# Complete snapshots retain every file, including their source lease
|
||||
# checkpoint. Only a valid relocated snapshot record can be ignored.
|
||||
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 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 recheck_previous_release(library, evidence_dir, *, block_name=None, required=False, progress=None):
|
||||
"""Historical records trigger fresh proof, never process termination or repair."""
|
||||
if not (required or has_blocker(library) or has_retained_library(library)):
|
||||
return None
|
||||
def notify(stage, **details):
|
||||
if progress is not None:
|
||||
try:
|
||||
progress({'stage': stage, 'block': block_name or '', **details})
|
||||
except Exception:
|
||||
import logging
|
||||
logging.getLogger(__name__).warning('Release progress observer failed', exc_info=True)
|
||||
notify('previous_release_check', detail='正在复核历史记录对应的实际占用')
|
||||
try:
|
||||
proof = release_retained_library(library, evidence_dir, block_name=block_name)
|
||||
except Exception as error:
|
||||
notify('previous_release_unconfirmed', detail=str(error))
|
||||
raise
|
||||
notify('previous_release_confirmed', release_proof=proof,
|
||||
detail='已确认会话释放并归档历史记录;这不表示历史数据已恢复')
|
||||
return proof
|
||||
|
||||
|
||||
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, block_name=None):
|
||||
library = Path(library).resolve()
|
||||
self.library = library
|
||||
self.path = library.with_name('.' + library.stem + '.cbb-upload.lock')
|
||||
self.key = _key(self.path)
|
||||
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
|
||||
|
||||
@classmethod
|
||||
def for_maintenance(cls, library, archive, guard, *, safe_to_release=None):
|
||||
lease = cls(library, safe_to_release=safe_to_release)
|
||||
lease.maintenance_guard = guard
|
||||
lease.maintenance_archive = Path(archive)
|
||||
return lease
|
||||
|
||||
def __enter__(self):
|
||||
with _guard:
|
||||
owner = _owners.get(self.key)
|
||||
if owner and owner['retained'] and owner['depth'] == 0 and owner.get('release_probe'):
|
||||
try:
|
||||
released = owner['release_probe']() is True
|
||||
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
|
||||
if self.maintenance_guard is not None:
|
||||
return _enter_maintenance(self)
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
owner = _owners.get(self.key)
|
||||
if owner:
|
||||
if owner['thread'] != threading.get_ident() or owner['retained']:
|
||||
raise LibraryBusy(f'中心库被其他任务占用:{self.path}')
|
||||
owner['depth'] += 1
|
||||
else:
|
||||
if self.checkpoint and has_blocker(self.library):
|
||||
raise LibraryBusy(f'中心库存在未解除的会话阻止记录,请先执行释放检查:{blocker_path(self.library)}')
|
||||
try:
|
||||
lease_id = uuid4().hex
|
||||
handle = win32file.CreateFile(str(self.path),
|
||||
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()}; '
|
||||
f'startedUtc={datetime.now(timezone.utc).isoformat()}; '
|
||||
f'library={self.library}').encode('utf-8')
|
||||
win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN)
|
||||
win32file.SetEndOfFile(handle)
|
||||
win32file.WriteFile(handle, data)
|
||||
win32file.FlushFileBuffers(handle)
|
||||
# Closing flushes the last-write timestamp on Windows. Set
|
||||
# it explicitly before complete-copy metadata is captured.
|
||||
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,
|
||||
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,
|
||||
'block_name': self.block_name,
|
||||
'release_probe': self.safe_to_release}
|
||||
self.entered = True
|
||||
return self
|
||||
|
||||
def snapshot_bytes(self):
|
||||
data = owned_snapshot(self.path)
|
||||
if data is None:
|
||||
raise LibraryBusy('当前线程不拥有此库协调锁')
|
||||
return data
|
||||
|
||||
def retain(self):
|
||||
with _guard:
|
||||
if self.entered:
|
||||
_owners[self.key]['retained'] = True
|
||||
|
||||
def __exit__(self, *_):
|
||||
try:
|
||||
self._exit_owned()
|
||||
finally:
|
||||
if self.maintenance_mutex is not None:
|
||||
import win32event
|
||||
win32event.ReleaseMutex(self.maintenance_mutex)
|
||||
self.maintenance_mutex.Close()
|
||||
self.maintenance_mutex = None
|
||||
|
||||
def _exit_owned(self):
|
||||
with _guard:
|
||||
if not self.entered:
|
||||
return
|
||||
owner = _owners[self.key]
|
||||
if not owner.get('checkpoint', True):
|
||||
if self.safe_to_release is not None:
|
||||
try:
|
||||
released = self.safe_to_release() is True
|
||||
except Exception:
|
||||
released = False
|
||||
if not released:
|
||||
owner['retained'] = True
|
||||
owner['depth'] -= 1
|
||||
self.entered = False
|
||||
if owner['depth'] == 0 and not owner['retained']:
|
||||
owner['handle'].Close()
|
||||
del _owners[self.key]
|
||||
return
|
||||
checkpoint = checkpoint_record(blocker_path(self.library))
|
||||
if checkpoint is not None and 'export_analysis' in checkpoint:
|
||||
owner['retained'] = True
|
||||
if self.safe_to_release is not None:
|
||||
try:
|
||||
safe = self.safe_to_release() is True
|
||||
except Exception:
|
||||
safe = False
|
||||
if not safe:
|
||||
# A supervisor can time out while its native child still
|
||||
# owns the DB. Keep excluding the uploader until app exit.
|
||||
owner['retained'] = True
|
||||
owner['depth'] -= 1
|
||||
self.entered = False
|
||||
if owner['depth'] == 0 and not owner['retained']:
|
||||
# This JSON is our durable operation checkpoint, not the
|
||||
# uploader's .lock file. Clear only after proven normal release.
|
||||
marker = blocker_path(self.library)
|
||||
record = checkpoint_record(marker)
|
||||
if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']):
|
||||
owner['retained'] = True
|
||||
raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。')
|
||||
try:
|
||||
marker.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
owner['retained'] = True
|
||||
raise
|
||||
owner['handle'].Close()
|
||||
del _owners[self.key]
|
||||
elif owner['retained']:
|
||||
marker = blocker_path(self.library)
|
||||
record = checkpoint_record(marker)
|
||||
if marker.exists() and (record is None or record['lease_id'] != owner['lease_id']):
|
||||
raise LibraryBusy('协调检查点被替换或损坏,保留冲突文件及库锁。')
|
||||
_persist_blocker(self.library, lease_id=owner['lease_id'])
|
||||
|
||||
|
||||
def _supervisor_alive(pid):
|
||||
import pywintypes, win32api, win32con, win32event
|
||||
try:
|
||||
handle = win32api.OpenProcess(win32con.SYNCHRONIZE, False, int(pid))
|
||||
except pywintypes.error as error:
|
||||
if error.winerror == 87: return False
|
||||
raise LibraryBusy('无法确认原监督进程退出') from error
|
||||
try:
|
||||
return win32event.WaitForSingleObject(handle, 0) != win32con.WAIT_OBJECT_0
|
||||
finally:
|
||||
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
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
handle = None
|
||||
existing = None
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired: raise LibraryBusy('已有 CBB 操作运行,维护不能接续')
|
||||
with _guard:
|
||||
marker = blocker_path(lease.library)
|
||||
if marker.with_suffix('.pending').exists():
|
||||
raise LibraryBusy('未完成检查点写入,不能自动接续')
|
||||
old = marker.read_bytes() if marker.exists() else None
|
||||
record = checkpoint_record(marker) if old is not None else None
|
||||
if old is not None and (record is None or _key(record['library']) != _key(lease.library)):
|
||||
raise LibraryBusy('维护拒绝未知、损坏或其他库的检查点')
|
||||
existing = _owners.get(lease.key)
|
||||
if existing:
|
||||
if (not existing['retained'] or existing['depth'] != 0 or record is None
|
||||
or record['pid'] != os.getpid() or record['lease_id'] != existing['lease_id']):
|
||||
raise LibraryBusy('本进程库锁仍在使用或检查点身份不符')
|
||||
handle = existing['handle']
|
||||
else:
|
||||
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('维护检查期间检查点变化')
|
||||
if old is not None:
|
||||
lease.maintenance_archive.mkdir(parents=True, exist_ok=False)
|
||||
saved = lease.maintenance_archive/'previous-checkpoint.json'
|
||||
with saved.open('xb') as stream:
|
||||
stream.write(old); stream.flush(); os.fsync(stream.fileno())
|
||||
if saved.read_bytes() != old: raise OSError('原检查点归档校验失败')
|
||||
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, 'checkpoint': True,
|
||||
'release_probe': lease.safe_to_release, 'block_name': None}
|
||||
lease.maintenance_proof = proof
|
||||
lease.entered = True
|
||||
lease.maintenance_mutex = mutex
|
||||
return lease
|
||||
except Exception:
|
||||
if handle is not None and existing is None: handle.Close()
|
||||
if acquired: win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
raise
|
||||
|
||||
|
||||
def owned_snapshot(path):
|
||||
"""Read only a lease held by this thread, never reopen an exclusive file."""
|
||||
import win32con, win32file
|
||||
with _guard:
|
||||
owner = _owners.get(_key(path))
|
||||
if not owner or owner['thread'] != threading.get_ident() or owner['retained']:
|
||||
return None
|
||||
handle = owner['handle']
|
||||
position = win32file.SetFilePointer(handle, 0, win32con.FILE_CURRENT)
|
||||
try:
|
||||
win32file.SetFilePointer(handle, 0, win32con.FILE_BEGIN)
|
||||
size = win32file.GetFileSize(handle)
|
||||
return win32file.ReadFile(handle, size)[1] if size else b''
|
||||
finally:
|
||||
win32file.SetFilePointer(handle, position, win32con.FILE_BEGIN)
|
||||
|
||||
|
||||
@contextmanager
|
||||
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
|
||||
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()
|
||||
try:
|
||||
names = ('LibraryManager.exe', 'ExpeditionPCB.exe', 'viewdraw.exe',
|
||||
'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe',
|
||||
'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 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', '--native-backup-worker')):
|
||||
owners.append(item)
|
||||
if owners:
|
||||
raise LibraryBusy(f'原生进程仍存在,不能解除协调阻止:{owners}')
|
||||
return owners
|
||||
check_owners()
|
||||
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.is_relative_to(scope_root)
|
||||
or path.is_relative_to(shared_root))]
|
||||
if addresses:
|
||||
raise LibraryBusy(f'库中仍有数据库地址记录,不能自动解除:{addresses}')
|
||||
# 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.is_relative_to(scope_root)
|
||||
or path.is_relative_to(shared_root))))]
|
||||
if library not in native:
|
||||
raise LibraryBusy('目标库身份缺失,不能解除协调阻止。')
|
||||
with ExitStack() as stack:
|
||||
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)
|
||||
|
||||
|
||||
def release_retained_library(library, evidence_dir, *, block_name=None):
|
||||
"""Reconcile historical leases after fresh owner/address/access proof.
|
||||
|
||||
The upload sidecar is never deleted. This acknowledges process release,
|
||||
not the correctness of a partially changed library or its recovery state.
|
||||
"""
|
||||
import win32con, win32event, win32file
|
||||
library = Path(library).resolve(strict=True)
|
||||
evidence = Path(evidence_dir).resolve()
|
||||
if evidence.is_relative_to(library.parent) or library.parent.is_relative_to(evidence):
|
||||
raise ValueError('释放检查证据必须位于目标库之外。')
|
||||
lease = LibraryLock(library)
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
external = None
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
raise LibraryBusy('另一个 CBB 操作仍在运行。')
|
||||
with _guard:
|
||||
owner = _owners.get(lease.key)
|
||||
if owner and (not owner['retained'] or owner['depth']):
|
||||
raise LibraryBusy('本应用操作仍在使用库锁。')
|
||||
if owner is None:
|
||||
try:
|
||||
external = win32file.CreateFile(str(lease.path),
|
||||
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
|
||||
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)
|
||||
reconciled = [{'kind': 'checkpoint', 'path': str(path), 'recognized': metadata is not None,
|
||||
**{key: (metadata or {}).get(key) for key in ('block_name', 'lease_id', 'pid', 'status', 'time')}}
|
||||
for path, data in before.items() if data is not None
|
||||
for metadata in (checkpoint_record(path),)]
|
||||
if owner:
|
||||
reconciled.append({'kind': 'retained_owner', 'path': str(lease.path),
|
||||
'block_name': owner.get('block_name'), 'lease_id': owner['lease_id'],
|
||||
'status': 'OWNER_RELEASE_UNCONFIRMED'})
|
||||
foreign = sorted({item['block_name'] for item in reconciled
|
||||
if block_name and item.get('block_name') and item['block_name'] != block_name})
|
||||
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:
|
||||
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,
|
||||
'requested_block_name': block_name, 'proof_scope': 'BLOCK' if scoped else 'LIBRARY',
|
||||
'proof_block_name': scoped, 'reconciled_checkpoints': reconciled,
|
||||
'reconciled_foreign_blockers': foreign,
|
||||
'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):
|
||||
if checkpoint.exists():
|
||||
data = checkpoint.read_bytes()
|
||||
archived = root/f'blocker-before-{index}.json'
|
||||
archived.write_bytes(data)
|
||||
if archived.read_bytes() != data:
|
||||
raise OSError('阻止记录归档校验失败,保持库锁。')
|
||||
checkpoint.unlink() # Checkpoint JSON only; never .lock.
|
||||
if owner:
|
||||
owner['handle'].Close()
|
||||
del _owners[lease.key]
|
||||
return record
|
||||
finally:
|
||||
if external is not None:
|
||||
external.Close()
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
@@ -0,0 +1,451 @@
|
||||
"""User-requested native inspection with or without an existing advertisement.
|
||||
|
||||
An advertisement is never deleted, quarantined or called stale by this module.
|
||||
Exclusive access and exact project binding permit an official inspection.
|
||||
"""
|
||||
from contextlib import contextmanager, ExitStack
|
||||
from dataclasses import asdict, replace
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path, PureWindowsPath
|
||||
import re
|
||||
import stat
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
def assert_native_idle(target=None):
|
||||
from .owner_probe import query_owners
|
||||
from .native_management import _retained_workers
|
||||
from .native_export_guard import assert_retained_exports_idle
|
||||
from .native_engineering_identity import _OWNER_NAMES
|
||||
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',
|
||||
'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe', *sorted(_OWNER_NAMES))
|
||||
owners = []
|
||||
for item in query_owners(names):
|
||||
if item['pid'] == os.getpid():
|
||||
continue
|
||||
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', '--native-backup-worker')):
|
||||
owners.append(item)
|
||||
if target is not None:
|
||||
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}')
|
||||
|
||||
|
||||
def _assert_native_idle_for(target):
|
||||
"""Keep test/third-party zero-argument idle probes compatible."""
|
||||
import inspect
|
||||
if not inspect.signature(assert_native_idle).parameters:
|
||||
return assert_native_idle()
|
||||
return assert_native_idle(target)
|
||||
|
||||
|
||||
def address_snapshot(paths):
|
||||
from .scoped_manifest import hash_file
|
||||
return {str(path.resolve()): {'sha256': hash_file(path), 'size': path.stat().st_size,
|
||||
'mtime_ns': path.stat().st_mtime_ns}
|
||||
for path in paths}
|
||||
|
||||
|
||||
def maintenance_entries(request, *, check=None):
|
||||
from .verify_runner import preflight
|
||||
from .scoped_manifest import scan_tree
|
||||
project, _ = preflight(request)
|
||||
root = Path(request.lmc_file).parent
|
||||
entries = scan_tree(project.parent, check=check)
|
||||
shared = root/'SymbolLibs/__ReusableBlocksSymb'
|
||||
if shared.exists():
|
||||
entries.update(scan_tree(shared, check=check))
|
||||
for library in root.glob('*.lmc'):
|
||||
info = library.lstat()
|
||||
if (info.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT
|
||||
or not stat.S_ISREG(info.st_mode)):
|
||||
raise ValueError(f'不支持通过文件链接维护:{library}')
|
||||
entries[library] = info
|
||||
return entries
|
||||
|
||||
|
||||
def _scoped_addresses(request, *, check=None, entries=None):
|
||||
from .scoped_manifest import scan_tree
|
||||
root = Path(request.lmc_file).parent
|
||||
addresses = []
|
||||
if entries is None:
|
||||
entries = maintenance_entries(request, check=check)
|
||||
if check is not None: check()
|
||||
for path in entries:
|
||||
if path.name.casefold() != 'saddress.adr':
|
||||
continue
|
||||
parts = tuple(part.casefold() for part in path.relative_to(root).parts)
|
||||
independent = (len(parts) >= 6 and parts[0] == 'reusableblockslp'
|
||||
and parts[1] != request.block_name.casefold() and parts[2] == 'work'
|
||||
and parts[3:] in (('database', 'cdbsvr', 'saddress.adr'),
|
||||
('pcb', 'database', 'cdbsvr', 'saddress.adr')))
|
||||
if not independent:
|
||||
# Current project and shared/unknown scopes remain visible to the
|
||||
# selector; only exact independent other-CBB Work stores are omitted.
|
||||
addresses.append(path)
|
||||
return addresses
|
||||
|
||||
|
||||
def maintenance_project_files(project, pcb):
|
||||
"""A legacy linked PCB need not contain the auxiliary PCB/pcb.prj.
|
||||
|
||||
Its explicit ProjectFile must bind back to the main project. Never guess
|
||||
template.prj (or another PRJ) as an alternative database repair target.
|
||||
"""
|
||||
from .backup_recovery import _project_text
|
||||
project, pcb = Path(project), Path(pcb)
|
||||
auxiliary = pcb.parent/'pcb.prj'
|
||||
if auxiliary.is_file():
|
||||
return project, auxiliary
|
||||
if os.path.lexists(auxiliary):
|
||||
raise ValueError(f'MAINTENANCE_TARGET_UNCONFIRMED: 工程配置不是文件:{auxiliary}')
|
||||
references = re.findall(r'^KEY ProjectFile "([^"\r\n]+)"', _project_text(pcb), re.M)
|
||||
if (len(references) != 1 or (pcb.parent/references[0]).resolve() != project.resolve()):
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少辅助 pcb.prj,且 PCB 的 ProjectFile 未唯一指向当前主工程')
|
||||
return (project,)
|
||||
|
||||
|
||||
def select_maintenance_target(request, *, check=None, entries=None):
|
||||
from .verify_runner import preflight
|
||||
from .backup_recovery import _project_text
|
||||
from .native_repair import RepairTarget
|
||||
from .scoped_manifest import scan_tree
|
||||
if request.maintenance is not True:
|
||||
raise ValueError('MAINTENANCE_INTENT_REQUIRED')
|
||||
project, pcb = preflight(request)
|
||||
if entries is None:
|
||||
entries = maintenance_entries(request, check=check)
|
||||
candidates = []
|
||||
for candidate in maintenance_project_files(project, pcb):
|
||||
if not candidate.is_file():
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少工程配置')
|
||||
text = _project_text(candidate)
|
||||
values = re.findall(r'^KEY iCDBDir "([^"]*)"', text, re.M)
|
||||
if candidate != project and not re.search(r'^KEY iCDBDir\b', text, re.M):
|
||||
continue
|
||||
if (len(values) != 1 or not values[0] or PureWindowsPath(values[0]).drive
|
||||
or PureWindowsPath(values[0]).root):
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: iCDBDir 必须唯一且为工程内相对路径')
|
||||
database = (candidate.parent/values[0]).resolve()
|
||||
# The proven Project Backup route binds PRJ.parent/database; do not
|
||||
# guess alternate stores or route shared data through another PRJ.
|
||||
if database != (candidate.parent/'database').resolve() or not database.is_relative_to(project.parent.resolve()):
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 未验证的 PRJ 数据库映射')
|
||||
if not (database/'icdb.dat').is_file():
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 缺少 icdb.dat')
|
||||
candidates.append(RepairTarget(candidate, candidate.parent/'database'))
|
||||
addresses = _scoped_addresses(request, check=check, entries=entries)
|
||||
if not addresses:
|
||||
# A crashed project may retain its internal open state after the server
|
||||
# advertisement disappears. Explicit maintenance must still inspect it.
|
||||
references = re.findall(r'^KEY ProjectFile "([^"\r\n]+)"', _project_text(pcb), re.M)
|
||||
if len(references) != 1 or (pcb.parent/references[0]).resolve() != project.resolve():
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: PCB 的 ProjectFile 未唯一指向所选主工程')
|
||||
if len(candidates) != 1:
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 无地址记录且存在多个工程数据库,不能推测修复目标')
|
||||
return candidates[0], project, pcb, addresses
|
||||
if len(addresses) != 1:
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED: 需要唯一工程地址记录;共享或多个数据库不能推测修复')
|
||||
matches = [target for target in candidates
|
||||
if (target.database/'cdbsvr/sAddress.adr').resolve() == addresses[0].resolve()]
|
||||
if len(matches) != 1:
|
||||
raise ValueError('SHARED_DATABASE_REPAIR_UNSUPPORTED: 地址记录不属于唯一匹配的工程数据库')
|
||||
return matches[0], project, pcb, addresses
|
||||
|
||||
|
||||
@contextmanager
|
||||
def maintenance_target_guard(request, expected=None, *, stop=None):
|
||||
"""Simultaneously hold native database files, preserving every address."""
|
||||
import win32con, win32file
|
||||
from .scoped_manifest import scan_tree, hash_file
|
||||
def check():
|
||||
if stop is not None and stop.is_set():
|
||||
raise ValueError('MAINTENANCE_CANCELLED: 已取消占用检查')
|
||||
check()
|
||||
library_root = Path(request.lmc_file).parent
|
||||
# Share one complete snapshot for target/address selection and file locks.
|
||||
# Refresh it again while the handles are held; do not reuse across guards.
|
||||
entries = maintenance_entries(request, check=check)
|
||||
target, project, pcb, addresses = select_maintenance_target(request, check=check, entries=entries)
|
||||
baseline = address_snapshot(addresses)
|
||||
context = {'library': str(Path(request.lmc_file).resolve()), 'block': request.block_name,
|
||||
'project': str(target.project.resolve()), 'database': str(target.database.resolve()),
|
||||
'addresses': baseline,
|
||||
'engineering_references': {str(p.resolve()): hash_file(p)
|
||||
for p in (pcb, *maintenance_project_files(project, pcb))}}
|
||||
if expected is not None and context != expected:
|
||||
raise ValueError('MAINTENANCE_SOURCE_CHANGED: 目标或地址记录在检查后变化')
|
||||
_assert_native_idle_for(target)
|
||||
check()
|
||||
def native_files(snapshot):
|
||||
return {path for path, info in snapshot.items() if stat.S_ISREG(info.st_mode) and (
|
||||
path.suffix.casefold() == '.lmc' or path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'))}
|
||||
files = native_files(entries)
|
||||
with ExitStack() as stack:
|
||||
for path in sorted(files):
|
||||
check()
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
_assert_native_idle_for(target)
|
||||
current = maintenance_entries(request, check=check)
|
||||
if native_files(current) != files:
|
||||
raise ValueError('MAINTENANCE_SOURCE_CHANGED: 检查期间库文件集合发生变化')
|
||||
current_addresses = _scoped_addresses(request, check=check, entries=current)
|
||||
if address_snapshot(current_addresses) != baseline:
|
||||
raise ValueError('MAINTENANCE_SOURCE_CHANGED: 地址记录在检查期间变化')
|
||||
yield {'target': target, 'context': context, 'project': project, 'pcb': pcb}
|
||||
|
||||
|
||||
def run_icdb_check(request, *, progress=None, stop=None):
|
||||
"""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):
|
||||
return VerifyResult('FAIL', error_category='ICDB_INSPECTION_UNCONFIRMED',
|
||||
stderr='检查记录目录需位于中心库之外。')
|
||||
record = {'operation': 'icdb_batch_check', 'automatic_repair': False,
|
||||
'library': str(request.lmc_file), 'block': request.block_name}
|
||||
started = False
|
||||
def save(result):
|
||||
report = root/'result.json'
|
||||
recorded = replace(result, evidence_path=report)
|
||||
try:
|
||||
report.write_text(json.dumps({**record, 'result': asdict(recorded)},
|
||||
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
except OSError as error:
|
||||
import logging
|
||||
logging.getLogger(__name__).warning('icdb_check_report_unavailable path=%s error=%s', report, error)
|
||||
return result
|
||||
return recorded
|
||||
try:
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
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=not started,
|
||||
error_category='ICDB_INSPECTION_UNCONFIRMED', stderr=f'无法确认 ICDB 状态:{error}'))
|
||||
|
||||
|
||||
def run_diagnosed_maintenance(request, *, diagnose, progress=None, stop=None):
|
||||
"""Open PCB once; only a current matching native fault admits maintenance."""
|
||||
from .verify_runner import VerifyResult
|
||||
from .icdb_recovery import parse_session_loss
|
||||
from .native_repair import select_repair_target
|
||||
if stop is not None and stop.is_set():
|
||||
return VerifyResult('CANCELLED', stderr='已取消,未打开工程或请求修复。')
|
||||
if progress:
|
||||
progress({'stage': 'icdb_startup_check', 'block': request.block_name,
|
||||
'detail': '尝试打开工程,依据本次原生报错判断是否需要 ICDB 维护'})
|
||||
startup = diagnose(replace(request, maintenance=False, auto_repair=False,
|
||||
diagnostic_only=True, native_lm_verify=False, post_repair_proof=None))
|
||||
def finish(result):
|
||||
if startup.evidence_path:
|
||||
report = startup.evidence_path.with_name('maintenance-result.json')
|
||||
recorded = replace(result, evidence_path=report)
|
||||
try:
|
||||
report.write_text(json.dumps({'block_name': request.block_name,
|
||||
'startup_evidence': str(startup.evidence_path), **asdict(recorded)},
|
||||
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
except OSError as error:
|
||||
import logging
|
||||
logging.getLogger(__name__).warning('maintenance_result_write_failed path=%s error=%s', report, error)
|
||||
else:
|
||||
result = recorded
|
||||
return result
|
||||
if startup.status == 'PASS' and startup.safe_to_continue:
|
||||
return finish(replace(startup, status='CHECKED', library_flag='UNKNOWN',
|
||||
stdout='工程打开正常,无需 ICDB 修复;未执行 CBB 校验。'))
|
||||
if startup.status == 'PASS':
|
||||
return finish(replace(startup, status='FAIL', stderr='工程打开后未确认会话释放,未请求 ICDB 修复。'))
|
||||
if startup.status != 'FAIL' or not startup.safe_to_continue:
|
||||
return startup
|
||||
# Do not search prior evidence directories or treat an error-code label,
|
||||
# address/checkpoint file, timeout or generic server error as a diagnosis.
|
||||
diagnosis = parse_session_loss(startup.stderr)
|
||||
if diagnosis is None:
|
||||
return startup
|
||||
try:
|
||||
target = select_repair_target(request, diagnosis)
|
||||
except (ValueError, OSError) as error:
|
||||
return finish(replace(startup, stderr=f'{startup.stderr}\n{error}',
|
||||
error_stage='icdb_target_check'))
|
||||
if stop is not None and stop.is_set():
|
||||
return finish(replace(startup, status='CANCELLED', stderr='已取消,未请求 ICDB 修复。'))
|
||||
return run_existing_address_maintenance(request, diagnosed_target=target, startup_result=startup,
|
||||
progress=progress, stop=stop)
|
||||
|
||||
|
||||
def run_existing_address_maintenance(request, *, diagnosed_target, startup_result, progress=None, stop=None):
|
||||
from .library_lock import LibraryLock
|
||||
from .native_repair import run_native_repair, native_success_confirmed, repair_release_state
|
||||
from .verify_runner import VerifyResult
|
||||
root = Path(request.evidence_dir).resolve()/('maintenance-' + 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):
|
||||
return VerifyResult('BLOCKED', stderr='维护证据必须位于目标库之外', safe_to_continue=False)
|
||||
root.mkdir(parents=True, exist_ok=False)
|
||||
record = {'operation': 'explicit_project_maintenance', 'library': str(request.lmc_file),
|
||||
'block': request.block_name, 'stage': 'preflight',
|
||||
'startup_diagnosis': asdict(startup_result)}
|
||||
report = root/'result.json'
|
||||
owner_safe = False
|
||||
recovery = None
|
||||
def emit(stage, **values):
|
||||
record['stage'] = stage
|
||||
if progress: progress({'stage': stage, 'block': request.block_name, **values})
|
||||
def save(result):
|
||||
result = replace(result, evidence_path=report)
|
||||
record['result'] = asdict(result)
|
||||
report.write_text(json.dumps(record, ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
return result
|
||||
def native_event(event):
|
||||
emit(event['stage'], **{key: value for key, value in event.items() if key != 'stage'})
|
||||
try:
|
||||
if stop is not None and stop.is_set():
|
||||
return save(VerifyResult('CANCELLED', stderr='维护已取消,未启动原生操作'))
|
||||
emit('preflight', detail='正在核对工程、库文件和进程占用;可请求取消')
|
||||
lease = LibraryLock.for_maintenance(request.lmc_file, root/'takeover',
|
||||
lambda: maintenance_target_guard(request, stop=stop), safe_to_release=lambda: owner_safe)
|
||||
with lease:
|
||||
owner_safe = True # Admission completed; no native client exists yet.
|
||||
checked = lease.maintenance_proof
|
||||
target, context = checked['target'], checked['context']
|
||||
record['maintenance_context'] = context
|
||||
if (target.project.resolve() != diagnosed_target.project.resolve()
|
||||
or target.database.resolve() != diagnosed_target.database.resolve()):
|
||||
return save(VerifyResult('FAIL', safe_to_continue=owner_safe,
|
||||
error_category='ICDB_TARGET_MISMATCH', error_stage='preflight',
|
||||
stderr='本次打开报错的数据库与维护目标不一致,未请求 ICDB 修复。'))
|
||||
if stop is not None and stop.is_set():
|
||||
raise ValueError('MAINTENANCE_CANCELLED: 已取消,未启动原生检查')
|
||||
emit('icdb_ownership_check', detail='调用原生 Project Backup 检查占用及恢复要求,不执行 Repair')
|
||||
owner_safe = False
|
||||
inspection = run_native_repair(target, root/'native-inspection',
|
||||
mentor_bin=request.mentor_bin, timeout_seconds=min(30, request.timeout_seconds),
|
||||
progress=native_event, maintenance_context=context, inspection_only=True, stop=stop)
|
||||
record['native_inspection'] = inspection
|
||||
owner_safe = (inspection.get('release', {}).get('native_exited') is True
|
||||
or inspection.get('native_pid') is None and inspection.get('worker_pid') is None)
|
||||
from .native_occupancy import confirmed_inspection
|
||||
if not confirmed_inspection(target, inspection):
|
||||
return save(VerifyResult('BLOCKED', safe_to_continue=owner_safe,
|
||||
error_category='ICDB_INSPECTION_UNCONFIRMED',
|
||||
stderr='原生占用检查未确认或已取消,未请求 Repair。' + str(inspection.get('detail', inspection.get('status')))))
|
||||
if inspection['status'] == 'IN_USE':
|
||||
return save(VerifyResult('BLOCKED', safe_to_continue=owner_safe,
|
||||
error_category='ICDB_DATABASE_IN_USE', stderr='原生工具确认工程正在使用,未请求 Repair。'))
|
||||
# Native loading may retire an old address itself. Permit only
|
||||
# disappearance, never a new/changed advertisement or project.
|
||||
with maintenance_target_guard(request, stop=stop) as refreshed:
|
||||
updated = refreshed['context']
|
||||
if ({k: v for k, v in context.items() if k != 'addresses'}
|
||||
!= {k: v for k, v in updated.items() if k != 'addresses'}
|
||||
or any(context['addresses'].get(k) != v for k, v in updated['addresses'].items())):
|
||||
raise ValueError('MAINTENANCE_SOURCE_CHANGED: 原生检查后工程或地址身份变化')
|
||||
context = updated
|
||||
record['post_inspection_context'] = context
|
||||
if not inspection['repair_required']:
|
||||
return save(VerifyResult('CHECKED', safe_to_continue=owner_safe,
|
||||
stdout='检查完成,无需修复。'))
|
||||
if stop is not None and stop.is_set():
|
||||
raise ValueError('MAINTENANCE_CANCELLED: 已取消,未开始 Repair')
|
||||
emit('icdb_diagnosis_confirmed', detail='本轮原生检查明确要求修复当前数据库,且确认未占用')
|
||||
native_root = root/'native-repair'; native_root.mkdir()
|
||||
attempt = native_root/'attempt.json'
|
||||
with attempt.open('x', encoding='utf-8') as stream:
|
||||
json.dump({'intent': 'explicit_maintenance', 'target': asdict(target),
|
||||
'maintenance_context': context}, stream, default=str, ensure_ascii=False)
|
||||
emit('native_repair_start', detail='核对占用后由官方工具检查/修复;应用不自动备份')
|
||||
# Repeat immediately before dispatch; the worker repeats before
|
||||
# launching the native process as well.
|
||||
with maintenance_target_guard(request, context, stop=stop): pass
|
||||
owner_safe = False
|
||||
recovery = run_native_repair(target, native_root, mentor_bin=request.mentor_bin,
|
||||
timeout_seconds=max(60, request.timeout_seconds), progress=native_event,
|
||||
maintenance_context=context, stop=stop)
|
||||
_assert_native_idle_for(target)
|
||||
recovery = {**recovery, 'native_status': recovery.get('status'), 'validation_passed': False}
|
||||
recovery['release'] = repair_release_state(request, target, recovery)
|
||||
safe = recovery['release'].get('safe_to_continue') is True
|
||||
confirmed = recovery.get('status') == 'REPAIRED' and safe and native_success_confirmed(target, recovery)
|
||||
# Release follows actual clients, not the repair/content verdict.
|
||||
owner_safe = safe or (recovery['release'].get('native_exited') is True
|
||||
and recovery['release'].get('database_exclusive') is True
|
||||
and recovery['release'].get('remaining_pids') == [])
|
||||
if confirmed:
|
||||
emit('native_repair_complete', detail='原生 ICDB 修复完成,CBB 待单独校验')
|
||||
recovery['status'] = 'REPAIRED_UNVERIFIED'
|
||||
return save(VerifyResult('REPAIRED', safe_to_continue=owner_safe,
|
||||
stdout='原生 ICDB 修复完成,CBB 待单独校验。', recovery_attempts=(recovery,)))
|
||||
if recovery.get('status') == 'REPAIRED':
|
||||
recovery['status'] = 'UNCONFIRMED'; owner_safe = False
|
||||
return save(VerifyResult('FAIL', stderr=f'{recovery["status"]}: {recovery.get("detail", "原生维护未确认修复")}',
|
||||
safe_to_continue=owner_safe, recovery_attempts=(recovery,), error_category='ICDB_MAINTENANCE_RESULT'))
|
||||
except Exception as error:
|
||||
return save(VerifyResult('BLOCKED' if recovery is None else 'FAIL', stderr=str(error),
|
||||
safe_to_continue=False, recovery_attempts=(recovery,) if recovery else (),
|
||||
error_category='ICDB_MAINTENANCE_BLOCKED', error_stage=record['stage']))
|
||||
@@ -0,0 +1,431 @@
|
||||
"""Checkpointed CBB mutations with native evidence and no automatic replay."""
|
||||
from dataclasses import asdict, dataclass, field, replace
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
from pathlib import Path, PureWindowsPath
|
||||
import re
|
||||
from uuid import uuid4
|
||||
|
||||
from .library_lock import LibraryLock, LibraryBusy
|
||||
from .scoped_manifest import (ManifestScope, build_manifest, build_exclusive_library_manifest,
|
||||
comparison_rules, copy_verified, hash_file, scan_tree)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Capability:
|
||||
supported: bool
|
||||
reason: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ManagementRequest:
|
||||
operation: str
|
||||
library: Path
|
||||
name: str
|
||||
partition: str = 'Logical-Physical'
|
||||
source: Path | None = None
|
||||
auto_verify: bool = True
|
||||
evidence_dir: Path = Path('management-evidence')
|
||||
top_level_block: str = ''
|
||||
timeout_seconds: float = 180
|
||||
mapped_drive: str = ''
|
||||
mapped_remote_root: str = ''
|
||||
transaction_token: str = ''
|
||||
check_previous_release: bool = False
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CatalogEntry:
|
||||
name: str
|
||||
partition: str
|
||||
engineering_identity: dict = field(default_factory=dict)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Catalog:
|
||||
library: Path
|
||||
flow: int
|
||||
entries: tuple[CatalogEntry, ...]
|
||||
safe_to_continue: bool = True
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class NativeOutcome:
|
||||
success: bool
|
||||
safe_to_continue: bool
|
||||
detail: str
|
||||
identity_matches: bool | None = None
|
||||
evidence_path: Path | None = None
|
||||
identity_proof: dict | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ManagementResult:
|
||||
operation: str
|
||||
library: Path
|
||||
name: str
|
||||
partition: str
|
||||
source: Path | None
|
||||
auto_verify: bool
|
||||
stage: str
|
||||
status: str
|
||||
safe_to_continue: bool
|
||||
detail: str = ''
|
||||
evidence_path: Path | None = None
|
||||
release_proof: dict | None = None
|
||||
change_report: dict | None = None
|
||||
|
||||
|
||||
def validate_name(value):
|
||||
if (not value or value != value.strip() or value.endswith('.') or value in ('.', '..')
|
||||
or any(ord(c) < 32 or c in '<>:"/\\|?*' for c in value)
|
||||
or value.split('.')[0].upper() in {'CON', 'PRN', 'AUX', 'NUL',
|
||||
*(f'COM{i}' for i in range(1, 10)), *(f'LPT{i}' for i in range(1, 10))}):
|
||||
raise ValueError('CBB 名称/分区不是有效单一名称,禁止路径、控制字符或保留名称。')
|
||||
|
||||
|
||||
def source_project(source, top_level_block=''):
|
||||
"""Require complete PRJ/PCB and self-contained engineering references."""
|
||||
from .backup_recovery import _project_text
|
||||
source = Path(source).resolve(strict=True)
|
||||
if source.suffix.casefold() != '.prj' or not source.is_file():
|
||||
raise ValueError('请选择完整工程的 PRJ 文件。')
|
||||
scan_tree(source.parent) # Reject reparse points, including file links.
|
||||
text = _project_text(source)
|
||||
roots = re.findall(r'^KEY RootBlock "([^"]+)"', text, re.M)
|
||||
if len(roots) != 1 or top_level_block and roots[0] != top_level_block:
|
||||
raise ValueError('工程顶层块不唯一或与输入不一致。')
|
||||
validate_name(roots[0])
|
||||
def reference(path, key):
|
||||
values = re.findall(r'^KEY ' + re.escape(key) + r' "([^"]+)"', _project_text(path), re.M)
|
||||
if len(values) != 1 or PureWindowsPath(values[0]).is_absolute() or PureWindowsPath(values[0]).drive:
|
||||
raise ValueError(f'{key} 必须为唯一工程内相对引用。')
|
||||
resolved = (path.parent/values[0]).resolve()
|
||||
if not resolved.is_relative_to(source.parent):
|
||||
raise ValueError(f'{key} 越出来源工程。')
|
||||
return resolved
|
||||
pcb = reference(source, 'PCBDesignPath')
|
||||
if not pcb.is_file() or reference(pcb, 'ProjectFile') != source:
|
||||
raise ValueError('PCB 未引用所选来源工程。')
|
||||
from .maintenance_entry import maintenance_project_files
|
||||
for path in maintenance_project_files(source, pcb):
|
||||
if not path.is_file():
|
||||
raise ValueError('工程缺少 PCB 工程配置。')
|
||||
# Healthy Expedition PCB projects can use Layout/LayoutDB.lyt without
|
||||
# declaring an independent iCDBDir. The schematic DB stays mandatory;
|
||||
# an explicitly declared PCB DB must still pass every reference check.
|
||||
if path != source and not re.search(r'^KEY iCDBDir\b', _project_text(path), re.M):
|
||||
continue
|
||||
db = reference(path, 'iCDBDir')
|
||||
if not (db/'icdb.dat').is_file():
|
||||
raise ValueError('工程缺少原理图/PCB 数据库。')
|
||||
if not (pcb.parent/'Layout/LayoutDB.lyt').is_file():
|
||||
raise ValueError('工程缺少布局数据库。')
|
||||
return source, pcb, roots[0]
|
||||
|
||||
|
||||
def engineering_identity(source):
|
||||
"""Compare actual registered engineering bytes, not a name-only acknowledgment.
|
||||
|
||||
Conservative until native normalization has an independently proven semantic
|
||||
comparison: changed database bytes cannot be claimed to match the source.
|
||||
"""
|
||||
source, pcb, top = source_project(source)
|
||||
from .backup_recovery import _project_text
|
||||
db = re.findall(r'^KEY iCDBDir "([^"]+)"', _project_text(source), re.M)[0]
|
||||
return {'top_level_block': top, 'schematic_sha256': hash_file(source.parent/db/'icdb.dat'),
|
||||
'layout_sha256': hash_file(pcb.parent/'Layout/LayoutDB.lyt')}
|
||||
|
||||
|
||||
def _write_json(path, value):
|
||||
path = Path(path)
|
||||
pending = path.with_suffix('.pending')
|
||||
with pending.open('w', encoding='utf-8') as stream:
|
||||
json.dump(value, stream, ensure_ascii=False, default=str, indent=2)
|
||||
stream.flush()
|
||||
import os
|
||||
os.fsync(stream.fileno())
|
||||
pending.replace(path)
|
||||
|
||||
|
||||
def unfinished_operations(root):
|
||||
results = []
|
||||
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
|
||||
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
|
||||
import win32con, win32event
|
||||
import pythoncom
|
||||
if adapter is None:
|
||||
from .native_management import NativeManagementAdapter
|
||||
adapter = NativeManagementAdapter()
|
||||
verifier = verifier or run_verify
|
||||
idle_check = idle_check or _idle_check
|
||||
root = report = None
|
||||
stage = 'preflight'
|
||||
history = []
|
||||
mutated = uploaded = removed = False
|
||||
owner_safe = True
|
||||
mutex = None
|
||||
acquired = False
|
||||
release_proof = None
|
||||
original = None
|
||||
change_report = 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
|
||||
history.append({'stage': stage, 'time': datetime.now(timezone.utc).isoformat(), **details})
|
||||
if report:
|
||||
_write_json(report, {**asdict(request), 'stage': stage, 'status': 'RUNNING',
|
||||
'safe_to_continue': False, 'history': history, 'backup_policy': 'manual'})
|
||||
if progress:
|
||||
progress({'stage': stage, 'block': request.name, 'operation': request.operation, **details})
|
||||
def finish(status, detail='', safe=True):
|
||||
nonlocal change_report
|
||||
if mutated and original is not None and change_report is None:
|
||||
change_report = {'status': 'UNCONFIRMED', 'scope': original['scope'],
|
||||
**comparison_rules(scope),
|
||||
'unrelated_cbb_content_checked': False,
|
||||
'detail': '原生会话尚未释放,未读取写后内容'}
|
||||
if safe:
|
||||
try:
|
||||
after = build_exclusive_library_manifest(library_root, scope=scope, progress=progress)
|
||||
_write_json(root/'library-after-manifest.json', after)
|
||||
before_files, after_files = original['files'], after['files']
|
||||
change_report.update(status='RECORDED', detail='目标工程及共享依赖的内容差分',
|
||||
added=sorted(after_files.keys()-before_files.keys()),
|
||||
deleted=sorted(before_files.keys()-after_files.keys()),
|
||||
modified=sorted(path for path in before_files.keys() & after_files.keys()
|
||||
if before_files[path] != after_files[path]))
|
||||
except Exception as error:
|
||||
change_report['detail'] = f'写后内容未确认:{error}'
|
||||
try:
|
||||
_write_json(root/'library-changes.json', change_report)
|
||||
except OSError as error:
|
||||
change_report.update(status='UNCONFIRMED', evidence_error=str(error),
|
||||
detail=f'变更记录保存失败:{error}')
|
||||
if change_report['status'] != 'RECORDED':
|
||||
detail = (detail+'\n'+change_report['detail']).strip()
|
||||
result = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', status, safe, detail, report, release_proof, change_report)
|
||||
if report:
|
||||
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()
|
||||
try:
|
||||
if request.operation not in ('add', 'update', 'remove'):
|
||||
raise ValueError('不支持的管理操作。')
|
||||
validate_name(request.name); validate_name(request.partition)
|
||||
from .network_path import absolute_preserving_drive
|
||||
library = absolute_preserving_drive(request.library)
|
||||
canonical_library = library.resolve(strict=True)
|
||||
library_root = canonical_library.parent
|
||||
if library.suffix.casefold() != '.lmc' or not library.is_file():
|
||||
raise ValueError('中心库必须为现有 LMC 文件。')
|
||||
evidence = Path(request.evidence_dir).resolve()
|
||||
if evidence.is_relative_to(library_root) or library_root.is_relative_to(evidence):
|
||||
raise ValueError('操作记录与暂存目录必须位于目标中心库之外,且不能包含目标库。')
|
||||
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
|
||||
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:
|
||||
raise ValueError('新增/更新需要完整来源工程。')
|
||||
source, pcb, top = source_project(request.source, request.top_level_block)
|
||||
if (source.parent.is_relative_to(library_root) or library_root.is_relative_to(source.parent)
|
||||
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 校验/管理/恢复任务运行;本次未启动。')
|
||||
from .library_lock import recheck_previous_release
|
||||
release_proof = recheck_previous_release(library, root/'previous-release', block_name=request.name,
|
||||
required=request.check_previous_release or bool(unfinished_target
|
||||
and unfinished_target.get('safe_to_continue') is not True), progress=progress)
|
||||
if release_proof is not None:
|
||||
checkpoint('release_check', release_proof=release_proof)
|
||||
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, 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':
|
||||
# Read actual engineering references when available; fallback
|
||||
# paths still permit removal of catalog entries missing Work.
|
||||
from .verify_runner import preflight
|
||||
try:
|
||||
target_project, target_pcb = preflight(VerifyRequest(library, request.name, root))
|
||||
except ValueError:
|
||||
pass
|
||||
idle_check(library, target_project, target_pcb)
|
||||
if source:
|
||||
idle_check(library, source, pcb)
|
||||
checkpoint('catalog_before')
|
||||
owner_safe = False
|
||||
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)
|
||||
same_name = [entry for entry in before.entries if entry.name.casefold() == request.name.casefold()]
|
||||
matches = [entry for entry in same_name if entry.partition == request.partition and entry.name == request.name]
|
||||
if request.operation == 'add' and same_name:
|
||||
return finish('UPLOAD_FAILED', '同名 CBB 已存在;新增禁止覆盖,请使用明确的同名更新。')
|
||||
if request.operation != 'add' and len(matches) != 1:
|
||||
return finish('BLOCKED', '目标名称和分区必须与原生目录唯一匹配。')
|
||||
if hasattr(adapter, 'preflight'):
|
||||
adapter.preflight(request, before)
|
||||
# Native/project owners must be idle before snapshot or mutation.
|
||||
idle_check(library, target_project, target_pcb)
|
||||
scope = ManifestScope.library(request.name, library.name)
|
||||
checkpoint('baseline')
|
||||
original = build_exclusive_library_manifest(library_root, scope=scope, progress=progress)
|
||||
_write_json(root/'library-baseline-manifest.json', original)
|
||||
staged = request
|
||||
identity = None
|
||||
if source:
|
||||
checkpoint('stage_source')
|
||||
source_manifest = copy_verified(source.parent, root/'source', progress=progress)
|
||||
_write_json(root/'source-manifest.json', source_manifest)
|
||||
staged = replace(request, source=root/'source'/source.name, top_level_block=top)
|
||||
identity = engineering_identity(staged.source)
|
||||
if build_manifest(source.parent) != source_manifest:
|
||||
raise OSError('来源工程在暂存期间变化。')
|
||||
if source and hasattr(adapter, 'prepare_add_transaction'):
|
||||
checkpoint('prepare_identity')
|
||||
owner_safe = False
|
||||
staged, proof = adapter.prepare_add_transaction(staged, root, original_source=source)
|
||||
owner_safe = proof.safe_to_continue
|
||||
if not owner_safe or proof.identity is None:
|
||||
return finish('UPLOAD_FAILED', proof.detail, owner_safe)
|
||||
identity = proof.identity
|
||||
if build_exclusive_library_manifest(library_root, scope=scope, progress=progress) != original:
|
||||
raise OSError('内容基线记录后中心库发生变化,停止写入。')
|
||||
if request.operation in ('remove', 'update'):
|
||||
checkpoint('native_remove')
|
||||
mutated = True; owner_safe = False
|
||||
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, owner_safe)
|
||||
idle_check(library, target_project, target_pcb)
|
||||
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', '原生移除后目录未确认目标缺失。', owner_safe)
|
||||
removed = True
|
||||
checkpoint('removed')
|
||||
if request.operation == 'remove':
|
||||
return finish('REMOVED', '已由原生操作移除,重新读取目录确认不存在;应用未创建备份。')
|
||||
checkpoint('native_add')
|
||||
mutated = True; owner_safe = False
|
||||
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)
|
||||
idle_check(library, target_project, target_pcb)
|
||||
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]
|
||||
proof_matches = (native.identity_matches is True and native.identity_proof == identity
|
||||
if hasattr(adapter, 'prepare_add_transaction') else
|
||||
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', '原生新增后名称、分区或来源工程内容未确认一致。', owner_safe)
|
||||
uploaded = True
|
||||
checkpoint('uploaded')
|
||||
if not request.auto_verify:
|
||||
return finish('UPLOADED', '上传已确认;本批次未启用自动校验。')
|
||||
# Adapter catalog cannot return until its exact native owner exits.
|
||||
checkpoint('verify')
|
||||
owner_safe = False
|
||||
verified = verifier(VerifyRequest(library, request.name, root/'verify',
|
||||
mapped_drive=request.mapped_drive, mapped_remote_root=request.mapped_remote_root), progress=progress)
|
||||
owner_safe = verified.safe_to_continue
|
||||
checkpoint('verified', verification=asdict(verified))
|
||||
good = verified.status == 'PASS' and verified.library_flag == 'GREEN' and owner_safe
|
||||
return finish('UPLOADED_VERIFIED' if good else 'UPLOADED_VERIFY_FAILED',
|
||||
verified.stderr or verified.library_flag_detail, owner_safe)
|
||||
except Exception as error:
|
||||
if uploaded:
|
||||
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'
|
||||
if stage == 'release_check' and not mutated:
|
||||
status = 'BLOCKED'
|
||||
return finish(status, str(error), owner_safe)
|
||||
finally:
|
||||
if mutex is not None:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
uninitialize_com(initialized)
|
||||
|
||||
|
||||
def run_management_batch(requests, *, stop=None, progress=None, **kwargs):
|
||||
results = []
|
||||
for request in requests:
|
||||
if stop is not None and stop.is_set():
|
||||
break
|
||||
result = run_management(request, progress=progress, **kwargs)
|
||||
results.append(result)
|
||||
if progress:
|
||||
progress({'stage': result.status, 'block': result.name, 'detail': result.detail,
|
||||
'evidence_path': str(result.evidence_path or '')})
|
||||
if not result.safe_to_continue:
|
||||
break
|
||||
return results
|
||||
@@ -0,0 +1,124 @@
|
||||
"""Prepared Add authority bound to the engine checkpoint, baseline and live lease."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import threading
|
||||
from uuid import uuid4
|
||||
|
||||
from .management import _write_json, engineering_identity
|
||||
from .scoped_manifest import build_manifest
|
||||
|
||||
|
||||
def _read(path):
|
||||
return json.loads(Path(path).read_text(encoding='utf-8'))
|
||||
|
||||
|
||||
def fixture_authority(root, library, token):
|
||||
root = Path(root).resolve()
|
||||
expected = root/'native-add/fixture/mentor_lib.lmc'
|
||||
owner = _read(root/'probe-owner.json')
|
||||
if (not token or owner.get('token') != token or not expected.is_file()
|
||||
or Path(library).resolve() != expected.resolve()
|
||||
or Path(owner.get('library', '')).resolve() != expected.resolve()):
|
||||
raise ValueError('Management research requires the exact newly created owned fixture.')
|
||||
return {'root': str(root), 'token': token}
|
||||
|
||||
|
||||
def current_lease(library):
|
||||
from .library_lock import LibraryLock, _guard, _owners, blocker_path, checkpoint_record
|
||||
lease = LibraryLock(library)
|
||||
with _guard:
|
||||
owner = _owners.get(lease.key)
|
||||
guard = checkpoint_record(blocker_path(library))
|
||||
if (owner is None or owner['retained'] or owner['thread'] != threading.get_ident()
|
||||
or guard is None or guard['pid'] != os.getpid()
|
||||
or guard['status'] != 'OPERATION_IN_PROGRESS'
|
||||
or owner['lease_id'] != guard['lease_id']
|
||||
or Path(guard['library']).resolve() != Path(library).resolve()):
|
||||
raise ValueError('Management transaction requires the current thread exact active library lease.')
|
||||
return {'lease_id': owner['lease_id'], 'lease_handle': int(owner['handle']), 'supervisor_pid': os.getpid()}
|
||||
|
||||
|
||||
def _authorize(record, mentor_home):
|
||||
fixture = record.get('fixture')
|
||||
if fixture is not None:
|
||||
fixture_authority(fixture['root'], record['library'], fixture['token'])
|
||||
else:
|
||||
from .native_management import NativeManagementAdapter
|
||||
capability = NativeManagementAdapter(mentor_home=mentor_home).capabilities()['add']
|
||||
if not capability.supported: raise ValueError(capability.reason)
|
||||
|
||||
|
||||
def prepare_transaction(request, root, expected_identity, *, mentor_home, fixture=None):
|
||||
root = Path(root).resolve()
|
||||
source = Path(request.source).resolve()
|
||||
if source.parent != root/'source':
|
||||
raise ValueError('Native Add accepts only the management engine staged source.')
|
||||
library = Path(request.library).resolve()
|
||||
if root.is_relative_to(library.parent) or library.parent.is_relative_to(root):
|
||||
raise ValueError('Management transaction evidence overlaps the target library.')
|
||||
checkpoint = _read(root/'checkpoint.json')
|
||||
stages = {item['stage'] for item in checkpoint.get('history', [])}
|
||||
if (checkpoint.get('status') != 'RUNNING' or checkpoint.get('stage') != 'prepare_identity'
|
||||
or Path(checkpoint['library']).resolve() != library
|
||||
or checkpoint['name'] != request.name or checkpoint['partition'] != request.partition
|
||||
or not {'baseline', 'stage_source'}.issubset(stages)):
|
||||
raise ValueError('Native Add requires the engine baseline/staging checkpoint.')
|
||||
library_baseline = _read(root/'library-baseline-manifest.json')
|
||||
baseline = _read(root/'source-identity/identity.json')
|
||||
if baseline.get('safe_to_continue') is not True or baseline.get('identity') != expected_identity:
|
||||
raise ValueError('Native source identity evidence is missing or changed.')
|
||||
record = {**current_lease(library), 'nonce': uuid4().hex, 'library': str(library),
|
||||
'name': request.name, 'partition': request.partition, 'top_level_block': request.top_level_block,
|
||||
'source_name': source.name, 'source_identity': engineering_identity(source),
|
||||
'source_manifest': build_manifest(source.parent), 'library_baseline': library_baseline,
|
||||
'expected_identity': expected_identity, 'fixture': fixture}
|
||||
_authorize(record, mentor_home)
|
||||
_write_json(root/'native-add-prepared.json', record)
|
||||
return record['nonce']
|
||||
|
||||
|
||||
def validate_transaction(job, root, *, check_source=True):
|
||||
from .library_lock import blocker_path, checkpoint_record, _supervisor_alive
|
||||
import win32api, win32con, win32file
|
||||
root = Path(root).resolve()
|
||||
record = _read(root/'native-add-prepared.json')
|
||||
library = Path(job['library']).resolve()
|
||||
source = root/'source'/record['source_name']
|
||||
guard = checkpoint_record(blocker_path(library))
|
||||
checkpoint = _read(root/'checkpoint.json')
|
||||
if (not job.get('transaction_nonce') or job['transaction_nonce'] != record['nonce']
|
||||
or Path(record['library']).resolve() != library
|
||||
or any(job.get(key) != record[key] for key in ('name', 'partition', 'top_level_block', 'source_identity'))
|
||||
or source.parent != root/'source' or source.resolve().parent != (root/'source').resolve()
|
||||
or Path(job['source']).resolve() != source.resolve()
|
||||
or guard is None or guard['lease_id'] != record['lease_id']
|
||||
or guard['pid'] != record['supervisor_pid'] or guard['pid'] != job['supervisor_pid']
|
||||
or guard['status'] != 'OPERATION_IN_PROGRESS'
|
||||
or Path(guard['library']).resolve() != library or not _supervisor_alive(guard['pid'])
|
||||
or checkpoint.get('status') != 'RUNNING' or checkpoint.get('stage') != 'native_add'
|
||||
or Path(checkpoint['library']).resolve() != library
|
||||
or checkpoint['name'] != record['name'] or checkpoint['partition'] != record['partition']):
|
||||
raise ValueError('Native Add transaction authority is missing, stale or foreign.')
|
||||
_authorize(record, job['mentor_home'])
|
||||
baseline = _read(root/'source-identity/identity.json')
|
||||
if (baseline.get('safe_to_continue') is not True or baseline.get('identity') != record['expected_identity']
|
||||
or _read(root/'library-baseline-manifest.json') != record['library_baseline']
|
||||
or check_source and (build_manifest(source.parent) != record['source_manifest']
|
||||
or engineering_identity(source) != record['source_identity'])):
|
||||
raise ValueError('Native Add library baseline or staged source proof changed.')
|
||||
process = win32api.OpenProcess(win32con.PROCESS_DUP_HANDLE, False, record['supervisor_pid'])
|
||||
duplicate = None
|
||||
try:
|
||||
duplicate = win32api.DuplicateHandle(process, record['lease_handle'], win32api.GetCurrentProcess(),
|
||||
0, False, win32con.DUPLICATE_SAME_ACCESS)
|
||||
final_path = win32file.GetFinalPathNameByHandle(duplicate, 0)
|
||||
if final_path.startswith('\\\\?\\UNC\\'): final_path = '\\\\'+final_path[8:]
|
||||
elif final_path.startswith('\\\\?\\'): final_path = final_path[4:]
|
||||
expected_lock = library.with_name('.'+library.stem+'.cbb-upload.lock')
|
||||
if Path(final_path).resolve() != expected_lock.resolve():
|
||||
raise ValueError('Native Add supervisor lease belongs to another library.')
|
||||
finally:
|
||||
if duplicate is not None: duplicate.Close()
|
||||
process.Close()
|
||||
return record
|
||||
@@ -217,21 +217,12 @@ def launch_pcb_process(command, cwd):
|
||||
|
||||
|
||||
def _process_details(name):
|
||||
queries = {
|
||||
'ExpeditionPCB.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='ExpeditionPCB.exe'",
|
||||
'LibraryManager.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='LibraryManager.exe'",
|
||||
'cscript.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='cscript.exe'",
|
||||
'viewdraw.exe': "SELECT ProcessId, ParentProcessId, ExecutablePath, CommandLine FROM Win32_Process WHERE Name='viewdraw.exe'",
|
||||
}
|
||||
if name not in queries:
|
||||
if name not in {'ExpeditionPCB.exe', 'LibraryManager.exe', 'iCDBProjectBackup.exe',
|
||||
'ReuseBlockImporter.exe', 'cscript.exe', 'viewdraw.exe'}:
|
||||
raise ValueError(f'Unsupported process query: {name}')
|
||||
import win32com.client
|
||||
service = win32com.client.GetObject('winmgmts:')
|
||||
return [
|
||||
(int(p.ProcessId), str(p.ExecutablePath or ''), str(p.CommandLine or ''),
|
||||
int(p.ParentProcessId or 0))
|
||||
for p in service.ExecQuery(queries[name])
|
||||
]
|
||||
from .owner_probe import query_owners
|
||||
return [(row['pid'], row['executable'], row['command'], row['parent'])
|
||||
for row in query_owners((name,))]
|
||||
|
||||
|
||||
def _processes(name):
|
||||
@@ -245,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):
|
||||
@@ -614,40 +606,25 @@ def execute_job(job, emit):
|
||||
|
||||
root = Path(job['root'])
|
||||
block_root = Path(job['project']).parent.parent
|
||||
# COM publishes a shared active application. Never attach while another
|
||||
# native PCB session (including a retained failure) is still running.
|
||||
pcb_before = {pid for pid, _, _ in pcb_processes()}
|
||||
if pcb_before:
|
||||
raise RuntimeError(f'PCB_SESSION_ACTIVE: 已有 PCB 会话,请正常关闭后再校验: {sorted(pcb_before)}')
|
||||
# Do not recursively snapshot a live CBB over SMB immediately before
|
||||
# launching Mentor. The backup is evidence only; on Win10 a timed-out
|
||||
# robocopy can retain network handles/oplocks and make Verify nondeterministic.
|
||||
# Unrelated designs stay open; COM and save helpers still require the exact
|
||||
# project identity before any integration or save call.
|
||||
from .failed_session import _command_args
|
||||
existing = tuple(pcb_processes())
|
||||
pcb_before = {pid for pid, _, _ in existing}
|
||||
expected = os.path.normcase(os.path.abspath(job['pcb']))
|
||||
for pid, _, command in existing:
|
||||
if command and expected in [os.path.normcase(os.path.abspath(a)) for a in _command_args(command)[1:]]:
|
||||
raise RuntimeError(f'PCB_SESSION_ACTIVE: 目标工程已经打开:{pid}')
|
||||
# Backups are user-managed on local and network libraries alike.
|
||||
mapped_drive = str(job.get('mapped_drive') or '')
|
||||
mapped_remote_root = str(job.get('mapped_remote_root') or '')
|
||||
if mapped_drive and mapped_remote_root:
|
||||
from .network_path import ensure_mapped_drive_visible
|
||||
mapping = ensure_mapped_drive_visible(mapped_drive, mapped_remote_root)
|
||||
emit(stage='mapped_drive_context', **mapping)
|
||||
emit(stage='backup', block=job['block_name'])
|
||||
if is_network_backed_path(block_root, mapped_drive=mapped_drive):
|
||||
backup = {'status': 'SKIPPED',
|
||||
'detail': '网络盘 CBB 跳过递归备份,避免 robocopy 与 Mentor 竞争 SMB 文件锁'}
|
||||
emit(stage='backup_skipped', reason='network_source', source=str(block_root))
|
||||
else:
|
||||
backup_budget = backup_timeout_seconds(block_root)
|
||||
emit(stage='backup_budget', timeout_seconds=backup_budget)
|
||||
backup = best_effort_backup(block_root, root/'backup'/block_root.name,
|
||||
timeout_seconds=backup_budget, emit=emit)
|
||||
emit(stage='backup_done', status=backup.get('status'), detail=backup.get('detail'))
|
||||
# Backup is evidence only, never a prerequisite for launching PCB. Volatile
|
||||
# database/log directories are intentionally excluded to avoid file locks.
|
||||
from .icdb_ownership import database_directories, unreleased_addresses
|
||||
for database in database_directories(job['lmc_file'], job['project'], job['pcb']):
|
||||
icdb_address = prepare_icdb_address(database, root, emit=emit)
|
||||
if icdb_address.get('status') != 'ABSENT':
|
||||
raise RuntimeError(
|
||||
'ICDB_SESSION_UNCONFIRMED: 数据库存在服务器地址记录,无法确认独占使用;'
|
||||
'已保留地址文件,请先检查原 PCB/iCDB 会话。' + str(icdb_address))
|
||||
emit(stage='backup_skipped', reason='manual_backup_policy', source=str(block_root),
|
||||
detail='应用不自动备份;请在操作前手动备份。')
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
from .verify_diagnostics import collect_preflight_diagnostics, collect_native_failure_logs
|
||||
diagnostics = collect_preflight_diagnostics(job['pcb'], root)
|
||||
emit(stage='preflight_diagnostics', non_blocking=True,
|
||||
@@ -660,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()
|
||||
@@ -687,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
|
||||
@@ -701,15 +680,30 @@ def execute_job(job, emit):
|
||||
launcher_process = None
|
||||
completed = False
|
||||
identity_verified = False
|
||||
diagnostic_close_attempted = False
|
||||
helper = None
|
||||
app = doc = pi = None
|
||||
try:
|
||||
ensure_pcb_central_library(
|
||||
job['pcb'],
|
||||
job['lmc_file'],
|
||||
evidence_dir=Path(root) / 'diagnostics',
|
||||
emit=emit,
|
||||
)
|
||||
if job.get('diagnostic_only'):
|
||||
if job.get('native_lm_verify') or job.get('post_repair_consent'):
|
||||
raise ValueError('DIAGNOSTIC_MODE_INVALID: 诊断不能执行原生校验菜单或恢复确认')
|
||||
from .backup_recovery import _project_text
|
||||
from .maintenance_entry import maintenance_project_files
|
||||
library = Path(job['lmc_file']).resolve()
|
||||
for config in maintenance_project_files(job['project'], job['pcb']):
|
||||
for match in _CENTRAL_LIBRARY_KEY.finditer(_project_text(config)):
|
||||
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'],
|
||||
job['lmc_file'],
|
||||
evidence_dir=Path(root) / 'diagnostics',
|
||||
emit=emit,
|
||||
)
|
||||
emit(
|
||||
stage='verify_mode',
|
||||
mode=('library_manager_native' if bool(job.get('native_lm_verify', False))
|
||||
@@ -719,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'])
|
||||
@@ -734,6 +729,9 @@ def execute_job(job, emit):
|
||||
emit(stage='lm_session_ready', lm_pid=lm_pid, block=job['block_name'])
|
||||
else:
|
||||
command = build_pcb_launch_command(job['pcb'], job.get('mentor_bin'))
|
||||
if job.get('post_repair_consent') is not None:
|
||||
from .post_repair_consent import prepare_launch
|
||||
prepare_launch(job, command)
|
||||
emit(stage='pcb_launch', command=list(command), strategy='reusable_circuit_direct')
|
||||
launcher_process = launch_pcb_process(command, Path(job['pcb']).parent)
|
||||
worker_parent_pid = int(launcher_process.pid)
|
||||
@@ -757,18 +755,50 @@ 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,
|
||||
)
|
||||
if job.get('diagnostic_only'):
|
||||
# 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 启动检查通过;工程及启动器已退出。'}
|
||||
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;
|
||||
@@ -798,6 +828,8 @@ def execute_job(job, emit):
|
||||
emit(stage='save_helper', helper_pid=helper.pid)
|
||||
last_markers = ()
|
||||
while helper.poll() is None:
|
||||
if job.get('cancel_signal') and Path(job['cancel_signal']).exists():
|
||||
raise RuntimeError('VERIFY_CANCELLED: 已停止等待保存客户端')
|
||||
try:
|
||||
current = tuple(line.strip() for line in (root/'save.log').read_text(encoding='utf-8', errors='replace').splitlines() if line.strip())
|
||||
except OSError:
|
||||
@@ -834,7 +866,7 @@ def execute_job(job, emit):
|
||||
raise RuntimeError(f'PCB_RELEASE_UNCONFIRMED: 未确认 PCB 退出: {released}')
|
||||
addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])
|
||||
if addresses:
|
||||
raise RuntimeError(f'ICDB_RELEASE_UNCONFIRMED: PCB 已退出但数据库地址记录仍存在;保留文件并停止批次: {addresses}')
|
||||
emit(stage='icdb_address_remaining', addresses=addresses, non_blocking=True)
|
||||
cert_pcb = block_root/'Cert'/Path(job['pcb']).relative_to(block_root/'Work')
|
||||
cert_evidence = collect_cert_evidence(block_root, cert_project, cert_pcb)
|
||||
emit(stage='cert_diagnostics', **cert_evidence)
|
||||
@@ -855,6 +887,10 @@ def execute_job(job, emit):
|
||||
pass
|
||||
if helper is not None and helper.poll() is None:
|
||||
emit(stage='save_helper_retained', helper_pid=helper.pid)
|
||||
elif diagnostic_close_attempted:
|
||||
# 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:
|
||||
from .failed_session import close_failed_document
|
||||
@@ -863,6 +899,15 @@ def execute_job(job, emit):
|
||||
lm_session.close()
|
||||
except Exception as error:
|
||||
emit(stage='failure_close_unconfirmed', detail=str(error))
|
||||
elif not completed and worker_pid is not None:
|
||||
try:
|
||||
from .failed_session import close_failed_startup
|
||||
failed_close = close_failed_startup(job, worker_pid, baseline_pids=pcb_before, emit=emit,
|
||||
launcher_process=launcher_process)
|
||||
if failed_close.get('status') == 'CLOSED' and lm_session is not None:
|
||||
lm_session.close()
|
||||
except Exception as error:
|
||||
emit(stage='startup_close_unconfirmed', detail=str(error))
|
||||
cleanup_verify_processes(
|
||||
lm_pid,
|
||||
worker_pid,
|
||||
@@ -904,6 +949,14 @@ def cleanup_verify_processes(lm_pid, worker_pid, worker_parent_pid=None, *,
|
||||
if (expected_pcb and int(pid) not in baseline
|
||||
and is_reusable_circuit_pcb(executable, command, expected_pcb)):
|
||||
tracked.add(int(pid))
|
||||
# Native launcher children can omit the PCB argument. Track only new
|
||||
# descendants of this task's already identified native processes.
|
||||
while True:
|
||||
children = {int(pid) for pid, _, _, parent in pcb_records
|
||||
if int(pid) not in baseline and int(parent) in tracked}
|
||||
if children.issubset(tracked):
|
||||
break
|
||||
tracked.update(children)
|
||||
alive = {int(record[0]) for record in (*pcb_records, *lm_records)}
|
||||
remaining = sorted(tracked & alive)
|
||||
result = {'safe_to_continue': not remaining, 'remaining_pids': remaining}
|
||||
@@ -949,14 +1002,19 @@ def worker_main(request_path):
|
||||
try:
|
||||
if job.get('supervisor_pid'):
|
||||
from .desktop_worker import start_supervisor_watchdog
|
||||
start_supervisor_watchdog(job['supervisor_pid'], cancel_path=job.get('cancel_signal'))
|
||||
start_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()
|
||||
@@ -1402,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():
|
||||
@@ -1409,15 +1469,14 @@ 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
|
||||
desktop = None
|
||||
|
||||
handle = None
|
||||
pid = None
|
||||
handled = set()
|
||||
log = log_path or Path(library).parent/'LogFiles'/'cbb_flag_read_dialogs.jsonl'
|
||||
try:
|
||||
desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None)
|
||||
startup = win32process.STARTUPINFO()
|
||||
startup.lpDesktop = 'winsta0\\' + desktop_name
|
||||
handle, thread, pid, _ = win32process.CreateProcess(
|
||||
@@ -1461,7 +1520,8 @@ def read_library_flag(library, name, *, timeout_seconds=90, log_path=None):
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
desktop.CloseDesktop()
|
||||
if desktop is not None:
|
||||
desktop.CloseDesktop()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@@ -1490,12 +1550,13 @@ def read_library_tree_image_map(library, names, *, timeout_seconds=45, log_path=
|
||||
if not executable.is_file():
|
||||
return result
|
||||
desktop_name = 'CbbStatusRead_' + os.urandom(5).hex()
|
||||
desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None)
|
||||
desktop = None
|
||||
handle = None
|
||||
pid = None
|
||||
handled = set()
|
||||
log = log_path or Path(library).parent/'LogFiles'/'cbb_status_read_dialogs.jsonl'
|
||||
try:
|
||||
desktop = win32service.CreateDesktop(desktop_name, 0, win32con.GENERIC_ALL, None)
|
||||
startup = win32process.STARTUPINFO()
|
||||
startup.lpDesktop = 'winsta0\\' + desktop_name
|
||||
handle, thread, pid, _ = win32process.CreateProcess(
|
||||
@@ -1549,7 +1610,8 @@ def read_library_tree_image_map(library, names, *, timeout_seconds=45, log_path=
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
desktop.CloseDesktop()
|
||||
if desktop is not None:
|
||||
desktop.CloseDesktop()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
"""Observed native copy failure and conservative legacy Win32 path admission."""
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import time
|
||||
|
||||
_COPY_ERROR = b'Error! Cannot copy iCDB base of Reusable Block project.'
|
||||
_LOG_LIMIT = 2*1024*1024
|
||||
|
||||
|
||||
def missing_other_pcb_notice(library, target, title, text, buttons):
|
||||
"""Only the observed editor-open information about unrelated missing PCBs."""
|
||||
from .management import validate_name
|
||||
from .verify_runner import preflight_skip_reason
|
||||
heading = ('The following Reusable Blocks do not contain PCB files and will not appear '
|
||||
'in the list of available reusable blocks:')
|
||||
if title != 'Library Manager' or len(text) > 131072 or len(buttons) != 1:
|
||||
return None
|
||||
button = buttons[0]
|
||||
if (button.get('id') != 2 or button.get('class') != 'Button'
|
||||
or button.get('text') not in ('OK', '确定') or button.get('enabled') is not True):
|
||||
return None
|
||||
lines = text.strip().splitlines()
|
||||
if not lines or lines[0] != heading:
|
||||
return None
|
||||
names = []
|
||||
for line in lines[1:]:
|
||||
if not line.strip(): continue
|
||||
if not line.startswith('\t'): return None
|
||||
name = line[1:]
|
||||
try: validate_name(name)
|
||||
except ValueError: return None
|
||||
if name.casefold() == str(target).casefold() or not preflight_skip_reason(library, name):
|
||||
return None
|
||||
names.append(name)
|
||||
if not names or len(names) != len({name.casefold() for name in names}):
|
||||
return None
|
||||
return {'names': names, 'button': button['hwnd']}
|
||||
|
||||
|
||||
def snapshot_add_log(library):
|
||||
path = Path(library).parent/'LogFiles/ReuseBlockLogLO.txt'
|
||||
try:
|
||||
data = b''
|
||||
if path.exists():
|
||||
with path.open('rb') as stream: data = stream.read(_LOG_LIMIT+1)
|
||||
if len(data) > _LOG_LIMIT: raise ValueError('Native Add log exceeds the bounded observation limit.')
|
||||
return {'path': str(path), 'size': len(data), 'sha256': hashlib.sha256(data).hexdigest()}
|
||||
except (OSError, ValueError) as error:
|
||||
return {'path': str(path), 'unavailable': str(error)}
|
||||
|
||||
|
||||
def fresh_add_failure(baseline, name):
|
||||
if 'unavailable' in baseline: return None
|
||||
try:
|
||||
with Path(baseline['path']).open('rb') as stream:
|
||||
data = stream.read(_LOG_LIMIT+1)
|
||||
offset = baseline['size']
|
||||
if (len(data) > _LOG_LIMIT or len(data) < offset
|
||||
or hashlib.sha256(data[:offset]).hexdigest() != baseline['sha256']):
|
||||
return None
|
||||
marker = re.compile(rb'REUSE BLOCK:\s*INFORMATION:\s*'+re.escape(name.encode('mbcs'))
|
||||
+rb'\s+'+re.escape(_COPY_ERROR))
|
||||
for line in data[offset:].splitlines():
|
||||
if marker.search(line): return line.decode('mbcs')
|
||||
except (OSError, UnicodeError):
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def failure_ack_button(title, static_text, buttons):
|
||||
if title != 'Library Manager' or static_text.strip() != '系统找不到指定的路径。' or len(buttons) != 1:
|
||||
return None
|
||||
button = buttons[0]
|
||||
if (button.get('id') == 2 and button.get('class') == 'Button'
|
||||
and button.get('text') == '确定' and button.get('enabled') is True):
|
||||
return button['hwnd']
|
||||
return None
|
||||
|
||||
|
||||
def validate_native_copy_paths(source, library, name):
|
||||
from .scoped_manifest import scan_tree
|
||||
from .network_path import absolute_preserving_drive
|
||||
source = absolute_preserving_drive(source)
|
||||
target = absolute_preserving_drive(library).parent/'ReusableBlocksLP'/name/'Work'
|
||||
paths = [source.parent, *scan_tree(source.parent)]
|
||||
for path in paths:
|
||||
relative = path.relative_to(source.parent)
|
||||
for label, candidate in (('暂存来源', path), ('目标', target/relative)):
|
||||
# Legacy CopyFile's 260-WCHAR buffer includes the terminating NUL.
|
||||
length = len(str(candidate).encode('utf-16-le'))//2
|
||||
if length >= 260:
|
||||
raise ValueError(f'NATIVE_PATH_TOO_LONG: {label}路径为 {length} 个字符,已超过原生复制支持范围。'
|
||||
f'请缩短操作数据/证据目录或目标库路径后重试;所有工程和日志文件均保留。路径:{candidate}')
|
||||
|
||||
|
||||
def observe_add_failure(desktop, owned_pids, root, baseline, name, state):
|
||||
"""Record owned modal evidence and acknowledge only the observed error box."""
|
||||
import pythoncom, win32con, win32gui, win32process
|
||||
from .management import _write_json
|
||||
fresh_log = fresh_add_failure(baseline, name)
|
||||
for handle in desktop.EnumDesktopWindows():
|
||||
hwnd = int(handle)
|
||||
if (win32process.GetWindowThreadProcessId(hwnd)[1] not in owned_pids
|
||||
or not win32gui.IsWindowVisible(hwnd) or win32gui.GetClassName(hwnd) != '#32770'):
|
||||
continue
|
||||
title = win32gui.GetWindowText(hwnd)
|
||||
if title in ('New Reusable Block', 'Reusable Blocks'): continue
|
||||
children = []
|
||||
win32gui.EnumChildWindows(hwnd, lambda child, _: children.append(int(child)), None)
|
||||
controls = [{'hwnd': child, 'id': win32gui.GetDlgCtrlID(child), 'class': win32gui.GetClassName(child),
|
||||
'text': win32gui.GetWindowText(child), 'enabled': bool(win32gui.IsWindowEnabled(child))}
|
||||
for child in children]
|
||||
static = '\n'.join(c['text'] for c in controls if c['class'] == 'Static')
|
||||
buttons = [c for c in controls if c['class'] == 'Button']
|
||||
record = {'hwnd': hwnd, 'title': title, 'controls': controls, 'fresh_log_failure': fresh_log}
|
||||
key = json.dumps(record, ensure_ascii=False, sort_keys=True)
|
||||
if key not in state.setdefault('observed', set()):
|
||||
state['observed'].add(key)
|
||||
with (Path(root)/'native-add-dialogs.jsonl').open('a', encoding='utf-8') as stream:
|
||||
stream.write(json.dumps(record, ensure_ascii=False)+'\n')
|
||||
button = failure_ack_button(title, static, buttons)
|
||||
if button is not None and win32process.GetWindowThreadProcessId(button)[1] in owned_pids:
|
||||
win32gui.PostMessage(button, win32con.BM_CLICK, 0, 0)
|
||||
deadline = time.monotonic()+2
|
||||
while win32gui.IsWindow(hwnd) and time.monotonic() < deadline:
|
||||
pythoncom.PumpWaitingMessages(); time.sleep(.05)
|
||||
record['acknowledged'] = not win32gui.IsWindow(hwnd)
|
||||
_write_json(Path(root)/'native-add-output.json', {'success_message': None, 'failure': record})
|
||||
raise RuntimeError('NATIVE_ADD_FAILED: '+static.strip()
|
||||
+ (' '+fresh_log if fresh_log else '')
|
||||
+ ('' if record['acknowledged'] else ';原生错误对话框尚未确认关闭。'))
|
||||
if fresh_log:
|
||||
first_seen = state.setdefault('failure_seen_at', time.monotonic())
|
||||
# Allow the paired native error popup to appear for normal acknowledgement.
|
||||
if time.monotonic()-first_seen >= 2:
|
||||
_write_json(Path(root)/'native-add-output.json', {'success_message': None,
|
||||
'failure': {'fresh_log_failure': fresh_log, 'acknowledged': False}})
|
||||
raise RuntimeError('NATIVE_ADD_FAILED: '+fresh_log)
|
||||
@@ -0,0 +1,155 @@
|
||||
"""Open the official backup UI; version selection and writes stay native."""
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
from pathlib import Path
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
def native_backup_executable(mentor_bin=''):
|
||||
from .mentor_paths import discover_sdd_home
|
||||
home = discover_sdd_home(mentor_bin)
|
||||
executable = home/'iCDB/win32/bin/iCDBProjectBackup.exe' if home else None
|
||||
if executable is None or not executable.is_file():
|
||||
raise ValueError('未找到 iCDB Project Backup,请检查 Mentor 安装位置。')
|
||||
return executable
|
||||
|
||||
|
||||
def native_backup_project(library, block):
|
||||
from .verify_runner import INVALID_NAME_CHARS
|
||||
library = Path(library).absolute()
|
||||
if not block or block in ('.', '..') or any(c in block for c in INVALID_NAME_CHARS):
|
||||
raise ValueError('CBB 名称非法。')
|
||||
if not library.is_file():
|
||||
raise ValueError('中心库文件不存在。')
|
||||
work = library.parent/'ReusableBlocksLP'/block/'Work'
|
||||
expected = library.parent.resolve()/'ReusableBlocksLP'/block/'Work'
|
||||
if work.resolve() != expected:
|
||||
raise ValueError('工程路径通过链接指向其他位置。')
|
||||
projects = list(work.glob('*.prj'))
|
||||
if len(projects) != 1 or not projects[0].is_file():
|
||||
raise ValueError('无法唯一确定所选 CBB 的工程 PRJ。')
|
||||
project = projects[0]
|
||||
if (project.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT
|
||||
or not project.resolve().is_relative_to(expected)):
|
||||
raise ValueError('工程文件通过链接指向其他位置。')
|
||||
return project
|
||||
|
||||
|
||||
@dataclass
|
||||
class NativeBackupSession:
|
||||
process: object
|
||||
job: Path
|
||||
library: Path
|
||||
block: str
|
||||
path_lease: object = None
|
||||
|
||||
|
||||
def launch_native_backup(request):
|
||||
project = native_backup_project(request.lmc_file, request.block_name)
|
||||
native_backup_executable(request.mentor_bin)
|
||||
root = Path(request.evidence_dir).resolve()/('native-backup-'+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):
|
||||
raise ValueError('运行记录目录需位于中心库之外。')
|
||||
root.mkdir(parents=True)
|
||||
job = root/'request.json'
|
||||
job.write_text(json.dumps({'library': str(request.lmc_file), 'block': request.block_name,
|
||||
'project': str(project), 'mentor_bin': request.mentor_bin}, ensure_ascii=False), 'utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify'])+['--native-backup-worker', str(job)]
|
||||
process = subprocess.Popen(command, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
return NativeBackupSession(process, job, Path(request.lmc_file), request.block_name)
|
||||
|
||||
|
||||
class _NativeBackupProcess:
|
||||
"""Track this visible tool and its descendants without killing on close."""
|
||||
def __init__(self, command, cwd):
|
||||
import win32con, win32job, win32process
|
||||
self.job = win32job.CreateJobObject(None, '')
|
||||
startup = win32process.STARTUPINFO()
|
||||
try:
|
||||
self.handle, thread, self.pid, _ = win32process.CreateProcess(
|
||||
command[0], subprocess.list2cmdline(command), None, None, False,
|
||||
win32con.CREATE_SUSPENDED, None, str(cwd), startup)
|
||||
except Exception:
|
||||
self.job.Close()
|
||||
raise
|
||||
try:
|
||||
win32job.AssignProcessToJobObject(self.job, self.handle)
|
||||
win32process.ResumeThread(thread)
|
||||
except Exception:
|
||||
# No native code has run while suspended; there can be no DB write.
|
||||
win32process.TerminateProcess(self.handle, 1)
|
||||
self.handle.Close(); self.job.Close()
|
||||
raise
|
||||
finally:
|
||||
thread.Close()
|
||||
|
||||
def poll(self):
|
||||
import win32process
|
||||
code = win32process.GetExitCodeProcess(self.handle)
|
||||
return None if code == 259 else code
|
||||
|
||||
def session_pids(self):
|
||||
import win32job
|
||||
return win32job.QueryInformationJobObject(self.job, win32job.JobObjectBasicProcessIdList)
|
||||
|
||||
def close(self):
|
||||
self.handle.Close(); self.job.Close()
|
||||
|
||||
|
||||
def native_backup_worker_main(job, *, process_factory=None):
|
||||
from .library_lock import LibraryLock
|
||||
from .management import _write_json
|
||||
job = Path(job)
|
||||
result = {'status': 'FAILED', 'safe_to_continue': True, 'restore_confirmed': False}
|
||||
native = None
|
||||
def write_evidence(name, value):
|
||||
try:
|
||||
_write_json(job.with_name(name), value)
|
||||
except Exception as error:
|
||||
# Evidence storage must never end supervision of a live transaction.
|
||||
result['evidence_error'] = str(error)
|
||||
try:
|
||||
record = json.loads(job.read_text('utf-8'))
|
||||
library, block = Path(record['library']), record['block']
|
||||
result.update(library=str(library), block=block)
|
||||
project = native_backup_project(library, block)
|
||||
if project != Path(record['project']):
|
||||
raise ValueError('工程身份已变化,未打开原生工具。')
|
||||
executable = native_backup_executable(record.get('mentor_bin', ''))
|
||||
released = True
|
||||
with LibraryLock(library, checkpoint=False, safe_to_release=lambda: released):
|
||||
native = (process_factory or _NativeBackupProcess)([str(executable), '-p', str(project)], project.parent)
|
||||
released = False
|
||||
result.update(status='OPEN', native_pid=native.pid, project=str(project), safe_to_continue=False)
|
||||
write_evidence('state.json', result)
|
||||
waiting = False
|
||||
while True:
|
||||
try:
|
||||
pids, exit_code = native.session_pids(), native.poll()
|
||||
except Exception as error:
|
||||
write_evidence('state.json', {**result,
|
||||
'status': 'RELEASE_UNCONFIRMED', 'detail': str(error)})
|
||||
time.sleep(.5)
|
||||
continue # Keep the lease while the live session cannot be observed.
|
||||
if not pids and exit_code is not None:
|
||||
break
|
||||
if exit_code is not None and not waiting:
|
||||
waiting = True
|
||||
write_evidence('state.json', {**result, 'status': 'WAITING_RELEASE'})
|
||||
time.sleep(.25)
|
||||
released = True
|
||||
result.update(status='CLOSED', native_exit_code=exit_code, safe_to_continue=True)
|
||||
return 0 if result['native_exit_code'] == 0 else 1
|
||||
except Exception as error:
|
||||
result.update(status='FAILED', detail=str(error), safe_to_continue=native is None)
|
||||
return 1
|
||||
finally:
|
||||
write_evidence('result.json', result)
|
||||
if native is not None:
|
||||
native.close() # No kill-on-close: never interrupt a native transaction.
|
||||
@@ -0,0 +1,476 @@
|
||||
"""Pinned EE7.9.5 native logical exports plus conservative physical/assets proof.
|
||||
|
||||
This contract is not raw iCDB equivalence or a claim of exhaustive arbitrary CES
|
||||
semantics. It compares the proven hierarchy/flat ASCII and CSV/property exports,
|
||||
and preserves physical design and all other primary engineering assets exactly.
|
||||
"""
|
||||
from dataclasses import dataclass
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
from .management import source_project, _write_json
|
||||
from .scoped_manifest import ManifestScope, build_manifest, hash_file
|
||||
|
||||
POLICY = 'mentor795-native-content-v1'
|
||||
COVERAGE = {'logical_connectivity_and_properties': 'native hierarchy/flat ASCII and CSV',
|
||||
'physical_design_and_primary_assets': 'raw SHA256',
|
||||
'schematic_graphical_geometry': 'native EDIF schematic', 'production_acceptance': True}
|
||||
EXPORTER_PROOF = {
|
||||
'icdb2ascii.exe': 'e37c87356eaf57c0b97e4183458bc8c5eb6eb0ac497cc7ad40dcd585d8cd213b',
|
||||
'icdb2csv.exe': '22059f4a432e8eabf30a5326ebcbd463ed6fa6495af21d7aad5ab5d2f892f255',
|
||||
'edifExporter.exe': '8b5f9682007da5b759e7b2f0fa9720f6771b2efb9a546636b270a58e1268a80e',
|
||||
}
|
||||
INF_HEADER = (b'Design Name\tSystem Name\tSystem Version\tSystem_Date\tCompany_Name\tDepartment_Name'
|
||||
b'\tAddress\tAddress1\tCity\tState_Province\tCountry\tZip\tPhone\tPhone_Ext\tFax\tFax_Ext')
|
||||
CSV_SUFFIXES = ('', '.esf', '.inf', '.npr', '.ppn', '.ppr', '.prm', '.prt', '.pth',
|
||||
'.sht', '.spn', '.spr', '.sym', '.thn')
|
||||
_retained_exports = []
|
||||
_OWNER_NAMES = frozenset({'icdb2ascii.exe', 'icdb2csv.exe', 'edifexporter.exe', 'icdbnetserver.exe',
|
||||
'icdbnetlauncher.exe', 'icdbservermanager.exe', 'icdbservermonitor.exe'})
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class IdentityOutcome:
|
||||
identity: dict | None
|
||||
safe_to_continue: bool
|
||||
detail: str
|
||||
evidence_path: Path
|
||||
|
||||
|
||||
def _digest(data):
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
|
||||
def normalize_ascii(data, top):
|
||||
"""Only the three observed, position-bound report metadata values vary."""
|
||||
lines = data.splitlines(keepends=True)
|
||||
prefixes = {0: b'iCDB Ascii Report Generation Date: ',
|
||||
4: b'\t iCDB directory: ', 5: b'\t DATE: '}
|
||||
if (len(lines) < 12 or lines[3].rstrip(b'\r\n') != b'\tReport Criteria:'
|
||||
or any(not lines[i].startswith(prefix) or not lines[i][len(prefix):].strip()
|
||||
for i, prefix in prefixes.items())):
|
||||
raise ValueError('Incomplete or unknown native ASCII report header.')
|
||||
header = b''.join(lines[:next((i for i, line in enumerate(lines) if line.startswith(b'Block ')), len(lines))])
|
||||
required = (b'\t REPORT FORMAT: FREEFORM', b'\t MODE: HIER AND FLAT BLOCKS',
|
||||
b'\t CONNECTION LISTS: ON', b'\t TEXT LISTS: ON')
|
||||
if any(header.count(value) != 1 for value in required):
|
||||
raise ValueError('Native ASCII export mode does not match the proven contract.')
|
||||
top_bytes = top.encode('mbcs')
|
||||
if (not any(line.rstrip(b'\r\n') == b'Block '+top_bytes+b' \tRoot' for line in lines)
|
||||
or not any(line.startswith(b'Config ') for line in lines)):
|
||||
raise ValueError('Native ASCII export lacks the exact root or flat configuration.')
|
||||
for index, prefix in prefixes.items():
|
||||
ending = b'\r\n' if lines[index].endswith(b'\r\n') else b'\n'
|
||||
lines[index] = prefix+b'<REPORT_METADATA>'+ending
|
||||
return b''.join(lines)
|
||||
|
||||
|
||||
def normalize_inf(data):
|
||||
lines = data.splitlines(keepends=True)
|
||||
if len(lines) != 2 or lines[0].rstrip(b'\r\n') != INF_HEADER:
|
||||
raise ValueError('Unknown native CSV information schema.')
|
||||
values = lines[1].rstrip(b'\r\n').split(b'\t')
|
||||
if len(values) != 16 or values[:3] != [b'identity', b'Design Capture', b'VB99.0']:
|
||||
raise ValueError('Malformed native CSV information row.')
|
||||
if not re.fullmatch(rb'\d{2}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}', values[3]):
|
||||
raise ValueError('Unknown native CSV System_Date format.')
|
||||
values[3] = b'<SYSTEM_DATE>'
|
||||
ending = b'\r\n' if lines[1].endswith(b'\r\n') else b'\n'
|
||||
return lines[0]+b'\t'.join(values)+ending
|
||||
|
||||
|
||||
def normalize_edif(data, top):
|
||||
"""Bounded structural parse; replace only root/status/written/timestamp."""
|
||||
from datetime import datetime
|
||||
if not data or len(data) > 64*1024*1024:
|
||||
raise ValueError('EDIF report is empty or exceeds the supported parsing limit.')
|
||||
token_pattern = re.compile(rb'\s+|[()]|"(?:[^"\\]|\\.)*"|[^\s()"]+')
|
||||
stack, roots, nodes = [], [], []
|
||||
position = 0
|
||||
for match in token_pattern.finditer(data):
|
||||
if match.start() != position: raise ValueError('Malformed EDIF token.')
|
||||
position = match.end(); token = match.group()
|
||||
if token.isspace(): continue
|
||||
if token == b'(':
|
||||
node = {'start': match.start(), 'items': []}
|
||||
if stack: stack[-1]['items'].append(node)
|
||||
else: roots.append(node)
|
||||
nodes.append(node); stack.append(node)
|
||||
if len(stack) > 256 or len(nodes) > 1000000:
|
||||
raise ValueError('EDIF exceeds supported structural limits.')
|
||||
elif token == b')':
|
||||
if not stack: raise ValueError('Unbalanced EDIF report.')
|
||||
node = stack.pop(); node['end'] = match.end()
|
||||
if not node['items'] or not isinstance(node['items'][0], bytes):
|
||||
raise ValueError('EDIF node has no keyword.')
|
||||
else:
|
||||
if not stack: raise ValueError('Unexpected EDIF content outside the root.')
|
||||
stack[-1]['items'].append(token)
|
||||
if position != len(data) or stack or len(roots) != 1 or roots[0]['items'][0] != b'edif':
|
||||
raise ValueError('Unbalanced or unsupported EDIF root.')
|
||||
def children(node, tag):
|
||||
return [n for n in node['items'] if isinstance(n, dict) and n['items'][0] == tag]
|
||||
def one(node, tag):
|
||||
matches = children(node, tag)
|
||||
if len(matches) != 1: raise ValueError(f'EDIF requires one {tag!r} node in the expected context.')
|
||||
return matches[0]
|
||||
root = roots[0]
|
||||
if one(root, b'edifVersion')['items'] != [b'edifVersion', b'2', b'0', b'0']:
|
||||
raise ValueError('Unsupported EDIF version.')
|
||||
written = one(one(root, b'status'), b'written')
|
||||
if one(written, b'program')['items'][1] != b'"DxDesigner Edif Exporter"':
|
||||
raise ValueError('Unrecognized native EDIF exporter.')
|
||||
stamp = one(written, b'timestamp')
|
||||
values = stamp['items'][1:]
|
||||
if len(values) != 6 or any(not isinstance(v, bytes) or not v.isdigit() for v in values):
|
||||
raise ValueError('Malformed EDIF header timestamp.')
|
||||
datetime(*(int(v) for v in values))
|
||||
design = one(root, b'design')
|
||||
top_bytes = top.encode('mbcs')
|
||||
if re.fullmatch(rb'[A-Za-z][A-Za-z0-9_]*', top_bytes):
|
||||
expected_identifier = top_bytes
|
||||
elif re.fullmatch(rb'[0-9][A-Za-z0-9_]*', top_bytes):
|
||||
# The installed exporter encodes digit-initial EDIF identifiers with
|
||||
# '&' (observed cellRef &1GPHY_8212). Retain all report bytes; this is
|
||||
# an exact identity check, not general name stripping or normalization.
|
||||
expected_identifier = b'&' + top_bytes
|
||||
else:
|
||||
raise ValueError('Unsupported native EDIF RootBlock identifier representation.')
|
||||
if one(design, b'cellRef')['items'][1] != expected_identifier:
|
||||
raise ValueError('EDIF top design differs from the requested RootBlock.')
|
||||
if (not any(n['items'] == [b'viewType', b'SCHEMATIC'] for n in nodes)
|
||||
or any(not any(n['items'][0] == tag for n in nodes) for tag in (b'page', b'figure', b'pt'))):
|
||||
raise ValueError('EDIF schematic drawing is absent or unsupported.')
|
||||
return data[:stamp['start']]+b'(timestamp <EXPORT_TIME>)'+data[stamp['end']:]
|
||||
|
||||
|
||||
def csv_identity(root):
|
||||
from .scoped_manifest import scan_tree
|
||||
root = Path(root)
|
||||
paths = {p.relative_to(root).as_posix(): p for p in scan_tree(root) if p.is_file()}
|
||||
expected = {'work/identity'+suffix for suffix in CSV_SUFFIXES}
|
||||
if paths.keys() != expected or any(not p.stat().st_size for p in paths.values()):
|
||||
raise ValueError('Native CSV export is empty, incomplete or an unsupported snapshot.')
|
||||
index = paths['work/identity'].read_bytes().splitlines()
|
||||
expected_index = {b'file:identity'+suffix.encode('ascii') for suffix in CSV_SUFFIXES if suffix}
|
||||
if len(index) != len(expected_index) or set(index) != expected_index:
|
||||
raise ValueError('Native CSV index does not bind every exported table.')
|
||||
return {name: _digest(normalize_inf(path.read_bytes()) if name.endswith('.inf') else path.read_bytes())
|
||||
for name, path in sorted(paths.items())}
|
||||
|
||||
|
||||
def _project_encoding(data):
|
||||
if data.startswith((b'\xff\xfe', b'\xfe\xff')): return 'utf-16'
|
||||
if data.startswith(b'\xef\xbb\xbf'): return 'utf-8-sig'
|
||||
try: data.decode('utf-8'); return 'utf-8'
|
||||
except UnicodeDecodeError: return 'mbcs'
|
||||
|
||||
|
||||
def project_library(source):
|
||||
"""Require one existing approved source library across every project ref."""
|
||||
from .backup_recovery import _project_text
|
||||
source, pcb, _ = source_project(source)
|
||||
from .maintenance_entry import maintenance_project_files
|
||||
libraries = set()
|
||||
for path in maintenance_project_files(source, pcb):
|
||||
values = re.findall(r'^KEY CentralLibrary "([^"\r\n]+)"', _project_text(path), re.M)
|
||||
if not values: raise ValueError('Project lacks CentralLibrary references.')
|
||||
for value in values:
|
||||
target = (path.parent/value).resolve(strict=True)
|
||||
if target.suffix.casefold() != '.lmc' or not target.is_file():
|
||||
raise ValueError('CentralLibrary is not an existing LMC.')
|
||||
libraries.add(target)
|
||||
if len(libraries) != 1: raise ValueError('Project CentralLibrary references disagree.')
|
||||
return libraries.pop()
|
||||
|
||||
|
||||
def _normalized_project(path, expected_library):
|
||||
data = path.read_bytes(); encoding = _project_encoding(data)
|
||||
text = data.decode(encoding)
|
||||
refs = re.findall(r'^KEY CentralLibrary "([^"\r\n]*)"', text, re.M)
|
||||
if not refs or any((path.parent/value).resolve() != expected_library.resolve() for value in refs):
|
||||
raise ValueError(f'CentralLibrary reference does not resolve to the approved library: {path}')
|
||||
return re.sub(r'(?m)^(KEY CentralLibrary ")[^"\r\n]*(")',
|
||||
r'\1<CENTRAL_LIBRARY>\2', text).encode(encoding)
|
||||
|
||||
|
||||
def asset_identity(source, expected_library):
|
||||
from .backup_recovery import _project_text
|
||||
source, pcb, top = source_project(source)
|
||||
expected_library = Path(expected_library)
|
||||
if not expected_library.is_file(): raise ValueError('Approved CentralLibrary is missing.')
|
||||
root = source.parent
|
||||
from .maintenance_entry import maintenance_project_files
|
||||
projects = maintenance_project_files(source, pcb)
|
||||
databases = []
|
||||
for project in projects:
|
||||
values = re.findall(r'^KEY iCDBDir "([^"\r\n]+)"', _project_text(project), re.M)
|
||||
databases.extend((project.parent/value).resolve() for value in values)
|
||||
main_database = databases[0]
|
||||
runtime = [db/'cdbsvr' for db in databases]
|
||||
files = build_manifest(root, scope=ManifestScope())['files']
|
||||
assets = {}
|
||||
for relative, record in files.items():
|
||||
path = root/relative
|
||||
known_runtime = False
|
||||
for server_root in runtime:
|
||||
if not path.is_relative_to(server_root): continue
|
||||
parts = path.relative_to(server_root).parts
|
||||
folded = tuple(p.casefold() for p in parts)
|
||||
known_runtime = (len(parts) > 1 and folded[0] in ('log', 'dbcheck', 'dbrestore', 'dbupgrade', 'pimlog')
|
||||
or folded in (('events', 'events.dat'), ('events', 'events.idx'))
|
||||
or len(parts) == 2 and folded[0] == 'plog'
|
||||
and re.fullmatch(r'\d{4}\.\d{2}\.\d{2}\.guid\.\d+\.txt', parts[1]) is not None)
|
||||
break
|
||||
if path == main_database/'icdb.dat' or known_runtime:
|
||||
continue
|
||||
if path in projects:
|
||||
data = _normalized_project(path, expected_library)
|
||||
assets[relative] = {'size': len(data), 'sha256': _digest(data)}
|
||||
else:
|
||||
assets[relative] = {'size': record['size'], 'sha256': record['sha256']}
|
||||
return {'policy': POLICY, 'top_level_block': top,
|
||||
'layout_sha256': hash_file(pcb.parent/'Layout/LayoutDB.lyt'), 'assets': assets}
|
||||
|
||||
|
||||
def check_exporters(mentor_home):
|
||||
for name, expected in EXPORTER_PROOF.items():
|
||||
path = Path(mentor_home)/('wv/win32/bin' if name == 'edifExporter.exe' else 'iCDB/win32/bin')/name
|
||||
if not path.is_file() or hash_file(path) != expected:
|
||||
raise ValueError(f'Native identity exporter is not the proven EE7.9.5 binary: {path}')
|
||||
|
||||
|
||||
def validate_export_output(kind, code, report, log):
|
||||
text = Path(log).read_bytes().decode('mbcs', errors='strict')
|
||||
if code != 0 or re.search(r'(?im)^\s*(?:#\s*)?(?:error|fatal)\b', text):
|
||||
raise ValueError('Native identity exporter failed or reported an error.')
|
||||
if kind == 'ascii':
|
||||
if (not Path(report).is_file() or not Path(report).stat().st_size
|
||||
or f'Report generation complete, report file is {report}' not in text):
|
||||
raise ValueError('Native ASCII export lacks a complete nonempty report.')
|
||||
elif kind == 'csv':
|
||||
if 'iCDB to CSV complete' not in [line.strip() for line in text.splitlines()]:
|
||||
raise ValueError('Native CSV export lacks its completion marker.')
|
||||
csv_identity(report)
|
||||
elif kind == 'edif':
|
||||
if (not Path(report).is_file() or not Path(report).stat().st_size
|
||||
or 'Done' not in [line.strip() for line in text.splitlines()]
|
||||
or f'Dumping edif file {report} ...' not in text):
|
||||
raise ValueError('Native EDIF export lacks a complete nonempty drawing report.')
|
||||
else:
|
||||
raise ValueError('Unsupported native export kind.')
|
||||
|
||||
|
||||
def _process_snapshot():
|
||||
from .owner_probe import query_process_snapshot
|
||||
return query_process_snapshot(timeout_seconds=10)
|
||||
|
||||
|
||||
def _database_released(project_root):
|
||||
"""Exclusive reads plus absent addresses prove the copied native DBs idle."""
|
||||
import win32file, win32con
|
||||
from contextlib import ExitStack
|
||||
root = Path(project_root)
|
||||
if any(root.rglob('sAddress.adr')):
|
||||
return False
|
||||
with ExitStack() as stack:
|
||||
for path in root.rglob('*'):
|
||||
if path.is_file() and path.name.casefold() in ('icdb.dat', 'layoutdb.lyt'):
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, 0, None)
|
||||
stack.callback(handle.Close)
|
||||
return True
|
||||
|
||||
|
||||
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)
|
||||
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.'}
|
||||
from .backup_recovery import _project_text
|
||||
_, _, top = source_project(project)
|
||||
database = (project.parent/re.findall(r'^KEY iCDBDir "([^"\r\n]+)"', _project_text(project), re.M)[0]).resolve()
|
||||
commands = [
|
||||
('ascii', [str(Path(mentor_home)/'iCDB/win32/bin/icdb2ascii.exe'),
|
||||
f'-project={project}', f'-file={root/"design.rpt"}', f'-clog={root/"ascii.log"}',
|
||||
'-f', '-h', '-c', '-t', '-nostrip'], root/'design.rpt', root/'ascii.log'),
|
||||
('csv', [str(Path(mentor_home)/'iCDB/win32/bin/icdb2csv.exe'),
|
||||
f'-project={project}', '-snap=DxD', '-file=identity', f'-output={root/"tables"}',
|
||||
f'-clog={root/"csv.log"}', '-properties', '-nostrip'], root/'tables', root/'csv.log'),
|
||||
('edif', [str(Path(mentor_home)/'wv/win32/bin/edifExporter.exe'), '-schematic',
|
||||
'-icdb', str(database), '-isnap', 'DxD', '-top', top, '-o', str(root/'design.edf')],
|
||||
root/'design.edf', root/'edif-stdout.txt'),
|
||||
]
|
||||
for kind, command, report, log in commands:
|
||||
process = None
|
||||
handles = {}
|
||||
identities = {}
|
||||
released = 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)
|
||||
while time.monotonic() < deadline:
|
||||
snapshot = _process_snapshot()
|
||||
changed = True
|
||||
while changed:
|
||||
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 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)
|
||||
if released: break
|
||||
time.sleep(.1)
|
||||
record.update(exit_code=process.poll(), safe_to_continue=released)
|
||||
if not released:
|
||||
record['detail'] = 'TIMEOUT: native export owner or copied database release remains unconfirmed.'
|
||||
return record
|
||||
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:
|
||||
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()
|
||||
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,
|
||||
timeout_seconds=180):
|
||||
"""Capture on a fresh project copy while an existing transaction owns its library."""
|
||||
from .backup_recovery import _align_library
|
||||
from .library_lock import LibraryLock, owned_snapshot
|
||||
from .scoped_manifest import copy_verified
|
||||
from .native_export_guard import begin_export_analysis, finish_export_analysis
|
||||
import win32event, win32con
|
||||
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)
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired: raise RuntimeError('Another CBB operation owns the management mutex.')
|
||||
with LibraryLock(library, safe_to_release=lambda: owner_safe):
|
||||
assets = asset_identity(source, Path(expected_library))
|
||||
original = copy_verified(source.parent, root/'project')
|
||||
clone = root/'project'/source.name
|
||||
_, clone_pcb, _ = source_project(clone)
|
||||
_align_library(clone, clone_pcb, Path(expected_library))
|
||||
if any((root/'project').rglob('sAddress.adr')):
|
||||
raise ValueError('Analysis source contains an unresolved database address.')
|
||||
export_token = begin_export_analysis(library, root, clone)
|
||||
owner_safe = False
|
||||
native = _run_exports(clone, root, mentor_home, timeout_seconds)
|
||||
if native.get('safe_to_continue') is True:
|
||||
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:
|
||||
ascii_bytes = normalize_ascii((root/'design.rpt').read_bytes(), top)
|
||||
csv = csv_identity(root/'tables')
|
||||
edif_bytes = normalize_edif((root/'design.edf').read_bytes(), top)
|
||||
if build_manifest(source.parent) != original:
|
||||
raise ValueError('Identity input changed while exporting its disposable copy.')
|
||||
fingerprint = {**assets, 'coverage': COVERAGE, 'ascii_sha256': _digest(ascii_bytes),
|
||||
'csv': csv, 'edif_sha256': _digest(edif_bytes)}
|
||||
outcome = IdentityOutcome(fingerprint, True, 'Native logical exports and primary assets captured.', root)
|
||||
except Exception as error:
|
||||
outcome = IdentityOutcome(None, owner_safe, str(error), root)
|
||||
finally:
|
||||
if acquired: win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
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
|
||||
@@ -0,0 +1,158 @@
|
||||
"""Export-copy ownership bound to the existing per-library lease checkpoint."""
|
||||
from contextlib import contextmanager, ExitStack
|
||||
import json
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from .management import _write_json
|
||||
|
||||
|
||||
def assert_retained_exports_idle(library=None):
|
||||
import win32con, win32event
|
||||
from .native_engineering_identity import _retained_exports
|
||||
from .library_lock import LibraryBusy
|
||||
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.')
|
||||
|
||||
|
||||
def begin_export_analysis(library, root, project):
|
||||
from .library_lock import _guard, blocker_path, checkpoint_record, LibraryBusy
|
||||
from .management_transaction import current_lease
|
||||
library, root, project = Path(library).resolve(), Path(root).resolve(), Path(project).resolve()
|
||||
owner = current_lease(library)
|
||||
if (project.parent != root/'project' or not project.is_file()
|
||||
or root.is_relative_to(library.parent) or library.parent.is_relative_to(root)):
|
||||
raise LibraryBusy('Export-copy ownership paths are not independent and exact.')
|
||||
record = {'protocol': 'cbb-native-export-analysis', 'version': 1, 'token': uuid4().hex,
|
||||
'library': str(library), 'lease_id': owner['lease_id'], 'supervisor_pid': owner['supervisor_pid'],
|
||||
'analysis_root': str(root), 'project': str(project)}
|
||||
with _guard:
|
||||
marker = blocker_path(library)
|
||||
checkpoint = checkpoint_record(marker)
|
||||
if (checkpoint is None or checkpoint['lease_id'] != owner['lease_id']
|
||||
or 'export_analysis' in checkpoint):
|
||||
raise LibraryBusy('A prior export analysis is unresolved or the lease changed.')
|
||||
_write_json(root/'export-owner.json', record)
|
||||
_write_json(marker, {**checkpoint, 'export_analysis': [record]})
|
||||
return record['token']
|
||||
|
||||
|
||||
@contextmanager
|
||||
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
|
||||
library = Path(library).resolve()
|
||||
assert_retained_exports_idle(library)
|
||||
marker = blocker_path(library)
|
||||
checkpoint = checkpoint_record(marker)
|
||||
if marker.exists() and checkpoint is None:
|
||||
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.')
|
||||
snapshot = None
|
||||
copies = []
|
||||
with ExitStack() as stack:
|
||||
for record in records:
|
||||
try:
|
||||
root, project = Path(record['analysis_root']), Path(record['project'])
|
||||
if (record.get('protocol') != 'cbb-native-export-analysis' or record.get('version') != 1
|
||||
or not record.get('token') or record['lease_id'] != checkpoint['lease_id']
|
||||
or record['supervisor_pid'] != checkpoint['pid']
|
||||
or Path(record['library']).resolve() != library
|
||||
or not root.is_absolute() or not project.is_absolute() or root.resolve() != root
|
||||
or project.parent != root/'project' or not project.is_file()
|
||||
or root.resolve().is_relative_to(library.parent)
|
||||
or library.parent.is_relative_to(root.resolve())
|
||||
or json.loads((root/'export-owner.json').read_text(encoding='utf-8')) != record):
|
||||
raise ValueError('Export record does not match the exact library, lease and copy.')
|
||||
processes = [root/(kind+'-process.json') for kind in ('ascii', 'csv', 'edif')
|
||||
if (root/(kind+'-process.json')).exists()]
|
||||
if not processes:
|
||||
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.')
|
||||
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):
|
||||
raise LibraryBusy('The retained analysis copy still has a database address.')
|
||||
databases = [path for path in entries if path.is_file()
|
||||
and path.name.casefold() in ('icdb.dat', 'layoutdb.lyt')]
|
||||
if not any(path.name.casefold() == 'icdb.dat' for path in databases):
|
||||
raise ValueError('The recorded analysis database is missing.')
|
||||
for path in databases:
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, 0, None)
|
||||
stack.callback(handle.Close)
|
||||
copies.append(str(root/'project'))
|
||||
except (KeyError, TypeError, ValueError, OSError, pywintypes.error) as error:
|
||||
raise LibraryBusy(f'Export analysis release is not proven: {error}') from error
|
||||
yield copies
|
||||
|
||||
|
||||
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,
|
||||
unstarted_token=token if native_not_started else ''):
|
||||
marker = blocker_path(library)
|
||||
checkpoint = checkpoint_record(marker)
|
||||
records = checkpoint.get('export_analysis', []) if checkpoint else []
|
||||
if len(records) != 1 or records[0].get('token') != token:
|
||||
raise LibraryBusy('Export ownership changed before confirmed cleanup.')
|
||||
del checkpoint['export_analysis']
|
||||
_write_json(marker, checkpoint)
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,61 @@
|
||||
"""Interpret the current Project Backup loading session, before any Repair."""
|
||||
import ntpath
|
||||
import re
|
||||
|
||||
|
||||
def _same(left, right):
|
||||
return ntpath.normcase(ntpath.normpath(str(left))) == ntpath.normcase(ntpath.normpath(str(right)))
|
||||
|
||||
|
||||
def classify_inspection(project, database, output):
|
||||
result = {'status': 'UNCONFIRMED', 'repair_required': False}
|
||||
if not isinstance(output, str):
|
||||
return result
|
||||
loaded = re.findall(r'Loading project file \[([^\]\r\n]+)\]\.', output)
|
||||
if len(loaded) != 1 or not _same(loaded[0], project):
|
||||
return result
|
||||
checking = 'Checking if project is in use.'
|
||||
if checking not in output:
|
||||
return result
|
||||
current = output.rsplit(checking, 1)[1]
|
||||
if re.search(r'\bProject is in use\b', current, re.I):
|
||||
return {**result, 'status': 'IN_USE'}
|
||||
if ("Project is not in use. Full iCDB Project Backup's functionality is available." not in current
|
||||
or 'Backup information loaded correctly.' not in current):
|
||||
return result
|
||||
# This exact native error was captured from the isolated EE7.9.5 project.
|
||||
# A generic 'try Repair' or an old error before the latest check is not proof.
|
||||
inconsistency_pattern = (
|
||||
r"Project database \[([^\]\r\n]+)\] is inconsistent\. It has been manually copied while the iCDB Server was running\. "
|
||||
r"Please use iCDB Project Backup 'Repair project' functionality to fix this issue\.")
|
||||
inconsistent = re.findall(inconsistency_pattern, current)
|
||||
from .icdb_recovery import _session_losses
|
||||
losses = list(_session_losses(current))
|
||||
if (len(inconsistent) > 1 or any(not _same(path, database) for path in inconsistent)
|
||||
or len(losses) > 1 or any(not lost.get('session') or lost.get('scope') != 'project'
|
||||
or not _same(lost.get('database_path', ''), database) for lost in losses)):
|
||||
return result
|
||||
required = bool(inconsistent or losses)
|
||||
remaining = re.sub(inconsistency_pattern, '', current)
|
||||
if required:
|
||||
remaining = remaining.replace('Please try to repair the project!', '')
|
||||
if re.search(r'\berror\b|try to repair|inconsistent|corrupt', remaining, re.I):
|
||||
return result
|
||||
return {'status': 'NOT_IN_USE', 'repair_required': bool(required)}
|
||||
|
||||
|
||||
def confirmed_inspection(target, record):
|
||||
"""Only an exact, exited, inspection-only native process grants a verdict."""
|
||||
if (not isinstance(record, dict) or record.get('inspection_only') is not True
|
||||
or record.get('repair_requested') is not False
|
||||
or type(record.get('native_pid')) is not int or record['native_pid'] <= 0
|
||||
or record.get('native_exit_code') != 0
|
||||
or record.get('release', {}).get('native_exited') is not True
|
||||
or not _same(record.get('project', ''), target.project)
|
||||
or not _same(record.get('database', ''), target.database)
|
||||
or record.get('unknown_dialogs')):
|
||||
return False
|
||||
expected = classify_inspection(target.project, target.database, record.get('output'))
|
||||
return (expected['status'] in ('NOT_IN_USE', 'IN_USE')
|
||||
and record.get('status') == expected['status']
|
||||
and record.get('repair_required') is expected['repair_required'])
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Backed-up, single-attempt recovery through the installed native tool."""
|
||||
"""Single-attempt recovery through the installed native tool."""
|
||||
from dataclasses import dataclass
|
||||
import hashlib
|
||||
import json
|
||||
@@ -23,6 +23,8 @@ def select_repair_target(request, diagnosis):
|
||||
raise ValueError('REPAIR_TARGET_UNCONFIRMED: 缺少原生失联会话信息')
|
||||
if diagnosis.get('scope') == 'central_library':
|
||||
raise ValueError('SHARED_DATABASE_REPAIR_UNSUPPORTED: 当前原生 Repair 入口要求 PRJ,不能改修无关的 Work 工程')
|
||||
if diagnosis.get('scope') != 'project':
|
||||
raise ValueError('REPAIR_TARGET_UNCONFIRMED: 数据库所有权范围未知')
|
||||
project, pcb = preflight(request)
|
||||
try:
|
||||
reported = Path(diagnosis.get('database_path', '')).resolve(strict=True)
|
||||
@@ -46,6 +48,19 @@ def _hash(path):
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def check_repair_access(target):
|
||||
"""Preserve the former pre-copy ownership checks without copying data."""
|
||||
import win32con, win32file, pywintypes
|
||||
from .scoped_manifest import scan_tree
|
||||
scan_tree(target.project.parent) # Reject links before native traversal.
|
||||
try:
|
||||
handle = win32file.CreateFile(str(target.database/'icdb.dat'), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
except pywintypes.error as error:
|
||||
raise OSError(f'REPAIR_DATABASE_IN_USE: 无法独占读取数据库:{error}') from error
|
||||
handle.Close()
|
||||
|
||||
|
||||
def backup_project(target, destination):
|
||||
"""Keep every original file, holding icdb.dat exclusively during the copy."""
|
||||
import win32con, win32file, pywintypes
|
||||
@@ -115,7 +130,8 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None):
|
||||
marker = root/'attempt.json'
|
||||
if marker.exists():
|
||||
return {'status': 'BLOCKED', 'detail': 'REPAIR_ALREADY_ATTEMPTED', 'evidence': str(root)}
|
||||
record = {'status': 'BLOCKED', 'evidence': str(root)}
|
||||
record = {'status': 'BLOCKED', 'evidence_path': str(root),
|
||||
'release': {'safe_to_continue': False}}
|
||||
try:
|
||||
process_record = Path(evidence_root)/'content-check-process.json'
|
||||
if process_record.is_file():
|
||||
@@ -127,7 +143,7 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None):
|
||||
target = select_repair_target(request, diagnosis)
|
||||
if progress:
|
||||
progress({'stage': 'native_repair_start', 'block': request.block_name,
|
||||
'detail': '正在备份并尝试原生数据库修复', 'project': str(target.project)})
|
||||
'detail': '正在核对占用并尝试原生数据库修复;应用不自动备份', 'project': str(target.project)})
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
if pcb_processes() or _process_details('LibraryManager.exe'):
|
||||
raise ValueError('REPAIR_DATABASE_IN_USE: 原生 PCB/Library Manager 会话尚未退出')
|
||||
@@ -137,14 +153,111 @@ def attempt_recovery(request, diagnosis, evidence_root, *, progress=None):
|
||||
callback = (lambda event: progress({'block': request.block_name, **event})) if progress else None
|
||||
record = run_native_repair(target, root, mentor_bin=request.mentor_bin,
|
||||
timeout_seconds=max(60, request.timeout_seconds), progress=callback)
|
||||
native_status = record.get('status')
|
||||
record['release'] = repair_release_state(request, target, record)
|
||||
if native_status == 'REPAIRED' and record['release']['safe_to_continue'] is not True:
|
||||
record.update(status='UNCONFIRMED', native_status=native_status,
|
||||
detail='NATIVE_REPAIR_EVIDENCE_UNCONFIRMED: 原生成功或释放证据不完整')
|
||||
record['evidence_path'] = str(root)
|
||||
except Exception as error:
|
||||
record['detail'] = str(error)
|
||||
marker.write_text(json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
return record
|
||||
|
||||
|
||||
def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None):
|
||||
def _native_identity_and_exit_confirmed(target, record):
|
||||
if not isinstance(record, dict):
|
||||
return False
|
||||
release = record.get('release')
|
||||
if (not isinstance(release, dict) or release.get('safe_to_continue') is not True
|
||||
or release.get('native_exited') is not True
|
||||
or not isinstance(release.get('addresses'), list) or release['addresses']
|
||||
or type(record.get('native_pid')) is not int or record['native_pid'] <= 0
|
||||
or not isinstance(record.get('unknown_dialogs', []), list)
|
||||
or record.get('unknown_dialogs')):
|
||||
return False
|
||||
try:
|
||||
for name, expected in (('project', target.project), ('database', target.database)):
|
||||
path = record.get(name)
|
||||
if (not isinstance(path, str) or not Path(path).is_absolute()
|
||||
or Path(path).resolve(strict=True) != expected.resolve(strict=True)):
|
||||
return False
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def native_success_confirmed(target, record):
|
||||
"""Validate the worker's complete, consistent success before any reopening."""
|
||||
if not _native_identity_and_exit_confirmed(target, record) or record.get('status') != 'REPAIRED':
|
||||
return False
|
||||
evidence = record.get('evidence')
|
||||
if (not isinstance(evidence, dict) or evidence.get('confirmed') is not True
|
||||
or evidence.get('native_backup') is not True
|
||||
or evidence.get('outcome') != 'UNCONFIRMED'
|
||||
or evidence.get('failure_complete') is not False
|
||||
or evidence.get('restore_complete') is not False
|
||||
or evidence.get('restored_from_backup') != ''):
|
||||
return False
|
||||
fields = ('diagnostic_tail', 'database_tail', 'restore_tail', 'output_tail')
|
||||
if any(not isinstance(evidence.get(field), str) for field in fields):
|
||||
return False
|
||||
if not evidence['diagnostic_tail'] or not evidence['database_tail']:
|
||||
return False
|
||||
from .native_repair_ui import classify_repair_evidence
|
||||
return (classify_repair_evidence(*(evidence[field] for field in fields))['outcome'] == 'UNCONFIRMED'
|
||||
and 'iCDB database restored from backup:' not in evidence['restore_tail'])
|
||||
|
||||
|
||||
def repair_release_state(request, target, native_result):
|
||||
"""Require normal exit, owner/address absence and exclusive database access."""
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
from .icdb_ownership import database_directories, unreleased_addresses
|
||||
from .verify_runner import preflight
|
||||
release = native_result.get('release') if isinstance(native_result, dict) else None
|
||||
result = {**(release if isinstance(release, dict) else {}), 'safe_to_continue': False,
|
||||
'database_exclusive': False}
|
||||
try:
|
||||
if (not _native_identity_and_exit_confirmed(target, native_result)
|
||||
or (native_result.get('status') == 'REPAIRED'
|
||||
and not native_success_confirmed(target, native_result))):
|
||||
result['error'] = 'NATIVE_REPAIR_EVIDENCE_UNCONFIRMED'
|
||||
return result
|
||||
project, pcb = preflight(request)
|
||||
owners = [int(r[0]) for r in pcb_processes()]
|
||||
for name in ('LibraryManager.exe', 'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe'):
|
||||
owners.extend(int(r[0]) for r in _process_details(name))
|
||||
for pid, _exe, command, _parent in _process_details('cscript.exe'):
|
||||
if (not command or str(project).casefold() in command.casefold()
|
||||
or str(pcb).casefold() in command.casefold()):
|
||||
owners.append(int(pid))
|
||||
result.update(remaining_pids=sorted(set(owners)),
|
||||
addresses=unreleased_addresses(request.lmc_file, project, pcb))
|
||||
if (native_result.get('release', {}).get('safe_to_continue') is not True
|
||||
or owners or result['addresses']):
|
||||
return result
|
||||
import win32con, win32file
|
||||
files = [target.database/'icdb.dat']
|
||||
files.extend(folder/'icdb.dat' for folder in database_directories(request.lmc_file, project, pcb)
|
||||
if (folder/'icdb.dat').is_file())
|
||||
result['exclusive_databases'] = []
|
||||
for database_file in dict.fromkeys(files):
|
||||
handle = win32file.CreateFile(str(database_file), win32con.GENERIC_READ,
|
||||
0, None, win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
handle.Close()
|
||||
result['exclusive_databases'].append(str(database_file))
|
||||
result.update(database_exclusive=True, safe_to_continue=True)
|
||||
except Exception as error:
|
||||
result['error'] = str(error)
|
||||
return result
|
||||
|
||||
|
||||
def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progress=None,
|
||||
maintenance_context=None, inspection_only=False, stop=None):
|
||||
"""Supervise the GUI on its own desktop; a repair timeout never kills it."""
|
||||
if stop is not None and stop.is_set():
|
||||
return {'status': 'CANCELLED', 'native_started': False,
|
||||
'detail': '原生操作启动前已取消', 'release': {'safe_to_continue': True}}
|
||||
from .desktop_worker import DesktopWorker
|
||||
root = Path(root)
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
@@ -153,16 +266,24 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr
|
||||
desktop = 'CbbRepair_'+uuid4().hex[:12]
|
||||
job_path.write_text(json.dumps({'project': str(target.project), 'database': str(target.database),
|
||||
'root': str(root), 'mentor_bin': str(mentor_bin or ''), 'timeout': timeout_seconds,
|
||||
'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop},
|
||||
'cancel_signal': str(cancel), 'supervisor_pid': os.getpid(), 'desktop': desktop,
|
||||
'maintenance_context': maintenance_context, 'inspection_only': inspection_only},
|
||||
ensure_ascii=False), encoding='utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--repair-worker', str(job_path)]
|
||||
worker = DesktopWorker(command, desktop)
|
||||
worker.bind_cancellation(cancel)
|
||||
deadline = time.monotonic()+max(1, float(timeout_seconds))+5
|
||||
# Worker admission includes filesystem/ownership checks before the native
|
||||
# timeout begins. Bound that preparation separately; never extend on polls.
|
||||
deadline = time.monotonic()+60+max(1, float(timeout_seconds))+5
|
||||
event_count = 0
|
||||
cancelled = False
|
||||
try:
|
||||
while worker.poll() is None and time.monotonic()<deadline:
|
||||
if not cancelled and stop is not None and stop.is_set():
|
||||
worker.request_stop()
|
||||
cancelled = True
|
||||
deadline = min(deadline, time.monotonic()+5)
|
||||
event_file = root/'repair-events.jsonl'
|
||||
if progress and event_file.is_file():
|
||||
lines = event_file.read_text('utf-8').splitlines()
|
||||
@@ -176,11 +297,14 @@ def run_native_repair(target, root, *, mentor_bin='', timeout_seconds=300, progr
|
||||
outcome = root/'repair-result.json'
|
||||
if worker.poll() is None or not outcome.is_file():
|
||||
worker.request_stop()
|
||||
return {'status': 'RETAINED', 'worker_pid': worker.pid, 'desktop': desktop,
|
||||
'detail': '原生修复尚未确认结束,保留操作并停止批次', 'evidence': str(root)}
|
||||
return {'status': 'CANCELLED_UNRELEASED' if cancelled else 'TIMEOUT_UNRELEASED', 'worker_pid': worker.pid, 'desktop': desktop,
|
||||
'release': {'safe_to_continue': False},
|
||||
'detail': '原生修复尚未确认结束,保留操作并停止批次', 'evidence_path': str(root)}
|
||||
result = json.loads(outcome.read_text('utf-8'))
|
||||
if worker.returncode != 0 and result.get('status') == 'REPAIRED':
|
||||
if cancelled:
|
||||
result.update(status='CANCELLED', repair_required=False)
|
||||
if worker.returncode != 0 and result.get('status') in ('REPAIRED', 'NOT_IN_USE', 'IN_USE'):
|
||||
result['status'] = 'UNCONFIRMED'
|
||||
return {**result, 'evidence': str(root)}
|
||||
return {**result, 'evidence_path': str(root)}
|
||||
finally:
|
||||
worker.close()
|
||||
@@ -5,6 +5,7 @@ import json
|
||||
import ntpath
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import time
|
||||
import zipfile
|
||||
@@ -30,6 +31,25 @@ def repair_completed(diagnostic_text, database_text, *, native_backup):
|
||||
and 'iCDBProjectBackupRepair' in database_text and 'IC: Closing project' in database_text)
|
||||
|
||||
|
||||
def classify_repair_evidence(diagnostic, database, restore, output):
|
||||
"""Classify this invocation only; an error alone does not prove native idle."""
|
||||
combined = '\n'.join((diagnostic, database, restore, output))
|
||||
# This observed loading-progress line precedes the actual ownership verdict.
|
||||
combined = re.sub(r'(?im)^\s*(?:\[[^\]\r\n]+\]\s*)?Checking if project is in use\.\s*$', '', combined)
|
||||
terminal_error = bool(re.search(r'Repair project error\s*\[[^\r\n]+\]', output, re.I))
|
||||
closed_error = ('Diagnostic: FINISH' in diagnostic and 'IC: Closing project' in database)
|
||||
if re.search(r'project is in use|database.*(?:in use|busy)|other clients are connected', combined, re.I):
|
||||
outcome = 'DATABASE_BUSY'
|
||||
elif re.search(r'there is no backup of (?:missing )?iCDB|no (?:healthy|valid) backup', combined, re.I):
|
||||
outcome = 'MISSING_HEALTHY_BACKUP'
|
||||
elif re.search(r'\bERROR\s*:|not repaired|repair.*failed|Repair project error', combined, re.I):
|
||||
outcome = 'NATIVE_REPAIR_FAILED'
|
||||
else:
|
||||
outcome = 'UNCONFIRMED'
|
||||
return {'outcome': outcome, 'failure_complete': bool(
|
||||
outcome != 'UNCONFIRMED' and (terminal_error or closed_error))}
|
||||
|
||||
|
||||
def _current_desktop_name():
|
||||
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||||
@@ -78,7 +98,7 @@ def _repair_command(executable):
|
||||
|
||||
|
||||
def _windows(pid):
|
||||
import win32gui, win32process
|
||||
import win32api, win32gui, win32process
|
||||
windows = []
|
||||
def observe(hwnd, _):
|
||||
if win32process.GetWindowThreadProcessId(hwnd)[1] != pid or not win32gui.IsWindowVisible(hwnd):
|
||||
@@ -89,20 +109,48 @@ def _windows(pid):
|
||||
children.append({'hwnd': handle, 'text': win32gui.GetWindowText(handle),
|
||||
'class': win32gui.GetClassName(handle), 'id': win32gui.GetDlgCtrlID(handle),
|
||||
'enabled': bool(win32gui.IsWindowEnabled(handle))})
|
||||
# Empty window lists are valid. pywin32 otherwise interprets a stale
|
||||
# thread error (observed ERROR_FILE_NOT_FOUND) as an enum failure.
|
||||
win32api.SetLastError(0)
|
||||
win32gui.EnumChildWindows(hwnd, child, None)
|
||||
windows.append({'hwnd': hwnd, 'title': win32gui.GetWindowText(hwnd),
|
||||
'class': win32gui.GetClassName(hwnd), 'enabled': bool(win32gui.IsWindowEnabled(hwnd)),
|
||||
'children': children})
|
||||
win32api.SetLastError(0)
|
||||
win32gui.EnumWindows(observe, None)
|
||||
return windows
|
||||
|
||||
|
||||
def _native_evidence(project, database, pid, started_ns, baseline):
|
||||
def _native_output(windows):
|
||||
"""Read Output without UI activation; a hung control has a bounded deadline."""
|
||||
user32 = ctypes.WinDLL('user32', use_last_error=True)
|
||||
user32.SendMessageTimeoutW.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM,
|
||||
wintypes.LPARAM, wintypes.UINT, wintypes.UINT, ctypes.POINTER(ctypes.c_size_t)]
|
||||
user32.SendMessageTimeoutW.restype = wintypes.LPARAM
|
||||
texts = []
|
||||
for window in windows:
|
||||
for child in window['children']:
|
||||
if not child['class'].casefold().startswith('richedit'):
|
||||
continue
|
||||
buffer = ctypes.create_unicode_buffer(131072)
|
||||
copied = ctypes.c_size_t()
|
||||
if not user32.SendMessageTimeoutW(child['hwnd'], 0x000D, len(buffer),
|
||||
ctypes.addressof(buffer), 0x0002, 200, ctypes.byref(copied)):
|
||||
return None
|
||||
if copied.value >= len(buffer)-1:
|
||||
return None # A truncated prefix cannot establish the current operation.
|
||||
texts.append(buffer.value)
|
||||
return '\n'.join(texts) if texts else None
|
||||
|
||||
|
||||
def _native_evidence(project, database, pid, started_ns, baseline, *, output_text=''):
|
||||
"""Use only this native process's new diagnostic/close and backup evidence."""
|
||||
log_paths = []
|
||||
def read_logs(folder):
|
||||
text = []
|
||||
for path in (database/'cdbsvr'/folder).glob(f'*-{pid}/*.txt'):
|
||||
if path.stat().st_mtime_ns >= started_ns:
|
||||
log_paths.append(str(path))
|
||||
text.append(path.read_bytes()[-256*1024:].decode('utf-8', errors='replace'))
|
||||
return '\n'.join(text)
|
||||
from .icdb_recovery import read_log_failure
|
||||
@@ -122,9 +170,13 @@ def _native_evidence(project, database, pid, started_ns, baseline):
|
||||
diagnostic, db_log = read_logs('dbcheck'), read_logs('log')
|
||||
restore_log = read_logs('dbrestore')
|
||||
restored = re.search(r'iCDB database restored from backup:\s*([^\r\n]+)', restore_log)
|
||||
valid = (not restored and str(database).casefold() in db_log.casefold()
|
||||
classification = classify_repair_evidence(diagnostic, db_log, restore_log, output_text)
|
||||
valid = (classification['outcome'] == 'UNCONFIRMED' and not restored
|
||||
and str(database).casefold() in db_log.casefold()
|
||||
and repair_completed(diagnostic, db_log, native_backup=native_backup))
|
||||
return {'confirmed': bool(valid), 'native_backup': native_backup,
|
||||
return {**classification, 'confirmed': bool(valid), 'native_backup': native_backup,
|
||||
'output_tail': output_text[-8192:], 'log_paths': log_paths,
|
||||
'backup_log_tail': text[-8192:],
|
||||
'diagnostic_tail': diagnostic[-8192:], 'database_tail': db_log[-8192:],
|
||||
'restored_from_backup': restored.group(1).strip() if restored else '',
|
||||
'restore_complete': bool(restored and '[0] DONE' in restore_log
|
||||
@@ -132,15 +184,99 @@ def _native_evidence(project, database, pid, started_ns, baseline):
|
||||
'restore_tail': restore_log[-8192:]}
|
||||
|
||||
|
||||
def _inspect_project(job, process, emit):
|
||||
"""Observe this process's native query; never send a Repair/menu command."""
|
||||
import win32con, win32gui
|
||||
from .native_occupancy import classify_inspection
|
||||
project, database = Path(job['project']), Path(job['database'])
|
||||
result = {'status': 'UNCONFIRMED', 'project': str(project), 'database': str(database),
|
||||
'native_pid': process.pid, 'inspection_only': True, 'repair_requested': False,
|
||||
'repair_required': False, 'output': ''}
|
||||
deadline = time.monotonic() + max(1, float(job['timeout']))
|
||||
closing = False
|
||||
last_state = None
|
||||
# Reserve a short cooperative-close grace after the inspection deadline.
|
||||
while process.poll() is None and time.monotonic() < deadline + 3:
|
||||
windows = [w for w in _windows(process.pid) if not (
|
||||
w['title'] == '' and w['class'] in ('UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator'))]
|
||||
shells = [w for w in windows if w['class'].startswith('Afx:') and w['title'] == 'iCDB Project Backup']
|
||||
exact = [w for w in windows if w['class'].startswith('Afx:')
|
||||
and w['title'].casefold() == ('iCDB Project Backup - '+str(project)).casefold()]
|
||||
# EE7.9 can keep the generic shell title while loading the requested PRJ.
|
||||
# Its Output still binds the project; waiting only for the decorated title
|
||||
# turns a valid session into a false timeout.
|
||||
mains = exact or shells
|
||||
main = mains[0] if len(mains) == 1 else None
|
||||
unknown = [w for w in windows if w is not main and w not in shells]
|
||||
if unknown:
|
||||
result.update(status='UNCONFIRMED', unknown_dialogs=unknown)
|
||||
break
|
||||
current = _native_output(windows)
|
||||
state = {'main_found': main is not None, 'output_readable': current is not None,
|
||||
'windows': windows}
|
||||
if state != last_state:
|
||||
(Path(job['root'])/'inspection-state.json').write_text(
|
||||
json.dumps(state, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
emit(stage='native_occupancy_wait', main_found=state['main_found'], output_readable=state['output_readable'])
|
||||
last_state = state
|
||||
if current is not None:
|
||||
result['output'] = current
|
||||
cancelled = Path(job['cancel_signal']).exists()
|
||||
expired = time.monotonic() >= deadline
|
||||
verdict = classify_inspection(project, database, current)
|
||||
if cancelled:
|
||||
result.update(status='CANCELLED', repair_required=False)
|
||||
if not closing and main and main['enabled'] and (cancelled or expired or verdict['status'] != 'UNCONFIRMED'):
|
||||
if not cancelled and not expired: result.update(verdict)
|
||||
emit(stage='native_occupancy', **result)
|
||||
win32gui.PostMessage(main['hwnd'], win32con.WM_CLOSE, 0, 0)
|
||||
closing = True
|
||||
time.sleep(.2)
|
||||
exited = process.poll() is not None
|
||||
result['native_exit_code'] = process.poll()
|
||||
result['release'] = {'native_exited': exited,
|
||||
'addresses': [str(database/'cdbsvr/sAddress.adr')] if (database/'cdbsvr/sAddress.adr').exists() else [],
|
||||
'safe_to_continue': exited and result['status'] == 'NOT_IN_USE'}
|
||||
if not exited:
|
||||
result.update(status='TIMEOUT_UNRELEASED', repair_required=False)
|
||||
result['detail'] = ('原生检查窗口未返回 Output,未执行 Repair。'
|
||||
if not result['output'] else '原生检查工具未退出,未执行 Repair。')
|
||||
return result
|
||||
|
||||
|
||||
class _CancellationFile:
|
||||
def __init__(self, path):
|
||||
self.path = Path(path)
|
||||
self._next_poll = 0.0
|
||||
self._cancelled = False
|
||||
self.polls = 0
|
||||
self.poll_seconds = 0.0
|
||||
|
||||
def is_set(self):
|
||||
# scan_tree checks between every entry. Avoid issuing tens of
|
||||
# thousands of filesystem requests just to observe one IPC flag.
|
||||
# The direct checks before Popen/Repair remain immediate.
|
||||
now = time.monotonic()
|
||||
if not self._cancelled and now >= self._next_poll:
|
||||
self._cancelled = self.path.exists()
|
||||
finished = time.monotonic()
|
||||
self.polls += 1
|
||||
self.poll_seconds += finished-now
|
||||
self._next_poll = finished + .1
|
||||
return self._cancelled
|
||||
|
||||
|
||||
def repair_worker_main(job_path):
|
||||
import win32con, win32gui
|
||||
from .native_repair import RepairTarget, backup_project
|
||||
from .native_repair import RepairTarget, check_repair_access
|
||||
from .mentor_paths import discover_sdd_home
|
||||
from .icdb_recovery import snapshot_log
|
||||
job = json.loads(Path(job_path).read_text('utf-8'))
|
||||
stop = _CancellationFile(job['cancel_signal'])
|
||||
root = Path(job['root'])
|
||||
project, database = Path(job['project']), Path(job['database'])
|
||||
result = {'status': 'BLOCKED', 'project': str(project), 'database': str(database)}
|
||||
result = {'status': 'BLOCKED', 'native_started': False, 'project': str(project), 'database': str(database),
|
||||
'release': {'safe_to_continue': False}}
|
||||
process = None
|
||||
def emit(**event):
|
||||
with (root/'repair-events.jsonl').open('a', encoding='utf-8') as stream:
|
||||
@@ -159,35 +295,99 @@ def repair_worker_main(job_path):
|
||||
if home is None:
|
||||
raise ValueError('NATIVE_REPAIR_UNAVAILABLE: 未找到 Mentor 安装目录')
|
||||
executable = home/'iCDB/win32/bin/iCDBProjectBackup.exe'
|
||||
command_id = _repair_command(executable)
|
||||
command_id = None if job.get('inspection_only') else _repair_command(executable)
|
||||
from .mentor_adapter import pcb_processes, _process_details
|
||||
if pcb_processes() or _process_details('LibraryManager.exe'):
|
||||
if not job.get('inspection_only') and (pcb_processes() or _process_details('LibraryManager.exe')):
|
||||
raise ValueError('REPAIR_DATABASE_IN_USE')
|
||||
backup_project(RepairTarget(project, database), root/'complete-backup')
|
||||
emit(stage='complete_backup_verified', path=str(root/'complete-backup'))
|
||||
maintenance_context = job.get('maintenance_context')
|
||||
if maintenance_context is not None:
|
||||
from .maintenance_entry import maintenance_target_guard
|
||||
from .verify_runner import VerifyRequest
|
||||
maintenance_request = VerifyRequest(Path(maintenance_context['library']),
|
||||
maintenance_context['block'], root, maintenance=True)
|
||||
emit(stage='native_preflight', detail='正在核对本次原生工具的目标及占用')
|
||||
if job.get('inspection_only'):
|
||||
from .scoped_manifest import scan_tree
|
||||
def check_cancelled():
|
||||
if stop.is_set():
|
||||
raise InterruptedError('检查已取消')
|
||||
scan_tree(project.parent, check=check_cancelled)
|
||||
else:
|
||||
check_repair_access(RepairTarget(project, database))
|
||||
emit(stage='backup_skipped', reason='manual_backup_policy',
|
||||
detail='应用不自动备份;Mentor 原生修复仍可能生成自身备份。')
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_AFTER_BACKUP')
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH')
|
||||
if maintenance_context is not None:
|
||||
# Manual backup policy leaves no mutation between preparation and
|
||||
# launch. Run the complete guard once here, retaining its identity,
|
||||
# address and exclusive-access checks immediately before Popen.
|
||||
with maintenance_target_guard(maintenance_request, maintenance_context, stop=stop) as checked:
|
||||
if (checked['target'].project.resolve() != project.resolve()
|
||||
or checked['target'].database.resolve() != database.resolve()):
|
||||
raise ValueError('MAINTENANCE_TARGET_UNCONFIRMED')
|
||||
emit(stage='native_preflight_complete', cancellation_polls=stop.polls,
|
||||
cancellation_poll_seconds=round(stop.poll_seconds, 3))
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH')
|
||||
baseline = snapshot_log(project.parent/'Log Files/ProjectBackupLog.txt')
|
||||
started_ns = time.time_ns()
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_LAUNCH')
|
||||
process = subprocess.Popen([str(executable), '-p', str(project)], cwd=str(project.parent))
|
||||
result['native_started'] = True
|
||||
result['native_pid'] = process.pid
|
||||
emit(stage='native_started', pid=process.pid, project=str(project))
|
||||
if job.get('inspection_only'):
|
||||
result = _inspect_project(job, process, emit)
|
||||
(root/'repair-result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
return 0 if result['status'] in ('NOT_IN_USE', 'IN_USE') else 1
|
||||
deadline = time.monotonic()+max(1, float(job['timeout']))
|
||||
invoked = confirmed = closing = False
|
||||
evidence = None
|
||||
output_baseline = None
|
||||
output = ''
|
||||
while process.poll() is None and time.monotonic()<deadline:
|
||||
windows = _windows(process.pid)
|
||||
main = next((w for w in windows if w['class'].startswith('Afx:')
|
||||
and w['title'].casefold() == ('iCDB Project Backup - '+str(project)).casefold()), None)
|
||||
windows = [w for w in _windows(process.pid) if not (
|
||||
w['title'] == '' and w['class'] in (
|
||||
'UAC_InputIndicatorOverlayWnd', 'UAC Input Indicator'))]
|
||||
mains = [w for w in windows if w['class'].startswith('Afx:')
|
||||
and w['title'].casefold() == ('iCDB Project Backup - '+str(project)).casefold()]
|
||||
main = mains[0] if len(mains) == 1 else None
|
||||
# The native Afx shell appears before it loads the requested PRJ.
|
||||
# Only a unique, exact generic shell before Repair may wait here;
|
||||
# it never authorizes a command or masks another target/window.
|
||||
shells = [w for w in windows if not invoked and w['class'].startswith('Afx:')
|
||||
and w['title'] == 'iCDB Project Backup']
|
||||
starting = shells[0] if len(shells) == 1 and not mains else None
|
||||
dialogs = [w for w in windows if w['class'] == '#32770']
|
||||
if not invoked and main and not dialogs and main['enabled']:
|
||||
unknown = [w for w in windows if w is not main and w is not starting and w not in dialogs]
|
||||
current_output = _native_output(windows)
|
||||
busy_before_start = (not invoked and current_output is not None
|
||||
and classify_repair_evidence('', '', '', current_output)['outcome'] == 'DATABASE_BUSY')
|
||||
if busy_before_start:
|
||||
output = current_output
|
||||
result.update(status='DATABASE_BUSY', detail='原生工具加载工程时报告占用;未请求 Repair')
|
||||
maintenance_ready = (maintenance_context is None or isinstance(current_output, str)
|
||||
and 'Project is not in use. Full iCDB Project Backup\'s functionality is available.' in current_output)
|
||||
if (not invoked and isinstance(current_output, str) and not busy_before_start and maintenance_ready
|
||||
and main and not dialogs and not unknown and main['enabled']):
|
||||
if Path(job['cancel_signal']).exists():
|
||||
raise ValueError('REPAIR_CANCELLED_BEFORE_START')
|
||||
output_baseline = current_output
|
||||
win32gui.PostMessage(main['hwnd'], win32con.WM_COMMAND, command_id, 0)
|
||||
invoked = True
|
||||
emit(stage='native_repair_requested', command=command_id)
|
||||
if invoked and not confirmed and main:
|
||||
for dialog in dialogs:
|
||||
if dialogs or unknown:
|
||||
known = [d for d in dialogs if invoked and not confirmed and main
|
||||
and confirmation_allowed(main['title'], project,
|
||||
'\n'.join(c['text'] for c in d['children']))]
|
||||
if unknown or len(dialogs) != 1 or len(known) != 1:
|
||||
result.update(status='UNKNOWN_DIALOG', unknown_dialogs=[*unknown, *dialogs],
|
||||
detail='未知原生弹窗;未发送操作,保留原生工具')
|
||||
# Capture evidence below before stopping supervision.
|
||||
else:
|
||||
dialog = known[0]
|
||||
text = '\n'.join(c['text'] for c in dialog['children'])
|
||||
if confirmation_allowed(main['title'], project, text):
|
||||
if Path(job['cancel_signal']).exists():
|
||||
@@ -198,16 +398,41 @@ def repair_worker_main(job_path):
|
||||
win32gui.PostMessage(button['hwnd'], win32con.BM_CLICK, 0, 0)
|
||||
confirmed = True
|
||||
emit(stage='native_repair_confirmed', text=text)
|
||||
if (invoked and output_baseline is not None and current_output is not None
|
||||
and current_output.startswith(output_baseline)):
|
||||
output = current_output[len(output_baseline):]
|
||||
evidence = _native_evidence(project, database, process.pid, started_ns, baseline,
|
||||
output_text=output)
|
||||
(root/'native-output.json').write_text(json.dumps({
|
||||
'native_pid': process.pid, 'baseline': output_baseline, 'current': current_output,
|
||||
'invocation_output': output}, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
if result['status'] == 'UNKNOWN_DIALOG' or busy_before_start:
|
||||
break
|
||||
if confirmed and not closing and main and not dialogs and main['enabled']:
|
||||
evidence = _native_evidence(project, database, process.pid, started_ns, baseline)
|
||||
if (evidence['confirmed'] or (evidence.get('restore_complete')
|
||||
if (evidence['outcome'] != 'DATABASE_BUSY'
|
||||
and not (database/'cdbsvr/sAddress.adr').exists() and (
|
||||
evidence['failure_complete'] or evidence['confirmed'] or (evidence.get('restore_complete')
|
||||
and evidence['native_backup']
|
||||
and not (database/'cdbsvr/sAddress.adr').exists())):
|
||||
and evidence['outcome'] == 'UNCONFIRMED'))):
|
||||
win32gui.PostMessage(main['hwnd'], win32con.WM_CLOSE, 0, 0)
|
||||
closing = True
|
||||
emit(stage='native_repair_finished', **evidence)
|
||||
time.sleep(.2)
|
||||
if (closing and process.poll() == 0 and evidence and evidence.get('restore_complete')
|
||||
if evidence:
|
||||
result['evidence'] = evidence
|
||||
for log_path in evidence['log_paths']:
|
||||
source = Path(log_path)
|
||||
destination = root/'native-logs'/source.relative_to(database/'cdbsvr')
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(source, destination)
|
||||
addresses = [str(database/'cdbsvr/sAddress.adr')] if (database/'cdbsvr/sAddress.adr').exists() else []
|
||||
result['release'] = {'native_exited': process.poll() is not None, 'addresses': addresses,
|
||||
'safe_to_continue': process.poll() is not None and not addresses}
|
||||
if result['status'] == 'UNKNOWN_DIALOG':
|
||||
pass
|
||||
elif evidence and evidence['outcome'] != 'UNCONFIRMED':
|
||||
result.update(status=evidence['outcome'], detail='原生 Repair 失败;详见本次日志和 Output')
|
||||
elif (closing and process.poll() == 0 and evidence and evidence.get('restore_complete')
|
||||
and not (database/'cdbsvr/sAddress.adr').exists()):
|
||||
result.update(status='RESTORED_REVIEW_REQUIRED', evidence=evidence,
|
||||
detail='原生工具从已有备份恢复数据库;需核对恢复版本和工程内容,未自动重试校验')
|
||||
@@ -215,10 +440,12 @@ def repair_worker_main(job_path):
|
||||
and not (database/'cdbsvr/sAddress.adr').exists()):
|
||||
result.update(status='REPAIRED', evidence=evidence)
|
||||
else:
|
||||
result.update(status='RETAINED' if process.poll() is None else 'UNCONFIRMED',
|
||||
result.update(status='TIMEOUT_UNRELEASED' if process.poll() is None else 'UNCONFIRMED',
|
||||
detail='原生 Repair 未取得完整成功/释放证据,未重试校验')
|
||||
except Exception as error:
|
||||
result.update(detail=str(error), status='RETAINED' if process and process.poll() is None else 'BLOCKED')
|
||||
result['preflight_cancellation'] = {'polls': stop.polls,
|
||||
'seconds': round(stop.poll_seconds, 3)}
|
||||
# Never terminate a repair or its database owner. Retained PID/desktop is
|
||||
# reported to the parent, including unknown native prompts.
|
||||
(root/'repair-result.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
|
||||
@@ -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,20 +260,61 @@ 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
|
||||
and self.created and self.drive.upper() == other.drive.upper()
|
||||
and self.remote_root and _same_remote(self.remote_root, other.remote_root)):
|
||||
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}"
|
||||
@@ -249,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
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Cooperative FIFO admission for this application's native operations."""
|
||||
from threading import Condition
|
||||
|
||||
|
||||
class QueueCancelled(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
class NativeQueue:
|
||||
def __init__(self):
|
||||
self.condition = Condition()
|
||||
self.pending = []
|
||||
self.active = False
|
||||
|
||||
def wake(self):
|
||||
with self.condition: self.condition.notify_all()
|
||||
|
||||
def run(self, action, *, stop=None, waiting=None, ready=lambda: True):
|
||||
ticket = object()
|
||||
acquired, notified = False, False
|
||||
with self.condition: self.pending.append((ticket, ready))
|
||||
try:
|
||||
while not acquired:
|
||||
with self.condition:
|
||||
if stop is not None and stop.is_set():
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
first = next((token for token, available in self.pending if available()), None)
|
||||
if not self.active and first is ticket:
|
||||
self.pending = [entry for entry in self.pending if entry[0] is not ticket]
|
||||
self.active = acquired = True
|
||||
else:
|
||||
self.condition.wait(.1)
|
||||
if not acquired and not notified and waiting is not None:
|
||||
notified = True
|
||||
try: waiting()
|
||||
except Exception: pass
|
||||
if stop is not None and stop.is_set():
|
||||
raise QueueCancelled('已取消排队,未启动原生操作。')
|
||||
return action()
|
||||
finally:
|
||||
with self.condition:
|
||||
self.pending = [entry for entry in self.pending if entry[0] is not ticket]
|
||||
if acquired: self.active = False
|
||||
self.condition.notify_all()
|
||||
@@ -0,0 +1,88 @@
|
||||
"""Bound WMI enumeration in a read-only helper, never in the Qt thread."""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
|
||||
def query_owners(names, *, timeout_seconds=10):
|
||||
names = tuple(names)
|
||||
if not names or any(not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names):
|
||||
raise ValueError('OWNER_QUERY_INVALID')
|
||||
return _query_processes(names, timeout_seconds=timeout_seconds)
|
||||
|
||||
|
||||
def query_process_snapshot(*, timeout_seconds=10):
|
||||
"""Bound a complete process tree; retain unknown executable descendants."""
|
||||
return _query_processes(None, timeout_seconds=timeout_seconds)
|
||||
|
||||
|
||||
def _query_processes(names, *, timeout_seconds):
|
||||
full_snapshot = names is None
|
||||
request = {'snapshot_version': 1} if full_snapshot else {'names': names}
|
||||
with tempfile.TemporaryDirectory(prefix='cbb-owner-probe-') as directory:
|
||||
job = Path(directory)/'request.json'
|
||||
job.write_text(json.dumps(request), encoding='utf-8')
|
||||
command = ([sys.executable] if getattr(sys, 'frozen', False) else
|
||||
[sys.executable, '-m', 'cbb_refresh_verify']) + ['--owner-probe', str(job)]
|
||||
try:
|
||||
result = subprocess.run(command, timeout=max(.1, min(10, timeout_seconds)),
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
except subprocess.TimeoutExpired as error:
|
||||
# This helper only enumerates processes; it never launches Mentor
|
||||
# or acquires a native database session.
|
||||
raise RuntimeError('OWNER_QUERY_TIMEOUT: 进程查询超时,占用状态无法确认') from error
|
||||
try:
|
||||
record = json.loads(job.with_name('result.json').read_text('utf-8'))
|
||||
rows = record['owners']
|
||||
identity_matches = (type(record.get('snapshot_version')) is int and record['snapshot_version'] == 1
|
||||
if full_snapshot else record.get('names') == list(names))
|
||||
if result.returncode != 0 or not identity_matches or not isinstance(rows, list):
|
||||
raise ValueError('invalid result')
|
||||
seen = set()
|
||||
for row in rows:
|
||||
if (type(row['pid']) is not int or row['pid'] < (0 if full_snapshot else 1)
|
||||
or row['pid'] in seen or not isinstance(row['name'], str) or not row['name']
|
||||
or not full_snapshot and row['name'].casefold() not in {n.casefold() for n in names}
|
||||
or full_snapshot and not isinstance(row['created'], str)
|
||||
or not isinstance(row['command'], str) or not isinstance(row['executable'], str)
|
||||
or type(row['parent']) is not int or row['parent'] < 0):
|
||||
raise ValueError('invalid owner')
|
||||
seen.add(row['pid'])
|
||||
return rows
|
||||
except (OSError, ValueError, KeyError, TypeError, AttributeError) as error:
|
||||
raise RuntimeError('OWNER_QUERY_UNCONFIRMED: 进程查询未返回有效证据') from error
|
||||
|
||||
|
||||
def owner_probe_main(job):
|
||||
import pythoncom
|
||||
import win32com.client
|
||||
job = Path(job)
|
||||
request = json.loads(job.read_text('utf-8'))
|
||||
full_snapshot = request == {'snapshot_version': 1} and type(request.get('snapshot_version')) is int
|
||||
names = request.get('names')
|
||||
if not full_snapshot:
|
||||
if (not isinstance(names, list) or not names
|
||||
or any(not isinstance(name, str) or not re.fullmatch(r'[\w.-]+\.exe', name, re.I) for name in names)):
|
||||
return 1
|
||||
pythoncom.CoInitialize()
|
||||
try:
|
||||
service = win32com.client.GetObject('winmgmts:')
|
||||
query = ('SELECT ProcessId,Name,CommandLine,ExecutablePath,ParentProcessId' +
|
||||
(',CreationDate' if full_snapshot else '') + ' FROM Win32_Process')
|
||||
if not full_snapshot:
|
||||
query += ' WHERE ' + ' OR '.join(f"Name='{name}'" for name in names)
|
||||
rows = [{'pid': int(item.ProcessId), 'name': str(item.Name),
|
||||
'command': str(item.CommandLine or ''), 'executable': str(item.ExecutablePath or ''),
|
||||
'parent': int(item.ParentProcessId or 0),
|
||||
**({'created': str(item.CreationDate or '')} if full_snapshot else {})}
|
||||
for item in service.ExecQuery(query)]
|
||||
service = None
|
||||
response = {'snapshot_version': 1} if full_snapshot else {'names': names}
|
||||
job.with_name('result.json').write_text(json.dumps({**response, 'owners': rows}), encoding='utf-8')
|
||||
return 0
|
||||
finally:
|
||||
pythoncom.CoUninitialize()
|
||||
@@ -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."""
|
||||
@@ -103,6 +132,11 @@ def wait_for_target_document(job, app_provider, *, process_alive, emit=lambda **
|
||||
try:
|
||||
app = app_provider()
|
||||
document = app.ActiveDocument
|
||||
if not document:
|
||||
observation = ('PCB_DOCUMENT_NOT_READY', 'COM 已连接,尚无目标文档')
|
||||
if observation != last_observation:
|
||||
emit(stage='pcb_document_wait', error_code=observation[0], detail=observation[1])
|
||||
last_observation = observation
|
||||
if document:
|
||||
try:
|
||||
integration = require_document_identity(document, job)
|
||||
@@ -126,18 +160,3 @@ def wait_for_target_document(job, app_provider, *, process_alive, emit=lambda **
|
||||
last_observation = observation
|
||||
sleep(.25)
|
||||
raise PcbBridgeError('PCB_EXITED_BEFORE_COM', 'PCB 在 COM 文档就绪前退出')
|
||||
|
||||
|
||||
def execution_outcome(*, save_complete: bool, pcb_exited: bool,
|
||||
fatal_error: str = '') -> dict[str, object]:
|
||||
if fatal_error:
|
||||
code, _, detail = str(fatal_error).partition(':')
|
||||
return {'status': 'FAIL', 'error_code': code.strip() or 'FATAL_DIALOG',
|
||||
'stderr': detail.strip() or str(fatal_error), 'safe_to_continue': True}
|
||||
if not save_complete:
|
||||
return {'status': 'FAIL', 'error_code': 'SAVE_NOT_CONFIRMED',
|
||||
'stderr': '保存/关闭未完成', 'safe_to_continue': True}
|
||||
if not pcb_exited:
|
||||
return {'status': 'FAIL', 'error_code': 'PCB_EXIT_NOT_CONFIRMED',
|
||||
'stderr': '保存后未确认 PCB 退出', 'safe_to_continue': True}
|
||||
return {'status': 'PASS', 'stdout': 'Project Integration 已同步;保存、关闭和 PCB 退出均已确认。'}
|
||||
@@ -0,0 +1,151 @@
|
||||
"""Explicit user choice for the owned PCB's native historical-version prompt."""
|
||||
from dataclasses import replace
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Lock
|
||||
import time
|
||||
|
||||
from .dialog_rules import pcb_version_choice
|
||||
|
||||
|
||||
class RestoreChoice:
|
||||
def __init__(self, block, library, candidates):
|
||||
self.block, self.library = block, library
|
||||
self.candidates = tuple(dict(c) for c in candidates)
|
||||
self.decision = None
|
||||
self._lock = Lock()
|
||||
|
||||
def choose(self, kind):
|
||||
if kind != 'cancel' and not any(c['kind'] == kind and c['enabled'] for c in self.candidates):
|
||||
raise ValueError('Unknown or disabled PCB restoration version')
|
||||
with self._lock:
|
||||
if self.decision is None:
|
||||
self.decision = kind
|
||||
|
||||
|
||||
def _candidate_key(candidates):
|
||||
return tuple((c['kind'], c['control_id'], c['label'], c['date'], c['enabled']) for c in candidates)
|
||||
|
||||
|
||||
def _choice(snapshot):
|
||||
choice = pcb_version_choice(snapshot)
|
||||
buttons = [c for c in snapshot.controls if c.class_name == 'Button']
|
||||
if (choice is None or any(c.control_id not in (6212, 6213, 6206, 1, 2) for c in buttons)
|
||||
or len([c for c in buttons if c.control_id == 1 and c.enabled and c.name == 'OK']) != 1):
|
||||
raise ValueError('PCB_VERSION_PROMPT_CHANGED')
|
||||
return choice
|
||||
|
||||
|
||||
class RestoreController:
|
||||
def __init__(self, job):
|
||||
from .post_repair_consent import _validate_grant
|
||||
self.job = job
|
||||
self.grant = _validate_grant(job)
|
||||
self.prompt = None
|
||||
self.original = None
|
||||
self.selection_sent = self.confirm_sent = self.failed = False
|
||||
self.sent_at = 0.0
|
||||
|
||||
def handle(self, snapshot, snapshots, *, target_pid, launcher_pid, worker_pid, companion_pids, show, perform):
|
||||
from . import post_repair_consent as consent
|
||||
from .dialog_supervisor import DialogPollResult
|
||||
from .management import _write_json
|
||||
try:
|
||||
choice = _choice(snapshot)
|
||||
if sum(c['selected'] for c in choice['candidates']) > 1:
|
||||
raise ValueError('PCB_VERSION_SELECTION_AMBIGUOUS')
|
||||
if self.failed or snapshot.pid != target_pid or len(snapshots) != 1:
|
||||
raise ValueError('PCB_VERSION_OWNER_OR_WINDOW_UNCONFIRMED')
|
||||
if self.original is not None and (snapshot.hwnd != self.original.hwnd
|
||||
or snapshot.pid != self.original.pid
|
||||
or _candidate_key(choice['candidates']) != _candidate_key(self.prompt.candidates)):
|
||||
raise ValueError('PCB_VERSION_CANDIDATES_CHANGED')
|
||||
pre = json.loads((Path(self.job['root'])/'post-repair-prelaunch.json').read_text(encoding='utf-8'))
|
||||
if (pre.get('nonce') != self.grant['nonce'] or pre.get('desktop') != self.job['desktop']
|
||||
or pre.get('worker_pid') != worker_pid or pre.get('supervisor_pid') != self.job['supervisor_pid']
|
||||
or pre.get('addresses') != []
|
||||
or not all(consent._same(pre[key], self.grant[key]) for key in ('project', 'pcb', 'library'))):
|
||||
raise ValueError('PCB_VERSION_PRELAUNCH_PROOF_INVALID')
|
||||
consent._validate_current_processes(self.job, pre, target_pid, launcher_pid, worker_pid, companion_pids)
|
||||
if self.prompt is None:
|
||||
self.original = snapshot
|
||||
self.prompt = RestoreChoice(self.job['block_name'], self.job['lmc_file'], choice['candidates'])
|
||||
show(self.prompt)
|
||||
if self.prompt.decision is None:
|
||||
return DialogPollResult(activity=True, observed_snapshots=tuple(snapshots))
|
||||
if self.prompt.decision == 'cancel':
|
||||
raise ValueError('用户取消 PCB 版本选择;未确认恢复,保留原生现场。')
|
||||
selected = next(c for c in choice['candidates'] if c['kind'] == self.prompt.decision)
|
||||
if not selected['enabled']:
|
||||
raise ValueError('PCB_VERSION_DISABLED')
|
||||
if self.confirm_sent:
|
||||
if time.monotonic()-self.sent_at < 2:
|
||||
return DialogPollResult(activity=False, observed_snapshots=tuple(snapshots))
|
||||
raise ValueError('PCB_VERSION_CONFIRMATION_NOT_COMPLETED')
|
||||
if self.selection_sent and not selected['selected']:
|
||||
if time.monotonic()-self.sent_at < 2:
|
||||
return DialogPollResult(activity=False, observed_snapshots=tuple(snapshots))
|
||||
raise ValueError('PCB_VERSION_SELECTION_NOT_CONFIRMED')
|
||||
confirming = selected['selected'] and sum(c['selected'] for c in choice['candidates']) == 1
|
||||
control = 1 if confirming else selected['control_id']
|
||||
action = {'action': 'user_selected_pcb_version', 'channels': ['win32_button_click'],
|
||||
'target': {'control_id': control}, 'desktop': self.job['desktop'], 'pid': target_pid,
|
||||
'choice': selected['kind'], 'candidates': list(self.prompt.candidates)}
|
||||
self.selection_sent = True
|
||||
self.confirm_sent = confirming
|
||||
self.sent_at = time.monotonic()
|
||||
record = {'project': self.job['project'], 'pcb': self.job['pcb'], 'choice': selected['kind'],
|
||||
'date': selected['date'], 'historical_restore': True, 'confirm_sent': confirming,
|
||||
'snapshot': snapshot.to_record()}
|
||||
_write_json(Path(self.job['root'])/'pcb-version-user-choice.json', record)
|
||||
result = perform(snapshot, action, snapshot.phase)
|
||||
if result.error_code or result.accepted_controls != (control,):
|
||||
self.failed = True
|
||||
return DialogPollResult(error_code=result.error_code or 'PCB_VERSION_ACTION_FAILED', detail=result.detail,
|
||||
snapshot=snapshot, observed_snapshots=tuple(snapshots))
|
||||
if confirming:
|
||||
_write_json(Path(self.job['root'])/'pcb-version-restoration.json', record)
|
||||
return DialogPollResult(activity=True, action_sent=True, snapshot=snapshot,
|
||||
action=action['action'], channel=result.channel, accepted_controls=result.accepted_controls,
|
||||
rule_id='explicit-user-pcb-version', observed_snapshots=tuple(snapshots))
|
||||
except Exception as error:
|
||||
self.failed = True
|
||||
return DialogPollResult(error_code='PCB_VERSION_CHOICE_REQUIRED', detail=str(error),
|
||||
snapshot=snapshot, observed_snapshots=tuple(snapshots))
|
||||
|
||||
|
||||
def validate_action_window(snapshot, action):
|
||||
"""Recheck the exact live window and radio selection on the private desktop."""
|
||||
import win32gui, win32process
|
||||
from .native_repair_ui import _current_desktop_name
|
||||
from .dialog_models import ControlSnapshot
|
||||
if (not str(action.get('desktop', '')).startswith('CbbVerify_')
|
||||
or _current_desktop_name() != action.get('desktop') or snapshot.pid != action.get('pid')
|
||||
or win32process.GetWindowThreadProcessId(snapshot.hwnd)[1] != snapshot.pid
|
||||
or win32gui.GetWindowText(snapshot.hwnd) != snapshot.title
|
||||
or win32gui.GetClassName(snapshot.hwnd) != snapshot.window_class
|
||||
or not win32gui.IsWindowVisible(snapshot.hwnd)):
|
||||
raise ValueError('PCB_VERSION_LIVE_WINDOW_MISMATCH')
|
||||
children = []
|
||||
win32gui.EnumChildWindows(snapshot.hwnd, lambda h, _: children.append(int(h)), None)
|
||||
live = []
|
||||
for h in children:
|
||||
control_id = win32gui.GetDlgCtrlID(h) & 0xFFFF
|
||||
checked = False
|
||||
if control_id in (6213, 6206):
|
||||
value = win32gui.SendMessageTimeout(h, 0x00F0, 0, 0, 2, 1000)
|
||||
checked = (value[-1] if isinstance(value, tuple) else value) == 1
|
||||
live.append(ControlSnapshot(h, control_id, win32gui.GetClassName(h), win32gui.GetWindowText(h),
|
||||
enabled=bool(win32gui.IsWindowEnabled(h)), checked=checked))
|
||||
choice = _choice(replace(snapshot, controls=tuple(live)))
|
||||
if _candidate_key(choice['candidates']) != _candidate_key(action.get('candidates', [])):
|
||||
raise ValueError('PCB_VERSION_LIVE_CANDIDATES_CHANGED')
|
||||
selected = next((c for c in choice['candidates'] if c['kind'] == action.get('choice') and c['enabled']), None)
|
||||
control_id = action.get('target', {}).get('control_id')
|
||||
if selected is None or control_id not in (selected['control_id'], 1):
|
||||
raise ValueError('PCB_VERSION_ACTION_MISMATCH')
|
||||
if control_id == 1 and (not selected['selected'] or sum(c['selected'] for c in choice['candidates']) != 1):
|
||||
raise ValueError('PCB_VERSION_LIVE_SELECTION_MISMATCH')
|
||||
target = next(c for c in live if c.control_id == control_id)
|
||||
if target.handle != next(c.handle for c in snapshot.controls if c.control_id == control_id):
|
||||
raise ValueError('PCB_VERSION_CONTROL_CHANGED')
|
||||
@@ -0,0 +1,286 @@
|
||||
"""One-use, local-only read/write consent after proven explicit native Repair.
|
||||
|
||||
No normal Verify or declarative rule can enable this path. Database recovery
|
||||
choices other than the captured read/write question remain unsupported.
|
||||
"""
|
||||
from contextlib import ExitStack
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import stat
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
PROMPT_TEXT = ("If an XDS or ExpeditionPCB is running on this design, it's likely that\n"
|
||||
"your file system and/or network system was too slow to determine\n"
|
||||
"which product the design is open in within the 15 second timeout\nperiod.\n\n"
|
||||
"Otherwise, the design may not have been closed properly due to\n"
|
||||
"the XDS or ExpeditionPCB crashing in the last session.\n\n"
|
||||
"Before continuing, please confirm that no other users have the\ndesign open.\n\n"
|
||||
"Continue to open the design with read/write access permission?")
|
||||
|
||||
|
||||
def _drive_type(path):
|
||||
import win32file
|
||||
return win32file.GetDriveType(str(Path(path).anchor))
|
||||
|
||||
|
||||
def require_local_fixed(path):
|
||||
from .network_path import absolute_preserving_drive
|
||||
path = absolute_preserving_drive(path)
|
||||
if path.drive.startswith('\\') or _drive_type(path) != 3:
|
||||
raise ValueError('LOCAL_FIXED_REQUIRED: 修复后读写确认仅支持本地固定磁盘;UNC/网络盘不自动授权')
|
||||
for item in (path, *path.parents):
|
||||
if item.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError('LOCAL_FIXED_REQUIRED: 不允许重解析点、目录链接或挂载路径')
|
||||
return path
|
||||
|
||||
|
||||
def _same(a, b):
|
||||
return Path(a).resolve() == Path(b).resolve()
|
||||
|
||||
|
||||
def _process_records():
|
||||
import win32com.client
|
||||
names = ('ExpeditionPCB.exe', 'LibraryManager.exe', 'viewdraw.exe', 'XDS.exe',
|
||||
'iCDBProjectBackup.exe', 'ReuseBlockImporter.exe', 'CleanWGLib.exe',
|
||||
'cscript.exe', 'wscript.exe', 'python.exe', 'pythonw.exe', 'CbbRefreshVerify.exe')
|
||||
service = win32com.client.GetObject('winmgmts:')
|
||||
rows = service.ExecQuery('SELECT ProcessId, Name, ExecutablePath, CommandLine, ParentProcessId '
|
||||
'FROM Win32_Process WHERE ' + ' OR '.join(f"Name='{n}'" for n in names))
|
||||
records = []
|
||||
for item in rows:
|
||||
name, command = str(item.Name), str(item.CommandLine or '')
|
||||
if name.casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe') and command and not any(
|
||||
flag in command for flag in ('--verify-worker', '--repair-worker', '--management-worker', '--dialog-agent')):
|
||||
continue
|
||||
records.append((int(item.ProcessId), name, str(item.ExecutablePath or ''), command, int(item.ParentProcessId or 0)))
|
||||
return records
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PostRepairProof:
|
||||
library: str
|
||||
project: str
|
||||
pcb: str
|
||||
block: str
|
||||
repair_project: str
|
||||
repair_database: str
|
||||
repair_json: str
|
||||
nonce: str
|
||||
|
||||
@classmethod
|
||||
def create(cls, request, target, recovery):
|
||||
from .native_repair import native_success_confirmed
|
||||
from .verify_runner import preflight
|
||||
if request.maintenance is not True or not native_success_confirmed(target, recovery):
|
||||
raise ValueError('POST_REPAIR_PROOF_REQUIRED: 必须是明确维护且严格确认的原生修复')
|
||||
if request.mapped_remote_root:
|
||||
raise ValueError('LOCAL_FIXED_REQUIRED: 网络映射库不自动确认修复后的读写访问')
|
||||
project, pcb = preflight(request)
|
||||
for path in (request.lmc_file, project, pcb, target.database/'icdb.dat', pcb.parent/'Layout/LayoutDB.lyt'):
|
||||
require_local_fixed(path)
|
||||
return cls(str(request.lmc_file), str(project), str(pcb), request.block_name,
|
||||
str(target.project), str(target.database), json.dumps(recovery, ensure_ascii=False, default=str), uuid4().hex)
|
||||
|
||||
def for_job(self, job):
|
||||
if (job.get('interactive_input') or job.get('native_lm_verify')
|
||||
or not str(job.get('desktop', '')).startswith('CbbVerify_')
|
||||
or job['block_name'] != self.block
|
||||
or not all(_same(job[key], value) for key, value in (
|
||||
('lmc_file', self.library), ('project', self.project), ('pcb', self.pcb)))):
|
||||
raise ValueError('POST_REPAIR_TARGET_MISMATCH')
|
||||
return {'nonce': self.nonce, 'intent': 'explicit_maintenance_after_confirmed_repair',
|
||||
'library': self.library, 'project': self.project, 'pcb': self.pcb, 'block': self.block,
|
||||
'repair_project': self.repair_project, 'repair_database': self.repair_database,
|
||||
'repair': json.loads(self.repair_json), 'desktop': job['desktop'], 'root': job['root']}
|
||||
|
||||
|
||||
def _validate_grant(job):
|
||||
from .native_repair import RepairTarget, native_success_confirmed
|
||||
grant = job.get('post_repair_consent')
|
||||
if (not isinstance(grant, dict) or grant.get('intent') != 'explicit_maintenance_after_confirmed_repair'
|
||||
or not isinstance(grant.get('nonce'), str) or len(grant['nonce']) != 32
|
||||
or job.get('interactive_input') or job.get('native_lm_verify') or job.get('mapped_remote_root')
|
||||
or grant.get('desktop') != job.get('desktop') or not str(job.get('desktop', '')).startswith('CbbVerify_')
|
||||
or not _same(grant['root'], job['root']) or grant.get('block') != job.get('block_name')
|
||||
or not all(_same(grant[key], job[field]) for key, field in (
|
||||
('library', 'lmc_file'), ('project', 'project'), ('pcb', 'pcb')))):
|
||||
raise ValueError('POST_REPAIR_PROOF_INVALID')
|
||||
target = RepairTarget(Path(grant['repair_project']), Path(grant['repair_database']))
|
||||
if not native_success_confirmed(target, grant['repair']):
|
||||
raise ValueError('POST_REPAIR_PROOF_INVALID: 原生成功证据不完整')
|
||||
return grant
|
||||
|
||||
|
||||
def prepare_launch(job, command):
|
||||
"""Called on the private worker immediately before direct PCB launch."""
|
||||
import win32file, win32con
|
||||
from .icdb_ownership import database_directories, unreleased_addresses
|
||||
from .management import _write_json
|
||||
grant = _validate_grant(job)
|
||||
from .native_repair_ui import _current_desktop_name
|
||||
if _current_desktop_name() != job['desktop']:
|
||||
raise ValueError('POST_REPAIR_PRIVATE_DESKTOP_REQUIRED')
|
||||
library, project, pcb = (Path(job[key]) for key in ('lmc_file', 'project', 'pcb'))
|
||||
from .maintenance_entry import maintenance_project_files
|
||||
for path in (library, pcb, *maintenance_project_files(project, pcb)):
|
||||
require_local_fixed(path)
|
||||
if len(command) != 3 or not _same(command[1], pcb) or command[2] != '-ReusableCircuit':
|
||||
raise ValueError('POST_REPAIR_LAUNCH_MISMATCH')
|
||||
def require_idle():
|
||||
# Self is an expected automation worker, never a native owner.
|
||||
others = [r for r in _process_records() if r[0] not in (os.getpid(), int(job['supervisor_pid']))
|
||||
and not (r[1].casefold() in ('python.exe', 'pythonw.exe', 'cbbrefreshverify.exe')
|
||||
and '--dialog-agent' in r[3] and r[4] == int(job['supervisor_pid']))]
|
||||
if others: raise ValueError('POST_REPAIR_FOREIGN_OWNER: 存在其他原生进程或助手')
|
||||
require_idle()
|
||||
if unreleased_addresses(library, project, pcb): raise ValueError('POST_REPAIR_ADDRESS_REMAINS')
|
||||
files = [library, pcb.parent/'Layout/LayoutDB.lyt']
|
||||
files.extend(folder/'icdb.dat' for folder in database_directories(library, project, pcb) if (folder/'icdb.dat').is_file())
|
||||
with ExitStack() as stack:
|
||||
for path in dict.fromkeys(files):
|
||||
require_local_fixed(path)
|
||||
try:
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
except Exception as error:
|
||||
raise ValueError(f'POST_REPAIR_EXCLUSIVE_ACCESS_REQUIRED: {path}: {error}') from error
|
||||
stack.callback(handle.Close)
|
||||
if unreleased_addresses(library, project, pcb): raise ValueError('POST_REPAIR_ADDRESS_CHANGED')
|
||||
require_idle()
|
||||
_write_json(Path(job['root'])/'post-repair-prelaunch.json', {
|
||||
'nonce': grant['nonce'], 'desktop': job['desktop'], 'project': str(project), 'pcb': str(pcb),
|
||||
'library': str(library), 'worker_pid': os.getpid(), 'supervisor_pid': job['supervisor_pid'],
|
||||
'command': list(command), 'exclusive_files': [str(path) for path in dict.fromkeys(files)],
|
||||
'addresses': [], 'monotonic': time.monotonic()})
|
||||
|
||||
|
||||
def exact_prompt(snapshot):
|
||||
from .dialog_models import DialogPhase
|
||||
if (snapshot.phase != DialogPhase.PCB_STARTING or snapshot.process_name.casefold() != 'expeditionpcb.exe'
|
||||
or snapshot.process_role != 'pcb' or snapshot.window_class != '#32770' or snapshot.title != 'Expedition PCB'):
|
||||
return False
|
||||
controls = []
|
||||
for control in snapshot.controls:
|
||||
self_window = (control.handle == snapshot.hwnd and control.control_id == 0
|
||||
and control.class_name == snapshot.window_class and control.name == snapshot.title
|
||||
and control.control_type == 'Window' and control.patterns == ('Window',))
|
||||
uia_chrome = (control.handle == 0 and control.control_id == 0
|
||||
and control.class_name == '' and control.automation_id == ''
|
||||
and not control.selected and not control.checked and (
|
||||
control.control_type == 'TitleBar' and control.name == '' and control.patterns == ('Value',)
|
||||
or control.control_type == 'Button' and not control.enabled
|
||||
and control.name in ('关闭', 'Close') and control.patterns == ('Invoke',)))
|
||||
if not self_window and not uia_chrome:
|
||||
controls.append(control)
|
||||
buttons = [c for c in controls if c.class_name == 'Button']
|
||||
static = [c for c in controls if c.class_name == 'Static' and c.name.strip()]
|
||||
return (len(buttons) == 2 and {c.control_id for c in buttons} == {6, 7}
|
||||
and all(c.enabled for c in buttons) and len(static) == 1 and static[0].control_id == 65535
|
||||
and ' '.join(static[0].name.split()) == ' '.join(PROMPT_TEXT.split())
|
||||
and all(c.class_name == 'Button' or c.class_name == 'Static' and c.control_id in (20, 65535)
|
||||
for c in controls))
|
||||
|
||||
|
||||
def _validate_current_processes(job, prelaunch, target_pid, launcher_pid, worker_pid, companion_pids):
|
||||
from .mentor_adapter import is_reusable_circuit_pcb
|
||||
rows = _process_records()
|
||||
by_pid = {r[0]: r for r in rows}
|
||||
if target_pid not in by_pid or launcher_pid not in by_pid:
|
||||
raise ValueError('POST_REPAIR_PROCESS_UNCONFIRMED')
|
||||
target, launcher = by_pid[target_pid], by_pid[launcher_pid]
|
||||
executable = Path(prelaunch['command'][0])
|
||||
expected_worker = executable.parents[3]/'wg/win32/bin/ExpeditionPCB.exe'
|
||||
if (not _same(launcher[2], executable) or not _same(target[2], expected_worker)
|
||||
or launcher[4] != worker_pid or target[4] != launcher_pid
|
||||
or not is_reusable_circuit_pcb(target[2], target[3], job['pcb'])
|
||||
or not is_reusable_circuit_pcb(launcher[2], launcher[3], job['pcb'])):
|
||||
raise ValueError('POST_REPAIR_PROCESS_IDENTITY_MISMATCH')
|
||||
allowed = {target_pid, launcher_pid, worker_pid, int(job['supervisor_pid']), *companion_pids}
|
||||
foreign = [r for r in rows if r[0] not in allowed]
|
||||
if foreign:
|
||||
from .management import _write_json
|
||||
_write_json(Path(job['root'])/'foreign-owners.json', {'allowed_pids': sorted(allowed),
|
||||
'observed': [{'pid': r[0], 'name': r[1], 'executable': r[2], 'command': r[3], 'parent_pid': r[4]}
|
||||
for r in foreign]})
|
||||
raise ValueError(f'POST_REPAIR_FOREIGN_OWNER: {[(r[0], r[1]) for r in foreign]}')
|
||||
|
||||
|
||||
class ConsentController:
|
||||
def __init__(self, proof, job):
|
||||
if not isinstance(proof, PostRepairProof): raise ValueError('POST_REPAIR_PROOF_REQUIRED')
|
||||
self.grant = proof.for_job(job)
|
||||
self.job = json.loads(json.dumps(job))
|
||||
self.used = False
|
||||
self.snapshot = None
|
||||
self.sent_at = 0.0
|
||||
|
||||
def handle(self, snapshot, snapshots, *, target_pid, launcher_pid, worker_pid, companion_pids, perform):
|
||||
from .dialog_supervisor import DialogPollResult
|
||||
from .management import _write_json
|
||||
try:
|
||||
if not exact_prompt(snapshot) or snapshot.pid != target_pid:
|
||||
raise ValueError('POST_REPAIR_PROMPT_MISMATCH')
|
||||
if len(snapshots) != 1:
|
||||
raise ValueError('POST_REPAIR_ADDITIONAL_DIALOG')
|
||||
if self.used:
|
||||
if (self.snapshot.hwnd == snapshot.hwnd and self.snapshot.signature == snapshot.signature
|
||||
and time.monotonic() - self.sent_at < 2):
|
||||
return DialogPollResult(activity=False, observed_snapshots=tuple(snapshots))
|
||||
raise ValueError('POST_REPAIR_CONSENT_ALREADY_USED')
|
||||
pre = json.loads((Path(self.job['root'])/'post-repair-prelaunch.json').read_text(encoding='utf-8'))
|
||||
required = {Path(self.grant['library']).resolve(),
|
||||
(Path(self.grant['pcb']).parent/'Layout/LayoutDB.lyt').resolve(),
|
||||
(Path(self.grant['repair_database'])/'icdb.dat').resolve()}
|
||||
if (pre.get('nonce') != self.grant['nonce'] or pre.get('desktop') != self.job['desktop']
|
||||
or pre.get('worker_pid') != worker_pid or pre.get('addresses') != []
|
||||
or pre.get('supervisor_pid') != self.job['supervisor_pid']
|
||||
or not required.issubset({Path(p).resolve() for p in pre.get('exclusive_files', [])})
|
||||
or not 0 <= time.monotonic() - pre.get('monotonic', 0) <= 180
|
||||
or not all(_same(pre[key], self.grant[key]) for key in ('project', 'pcb', 'library'))):
|
||||
raise ValueError('POST_REPAIR_PRELAUNCH_PROOF_INVALID')
|
||||
for path in (self.grant['library'], self.grant['project'], self.grant['pcb']): require_local_fixed(path)
|
||||
_validate_current_processes(self.job, pre, target_pid, launcher_pid, worker_pid, companion_pids)
|
||||
payload = {'action': 'post_repair_local_read_write', 'channels': ['win32_button_click'],
|
||||
'target': {'control_id': 6}, 'postcondition': 'window_gone',
|
||||
'desktop': self.job['desktop'], 'pid': target_pid}
|
||||
# Consume before messaging: failure/timeout cannot authorize another click.
|
||||
self.used = True; self.snapshot = snapshot; self.sent_at = time.monotonic()
|
||||
_write_json(Path(self.job['root'])/'post-repair-consent.json', {
|
||||
'nonce': self.grant['nonce'], 'consumed': True, 'target_pid': target_pid,
|
||||
'launcher_pid': launcher_pid, 'worker_pid': worker_pid, 'snapshot': snapshot.to_record()})
|
||||
result = perform(snapshot, payload, snapshot.phase)
|
||||
return DialogPollResult(activity=True, action_sent=bool(result.accepted_controls), snapshot=snapshot,
|
||||
error_code=result.error_code, detail=result.detail, accepted_controls=result.accepted_controls,
|
||||
action=payload['action'], channel=result.channel, rule_id='explicit-local-post-repair-consent',
|
||||
observed_snapshots=tuple(snapshots))
|
||||
except Exception as error:
|
||||
return DialogPollResult(error_code='DATABASE_RECOVERY_REQUIRED', detail=str(error),
|
||||
snapshot=snapshot, observed_snapshots=tuple(snapshots))
|
||||
|
||||
|
||||
def validate_action_window(snapshot, action):
|
||||
"""Final check on the agent's actual private desktop, immediately before Yes."""
|
||||
import win32gui, win32process
|
||||
from .native_repair_ui import _current_desktop_name
|
||||
from .dialog_models import ControlSnapshot
|
||||
if (not exact_prompt(snapshot) or snapshot.pid != action.get('pid')
|
||||
or _current_desktop_name() != action.get('desktop')
|
||||
or not str(action.get('desktop', '')).startswith('CbbVerify_')
|
||||
or win32process.GetWindowThreadProcessId(snapshot.hwnd)[1] != snapshot.pid
|
||||
or win32gui.GetClassName(snapshot.hwnd) != snapshot.window_class
|
||||
or win32gui.GetWindowText(snapshot.hwnd) != snapshot.title or not win32gui.IsWindowVisible(snapshot.hwnd)):
|
||||
raise ValueError('POST_REPAIR_LIVE_WINDOW_MISMATCH')
|
||||
children = []
|
||||
win32gui.EnumChildWindows(snapshot.hwnd, lambda hwnd, _: children.append(int(hwnd)), None)
|
||||
live = tuple(ControlSnapshot(hwnd, win32gui.GetDlgCtrlID(hwnd) & 0xFFFF,
|
||||
win32gui.GetClassName(hwnd), win32gui.GetWindowText(hwnd), enabled=bool(win32gui.IsWindowEnabled(hwnd)))
|
||||
for hwnd in children)
|
||||
from dataclasses import replace
|
||||
if not exact_prompt(replace(snapshot, controls=live)):
|
||||
raise ValueError('POST_REPAIR_LIVE_PROMPT_CHANGED')
|
||||
yes = next(c for c in live if c.control_id == 6)
|
||||
if yes.handle != next(c.handle for c in snapshot.controls if c.control_id == 6):
|
||||
raise ValueError('POST_REPAIR_BUTTON_CHANGED')
|
||||
@@ -0,0 +1,75 @@
|
||||
"""Explicit copy-based recovery after failed maintenance; never auto-replay."""
|
||||
from dataclasses import asdict
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
def offers_reimport(result):
|
||||
return (result.status == 'FAIL' and result.evidence_path is not None
|
||||
and any(attempt.get('status') in {
|
||||
'REPAIR_VALIDATION_FAILED', 'MISSING_HEALTHY_BACKUP', 'NATIVE_REPAIR_FAILED'}
|
||||
for attempt in result.recovery_attempts))
|
||||
|
||||
|
||||
def run_reimport_recovery(requests, *, previous_evidence, evidence_dir,
|
||||
adapter=None, stop=None, progress=None):
|
||||
from .management import ManagementResult, _write_json, source_project, run_management_batch
|
||||
from .library_lock import release_retained_library
|
||||
requests = tuple(requests)
|
||||
if not requests or stop is not None and stop.is_set():
|
||||
return []
|
||||
library = requests[0].library.resolve()
|
||||
evidence_dir = Path(evidence_dir).resolve()
|
||||
if evidence_dir.is_relative_to(library.parent) or library.parent.is_relative_to(evidence_dir):
|
||||
raise ValueError('恢复记录必须位于目标库之外。')
|
||||
root = evidence_dir/('reimport-' + uuid4().hex)
|
||||
root.mkdir(parents=True)
|
||||
report = root/'recovery.json'
|
||||
record = {'library': str(library), 'previous_evidence': previous_evidence,
|
||||
'requests': [asdict(r) for r in requests], 'original_repair_succeeded': False}
|
||||
stage = 'preflight'
|
||||
def save(stage, **extra):
|
||||
record.update(stage=stage, **extra)
|
||||
_write_json(report, record)
|
||||
def cancelled():
|
||||
if stop is not None and stop.is_set():
|
||||
save('cancelled', management_started=False)
|
||||
return True
|
||||
return False
|
||||
try:
|
||||
save(stage, management_started=False)
|
||||
for request in requests:
|
||||
if (request.operation != 'update' or request.library.resolve() != library
|
||||
or not previous_evidence.get(request.name)):
|
||||
raise ValueError('恢复目标与原失败项不一致。')
|
||||
source, _, _ = source_project(request.source, request.top_level_block)
|
||||
if (source.parent.is_relative_to(library.parent)
|
||||
or library.parent.is_relative_to(source.parent)):
|
||||
raise ValueError('请选择目标中心库之外的完整工程副本。')
|
||||
if cancelled(): return []
|
||||
stage = 'release_check'
|
||||
save(stage)
|
||||
if progress: progress({'stage': stage, 'detail': '正在检查原生会话释放,尚未替换条目'})
|
||||
import inspect
|
||||
block_name = requests[0].name if len(requests) == 1 else None
|
||||
if 'block_name' in inspect.signature(release_retained_library).parameters:
|
||||
proof = release_retained_library(library, root/'release', block_name=block_name)
|
||||
else:
|
||||
proof = release_retained_library(library, root/'release')
|
||||
if proof.get('status') != 'RELEASE_CONFIRMED':
|
||||
raise ValueError('原生会话释放未确认,未开始导入。')
|
||||
save('released', release=proof)
|
||||
if cancelled(): return []
|
||||
stage = 'management'
|
||||
save(stage, management_started=True)
|
||||
results = run_management_batch(requests, adapter=adapter, stop=stop, progress=progress)
|
||||
save('complete', results=[asdict(result) for result in results],
|
||||
version_review_required=True)
|
||||
return results
|
||||
except Exception as error:
|
||||
save('release_failed' if stage == 'release_check' else 'failed', detail=str(error))
|
||||
guidance = ('请正常关闭提示中的原生程序;若提示数据库地址记录,请先对对应工程执行 ICDB 维护,再重试副本恢复。'
|
||||
if stage == 'release_check' else '')
|
||||
return [ManagementResult(r.operation, r.library, r.name, r.partition, r.source,
|
||||
r.auto_verify, stage, 'BLOCKED' if stage != 'management' else 'RECOVERY_REQUIRED',
|
||||
False, f'副本恢复未完成:{error}。{guidance}原修复失败记录仍保留。', report) for r in requests]
|
||||
@@ -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,5 @@
|
||||
Option Explicit
|
||||
Dim app, doc, pi, licensing, token, expected, attempt, stable, measuredStart, operationOK
|
||||
Dim app, doc, pi, licensing, token, expected, attempt, stable, measuredStart, operationOK, remaining
|
||||
expected = WScript.Arguments(0)
|
||||
Set app = GetObject(, "MGCPCB.Application")
|
||||
Set doc = app.ActiveDocument
|
||||
@@ -55,6 +55,13 @@ WScript.Echo "CLOSE_OK"
|
||||
Set pi = Nothing
|
||||
Set doc = Nothing
|
||||
Set licensing = Nothing
|
||||
Set remaining = app.ActiveDocument
|
||||
If Not remaining Is Nothing Then
|
||||
Set remaining = Nothing
|
||||
WScript.Echo "APP_RETAINED_OTHER_DOCUMENT"
|
||||
WScript.Quit 10
|
||||
End If
|
||||
Set remaining = Nothing
|
||||
measuredStart = Timer
|
||||
app.Quit
|
||||
LogElapsed "quit", measuredStart
|
||||
|
||||
@@ -0,0 +1,413 @@
|
||||
"""Complete, metadata-verified snapshots with scoped semantic SHA256 manifests.
|
||||
|
||||
Unknown engineering files are primary. Only explicit runtime, history and
|
||||
generated categories are ignored by semantic comparisons; they are still copied
|
||||
and their membership/size/mtime must remain stable throughout the snapshot.
|
||||
"""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextlib import ExitStack
|
||||
from dataclasses import asdict, dataclass
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import stat
|
||||
|
||||
|
||||
MANIFEST_VERSION = 1
|
||||
MANIFEST_RULE_VERSION = 1
|
||||
_ROOT_RUNTIME_PREFIX = 'work.before-restore-'
|
||||
_ROOT_RUNTIME_PATH = ('projectbackup', 'backups')
|
||||
_NATIVE_RUNTIME_DIRECTORIES = ('log', 'dbcheck', 'dbrestore')
|
||||
_NATIVE_SERVER_DIRECTORY = 'cdbsvr'
|
||||
_PROJECT_PCB_DIRECTORY = 'pcb'
|
||||
_PROJECT_DATABASE_PREFIXES = (('database',), (_PROJECT_PCB_DIRECTORY, 'database'))
|
||||
_NONPRIMARY_FILENAMES = ('saddress.adr',)
|
||||
_NONPRIMARY_FILENAME_PREFIXES = ('~$',)
|
||||
_NONPRIMARY_DIRECTORIES = frozenset({
|
||||
'log', 'logs', 'temp', 'tmp', '__pycache__', 'backup', 'backups',
|
||||
'output', 'outputs', 'report', 'reports', 'gerber', 'gerbers', 'odb++',
|
||||
'log files', 'logfiles',
|
||||
})
|
||||
_NONPRIMARY_SUFFIXES = frozenset({
|
||||
'.log', '.tmp', '.temp', '.lck', '.lock', '.bak', '.backup', '.pdf',
|
||||
})
|
||||
|
||||
|
||||
def _root_runtime(directories):
|
||||
"""Known runtime/history children of a library or project workspace root."""
|
||||
return bool(directories and (
|
||||
directories[0] in _NONPRIMARY_DIRECTORIES
|
||||
or directories[0].startswith(_ROOT_RUNTIME_PREFIX)
|
||||
or directories[:len(_ROOT_RUNTIME_PATH)] == _ROOT_RUNTIME_PATH))
|
||||
|
||||
|
||||
def _native_runtime(directories, database_prefix):
|
||||
runtime_prefix = database_prefix + (_NATIVE_SERVER_DIRECTORY,)
|
||||
return (directories[:len(runtime_prefix)] == runtime_prefix
|
||||
and len(directories) > len(runtime_prefix)
|
||||
and directories[len(runtime_prefix)] in _NATIVE_RUNTIME_DIRECTORIES)
|
||||
|
||||
|
||||
def _project_runtime(directories):
|
||||
return (_root_runtime(directories)
|
||||
or (directories[:1] == (_PROJECT_PCB_DIRECTORY,) and _root_runtime(directories[1:]))
|
||||
or any(_native_runtime(directories, prefix) for prefix in _PROJECT_DATABASE_PREFIXES))
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ManifestScope:
|
||||
"""Project root by default; library selects one CBB and shared dependencies."""
|
||||
|
||||
kind: str = 'project'
|
||||
block_name: str = ''
|
||||
library_name: str = ''
|
||||
|
||||
def __post_init__(self):
|
||||
if self.kind not in ('project', 'library'):
|
||||
raise ValueError('Unknown manifest scope')
|
||||
if self.kind == 'library':
|
||||
for value in (self.block_name, self.library_name):
|
||||
if not value or value in ('.', '..') or any(c in value for c in '/\\:'):
|
||||
raise ValueError('Library scope requires single-component block/library names')
|
||||
|
||||
@classmethod
|
||||
def library(cls, block_name, library_name):
|
||||
return cls('library', block_name, library_name)
|
||||
|
||||
def includes_directory(self, relative):
|
||||
parts = tuple(part.casefold() for part in Path(relative).parts)
|
||||
if self._runtime_path(parts, directory=True):
|
||||
return False
|
||||
if self.kind == 'project' or not parts or parts[0] != 'reusableblockslp':
|
||||
return True
|
||||
return ((len(parts) < 2 or parts[1] == self.block_name.casefold())
|
||||
and (len(parts) < 3 or parts[2] in ('work', 'cert')))
|
||||
|
||||
def _runtime_path(self, parts, *, directory=False):
|
||||
"""Recognize runtime locations only in their structural context.
|
||||
|
||||
Generic names such as ``Logs`` are runtime when they are a project-root
|
||||
or selected Work/Cert child. The same name deeper in an engineering
|
||||
partition (or in shared SymbolLibs data) remains primary.
|
||||
"""
|
||||
directories = parts if directory else parts[:-1]
|
||||
if not directory and (Path(parts[-1]).suffix in _NONPRIMARY_SUFFIXES
|
||||
or parts[-1] in _NONPRIMARY_FILENAMES
|
||||
or parts[-1].startswith(_NONPRIMARY_FILENAME_PREFIXES)):
|
||||
return True
|
||||
if self.kind == 'project':
|
||||
return _project_runtime(directories)
|
||||
if directories[:1] == ('reusableblockslp',):
|
||||
# Identity and Work/Cert role components never act as runtime names.
|
||||
if (len(directories) >= 3 and directories[1] == self.block_name.casefold()
|
||||
and directories[2] in ('work', 'cert')):
|
||||
return _project_runtime(directories[3:])
|
||||
return False
|
||||
if _root_runtime(directories):
|
||||
return True
|
||||
# Shared dependency names/partitions are engineering data. Only native
|
||||
# server runtime subtrees within known DB positions are excluded.
|
||||
if directories[:1] == ('symbollibs',) and len(directories) >= 2:
|
||||
return _native_runtime(directories, directories[:2])
|
||||
return bool(directories and _native_runtime(directories, directories[:1]))
|
||||
|
||||
def classify(self, relative):
|
||||
"""Return a semantic scope label or None; paths are relative to the root."""
|
||||
parts = tuple(part.casefold() for part in Path(relative).parts)
|
||||
if self._runtime_path(parts):
|
||||
return None
|
||||
if self.kind == 'project':
|
||||
return 'project'
|
||||
if parts[0] == 'reusableblockslp':
|
||||
if len(parts) < 4 or parts[1] != self.block_name.casefold():
|
||||
return None
|
||||
return {'work': 'target_work', 'cert': 'target_cert'}.get(parts[2])
|
||||
if len(parts) == 1:
|
||||
if Path(parts[0]).suffix == '.lmc' and parts[0] != self.library_name.casefold():
|
||||
return None
|
||||
return 'library_config'
|
||||
# Shared DB/config/dependency trees are conservative: new dependency
|
||||
# directory names remain primary without adding them to an allowlist.
|
||||
return 'shared_dependency'
|
||||
|
||||
|
||||
def comparison_rules(scope):
|
||||
"""Describe semantic exclusions using the classifier's rule constants."""
|
||||
project_positions = ['project root or its PCB child']
|
||||
project_native = ' or '.join('/'.join(prefix + (_NATIVE_SERVER_DIRECTORY,))
|
||||
for prefix in _PROJECT_DATABASE_PREFIXES)
|
||||
native_positions = [project_native + ' under project root']
|
||||
if scope.kind == 'library':
|
||||
project_positions = ['library root', 'selected CBB Work/Cert root or its PCB child']
|
||||
native_positions = [
|
||||
project_native + ' under selected CBB Work/Cert',
|
||||
'SymbolLibs/<partition>/' + _NATIVE_SERVER_DIRECTORY,
|
||||
'<shared dependency>/' + _NATIVE_SERVER_DIRECTORY,
|
||||
]
|
||||
return {
|
||||
'rule_version': MANIFEST_RULE_VERSION,
|
||||
'excluded_categories': sorted(_NONPRIMARY_DIRECTORIES),
|
||||
'excluded_suffixes': sorted(_NONPRIMARY_SUFFIXES),
|
||||
'runtime_exclusions': {
|
||||
'directory_name_positions': project_positions,
|
||||
'directory_prefixes': [_ROOT_RUNTIME_PREFIX],
|
||||
'relative_directories': ['/'.join(_ROOT_RUNTIME_PATH)],
|
||||
'native_directory_names': sorted(_NATIVE_RUNTIME_DIRECTORIES),
|
||||
'native_directory_positions': native_positions,
|
||||
'file_names': list(_NONPRIMARY_FILENAMES),
|
||||
'file_name_prefixes': list(_NONPRIMARY_FILENAME_PREFIXES),
|
||||
'coordination_checkpoints': 'recognized CBB coordination checkpoints',
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def hash_file(path):
|
||||
digest = hashlib.sha256()
|
||||
with Path(path).open('rb') as stream:
|
||||
for data in iter(lambda: stream.read(1024 * 1024), b''):
|
||||
digest.update(data)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _coordination_checkpoint(path):
|
||||
from .library_lock import checkpoint_record
|
||||
return checkpoint_record(path) is not None
|
||||
|
||||
|
||||
def scan_tree(root, *, scope=None, exclude=None, check=None):
|
||||
"""Refuse links/special files; optionally prune trees outside semantic scope.
|
||||
|
||||
Copies always call this without a scope, validating complete membership.
|
||||
Later semantic scans avoid entering runtime or unrelated CBB directories.
|
||||
"""
|
||||
root = Path(root)
|
||||
if root.lstat().st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError(f'不支持通过目录链接恢复:{root}')
|
||||
entries = {}
|
||||
folders = [root]
|
||||
while folders:
|
||||
if check is not None: check()
|
||||
with os.scandir(folders.pop()) as scanned:
|
||||
for entry in scanned:
|
||||
if check is not None: check()
|
||||
path = Path(entry.path)
|
||||
if exclude is not None and path.is_relative_to(exclude):
|
||||
continue
|
||||
if scope is not None:
|
||||
relative = path.relative_to(root)
|
||||
if entry.is_dir(follow_symlinks=False):
|
||||
if not scope.includes_directory(relative):
|
||||
continue
|
||||
elif scope.classify(relative) is None or _coordination_checkpoint(path):
|
||||
continue
|
||||
metadata = entry.stat(follow_symlinks=False)
|
||||
if metadata.st_file_attributes & stat.FILE_ATTRIBUTE_REPARSE_POINT:
|
||||
raise ValueError(f'目录含文件或目录链接,停止恢复:{entry.path}')
|
||||
if not (stat.S_ISDIR(metadata.st_mode) or stat.S_ISREG(metadata.st_mode)):
|
||||
raise ValueError(f'不支持的特殊文件:{entry.path}')
|
||||
entries[path] = metadata
|
||||
if stat.S_ISDIR(metadata.st_mode):
|
||||
folders.append(path)
|
||||
return dict(sorted(entries.items()))
|
||||
|
||||
|
||||
def _metadata_record(root, entries):
|
||||
return {path.relative_to(root).as_posix():
|
||||
('file', info.st_size, info.st_mtime_ns) if stat.S_ISREG(info.st_mode) else ('dir',)
|
||||
for path, info in entries.items()}
|
||||
|
||||
|
||||
def _manifest(scope, files):
|
||||
return {'version': MANIFEST_VERSION, 'scope': asdict(scope), 'files': dict(sorted(files.items()))}
|
||||
|
||||
|
||||
def _emit(progress, stage, hashed_files, hashed_bytes, **extra):
|
||||
if progress:
|
||||
progress({'stage': stage, 'hashed_files': hashed_files, 'hashed_bytes': hashed_bytes, **extra})
|
||||
|
||||
|
||||
def build_manifest(root, *, scope=None, exclude=None, progress=None, hash_function=hash_file):
|
||||
"""Hash primary files only. Excluded subtrees are for private incoming Work.
|
||||
|
||||
The returned dict is versioned, JSON-compatible and relocatable. Equality
|
||||
compares primary path membership, size, hash and scope, never runtime mtimes.
|
||||
"""
|
||||
root = Path(root)
|
||||
scope = scope or ManifestScope()
|
||||
entries = scan_tree(root, scope=scope, exclude=exclude)
|
||||
selected = {path: scope.classify(path.relative_to(root)) for path, info in entries.items()
|
||||
if stat.S_ISREG(info.st_mode)}
|
||||
selected = {path: label for path, label in selected.items() if label is not None}
|
||||
def hashed(path):
|
||||
digest = hash_function(path)
|
||||
after = path.stat()
|
||||
before = entries[path]
|
||||
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
|
||||
raise OSError(f'计算哈希期间文件变化:{path}')
|
||||
return path.relative_to(root).as_posix(), {
|
||||
'size': before.st_size, 'sha256': digest, 'scope': selected[path]}
|
||||
files = {}
|
||||
hashed_bytes = 0
|
||||
_emit(progress, 'hash', 0, 0, total_files=len(selected))
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
for count, (relative, item) in enumerate(pool.map(hashed, selected), 1):
|
||||
files[relative] = item
|
||||
hashed_bytes += item['size']
|
||||
if count % 256 == 0 or count == len(selected):
|
||||
_emit(progress, 'hash', count, hashed_bytes, total_files=len(selected))
|
||||
# Check primary membership and metadata again after every hash completed.
|
||||
# Runtime files can change/add/disappear without a semantic conflict.
|
||||
after = scan_tree(root, scope=scope, exclude=exclude)
|
||||
before_primary = {p: entries[p] for p in selected}
|
||||
after_primary = {p: info for p, info in after.items() if stat.S_ISREG(info.st_mode)
|
||||
and (exclude is None or not p.is_relative_to(exclude))
|
||||
and scope.classify(p.relative_to(root)) is not None}
|
||||
if _metadata_record(root, before_primary) != _metadata_record(root, after_primary):
|
||||
raise OSError(f'计算哈希期间主要文件变化:{root}')
|
||||
return _manifest(scope, files)
|
||||
|
||||
|
||||
def build_exclusive_library_manifest(root, *, scope, progress=None):
|
||||
"""Check every native database is idle while hashing, without making a copy."""
|
||||
import win32con, win32file, pywintypes
|
||||
root = Path(root)
|
||||
entries = scan_tree(root) # Preserve full-library link and ownership checks.
|
||||
with ExitStack() as stack:
|
||||
handles = {}
|
||||
for path, metadata in entries.items():
|
||||
if stat.S_ISREG(metadata.st_mode) and (path.suffix.casefold() == '.lmc'
|
||||
or path.name.casefold() == 'icdb.dat'):
|
||||
try:
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
except pywintypes.error as error:
|
||||
raise OSError(f'LIBRARY_DATABASE_IN_USE: 无法独占读取 {path}:{error}') from error
|
||||
handles[path] = handle
|
||||
stack.callback(handle.Close)
|
||||
|
||||
def digest(path):
|
||||
if path not in handles:
|
||||
return hash_file(path)
|
||||
value = hashlib.sha256()
|
||||
while True:
|
||||
try:
|
||||
_, data = win32file.ReadFile(handles[path], 1024 * 1024)
|
||||
except pywintypes.error as error:
|
||||
if error.winerror == 38: # ERROR_HANDLE_EOF
|
||||
break
|
||||
raise
|
||||
if not data:
|
||||
break
|
||||
value.update(data)
|
||||
return value.hexdigest()
|
||||
|
||||
return build_manifest(root, scope=scope, progress=progress, hash_function=digest)
|
||||
|
||||
|
||||
def copy_verified(source, destination, *, scope=None, progress=None, hash_function=hash_file):
|
||||
"""Copy the whole tree, validate all metadata, SHA256 only scoped primary files.
|
||||
|
||||
Native icdb.dat/LMC files are held with exclusive read handles for the copy.
|
||||
Their expected SHA256 comes from the source read stream, independently of
|
||||
hashing destination bytes. Unlocked primary files are also hashed while
|
||||
streaming the copy, then rehashed at source to detect even same-size/mtime
|
||||
edits. Every primary destination is independently hashed. Failures retain
|
||||
the partial destination evidence.
|
||||
Progress receives one dict with stage, hashed_files/bytes and copied_files.
|
||||
"""
|
||||
import win32con
|
||||
import win32file
|
||||
|
||||
source, destination = Path(source), Path(destination)
|
||||
scope = scope or ManifestScope()
|
||||
if destination.resolve().is_relative_to(source.resolve()):
|
||||
raise ValueError('复制目标不能位于源目录内。')
|
||||
entries = scan_tree(source)
|
||||
expected_metadata = _metadata_record(source, entries)
|
||||
destination.mkdir(parents=True, exist_ok=False)
|
||||
files = [path for path, info in entries.items() if stat.S_ISREG(info.st_mode)]
|
||||
from .library_lock import owned_snapshot
|
||||
# Resolve ownership on the operation thread, before parallel copy workers.
|
||||
owned = {path: data for path in files
|
||||
if path.name.endswith('.cbb-upload.lock')
|
||||
and (data := owned_snapshot(path)) is not None}
|
||||
record = {}
|
||||
hashed_count = hashed_bytes = 0
|
||||
_emit(progress, 'copy', 0, 0, copied_files=0, total_files=len(files))
|
||||
with ExitStack() as stack:
|
||||
locks = {}
|
||||
for path in files:
|
||||
if path.name.casefold() == 'icdb.dat' or path.suffix.casefold() == '.lmc':
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
stack.callback(handle.Close)
|
||||
locks[path] = handle
|
||||
for path, info in entries.items():
|
||||
if stat.S_ISDIR(info.st_mode):
|
||||
(destination/path.relative_to(source)).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def copy_file(path):
|
||||
relative = path.relative_to(source)
|
||||
copied = destination/relative
|
||||
copied.parent.mkdir(parents=True, exist_ok=True)
|
||||
label = None if _coordination_checkpoint(path) else scope.classify(relative)
|
||||
digest = None
|
||||
if path in owned:
|
||||
copied.write_bytes(owned[path])
|
||||
shutil.copystat(path, copied)
|
||||
if label is not None:
|
||||
digest = hashlib.sha256(owned[path]).hexdigest()
|
||||
elif path in locks:
|
||||
stream_hash = hashlib.sha256() if label is not None else None
|
||||
with copied.open('wb') as stream:
|
||||
while True:
|
||||
_, data = win32file.ReadFile(locks[path], 1024 * 1024)
|
||||
if not data:
|
||||
break
|
||||
if stream_hash is not None:
|
||||
stream_hash.update(data)
|
||||
stream.write(data)
|
||||
shutil.copystat(path, copied)
|
||||
if stream_hash is not None:
|
||||
digest = stream_hash.hexdigest()
|
||||
elif label is not None:
|
||||
stream_hash = hashlib.sha256()
|
||||
with path.open('rb') as incoming, copied.open('wb') as outgoing:
|
||||
for data in iter(lambda: incoming.read(1024 * 1024), b''):
|
||||
stream_hash.update(data)
|
||||
outgoing.write(data)
|
||||
shutil.copystat(path, copied)
|
||||
digest = stream_hash.hexdigest()
|
||||
else:
|
||||
shutil.copy2(path, copied)
|
||||
before, after = entries[path], path.stat()
|
||||
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
|
||||
raise OSError(f'复制期间文件变化:{path}')
|
||||
return relative.as_posix(), digest, label
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
for count, (relative, digest, label) in enumerate(pool.map(copy_file, files), 1):
|
||||
if label is not None:
|
||||
size = entries[source/relative].st_size
|
||||
record[relative] = {'size': size, 'sha256': digest, 'scope': label}
|
||||
hashed_count += 1
|
||||
hashed_bytes += size
|
||||
if count % 256 == 0 or count == len(files):
|
||||
_emit(progress, 'copy', hashed_count, hashed_bytes,
|
||||
copied_files=count, total_files=len(files))
|
||||
|
||||
def validate_bytes(relative):
|
||||
item = record[relative]
|
||||
path = source/relative
|
||||
if path not in locks and hash_function(path) != item['sha256']:
|
||||
raise OSError(f'复制期间主要文件内容变化:{path}')
|
||||
if hash_function(destination/relative) != item['sha256']:
|
||||
raise OSError(f'复制内容校验失败:{path}')
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
list(pool.map(validate_bytes, record))
|
||||
if _metadata_record(source, scan_tree(source)) != expected_metadata:
|
||||
raise OSError(f'复制期间目录内容变化:{source}')
|
||||
if _metadata_record(destination, scan_tree(destination)) != expected_metadata:
|
||||
raise OSError(f'复制目标目录或文件元数据不一致:{destination}')
|
||||
return _manifest(scope, record)
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Shared wording for user-managed backups before write operations."""
|
||||
|
||||
MANUAL_BACKUP_NOTICE = (
|
||||
'应用不会自动备份。请先手动备份中心库及相关工程,并确认备份可用于恢复。'
|
||||
)
|
||||
@@ -1,11 +1,13 @@
|
||||
"""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,
|
||||
QHBoxLayout, QLabel, QLineEdit, QPushButton)
|
||||
QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox)
|
||||
|
||||
from .workers import ServiceWorker
|
||||
from .backup_notice import MANUAL_BACKUP_NOTICE
|
||||
|
||||
|
||||
class RecoverySignals(QObject):
|
||||
@@ -22,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)
|
||||
@@ -37,7 +41,10 @@ class BackupRecoveryDialog(QDialog):
|
||||
self.restore_button.clicked.connect(self._restore)
|
||||
self.close_button = QPushButton('关闭')
|
||||
self.close_button.clicked.connect(self.reject)
|
||||
self.status_label = QLabel('使用所选备份版本恢复同名 CBB。先在库副本中验证,原工程与完整库副本会保留;需要额外磁盘空间。')
|
||||
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)
|
||||
layout.addRow('目标 CBB', self.block_combo)
|
||||
@@ -49,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)
|
||||
|
||||
@@ -77,12 +85,30 @@ class BackupRecoveryDialog(QDialog):
|
||||
except Exception as error:
|
||||
self.status_label.setText(str(error))
|
||||
return
|
||||
if QMessageBox.question(self, '确认从备份恢复',
|
||||
f'{MANUAL_BACKUP_NOTICE}\n\n目标库:{request.lmc_file}\nCBB:{name}\n来源:{source}'
|
||||
'\n\n将创建用于校验的隔离库副本,校验通过后替换目标工程。确认继续?',
|
||||
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)
|
||||
@@ -95,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))
|
||||
@@ -115,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)
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,250 @@
|
||||
"""Explicit preview and asynchronous, checkpointed CBB management batch."""
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
from PySide6.QtCore import QObject, QThreadPool, Signal, Qt
|
||||
from PySide6.QtWidgets import (QCheckBox, QDialog, QFileDialog, QHBoxLayout, QLabel,
|
||||
QMessageBox, QPushButton, QTableWidget, QTableWidgetItem, QTextEdit, QVBoxLayout)
|
||||
|
||||
from .workers import ServiceWorker
|
||||
from .backup_notice import MANUAL_BACKUP_NOTICE
|
||||
from ..management import run_management_batch, validate_name
|
||||
|
||||
RESULT_LABELS = {
|
||||
'REMOVED': '已移除', 'UPLOADED': '已上传 / 未自动校验',
|
||||
'UPLOADED_VERIFIED': '已上传且校验通过', 'UPLOADED_VERIFY_FAILED': '已上传但校验未通过',
|
||||
'UPLOAD_FAILED': '上传失败', 'RECOVERY_REQUIRED': '需要人工恢复',
|
||||
'BLOCKED': '操作被阻止', 'UNSUPPORTED': '当前环境不支持',
|
||||
}
|
||||
STAGE_LABELS = {
|
||||
'prepare_identity': '正在核对来源工程内容',
|
||||
'catalog_before': '正在核对原生目录', 'baseline': '正在记录中心库内容基线',
|
||||
'stage_source': '正在复制并检查来源工程', 'native_remove': '正在移除',
|
||||
'removed': '原条目已移除', 'native_add': '正在上传', 'uploaded': '上传已确认',
|
||||
'verify': '正在校验上传结果', 'verified': '校验已返回', 'copy': '正在准备来源副本',
|
||||
'hash': '正在校验主要文件',
|
||||
'release_check': '正在检查工程是否被占用',
|
||||
'previous_release_check': '正在复核历史占用',
|
||||
'previous_release_confirmed': '历史会话已确认释放',
|
||||
'previous_release_unconfirmed': '占用复核未通过',
|
||||
}
|
||||
|
||||
|
||||
class ManagementSignals(QObject):
|
||||
progress = Signal(object)
|
||||
|
||||
|
||||
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)
|
||||
self.templates = tuple(requests)
|
||||
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:
|
||||
from ..reimport_recovery import run_reimport_recovery
|
||||
self.runner = lambda requests, **kwargs: run_reimport_recovery(requests,
|
||||
previous_evidence=self.recovery_from,
|
||||
evidence_dir=self.template.evidence_dir.parent/'reimport-recovery', **kwargs)
|
||||
self.busy = False
|
||||
self.executed = False
|
||||
self.stop = Event()
|
||||
self.pool = QThreadPool(self); self.pool.setMaxThreadCount(1)
|
||||
self.signals = ManagementSignals(self)
|
||||
self.signals.progress.connect(self._progress)
|
||||
self.setWindowTitle({'add': '新增 CBB', 'update': '同名更新 CBB', 'remove': '移除 CBB'}[self.operation])
|
||||
self.resize(850, 450)
|
||||
self.table = QTableWidget(0, 4)
|
||||
self.table.setHorizontalHeaderLabels(['CBB 名称', '分区', '来源 PRJ(完整工程)', '顶层块(空白时读取工程)'])
|
||||
self.table.horizontalHeader().setStretchLastSection(True)
|
||||
self.table.setColumnWidth(2, 300)
|
||||
self.sources_button = QPushButton('选择来源工程…')
|
||||
self.sources_button.setVisible(self.operation != 'remove')
|
||||
self.sources_button.clicked.connect(self._browse)
|
||||
self.auto_verify = QCheckBox('本批次上传后自动校验')
|
||||
self.auto_verify.setChecked(self.template.auto_verify)
|
||||
self.auto_verify.setVisible(self.operation != 'remove')
|
||||
self.status_label = QLabel(MANUAL_BACKUP_NOTICE + ' 更新会先移除再重新导入,失败时请使用手动备份恢复。')
|
||||
self.status_label.setWordWrap(True)
|
||||
self.output = QTextEdit(); self.output.setReadOnly(True)
|
||||
self.output.document().setMaximumBlockCount(2000)
|
||||
self.output.hide()
|
||||
self.logs_button = QPushButton('查看日志')
|
||||
self.logs_button.setCheckable(True)
|
||||
self.logs_button.toggled.connect(self.output.setVisible)
|
||||
if self.recovery_from:
|
||||
self.setWindowTitle('从副本恢复 CBB')
|
||||
self.status_label.setText('修复未完成。请选择完整工程副本(.prj)。所列 CBB 将替换为副本中的版本。\n'
|
||||
+ MANUAL_BACKUP_NOTICE)
|
||||
self.output.append('\n'.join(f'原失败记录 {name}:{path}' for name, path in self.recovery_from.items()))
|
||||
self.execute_button = QPushButton('预览并确认执行…')
|
||||
self.execute_button.clicked.connect(self._execute)
|
||||
self.stop_button = QPushButton('当前项结束后停止'); self.stop_button.setEnabled(False)
|
||||
self.stop_button.clicked.connect(self.stop.set)
|
||||
self.close_button = QPushButton('关闭'); self.close_button.clicked.connect(self.reject)
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addWidget(QLabel(f'目标库:{self.template.library}'))
|
||||
layout.addWidget(self.table)
|
||||
layout.addWidget(self.sources_button); layout.addWidget(self.auto_verify)
|
||||
layout.addWidget(self.status_label); layout.addWidget(self.logs_button); layout.addWidget(self.output)
|
||||
buttons = QHBoxLayout()
|
||||
for widget in (self.execute_button, self.stop_button, self.close_button): buttons.addWidget(widget)
|
||||
layout.addLayout(buttons)
|
||||
for request in self.templates:
|
||||
if request.name:
|
||||
self._append(request)
|
||||
|
||||
def _append(self, request):
|
||||
row = self.table.rowCount(); self.table.insertRow(row)
|
||||
for column, value in enumerate((request.name, request.partition, str(request.source or ''), request.top_level_block)):
|
||||
item = QTableWidgetItem(value)
|
||||
if column == 1 or column == 0 and self.operation != 'add' or self.operation == 'remove':
|
||||
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
|
||||
self.table.setItem(row, column, item)
|
||||
|
||||
def add_sources(self, sources):
|
||||
if self.operation == 'add':
|
||||
for source in sources:
|
||||
source = Path(source)
|
||||
self._append(replace(self.template, name=source.stem, source=source))
|
||||
elif self.operation == 'update' and sources:
|
||||
row = max(0, self.table.currentRow())
|
||||
self.table.item(row, 2).setText(str(sources[0]))
|
||||
|
||||
def _browse(self):
|
||||
if self.operation == 'add':
|
||||
paths, _ = QFileDialog.getOpenFileNames(self, '选择完整来源工程', '', '工程 (*.prj)')
|
||||
else:
|
||||
path, _ = QFileDialog.getOpenFileName(self, '为当前行选择同名更新来源', '', '工程 (*.prj)')
|
||||
paths = [path] if path else []
|
||||
self.add_sources(paths)
|
||||
|
||||
def requests(self):
|
||||
result = []
|
||||
for row in range(self.table.rowCount()):
|
||||
name, partition, source, top = (self.table.item(row, i).text().strip() for i in range(4))
|
||||
result.append(replace(self.template, name=name, partition=partition,
|
||||
source=Path(source) if source else None, top_level_block=top,
|
||||
auto_verify=self.operation != 'remove' and self.auto_verify.isChecked()))
|
||||
return result
|
||||
|
||||
def _execute(self):
|
||||
if self.busy or self.executed:
|
||||
return
|
||||
requests = self.requests()
|
||||
try:
|
||||
if not requests:
|
||||
raise ValueError('请先选择来源工程。')
|
||||
for request in requests:
|
||||
validate_name(request.name)
|
||||
if request.operation != 'remove' and (request.source is None or not request.source.is_file()):
|
||||
raise ValueError(f'{request.name}:请选择完整来源 PRJ。')
|
||||
if len({(r.partition.casefold(), r.name.casefold()) for r in requests}) != len(requests):
|
||||
raise ValueError('批次包含重复目标名称。')
|
||||
except ValueError as error:
|
||||
self.status_label.setText(str(error)); return
|
||||
preview = '\n'.join(f'{r.partition} / {r.name}' + (f' ← {r.source}' if r.source else '') for r in requests)
|
||||
description = ('同名更新会原生移除并重新导入;导入失败需要使用手动备份恢复。' if self.operation == 'update'
|
||||
else '将通过原生操作移除所列 CBB;恢复时需要使用手动备份。' if self.operation == 'remove'
|
||||
else '新增同名冲突会拒绝覆盖,完整来源工程将先复制到暂存区。')
|
||||
if self.recovery_from:
|
||||
description = ('将用所选副本替换这些 CBB。操作会先移除原条目,再导入副本;'
|
||||
'导入失败时需要用备份恢复。请确认副本版本。')
|
||||
answer = QMessageBox.question(self, '确认管理操作',
|
||||
f'{MANUAL_BACKUP_NOTICE}\n{description}\n\n目标库:{self.template.library}\n{preview}\n\n'
|
||||
'确认继续执行?', QMessageBox.Yes | QMessageBox.No,
|
||||
QMessageBox.No)
|
||||
if answer != QMessageBox.Yes:
|
||||
return
|
||||
self.busy = True; self.executed = True
|
||||
self._enabled(False)
|
||||
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)
|
||||
self.pool.start(self.worker)
|
||||
|
||||
def _enabled(self, enabled):
|
||||
for widget in (self.table, self.sources_button, self.auto_verify, self.close_button):
|
||||
widget.setEnabled(enabled)
|
||||
self.execute_button.setEnabled(enabled and not self.executed)
|
||||
self.stop_button.setEnabled(not enabled)
|
||||
|
||||
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", "?")}'
|
||||
elif 'hashed_files' in event:
|
||||
text += f' {event["hashed_files"]}/{event.get("total_files", "?")} · {event.get("hashed_bytes", 0)} 字节'
|
||||
self.status_label.setText(text)
|
||||
|
||||
def _result(self, results):
|
||||
lines, summaries = [], []
|
||||
for result in results:
|
||||
summary = f'{result.name}:{RESULT_LABELS.get(result.status, result.status)}'
|
||||
text = f'{summary} — {result.detail}'
|
||||
changes = getattr(result, 'change_report', None)
|
||||
if changes:
|
||||
if changes.get('status') == 'RECORDED':
|
||||
text += (f'\n已比对的工程文件:新增 {len(changes["added"])}、删除 {len(changes["deleted"])}、'
|
||||
f'修改 {len(changes["modified"])};范围仅限报告列出的工程文件。')
|
||||
else:
|
||||
summary += ',变更清单待核对'
|
||||
text += '\n'+changes.get('detail', '变更清单未确认')
|
||||
if getattr(result, 'release_proof', None):
|
||||
text += '\n历史会话已释放;历史工程数据的恢复状态需另行核对。'
|
||||
if not result.safe_to_continue:
|
||||
summary += ',原生会话释放待确认'
|
||||
if self.recovery_from and result.status == 'UPLOADED_VERIFIED':
|
||||
summary += ',版本待核对。'
|
||||
elif result.status not in ('UPLOADED', 'UPLOADED_VERIFIED', 'REMOVED'):
|
||||
summary += ',详情请查看日志。'
|
||||
summaries.append(summary)
|
||||
if result.evidence_path:
|
||||
text += f'\n记录:{result.evidence_path}'
|
||||
lines.append(text)
|
||||
message = '\n'.join(lines) if lines else '队列已取消,未执行任何项。'
|
||||
self.status_label.setText('\n'.join(summaries) if summaries else message); self.output.append(message)
|
||||
self.completed.emit(results)
|
||||
|
||||
def _failed(self, error):
|
||||
from ..management import ManagementResult
|
||||
request = self.template
|
||||
self._result([ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', 'RECOVERY_REQUIRED', False, str(error))])
|
||||
|
||||
def _finished(self):
|
||||
self.busy = False; self._enabled(True)
|
||||
|
||||
def reject(self):
|
||||
if not self.busy:
|
||||
super().reject()
|
||||
|
||||
def closeEvent(self, event):
|
||||
if self.busy:
|
||||
self.stop.set(); event.ignore()
|
||||
else:
|
||||
super().closeEvent(event)
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Show the native version dates without preselecting a recovery version."""
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
|
||||
class PcbRestoreDialog(QMessageBox):
|
||||
def __init__(self, choice, parent=None):
|
||||
super().__init__(parent)
|
||||
self.choice = choice
|
||||
self.setWindowTitle('选择 PCB 恢复版本')
|
||||
self.setIcon(QMessageBox.Question)
|
||||
candidates = {c['kind']: c for c in choice.candidates}
|
||||
manual, auto = candidates['last_user_saved'], candidates['autosave']
|
||||
self.setText(f'CBB:{choice.block}\n中心库:{choice.library}\n\n'
|
||||
'原生程序检测到上次异常退出,需要选择 PCB 恢复版本。\n'
|
||||
f'最后手动保存:{manual["date"]}\n自动保存:{auto["date"]}\n\n'
|
||||
'选择历史版本可能丢失此版本之后的修改。选择后继续校验、保存和状态确认;恢复版本仍需核对。'
|
||||
'\n取消将停止本项并保留原生现场,不自动选取版本。')
|
||||
self.manual_button = self.addButton(f'最后手动保存({manual["date"]})', QMessageBox.ActionRole)
|
||||
self.auto_button = self.addButton(f'自动保存({auto["date"]})', QMessageBox.ActionRole)
|
||||
self.manual_button.setEnabled(manual['enabled'])
|
||||
self.auto_button.setEnabled(auto['enabled'])
|
||||
self.cancel_button = self.addButton(QMessageBox.Cancel)
|
||||
self.setDefaultButton(self.cancel_button)
|
||||
self.setEscapeButton(self.cancel_button)
|
||||
self.buttonClicked.connect(self._chosen)
|
||||
self.finished.connect(lambda _: choice.choose('cancel'))
|
||||
self.setModal(True)
|
||||
|
||||
def _chosen(self, button):
|
||||
self.choice.choose('last_user_saved' if button is self.manual_button else
|
||||
'autosave' if button is self.auto_button else 'cancel')
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -56,13 +73,16 @@ class VerifyRequest:
|
||||
# A reserved iCDB range is a host-wide prerequisite failure. Do not
|
||||
# launch a PCB that is guaranteed to time out; batch orchestration will
|
||||
# record this item and continue with the next one.
|
||||
strict_port_check: bool = True
|
||||
strict_port_check: bool = False
|
||||
# A mapped drive can be invisible to an elevated/private worker on Win10.
|
||||
# Preserve the parent session's mapping identity so the worker can restore
|
||||
# it before launching Library Manager/PCB.
|
||||
mapped_drive: str = ''
|
||||
mapped_remote_root: str = ''
|
||||
auto_repair: bool = True
|
||||
auto_repair: bool = False
|
||||
maintenance: bool = False
|
||||
post_repair_proof: object | None = field(default=None, repr=False, compare=False)
|
||||
diagnostic_only: bool = False
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -82,6 +102,7 @@ class VerifyResult:
|
||||
timings_seconds: dict[str, float] = field(default_factory=dict)
|
||||
incomplete_timing_phases: tuple[str, ...] = ()
|
||||
recovery_attempts: tuple[dict, ...] = ()
|
||||
release_proof: dict | None = None
|
||||
|
||||
|
||||
def resolve_importer(mentor_bin: str | None = None) -> Path:
|
||||
@@ -132,7 +153,8 @@ def classify_output(exit_code: int | None, output: str, *, timed_out: bool = Fal
|
||||
from .icdb_recovery import parse_session_loss
|
||||
if parse_session_loss(output):
|
||||
return 'FAIL'
|
||||
if exit_code == 0 and SUCCESS_TEXT.search(output) and not ERROR_TEXT.search(output):
|
||||
diagnostic_text = re.sub(r'\b0\s+errors?\b', '', output, flags=re.I)
|
||||
if exit_code == 0 and SUCCESS_TEXT.search(output) and not ERROR_TEXT.search(diagnostic_text):
|
||||
return 'PASS'
|
||||
if ERROR_TEXT.search(output):
|
||||
return 'FAIL'
|
||||
@@ -154,7 +176,8 @@ def preflight(request):
|
||||
if len(projects) != 1:
|
||||
raise ValueError(f'{name}: 缺少或无法唯一确定 Work 下的工程 PRJ')
|
||||
project = absolute_preserving_drive(projects[0])
|
||||
content = project.read_text(encoding='utf-8', errors='replace')
|
||||
from .backup_recovery import _project_text
|
||||
content = _project_text(project)
|
||||
refs = re.findall(r'^KEY PCBDesignPath "([^"]+)"', content, re.M)
|
||||
if not refs:
|
||||
raise ValueError(f'{name}: 工程没有 PCBDesignPath')
|
||||
@@ -217,7 +240,17 @@ def _stop_worker(process, *, timeout=5):
|
||||
try:
|
||||
process.wait(timeout=timeout)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
process.kill() # Automation clients only; never the native tree.
|
||||
process.wait(timeout=5)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if hasattr(process, 'stop_automation_clients'):
|
||||
process.stop_automation_clients()
|
||||
return process.poll() is not None
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _normalize_bridge_result(probe):
|
||||
@@ -262,9 +295,16 @@ def read_library_flag_with_retry(reader, library, block_name, *, attempts=3,
|
||||
def classify_failure_category(detail):
|
||||
text = str(detail or '')
|
||||
lower = text.casefold()
|
||||
if 'pcb_version_choice_required' in lower:
|
||||
return 'PCB_VERSION_CHOICE_REQUIRED'
|
||||
from .icdb_recovery import ERROR_CODE, parse_session_loss
|
||||
if ERROR_CODE.casefold() in lower or parse_session_loss(text):
|
||||
return ERROR_CODE
|
||||
if 'back annotation is not allowed on verified (or once verified) reusable blocks.' in lower:
|
||||
# Preserve the native integration failure even when shutdown adds a
|
||||
# secondary release warning. This category does not grant permission
|
||||
# to reimport or change safe_to_continue.
|
||||
return 'CBB_REIMPORT_REQUIRED'
|
||||
if any(code in lower for code in (
|
||||
'pcb_release_unconfirmed', 'icdb_release_unconfirmed',
|
||||
'icdb_session_unconfirmed', 'pcb_session_active', 'database_recovery_required')):
|
||||
@@ -470,7 +510,7 @@ def native_lm_verify_enabled():
|
||||
}
|
||||
|
||||
|
||||
def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline,
|
||||
def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline, launcher_pid=None,
|
||||
progress=None):
|
||||
"""Record native release or retention after automation has stopped."""
|
||||
from .mentor_adapter import cleanup_verify_processes
|
||||
@@ -489,12 +529,12 @@ def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline,
|
||||
except OSError:
|
||||
pass
|
||||
if progress:
|
||||
progress(event)
|
||||
_notify_progress(progress, event)
|
||||
|
||||
return cleanup_verify_processes(
|
||||
lm_pid,
|
||||
target_pid,
|
||||
None,
|
||||
launcher_pid,
|
||||
expected_pcb=pcb,
|
||||
baseline_pids=pcb_baseline,
|
||||
emit=emit,
|
||||
@@ -502,22 +542,50 @@ def _cleanup_session_processes(root, *, lm_pid, target_pid, pcb, pcb_baseline,
|
||||
|
||||
|
||||
def run_verify(request, *, progress=None, flag_reader=None, process_runner=None, port_check=None,
|
||||
dialog_supervisor_factory=None):
|
||||
dialog_supervisor_factory=None, version_prompt=None, stop=None):
|
||||
# Cover contents checking, native work and final state reads with one
|
||||
# session-wide mutex. The worker-stage lock below is reentrant on Windows.
|
||||
import win32event, win32con
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
mutex = win32event.CreateMutex(None, False, r'Local\CbbRefreshVerify795')
|
||||
acquired = False
|
||||
library_lock = None
|
||||
previous_release = None
|
||||
release_confirmed = True
|
||||
remaining_clients = ()
|
||||
def clients_released():
|
||||
if release_confirmed:
|
||||
return True
|
||||
from .library_lock import _supervisor_alive
|
||||
return bool(remaining_clients) and not any(_supervisor_alive(pid) for pid in remaining_clients)
|
||||
try:
|
||||
acquired = win32event.WaitForSingleObject(mutex, 0) in (
|
||||
win32con.WAIT_OBJECT_0, win32con.WAIT_ABANDONED)
|
||||
if not acquired:
|
||||
return VerifyResult('BLOCKED', stderr='已有 CBB 校验任务运行,请等待结束。',
|
||||
safe_to_continue=False)
|
||||
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, recheck_previous_release
|
||||
import pywintypes
|
||||
if Path(request.lmc_file).is_file():
|
||||
try:
|
||||
previous_release = recheck_previous_release(request.lmc_file,
|
||||
Path(request.evidence_dir)/'previous-release', block_name=request.block_name, progress=progress)
|
||||
library_lock = LibraryLock(request.lmc_file, checkpoint=False,
|
||||
safe_to_release=clients_released)
|
||||
library_lock.__enter__()
|
||||
except LibraryBusy as error:
|
||||
return VerifyResult('FAIL', stderr=str(error), error_category='LIBRARY_IN_USE')
|
||||
except (OSError, ValueError, RuntimeError, 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,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
dialog_supervisor_factory=dialog_supervisor_factory)
|
||||
dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt, stop=stop)
|
||||
if (request.auto_repair and result.status not in {'TIMEOUT', 'BLOCKED'}
|
||||
and result.error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
and result.evidence_path):
|
||||
@@ -525,14 +593,63 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
try:
|
||||
diagnosis = json.loads((result.evidence_path.parent/'diagnostics/icdb-recovery.json').read_text('utf-8'))
|
||||
original = result
|
||||
snapshot = original.evidence_path.with_name('pre-repair-result.json')
|
||||
snapshot.write_text(json.dumps(asdict(original), ensure_ascii=False, default=str, indent=2),
|
||||
encoding='utf-8')
|
||||
recovery = attempt_recovery(request, diagnosis, result.evidence_path.parent, progress=progress)
|
||||
recovery = {**recovery, 'original_error': original.stderr,
|
||||
'original_evidence': str(original.evidence_path)}
|
||||
'original_evidence': str(original.evidence_path),
|
||||
'original_result_snapshot': str(snapshot),
|
||||
'original_stage': original.error_stage,
|
||||
'native_status': recovery.get('native_status', recovery.get('status')),
|
||||
'validation_passed': False}
|
||||
if recovery.get('status') == 'REPAIRED':
|
||||
from .native_repair import native_success_confirmed, select_repair_target
|
||||
try:
|
||||
confirmed = native_success_confirmed(select_repair_target(request, diagnosis), recovery)
|
||||
except (ValueError, OSError):
|
||||
confirmed = False
|
||||
if not confirmed:
|
||||
release = recovery.get('release')
|
||||
recovery.update(status='UNCONFIRMED',
|
||||
release={**(release if isinstance(release, dict) else {}), 'safe_to_continue': False},
|
||||
detail='NATIVE_REPAIR_EVIDENCE_UNCONFIRMED: 原生成功记录不完整或矛盾,未重试校验')
|
||||
if (recovery.get('status') == 'REPAIRED'
|
||||
and recovery.get('release', {}).get('safe_to_continue') is True):
|
||||
result = replace(original, recovery_attempts=(recovery,))
|
||||
recovery['status'] = 'REPAIR_VALIDATION_FAILED'
|
||||
result = _run_verify_serialized(
|
||||
replace(request, auto_repair=False), progress=progress, flag_reader=flag_reader,
|
||||
replace(request, auto_repair=False, maintenance=False), progress=progress, flag_reader=flag_reader,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
dialog_supervisor_factory=dialog_supervisor_factory)
|
||||
dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt)
|
||||
recovery['validation_evidence'] = str(result.evidence_path or '')
|
||||
recovery['validation_passed'] = (result.status == 'PASS'
|
||||
and result.library_flag == 'GREEN' and result.safe_to_continue)
|
||||
if not recovery['validation_passed']:
|
||||
recovery['status'] = 'REPAIR_VALIDATION_FAILED'
|
||||
if result.status == 'PASS':
|
||||
result = replace(result, status='FAIL',
|
||||
stderr=(result.stderr+'\nREPAIR_VALIDATION_UNCONFIRMED: 未取得校验 GREEN').strip())
|
||||
else:
|
||||
recovery['status'] = 'REPAIRED'
|
||||
else:
|
||||
# A failed local database must not block unrelated CBBs once
|
||||
# every owner is released. Re-check target ownership here so
|
||||
# an incomplete repair record cannot authorize the queue.
|
||||
safe = False
|
||||
if (recovery.get('status') in {'MISSING_HEALTHY_BACKUP', 'NATIVE_REPAIR_FAILED'}
|
||||
and recovery.get('evidence', {}).get('failure_complete') is True
|
||||
and recovery.get('release', {}).get('safe_to_continue') is True):
|
||||
from .native_repair import select_repair_target
|
||||
try:
|
||||
target = select_repair_target(request, diagnosis)
|
||||
safe = (Path(recovery.get('project', '')).resolve() == target.project
|
||||
and Path(recovery.get('database', '')).resolve() == target.database)
|
||||
except (ValueError, OSError):
|
||||
pass
|
||||
result = replace(result, safe_to_continue=safe, stderr=(result.stderr + '\n'
|
||||
+ str(recovery.get('status', 'REPAIR_UNCONFIRMED')) + ': '
|
||||
+ str(recovery.get('detail', '原生 Repair 未完成恢复'))).strip())
|
||||
result = replace(result, recovery_attempts=(recovery,))
|
||||
result.evidence_path.write_text(json.dumps({'block_name': request.block_name, **asdict(result)},
|
||||
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
@@ -540,20 +657,61 @@ def run_verify(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
result = replace(result, status='FAIL' if result.status == 'PASS' else result.status,
|
||||
safe_to_continue=False,
|
||||
stderr=(result.stderr+f'\nREPAIR_UNCONFIRMED: {error}').strip())
|
||||
if result.evidence_path:
|
||||
try:
|
||||
result.evidence_path.write_text(json.dumps({'block_name': request.block_name, **asdict(result)},
|
||||
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
except OSError:
|
||||
pass # Keep the native/repair errors even if evidence storage fails.
|
||||
release_confirmed = result.safe_to_continue
|
||||
if not release_confirmed and result.evidence_path:
|
||||
try:
|
||||
release = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8'))
|
||||
release_confirmed = (release.get('worker_exited') is True
|
||||
and release.get('safe_to_continue') is True and release.get('block') == request.block_name
|
||||
and Path(release['library']).resolve() == Path(request.lmc_file).resolve())
|
||||
if (release.get('worker_exited') is True and not release.get('error')
|
||||
and not release.get('close_errors') and not release.get('automation_release', {}).get('error')
|
||||
and release.get('block') == request.block_name
|
||||
and Path(release['library']).resolve() == Path(request.lmc_file).resolve()):
|
||||
remaining_clients = tuple(pid for pid in release.get('remaining_pids', [])
|
||||
if type(pid) is int and pid > 0)
|
||||
except (OSError, ValueError, KeyError):
|
||||
pass
|
||||
if previous_release is not None:
|
||||
result = replace(result, release_proof=previous_release)
|
||||
if result.evidence_path:
|
||||
_write_optional_evidence(result.evidence_path, {'block_name': request.block_name, **asdict(result)})
|
||||
return result
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
try:
|
||||
if library_lock is not None:
|
||||
library_lock.__exit__(None, None, None)
|
||||
finally:
|
||||
if acquired:
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
uninitialize_com(initialized)
|
||||
|
||||
|
||||
def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_runner=None,
|
||||
port_check=None, dialog_supervisor_factory=None):
|
||||
port_check=None, dialog_supervisor_factory=None, version_prompt=None, stop=None):
|
||||
timing = RunTimings()
|
||||
result = _run_verify(request, timing=timing, progress=progress, flag_reader=flag_reader,
|
||||
process_runner=process_runner, port_check=port_check,
|
||||
dialog_supervisor_factory=dialog_supervisor_factory)
|
||||
if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'} and result.evidence_path:
|
||||
dialog_supervisor_factory=dialog_supervisor_factory, version_prompt=version_prompt, stop=stop)
|
||||
if not request.auto_repair and result.evidence_path:
|
||||
try:
|
||||
release = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8'))
|
||||
if (release.get('block') == request.block_name
|
||||
and Path(release['library']).resolve() == Path(request.lmc_file).resolve()):
|
||||
released = release.get('worker_exited') is True and release.get('safe_to_continue') is True
|
||||
result = replace(result, safe_to_continue=released)
|
||||
if not released and result.status == 'PASS':
|
||||
result = replace(result, status='FAIL', stderr='原生会话或自动化客户端尚未退出。')
|
||||
except (OSError, ValueError, KeyError):
|
||||
pass
|
||||
if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP', 'CANCELLED'} and result.evidence_path:
|
||||
job_path = result.evidence_path.with_name('request.json')
|
||||
if job_path.is_file():
|
||||
timing.begin('failure_diagnostics')
|
||||
@@ -579,8 +737,7 @@ def _run_verify_serialized(request, *, progress=None, flag_reader=None, process_
|
||||
detail = result.stderr
|
||||
if recovery and RECOVERY_ACTION not in detail:
|
||||
detail = (detail + '\n' + recovery_detail(recovery)).strip()
|
||||
result = replace(result, safe_to_continue=False, stderr=detail,
|
||||
error_category=ERROR_CODE)
|
||||
result = replace(result, stderr=detail, error_category=ERROR_CODE)
|
||||
if recovery and result.evidence_path:
|
||||
try:
|
||||
record_path = result.evidence_path.parent/'diagnostics/icdb-recovery.json'
|
||||
@@ -597,28 +754,29 @@ 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
|
||||
|
||||
|
||||
def _run_verify(request, *, timing, progress=None, flag_reader=None, process_runner=None, port_check=None,
|
||||
dialog_supervisor_factory=None):
|
||||
dialog_supervisor_factory=None, version_prompt=None, stop=None):
|
||||
"""Refresh one CBB and record its evidence.
|
||||
|
||||
``flag_reader`` is a read-only probe returning an authoritative ``BridgeResult``.
|
||||
@@ -627,12 +785,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:
|
||||
@@ -650,6 +810,8 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
pass
|
||||
return replace(result, evidence_path=result_path)
|
||||
|
||||
if stop is not None and stop.is_set():
|
||||
return finish(VerifyResult('CANCELLED', stderr='已取消,未启动校验。'))
|
||||
timing.begin('preflight')
|
||||
try:
|
||||
project, pcb = preflight(request)
|
||||
@@ -658,18 +820,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
finally:
|
||||
timing.end('preflight')
|
||||
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
try:
|
||||
addresses = unreleased_addresses(request.lmc_file, project, pcb)
|
||||
if addresses:
|
||||
return finish(VerifyResult('BLOCKED', safe_to_continue=False,
|
||||
stderr='ICDB_SESSION_UNCONFIRMED: 原生数据库所有权未释放,未开始内容检查或校验:'
|
||||
+ '; '.join(addresses)))
|
||||
except OSError as error:
|
||||
return finish(VerifyResult('BLOCKED', safe_to_continue=False,
|
||||
stderr=f'ICDB_SESSION_UNCONFIRMED: 无法确认数据库所有权:{error}'))
|
||||
|
||||
content_check_enabled = bool(str(request.libspec).strip() and str(request.dms_login_config).strip())
|
||||
content_check_enabled = not request.diagnostic_only and bool(str(request.libspec).strip() and str(request.dms_login_config).strip())
|
||||
if content_check_enabled:
|
||||
try:
|
||||
executable = resolve_importer(request.mentor_bin)
|
||||
@@ -677,7 +828,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:
|
||||
@@ -699,20 +850,20 @@ 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'})
|
||||
|
||||
# The design server needs a port in 10000-10100; a system reservation there
|
||||
# blocks every item, so fail fast with the fix instead of timing out.
|
||||
# Sampling cannot establish that the whole native port range is unusable.
|
||||
from .mentor_adapter import icdb_ports_bindable
|
||||
check_ports = port_check or icdb_ports_bindable
|
||||
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))
|
||||
|
||||
job = {'lmc_file': str(absolute_preserving_drive(request.lmc_file)), 'block_name': request.block_name,
|
||||
'diagnostic_only': request.diagnostic_only,
|
||||
'supervisor_pid': os.getpid(),
|
||||
'project': str(project), 'pcb': str(pcb), 'root': str(root), 'timeout': request.timeout_seconds,
|
||||
'mentor_bin': str(request.mentor_bin or ''),
|
||||
@@ -723,11 +874,22 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
'desktop': 'CbbVerify_' + uuid4().hex[:12],
|
||||
'cancel_signal': str(root/'cancel-worker.signal'),
|
||||
'start_gate': str(root/'start-worker.signal')}
|
||||
consent = None
|
||||
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
|
||||
launcher_pid = None
|
||||
lm_pid = None
|
||||
verify_invoked = False
|
||||
invoke_returned = False
|
||||
@@ -743,10 +905,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)
|
||||
@@ -757,15 +922,17 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
deadline = ActivityDeadline(request.timeout_seconds)
|
||||
last_activity_key = None
|
||||
phase = DialogPhase.PCB_STARTING
|
||||
restore_controller = None
|
||||
dialog_agent = None
|
||||
latest_snapshots = ()
|
||||
if dialog_supervisor_factory is None:
|
||||
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:
|
||||
@@ -778,8 +945,14 @@ 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():
|
||||
_stop_worker(process)
|
||||
return finish(VerifyResult('CANCELLED', stderr='已取消当前校验。',
|
||||
safe_to_continue=False, error_stage=phase.value))
|
||||
if events.exists():
|
||||
with events.open(encoding='utf-8') as stream:
|
||||
stream.seek(offset)
|
||||
@@ -790,6 +963,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
break
|
||||
timing.record(event)
|
||||
if event.get('pcb_pid'): target_pid = event['pcb_pid']
|
||||
if event.get('launcher_pid'): launcher_pid = event['launcher_pid']
|
||||
if event.get('lm_pid'): lm_pid = event['lm_pid']
|
||||
stage = event.get('stage')
|
||||
if stage == 'verify_invoke': verify_invoked = True
|
||||
@@ -813,7 +987,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
|
||||
@@ -841,6 +1015,27 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
dialog_result = dialog_supervisor.poll()
|
||||
else:
|
||||
dialog_result = dialog_supervisor.poll()
|
||||
if (consent is not None and dialog_agent is not None
|
||||
and dialog_result.error_code == 'DATABASE_RECOVERY_REQUIRED'
|
||||
and dialog_result.snapshot is not None and exact_prompt(dialog_result.snapshot)):
|
||||
consent_snapshot = dialog_result.snapshot
|
||||
if consent_snapshot.hwnd in {int(h) for h in process.desktop.EnumDesktopWindows()}:
|
||||
dialog_result = consent.handle(consent_snapshot, dialog_result.observed_snapshots,
|
||||
target_pid=target_pid, launcher_pid=launcher_pid, worker_pid=process.pid,
|
||||
companion_pids=tuple(process.companion_pids), perform=dialog_supervisor.backend.perform)
|
||||
if dialog_result.error_code == 'PCB_VERSION_CHOICE_REQUIRED' and dialog_result.snapshot is not None:
|
||||
from .dialog_rules import archive_version_choice
|
||||
choice_path = archive_version_choice(dialog_result.snapshot, root)
|
||||
dialog_result = replace(dialog_result, detail=dialog_result.detail + f'\n版本候选与原生窗口证据:{choice_path}')
|
||||
if (version_prompt is not None and consent is not None and dialog_agent is not None
|
||||
and dialog_result.snapshot.hwnd in {int(h) for h in process.desktop.EnumDesktopWindows()}):
|
||||
from .pcb_restore_prompt import RestoreController
|
||||
if restore_controller is None:
|
||||
restore_controller = RestoreController(job)
|
||||
dialog_result = restore_controller.handle(dialog_result.snapshot,
|
||||
dialog_result.observed_snapshots, target_pid=target_pid, launcher_pid=launcher_pid,
|
||||
worker_pid=process.pid, companion_pids=tuple(process.companion_pids),
|
||||
show=version_prompt, perform=dialog_supervisor.backend.perform)
|
||||
# Normal progress stays in memory. Only actions/errors and the
|
||||
# last failure snapshots are persisted, not every elapsed tick.
|
||||
latest_snapshots = dialog_result.observed_snapshots
|
||||
@@ -866,7 +1061,10 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
if dialog_result.error_code:
|
||||
_write_failure_windows(root, latest_snapshots)
|
||||
_write_process_tree(root, process, target_pid)
|
||||
_stop_worker(process)
|
||||
# A failed database load has no document to close. Give the
|
||||
# worker time to normally close its owned empty PCB before
|
||||
# recovery checks for remaining native database clients.
|
||||
_stop_worker(process, timeout=40)
|
||||
return finish(VerifyResult(
|
||||
'FAIL',
|
||||
stderr=f'{dialog_result.error_code}: {dialog_result.detail}',
|
||||
@@ -893,14 +1091,14 @@ 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,
|
||||
'observation': last_post_verify_observation,
|
||||
})
|
||||
return finish(VerifyResult(status, stderr=detail, safe_to_continue=False))
|
||||
return finish(VerifyResult('TIMEOUT', stderr='操作超时;已停止自动化并保留原生会话,停止批次,请检查恢复记录。', error_stage=phase.value, safe_to_continue=False))
|
||||
return finish(VerifyResult('TIMEOUT', stderr='操作超时,当前项未完成。', error_stage=phase.value, safe_to_continue=False))
|
||||
time.sleep(.2)
|
||||
worker_result = root/'outcome.json'
|
||||
if not worker_result.exists():
|
||||
@@ -912,9 +1110,25 @@ 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
|
||||
automation_release = {'safe_to_continue': False}
|
||||
if 'dialog_agent' in locals() and dialog_agent is not None:
|
||||
try:
|
||||
dialog_agent.close()
|
||||
@@ -922,15 +1136,37 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
close_errors.append(str(error))
|
||||
if 'process' in locals():
|
||||
try:
|
||||
worker_exited = process.poll() is not None
|
||||
if not worker_exited:
|
||||
worker_exited = _stop_worker(process)
|
||||
if hasattr(process, 'automation_release_state'):
|
||||
automation_release = process.automation_release_state()
|
||||
else:
|
||||
automation_release = {'safe_to_continue': worker_exited, 'remaining_pids': []}
|
||||
process.close()
|
||||
except Exception as error:
|
||||
close_errors.append(str(error))
|
||||
if 'process' in locals():
|
||||
try:
|
||||
if events.is_file():
|
||||
with events.open(encoding='utf-8') as stream:
|
||||
stream.seek(offset)
|
||||
for line in stream:
|
||||
try:
|
||||
event = json.loads(line)
|
||||
timing.record(event)
|
||||
if event.get('pcb_pid'): target_pid = event['pcb_pid']
|
||||
if event.get('launcher_pid'): launcher_pid = event['launcher_pid']
|
||||
if event.get('lm_pid'): lm_pid = event['lm_pid']
|
||||
if event.get('stage') == 'pcb_baseline': pcb_baseline = set(event.get('pids', ()))
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
continue
|
||||
offset = stream.tell()
|
||||
release_result = _cleanup_session_processes(
|
||||
root,
|
||||
lm_pid=lm_pid,
|
||||
target_pid=target_pid,
|
||||
launcher_pid=launcher_pid,
|
||||
pcb=pcb,
|
||||
pcb_baseline=pcb_baseline,
|
||||
progress=progress,
|
||||
@@ -941,16 +1177,28 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
from .icdb_ownership import unreleased_addresses
|
||||
addresses = unreleased_addresses(request.lmc_file, project, pcb)
|
||||
if addresses:
|
||||
release_result = {**(release_result or {}), 'safe_to_continue': False,
|
||||
'icdb_address': addresses[0], 'icdb_addresses': addresses}
|
||||
release_result = {**(release_result or {}), 'icdb_addresses': addresses}
|
||||
except OSError as error:
|
||||
close_errors.append(str(error))
|
||||
if close_errors:
|
||||
release_result = {**(release_result or {}), 'safe_to_continue': False,
|
||||
'close_errors': close_errors}
|
||||
if not worker_exited or not automation_release.get('safe_to_continue'):
|
||||
release_result = {**(release_result or {}), 'safe_to_continue': False,
|
||||
'automation_release': automation_release, 'worker_exited': worker_exited}
|
||||
release_result['remaining_pids'] = sorted(set(release_result.get('remaining_pids', []))
|
||||
| set(automation_release.get('remaining_pids', [])))
|
||||
try:
|
||||
(root/'session-release.json').write_text(json.dumps({
|
||||
**(release_result or {}), 'block': request.block_name,
|
||||
'library': str(request.lmc_file), 'worker_exited': worker_exited,
|
||||
'worker_pid': getattr(process, 'pid', None)},
|
||||
ensure_ascii=False, default=str, indent=2), encoding='utf-8')
|
||||
except OSError:
|
||||
pass
|
||||
if not release_result or not release_result.get('safe_to_continue'):
|
||||
if 'result' in locals():
|
||||
result = replace(result, status='FAIL', safe_to_continue=False,
|
||||
result = replace(result, status='FAIL' if result.status == 'PASS' else result.status, safe_to_continue=False,
|
||||
stderr=(result.stderr + '\nPCB_RELEASE_UNCONFIRMED: 未确认原生会话退出').strip())
|
||||
recovery = {'desktop': job['desktop'], 'project': str(project),
|
||||
'pcb': str(pcb), 'release': release_result,
|
||||
@@ -978,22 +1226,22 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
|
||||
win32event.ReleaseMutex(mutex)
|
||||
mutex.Close()
|
||||
|
||||
if result.status == 'PASS':
|
||||
if result.status == 'PASS' and not request.diagnostic_only:
|
||||
timing.begin('library_flag')
|
||||
bridge_records = []
|
||||
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')
|
||||
@@ -1019,10 +1267,10 @@ def run_batch(requests, max_workers=1, verifier=None, *, stop=None, progress=Non
|
||||
result = VerifyResult('FAIL', stderr=str(error), safe_to_continue=False)
|
||||
results.append(result)
|
||||
# Never start another writer after an unconfirmed native shutdown.
|
||||
queue_stop = queue_stop or result.status == 'QUEUE_STOP' or not result.safe_to_continue
|
||||
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
|
||||
|
||||
|
||||
@@ -1043,11 +1291,21 @@ def write_failure_report(requests, results, root, *, path=None):
|
||||
root = Path(root); root.mkdir(parents=True, exist_ok=True)
|
||||
path = Path(path) if path else root/('failures-' + time.strftime('%Y%m%d-%H%M%S') + '-' + uuid4().hex[:6] + '.log')
|
||||
failed = [(req, result) for req, result in zip(requests, results)
|
||||
if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'}]
|
||||
if result.status not in {'PASS', 'SKIPPED', 'QUEUE_STOP'}
|
||||
and not (result.status in {'CHECKED', 'REPAIRED'} and result.safe_to_continue)]
|
||||
passed = sum(result.status == 'PASS' for result in results)
|
||||
skipped = len(results)-passed-len(failed)
|
||||
lines = ['CBB Verify 失败清单', f'总数: {len(results)} 成功: {passed} 失败: {len(failed)} 未执行: {skipped}',
|
||||
checked = sum(result.status == 'CHECKED' and result.safe_to_continue for result in results)
|
||||
repaired = sum(result.status == 'REPAIRED' and result.safe_to_continue for result in results)
|
||||
skipped = len(results)-passed-len(failed)-checked-repaired
|
||||
lines = ['CBB Verify 失败清单', f'总数: {len(results)} 成功: {passed} 失败: {len(failed)} 未执行: {skipped} 仅检查未修复: {checked} ICDB已修复待校验: {repaired}',
|
||||
'CBB\t状态\t错误分类\t失败阶段\t桥状态\t原始错误\t证据\t实际耗时(秒)']
|
||||
if requests and all(req.maintenance for req in requests):
|
||||
from .icdb_maintenance import maintenance_counts
|
||||
counts = maintenance_counts(results)
|
||||
lines[:2] = ['ICDB 检查异常清单',
|
||||
f'总数: {len(results)} 未发现异常: {checked} 发现问题: {counts["issues_found"]} '
|
||||
f'占用: {counts["in_use"]} 无法确认: {counts["unconfirmed"]} '
|
||||
f'取消: {counts["cancelled"]} 未执行: {counts["skipped"]}']
|
||||
for req, result in failed:
|
||||
timings = ' '.join(f'{key}={value:.3f}s' +
|
||||
('(unfinished)' if key in result.incomplete_timing_phases else '')
|
||||
@@ -1068,6 +1326,8 @@ def write_batch_report(requests, results, root):
|
||||
path = root/('batch-' + time.strftime('%Y%m%d-%H%M%S') + '-' + uuid4().hex[:6] + '.log')
|
||||
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 批量检查结果', '流程: 原生 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' +
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"source": "Frozen b90a9bb native startup capture: no-history-repair/repair-result.json; HWNDs sanitized. Auxiliary classes/empty titles from coordinator's readback of the same owned native process.",
|
||||
"startup_shell": {
|
||||
"hwnd": 100,
|
||||
"title": "iCDB Project Backup",
|
||||
"class": "Afx:00400000:b:00010003:00000006:5F991B0D",
|
||||
"enabled": true,
|
||||
"children": [
|
||||
{"hwnd": 401, "text": "", "class": "BCGPSlider:400000:8:10003:10", "id": -1, "enabled": true},
|
||||
{"hwnd": 402, "text": "Output ", "class": "BCGPControlBar:400000:8:10003:10", "id": 313, "enabled": true},
|
||||
{"hwnd": 403, "text": "", "class": "#32770", "id": 0, "enabled": true},
|
||||
{"hwnd": 400, "text": "", "class": "RichEdit20A", "id": 1002, "enabled": true},
|
||||
{"hwnd": 404, "text": "", "class": "BCGPStatusBar:400000:8:10003:10", "id": 59393, "enabled": true},
|
||||
{"hwnd": 405, "text": "", "class": "BCGPDockBar:400000:8:10003:10", "id": 59419, "enabled": true},
|
||||
{"hwnd": 406, "text": "Standard", "class": "BCGPToolBar:400000:8:10003:10", "id": 59392, "enabled": true},
|
||||
{"hwnd": 407, "text": "Menu Bar", "class": "BCGPToolBar:400000:8:10003:10", "id": 59398, "enabled": true},
|
||||
{"hwnd": 408, "text": "", "class": "#32770", "id": 59648, "enabled": true}
|
||||
]
|
||||
},
|
||||
"auxiliary_windows": [
|
||||
{"hwnd": 500, "title": "", "class": "UAC_InputIndicatorOverlayWnd", "enabled": true, "children": []},
|
||||
{"hwnd": 501, "title": "", "class": "UAC Input Indicator", "enabled": true, "children": []}
|
||||
],
|
||||
"loaded_title_prefix": "iCDB Project Backup - ",
|
||||
"loaded_output": "Checking if project is in use.\r\nProject is not in use. Full iCDB Project Backup's functionality is available.\r\n"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"source": "Issue 2 data.zip, verify-86f90ffba911; selected fields only; paths and process identities sanitized",
|
||||
"limitations": "Logs only. No field database, executable fault reproduction or recovery claim.",
|
||||
"block": "1GPHY_8211",
|
||||
"diagnosis": {
|
||||
"error_code": "ICDB_REPAIR_REQUIRED",
|
||||
"scope": "project",
|
||||
"machine": "FIELD-PC",
|
||||
"session": "2026-10-20 14.03.54-18212",
|
||||
"database_relative": "Work/database"
|
||||
},
|
||||
"native_error": "The last server session on different server machine lost the connection to the database. Please use iCDB Project Backup 'Repair project' functionality to fix this issue. (Machine:[FIELD-PC] Path:[<library>/ReusableBlocksLP/1GPHY_8211/Work/database] session:[2026-10-20 14.03.54-18212])",
|
||||
"observed_events": [
|
||||
{"stage": "pcb_launch", "strategy": "reusable_circuit_direct"},
|
||||
{"stage": "pcb_launcher_started", "launcher_pid": 11},
|
||||
{"stage": "pcb_worker_started", "pcb_pid": 12, "binding": "commandline"},
|
||||
{"stage": "native_failure_logs"},
|
||||
{"stage": "process_retained", "safe_to_continue": false},
|
||||
{"stage": "failed", "error": "VERIFY_CANCELLED: stop and normally close target document"}
|
||||
],
|
||||
"dialog": {
|
||||
"pid": 12,
|
||||
"phase": "PCB_STARTING",
|
||||
"action": "acknowledge_fatal",
|
||||
"error_code": "MENTOR_DATABASE_LOAD_FAILED",
|
||||
"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?"
|
||||
},
|
||||
"original_result": {"status": "FAIL", "error_category": "ICDB_REPAIR_REQUIRED", "error_stage": "PCB_STARTING"},
|
||||
"original_repair": {"status": "BLOCKED", "detail": "REPAIR_DATABASE_IN_USE"}
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
{
|
||||
"hwnd": 100,
|
||||
"pid": 20,
|
||||
"process_name": "ExpeditionPCB.exe",
|
||||
"process_role": "pcb",
|
||||
"window_class": "#32770",
|
||||
"title": "Database Restoration",
|
||||
"phase": "PCB_STARTING",
|
||||
"controls": [
|
||||
{
|
||||
"handle": 101,
|
||||
"control_id": 65535,
|
||||
"class_name": "Static",
|
||||
"name": "",
|
||||
"automation_id": "65535",
|
||||
"control_type": "Image",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": []
|
||||
},
|
||||
{
|
||||
"handle": 102,
|
||||
"control_id": 6208,
|
||||
"class_name": "Static",
|
||||
"name": "The system has detected this design abnormally terminated the last time it was used. The user has to decide what database (if any) to associate with the design.",
|
||||
"automation_id": "6208",
|
||||
"control_type": "Text",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": []
|
||||
},
|
||||
{
|
||||
"handle": 103,
|
||||
"control_id": 6212,
|
||||
"class_name": "Button",
|
||||
"name": "Restore Database",
|
||||
"automation_id": "6212",
|
||||
"control_type": "Group",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": []
|
||||
},
|
||||
{
|
||||
"handle": 104,
|
||||
"control_id": 6213,
|
||||
"class_name": "Button",
|
||||
"name": "Load the last user saved database dated 2026/9/24 23:06.",
|
||||
"automation_id": "6213",
|
||||
"control_type": "RadioButton",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": [
|
||||
"Invoke",
|
||||
"SelectionItem"
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": 105,
|
||||
"control_id": 6206,
|
||||
"class_name": "Button",
|
||||
"name": "Load the autosave database dated 2026/9/24 23:06.",
|
||||
"automation_id": "6206",
|
||||
"control_type": "RadioButton",
|
||||
"enabled": true,
|
||||
"selected": true,
|
||||
"checked": true,
|
||||
"patterns": [
|
||||
"Invoke",
|
||||
"SelectionItem"
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": 106,
|
||||
"control_id": 6207,
|
||||
"class_name": "Static",
|
||||
"name": "This will restore the design to the last autosave state.",
|
||||
"automation_id": "6207",
|
||||
"control_type": "Text",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": []
|
||||
},
|
||||
{
|
||||
"handle": 107,
|
||||
"control_id": 6209,
|
||||
"class_name": "Static",
|
||||
"name": "This will restore the design to the data from the last user save.",
|
||||
"automation_id": "6209",
|
||||
"control_type": "Text",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": []
|
||||
},
|
||||
{
|
||||
"handle": 108,
|
||||
"control_id": 6210,
|
||||
"class_name": "Static",
|
||||
"name": "Autosaved changes will be lost.",
|
||||
"automation_id": "6210",
|
||||
"control_type": "Text",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": []
|
||||
},
|
||||
{
|
||||
"handle": 109,
|
||||
"control_id": 6211,
|
||||
"class_name": "Static",
|
||||
"name": "* Cancelling will load no design.",
|
||||
"automation_id": "6211",
|
||||
"control_type": "Text",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": []
|
||||
},
|
||||
{
|
||||
"handle": 110,
|
||||
"control_id": 1,
|
||||
"class_name": "Button",
|
||||
"name": "OK",
|
||||
"automation_id": "1",
|
||||
"control_type": "Button",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": [
|
||||
"Invoke"
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": 111,
|
||||
"control_id": 2,
|
||||
"class_name": "Button",
|
||||
"name": "Cancel",
|
||||
"automation_id": "2",
|
||||
"control_type": "Button",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": [
|
||||
"Invoke"
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": 100,
|
||||
"control_id": 0,
|
||||
"class_name": "#32770",
|
||||
"name": "Database Restoration",
|
||||
"automation_id": "",
|
||||
"control_type": "Window",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": [
|
||||
"Window"
|
||||
]
|
||||
},
|
||||
{
|
||||
"handle": 0,
|
||||
"control_id": 0,
|
||||
"class_name": "",
|
||||
"name": "",
|
||||
"automation_id": "",
|
||||
"control_type": "TitleBar",
|
||||
"enabled": true,
|
||||
"selected": false,
|
||||
"checked": false,
|
||||
"patterns": [
|
||||
"Value"
|
||||
]
|
||||
}
|
||||
],
|
||||
"owner_hwnd": 90,
|
||||
"diagnostics": []
|
||||
}
|
||||
@@ -69,7 +69,7 @@ def test_crashed_worker_cannot_pass_even_with_pass_file(tmp_path, monkeypatch, o
|
||||
pytest.fail('A crashed writer must not become PASS via a pre-existing READY flag')
|
||||
result = run_verify(request(tmp_path), port_check=lambda: (True, ''), flag_reader=must_not_query)
|
||||
assert result.status == 'FAIL'
|
||||
assert result.safe_to_continue is False
|
||||
assert result.safe_to_continue is True # Proven release permits another item; crash stays FAIL.
|
||||
|
||||
|
||||
def test_supervisor_crash_stops_registered_save_helper_but_not_native(tmp_path):
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
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_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
|
||||
@@ -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'
|
||||
@@ -56,7 +56,8 @@ def test_restore_checks_source_in_library_copy_then_opens_actual_target(recovery
|
||||
retained = list(original.parent.parent.glob('Work.before-restore-*'))
|
||||
assert len(retained) == 1
|
||||
assert (retained[0]/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
assert list(request.evidence_dir.rglob('library-before/target.lmc'))
|
||||
assert not list(request.evidence_dir.rglob('library-before'))
|
||||
assert not list(request.evidence_dir.rglob('source-before'))
|
||||
|
||||
|
||||
def test_invalid_source_does_not_touch_target(recovery_files):
|
||||
@@ -143,10 +144,68 @@ def test_idle_guard_queries_supported_native_process_names(recovery_files, monke
|
||||
queries.append(query)
|
||||
return []
|
||||
monkeypatch.setattr(win32com.client, 'GetObject', lambda _: EmptyProcesses())
|
||||
from cbb_refresh_verify import owner_probe
|
||||
import subprocess
|
||||
monkeypatch.setattr(owner_probe.subprocess, 'run', lambda command, **kw:
|
||||
subprocess.CompletedProcess(command, owner_probe.owner_probe_main(command[-1])))
|
||||
_idle_check(request.lmc_file, original, original.parent/'PCB/A.pcb')
|
||||
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
|
||||
@@ -188,3 +247,144 @@ def test_equivalent_library_reference_preserves_native_project_bytes_and_date(re
|
||||
before = original.read_bytes(), original.stat().st_mtime_ns
|
||||
_align_library(original, original.parent/'PCB/A.pcb', request.lmc_file)
|
||||
assert (original.read_bytes(), original.stat().st_mtime_ns) == before
|
||||
|
||||
|
||||
def test_logs_and_unrelated_cbb_changes_after_snapshot_do_not_block_install(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
(source.parent/'session.log').write_bytes(b'snapshot log')
|
||||
other = project(request.lmc_file.parent/'ReusableBlocksLP/B/Work', 'B', b'unrelated')
|
||||
calls = []
|
||||
def verify(req, **kwargs):
|
||||
calls.append(req)
|
||||
if len(calls) == 1:
|
||||
(source.parent/'session.log').write_bytes(b'updated source log')
|
||||
(original.parent/'new.log').write_bytes(b'new target log')
|
||||
(other.parent/'database/icdb.dat').write_bytes(b'unrelated update')
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status == 'PASS'
|
||||
assert len(calls) == 2
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'healthy'
|
||||
assert (other.parent/'database/icdb.dat').read_bytes() == b'unrelated update'
|
||||
staged = calls[0].lmc_file.parent/'ReusableBlocksLP/A/Work'
|
||||
assert (staged/'session.log').read_bytes() == b'snapshot log'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('location,change', [
|
||||
('source', 'edit'), ('source', 'delete'), ('source', 'add'),
|
||||
('target', 'delete'), ('target', 'add'), ('cert', 'edit'), ('shared', 'edit'),
|
||||
])
|
||||
def test_primary_conflicts_preserve_target_and_validation_evidence(recovery_files, location, change):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
if location == 'source':
|
||||
conflict = source.parent/'database/icdb.dat'
|
||||
elif location == 'target':
|
||||
conflict = original.parent/'PCB/Layout/LayoutDB.lyt'
|
||||
elif location == 'cert':
|
||||
conflict = original.parent.parent/'Cert/A.prj'
|
||||
else:
|
||||
conflict = request.lmc_file.parent/'SymbolLibs/__ReusableBlocksSymb/icdb.dat'
|
||||
conflict.parent.mkdir(parents=True, exist_ok=True)
|
||||
if location in ('cert', 'shared'):
|
||||
conflict.write_bytes(b'before')
|
||||
calls = []
|
||||
def verify(*args, **kwargs):
|
||||
calls.append(args)
|
||||
if change == 'delete':
|
||||
conflict.unlink()
|
||||
elif change == 'add':
|
||||
(conflict.parent/'new-engineering.bin').write_bytes(b'new')
|
||||
else:
|
||||
conflict.write_bytes(b'concurrent edit')
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert len(calls) == 1
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
assert not list(original.parent.parent.glob('Work.before-restore-*'))
|
||||
assert list(request.evidence_dir.rglob('library-baseline-manifest.json'))
|
||||
assert list(request.evidence_dir.rglob('source-manifest.json'))
|
||||
assert calls[0][0].lmc_file.is_file()
|
||||
assert not list(request.evidence_dir.rglob('library-before'))
|
||||
|
||||
|
||||
def test_source_conflict_while_preparing_incoming_work_blocks_install(recovery_files, monkeypatch):
|
||||
from cbb_refresh_verify import backup_recovery as recovery
|
||||
request, original, source = recovery_files
|
||||
copy = recovery._copy_verified
|
||||
def copy_then_change_source(src, dst, **kwargs):
|
||||
result = copy(src, dst, **kwargs)
|
||||
if dst.name.startswith('Work.restore-'):
|
||||
(source.parent/'database/icdb.dat').write_bytes(b'late concurrent change')
|
||||
return result
|
||||
monkeypatch.setattr(recovery, '_copy_verified', copy_then_change_source)
|
||||
result = recovery.recover_from_backup(request, source,
|
||||
verifier=lambda *a, **k: VerifyResult('PASS', library_flag='GREEN'), idle_check=lambda *_: None)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
|
||||
|
||||
def test_source_address_record_is_rechecked_after_validation(recovery_files):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
address = source.parent/'database/cdbsvr/sAddress.adr'
|
||||
def idle(library, project, pcb):
|
||||
if project == source and address.exists():
|
||||
raise ValueError('Source database ownership is still active')
|
||||
def verify(*args, **kwargs):
|
||||
address.parent.mkdir(parents=True, exist_ok=True)
|
||||
address.write_bytes(b'live owner')
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=idle)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert 'ownership' in result.stderr
|
||||
assert address.read_bytes() == b'live owner'
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', ['Logs', 'tEmP', 'bAcKuP', 'Output'])
|
||||
@pytest.mark.parametrize('role', ['Work', 'Cert'])
|
||||
def test_cbb_names_matching_runtime_labels_do_not_hide_target_conflicts(tmp_path, monkeypatch, name, role):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
monkeypatch.setattr(Path, 'home', lambda: tmp_path/'profile')
|
||||
library = tmp_path/'target/target.lmc'
|
||||
original = project(library.parent/f'ReusableBlocksLP/{name}/Work', name, b'damaged')
|
||||
library.write_bytes(b'library')
|
||||
source = project(tmp_path/'backup/Work', name, b'healthy')
|
||||
request = VerifyRequest(library, name, tmp_path/'evidence')
|
||||
conflict = original.parent.parent/role/'database/icdb.dat'
|
||||
conflict.parent.mkdir(parents=True, exist_ok=True)
|
||||
if role == 'Cert':
|
||||
conflict.write_bytes(b'cert before')
|
||||
calls = []
|
||||
def verify(*args, **kwargs):
|
||||
calls.append(args)
|
||||
conflict.write_bytes(b'concurrent target edit')
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert len(calls) == 1
|
||||
assert conflict.read_bytes() == b'concurrent target edit'
|
||||
assert not list(original.parent.parent.glob('Work.before-restore-*'))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('relative', ['SymbolLibs/temp/icdb.dat', 'SymbolLibs/Temp_a/device.sym',
|
||||
'SharedDependency/hdl/Logs/design.v'])
|
||||
def test_shared_partition_engineering_conflicts_block_install(recovery_files, relative):
|
||||
from cbb_refresh_verify.backup_recovery import recover_from_backup
|
||||
request, original, source = recovery_files
|
||||
conflict = request.lmc_file.parent/relative
|
||||
conflict.parent.mkdir(parents=True, exist_ok=True)
|
||||
conflict.write_bytes(b'shared before')
|
||||
calls = []
|
||||
def verify(*args, **kwargs):
|
||||
calls.append(args)
|
||||
conflict.write_bytes(b'concurrent shared edit')
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
result = recover_from_backup(request, source, verifier=verify, idle_check=lambda *_: None)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert len(calls) == 1
|
||||
assert (original.parent/'database/icdb.dat').read_bytes() == b'damaged'
|
||||
assert conflict.read_bytes() == b'concurrent shared edit'
|
||||
@@ -2,6 +2,13 @@ from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def accept_manual_backup_reminder(monkeypatch):
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
|
||||
|
||||
def test_backup_dialog_requires_a_project_and_passes_selected_target(qtbot, tmp_path):
|
||||
@@ -51,11 +58,12 @@ def test_backup_dialog_keeps_ui_responsive_and_cannot_close_during_restore(qtbot
|
||||
assert dialog.close_button.isEnabled()
|
||||
|
||||
|
||||
def test_main_window_opens_simple_dialog_and_failure_clears_ready(qtbot, tmp_path):
|
||||
def test_main_window_opens_non_ready_dialog_and_failure_retains_ready_untargeted(qtbot, tmp_path):
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
from cbb_refresh_verify.library_reader import CbbRecord, LibraryReadResult
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((
|
||||
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), data_root=tmp_path)
|
||||
CbbRecord('A', 'LP', status='READY'), CbbRecord('B', 'LP'))), data_root=tmp_path,
|
||||
status_refresher=lambda names, _: {name: 'UNKNOWN' for name in names})
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('lib.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
@@ -63,12 +71,12 @@ def test_main_window_opens_simple_dialog_and_failure_clears_ready(qtbot, tmp_pat
|
||||
window.backup_restore_button.click()
|
||||
dialog = window.backup_dialog
|
||||
assert dialog.isVisible()
|
||||
assert dialog.block_combo.currentText() == 'A'
|
||||
dialog.started.emit('A')
|
||||
assert dialog.block_combo.currentText() == 'B'
|
||||
dialog.started.emit('B')
|
||||
assert window.busy
|
||||
assert 'A' not in window.ready_names
|
||||
dialog.completed.emit('A', VerifyResult('FAIL', stderr='损坏'))
|
||||
assert window.cbb_table.item(0, 2).text() == '未知 / 需确认'
|
||||
assert 'A' in window.ready_names
|
||||
dialog.completed.emit('B', VerifyResult('FAIL', stderr='无法确认', safe_to_continue=False))
|
||||
assert window.cbb_table.item(1, 2).text() == '未知 / 需确认'
|
||||
dialog.reject()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
"""Only live clients stop a batch; native writes are replaced by fixtures."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
from test_verify_runner import fixture, request, PORTS_OK
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status,detail', [('FAIL', 'UNKNOWN_DIALOG: native prompt'),
|
||||
('TIMEOUT', 'native operation timed out')])
|
||||
@pytest.mark.parametrize('released', [True, False])
|
||||
def test_batch_continuation_follows_actual_release(tmp_path, monkeypatch, status, detail, released):
|
||||
fixture(tmp_path)
|
||||
launches = []
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = 1
|
||||
def __init__(self, command, *_a, **_k):
|
||||
launches.append(command)
|
||||
Path(command[-1]).with_name('outcome.json').write_text(json.dumps({
|
||||
'status': status, 'stderr': detail, 'safe_to_continue': False}))
|
||||
def poll(self): return 1
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k:
|
||||
{'safe_to_continue': released, 'remaining_pids': [] if released else [123]})
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None)
|
||||
req = request(tmp_path)
|
||||
try:
|
||||
results = runner.run_batch([req, req], verifier=lambda r: runner.run_verify(r, port_check=PORTS_OK))
|
||||
assert results[0].status == status and results[0].safe_to_continue is released
|
||||
assert len(launches) == (2 if released else 1)
|
||||
assert results[1].status == (status if released else 'SKIPPED')
|
||||
finally:
|
||||
from cbb_refresh_verify import library_lock as locks
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(locks.LibraryLock(req.lmc_file).key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_item_failure_with_confirmed_release_does_not_stop_unrelated_items(tmp_path):
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult
|
||||
library = tmp_path/'library.lmc'; library.touch()
|
||||
requests = [VerifyRequest(library, name, tmp_path/'evidence') for name in ('A', 'B')]
|
||||
calls = []
|
||||
def verify(request):
|
||||
calls.append(request.block_name)
|
||||
return VerifyResult('FAIL' if request.block_name == 'A' else 'PASS',
|
||||
stderr='单项失败但会话已释放' if request.block_name == 'A' else '',
|
||||
safe_to_continue=True)
|
||||
results = runner.run_batch(requests, verifier=verify)
|
||||
assert calls == ['A', 'B']
|
||||
assert [result.status for result in results] == ['FAIL', 'PASS']
|
||||
|
||||
|
||||
def test_stop_worker_escalates_only_after_cooperative_deadline():
|
||||
calls = []
|
||||
class Worker:
|
||||
stopped = False
|
||||
def request_stop(self): calls.append('cancel')
|
||||
def wait(self, timeout=5):
|
||||
calls.append('wait')
|
||||
if not self.stopped: raise TimeoutError()
|
||||
def kill(self): calls.append('client_stop'); self.stopped = True
|
||||
def poll(self): return 1 if self.stopped else None
|
||||
assert runner._stop_worker(Worker(), timeout=.1) is True
|
||||
assert calls == ['cancel', 'wait', 'client_stop', 'wait']
|
||||
|
||||
|
||||
def test_cancel_reaches_current_supervised_worker(tmp_path, monkeypatch):
|
||||
fixture(tmp_path)
|
||||
stop = Event()
|
||||
cancelled = []
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = None
|
||||
def __init__(self, command, *_a, **_k):
|
||||
self.job = json.loads(Path(command[-1]).read_text('utf-8'))
|
||||
stop.set()
|
||||
def bind_cancellation(self, path): self.cancel = Path(path)
|
||||
def request_stop(self):
|
||||
self.cancel.touch(); cancelled.append(self.cancel)
|
||||
self.returncode = 1
|
||||
def wait(self, **_k): return 1
|
||||
def poll(self): return self.returncode
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None)
|
||||
result = runner.run_verify(request(tmp_path), stop=stop, port_check=PORTS_OK)
|
||||
assert result.status == 'CANCELLED' and result.safe_to_continue
|
||||
assert len(cancelled) == 1 and cancelled[0].is_file()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('text,expected', [
|
||||
('content checking completed successfully! 0 errors, 0 warnings', 'PASS'),
|
||||
('content checking completed successfully! 10 errors', 'FAIL'),
|
||||
('content checking completed successfully! error: missing symbol', 'FAIL'),
|
||||
('content checking completed successfully! 0 errors\nERROR: cannot connect', 'FAIL'),
|
||||
])
|
||||
def test_zero_error_count_does_not_block_content_check(text, expected):
|
||||
assert runner.classify_output(0, text) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('codec', ['utf-16', 'utf-8-sig', 'mbcs'])
|
||||
def test_preflight_reads_native_project_encodings(tmp_path, codec):
|
||||
work = fixture(tmp_path)
|
||||
(work/'CBB1.prj').write_bytes(('# 中文工程\nKEY PCBDesignPath "PCB\\actual.pcb"\n').encode(codec))
|
||||
project, pcb = runner.preflight(request(tmp_path))
|
||||
assert pcb == work/'PCB/actual.pcb'
|
||||
|
||||
|
||||
def test_unrelated_pcb_does_not_block_target_before_launch(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(adapter, 'pcb_processes', lambda: ((999, 'ExpeditionPCB.exe', '"ExpeditionPCB.exe" "C:\\other\\board.pcb"'),))
|
||||
assert execute()['status'] == 'PASS'
|
||||
assert any(e['stage'] == 'pcb_launch' for e in events)
|
||||
|
||||
|
||||
def test_sampling_ports_only_warn_by_default(tmp_path, monkeypatch):
|
||||
fixture(tmp_path)
|
||||
launches, warnings = [], []
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = 0
|
||||
def __init__(self, command, *_a, **_k):
|
||||
launches.append(command)
|
||||
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', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
|
||||
result = runner.run_verify(request(tmp_path), port_check=lambda: (False, 'sample unavailable'),
|
||||
flag_reader=lambda *_: __import__('cbb_refresh_verify.status_bridge', fromlist=['BridgeResult']).BridgeResult('READY', 'IsVerifiedReuseBlock', 1),
|
||||
progress=warnings.append)
|
||||
assert launches and result.status == 'PASS'
|
||||
assert any(e['stage'] == 'port_warning' for e in warnings)
|
||||
|
||||
|
||||
def test_cleanup_includes_late_native_descendant_without_pcb_argument(monkeypatch):
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
rows = [(10, 'ExpeditionPCB.exe', '', 1), (11, 'ExpeditionPCB.exe', '', 10),
|
||||
(99, 'ExpeditionPCB.exe', '', 77)]
|
||||
monkeypatch.setattr(adapter, '_process_details', lambda name: rows if name == 'ExpeditionPCB.exe' else [])
|
||||
result = adapter.cleanup_verify_processes(None, None, 10, baseline_pids={99})
|
||||
assert not result['safe_to_continue'] and result['remaining_pids'] == [10, 11]
|
||||
|
||||
|
||||
def test_save_client_without_worker_still_prevents_continuation(tmp_path, monkeypatch):
|
||||
fixture(tmp_path)
|
||||
class Worker:
|
||||
desktop = None
|
||||
returncode = 1
|
||||
def __init__(self, command, *_a, **_k):
|
||||
Path(command[-1]).with_name('outcome.json').write_text('{"status":"FAIL"}')
|
||||
def poll(self): return 1
|
||||
def close(self): pass
|
||||
def automation_release_state(self): return {'safe_to_continue': False, 'remaining_pids': [333]}
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
monkeypatch.setattr(runner, '_cleanup_session_processes', lambda *a, **k: {'safe_to_continue': True})
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', lambda *_: None)
|
||||
req = request(tmp_path)
|
||||
try:
|
||||
result = runner.run_verify(req, port_check=PORTS_OK)
|
||||
assert not result.safe_to_continue
|
||||
proof = json.loads(result.evidence_path.with_name('session-release.json').read_text('utf-8'))
|
||||
assert proof['worker_exited'] is True
|
||||
assert proof['automation_release']['remaining_pids'] == [333]
|
||||
assert proof['remaining_pids'] == [333]
|
||||
finally:
|
||||
from cbb_refresh_verify import library_lock as locks
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(locks.LibraryLock(req.lmc_file).key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_closed_desktop_worker_keeps_exit_status_without_using_closed_handle(monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
from cbb_refresh_verify import desktop_worker as desktop
|
||||
worker = desktop.DesktopWorker.__new__(desktop.DesktopWorker)
|
||||
handle = SimpleNamespace(closed=False)
|
||||
def close(): handle.closed = True
|
||||
handle.Close = close
|
||||
worker.handle = handle
|
||||
worker.job = SimpleNamespace(Close=lambda: None)
|
||||
worker.desktop = None; worker.companions = []; worker.companion_pids = []
|
||||
def code(h):
|
||||
assert not h.closed, 'Read from a closed Win32 process handle'
|
||||
return 1
|
||||
monkeypatch.setattr(desktop.win32process, 'GetExitCodeProcess', code)
|
||||
worker.close()
|
||||
assert worker.poll() == 1
|
||||
worker.close() # Idempotent cleanup.
|
||||
|
||||
|
||||
def test_native_process_queries_use_bounded_shared_helper(monkeypatch):
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
calls = []
|
||||
def query(names):
|
||||
calls.append(names)
|
||||
return [{'pid': 12, 'executable': 'ExpeditionPCB.exe', 'command': 'native', 'parent': 11}]
|
||||
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', query)
|
||||
assert adapter._process_details('ExpeditionPCB.exe') == [(12, 'ExpeditionPCB.exe', 'native', 11)]
|
||||
assert calls == [('ExpeditionPCB.exe',)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script', ['save_verified_block.vbs', 'close_failed_block.vbs'])
|
||||
def test_save_and_failure_close_clients_are_both_tracked(script, monkeypatch):
|
||||
from cbb_refresh_verify import desktop_worker as desktop
|
||||
worker = desktop.DesktopWorker.__new__(desktop.DesktopWorker)
|
||||
worker.pid = 11; worker._closed = True; worker._exit_code = 1
|
||||
command = f'cscript.exe //nologo "{Path(desktop.__file__).with_name(script)}" "C:\\fixture\\design.prj"'
|
||||
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_a, **_k: [
|
||||
{'pid': 22, 'parent': 11, 'command': command},
|
||||
{'pid': 33, 'parent': 99, 'command': command},
|
||||
{'pid': 44, 'parent': 11, 'command': 'cscript.exe unrelated.vbs'}])
|
||||
proof = worker.automation_release_state()
|
||||
assert proof['safe_to_continue'] is False and proof['remaining_pids'] == [22]
|
||||
|
||||
|
||||
def test_watchdog_with_cancel_signal_stops_clients_after_supervisor_exit(tmp_path):
|
||||
import subprocess, sys, time
|
||||
import win32api, win32con, win32process
|
||||
supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)'])
|
||||
script = tmp_path/'watchdog.py'; ready = tmp_path/'ready.json'; cancel = tmp_path/'cancel.signal'
|
||||
script.write_text(
|
||||
'import sys, subprocess, time, json\nfrom pathlib import Path\n'
|
||||
'from cbb_refresh_verify.desktop_worker import start_supervisor_watchdog, launch_save_helper\n'
|
||||
f'start_supervisor_watchdog({supervisor.pid}, cancel_path={str(cancel)!r}, stop_clients=True)\n'
|
||||
'native = subprocess.Popen([sys.executable,"-c","import time; time.sleep(30)"])\n'
|
||||
'client = launch_save_helper([sys.executable,"-c","import time; time.sleep(30)"])\n'
|
||||
f'Path({str(ready)!r}).write_text(json.dumps([native.pid,client.pid]))\n'
|
||||
'time.sleep(30)\n', encoding='utf-8')
|
||||
worker = subprocess.Popen([sys.executable, str(script)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
handles = []
|
||||
try:
|
||||
deadline = time.monotonic()+10
|
||||
while not ready.exists() and worker.poll() is None and time.monotonic()<deadline:
|
||||
time.sleep(.05)
|
||||
assert ready.exists()
|
||||
for pid in json.loads(ready.read_text()):
|
||||
handles.append(win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_TERMINATE, False, pid))
|
||||
supervisor.terminate(); supervisor.wait(5)
|
||||
assert worker.wait(10) == 75 and cancel.is_file()
|
||||
assert win32process.GetExitCodeProcess(handles[0]) == 259
|
||||
assert win32process.GetExitCodeProcess(handles[1]) != 259
|
||||
finally:
|
||||
for p in (supervisor, worker):
|
||||
if p.poll() is None: p.terminate()
|
||||
p.wait(5)
|
||||
for h in handles:
|
||||
if win32process.GetExitCodeProcess(h) == 259: win32process.TerminateProcess(h, 0)
|
||||
h.Close()
|
||||
@@ -84,11 +84,30 @@ def test_scan_reuses_checkpoint_and_queries_only_missing(tmp_path):
|
||||
encoding="utf-8",
|
||||
)
|
||||
bridge = FakeBridge()
|
||||
records = BatchStatusSession(bridge).scan(["A", "B"], lmc, checkpoint)
|
||||
records = BatchStatusSession(bridge).scan(["A", "B"], lmc, checkpoint, resume=True)
|
||||
assert bridge.requests == ["B"]
|
||||
assert {record.block_name for record in records} == {"A", "B"}
|
||||
|
||||
|
||||
def test_default_scan_refreshes_cached_ready_after_payload_change(tmp_path):
|
||||
lmc = tmp_path/'library.lmc'; lmc.write_bytes(b'library')
|
||||
checkpoint = tmp_path/'scan.jsonl'
|
||||
BatchStatusSession(FakeBridge()).scan(['A'], lmc, checkpoint)
|
||||
# The lightweight fingerprint cannot prove payload status is unchanged.
|
||||
fingerprint = fingerprint_library(lmc)
|
||||
(tmp_path/'payload.pcb').write_bytes(b'native edit without LMC change')
|
||||
assert fingerprint_library(lmc) == fingerprint
|
||||
class PendingBridge(FakeBridge):
|
||||
def query(self, *args, **kwargs):
|
||||
self.requests.append(args[1])
|
||||
return BridgeResult('PENDING', 'fake')
|
||||
bridge = PendingBridge()
|
||||
records = BatchStatusSession(bridge).scan(['A'], lmc, checkpoint)
|
||||
assert [record.status for record in records] == ['PENDING']
|
||||
assert bridge.requests == ['A']
|
||||
assert len(checkpoint.read_text('utf-8').splitlines()) == 1
|
||||
|
||||
|
||||
def test_timeout_isolated_and_later_item_continues(tmp_path):
|
||||
lmc = tmp_path / "mentor_lib.lmc"
|
||||
lmc.write_bytes(b"library")
|
||||
@@ -99,7 +118,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 +135,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
|
||||
|
||||
|
||||
@@ -172,3 +191,26 @@ def test_authoritative_status_map_never_reuses_previous_scan_status(tmp_path, mo
|
||||
assert first == {'A': 'PENDING'}
|
||||
assert second == {'A': 'READY'}
|
||||
assert calls == ['A', 'A']
|
||||
|
||||
|
||||
def test_retry_rechecks_failed_scan_without_library_changes(tmp_path):
|
||||
lmc = tmp_path/'library.lmc'; lmc.write_bytes(b'library')
|
||||
checkpoint = tmp_path/'scan.jsonl'
|
||||
first = BatchStatusSession(FakeBridge(timeout_name='A')).scan(['A', 'B'], lmc, checkpoint)
|
||||
assert first[0].status == 'TIMEOUT'
|
||||
bridge = FakeBridge()
|
||||
second = BatchStatusSession(bridge).scan(['A', 'B'], lmc, checkpoint, resume=True)
|
||||
assert bridge.requests == ['A']
|
||||
assert second[0].status == 'READY'
|
||||
|
||||
|
||||
def test_missing_x86_runtime_does_not_restart_for_every_cbb(tmp_path):
|
||||
class BrokenBridge(FakeBridge):
|
||||
def query(self, library, name, **kwargs):
|
||||
self.requests.append(name)
|
||||
raise BridgeResponseError('状态桥进程失败: -1073741515')
|
||||
lmc = tmp_path/'library.lmc'; lmc.write_bytes(b'library')
|
||||
bridge = BrokenBridge()
|
||||
result = BatchStatusSession(bridge).scan(['A', 'B', 'C'], lmc, tmp_path/'scan.jsonl')
|
||||
assert bridge.requests == ['A']
|
||||
assert [row.status for row in result] == ['UNAVAILABLE'] * 3
|
||||
@@ -82,5 +82,5 @@ def test_fingerprint_change_does_not_reuse_checkpoint(tmp_path):
|
||||
from cbb_refresh_verify.status_bridge import BridgeResult
|
||||
return BridgeResult("PENDING", "fake")
|
||||
bridge = Bridge()
|
||||
BatchStatusSession(bridge).scan(["A"], lmc, checkpoint)
|
||||
BatchStatusSession(bridge).scan(["A"], lmc, checkpoint, resume=True)
|
||||
assert bridge.requests == ["A"]
|
||||
@@ -0,0 +1,207 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.mark.parametrize('failure, expected', [
|
||||
('desktop', []),
|
||||
('job', ['desktop']),
|
||||
('process', ['job', 'desktop']),
|
||||
('thread', ['thread', 'terminate', 'process', 'job', 'desktop']),
|
||||
])
|
||||
def test_constructor_rolls_back_allocated_resources(monkeypatch, failure, expected):
|
||||
import cbb_refresh_verify.desktop_worker as module
|
||||
|
||||
closed = []
|
||||
error = RuntimeError(failure + ' failed')
|
||||
|
||||
class FakeJob:
|
||||
def Close(self):
|
||||
closed.append('job')
|
||||
|
||||
class FakeDesktop:
|
||||
def CloseDesktop(self):
|
||||
closed.append('desktop')
|
||||
|
||||
class FakeProcess:
|
||||
def Close(self):
|
||||
closed.append('process')
|
||||
|
||||
class FakeThread:
|
||||
def Close(self):
|
||||
closed.append('thread')
|
||||
raise error
|
||||
|
||||
def allocate(stage, value):
|
||||
if failure == stage:
|
||||
raise error
|
||||
return value
|
||||
|
||||
process = FakeProcess()
|
||||
monkeypatch.setattr(module.win32service, 'CreateDesktop',
|
||||
lambda *args: allocate('desktop', FakeDesktop()))
|
||||
monkeypatch.setattr(module.win32job, 'CreateJobObject',
|
||||
lambda *args: allocate('job', FakeJob()))
|
||||
monkeypatch.setattr(module.win32process, 'STARTUPINFO', lambda: type('Startup', (), {})())
|
||||
monkeypatch.setattr(module.win32process, 'CreateProcess',
|
||||
lambda *args: allocate('process', (process, FakeThread(), 321, 1)))
|
||||
|
||||
def terminate(handle, code):
|
||||
assert handle is process
|
||||
closed.append('terminate')
|
||||
|
||||
monkeypatch.setattr(module.win32process, 'TerminateProcess', terminate)
|
||||
|
||||
with pytest.raises(RuntimeError) as caught:
|
||||
module.DesktopWorker(['worker.exe'], 'PrivateDesk')
|
||||
|
||||
assert caught.value is error
|
||||
assert closed == expected
|
||||
|
||||
|
||||
def test_library_flag_loads_ui_dependencies_before_creating_private_desktop(monkeypatch, tmp_path):
|
||||
import builtins
|
||||
import win32service
|
||||
import cbb_refresh_verify.mentor_adapter as module
|
||||
|
||||
created = []
|
||||
original_import = builtins.__import__
|
||||
executable = tmp_path / 'LibraryManager.exe'
|
||||
executable.touch()
|
||||
|
||||
def deny_pywinauto(name, *args, **kwargs):
|
||||
if name == 'pywinauto' or name.startswith('pywinauto.'):
|
||||
raise ImportError('pywinauto unavailable')
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(builtins, '__import__', deny_pywinauto)
|
||||
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
|
||||
monkeypatch.setattr(win32service, 'CreateDesktop',
|
||||
lambda *args: created.append(args))
|
||||
|
||||
with pytest.raises(ImportError, match='pywinauto unavailable'):
|
||||
module.read_library_flag('library.lmc', 'Block')
|
||||
|
||||
assert created == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reader', ['read_library_flag', 'read_library_tree_image_map'])
|
||||
def test_reader_does_not_create_desktop_before_preparation_finishes(monkeypatch, tmp_path, reader):
|
||||
import win32service
|
||||
import cbb_refresh_verify.mentor_adapter as module
|
||||
|
||||
closed = []
|
||||
|
||||
class FakeDesktop:
|
||||
def CloseDesktop(self):
|
||||
closed.append('closed')
|
||||
|
||||
class InvalidLibrary:
|
||||
def __fspath__(self):
|
||||
raise ValueError('invalid library path')
|
||||
|
||||
executable = tmp_path / 'LibraryManager.exe'
|
||||
executable.touch()
|
||||
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
|
||||
monkeypatch.setattr(win32service, 'CreateDesktop',
|
||||
lambda *args: closed.append('created') or FakeDesktop())
|
||||
|
||||
with pytest.raises(ValueError, match='invalid library path'):
|
||||
getattr(module, reader)(InvalidLibrary(), 'Block')
|
||||
|
||||
assert closed == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reader', ['read_library_flag', 'read_library_tree_image_map'])
|
||||
def test_reader_closes_desktop_when_process_launch_fails(monkeypatch, tmp_path, reader):
|
||||
import win32service
|
||||
import win32process
|
||||
import cbb_refresh_verify.mentor_adapter as module
|
||||
|
||||
closed = []
|
||||
|
||||
class FakeDesktop:
|
||||
def CloseDesktop(self):
|
||||
closed.append('desktop')
|
||||
|
||||
def fail(*args):
|
||||
raise RuntimeError('launch failed')
|
||||
|
||||
executable = tmp_path / 'LibraryManager.exe'
|
||||
executable.touch()
|
||||
monkeypatch.setattr(module, 'library_manager_path', lambda: executable)
|
||||
monkeypatch.setattr(win32service, 'CreateDesktop', lambda *args: FakeDesktop())
|
||||
monkeypatch.setattr(win32process, 'CreateProcess', fail)
|
||||
if reader == 'read_library_flag':
|
||||
flag, detail = module.read_library_flag(tmp_path / 'library.lmc', 'Block')
|
||||
assert flag == module.FLAG_UNKNOWN
|
||||
assert 'launch failed' in detail
|
||||
else:
|
||||
with pytest.raises(RuntimeError, match='launch failed'):
|
||||
module.read_library_tree_image_map(tmp_path / 'library.lmc', ['Block'])
|
||||
assert closed == ['desktop']
|
||||
|
||||
|
||||
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': []}
|
||||
@@ -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)
|
||||
@@ -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'})
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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']
|
||||
@@ -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()
|
||||
|
||||
@@ -0,0 +1,366 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify import failed_session, mentor_adapter
|
||||
|
||||
|
||||
def startup_fixture(tmp_path, monkeypatch):
|
||||
import win32gui, win32process
|
||||
from cbb_refresh_verify import native_repair_ui
|
||||
job = {'root': str(tmp_path), 'pcb': str(tmp_path/'PCB/CBB.pcb'),
|
||||
'project': str(tmp_path/'CBB.prj'), 'lmc_file': str(tmp_path/'lib.lmc'),
|
||||
'desktop': 'CbbVerify_test'}
|
||||
diagnostics = tmp_path/'diagnostics'
|
||||
diagnostics.mkdir()
|
||||
(diagnostics/'icdb-recovery.json').write_text(json.dumps({
|
||||
'error_code': 'ICDB_REPAIR_REQUIRED', 'scope': 'project',
|
||||
'database_path': str(tmp_path/'database'), 'session': 'lost-session'}))
|
||||
records = [(12, r'C:\Mentor\wg\ExpeditionPCB.exe',
|
||||
f'ExpeditionPCB.exe "{job["pcb"]}" -ReusableCircuit', 11)]
|
||||
monkeypatch.setattr(mentor_adapter, '_process_details', lambda _: list(records))
|
||||
monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'CbbVerify_test')
|
||||
monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: fn(100, arg))
|
||||
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda *args: None)
|
||||
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda _: (1, 12))
|
||||
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda _: True)
|
||||
monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda _: True)
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda _: 'Expedition PCB')
|
||||
monkeypatch.setattr(win32gui, 'GetClassName', lambda _: 'Afx:main')
|
||||
messages = []
|
||||
def post(hwnd, message, wparam, lparam):
|
||||
messages.append((hwnd, message))
|
||||
records.clear()
|
||||
monkeypatch.setattr(win32gui, 'PostMessage', post)
|
||||
return job, records, messages
|
||||
|
||||
|
||||
@pytest.mark.parametrize('title', ['Expedition PCB', 'Expedition PCB - Reusable Block'])
|
||||
def test_failed_startup_normally_closes_owned_empty_pcb(tmp_path, monkeypatch, title):
|
||||
import win32con
|
||||
import win32gui
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda _: title)
|
||||
result = failed_session.close_failed_startup(job, 12, baseline_pids=(), timeout_seconds=0)
|
||||
assert result['status'] == 'CLOSED'
|
||||
assert not records
|
||||
assert messages == [(100, win32con.WM_CLOSE)]
|
||||
assert json.loads((tmp_path/'startup-close.json').read_text())['status'] == 'CLOSED'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obstacle', ['baseline', 'wrong_path', 'path_prefix', 'desktop', 'address',
|
||||
'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
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
baseline = ()
|
||||
if obstacle == 'baseline': baseline = (12,)
|
||||
if obstacle == 'wrong_path': records[0] = (12, records[0][1], 'other.pcb -ReusableCircuit', 11)
|
||||
if obstacle == 'path_prefix':
|
||||
records[0] = (12, records[0][1], f'pcb.exe "{job["pcb"]}.other" -ReusableCircuit', 11)
|
||||
if obstacle == 'desktop': monkeypatch.setattr(native_repair_ui, '_current_desktop_name', lambda: 'Default')
|
||||
if obstacle == 'address':
|
||||
address = tmp_path/'database/cdbsvr/sAddress.adr'
|
||||
address.parent.mkdir(parents=True)
|
||||
address.write_text('owner')
|
||||
if obstacle == 'dialog': monkeypatch.setattr(win32gui, 'GetClassName', lambda _: '#32770')
|
||||
if obstacle == 'document_title': monkeypatch.setattr(win32gui, 'GetWindowText', lambda _: 'CBB.pcb - Expedition PCB')
|
||||
if obstacle == 'mdi_document':
|
||||
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, fn, arg: fn(101, arg))
|
||||
monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'MDIClient' if hwnd == 101 else 'Afx:main')
|
||||
monkeypatch.setattr(win32gui, 'GetWindow', lambda *args: 102)
|
||||
if obstacle == 'disabled': monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda _: False)
|
||||
if obstacle == 'no_diagnosis': (tmp_path/'diagnostics/icdb-recovery.json').unlink()
|
||||
result = failed_session.close_failed_startup(job, 12, baseline_pids=baseline, timeout_seconds=0)
|
||||
assert result['status'] == 'RETAINED'
|
||||
assert records
|
||||
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)
|
||||
monkeypatch.setattr(win32gui, 'PostMessage', lambda hwnd, message, *_: messages.append((hwnd, message)))
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
|
||||
assert result['status'] == 'RETAINED'
|
||||
assert 'STARTUP_EXIT_UNCONFIRMED' in result['detail']
|
||||
assert len(messages) == 1
|
||||
assert records
|
||||
|
||||
|
||||
@pytest.mark.parametrize('exits', [True, False])
|
||||
def test_after_owned_close_transient_empty_command_waits_for_exit(tmp_path, monkeypatch, exits):
|
||||
from types import SimpleNamespace
|
||||
import win32gui
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
ticks = [0.0]
|
||||
def post(hwnd, message, *_):
|
||||
messages.append((hwnd, message))
|
||||
records[0] = (12, '', '', 11) # WMI during process teardown.
|
||||
def advance(seconds):
|
||||
ticks[0] += seconds
|
||||
if exits: records.clear()
|
||||
monkeypatch.setattr(win32gui, 'PostMessage', post)
|
||||
monkeypatch.setattr(failed_session, 'time', SimpleNamespace(monotonic=lambda: ticks[0], sleep=advance))
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=1)
|
||||
assert ticks[0] > 0 and len(messages) == 1
|
||||
assert result['status'] == ('CLOSED' if exits else 'RETAINED')
|
||||
assert 'STARTUP_PROCESS_UNCONFIRMED' not in result.get('detail', '')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obstacle', ['address', 'dialog', 'process'])
|
||||
def test_startup_close_waits_for_native_state_transitions(tmp_path, monkeypatch, obstacle):
|
||||
from types import SimpleNamespace
|
||||
import win32gui
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
ticks = [0]
|
||||
address = tmp_path/'database/cdbsvr/sAddress.adr'
|
||||
if obstacle == 'address':
|
||||
address.parent.mkdir(parents=True)
|
||||
address.write_text('owner')
|
||||
if obstacle == 'dialog':
|
||||
monkeypatch.setattr(win32gui, 'GetClassName',
|
||||
lambda _: '#32770' if ticks[0] == 0 else 'Afx:main')
|
||||
if obstacle == 'process':
|
||||
monkeypatch.setattr(win32gui, 'PostMessage',
|
||||
lambda hwnd, message, *_: messages.append((hwnd, message)))
|
||||
def advance(seconds):
|
||||
ticks[0] += seconds
|
||||
if obstacle == 'address' and address.exists(): address.unlink()
|
||||
if obstacle == 'process' and messages: records.clear()
|
||||
monkeypatch.setattr(failed_session, 'time', SimpleNamespace(
|
||||
monotonic=lambda: ticks[0], sleep=advance))
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=1)
|
||||
assert ticks[0] > 0
|
||||
assert result['status'] == 'CLOSED'
|
||||
assert len(messages) == 1
|
||||
assert not records
|
||||
|
||||
|
||||
def test_worker_closes_startup_failure_before_document_identity(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
job['desktop'] = 'CbbVerify_test'
|
||||
def fail_open(*args, **kwargs):
|
||||
raise RuntimeError('VERIFY_CANCELLED: database open failed')
|
||||
monkeypatch.setattr('cbb_refresh_verify.pcb_bridge.wait_for_target_document', fail_open)
|
||||
# Native process/window behavior is covered above; here verify the worker
|
||||
# actually reaches startup cleanup instead of requiring a loaded document.
|
||||
def close(job, pid, **kwargs):
|
||||
kwargs['emit'](stage='startup_close_result', status='CLOSED', pcb_pid=pid)
|
||||
return {'status': 'CLOSED'}
|
||||
monkeypatch.setattr(failed_session, 'close_failed_startup', close, raising=False)
|
||||
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED'):
|
||||
execute()
|
||||
assert any(e['stage'] == 'startup_close_result' and e['pcb_pid'] == 2 for e in events)
|
||||
assert not any(e['stage'] == 'save_start' for e in events)
|
||||
|
||||
|
||||
def test_failed_startup_accepts_current_owned_database_failure_dialog(tmp_path, monkeypatch):
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
(tmp_path/'diagnostics/icdb-recovery.json').unlink()
|
||||
(tmp_path/'dialogs.jsonl').write_text(json.dumps({
|
||||
'pid': 12, 'phase': 'PCB_STARTING', 'error_code': 'MENTOR_DATABASE_LOAD_FAILED'})+'\n')
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
|
||||
assert result['status'] == 'CLOSED'
|
||||
assert not records
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pid,phase', [(99, 'PCB_STARTING'), (12, 'SAVING')])
|
||||
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'] == 'CLOSED'
|
||||
assert len(messages) == 1 and not records
|
||||
|
||||
|
||||
@pytest.mark.parametrize('exits', [True, False])
|
||||
def test_startup_close_waits_for_launcher_after_empty_pcb_exit(tmp_path, monkeypatch, exits):
|
||||
from types import SimpleNamespace
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
ticks = [0.0]
|
||||
launcher = SimpleNamespace(pid=11, poll=lambda: 0 if exits and ticks[0] >= .4 else None)
|
||||
monkeypatch.setattr(failed_session, 'time', SimpleNamespace(
|
||||
monotonic=lambda: ticks[0], sleep=lambda seconds: ticks.__setitem__(0, ticks[0]+seconds)))
|
||||
result = failed_session.close_failed_startup(job, 12, launcher_process=launcher, timeout_seconds=1)
|
||||
assert ticks[0] >= .4
|
||||
assert result['status'] == ('CLOSED' if exits else 'RETAINED')
|
||||
assert result['release']['launcher_exited'] is exits
|
||||
assert result['release']['pcb_exited'] is True
|
||||
assert len(messages) == 1
|
||||
|
||||
|
||||
def test_startup_exit_does_not_hide_late_database_address(tmp_path, monkeypatch):
|
||||
import win32gui
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
def close(*args):
|
||||
records.clear()
|
||||
address = tmp_path/'database/cdbsvr/sAddress.adr'
|
||||
address.parent.mkdir(parents=True)
|
||||
address.write_text('still owned')
|
||||
monkeypatch.setattr(win32gui, 'PostMessage', close)
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
|
||||
assert result['status'] == 'RETAINED'
|
||||
assert result['release']['addresses']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('window_class,title', [('CustomPrompt', 'Database action pending'),
|
||||
('Afx:tool', ''), ('UAC_InputIndicatorOverlayWnd', 'Unknown prompt'), ('UAC Input Indicator', ' ')])
|
||||
def test_enabled_empty_main_with_unaccounted_owned_window_is_never_closed(tmp_path, monkeypatch, window_class, title):
|
||||
import win32gui
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(hwnd, arg) for hwnd in (100, 200)])
|
||||
monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'Afx:main' if hwnd == 100 else window_class)
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else title)
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
|
||||
assert result['status'] == 'RETAINED' and not result['release']['safe_to_continue']
|
||||
assert messages == [] and records
|
||||
assert result['unknown_windows'] == [{'hwnd': 200, 'class': window_class, 'title': title, 'enabled': True}]
|
||||
assert json.loads((tmp_path/'startup-close.json').read_text(encoding='utf-8'))['unknown_windows'] == result['unknown_windows']
|
||||
|
||||
|
||||
def test_empty_title_known_uac_windows_are_accounted_for_before_normal_close(tmp_path, monkeypatch):
|
||||
import win32gui, win32con
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
classes = {100: 'Afx:main', 200: 'UAC_InputIndicatorOverlayWnd', 201: 'UAC Input Indicator'}
|
||||
monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(hwnd, arg) for hwnd in classes])
|
||||
monkeypatch.setattr(win32gui, 'GetClassName', classes.__getitem__)
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else '')
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
|
||||
assert result['status'] == 'CLOSED' and messages == [(100, win32con.WM_CLOSE)]
|
||||
assert result['unknown_windows'] == []
|
||||
assert [entry['hwnd'] for entry in result['benign_auxiliary_windows']] == [200, 201]
|
||||
|
||||
|
||||
def test_custom_prompt_must_disappear_before_main_close(tmp_path, monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
import win32gui
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
ticks = [0.0]
|
||||
monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg:
|
||||
[fn(hwnd, arg) for hwnd in ((100, 200) if ticks[0] == 0 else (100,))])
|
||||
monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'Afx:main' if hwnd == 100 else 'CustomPrompt')
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else 'Still processing')
|
||||
monkeypatch.setattr(failed_session, 'time', SimpleNamespace(monotonic=lambda: ticks[0],
|
||||
sleep=lambda seconds: ticks.__setitem__(0, ticks[0] + seconds)))
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=1)
|
||||
assert result['status'] == 'CLOSED' and ticks[0] > 0
|
||||
assert len(messages) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obstacle', ['', 'title', 'area', 'children', 'near_class'])
|
||||
def test_only_exact_empty_zero_area_childless_ie_infrastructure_is_benign(tmp_path, monkeypatch, obstacle):
|
||||
import win32gui, win32con
|
||||
job, records, messages = startup_fixture(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(win32gui, 'EnumWindows', lambda fn, arg: [fn(hwnd, arg) for hwnd in (100, 200)])
|
||||
monkeypatch.setattr(win32gui, 'GetClassName', lambda hwnd: 'Afx:main' if hwnd == 100 else
|
||||
'Internet Explorer_Hidden_other' if obstacle == 'near_class' else 'Internet Explorer_Hidden')
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Expedition PCB' if hwnd == 100 else
|
||||
'Active prompt' if obstacle == 'title' else '')
|
||||
monkeypatch.setattr(win32gui, 'GetWindowRect', lambda hwnd: (0, 0, 100, 50) if obstacle == 'area' else (0, 0, 0, 0))
|
||||
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda hwnd, fn, arg:
|
||||
fn(201, arg) if hwnd == 200 and obstacle == 'children' else None)
|
||||
result = failed_session.close_failed_startup(job, 12, timeout_seconds=0)
|
||||
if obstacle:
|
||||
assert result['status'] == 'RETAINED' and messages == []
|
||||
assert result['unknown_windows'][0]['hwnd'] == 200
|
||||
else:
|
||||
assert result['status'] == 'CLOSED' and messages == [(100, win32con.WM_CLOSE)]
|
||||
entry = result['benign_auxiliary_windows'][0]
|
||||
assert entry['class'] == 'Internet Explorer_Hidden' and entry['rect'] == [0, 0, 0, 0]
|
||||
assert entry['children'] == []
|
||||
@@ -200,14 +200,14 @@ def test_worker_attempts_normal_close_after_idle_integration_failure(tmp_path, m
|
||||
assert not any(event['stage'] == 'save_start' for event in events)
|
||||
|
||||
|
||||
def test_stop_requests_cooperative_cancel_without_terminating_busy_client(tmp_path):
|
||||
def test_stop_cancels_then_stops_automation_client_after_deadline(tmp_path):
|
||||
calls = []
|
||||
class BusyWorker:
|
||||
def request_stop(self): calls.append('cancel')
|
||||
def wait(self, timeout): raise TimeoutError('native Save still running')
|
||||
def kill(self): calls.append('kill')
|
||||
runner._stop_worker(BusyWorker(), timeout=0)
|
||||
assert calls == ['cancel']
|
||||
assert calls == ['cancel', 'kill']
|
||||
|
||||
|
||||
def test_status_server_close_allows_eof_cleanup_before_termination(tmp_path):
|
||||
@@ -225,21 +225,22 @@ def test_status_server_close_allows_eof_cleanup_before_termination(tmp_path):
|
||||
assert marker.is_file()
|
||||
|
||||
|
||||
def test_shared_symbol_database_owner_blocks_content_check_and_pcb(tmp_path, monkeypatch):
|
||||
def test_shared_address_does_not_block_actual_content_check(tmp_path, monkeypatch):
|
||||
from test_verify_runner import fixture, request
|
||||
fixture(tmp_path)
|
||||
address = tmp_path/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr'
|
||||
address.parent.mkdir(parents=True)
|
||||
address.write_bytes(b'unknown native owner')
|
||||
req = request(tmp_path, libspec='EE', dms_login_config='login')
|
||||
outcome = runner.run_verify(req, process_runner=lambda *args: pytest.fail('must not launch importer'))
|
||||
assert outcome.status == 'BLOCKED'
|
||||
assert outcome.safe_to_continue is False
|
||||
assert '__ReusableBlocksSymb' in outcome.stderr
|
||||
calls = []
|
||||
outcome = runner.run_verify(req, process_runner=lambda *args:
|
||||
(calls.append(args) or (1, 'ERROR: shared database unavailable', '', False)))
|
||||
assert calls and outcome.status == 'FAIL' and outcome.safe_to_continue
|
||||
assert 'shared database unavailable' in outcome.stderr
|
||||
assert address.read_bytes() == b'unknown native owner'
|
||||
|
||||
|
||||
def test_shared_symbol_database_must_be_released_before_pass(tmp_path, monkeypatch):
|
||||
def test_shared_address_is_diagnostic_after_successful_native_execution(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
@@ -250,9 +251,8 @@ def test_shared_symbol_database_must_be_released_before_pass(tmp_path, monkeypat
|
||||
address.write_bytes(b'shared owner retained')
|
||||
return original(*args, **kwargs)
|
||||
monkeypatch.setattr(adapter.subprocess, 'Popen', helper)
|
||||
with pytest.raises(RuntimeError, match='ICDB_RELEASE_UNCONFIRMED'):
|
||||
execute()
|
||||
assert not any(event['stage'] == 'complete' for event in events)
|
||||
assert execute()['status'] == 'PASS'
|
||||
assert any(event['stage'] == 'icdb_address_remaining' for event in events)
|
||||
|
||||
|
||||
def test_cooperative_supervisor_loss_keeps_pending_save_helper_alive(tmp_path):
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
"""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
|
||||
@@ -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']
|
||||
@@ -258,7 +258,7 @@ def test_native_log_capture_generates_only_fresh_repair_evidence(tmp_path, name)
|
||||
assert log.read_text('utf-8') == ERROR
|
||||
|
||||
|
||||
def test_contents_session_loss_stops_batch_before_any_pcb_launch(tmp_path, monkeypatch):
|
||||
def test_contents_session_loss_records_each_item_without_launching_pcb_or_repair(tmp_path, monkeypatch):
|
||||
from test_verify_runner import fixture, request
|
||||
fixture(tmp_path)
|
||||
monkeypatch.setattr(runner, 'resolve_importer', lambda *_: Path('ReuseBlockImporter.exe'))
|
||||
@@ -269,11 +269,12 @@ def test_contents_session_loss_stops_batch_before_any_pcb_launch(tmp_path, monke
|
||||
results = runner.run_batch([request(tmp_path, libspec='EE', dms_login_config='login')]*2,
|
||||
verifier=lambda req: runner.run_verify(req, process_runner=content_check,
|
||||
port_check=lambda: pytest.fail('PCB must not start')))
|
||||
assert len(calls) == 1
|
||||
assert len(calls) == 2
|
||||
assert results[0].error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
assert results[0].safe_to_continue is False
|
||||
assert results[0].safe_to_continue is True
|
||||
assert 'Repair project' in results[0].stderr
|
||||
assert results[1].status == 'SKIPPED'
|
||||
assert results[1].status == 'FAIL'
|
||||
assert results[1].error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
|
||||
|
||||
def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, monkeypatch):
|
||||
@@ -302,7 +303,7 @@ def test_supervisor_enriches_generic_failure_from_fresh_native_log(tmp_path, mon
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_diagnostics.capture_failure_logs_with_timeout', failure_logs_main)
|
||||
result = runner.run_verify(request(tmp_path), port_check=lambda: (True, ''))
|
||||
assert result.error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
assert result.safe_to_continue is False
|
||||
assert result.safe_to_continue is True
|
||||
assert 'opening failed' in result.stderr
|
||||
assert '200091-CBB' in result.stderr
|
||||
assert '__ReusableBlocksSymb' in result.stderr
|
||||
|
||||
@@ -27,12 +27,11 @@ def test_network_timeout_cannot_remove_database_advertisement(tmp_path, monkeypa
|
||||
|
||||
|
||||
@pytest.mark.parametrize('state', ['LIVE', 'UNREACHABLE', 'INVALID'])
|
||||
def test_existing_database_owner_blocks_before_pcb_launch(tmp_path, monkeypatch, state):
|
||||
def test_address_probe_result_is_not_a_substitute_for_native_verify(tmp_path, monkeypatch, state):
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(adapter, 'prepare_icdb_address', lambda *a, **k: {'status': state})
|
||||
with pytest.raises(RuntimeError, match='ICDB_SESSION_UNCONFIRMED'):
|
||||
execute()
|
||||
assert not any(e['stage'] == 'pcb_launch' for e in events)
|
||||
assert execute()['status'] == 'PASS'
|
||||
assert any(e['stage'] == 'pcb_launch' for e in events)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('text', [
|
||||
@@ -114,7 +113,7 @@ def test_desktop_abort_does_not_kill_native_process_tree(monkeypatch):
|
||||
assert worker.handle in terminated
|
||||
|
||||
|
||||
def test_saved_pcb_with_retained_icdb_address_cannot_pass(tmp_path, monkeypatch):
|
||||
def test_saved_pcb_with_address_records_remains_an_execution_result_for_bridge_validation(tmp_path, monkeypatch):
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
original = adapter.subprocess.Popen
|
||||
def helper(*a, **k):
|
||||
@@ -123,9 +122,8 @@ def test_saved_pcb_with_retained_icdb_address_cannot_pass(tmp_path, monkeypatch)
|
||||
address.write_text('still owned', encoding='ascii')
|
||||
return original(*a, **k)
|
||||
monkeypatch.setattr(adapter.subprocess, 'Popen', helper)
|
||||
with pytest.raises(RuntimeError, match='ICDB_RELEASE_UNCONFIRMED'):
|
||||
execute()
|
||||
assert not any(e['stage'] == 'complete' for e in events)
|
||||
assert execute()['status'] == 'PASS'
|
||||
assert any(e['stage'] == 'icdb_address_remaining' for e in events)
|
||||
|
||||
|
||||
def test_retained_pcb_after_close_cannot_pass_or_be_killed(tmp_path, monkeypatch):
|
||||
@@ -189,7 +187,8 @@ def test_supervisor_rejects_pass_when_native_release_is_unconfirmed(tmp_path, mo
|
||||
|
||||
def test_existing_pcb_session_blocks_before_mutating_project(tmp_path, monkeypatch):
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch)
|
||||
monkeypatch.setattr(adapter, 'pcb_processes', lambda: ((20, 'ExpeditionPCB.exe', 'user design'),))
|
||||
monkeypatch.setattr(adapter, 'pcb_processes', lambda:
|
||||
((20, 'ExpeditionPCB.exe', f'ExpeditionPCB.exe "{job["pcb"]}"'),))
|
||||
with pytest.raises(RuntimeError, match='PCB_SESSION_ACTIVE'):
|
||||
execute()
|
||||
assert not events
|
||||
@@ -209,7 +208,9 @@ def test_real_windows_job_abort_preserves_native_like_child(tmp_path):
|
||||
script.write_text(
|
||||
'import subprocess, sys, time\nfrom pathlib import Path\n'
|
||||
'child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"])\n'
|
||||
f'Path({str(pid_path)!r}).write_text(str(child.pid))\n'
|
||||
f'pid_file = Path({str(pid_path)!r})\n'
|
||||
'pending = pid_file.with_suffix(".pending")\n'
|
||||
'pending.write_text(str(child.pid)); pending.replace(pid_file)\n'
|
||||
'time.sleep(30)\n', encoding='utf-8')
|
||||
worker = DesktopWorker([sys.executable, str(script)], 'CbbSafetyTest_' + uuid.uuid4().hex[:8])
|
||||
child_handle = None
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from test_maintenance_entry import case
|
||||
|
||||
|
||||
def test_verify_defaults_to_no_repair_and_does_not_retain_a_failure_lock(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, library_lock as locks, native_repair
|
||||
library = tmp_path / 'library/library.lmc'; library.parent.mkdir(); library.touch()
|
||||
req = runner.VerifyRequest(library, 'CBB', tmp_path / 'evidence')
|
||||
assert req.auto_repair is False
|
||||
marker = locks.blocker_path(library)
|
||||
marker.write_text('historical diagnostic record')
|
||||
calls = []
|
||||
result = runner.VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: session lost',
|
||||
error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: (calls.append('verify') or result))
|
||||
monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **k: pytest.fail('verify requested Repair'))
|
||||
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
|
||||
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: [])
|
||||
first = runner.run_verify(req)
|
||||
assert first.status == result.status and first.error_category == result.error_category
|
||||
assert first.release_proof['status'] == 'RELEASE_CONFIRMED'
|
||||
assert runner.run_verify(req) is result
|
||||
assert calls == ['verify', 'verify']
|
||||
assert not marker.exists()
|
||||
archive = Path(first.release_proof['evidence_path']).with_name('blocker-before-0.json')
|
||||
assert archive.read_text() == 'historical diagnostic record'
|
||||
assert locks._key(library.parent / '.library.cbb-upload.lock') not in locks._owners
|
||||
|
||||
|
||||
def test_catalog_checkpoint_does_not_disable_ordinary_verify(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', stderr='ICDB_REPAIR_REQUIRED: lost',
|
||||
error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True)),
|
||||
status_refresher=lambda names, _: {name: 'PENDING' for name in names})
|
||||
window._maintenance_recovery_view = True
|
||||
window._management_unsafe = True
|
||||
window.search_edit.setText('Beta'); window.select_visible_button.click(); window._update()
|
||||
assert window.selected_batch_button.isEnabled()
|
||||
window.verify_selected(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert len(calls) == 1 and not calls[0].maintenance and not calls[0].auto_repair
|
||||
assert window.selected_batch_button.isEnabled()
|
||||
assert 'ICDB' in window.cbb_table.item(window._row_by_name['Beta'], 3).text()
|
||||
|
||||
|
||||
def test_management_release_block_is_scoped_to_the_loaded_library(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
from cbb_refresh_verify.management import Capability
|
||||
adapter = type('Adapter', (), {
|
||||
'capabilities': lambda self: {name: Capability(True, 'available')
|
||||
for name in ('add', 'update', 'remove')}})()
|
||||
window = load_window(qtbot, tmp_path, monkeypatch, management_adapter=adapter)
|
||||
window._management_unsafe = True
|
||||
window._management_unsafe_library = (tmp_path/'library-a.lmc').resolve()
|
||||
window.lmc_edit.setText(str(tmp_path/'library-b.lmc'))
|
||||
window.loaded_path = str(tmp_path/'library-b.lmc')
|
||||
window.mentor_lmc_path = Path(window.loaded_path)
|
||||
window.select_visible_button.click()
|
||||
window._update()
|
||||
assert window.add_button.isEnabled()
|
||||
assert '上次管理操作' not in window._management_block_reason('update')
|
||||
|
||||
|
||||
def test_loading_a_different_library_clears_old_management_block(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
window = load_window(qtbot, tmp_path, monkeypatch)
|
||||
old = (tmp_path/'library-a.lmc').resolve()
|
||||
window._management_unsafe = True
|
||||
window._management_unsafe_library = old
|
||||
window.lmc_edit.setText(str(tmp_path/'library-b.lmc'))
|
||||
window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert not window._management_unsafe
|
||||
assert window._management_unsafe_library is None
|
||||
|
||||
|
||||
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
|
||||
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': f'ExpeditionPCB.exe "{other_pcb}"'},
|
||||
])
|
||||
entry.assert_native_idle(RepairTarget(project, work/'database'))
|
||||
|
||||
|
||||
def test_maintenance_guard_does_not_scan_other_cbb_projects(case, monkeypatch):
|
||||
from cbb_refresh_verify import maintenance_entry as entry
|
||||
from cbb_refresh_verify import scoped_manifest as manifests
|
||||
req, project, _ = case
|
||||
scans = []
|
||||
scan = manifests.scan_tree
|
||||
monkeypatch.setattr(manifests, 'scan_tree', lambda root, **kw: (scans.append(Path(root)) or scan(root, **kw)))
|
||||
with entry.maintenance_target_guard(req):
|
||||
pass
|
||||
assert req.lmc_file.parent not in scans
|
||||
assert scans.count(project.parent) == 2
|
||||
|
||||
|
||||
def test_repair_finishes_without_calling_verify(case, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner
|
||||
from test_maintenance_entry import patch_verification_after_fault, run_legacy_maintenance
|
||||
from test_native_repair import complete_native_success
|
||||
req, _, address = case
|
||||
def repair(target, *a, **kw):
|
||||
address.unlink()
|
||||
return complete_native_success(target)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', repair)
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release'])
|
||||
patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('maintenance started CBB verification'))
|
||||
result = run_legacy_maintenance(req)
|
||||
assert result.status == 'REPAIRED' and result.safe_to_continue
|
||||
assert result.library_flag == 'UNKNOWN'
|
||||
assert result.recovery_attempts[0]['validation_passed'] is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize('stage,expected_safe', [('PCB_STARTING', True), ('back_annotation', True)])
|
||||
def test_released_failure_can_continue_from_icdb_diagnosis(tmp_path, monkeypatch, stage, expected_safe):
|
||||
import json
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
library = tmp_path/'library.lmc'; library.touch()
|
||||
evidence = tmp_path/'evidence'; evidence.mkdir()
|
||||
report = evidence/'result.json'; report.write_text('{}')
|
||||
(evidence/'session-release.json').write_text(json.dumps({'library': str(library),
|
||||
'block': 'CBB', 'worker_exited': True, 'safe_to_continue': True}))
|
||||
failure = runner.VerifyResult('FAIL', stderr='ICDB_REPAIR_REQUIRED: lost session',
|
||||
error_stage=stage, safe_to_continue=False, evidence_path=report)
|
||||
monkeypatch.setattr(runner, '_run_verify', lambda *a, **k: failure)
|
||||
result = runner._run_verify_serialized(runner.VerifyRequest(library, 'CBB', evidence))
|
||||
assert result.status == 'FAIL' and result.error_category == 'ICDB_REPAIR_REQUIRED'
|
||||
assert result.safe_to_continue is expected_safe
|
||||
|
||||
|
||||
def test_live_writer_keeps_execution_lock_without_a_permanent_diagnostic_blocker(tmp_path, monkeypatch):
|
||||
import win32con, win32file
|
||||
from cbb_refresh_verify import library_lock as locks, verify_runner as runner
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k:
|
||||
runner.VerifyResult('TIMEOUT', safe_to_continue=False))
|
||||
result = runner.run_verify(runner.VerifyRequest(library, 'CBB', tmp_path/'evidence'))
|
||||
lock = locks.LibraryLock(library)
|
||||
try:
|
||||
assert not result.safe_to_continue and not locks.blocker_path(library).exists()
|
||||
with pytest.raises(Exception):
|
||||
win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, 0, None)
|
||||
finally:
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(lock.key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
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
|
||||
request, project, address = case
|
||||
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 '未发现 ICDB 错误' in result.stdout
|
||||
assert not locks.blocker_path(request.lmc_file).exists()
|
||||
with locks.LibraryLock(request.lmc_file): pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation', ['verify', 'maintenance'])
|
||||
def test_retained_execution_lease_can_retry_after_its_actual_client_exits(tmp_path, operation):
|
||||
from contextlib import contextmanager
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
|
||||
library = tmp_path/'library.lmc'; library.touch()
|
||||
released = [False]
|
||||
with LibraryLock(library, checkpoint=False, safe_to_release=lambda: released[0]): pass
|
||||
with pytest.raises(LibraryBusy):
|
||||
with LibraryLock(library, checkpoint=False): pass
|
||||
released[0] = True
|
||||
@contextmanager
|
||||
def guard(): yield {'target': 'isolated fixture'}
|
||||
lock = (LibraryLock(library, checkpoint=False) if operation == 'verify' else
|
||||
LibraryLock.for_maintenance(library, tmp_path/'archive', guard, safe_to_release=lambda: True))
|
||||
with lock: pass
|
||||
@@ -290,6 +290,45 @@ def test_refresh_integration_skips_native_disabled_green_action():
|
||||
assert pi.actions == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('ces_allowed', [True, False])
|
||||
def test_partial_sync_respects_protected_green_native_actions(ces_allowed):
|
||||
class RestoredVerifiedBlock(GreenProjectIntegration):
|
||||
IsForwardAnnotationAllowed = False
|
||||
BackAnnotationStatus = 1
|
||||
|
||||
def ForwardAnnotate(self):
|
||||
raise RuntimeError('Forward Annotation is not allowed on verified Reusable Blocks')
|
||||
|
||||
def BackAnnotate(self):
|
||||
super().BackAnnotate()
|
||||
self.BackAnnotationStatus = 2
|
||||
return True
|
||||
|
||||
pi = RestoredVerifiedBlock()
|
||||
pi.IsSynchCESAllowed = pi.IsLoadCESAllowed = ces_allowed
|
||||
events = []
|
||||
result = refresh_integration(pi, settle_seconds=0, emit=lambda **e: events.append(e))
|
||||
assert set(result.values()) == {2}
|
||||
assert pi.actions == (['SynchCES', 'LoadCES', 'BackAnnotate'] if ces_allowed else ['BackAnnotate'])
|
||||
assert [e['action'] for e in events if e['stage'] == 'integration_action'] == pi.actions
|
||||
|
||||
|
||||
def test_skipped_green_action_cannot_hide_a_later_required_native_gate():
|
||||
class ChangedAfterCES(GreenProjectIntegration):
|
||||
IsForwardAnnotationAllowed = False
|
||||
BackAnnotationStatus = 1
|
||||
|
||||
def SynchCES(self):
|
||||
super().SynchCES()
|
||||
self.ForwardAnnotationStatus = 1
|
||||
return True
|
||||
|
||||
pi = ChangedAfterCES()
|
||||
with pytest.raises(IntegrationError, match='ForwardAnnotation: required but not allowed'):
|
||||
refresh_integration(pi, settle_seconds=0)
|
||||
assert pi.actions == ['SynchCES']
|
||||
|
||||
|
||||
def test_refresh_integration_never_counts_rejected_green_action_as_completed():
|
||||
class VerifiedProjectIntegration(GreenProjectIntegration):
|
||||
def _blocked(self, action):
|
||||
|
||||
@@ -0,0 +1,286 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import pytest
|
||||
import win32con
|
||||
import win32file
|
||||
|
||||
|
||||
def test_library_lock_excludes_other_thread_and_uploader_but_reenters(tmp_path):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
|
||||
library = tmp_path/'test.lmc'
|
||||
library.write_bytes(b'fixture')
|
||||
with LibraryLock(library) as lock:
|
||||
assert lock.path.name == '.test.cbb-upload.lock'
|
||||
with LibraryLock(library):
|
||||
assert lock.snapshot_bytes()
|
||||
with pytest.raises(Exception):
|
||||
win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, 0, None)
|
||||
def competing():
|
||||
with pytest.raises(LibraryBusy):
|
||||
with LibraryLock(library):
|
||||
pass
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
pool.submit(competing).result()
|
||||
assert lock.path.exists()
|
||||
with LibraryLock(library):
|
||||
pass
|
||||
|
||||
|
||||
def test_complete_copy_reads_owned_lock_without_reopening(tmp_path):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock
|
||||
from cbb_refresh_verify.scoped_manifest import copy_verified
|
||||
source = tmp_path/'library'
|
||||
source.mkdir()
|
||||
library = source/'test.lmc'
|
||||
library.write_bytes(b'fixture')
|
||||
with LibraryLock(library) as lock:
|
||||
expected = lock.snapshot_bytes()
|
||||
copy_verified(source, tmp_path/'snapshot')
|
||||
assert (tmp_path/'snapshot'/lock.path.name).read_bytes() == expected
|
||||
|
||||
|
||||
def test_external_lock_never_gets_snapshot_exception(tmp_path):
|
||||
from cbb_refresh_verify.scoped_manifest import copy_verified
|
||||
source = tmp_path/'library'
|
||||
source.mkdir()
|
||||
path = source/'.test.cbb-upload.lock'
|
||||
path.write_bytes(b'external')
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_EXISTING, 0, None)
|
||||
try:
|
||||
with pytest.raises(OSError):
|
||||
copy_verified(source, tmp_path/'snapshot')
|
||||
finally:
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_verification_refuses_uploader_exclusive_lock_before_native_work(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
library = tmp_path/'lib.lmc'
|
||||
library.touch()
|
||||
lock = tmp_path/'.lib.cbb-upload.lock'
|
||||
handle = win32file.CreateFile(str(lock), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, 0, None)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: pytest.fail('native work bypassed lock'))
|
||||
try:
|
||||
result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
||||
assert result.status == 'FAIL' and result.safe_to_continue
|
||||
finally:
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_unsafe_verify_records_failure_without_persistent_library_blocker(tmp_path, monkeypatch):
|
||||
import json
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
evidence = tmp_path/'out'; evidence.mkdir()
|
||||
(evidence/'session-release.json').write_text(json.dumps({'library': str(library),
|
||||
'block': 'Block', 'worker_exited': True, 'safe_to_continue': True}))
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k:
|
||||
runner.VerifyResult('FAIL', safe_to_continue=False, evidence_path=evidence/'result.json'))
|
||||
result = runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
||||
assert not result.safe_to_continue
|
||||
assert not locks.blocker_path(library).exists()
|
||||
with locks.LibraryLock(library): pass
|
||||
|
||||
|
||||
def test_explicit_release_requires_fresh_proof_and_never_deletes_sidecar(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
|
||||
with locks.LibraryLock(library, safe_to_release=lambda: False) as lease:
|
||||
pass
|
||||
try:
|
||||
def denied(*a):
|
||||
raise ValueError('native owner remains')
|
||||
monkeypatch.setattr(locks, '_prove_library_released', denied)
|
||||
with pytest.raises(ValueError, match='native owner'):
|
||||
locks.release_retained_library(library, tmp_path/'release')
|
||||
assert locks.blocker_path(library).is_file()
|
||||
from contextlib import contextmanager
|
||||
@contextmanager
|
||||
def proof(*a):
|
||||
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
|
||||
monkeypatch.setattr(locks, '_prove_library_released', proof)
|
||||
result = locks.release_retained_library(library, tmp_path/'release')
|
||||
assert result['status'] == 'RELEASE_CONFIRMED'
|
||||
assert lease.path.is_file() and not locks.blocker_path(library).exists()
|
||||
with locks.LibraryLock(library): pass
|
||||
finally:
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(str(lease.path.resolve()).casefold(), None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def test_release_proof_checks_addresses_and_exclusive_databases(tmp_path, monkeypatch):
|
||||
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()
|
||||
address = tmp_path/'database/cdbsvr/sAddress.adr'; address.parent.mkdir(parents=True)
|
||||
address.touch()
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks._prove_library_released(library): pass
|
||||
address.unlink() # Test-created empty marker, never a native address file.
|
||||
handle = win32file.CreateFile(str(library), win32con.GENERIC_READ, 0, None,
|
||||
win32con.OPEN_EXISTING, 0, None)
|
||||
try:
|
||||
with pytest.raises(Exception):
|
||||
with locks._prove_library_released(library): pass
|
||||
finally:
|
||||
handle.Close()
|
||||
with locks._prove_library_released(library) as proof:
|
||||
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
|
||||
monkeypatch.setattr(probe, 'query_owners', lambda *_: [])
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
other = tmp_path/'ReusableBlocksLP/Other/Work/database/cdbsvr/sAddress.adr'
|
||||
other.parent.mkdir(parents=True); other.touch()
|
||||
with locks._prove_library_released(library, block_name='Target') as proof:
|
||||
assert proof['addresses'] == []
|
||||
|
||||
|
||||
def test_release_probe_timeout_does_not_clear_retained_checkpoint(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
import cbb_refresh_verify.owner_probe as probe
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
|
||||
with locks.LibraryLock(library, safe_to_release=lambda: False) as lease: pass
|
||||
marker = locks.blocker_path(library)
|
||||
before = marker.read_bytes()
|
||||
def timeout(*args): raise RuntimeError('OWNER_QUERY_TIMEOUT')
|
||||
monkeypatch.setattr(probe, 'query_owners', timeout)
|
||||
try:
|
||||
with pytest.raises(RuntimeError, match='OWNER_QUERY_TIMEOUT'):
|
||||
locks.release_retained_library(library, tmp_path/'release')
|
||||
assert marker.read_bytes() == before
|
||||
finally:
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(lease.key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name,command', [
|
||||
('cscript.exe', 'cscript.exe close_failed_block.vbs'),
|
||||
('python.exe', 'python.exe -m cbb_refresh_verify --repair-worker job.json'),
|
||||
])
|
||||
def test_release_proof_waits_for_late_operation_helpers(tmp_path, monkeypatch, name, command):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
import cbb_refresh_verify.owner_probe as probe
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
monkeypatch.setattr(probe, 'query_owners', lambda names:
|
||||
[{'pid': 123456, 'name': name, 'command': command}] if name in names else [])
|
||||
with pytest.raises(locks.LibraryBusy, match='123456'):
|
||||
with locks._prove_library_released(library): pass
|
||||
|
||||
|
||||
def test_release_rechecks_owners_after_database_handles_are_acquired(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
import cbb_refresh_verify.owner_probe as probe
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
calls = []
|
||||
def query(names):
|
||||
calls.append(1)
|
||||
return [] if len(calls) == 1 else [{'pid': 123456, 'name': 'cscript.exe', 'command': 'helper'}]
|
||||
monkeypatch.setattr(probe, 'query_owners', query)
|
||||
with pytest.raises(locks.LibraryBusy, match='123456'):
|
||||
with locks._prove_library_released(library): pass
|
||||
|
||||
|
||||
def test_public_verify_initializes_com_in_its_operation_thread(tmp_path, monkeypatch):
|
||||
import ctypes
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
def serialized(*a, **k):
|
||||
apartment, qualifier = ctypes.c_int(), ctypes.c_int()
|
||||
ole32 = ctypes.WinDLL('ole32')
|
||||
ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)]
|
||||
initialized = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0
|
||||
return runner.VerifyResult('PASS' if initialized else 'FAIL', stderr='COM apartment check')
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', serialized)
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
result = pool.submit(runner.run_verify, runner.VerifyRequest(library, 'Block', tmp_path/'out')).result()
|
||||
assert result.status == 'PASS'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode,expected', [(0, 1), (2, 0)])
|
||||
def test_public_verify_preserves_existing_apartment(tmp_path, monkeypatch, mode, expected):
|
||||
import ctypes, pythoncom
|
||||
import cbb_refresh_verify.verify_runner as runner
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k: runner.VerifyResult('PASS'))
|
||||
def existing_mta():
|
||||
pythoncom.CoInitializeEx(mode)
|
||||
try:
|
||||
apartment, qualifier = ctypes.c_int(), ctypes.c_int()
|
||||
ole32 = ctypes.WinDLL('ole32')
|
||||
ole32.CoGetApartmentType.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)]
|
||||
assert ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier)) == 0
|
||||
before = apartment.value
|
||||
# A thread can be the main STA (3) when it creates the process's
|
||||
# first remaining STA. Verify must preserve either STA flavor.
|
||||
assert before in ((1,) if mode == 0 else (0, 3))
|
||||
runner.run_verify(runner.VerifyRequest(library, 'Block', tmp_path/'out'))
|
||||
result = ole32.CoGetApartmentType(ctypes.byref(apartment), ctypes.byref(qualifier))
|
||||
return result, apartment.value, before
|
||||
finally:
|
||||
pythoncom.CoUninitialize()
|
||||
with ThreadPoolExecutor(1) as pool:
|
||||
result, after, before = pool.submit(existing_mta).result()
|
||||
assert result == 0 and after == before
|
||||
|
||||
|
||||
def test_nested_snapshot_retains_runtime_checkpoint_bytes_without_semantic_conflicts(tmp_path):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
from cbb_refresh_verify.scoped_manifest import copy_verified, build_manifest, ManifestScope
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.write_bytes(b'library')
|
||||
scope = ManifestScope.library('Block', library.name)
|
||||
with locks.LibraryLock(library) as lease:
|
||||
before = copy_verified(library.parent, tmp_path/'snapshot', scope=scope)
|
||||
marker = locks.blocker_path(library)
|
||||
assert (tmp_path/'snapshot'/marker.name).read_bytes() == marker.read_bytes()
|
||||
with locks.LibraryLock(library):
|
||||
owner = locks._owners[lease.key]
|
||||
locks._persist_blocker(library, lease_id=owner['lease_id'])
|
||||
assert build_manifest(library.parent, scope=scope) == before
|
||||
assert not marker.exists() and lease.path.exists()
|
||||
|
||||
|
||||
def test_arbitrary_guard_filename_is_preserved_and_primary(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
from cbb_refresh_verify.scoped_manifest import build_manifest, ManifestScope
|
||||
library = tmp_path/'library/lib.lmc'; library.parent.mkdir(); library.touch()
|
||||
marker = locks.blocker_path(library); marker.write_bytes(b'{not a guard record')
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks.LibraryLock(library): pass
|
||||
manifest = build_manifest(library.parent, scope=ManifestScope.library('Block', library.name))
|
||||
assert marker.name in manifest['files']
|
||||
from contextlib import contextmanager
|
||||
@contextmanager
|
||||
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')
|
||||
assert marker.read_bytes() == b'{not a guard record'
|
||||
@@ -0,0 +1,84 @@
|
||||
"""Native occupancy must precede repair even when old addresses remain."""
|
||||
import pytest
|
||||
from test_maintenance_entry import case, no_native_launch, startup_failure, run_legacy_maintenance
|
||||
from test_native_occupancy import output
|
||||
|
||||
|
||||
def inspection(target, *, fault=False):
|
||||
return {'inspection_only': True, 'repair_requested': False, 'native_pid': 777,
|
||||
'native_exit_code': 0, 'project': str(target.project), 'database': str(target.database),
|
||||
'output': output(target.project, target.database, fault=fault),
|
||||
'status': 'NOT_IN_USE', 'repair_required': fault, 'release': {'native_exited': True}}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('old_address', [False, True])
|
||||
def test_diagnosed_native_inspection_holds_lock_after_opening_pcb(case, monkeypatch, old_address):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair, library_lock
|
||||
request, project, address = case
|
||||
if not old_address: address.unlink()
|
||||
calls = []
|
||||
def startup(req, **kw):
|
||||
assert req.diagnostic_only
|
||||
calls.append('open')
|
||||
return startup_failure(req)
|
||||
def native(target, root, **kw):
|
||||
assert calls == ['open']
|
||||
key = library_lock.LibraryLock(request.lmc_file).key
|
||||
assert library_lock._owners[key]['depth'] > 0
|
||||
assert kw['inspection_only'] is True
|
||||
assert kw['timeout_seconds'] <= 30
|
||||
return inspection(target)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', native)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', startup)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status == 'CHECKED' and result.recovery_attempts == ()
|
||||
assert result.library_flag == 'UNKNOWN'
|
||||
assert result.safe_to_continue is True
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fault', ['missing', 'generic', 'assertion', 'wrong_database', 'stale_verdict', 'not_exited'])
|
||||
def test_unconfirmed_native_inspection_never_repairs(case, monkeypatch, fault):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
request, project, address = case
|
||||
def native(target, root, **kw):
|
||||
assert kw['inspection_only'] is True
|
||||
if fault == 'assertion': raise AssertionError('probe failed')
|
||||
if fault == 'missing': return {}
|
||||
record = inspection(target, fault=True)
|
||||
if fault == 'generic': record['output'] = 'DATABASE_SAFETY_STOP'
|
||||
if fault == 'wrong_database': record['database'] = str(project.parent/'other')
|
||||
if fault == 'stale_verdict': record['output'] += 'Checking if project is in use.'
|
||||
if fault == 'not_exited': record['release']['native_exited'] = False
|
||||
return record
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', native)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status == 'BLOCKED' and not result.recovery_attempts
|
||||
|
||||
|
||||
def test_native_fault_repairs_once_without_running_separate_verification(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
from test_native_repair import complete_native_success
|
||||
request, project, address = case
|
||||
calls = []
|
||||
def native(target, root, **kw):
|
||||
if kw.get('inspection_only'):
|
||||
assert address.exists()
|
||||
calls.append('inspect')
|
||||
return inspection(target, fault=True)
|
||||
calls.append('repair')
|
||||
address.unlink() # Simulated native repair release only.
|
||||
return complete_native_success(target)
|
||||
def verify(req, **kw):
|
||||
if req.diagnostic_only:
|
||||
calls.append('open')
|
||||
return startup_failure(req)
|
||||
assert req.post_repair_proof is not None and not req.diagnostic_only and not req.auto_repair
|
||||
calls.append('verify')
|
||||
return runner.VerifyResult('PASS', library_flag='GREEN')
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', native)
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release'])
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', verify)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert calls == ['open', 'inspect', 'repair']
|
||||
assert result.status == 'REPAIRED' and len(result.recovery_attempts) == 1
|
||||
assert result.library_flag == 'UNKNOWN' and not result.recovery_attempts[0]['validation_passed']
|
||||
@@ -0,0 +1,464 @@
|
||||
"""Explicit maintenance for preexisting advertisements; native writes forbidden."""
|
||||
from dataclasses import replace
|
||||
from contextlib import contextmanager
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def no_native_launch(monkeypatch):
|
||||
import subprocess, win32process, win32com.client
|
||||
import cbb_refresh_verify.desktop_worker as desktop
|
||||
def forbidden(*a, **k): raise AssertionError('No native launch allowed in maintenance tests')
|
||||
monkeypatch.setattr(subprocess, 'Popen', forbidden)
|
||||
monkeypatch.setattr(win32process, 'CreateProcess', forbidden)
|
||||
monkeypatch.setattr(win32com.client, 'Dispatch', forbidden)
|
||||
monkeypatch.setattr(win32com.client, 'DispatchEx', forbidden)
|
||||
monkeypatch.setattr(desktop, 'DesktopWorker', forbidden)
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_runner.runtime_fingerprint', lambda *_: {})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def case(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest
|
||||
import cbb_refresh_verify.maintenance_entry as entry
|
||||
library = tmp_path/'library/mentor.lmc'; library.parent.mkdir(); library.write_bytes(b'library')
|
||||
work = library.parent/'ReusableBlocksLP/IP101GR/Work'
|
||||
(work/'database/cdbsvr').mkdir(parents=True)
|
||||
(work/'database/icdb.dat').write_bytes(b'schematic')
|
||||
address = work/'database/cdbsvr/sAddress.adr'; address.write_bytes(b'old server address - preserved')
|
||||
(work/'PCB/Layout').mkdir(parents=True)
|
||||
(work/'PCB/Layout/LayoutDB.lyt').write_bytes(b'layout')
|
||||
project = work/'IP101GR.prj'
|
||||
project.write_text('KEY iCDBDir ".\\database"\nKEY PCBDesignPath "PCB/IP101GR.pcb"\n')
|
||||
(work/'PCB/IP101GR.pcb').write_text('KEY ProjectFile "../IP101GR.prj"\n')
|
||||
(work/'PCB/pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n')
|
||||
monkeypatch.setattr(entry, 'assert_native_idle', lambda: None)
|
||||
request = VerifyRequest(library, 'IP101GR', tmp_path/'evidence', maintenance=True)
|
||||
monkeypatch.setattr('cbb_refresh_verify.verify_runner._run_verify_serialized',
|
||||
lambda req, **kw: startup_failure(req))
|
||||
yield request, project, address
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
key = locks.LibraryLock(library).key
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
|
||||
|
||||
def startup_failure(request):
|
||||
"""Stub the PCB process boundary with a complete current native failure."""
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
database = request.lmc_file.parent/'ReusableBlocksLP'/request.block_name/'Work/database'
|
||||
return VerifyResult('FAIL', safe_to_continue=True, error_category='ICDB_REPAIR_REQUIRED',
|
||||
stderr=("The last server session on different server machine lost the connection to the database. "
|
||||
"Please use iCDB Project Backup 'Repair project' functionality to fix this issue. "
|
||||
f"Machine:[fixture-server] Path:[{database}] session:[fixture-current-session]"))
|
||||
|
||||
|
||||
def run_legacy_maintenance(request, **kwargs):
|
||||
"""Keep explicit recovery helper coverage separate from the batch-check API."""
|
||||
from cbb_refresh_verify import verify_runner as runner, maintenance_entry as entry
|
||||
if not request.maintenance:
|
||||
return runner.run_verify(request, **kwargs)
|
||||
return entry.run_diagnosed_maintenance(request,
|
||||
diagnose=lambda probe: runner.run_verify(probe, **kwargs),
|
||||
progress=kwargs.get('progress'), stop=kwargs.get('stop'))
|
||||
|
||||
|
||||
def patch_verification_after_fault(monkeypatch, runner, verify):
|
||||
"""Inject only native process boundaries; production interprets the Output."""
|
||||
from cbb_refresh_verify import native_repair
|
||||
from test_native_occupancy import output
|
||||
repair = native_repair.run_native_repair
|
||||
def native(target, root, **kw):
|
||||
if not kw.get('inspection_only'):
|
||||
return repair(target, root, **kw)
|
||||
return {'inspection_only': True, 'repair_requested': False, 'native_pid': 777,
|
||||
'native_exit_code': 0, 'project': str(target.project), 'database': str(target.database),
|
||||
'output': output(target.project, target.database, fault=True),
|
||||
'status': 'NOT_IN_USE', 'repair_required': True, 'release': {'native_exited': True}}
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', native)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized',
|
||||
lambda req, **kw: startup_failure(req) if req.diagnostic_only else verify(req, **kw))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('pcb_config', ['standard', 'legacy_template'])
|
||||
@pytest.mark.parametrize('advertisement', ['present', 'absent'])
|
||||
def test_diagnosed_maintenance_enters_official_repair_once(case, monkeypatch, pcb_config, advertisement):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
from test_native_repair import complete_native_success
|
||||
request, project, address = case
|
||||
if advertisement == 'absent':
|
||||
address.unlink()
|
||||
if pcb_config == 'legacy_template':
|
||||
(project.parent/'PCB/pcb.prj').rename(project.parent/'PCB/template.prj')
|
||||
calls = []
|
||||
def repair(target, root, **kwargs):
|
||||
calls.append('repair')
|
||||
if advertisement == 'present':
|
||||
assert address.read_bytes() == b'old server address - preserved'
|
||||
assert kwargs['maintenance_context']['addresses'][str(address.resolve())]['sha256']
|
||||
address.unlink() # Simulated native release, never a production cleanup.
|
||||
else:
|
||||
assert kwargs['maintenance_context']['addresses'] == {}
|
||||
assert target.project == project
|
||||
return complete_native_success(target)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', repair)
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a:
|
||||
{'safe_to_continue': True, 'native_exited': True, 'addresses': []})
|
||||
def verify(req, **kwargs):
|
||||
calls.append('verify')
|
||||
from cbb_refresh_verify.post_repair_consent import PostRepairProof
|
||||
assert isinstance(req.post_repair_proof, PostRepairProof)
|
||||
assert not req.maintenance and not req.auto_repair
|
||||
assert req.lmc_file == request.lmc_file and req.block_name == request.block_name
|
||||
return runner.VerifyResult('PASS', library_flag='GREEN')
|
||||
patch_verification_after_fault(monkeypatch, runner, verify)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert calls == ['repair'] and result.status == 'REPAIRED'
|
||||
assert result.recovery_attempts[0]['validation_passed'] is False
|
||||
assert result.library_flag == 'UNKNOWN'
|
||||
record = json.loads(result.evidence_path.read_text(encoding='utf-8'))
|
||||
assert record['operation'] == 'explicit_project_maintenance'
|
||||
assert 'session' not in record['maintenance_context']
|
||||
|
||||
|
||||
def test_addressless_target_binds_unique_project_and_rejects_new_address(case):
|
||||
from cbb_refresh_verify.maintenance_entry import maintenance_target_guard
|
||||
request, project, address = case
|
||||
address.unlink()
|
||||
with maintenance_target_guard(request) as checked:
|
||||
context = checked['context']
|
||||
assert checked['target'].project == project and context['addresses'] == {}
|
||||
address.write_bytes(b'owner appeared after preflight')
|
||||
with pytest.raises(ValueError, match='MAINTENANCE_SOURCE_CHANGED'):
|
||||
with maintenance_target_guard(request, context): pass
|
||||
|
||||
|
||||
def test_guard_reuses_full_tree_scan_but_still_refreshes_before_yield(case, monkeypatch):
|
||||
from cbb_refresh_verify.maintenance_entry import maintenance_target_guard
|
||||
import cbb_refresh_verify.scoped_manifest as manifests
|
||||
request, project, _ = case
|
||||
scans = []
|
||||
original = manifests.scan_tree
|
||||
def scan(root, **kwargs):
|
||||
scans.append(Path(root))
|
||||
return original(root, **kwargs)
|
||||
monkeypatch.setattr(manifests, 'scan_tree', scan)
|
||||
with maintenance_target_guard(request): pass
|
||||
assert request.lmc_file.parent not in scans
|
||||
assert scans.count(project.parent) == 2
|
||||
|
||||
|
||||
def test_guard_rejects_new_native_database_appearing_during_checks(case, monkeypatch):
|
||||
import cbb_refresh_verify.maintenance_entry as entry
|
||||
request, _, _ = case
|
||||
calls = []
|
||||
def idle():
|
||||
calls.append(1)
|
||||
if len(calls) == 2:
|
||||
(request.lmc_file.parent/'new.lmc').write_bytes(b'new native store')
|
||||
monkeypatch.setattr(entry, 'assert_native_idle', idle)
|
||||
with pytest.raises(ValueError, match='MAINTENANCE_SOURCE_CHANGED'):
|
||||
with entry.maintenance_target_guard(request): pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reference', ['', 'KEY ProjectFile "../Other.prj"\n',
|
||||
'KEY ProjectFile "../IP101GR.prj"\nKEY ProjectFile "../IP101GR.prj"\n'])
|
||||
def test_addressless_target_requires_exact_pcb_back_reference(case, reference):
|
||||
from cbb_refresh_verify.maintenance_entry import select_maintenance_target
|
||||
request, project, address = case
|
||||
address.unlink()
|
||||
(project.parent/'PCB/IP101GR.pcb').write_text(reference)
|
||||
with pytest.raises(ValueError, match='PCB.*ProjectFile'):
|
||||
select_maintenance_target(request)
|
||||
|
||||
|
||||
def test_addressless_multiple_project_databases_are_not_guessed(case):
|
||||
from cbb_refresh_verify.maintenance_entry import select_maintenance_target
|
||||
request, project, address = case
|
||||
address.unlink()
|
||||
(project.parent/'PCB/pcb.prj').write_text('KEY iCDBDir "database"\n')
|
||||
database = project.parent/'PCB/database'; database.mkdir()
|
||||
(database/'icdb.dat').write_bytes(b'another native database')
|
||||
with pytest.raises(ValueError, match='多个工程数据库'):
|
||||
select_maintenance_target(request)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('back_reference', ['', 'KEY ProjectFile "../other.prj"\n',
|
||||
'KEY ProjectFile "../IP101GR.prj"\nKEY ProjectFile "../IP101GR.prj"\n'])
|
||||
def test_legacy_maintenance_requires_unambiguous_pcb_back_reference(case, back_reference):
|
||||
from cbb_refresh_verify.maintenance_entry import select_maintenance_target
|
||||
request, project, _ = case
|
||||
(project.parent/'PCB/pcb.prj').rename(project.parent/'PCB/template.prj')
|
||||
(project.parent/'PCB/IP101GR.pcb').write_text(back_reference)
|
||||
with pytest.raises(ValueError, match='MAINTENANCE_TARGET_UNCONFIRMED'):
|
||||
select_maintenance_target(request)
|
||||
|
||||
|
||||
def test_legacy_target_does_not_guess_template_for_physical_address(case):
|
||||
from cbb_refresh_verify.maintenance_entry import select_maintenance_target
|
||||
request, project, address = case
|
||||
(project.parent/'PCB/pcb.prj').rename(project.parent/'PCB/template.prj')
|
||||
address.unlink()
|
||||
physical = project.parent/'PCB/database/cdbsvr/sAddress.adr'
|
||||
physical.parent.mkdir(parents=True); physical.write_bytes(b'physical owner')
|
||||
with pytest.raises(ValueError, match='SHARED_DATABASE_REPAIR_UNSUPPORTED'):
|
||||
select_maintenance_target(request)
|
||||
|
||||
|
||||
def test_optional_pcb_project_appearance_invalidates_maintenance_proof(case):
|
||||
from cbb_refresh_verify.maintenance_entry import maintenance_target_guard
|
||||
request, project, _ = case
|
||||
config = project.parent/'PCB/pcb.prj'
|
||||
config.rename(config.with_name('template.prj'))
|
||||
with maintenance_target_guard(request) as proof:
|
||||
before = proof['context']
|
||||
config.write_text('KEY CentralLibrary "different.lmc"\n')
|
||||
with pytest.raises(ValueError, match='MAINTENANCE_SOURCE_CHANGED'):
|
||||
with maintenance_target_guard(request, before): pass
|
||||
|
||||
|
||||
def test_ordinary_verify_calls_native_verifier_despite_existing_address(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
request, _, address = case
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('ordinary verify must not repair'))
|
||||
calls = []
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **k:
|
||||
(calls.append('verify') or runner.VerifyResult('FAIL', stderr='native ICDB problem')))
|
||||
result = run_legacy_maintenance(replace(request, maintenance=False))
|
||||
assert calls == ['verify'] and result.status == 'FAIL' and result.safe_to_continue
|
||||
assert address.read_bytes() == b'old server address - preserved'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('obstacle', ['active_owner', 'exclusive', 'shared_address', 'ambiguous', 'address_mutation'])
|
||||
def test_maintenance_guard_blocks_before_native(case, monkeypatch, obstacle):
|
||||
from cbb_refresh_verify import maintenance_entry as entry, native_repair, verify_runner as runner
|
||||
import win32file, win32con
|
||||
request, project, address = case
|
||||
handle = None
|
||||
if obstacle == 'active_owner':
|
||||
monkeypatch.setattr(entry, 'assert_native_idle', lambda: (_ for _ in ()).throw(ValueError('native owner')))
|
||||
if obstacle == 'exclusive':
|
||||
handle = win32file.CreateFile(str(project.parent/'database/icdb.dat'), win32con.GENERIC_READ,
|
||||
0, None, win32con.OPEN_EXISTING, 0, None)
|
||||
if obstacle == 'shared_address':
|
||||
shared = request.lmc_file.parent/'SymbolLibs/__ReusableBlocksSymb/cdbsvr/sAddress.adr'
|
||||
shared.parent.mkdir(parents=True); shared.write_bytes(b'shared owner')
|
||||
if obstacle == 'ambiguous': project.write_text(project.read_text() + 'KEY iCDBDir "other"\n')
|
||||
if obstacle == 'address_mutation':
|
||||
original = entry.address_snapshot
|
||||
def mutate(paths):
|
||||
result = original(paths)
|
||||
address.write_bytes(b'changed after snapshot')
|
||||
return result
|
||||
monkeypatch.setattr(entry, 'address_snapshot', mutate)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: pytest.fail('guard bypass'))
|
||||
try:
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status == 'BLOCKED' and not result.safe_to_continue
|
||||
assert result.error_stage == 'preflight'
|
||||
finally:
|
||||
if handle: handle.Close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['dead', 'same_process', 'live_foreign', 'malformed', 'foreign_library', 'guard_failure'])
|
||||
def test_takeover_preserves_old_checkpoint_until_every_guard_passes(case, monkeypatch, change):
|
||||
from cbb_refresh_verify import library_lock as locks, maintenance_entry as entry
|
||||
request, _, address = case
|
||||
library = request.lmc_file
|
||||
with locks.LibraryLock(library, safe_to_release=lambda: False): pass
|
||||
marker = locks.blocker_path(library)
|
||||
record = json.loads(marker.read_text(encoding='utf-8'))
|
||||
if change != 'same_process':
|
||||
with locks._guard: locks._owners.pop(locks.LibraryLock(library).key)['handle'].Close()
|
||||
record['pid'] = 789123
|
||||
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, '_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'
|
||||
guard = lambda: entry.maintenance_target_guard(request)
|
||||
if change in ('dead', 'same_process'):
|
||||
lease = locks.LibraryLock.for_maintenance(library, archive, guard, safe_to_release=lambda: True)
|
||||
with lease:
|
||||
assert (archive/'previous-checkpoint.json').read_bytes() == old
|
||||
assert address.exists()
|
||||
import win32file, win32con
|
||||
with pytest.raises(Exception):
|
||||
win32file.CreateFile(str(lease.path), win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
|
||||
assert lease.path.exists() and not marker.exists()
|
||||
else:
|
||||
with pytest.raises((ValueError, OSError)):
|
||||
with locks.LibraryLock.for_maintenance(library, archive, guard): pass
|
||||
assert marker.read_bytes() == old
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status', ['MISSING_HEALTHY_BACKUP', 'RESTORED_REVIEW_REQUIRED', 'DATABASE_BUSY', 'TIMEOUT_UNRELEASED'])
|
||||
def test_nonrepair_outcomes_never_verify_or_count_success(case, monkeypatch, status):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner
|
||||
request, _, _ = case
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **k: {
|
||||
'status': status, 'release': {'safe_to_continue': False}, 'detail': 'native result'})
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda *a: {'safe_to_continue': False})
|
||||
patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('unsafe or nonrepair outcome reopened'))
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status != 'PASS' and not result.safe_to_continue
|
||||
assert result.recovery_attempts[0]['status'] == status
|
||||
|
||||
|
||||
def test_native_success_cannot_bypass_remaining_address(case, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner
|
||||
from test_native_repair import complete_native_success
|
||||
request, _, _ = case
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda target, *a, **k: complete_native_success(target))
|
||||
patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('retained address reopened'))
|
||||
# Native release queries remain mocked, actual address/exclusive checks stay real.
|
||||
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter.pcb_processes', lambda: [])
|
||||
monkeypatch.setattr('cbb_refresh_verify.mentor_adapter._process_details', lambda name: [])
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status != 'PASS' and not result.safe_to_continue
|
||||
|
||||
|
||||
@pytest.mark.parametrize('output', ['', 'Checking if project is in use.',
|
||||
"Project is not in use. Full iCDB Project Backup's functionality is available.",
|
||||
"Project is not in use. Full iCDB Project Backup's functionality is available.\nProject is in use."])
|
||||
def test_explicit_worker_requires_positive_native_idle_before_repair(tmp_path, monkeypatch, output):
|
||||
import win32con
|
||||
from test_native_repair import repair_worker_fixture
|
||||
import cbb_refresh_verify.maintenance_entry as entry
|
||||
from cbb_refresh_verify.native_repair import RepairTarget
|
||||
ui, root, job, actions = repair_worker_fixture(tmp_path, monkeypatch, 'active')
|
||||
values = json.loads(job.read_text())
|
||||
values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
|
||||
job.write_text(json.dumps(values))
|
||||
@contextmanager
|
||||
def guard(*args, stop=None):
|
||||
assert stop is not None
|
||||
yield {'target': RepairTarget(Path(values['project']), Path(values['database']))}
|
||||
monkeypatch.setattr(entry, 'maintenance_target_guard', guard)
|
||||
monkeypatch.setattr(ui, '_native_output', lambda windows: output)
|
||||
ui.repair_worker_main(job)
|
||||
expected = output == "Project is not in use. Full iCDB Project Backup's functionality is available."
|
||||
assert any(message == win32con.WM_COMMAND for _, message in actions) is expected
|
||||
|
||||
|
||||
def test_worker_rechecks_address_after_access_probe_before_native_launch(tmp_path, monkeypatch):
|
||||
from test_native_repair import repair_worker_fixture
|
||||
import cbb_refresh_verify.maintenance_entry as entry
|
||||
from cbb_refresh_verify.native_repair import RepairTarget
|
||||
ui, root, job, _ = repair_worker_fixture(tmp_path, monkeypatch, 'active')
|
||||
values = json.loads(job.read_text()); values['maintenance_context'] = {'library': str(tmp_path/'lib.lmc'), 'block': 'CBB1'}
|
||||
job.write_text(json.dumps(values))
|
||||
calls = []
|
||||
monkeypatch.setattr('cbb_refresh_verify.native_repair.check_repair_access',
|
||||
lambda *_: calls.append('access'))
|
||||
@contextmanager
|
||||
def guard(*args, stop=None):
|
||||
assert stop is not None
|
||||
calls.append('guard')
|
||||
assert calls == ['access', 'guard']
|
||||
raise ValueError('address changed after access probe')
|
||||
yield {'target': RepairTarget(Path(values['project']), Path(values['database']))}
|
||||
monkeypatch.setattr(entry, 'maintenance_target_guard', guard)
|
||||
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **k: pytest.fail('mutation must stop native launch'))
|
||||
ui.repair_worker_main(job)
|
||||
assert calls == ['access', 'guard']
|
||||
assert 'address changed' in json.loads((root/'repair-result.json').read_text())['detail']
|
||||
|
||||
|
||||
def test_guard_failure_keeps_takeover_checkpoint_byte_exact_when_address_changes(case, monkeypatch):
|
||||
from cbb_refresh_verify import library_lock as locks, maintenance_entry as entry
|
||||
request, _, address = case
|
||||
with locks.LibraryLock(request.lmc_file, safe_to_release=lambda: False): pass
|
||||
marker = locks.blocker_path(request.lmc_file); old = marker.read_bytes()
|
||||
snapshot = entry.address_snapshot
|
||||
def mutate(paths):
|
||||
result = snapshot(paths); address.write_bytes(b'changed'); return result
|
||||
monkeypatch.setattr(entry, 'address_snapshot', mutate)
|
||||
with pytest.raises(ValueError):
|
||||
with locks.LibraryLock.for_maintenance(request.lmc_file, request.evidence_dir/'takeover',
|
||||
lambda: entry.maintenance_target_guard(request)): pass
|
||||
assert marker.read_bytes() == old
|
||||
|
||||
|
||||
def test_independent_cbb_address_does_not_select_or_block_current_project(case):
|
||||
from cbb_refresh_verify.maintenance_entry import maintenance_target_guard
|
||||
request, project, address = case
|
||||
other = request.lmc_file.parent/'ReusableBlocksLP/1GPHY_8212/Work/database/cdbsvr/sAddress.adr'
|
||||
other.parent.mkdir(parents=True); other.write_bytes(b'independent CBB original address')
|
||||
with maintenance_target_guard(request) as checked:
|
||||
assert checked['target'].project == project
|
||||
assert set(checked['context']['addresses']) == {str(address.resolve())}
|
||||
assert other.read_bytes() == b'independent CBB original address'
|
||||
assert other.read_bytes() == b'independent CBB original address'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('status,complete,expected_safe', [
|
||||
('RESTORED_REVIEW_REQUIRED', True, True), ('DATABASE_BUSY', True, True),
|
||||
('UNKNOWN_DIALOG', True, True), ('UNCONFIRMED', True, True),
|
||||
('MISSING_HEALTHY_BACKUP', False, True), ('NATIVE_REPAIR_FAILED', False, True),
|
||||
('MISSING_HEALTHY_BACKUP', True, True), ('NATIVE_REPAIR_FAILED', True, True)])
|
||||
def test_failed_content_does_not_block_other_items_after_actual_release(case, monkeypatch, status, complete, expected_safe):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner, library_lock
|
||||
from test_native_repair import complete_native_success
|
||||
request, _, address = case
|
||||
def repair(target, *args, **kwargs):
|
||||
address.unlink() # Simulate official process releasing its test-only advertisement.
|
||||
result = complete_native_success(target)
|
||||
result.update(status=status, detail='native completed')
|
||||
result['evidence']['failure_complete'] = complete
|
||||
return result
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', repair)
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release'])
|
||||
patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: pytest.fail('nonrepair outcome reopened'))
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status != 'PASS'
|
||||
assert result.safe_to_continue is expected_safe
|
||||
assert result.recovery_attempts[0]['status'] == status
|
||||
assert library_lock.has_blocker(request.lmc_file) is not expected_safe
|
||||
|
||||
|
||||
def test_successful_icdb_repair_then_version_choice_reports_partial_completion(case, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner
|
||||
from test_native_repair import complete_native_success
|
||||
request, _, address = case
|
||||
def repair(target, *a, **k):
|
||||
address.unlink()
|
||||
return complete_native_success(target)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', repair)
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release'])
|
||||
patch_verification_after_fault(monkeypatch, runner, lambda *a, **k: runner.VerifyResult('FAIL',
|
||||
error_category='PCB_VERSION_CHOICE_REQUIRED', stderr='PCB版本待选:最后用户保存/自动保存 2026/9/24 23:06',
|
||||
safe_to_continue=False, evidence_path=request.evidence_dir/'verify/result.json'))
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status == 'REPAIRED' and result.safe_to_continue
|
||||
assert '待单独校验' in result.stdout
|
||||
assert result.recovery_attempts[0]['native_status'] == 'REPAIRED'
|
||||
assert result.recovery_attempts[0]['status'] == 'REPAIRED_UNVERIFIED'
|
||||
assert result.recovery_attempts[0]['validation_passed'] is False
|
||||
|
||||
|
||||
def test_user_chosen_pcb_history_is_not_reported_as_lossless_repair(case, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair, verify_runner as runner
|
||||
from test_native_repair import complete_native_success
|
||||
request, project, address = case
|
||||
address.unlink()
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda target, *a, **k: complete_native_success(target))
|
||||
monkeypatch.setattr(native_repair, 'repair_release_state', lambda req, target, result: result['release'])
|
||||
def verify(*a, **k):
|
||||
root = request.evidence_dir/'verify'; root.mkdir(exist_ok=True)
|
||||
(root/'pcb-version-restoration.json').write_text(json.dumps({'project': str(project),
|
||||
'choice': 'last_user_saved', 'date': '2026/9/24 23:06', 'confirm_sent': True, 'historical_restore': True}))
|
||||
return runner.VerifyResult('PASS', library_flag='GREEN', evidence_path=root/'result.json')
|
||||
patch_verification_after_fault(monkeypatch, runner, verify)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status == 'REPAIRED' and result.library_flag == 'UNKNOWN'
|
||||
assert result.recovery_attempts[0]['status'] == 'REPAIRED_UNVERIFIED'
|
||||
assert not (request.evidence_dir/'verify').exists()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,171 @@
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
from PySide6.QtCore import Qt
|
||||
|
||||
|
||||
def test_ready_rows_never_enter_maintenance_even_when_unchecked(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window, MIXED
|
||||
window = load_window(qtbot, tmp_path, monkeypatch, records=(MIXED[0],))
|
||||
assert window._maintenance_names() == []
|
||||
assert not window.icdb_repair_button.isEnabled()
|
||||
assert not window.backup_restore_button.isEnabled()
|
||||
window.select_visible_button.click()
|
||||
assert window._maintenance_names() == []
|
||||
|
||||
|
||||
def test_selected_ready_rows_are_ignored_in_mixed_maintenance(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
window = load_window(qtbot, tmp_path, monkeypatch)
|
||||
window.select_visible_button.click()
|
||||
assert window._maintenance_names() == ['Beta', 'Gamma', 'Delta']
|
||||
window._apply_refreshed_records((('Beta', 'Gamma'), {'Beta': 'READY', 'Gamma': 'READY'}, {}, ''))
|
||||
assert window._maintenance_names() == ['Delta']
|
||||
assert window.icdb_repair_button.isEnabled()
|
||||
|
||||
|
||||
def test_diagnostic_worker_closes_without_integration_save_or_config_edit(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = diagnostic_worker(tmp_path, monkeypatch)
|
||||
job['diagnostic_only'] = True
|
||||
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 = []
|
||||
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)
|
||||
|
||||
|
||||
def test_diagnostic_close_failure_never_returns_healthy(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
job, events, execute = diagnostic_worker(tmp_path, monkeypatch)
|
||||
job['diagnostic_only'] = True
|
||||
closes = []
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('prefix', ['KEY CentralLibrary ', ' KEY CentralLibrary ', 'KEY\tCentralLibrary\t'])
|
||||
def test_diagnosis_rejects_foreign_library_reference_before_native_launch(tmp_path, monkeypatch, prefix):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = diagnostic_worker(tmp_path, monkeypatch)
|
||||
job['diagnostic_only'] = True
|
||||
project = Path(job['project'])
|
||||
project.write_text(prefix + '"foreign.lmc"\n')
|
||||
monkeypatch.setattr(adapter, 'launch_pcb_process', lambda *a: pytest.fail('foreign library opened'))
|
||||
with pytest.raises(ValueError, match='DIAGNOSTIC_LIBRARY_MISMATCH'):
|
||||
execute()
|
||||
|
||||
def diagnostic_worker(tmp_path, monkeypatch):
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
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
|
||||
|
||||
|
||||
def test_diagnostic_runner_skips_importer_and_ready_verification(tmp_path, monkeypatch):
|
||||
import json
|
||||
from test_verify_runner import fixture, request, PORTS_OK
|
||||
from cbb_refresh_verify.verify_runner import run_verify
|
||||
fixture(tmp_path)
|
||||
captured = {}
|
||||
class FinishedWorker:
|
||||
desktop = None
|
||||
returncode = 0
|
||||
def __init__(self, command, name, **kwargs):
|
||||
job = Path(command[-1])
|
||||
captured.update(json.loads(job.read_text('utf-8')))
|
||||
job.with_name('outcome.json').write_text('{"status":"PASS"}', encoding='utf-8')
|
||||
def poll(self): return 0
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', FinishedWorker)
|
||||
def forbidden(*a, **kw): pytest.fail('diagnosis must not run importer or query READY')
|
||||
result = run_verify(request(tmp_path, diagnostic_only=True, auto_repair=False,
|
||||
libspec='configured', dms_login_config='configured'), port_check=PORTS_OK,
|
||||
flag_reader=forbidden, process_runner=forbidden)
|
||||
assert result.status == 'PASS' and result.library_flag == 'UNKNOWN'
|
||||
assert captured['diagnostic_only'] is True
|
||||
|
||||
|
||||
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
|
||||
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'])
|
||||
def test_diagnostic_checks_late_startup_errors_before_declaring_healthy(tmp_path, monkeypatch, when):
|
||||
from cbb_refresh_verify import mentor_adapter as adapter
|
||||
job, events, execute = diagnostic_worker(tmp_path, monkeypatch)
|
||||
job['diagnostic_only'] = True
|
||||
source = Path(job['pcb']).parent/'LogFiles/PCBLogFile.txt'
|
||||
def late_fault():
|
||||
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')
|
||||
closes = []
|
||||
def check(*a, **kw):
|
||||
if when == 'document_ready':
|
||||
late_fault()
|
||||
raise RuntimeError(kw['error_reader']())
|
||||
closes.append(True)
|
||||
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 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'
|
||||
@@ -0,0 +1,185 @@
|
||||
"""Project Backup is reached only after a current, released PCB startup fault."""
|
||||
from dataclasses import replace
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from test_maintenance_entry import case, no_native_launch, run_legacy_maintenance
|
||||
|
||||
|
||||
def fault_text(database):
|
||||
return ("The last server session on different server machine lost the connection to the database. "
|
||||
"Please use iCDB Project Backup 'Repair project' functionality to fix this issue. "
|
||||
f"Machine:[fixture-server] Path:[{database}] session:[fixture-current-session]")
|
||||
|
||||
|
||||
def install_probe(monkeypatch, result, calls):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
def probe(request, **kwargs):
|
||||
assert request.diagnostic_only and not request.maintenance and not request.auto_repair
|
||||
assert not request.native_lm_verify and request.post_repair_proof is None
|
||||
calls.append('open')
|
||||
return result
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', probe)
|
||||
|
||||
|
||||
def forbid_backup(monkeypatch):
|
||||
from cbb_refresh_verify import native_repair
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair',
|
||||
lambda *a, **k: pytest.fail('Project Backup launched without a current matching startup fault'))
|
||||
|
||||
|
||||
def test_healthy_startup_skips_project_backup_even_with_old_address(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
request, _, address = case
|
||||
calls = []
|
||||
install_probe(monkeypatch, runner.VerifyResult('PASS', safe_to_continue=True), calls)
|
||||
forbid_backup(monkeypatch)
|
||||
result = run_legacy_maintenance(replace(request, native_lm_verify=True))
|
||||
assert calls == ['open']
|
||||
assert result.status == 'CHECKED' and result.library_flag == 'UNKNOWN'
|
||||
assert result.recovery_attempts == () and result.safe_to_continue
|
||||
assert address.read_bytes() == b'old server address - preserved'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('message', ['License unavailable', 'Could not run server',
|
||||
'COM 已连接,尚无目标文档', 'ICDB_REPAIR_REQUIRED',
|
||||
'PCB 文件损坏', 'Lost connection to server'])
|
||||
def test_generic_startup_errors_remain_failures_without_backup(case, monkeypatch, message):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
request, _, _ = case
|
||||
calls = []
|
||||
failed = runner.VerifyResult('FAIL', stderr=message, safe_to_continue=True)
|
||||
install_probe(monkeypatch, failed, calls)
|
||||
forbid_backup(monkeypatch)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert calls == ['open'] and result == failed
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reason', ['shared', 'other_project', 'no_session', 'unreleased', 'timeout', 'cancelled'])
|
||||
def test_unmatched_or_unreleased_icdb_fault_never_enters_backup(case, monkeypatch, reason):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
request, project, _ = case
|
||||
database = (project.parent/'database' if reason not in ('shared', 'other_project') else
|
||||
request.lmc_file.parent/('SymbolLibs/__ReusableBlocksSymb' if reason == 'shared'
|
||||
else 'ReusableBlocksLP/Other/Work/database'))
|
||||
if not database.exists(): database.mkdir(parents=True); (database/'icdb.dat').touch()
|
||||
text = fault_text(database)
|
||||
if reason == 'no_session': text = text.replace('session:[fixture-current-session]', '')
|
||||
failed = runner.VerifyResult('TIMEOUT' if reason == 'timeout' else
|
||||
'CANCELLED' if reason == 'cancelled' else 'FAIL', stderr=text,
|
||||
error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=reason != 'unreleased')
|
||||
calls = []
|
||||
install_probe(monkeypatch, failed, calls)
|
||||
forbid_backup(monkeypatch)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert calls == ['open'] and result.status == failed.status
|
||||
assert not result.recovery_attempts
|
||||
assert result.safe_to_continue is failed.safe_to_continue
|
||||
|
||||
|
||||
def test_only_current_matching_fault_enters_native_maintenance(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, maintenance_entry as entry
|
||||
request, project, _ = case
|
||||
calls = []
|
||||
failed = runner.VerifyResult('FAIL', stderr=fault_text(project.parent/'database'),
|
||||
error_category='ICDB_REPAIR_REQUIRED', safe_to_continue=True)
|
||||
install_probe(monkeypatch, failed, calls)
|
||||
def maintain(req, *, diagnosed_target, startup_result, **kwargs):
|
||||
assert diagnosed_target.project == project and diagnosed_target.database == project.parent/'database'
|
||||
assert startup_result == failed
|
||||
calls.append('maintenance')
|
||||
return runner.VerifyResult('REPAIRED')
|
||||
monkeypatch.setattr(entry, 'run_existing_address_maintenance', maintain)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert calls == ['open', 'maintenance'] and result.status == 'REPAIRED'
|
||||
|
||||
|
||||
def test_old_saved_diagnosis_does_not_turn_generic_startup_failure_into_repair(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
request, project, _ = case
|
||||
root = request.evidence_dir/'old'; (root/'diagnostics').mkdir(parents=True)
|
||||
(root/'diagnostics/icdb-recovery.json').write_text(json.dumps({
|
||||
'error_code': 'ICDB_REPAIR_REQUIRED', 'native_message': fault_text(project.parent/'database')}))
|
||||
calls = []
|
||||
failure = runner.VerifyResult('FAIL', stderr='License unavailable', evidence_path=root/'result.json')
|
||||
install_probe(monkeypatch, failure, calls)
|
||||
forbid_backup(monkeypatch)
|
||||
assert run_legacy_maintenance(request) == failure
|
||||
assert calls == ['open']
|
||||
|
||||
|
||||
def test_native_admission_cannot_switch_the_diagnosed_database(case, monkeypatch):
|
||||
from cbb_refresh_verify import maintenance_entry as entry, native_repair
|
||||
from test_maintenance_entry import startup_failure
|
||||
request, project, _ = case
|
||||
forbid_backup(monkeypatch)
|
||||
result = entry.run_existing_address_maintenance(request,
|
||||
diagnosed_target=native_repair.RepairTarget(project, project.parent/'other'),
|
||||
startup_result=startup_failure(request))
|
||||
assert result.status == 'FAIL' and result.safe_to_continue
|
||||
assert result.error_category == 'ICDB_TARGET_MISMATCH'
|
||||
|
||||
|
||||
def test_equivalent_diagnosed_path_keeps_the_native_launch_path(case, monkeypatch):
|
||||
from cbb_refresh_verify import maintenance_entry as entry, native_repair
|
||||
from test_maintenance_entry import startup_failure
|
||||
from test_maintenance_diagnosis import inspection
|
||||
request, project, _ = case
|
||||
seen = []
|
||||
def inspect(target, *args, **kwargs):
|
||||
assert kwargs['inspection_only'] is True
|
||||
seen.append(target.project)
|
||||
return inspection(target)
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', inspect)
|
||||
alias = project.parent/'unused/..'
|
||||
result = entry.run_existing_address_maintenance(request,
|
||||
diagnosed_target=native_repair.RepairTarget(alias/project.name, alias/'database'),
|
||||
startup_result=startup_failure(request))
|
||||
assert result.status == 'CHECKED' and result.safe_to_continue, result
|
||||
assert seen == [project]
|
||||
|
||||
|
||||
def test_unsafe_startup_success_is_not_reported_as_healthy(case, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
request, _, _ = case
|
||||
install_probe(monkeypatch, runner.VerifyResult('PASS', safe_to_continue=False), [])
|
||||
forbid_backup(monkeypatch)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status == 'FAIL' and not result.safe_to_continue
|
||||
|
||||
|
||||
def test_report_write_failure_does_not_block_a_released_healthy_project(case, monkeypatch):
|
||||
from pathlib import Path
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
request, _, _ = case
|
||||
evidence = request.evidence_dir/'result.json'; evidence.parent.mkdir(); evidence.write_text('{}')
|
||||
install_probe(monkeypatch, runner.VerifyResult('PASS', evidence_path=evidence), [])
|
||||
forbid_backup(monkeypatch)
|
||||
write = Path.write_text
|
||||
def unavailable(path, *args, **kwargs):
|
||||
if path.name == 'maintenance-result.json': raise PermissionError('report unavailable')
|
||||
return write(path, *args, **kwargs)
|
||||
monkeypatch.setattr(Path, 'write_text', unavailable)
|
||||
result = run_legacy_maintenance(request)
|
||||
assert result.status == 'CHECKED' and result.safe_to_continue
|
||||
assert result.evidence_path == evidence and evidence.read_text() == '{}'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('before_open', [True, False])
|
||||
def test_cancelled_startup_diagnosis_never_requests_repair(case, monkeypatch, before_open):
|
||||
from threading import Event
|
||||
from cbb_refresh_verify import verify_runner as runner
|
||||
from test_maintenance_entry import startup_failure
|
||||
request, _, _ = case
|
||||
stop = Event()
|
||||
if before_open: stop.set()
|
||||
calls = []
|
||||
def probe(req, **kw):
|
||||
calls.append('open'); stop.set()
|
||||
return startup_failure(req)
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', probe)
|
||||
forbid_backup(monkeypatch)
|
||||
result = run_legacy_maintenance(request, stop=stop)
|
||||
assert result.status == 'CANCELLED' and result.safe_to_continue
|
||||
assert calls == ([] if before_open else ['open'])
|
||||
@@ -0,0 +1,75 @@
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
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'
|
||||
@@ -0,0 +1,362 @@
|
||||
"""Operation safety tests use a file-only native boundary; never launch Mentor."""
|
||||
from dataclasses import replace
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def project(root, name='Source', marker=b'original'):
|
||||
root.mkdir(parents=True)
|
||||
(root/'database').mkdir()
|
||||
(root/'database/icdb.dat').write_bytes(marker)
|
||||
(root/'PCB/Layout').mkdir(parents=True)
|
||||
(root/'PCB/Layout/LayoutDB.lyt').write_bytes(marker)
|
||||
(root/'PCB/pcb.prj').write_text('KEY iCDBDir "../database"\n', encoding='utf-8')
|
||||
(root/'PCB/main.pcb').write_text(f'KEY ProjectFile "../{name}.prj"\n', encoding='utf-8')
|
||||
path = root/f'{name}.prj'
|
||||
path.write_text('KEY RootBlock "Top"\nKEY PCBDesignPath "PCB/main.pcb"\n'
|
||||
'KEY iCDBDir "database"\n', encoding='utf-8')
|
||||
return path
|
||||
|
||||
|
||||
class FileAdapter:
|
||||
def __init__(self):
|
||||
self.records = []
|
||||
self.calls = []
|
||||
self.fail_add = False
|
||||
self.lie = False
|
||||
self.released = True
|
||||
|
||||
def capabilities(self):
|
||||
from cbb_refresh_verify.management import Capability
|
||||
return {op: Capability(True, 'test boundary') for op in ('add', 'update', 'remove')}
|
||||
|
||||
def catalog(self, library, evidence):
|
||||
from cbb_refresh_verify.management import Catalog
|
||||
return Catalog(library, 1, tuple(self.records), safe_to_continue=self.released)
|
||||
|
||||
def remove(self, request, evidence):
|
||||
from cbb_refresh_verify.management import NativeOutcome
|
||||
self.calls.append('remove')
|
||||
if not self.lie:
|
||||
self.records = [r for r in self.records if r.name != request.name]
|
||||
return NativeOutcome(True, self.released, 'native removal accepted')
|
||||
|
||||
def add(self, request, evidence):
|
||||
from cbb_refresh_verify.management import NativeOutcome, CatalogEntry, engineering_identity
|
||||
self.calls.append(('add', request.source))
|
||||
if self.fail_add:
|
||||
return NativeOutcome(False, self.released, 'native add failed')
|
||||
if not self.lie:
|
||||
self.records.append(CatalogEntry(request.name, request.partition,
|
||||
engineering_identity(request.source)))
|
||||
return NativeOutcome(True, self.released, 'native add accepted')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup(tmp_path):
|
||||
from cbb_refresh_verify.management import ManagementRequest, CatalogEntry
|
||||
library = tmp_path/'library/mentor.lmc'
|
||||
library.parent.mkdir()
|
||||
library.write_bytes(b'library')
|
||||
old = project(library.parent/'ReusableBlocksLP/Block/Work', 'Block')
|
||||
source = project(tmp_path/'source')
|
||||
adapter = FileAdapter()
|
||||
adapter.records = [CatalogEntry('Block', 'Logical-Physical')]
|
||||
request = ManagementRequest('remove', library, 'Block', evidence_dir=tmp_path/'evidence')
|
||||
yield request, adapter, source
|
||||
# FileAdapter never launches native owners. Release only this test's
|
||||
# intentionally retained lease so pytest can remove its temporary tree.
|
||||
from cbb_refresh_verify.library_lock import _guard, _owners
|
||||
key = str(library.with_name('.mentor.cbb-upload.lock').resolve()).casefold()
|
||||
with _guard:
|
||||
owner = _owners.pop(key, None)
|
||||
if owner:
|
||||
owner['handle'].Close()
|
||||
|
||||
|
||||
def execute(request, adapter, **kwargs):
|
||||
from cbb_refresh_verify.management import run_management
|
||||
return run_management(request, adapter=adapter, idle_check=lambda *_: None, **kwargs)
|
||||
|
||||
|
||||
def test_remove_uses_baseline_without_automatic_library_backup(setup):
|
||||
request, adapter, _ = setup
|
||||
result = execute(request, adapter)
|
||||
assert result.status == 'REMOVED' and result.safe_to_continue
|
||||
assert not (result.evidence_path.parent/'library-before').exists()
|
||||
assert (result.evidence_path.parent/'library-baseline-manifest.json').is_file()
|
||||
assert '已备份' not in result.detail
|
||||
record = json.loads(result.evidence_path.read_text(encoding='utf-8'))
|
||||
assert record['operation'] == 'remove' and record['partition'] == 'Logical-Physical'
|
||||
assert record['stage'] == 'complete'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation', ['add', 'update', 'remove'])
|
||||
def test_management_never_copies_target_library(setup, monkeypatch, operation):
|
||||
import cbb_refresh_verify.management as management
|
||||
request, adapter, source = setup
|
||||
real_copy = management.copy_verified
|
||||
def copy(source_path, destination, **kwargs):
|
||||
assert Path(source_path).resolve() != request.library.parent.resolve(), 'automatic whole-library backup'
|
||||
return real_copy(source_path, destination, **kwargs)
|
||||
monkeypatch.setattr(management, 'copy_verified', copy)
|
||||
result = execute(replace(request, operation=operation, source=source if operation != 'remove' else None,
|
||||
name='New' if operation == 'add' else 'Block', auto_verify=False), adapter)
|
||||
assert result.status == ('REMOVED' if operation == 'remove' else 'UPLOADED')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('relative', ['mentor.lmc', 'SymbolLibs/shared/icdb.dat',
|
||||
'ReusableBlocksLP/Other/Work/database/icdb.dat'])
|
||||
def test_manual_backup_policy_still_rejects_external_shared_read_owner(setup, relative):
|
||||
import win32con, win32file
|
||||
request, adapter, _ = setup
|
||||
path = request.library.parent/relative
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if not path.exists():
|
||||
path.write_bytes(b'owned database')
|
||||
handle = win32file.CreateFile(str(path), win32con.GENERIC_READ, win32con.FILE_SHARE_READ, None,
|
||||
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
|
||||
try:
|
||||
result = execute(request, adapter)
|
||||
assert result.status == 'BLOCKED'
|
||||
assert not adapter.calls, 'An existing reader must block native writes even without address/process evidence'
|
||||
assert not list(request.evidence_dir.rglob('library-before'))
|
||||
finally:
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_add_conflict_cannot_overwrite(setup):
|
||||
request, adapter, source = setup
|
||||
result = execute(replace(request, operation='add', source=source), adapter)
|
||||
assert result.status == 'UPLOAD_FAILED' and adapter.calls == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize('verify_status,flag,expected', [('PASS', 'GREEN', 'UPLOADED_VERIFIED'),
|
||||
('FAIL', 'RED', 'UPLOADED_VERIFY_FAILED'), ('PASS', 'UNKNOWN', 'UPLOADED_VERIFY_FAILED')])
|
||||
def test_add_stages_source_and_requires_authoritative_verify(setup, verify_status, flag, expected):
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
request, adapter, source = setup
|
||||
original = source.read_bytes()
|
||||
def verifier(req, **_):
|
||||
assert adapter.released
|
||||
return VerifyResult(verify_status, library_flag=flag)
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter,
|
||||
verifier=verifier)
|
||||
assert result.status == expected and source.read_bytes() == original
|
||||
assert adapter.calls[0][1] != source
|
||||
assert adapter.calls[0][1].is_relative_to(result.evidence_path.parent)
|
||||
|
||||
|
||||
def test_add_no_auto_verify_and_reimport_after_remove(setup):
|
||||
request, adapter, source = setup
|
||||
assert execute(request, adapter).status == 'REMOVED'
|
||||
result = execute(replace(request, operation='add', source=source, auto_verify=False), adapter,
|
||||
verifier=lambda *_: pytest.fail('auto verify disabled'))
|
||||
assert result.status == 'UPLOADED'
|
||||
|
||||
|
||||
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 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]
|
||||
assert (result.evidence_path.parent/'library-baseline-manifest.json').is_file()
|
||||
assert not (result.evidence_path.parent/'library-before').exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation', ['remove', 'add'])
|
||||
def test_lying_native_output_does_not_report_success(setup, operation):
|
||||
request, adapter, source = setup
|
||||
adapter.lie = True
|
||||
request = replace(request, operation=operation, source=source,
|
||||
name='New' if operation == 'add' else 'Block')
|
||||
result = execute(request, adapter)
|
||||
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
|
||||
|
||||
|
||||
def test_unreleased_native_owner_prevents_postread_and_verification(setup):
|
||||
request, adapter, _ = setup
|
||||
original = adapter.remove
|
||||
def unreleased(*args):
|
||||
adapter.released = False
|
||||
return original(*args)
|
||||
adapter.remove = unreleased
|
||||
reads = []
|
||||
catalog = adapter.catalog
|
||||
adapter.catalog = lambda *a: (reads.append(1) or catalog(*a))
|
||||
result = execute(request, adapter, verifier=lambda *_: pytest.fail('unsafe verify'))
|
||||
assert not result.safe_to_continue and len(reads) == 1
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
|
||||
with pytest.raises(LibraryBusy):
|
||||
with LibraryLock(request.library):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name', ['../escape', 'A\tB', 'A\nB', 'A/B', 'NUL', 'a.', ''])
|
||||
def test_invalid_native_identity_rejected_before_writes(setup, name):
|
||||
request, adapter, _ = setup
|
||||
result = execute(replace(request, name=name), adapter)
|
||||
assert result.status == 'BLOCKED' and adapter.calls == []
|
||||
|
||||
|
||||
def test_evidence_inside_target_rejected(setup):
|
||||
request, adapter, _ = setup
|
||||
result = execute(replace(request, evidence_dir=request.library.parent/'evidence'), adapter)
|
||||
assert result.status == 'BLOCKED' and not (request.library.parent/'evidence').exists()
|
||||
|
||||
|
||||
def test_cancel_and_restart_never_replay(setup):
|
||||
from cbb_refresh_verify.management import run_management_batch, unfinished_operations
|
||||
request, adapter, _ = setup
|
||||
stop = Event(); stop.set()
|
||||
assert run_management_batch([request], stop=stop, adapter=adapter) == []
|
||||
checkpoint = request.evidence_dir/'interrupted/checkpoint.json'
|
||||
checkpoint.parent.mkdir(parents=True)
|
||||
checkpoint.write_text('{"stage":"native_remove", "status":"RUNNING"}', encoding='utf-8')
|
||||
found = unfinished_operations(request.evidence_dir)
|
||||
assert len(found) == 1 and found[0]['evidence_path'] == str(checkpoint)
|
||||
assert adapter.calls == []
|
||||
|
||||
|
||||
def test_capability_gate_enforced_below_ui(setup):
|
||||
from cbb_refresh_verify.management import Capability
|
||||
request, adapter, source = setup
|
||||
adapter.capabilities = lambda: {'add': Capability(False, 'isolated add not validated')}
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter)
|
||||
assert result.status == 'UNSUPPORTED' and 'isolated' in result.detail
|
||||
assert adapter.calls == []
|
||||
|
||||
|
||||
def test_timeout_stops_batch_and_persists_ambiguous_native_stage(setup):
|
||||
from cbb_refresh_verify.management import run_management_batch
|
||||
request, adapter, _ = setup
|
||||
def timeout(*_):
|
||||
raise TimeoutError('network timeout')
|
||||
adapter.remove = timeout
|
||||
results = run_management_batch([request, replace(request, name='Other')], adapter=adapter,
|
||||
idle_check=lambda *_: None)
|
||||
assert len(results) == 1 and not results[0].safe_to_continue
|
||||
assert results[0].status == 'RECOVERY_REQUIRED'
|
||||
|
||||
|
||||
def test_catalog_timeout_is_not_safe_to_continue(setup):
|
||||
request, adapter, _ = setup
|
||||
def timeout(*_):
|
||||
raise TimeoutError('catalog owner still running')
|
||||
adapter.catalog = timeout
|
||||
result = execute(request, adapter)
|
||||
assert not result.safe_to_continue and adapter.calls == []
|
||||
|
||||
|
||||
def test_replaced_source_bytes_fail_identity_even_with_same_name(setup):
|
||||
request, adapter, source = setup
|
||||
real_add = adapter.add
|
||||
def wrong_source(*args):
|
||||
from cbb_refresh_verify.management import CatalogEntry
|
||||
result = real_add(*args)
|
||||
entry = adapter.records[-1]
|
||||
adapter.records[-1] = CatalogEntry(entry.name, entry.partition, {'wrong': 'project'})
|
||||
return result
|
||||
adapter.add = wrong_source
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter)
|
||||
assert result.status == 'RECOVERY_REQUIRED' and result.safe_to_continue
|
||||
|
||||
|
||||
def test_update_success_requires_same_identity_and_no_implicit_partition_move(setup):
|
||||
request, adapter, source = setup
|
||||
result = execute(replace(request, operation='update', source=source, partition='Other'), adapter)
|
||||
assert result.status == 'BLOCKED' and not adapter.calls
|
||||
result = execute(replace(request, operation='update', source=source, auto_verify=False), adapter)
|
||||
assert result.status == 'UPLOADED'
|
||||
assert adapter.calls[0] == 'remove' and adapter.calls[1][0] == 'add'
|
||||
|
||||
|
||||
def test_verify_exception_after_upload_retains_coordination_lock(setup):
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy
|
||||
request, adapter, source = setup
|
||||
def broken_verifier(*a, **k):
|
||||
raise OSError('verification worker disconnected')
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter,
|
||||
verifier=broken_verifier)
|
||||
assert result.status == 'UPLOADED_VERIFY_FAILED' and not result.safe_to_continue
|
||||
with pytest.raises(LibraryBusy):
|
||||
with LibraryLock(request.library):
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.parametrize('nested', [True, False])
|
||||
def test_library_inside_source_tree_rejected_before_catalog_or_guard(setup, nested):
|
||||
request, adapter, source = setup
|
||||
library = source.parent/('nested/lib.lmc' if nested else 'lib.lmc')
|
||||
library.parent.mkdir(exist_ok=True)
|
||||
library.write_bytes(b'library')
|
||||
adapter.catalog = lambda *a: pytest.fail('overlapping library reached native catalog')
|
||||
result = execute(replace(request, operation='add', name='New', source=source, library=library), adapter)
|
||||
assert result.status == 'BLOCKED' and not adapter.calls
|
||||
assert not library.with_name('.lib.cbb-upload.lock').exists()
|
||||
assert not request.evidence_dir.exists()
|
||||
|
||||
|
||||
def test_mapped_library_spelling_and_context_survive_engine_verify_boundary(setup, monkeypatch):
|
||||
import shutil
|
||||
from cbb_refresh_verify.verify_runner import VerifyResult
|
||||
request, adapter, source = setup
|
||||
alias_root = request.library.parent
|
||||
canonical_root = alias_root.with_name('canonical-library')
|
||||
shutil.copytree(alias_root, canonical_root)
|
||||
original_resolve = Path.resolve
|
||||
def resolve(path, *a, **k):
|
||||
if path.is_relative_to(alias_root):
|
||||
return original_resolve(canonical_root/path.relative_to(alias_root), *a, **k)
|
||||
return original_resolve(path, *a, **k)
|
||||
monkeypatch.setattr(Path, 'resolve', resolve)
|
||||
catalogs, verified = [], []
|
||||
catalog = adapter.catalog
|
||||
adapter.catalog = lambda library, evidence: (catalogs.append(library) or catalog(library, evidence))
|
||||
def verifier(req, **k):
|
||||
verified.append(req)
|
||||
return VerifyResult('PASS', library_flag='GREEN')
|
||||
request = replace(request, operation='add', name='New', source=source,
|
||||
mapped_drive='S:', mapped_remote_root=r'\\server\share')
|
||||
result = execute(request, adapter, verifier=verifier)
|
||||
assert result.status == 'UPLOADED_VERIFIED'
|
||||
assert catalogs == [request.library, request.library]
|
||||
assert verified[0].lmc_file == request.library
|
||||
assert verified[0].mapped_drive == 'S:' and verified[0].mapped_remote_root == r'\\server\share'
|
||||
|
||||
|
||||
def test_source_inside_target_library_rejected_before_catalog(setup):
|
||||
request, adapter, _ = setup
|
||||
source = request.library.parent/'ReusableBlocksLP/Block/Work/Block.prj'
|
||||
adapter.catalog = lambda *a: pytest.fail('overlapping source reached native catalog')
|
||||
result = execute(replace(request, operation='add', name='New', source=source), adapter)
|
||||
assert result.status == 'BLOCKED' and not request.evidence_dir.exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('matched', [True, False])
|
||||
def test_prepared_native_identity_is_required_after_exact_catalog(setup, matched):
|
||||
from cbb_refresh_verify.native_engineering_identity import IdentityOutcome
|
||||
from cbb_refresh_verify.management import NativeOutcome
|
||||
request, adapter, source = setup
|
||||
expected = {'policy': 'native-test', 'logical': 'same', 'physical': 'same'}
|
||||
def prepare(staged, root, *, original_source):
|
||||
assert original_source == source and staged.source != source
|
||||
assert (root/'library-baseline-manifest.json').exists()
|
||||
adapter.calls.append('prepare_identity')
|
||||
return staged, IdentityOutcome(expected, True, 'baseline captured', root)
|
||||
adapter.prepare_add_transaction = prepare
|
||||
original_add = adapter.add
|
||||
def add(staged, evidence):
|
||||
original_add(staged, evidence)
|
||||
return NativeOutcome(True, True, 'native registered', identity_matches=matched,
|
||||
identity_proof=expected if matched else {'different': True})
|
||||
adapter.add = add
|
||||
result = execute(replace(request, operation='update', source=source, auto_verify=False), adapter)
|
||||
assert adapter.calls[0:2] == ['prepare_identity', 'remove']
|
||||
assert result.status == ('UPLOADED' if matched else 'RECOVERY_REQUIRED')
|
||||
@@ -0,0 +1,141 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import ModuleType, SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_raw_dispatch_diagnostics_preserve_hresult_and_known_member_ids(tmp_path):
|
||||
from cbb_refresh_verify.native_management import _dispatch_diagnostics
|
||||
class Error(RuntimeError):
|
||||
hresult = -2147221008
|
||||
class Raw:
|
||||
def GetIDsOfNames(self, name):
|
||||
if name == 'Quit': raise Error('COM apartment unavailable')
|
||||
return 17 if name == 'Close' else 21
|
||||
obj = SimpleNamespace(_oleobj_=Raw())
|
||||
_dispatch_diagnostics(tmp_path, 'before-import', obj, obj)
|
||||
record = json.loads((tmp_path/'dispatch-diagnostics.jsonl').read_text(encoding='utf-8'))
|
||||
assert record['members']['application.OnReusableBlocksEditor']['dispid'] == 21
|
||||
assert record['members']['library.Close']['dispid'] == 17
|
||||
error = record['members']['application.Quit']['error']
|
||||
assert error['hresult'] == -2147221008 and error['args'] == ['COM apartment unavailable']
|
||||
assert error['type'].endswith('Error')
|
||||
|
||||
|
||||
def test_probe_records_dispatch_before_after_import_and_after_source_alignment(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import sys, os
|
||||
source = tmp_path/'source/Block.prj'
|
||||
source.parent.mkdir()
|
||||
source.write_text('KEY RootBlock "Block"\nKEY PCBDesignPath "PCB/Block.pcb"\n'
|
||||
'KEY iCDBDir "database"\nKEY CentralLibrary "old.lmc"\n')
|
||||
(source.parent/'database').mkdir(); (source.parent/'database/icdb.dat').write_bytes(b'fixture')
|
||||
pcb = source.parent/'PCB/Block.pcb'; (pcb.parent/'Layout').mkdir(parents=True)
|
||||
pcb.write_text('KEY ProjectFile "../Block.prj"\n')
|
||||
(pcb.parent/'pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n')
|
||||
(pcb.parent/'Layout/LayoutDB.lyt').write_bytes(b'fixture')
|
||||
root = tmp_path/'native-add'; root.mkdir()
|
||||
library = root/'fixture/mentor_lib.lmc'; library.parent.mkdir(); library.touch()
|
||||
phase = ['before']
|
||||
class Raw:
|
||||
def GetIDsOfNames(self, name): return 1 if phase[0] == 'before' else 2
|
||||
class App:
|
||||
_oleobj_ = Raw()
|
||||
def OnReusableBlocksEditor(self): raise RuntimeError('stop before native UI')
|
||||
class Uia(ModuleType):
|
||||
def __getattr__(self, name):
|
||||
if name == 'Desktop':
|
||||
phase[0] = 'after'
|
||||
return None
|
||||
raise AttributeError(name)
|
||||
monkeypatch.setitem(sys.modules, 'pywinauto', Uia('pywinauto'))
|
||||
job = {'operation': 'probe_create_add', 'source': str(source), 'library': str(library),
|
||||
'name': 'Block', 'timeout_seconds': 0, 'supervisor_pid': os.getpid()}
|
||||
with pytest.raises(RuntimeError, match='stop before native UI'):
|
||||
native._probe_add_dialog_impl(App(), job, None, set(), root, library=SimpleNamespace(_oleobj_=Raw()))
|
||||
records = [json.loads(line) for line in (root/'dispatch-diagnostics.jsonl').read_text(encoding='utf-8').splitlines()]
|
||||
assert [r['stage'] for r in records] == ['before-pywinauto-import', 'after-pywinauto-import', 'after-source-alignment']
|
||||
assert records[0]['members']['application.Quit']['dispid'] == 1
|
||||
assert records[1]['members']['application.Quit']['dispid'] == 2
|
||||
assert records[2]['paths'] == {'source': str(source), 'pcb': str(pcb), 'library_argument': str(library)}
|
||||
assert 'old.lmc' not in source.read_text()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation', ['probe_create_add', 'probe_readd', 'catalog'])
|
||||
def test_probe_preloads_uia_before_com_proxy_creation_but_catalog_stays_read_only(tmp_path, monkeypatch, operation):
|
||||
import sys, win32com.client
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import cbb_refresh_verify.mentor_adapter as mentor
|
||||
root = tmp_path/'job'; root.mkdir()
|
||||
library_path = tmp_path/'fixture.lmc'; library_path.touch()
|
||||
events, running = [], [False]
|
||||
class Uia(ModuleType):
|
||||
def __getattr__(self, name):
|
||||
if name == 'Desktop':
|
||||
events.append('uia-loaded')
|
||||
return object
|
||||
raise AttributeError(name)
|
||||
monkeypatch.setitem(sys.modules, 'pywinauto', Uia('pywinauto'))
|
||||
library = SimpleNamespace(FullName=str(library_path), FlowType=1,
|
||||
Partitions=lambda kind: SimpleNamespace(Count=0), Close=lambda: events.append('library-close'))
|
||||
class App:
|
||||
def OpenLibrary(self, path): return library
|
||||
def CreateLibrary(self, path, flow): return library
|
||||
def Quit(self): events.append('quit'); running[0] = False
|
||||
def dispatch(progid):
|
||||
events.append('dispatch'); running[0] = True
|
||||
return App()
|
||||
monkeypatch.setattr(win32com.client, 'DispatchEx', dispatch)
|
||||
monkeypatch.setattr(native, '_current_desktop', lambda:
|
||||
(SimpleNamespace(EnumDesktopWindows=lambda: []), 'CbbManagement_test'))
|
||||
monkeypatch.setattr(mentor, '_process_details', lambda name:
|
||||
[(123, str(tmp_path/'lm/win32/bin/LibraryManager.exe'), '', 0)] if running[0] else [])
|
||||
monkeypatch.setattr(native, 'prepare_probe_library', lambda *a: library_path)
|
||||
monkeypatch.setattr(native, 'validate_probe_readd', lambda *a: library_path)
|
||||
class Disconnected(RuntimeError):
|
||||
hresult = -2147220995
|
||||
def editor(*a, **k): raise Disconnected('controlled native failure')
|
||||
monkeypatch.setattr(native, '_probe_add_dialog', editor)
|
||||
monkeypatch.setattr(native, '_wait_native_exit', lambda pids: True)
|
||||
result = native._catalog_native({'operation': operation, 'desktop': 'CbbManagement_test',
|
||||
'library': str(library_path), 'mentor_home': str(tmp_path), 'probe_token': 'fixture'}, root)
|
||||
assert events[:2] == (['dispatch', 'library-close'] if operation == 'catalog' else ['uia-loaded', 'dispatch'])
|
||||
if operation == 'catalog':
|
||||
assert result['success'] and 'uia-loaded' not in events
|
||||
else:
|
||||
assert result['exception']['hresult'] == -2147220995
|
||||
assert result['exception']['args'] == ['controlled native failure']
|
||||
|
||||
|
||||
def test_probe_converts_desktop_pyhandle_before_uia_boundary(tmp_path, monkeypatch):
|
||||
import sys, win32gui, win32process
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import cbb_refresh_verify.management as management
|
||||
import cbb_refresh_verify.backup_recovery as recovery
|
||||
source = tmp_path/'source/Block.prj'
|
||||
source.parent.mkdir()
|
||||
source.write_bytes(b'file-only UIA boundary fixture')
|
||||
pcb = source.parent/'PCB/Block.pcb'
|
||||
root = tmp_path/'native-add'; root.mkdir()
|
||||
class Handle:
|
||||
def __int__(self): return 200
|
||||
handle = Handle()
|
||||
desktop = SimpleNamespace(EnumDesktopWindows=lambda: [handle])
|
||||
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda hwnd: (1, 9))
|
||||
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda hwnd: True)
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda hwnd: 'Reusable Blocks')
|
||||
monkeypatch.setattr(management, 'source_project', lambda *a: (source, pcb, 'Block'))
|
||||
monkeypatch.setattr(recovery, '_align_library', lambda *a: None)
|
||||
class Desktop:
|
||||
def __init__(self, **kwargs): pass
|
||||
def window(self, *, handle):
|
||||
assert type(handle) is int and handle == 200
|
||||
raise RuntimeError('integer UIA boundary reached')
|
||||
monkeypatch.setitem(sys.modules, 'pywinauto', SimpleNamespace(Desktop=Desktop))
|
||||
app = SimpleNamespace(OnReusableBlocksEditor=lambda: None)
|
||||
import os
|
||||
job = {'operation': 'probe_create_add', 'source': str(source), 'name': 'Block', 'supervisor_pid': os.getpid(),
|
||||
'library': str(root/'fixture/mentor_lib.lmc'), 'timeout_seconds': 5}
|
||||
with pytest.raises(RuntimeError, match='integer UIA boundary reached'):
|
||||
native._probe_add_dialog_impl(app, job, desktop, {9}, root)
|
||||
@@ -0,0 +1,155 @@
|
||||
"""Run the real management engine with only the native/export boundaries faked."""
|
||||
from dataclasses import replace
|
||||
import json
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def transaction(tmp_path, monkeypatch):
|
||||
from test_management import project
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import cbb_refresh_verify.native_engineering_identity as identity
|
||||
import cbb_refresh_verify.backup_recovery as recovery
|
||||
from cbb_refresh_verify.management import ManagementRequest, engineering_identity, _write_json
|
||||
from cbb_refresh_verify.management_transaction import validate_transaction
|
||||
root = tmp_path/'probe'
|
||||
library = root/'native-add/fixture/mentor_lib.lmc'
|
||||
library.parent.mkdir(parents=True); library.write_bytes(b'fixture library')
|
||||
(root/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8')
|
||||
source = project(tmp_path/'source')
|
||||
for path in (source, source.parent/'PCB/pcb.prj'):
|
||||
with path.open('a') as stream: stream.write(f'KEY CentralLibrary "{library}"\n')
|
||||
state = {'entries': [], 'calls': [], 'corrupt': False, 'fail_add': False}
|
||||
monkeypatch.setattr(native.NativeManagementAdapter, '_binary_capability', lambda _: '')
|
||||
monkeypatch.setattr(identity, 'check_exporters', lambda _: None)
|
||||
monkeypatch.setattr(recovery, '_idle_check', lambda *_: None)
|
||||
def capture(source, evidence, *, expected_library, **kwargs):
|
||||
evidence = Path(evidence); evidence.mkdir(parents=True, exist_ok=False)
|
||||
value = identity.asset_identity(source, expected_library)
|
||||
value.update(ascii_sha256='logical', csv={'work/identity': 'tables'}, edif_sha256='geometry')
|
||||
_write_json(evidence/'identity.json', {'identity': value, 'safe_to_continue': True})
|
||||
return identity.IdentityOutcome(value, True, 'file-only export proof', evidence)
|
||||
monkeypatch.setattr(identity, 'capture_identity', capture)
|
||||
def worker(adapter, job, evidence):
|
||||
evidence = Path(evidence); evidence.mkdir(parents=True, exist_ok=False)
|
||||
state['calls'].append(job['operation'])
|
||||
if job['operation'] == 'catalog':
|
||||
return {'success': True, 'safe_to_continue': True, 'library': str(library), 'flow': 1,
|
||||
'entries': list(state['entries'])}
|
||||
if job['operation'] == 'add':
|
||||
if state.get('tamper') == 'nonce': job['transaction_nonce'] = 'foreign'
|
||||
if state.get('tamper') == 'supervisor': job['supervisor_pid'] = -1
|
||||
if state.get('tamper') == 'baseline':
|
||||
(evidence.parent/'library-baseline-manifest.json').write_text('{}')
|
||||
if state.get('tamper') == 'source':
|
||||
(Path(job['source']).parent/'database/icdb.dat').write_bytes(b'changed staged input')
|
||||
validate_transaction(job, evidence.parent)
|
||||
if state['fail_add']: return {'success': False, 'safe_to_continue': True, 'detail': 'native failure'}
|
||||
staged = Path(job['source'])
|
||||
(staged.parent/'database/icdb.dat').write_bytes(b'native database normalization')
|
||||
work = library.parent/'ReusableBlocksLP'/job['name']/'Work'
|
||||
shutil.copytree(staged.parent, work)
|
||||
pcb_project = work/'PCB/pcb.prj'
|
||||
text = pcb_project.read_text()
|
||||
import re
|
||||
pcb_project.write_text(re.sub(r'KEY CentralLibrary "[^"]*"', 'KEY CentralLibrary "wrong-relative.lmc"', text))
|
||||
if state['corrupt'] == 'unknown_cdbsvr':
|
||||
unknown = work/'database/cdbsvr/persistent.dat'; unknown.parent.mkdir(parents=True, exist_ok=True)
|
||||
unknown.write_bytes(b'unexpected engineering content')
|
||||
elif state['corrupt']: (work/'PCB/Layout/LayoutDB.lyt').write_bytes(b'wrong physical design')
|
||||
state['entries'] = [{'name': job['name'], 'partition': job['partition'],
|
||||
'engineering_identity': engineering_identity(work/staged.name)}]
|
||||
return {'success': True, 'safe_to_continue': True, 'identity_matches': False}
|
||||
if job['operation'] == 'remove':
|
||||
state['entries'] = []
|
||||
shutil.rmtree(library.parent/'ReusableBlocksLP'/job['name'])
|
||||
return {'success': True, 'safe_to_continue': True}
|
||||
pytest.fail('unexpected native operation')
|
||||
monkeypatch.setattr(native.NativeManagementAdapter, '_worker', worker)
|
||||
request = ManagementRequest('add', library, 'Block', source=source, auto_verify=False,
|
||||
evidence_dir=tmp_path/'evidence')
|
||||
yield root, request, state
|
||||
from cbb_refresh_verify.library_lock import LibraryLock, _owners, _guard, blocker_path
|
||||
with _guard:
|
||||
owner = _owners.pop(LibraryLock(library).key, None)
|
||||
if owner: owner['handle'].Close()
|
||||
blocker_path(library).unlink(missing_ok=True)
|
||||
|
||||
|
||||
def execute(root, request):
|
||||
from cbb_refresh_verify.native_management import run_owned_fixture_management
|
||||
return run_owned_fixture_management(request, root, probe_token='owned', idle_check=lambda *_: None)
|
||||
|
||||
|
||||
def test_owned_fixture_engine_add_conflict_update_remove_and_identity(transaction):
|
||||
from cbb_refresh_verify.native_engineering_identity import project_library
|
||||
root, request, state = transaction
|
||||
original = request.source.read_bytes()
|
||||
assert execute(root, request).status == 'UPLOADED'
|
||||
registered = request.library.parent/'ReusableBlocksLP/Block/Work/Source.prj'
|
||||
assert project_library(registered) == request.library.resolve()
|
||||
calls = len(state['calls'])
|
||||
assert execute(root, request).status == 'UPLOAD_FAILED'
|
||||
assert state['calls'][calls:] == ['catalog']
|
||||
(request.source.parent/'PCB/Layout/LayoutDB.lyt').write_bytes(b'replacement physical design')
|
||||
assert execute(root, replace(request, operation='update')).status == 'UPLOADED'
|
||||
assert (registered.parent/'PCB/Layout/LayoutDB.lyt').read_bytes() == b'replacement physical design'
|
||||
assert execute(root, replace(request, operation='remove', source=None)).status == 'REMOVED'
|
||||
assert execute(root, request).status == 'UPLOADED'
|
||||
assert request.source.read_bytes() == original
|
||||
|
||||
|
||||
@pytest.mark.parametrize('corrupt', [True, 'unknown_cdbsvr'])
|
||||
def test_native_registered_but_asset_mismatch_retains_recovery(transaction, corrupt):
|
||||
root, request, state = transaction
|
||||
state['corrupt'] = corrupt
|
||||
result = execute(root, request)
|
||||
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):
|
||||
root, request, state = transaction
|
||||
assert execute(root, request).status == 'UPLOADED'
|
||||
state['fail_add'] = True
|
||||
result = execute(root, replace(request, operation='update'))
|
||||
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']]
|
||||
|
||||
|
||||
def test_fixture_grant_cannot_authorize_arbitrary_library(transaction, tmp_path):
|
||||
from cbb_refresh_verify.native_management import run_owned_fixture_management
|
||||
root, request, state = transaction
|
||||
other = tmp_path/'production.lmc'; other.write_bytes(b'unchanged')
|
||||
with pytest.raises(ValueError):
|
||||
run_owned_fixture_management(replace(request, library=other), root, probe_token='owned')
|
||||
assert other.read_bytes() == b'unchanged' and not state['calls']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('tamper', ['nonce', 'supervisor', 'baseline', 'source'])
|
||||
def test_worker_rejects_modified_management_authority_before_add(transaction, tamper):
|
||||
root, request, state = transaction
|
||||
state['tamper'] = tamper
|
||||
original = request.source.read_bytes()
|
||||
result = execute(root, request)
|
||||
assert result.status == 'RECOVERY_REQUIRED' and not result.safe_to_continue
|
||||
assert state['entries'] == [] and request.source.read_bytes() == original
|
||||
|
||||
|
||||
def test_long_staged_runtime_path_blocks_update_before_native_remove(transaction):
|
||||
root, request, state = transaction
|
||||
assert execute(root, request).status == 'UPLOADED'
|
||||
runtime = request.source.parent/'CES/LogFiles'/('long-'+'x'*100)/('y'*60+'.log')
|
||||
runtime.parent.mkdir(parents=True); runtime.write_bytes(b'complete original runtime history')
|
||||
before = len(state['calls'])
|
||||
result = execute(root, replace(request, operation='update'))
|
||||
assert result.status == 'UPLOAD_FAILED' and result.safe_to_continue
|
||||
assert 'NATIVE_PATH_TOO_LONG' in result.detail
|
||||
assert state['calls'][before:] == ['catalog']
|
||||
assert len(state['entries']) == 1 and runtime.read_bytes() == b'complete original runtime history'
|
||||
@@ -0,0 +1,189 @@
|
||||
"""Exercise actual guards/snapshots; only the native worker transport is fake."""
|
||||
from dataclasses import asdict
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def prohibit_native_launch(monkeypatch):
|
||||
"""Backstop independent of the boundary under development: no live apps."""
|
||||
import subprocess, win32process, win32com.client
|
||||
import cbb_refresh_verify.desktop_worker as desktop
|
||||
def forbidden(*a, **k):
|
||||
raise AssertionError('Native/process launch is forbidden in probe-guard tests')
|
||||
monkeypatch.setattr(desktop, 'DesktopWorker', forbidden)
|
||||
monkeypatch.setattr(win32process, 'CreateProcess', forbidden)
|
||||
monkeypatch.setattr(subprocess, 'Popen', forbidden)
|
||||
monkeypatch.setattr(win32com.client, 'Dispatch', forbidden)
|
||||
monkeypatch.setattr(win32com.client, 'DispatchEx', forbidden)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def probe_files(tmp_path):
|
||||
def source(root, data):
|
||||
(root/'database').mkdir(parents=True)
|
||||
(root/'database/icdb.dat').write_bytes(data)
|
||||
(root/'PCB/Layout').mkdir(parents=True)
|
||||
(root/'PCB/Layout/LayoutDB.lyt').write_bytes(data)
|
||||
(root/'PCB/pcb.prj').write_text('KEY CentralLibrary "old.lmc"\n')
|
||||
(root/'PCB/Block.pcb').write_text('KEY ProjectFile "../Block.prj"\n')
|
||||
project = root/'Block.prj'
|
||||
project.write_text('KEY RootBlock "Block"\nKEY iCDBDir "database"\nKEY PCBDesignPath "PCB/Block.pcb"\n')
|
||||
return project
|
||||
original = source(tmp_path/'original', b'original')
|
||||
replacement = source(tmp_path/'replacement', b'replacement')
|
||||
root = tmp_path/'probe'
|
||||
yield original, replacement, root
|
||||
# Only file fakes ran; close this test's deliberately retained OS handle.
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
library = root/'native-add/fixture/mentor_lib.lmc'
|
||||
with locks._guard:
|
||||
owner = locks._owners.pop(locks._key(library.with_name('.mentor_lib.cbb-upload.lock')), None)
|
||||
if owner:
|
||||
owner['handle'].Close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fault', ['create_unsafe', 'first_catalog_exception', 'remove_unsafe',
|
||||
'remove_exception', 'after_remove_exception', 'readd_unsafe', 'readd_exception', 'final_catalog_exception'])
|
||||
def test_isolated_probe_retains_guard_on_uncertain_native_lifecycle(probe_files, monkeypatch, fault):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
from cbb_refresh_verify.management import Capability, CatalogEntry, engineering_identity
|
||||
original, replacement, root = probe_files
|
||||
records = []
|
||||
monkeypatch.setattr(native.NativeManagementAdapter, 'capabilities', lambda self:
|
||||
{'remove': Capability(True, 'file transport fixture')})
|
||||
def launch(self, job, evidence):
|
||||
nonlocal records
|
||||
evidence = Path(evidence)
|
||||
operation = job['operation']
|
||||
library = Path(job['library'])
|
||||
if operation == 'probe_create_add':
|
||||
# Directory creation belongs to the real supervising adapter.
|
||||
assert native.prepare_probe_library(job, evidence) == library
|
||||
library.write_bytes(b'native-created fixture')
|
||||
(root/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': job['probe_token']}))
|
||||
records = [CatalogEntry(job['name'], 'Logical-Physical', engineering_identity(job['source']))]
|
||||
if fault == 'create_unsafe':
|
||||
return {'success': False, 'safe_to_continue': False, 'detail': 'creation owner retained'}
|
||||
elif operation == 'remove':
|
||||
if fault == 'remove_exception':
|
||||
raise TimeoutError('native removal transport disconnected')
|
||||
if fault == 'remove_unsafe':
|
||||
return {'success': False, 'safe_to_continue': False, 'detail': 'removal owner retained'}
|
||||
records = []
|
||||
elif operation == 'probe_readd':
|
||||
if fault == 'readd_exception':
|
||||
raise TimeoutError('native readd transport disconnected')
|
||||
if fault == 'readd_unsafe':
|
||||
return {'success': False, 'safe_to_continue': False, 'detail': 'readd owner retained'}
|
||||
records = [CatalogEntry(job['name'], 'Logical-Physical', engineering_identity(job['source']))]
|
||||
elif operation == 'catalog':
|
||||
stage_fault = {'reopen': 'first_catalog_exception', 'after-remove': 'after_remove_exception',
|
||||
'reopen-update': 'final_catalog_exception'}.get(evidence.name)
|
||||
if stage_fault == fault:
|
||||
raise TimeoutError('catalog owner release unknown')
|
||||
return {'success': True, 'safe_to_continue': True, 'library': str(library), 'flow': 1,
|
||||
'entries': [asdict(entry) for entry in records], 'detail': 'file fixture result'}
|
||||
class Worker:
|
||||
def __init__(self, command, desktop):
|
||||
request = Path(command[-1])
|
||||
job = json.loads(request.read_text(encoding='utf-8'))
|
||||
result = launch(None, job, request.parent)
|
||||
(request.parent/'result.json').write_text(json.dumps(result), encoding='utf-8')
|
||||
def poll(self): return 0
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
try:
|
||||
native.run_isolated_probe(original, root, 'Block', replacement_source=replacement)
|
||||
except TimeoutError:
|
||||
pass
|
||||
library = root/'native-add/fixture/mentor_lib.lmc'
|
||||
assert locks.has_blocker(library)
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
with locks.LibraryLock(library): pass
|
||||
assert (root/'probe-result.json').is_file()
|
||||
from contextlib import contextmanager
|
||||
@contextmanager
|
||||
def denied(*a):
|
||||
raise locks.LibraryBusy('fresh proof still sees a native owner')
|
||||
yield
|
||||
monkeypatch.setattr(locks, '_prove_library_released', denied)
|
||||
with pytest.raises(locks.LibraryBusy):
|
||||
locks.release_retained_library(library, root/'release-checks')
|
||||
assert locks.has_blocker(library)
|
||||
@contextmanager
|
||||
def released(*a):
|
||||
yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]}
|
||||
monkeypatch.setattr(locks, '_prove_library_released', released)
|
||||
assert locks.release_retained_library(library, root/'release-checks')['status'] == 'RELEASE_CONFIRMED'
|
||||
with locks.LibraryLock(library): pass
|
||||
|
||||
|
||||
def test_mapping_context_is_serialized_and_restored_before_catalog(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import cbb_refresh_verify.network_path as network
|
||||
captured = []
|
||||
class Worker:
|
||||
def __init__(self, command, desktop):
|
||||
request = Path(command[-1])
|
||||
job = json.loads(request.read_text(encoding='utf-8'))
|
||||
captured.append(job)
|
||||
(request.parent/'result.json').write_text(json.dumps({'success': True, 'safe_to_continue': True,
|
||||
'library': job['library'], 'flow': 1, 'entries': []}), encoding='utf-8')
|
||||
def poll(self): return 0
|
||||
def close(self): pass
|
||||
adapter = native.NativeManagementAdapter(worker_factory=Worker)
|
||||
adapter.bind_library_mapping('S:', r'\\server\share')
|
||||
library = Path('S:/library/mentor.lmc')
|
||||
adapter.catalog(library, tmp_path/'catalog')
|
||||
assert captured[0]['library'] == str(library)
|
||||
assert captured[0]['mapped_drive'] == 'S:'
|
||||
order = []
|
||||
monkeypatch.setattr(native, '_current_desktop', lambda: (None, captured[0]['desktop']))
|
||||
monkeypatch.setattr(network, 'ensure_mapped_drive_visible', lambda drive, remote:
|
||||
order.append(('mapping', drive, remote)))
|
||||
monkeypatch.setattr(native, '_catalog_native', lambda job, root:
|
||||
(order.append(('catalog', job['library'])) or {'success': True, 'safe_to_continue': True}))
|
||||
assert native.management_worker_main(tmp_path/'catalog/request.json') == 0
|
||||
assert order == [('mapping', 'S:', r'\\server\share'), ('catalog', str(library))]
|
||||
|
||||
|
||||
def test_removal_cli_keeps_mapped_drive_spelling(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
from cbb_refresh_verify.management import Capability
|
||||
job = {'operation': 'remove', 'library': 'S:\\library\\mentor.lmc', 'name': 'Block',
|
||||
'partition': 'Logical-Physical', 'desktop': 'CbbManagement_fixture',
|
||||
'mentor_home': str(tmp_path), 'timeout_seconds': 10}
|
||||
request = tmp_path/'request.json'; request.write_text(json.dumps(job), encoding='utf-8')
|
||||
monkeypatch.setattr(native, '_current_desktop', lambda: (None, job['desktop']))
|
||||
monkeypatch.setattr(native.NativeManagementAdapter, 'capabilities', lambda self: {'remove': Capability(True, 'fake transport')})
|
||||
commands = []
|
||||
class Process:
|
||||
pid = 1234
|
||||
def __init__(self, command, *, stdout, **kwargs):
|
||||
commands.append(command)
|
||||
stdout.write(b"# Note: Removed REUSE_BLOCK 'Block' from partition 'Logical-Physical'.\n")
|
||||
stdout.flush()
|
||||
def wait(self, **kwargs): return 0
|
||||
monkeypatch.setattr(native.subprocess, 'Popen', Process)
|
||||
assert native.management_worker_main(request) == 0
|
||||
assert commands[0][1] == job['library']
|
||||
|
||||
|
||||
def test_staged_project_alignment_keeps_selected_mapped_drive(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.backup_recovery import _align_library
|
||||
project = tmp_path/'Block.prj'
|
||||
project.write_text('KEY CentralLibrary "old.lmc"\n', encoding='utf-8')
|
||||
pcb = tmp_path/'PCB/Block.pcb'; pcb.parent.mkdir(); pcb.touch()
|
||||
pcb_project = pcb.parent/'pcb.prj'
|
||||
pcb_project.write_text('KEY CentralLibrary "old.lmc"\n', encoding='utf-8')
|
||||
library = Path('S:/library/mentor.lmc')
|
||||
original_resolve = Path.resolve
|
||||
monkeypatch.setattr(Path, 'resolve', lambda path, *a, **k:
|
||||
Path(r'\\server\share\library\mentor.lmc') if path == library else original_resolve(path, *a, **k))
|
||||
_align_library(project, pcb, library)
|
||||
for path in (project, pcb_project):
|
||||
assert 'KEY CentralLibrary "S:\\library\\mentor.lmc"' in path.read_text(encoding='utf-8')
|
||||
@@ -0,0 +1,571 @@
|
||||
"""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_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', resume=True)
|
||||
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)
|
||||
@@ -0,0 +1,66 @@
|
||||
"""Real EE7.9.5 PRJ shape: PCB pcb.prj may use LayoutDB without iCDBDir."""
|
||||
import pytest
|
||||
|
||||
from cbb_refresh_verify.management import source_project, engineering_identity
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def healthy_source(tmp_path):
|
||||
(tmp_path/'database').mkdir()
|
||||
(tmp_path/'database/icdb.dat').write_bytes(b'schematic database')
|
||||
(tmp_path/'PCB/Layout').mkdir(parents=True)
|
||||
(tmp_path/'PCB/Layout/LayoutDB.lyt').write_bytes(b'layout database')
|
||||
source = tmp_path/'IP101GR.prj'
|
||||
source.write_text('KEY RootBlock "IP101GR"\nKEY FlowType "DX"\n'
|
||||
'KEY iCDBDir ".\\database"\nKEY PCBDesignPath "PCB\\IP101GR.pcb"\n', encoding='utf-8')
|
||||
(tmp_path/'PCB/IP101GR.pcb').write_text('KEY ProjectFile "..\\IP101GR.prj"\n', encoding='utf-8')
|
||||
(tmp_path/'PCB/pcb.prj').write_text('SECTION Project\nKEY CentralLibrary "old.lmc"\nENDSECTION\n', encoding='utf-8')
|
||||
return source
|
||||
|
||||
|
||||
def test_layout_based_pcb_without_independent_icdb_directory_is_complete(healthy_source):
|
||||
source, pcb, top = source_project(healthy_source)
|
||||
assert source == healthy_source
|
||||
assert pcb == healthy_source.parent/'PCB/IP101GR.pcb'
|
||||
assert top == 'IP101GR'
|
||||
assert engineering_identity(source)['top_level_block'] == 'IP101GR'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('missing', ['database/icdb.dat', 'PCB/Layout/LayoutDB.lyt'])
|
||||
def test_optional_pcb_icdb_does_not_relax_required_engineering_files(healthy_source, missing):
|
||||
(healthy_source.parent/missing).unlink()
|
||||
with pytest.raises(ValueError):
|
||||
source_project(healthy_source)
|
||||
|
||||
|
||||
def test_linked_legacy_pcb_uses_explicit_main_project_without_guessing_template(healthy_source):
|
||||
(healthy_source.parent/'PCB/pcb.prj').rename(healthy_source.parent/'PCB/template.prj')
|
||||
assert source_project(healthy_source)[0] == healthy_source
|
||||
(healthy_source.parent/'PCB/IP101GR.pcb').write_text('KEY ProjectFile "../wrong.prj"\n')
|
||||
with pytest.raises(ValueError, match='PCB'):
|
||||
source_project(healthy_source)
|
||||
|
||||
|
||||
def test_main_schematic_icdb_declaration_remains_required(healthy_source):
|
||||
text = healthy_source.read_text(encoding='utf-8').replace('KEY iCDBDir ".\\database"\n', '')
|
||||
healthy_source.write_text(text, encoding='utf-8')
|
||||
with pytest.raises(ValueError, match='iCDBDir'):
|
||||
source_project(healthy_source)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('declared', [
|
||||
'KEY iCDBDir "missing"\n',
|
||||
'KEY iCDBDir "..\\..\\outside"\n',
|
||||
'KEY iCDBDir "C:\\external"\n',
|
||||
'KEY iCDBDir ""\n',
|
||||
'KEY iCDBDir "..\\database"\nKEY iCDBDir "..\\database"\n',
|
||||
])
|
||||
def test_explicit_pcb_icdb_reference_must_be_unique_contained_and_exist(healthy_source, declared):
|
||||
(healthy_source.parent/'PCB/pcb.prj').write_text(declared, encoding='utf-8')
|
||||
with pytest.raises(ValueError):
|
||||
source_project(healthy_source)
|
||||
|
||||
|
||||
def test_valid_explicit_pcb_icdb_directory_remains_supported(healthy_source):
|
||||
(healthy_source.parent/'PCB/pcb.prj').write_text('KEY iCDBDir "..\\database"\n', encoding='utf-8')
|
||||
assert source_project(healthy_source)[0] == healthy_source
|
||||
@@ -0,0 +1,113 @@
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
|
||||
|
||||
|
||||
def test_management_can_select_ready_and_capability_disabled_explanation(qtbot, tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.management import Capability
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
caps = {name: Capability(name == 'remove', name + ' not validated') for name in ('add', 'update', 'remove')}
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((CbbRecord('Ready', 'Logical-Physical', status='READY'),)),
|
||||
data_root=tmp_path, management_adapter=SimpleNamespace(capabilities=lambda: caps))
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('library.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
window.select_visible_button.click()
|
||||
assert window.remove_button.isEnabled()
|
||||
assert not window.add_button.isEnabled() and not window.update_button.isEnabled()
|
||||
assert 'not validated' in window.management_capability_label.text()
|
||||
assert not window.selected_batch_button.isEnabled()
|
||||
window.remove_button.click()
|
||||
assert window.management_dialog.requests()[0].name == 'Ready'
|
||||
assert window.management_dialog.requests()[0].partition == 'Logical-Physical'
|
||||
window.management_dialog.reject()
|
||||
|
||||
|
||||
def test_management_dialog_preview_confirmation_and_result_status(qtbot, tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.management import ManagementRequest, ManagementResult
|
||||
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
|
||||
requests = [ManagementRequest('remove', tmp_path/'lib.lmc', 'Block', evidence_dir=tmp_path/'out')]
|
||||
calls, confirmations = [], []
|
||||
def runner(items, **_):
|
||||
calls.extend(items)
|
||||
return [ManagementResult('remove', items[0].library, 'Block', 'Logical-Physical', None,
|
||||
False, 'complete', 'REMOVED', True, 'removed', tmp_path/'checkpoint.json')]
|
||||
dialog = ManagementDialog(requests, runner=runner)
|
||||
qtbot.addWidget(dialog)
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (confirmations.append(a[2]) or QMessageBox.No))
|
||||
dialog.execute_button.click()
|
||||
assert not calls and 'Block' in confirmations[0] and '手动备份' in confirmations[0]
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: QMessageBox.Yes)
|
||||
dialog.execute_button.click()
|
||||
qtbot.waitUntil(lambda: not dialog.busy)
|
||||
assert len(calls) == 1 and '已移除' in dialog.status_label.text()
|
||||
assert str(tmp_path/'checkpoint.json') in dialog.output.toPlainText()
|
||||
assert not dialog.execute_button.isEnabled()
|
||||
|
||||
|
||||
def test_add_dialog_default_auto_verify_and_source_identity_rows(qtbot, tmp_path):
|
||||
from cbb_refresh_verify.management import ManagementRequest
|
||||
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
|
||||
dialog = ManagementDialog([ManagementRequest('add', tmp_path/'lib.lmc', '', evidence_dir=tmp_path/'out')])
|
||||
qtbot.addWidget(dialog)
|
||||
dialog.add_sources([tmp_path/'One.prj', tmp_path/'Two.prj'])
|
||||
requests = dialog.requests()
|
||||
assert [r.name for r in requests] == ['One', 'Two']
|
||||
assert all(r.auto_verify and r.source.suffix == '.prj' for r in requests)
|
||||
dialog.auto_verify.setChecked(False)
|
||||
assert not any(r.auto_verify for r in dialog.requests())
|
||||
dialog._progress({'stage': 'prepare_identity', 'block': 'One'})
|
||||
assert '正在核对来源工程内容' in dialog.status_label.text()
|
||||
assert 'prepare_identity' not in dialog.status_label.text()
|
||||
|
||||
|
||||
def test_restart_shows_unfinished_without_starting_worker(qtbot, tmp_path):
|
||||
import json
|
||||
from cbb_refresh_verify.management import Capability
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
checkpoint = tmp_path/'management/interrupted/checkpoint.json'
|
||||
checkpoint.parent.mkdir(parents=True)
|
||||
checkpoint.write_text(json.dumps({'status': 'RUNNING', 'stage': 'native_remove', 'name': 'Block'}), encoding='utf-8')
|
||||
adapter = SimpleNamespace(capabilities=lambda: {n: Capability(False, 'disabled') for n in ('add', 'update', 'remove')})
|
||||
window = MainWindow(data_root=tmp_path, management_adapter=adapter)
|
||||
qtbot.addWidget(window)
|
||||
assert '未完成' in window.unfinished_label.text() and not window.busy
|
||||
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()
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation', ['add', 'update', 'remove'])
|
||||
def test_management_prompts_for_manual_backup_and_cancel_starts_nothing(qtbot, tmp_path, monkeypatch, operation):
|
||||
from cbb_refresh_verify.management import ManagementRequest
|
||||
from cbb_refresh_verify.ui.management_dialog import ManagementDialog
|
||||
source = tmp_path/'Block.prj'; source.touch()
|
||||
seen, calls = [], []
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (seen.append(a[2]) or QMessageBox.No))
|
||||
dialog = ManagementDialog([ManagementRequest(operation, tmp_path/'lib.lmc', 'Block', source=source)],
|
||||
runner=lambda *a, **k: calls.append(a))
|
||||
qtbot.addWidget(dialog)
|
||||
dialog.execute_button.click()
|
||||
assert not calls and not dialog.busy
|
||||
assert len(seen) == 1 and '手动备份' in seen[0] and '不会自动备份' in seen[0]
|
||||
assert '完整备份与检查点' not in seen[0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('action', ['verify_all', 'verify_selected'])
|
||||
def test_batch_prompts_once_for_manual_backup_and_cancel_does_not_start(qtbot, tmp_path, monkeypatch, action):
|
||||
from cbb_refresh_verify.ui.main_window import MainWindow
|
||||
from cbb_refresh_verify.models import CbbRecord, LibraryReadResult
|
||||
window = MainWindow(reader=lambda _: LibraryReadResult((CbbRecord('Block', 'LP'),)), data_root=tmp_path)
|
||||
qtbot.addWidget(window)
|
||||
window.lmc_edit.setText('library.lmc'); window.load_library()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
window.select_visible_button.click()
|
||||
seen, calls = [], []
|
||||
monkeypatch.setattr(window, '_run', lambda *a, **k: calls.append(a))
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (seen.append(a[2]) or QMessageBox.No))
|
||||
getattr(window, action)()
|
||||
assert not calls
|
||||
assert len(seen) == 1 and '手动备份' in seen[0] and '不会自动备份' in seen[0]
|
||||
|
||||
|
||||
def test_restore_prompts_for_manual_backup_before_starting_worker(qtbot, tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest, VerifyResult
|
||||
from cbb_refresh_verify.ui.backup_recovery_dialog import BackupRecoveryDialog
|
||||
calls, seen = [], []
|
||||
source = tmp_path/'Block.prj'; source.touch()
|
||||
monkeypatch.setattr(QMessageBox, 'question', lambda *a, **k: (seen.append(a[2]) or QMessageBox.No))
|
||||
dialog = BackupRecoveryDialog(['Block'], lambda n: VerifyRequest(tmp_path/'lib.lmc', n, tmp_path/'out'),
|
||||
recoverer=lambda *a, **k: (calls.append(a) or VerifyResult('PASS')))
|
||||
qtbot.addWidget(dialog)
|
||||
dialog.source_edit.setText(str(source)); dialog.restore_button.click()
|
||||
qtbot.waitUntil(lambda: not dialog.busy)
|
||||
assert not calls
|
||||
assert len(seen) == 1 and '手动备份' in seen[0] and '不会自动备份' in seen[0]
|
||||
@@ -0,0 +1,195 @@
|
||||
"""File-only native failure and legacy path-boundary checks."""
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
ERROR = b'REUSE BLOCK: INFORMATION: Block\tError! Cannot copy iCDB base of Reusable Block project.\r\n'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['', 'target', 'pcb_present', 'question', 'extra_button', 'wrong_button', 'escape'])
|
||||
def test_editor_missing_pcb_notice_is_only_for_unrelated_incomplete_entries(tmp_path, change):
|
||||
from cbb_refresh_verify.native_add_failures import missing_other_pcb_notice
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
work = tmp_path/'ReusableBlocksLP/Missing/Work'
|
||||
(work/'PCB/Layout').mkdir(parents=True)
|
||||
(work/'Missing.prj').write_text('KEY PCBDesignPath "PCB/Missing.pcb"\n')
|
||||
text = ('The following Reusable Blocks do not contain PCB files and will not appear in the list of available reusable blocks:'
|
||||
'\n\n\tMissing')
|
||||
buttons = [{'hwnd':123, 'id':2, 'class':'Button', 'text':'确定', 'enabled':True}]
|
||||
target = 'Missing' if change == 'target' else 'Current'
|
||||
if change == 'pcb_present':
|
||||
(work/'PCB/Missing.pcb').touch(); (work/'PCB/Layout/LayoutDB.lyt').touch()
|
||||
if change == 'question': text += '\nOverwrite existing files?'
|
||||
if change == 'extra_button': buttons.append({**buttons[0], 'hwnd':456})
|
||||
if change == 'wrong_button': buttons[0]['text']='Yes'
|
||||
if change == 'escape': text=text.replace('\tMissing', '\t../other')
|
||||
result = missing_other_pcb_notice(library, target, 'Library Manager', text, buttons)
|
||||
assert result == ({'names':['Missing'], 'button':123} if not change else None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reason', ['cancelled', 'supervisor_exited'])
|
||||
def test_add_cannot_submit_after_timeout_or_supervisor_exit(tmp_path, monkeypatch, reason):
|
||||
from cbb_refresh_verify.native_management import _require_add_supervisor
|
||||
import cbb_refresh_verify.library_lock as locks
|
||||
if reason == 'cancelled': (tmp_path/'cancel-native-add.signal').touch()
|
||||
monkeypatch.setattr(locks, '_supervisor_alive', lambda _:reason != 'supervisor_exited')
|
||||
with pytest.raises(RuntimeError, match='NATIVE_ADD_CANCELLED'):
|
||||
_require_add_supervisor({'supervisor_pid':123}, tmp_path)
|
||||
|
||||
|
||||
def test_add_timeout_publishes_cancellation_without_killing_native_owner(tmp_path, monkeypatch):
|
||||
import cbb_refresh_verify.native_management as native
|
||||
monkeypatch.setattr(native, '_retained_workers', [])
|
||||
class Running:
|
||||
def __init__(self, *args): pass
|
||||
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,
|
||||
'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
|
||||
|
||||
|
||||
def test_cancellation_during_log_evidence_prevents_native_submit(tmp_path, monkeypatch):
|
||||
import os,sys
|
||||
from types import SimpleNamespace
|
||||
import win32gui,win32process
|
||||
import cbb_refresh_verify.native_management as native
|
||||
import cbb_refresh_verify.native_add_failures as failures
|
||||
from test_management import project
|
||||
source=project(tmp_path/'source',name='Block')
|
||||
root=tmp_path/'native-add';root.mkdir()
|
||||
library=root/'fixture/lib.lmc';library.parent.mkdir();library.touch()
|
||||
for path in (source,source.parent/'PCB/pcb.prj'):
|
||||
with path.open('a',encoding='utf-8') as stream:stream.write(f'KEY CentralLibrary "{library}"\n')
|
||||
state={'new':False,'posted':[]}
|
||||
tab=SimpleNamespace(window_text=lambda:'Logical - Physical',is_selected=lambda:True)
|
||||
monkeypatch.setitem(sys.modules,'pywinauto',SimpleNamespace(Desktop=lambda **k:SimpleNamespace(
|
||||
window=lambda **k:SimpleNamespace(descendants=lambda **k:[tab]))))
|
||||
monkeypatch.setattr(win32process,'GetWindowThreadProcessId',lambda h:(1,99))
|
||||
monkeypatch.setattr(win32gui,'IsWindowVisible',lambda h:True)
|
||||
monkeypatch.setattr(win32gui,'IsWindowEnabled',lambda h:True)
|
||||
monkeypatch.setattr(win32gui,'GetWindowText',lambda h:{100:'Reusable Blocks',200:'New Reusable Block'}[int(h)])
|
||||
monkeypatch.setattr(win32gui,'GetDlgItem',lambda h,c:110 if h==100 else 210 if c==1083 else c+1000)
|
||||
monkeypatch.setattr(win32gui,'GetClassName',lambda h:'Edit')
|
||||
monkeypatch.setattr(native,'_set_probe_field',lambda *a:None)
|
||||
def post(h,*args):
|
||||
state['posted'].append(h)
|
||||
if h==110:state['new']=True
|
||||
monkeypatch.setattr(win32gui,'PostMessage',post)
|
||||
original=native._write_json
|
||||
def write(path,data):
|
||||
original(path,data)
|
||||
if path.name=='native-add-log-baseline.json':(root/'cancel-native-add.signal').touch()
|
||||
monkeypatch.setattr(native,'_write_json',write)
|
||||
monkeypatch.setattr(failures,'observe_add_failure',lambda *a:(_ for _ in ()).throw(RuntimeError('unsafe OK posted')))
|
||||
desktop=SimpleNamespace(EnumDesktopWindows=lambda:[100,200] if state['new'] else [100])
|
||||
job={'operation':'probe_create_add','source':str(source),'library':str(library),
|
||||
'name':'Block','timeout_seconds':5,'supervisor_pid':os.getpid()}
|
||||
with pytest.raises(RuntimeError,match='NATIVE_ADD_CANCELLED'):
|
||||
native._probe_add_dialog_impl(SimpleNamespace(OnReusableBlocksEditor=lambda:None),job,desktop,{99},root)
|
||||
assert state['posted']==[110]
|
||||
|
||||
|
||||
def test_copy_failure_log_must_be_new_and_match_current_block(tmp_path):
|
||||
from cbb_refresh_verify.native_add_failures import snapshot_add_log, fresh_add_failure
|
||||
library = tmp_path/'lib.lmc'; library.touch()
|
||||
log = tmp_path/'LogFiles/ReuseBlockLogLO.txt'; log.parent.mkdir(); log.write_bytes(ERROR)
|
||||
baseline = snapshot_add_log(library)
|
||||
assert fresh_add_failure(baseline, 'Block') is None
|
||||
with log.open('ab') as stream: stream.write(ERROR.replace(b'Block\t', b'Other\t'))
|
||||
assert fresh_add_failure(baseline, 'Block') is None
|
||||
with log.open('ab') as stream: stream.write(ERROR)
|
||||
assert 'Cannot copy iCDB base' in fresh_add_failure(baseline, 'Block')
|
||||
# A replaced/rotated prefix is not evidence that an old error is new.
|
||||
log.write_bytes(b'replaced\n'+ERROR)
|
||||
assert fresh_add_failure(baseline, 'Block') is None
|
||||
|
||||
|
||||
def test_newly_created_failure_log_is_current_round_evidence(tmp_path):
|
||||
from cbb_refresh_verify.native_add_failures import snapshot_add_log, fresh_add_failure
|
||||
library = tmp_path/'lib.lmc'
|
||||
baseline = snapshot_add_log(library)
|
||||
log = tmp_path/'LogFiles/ReuseBlockLogLO.txt'; log.parent.mkdir(); log.write_bytes(ERROR)
|
||||
assert fresh_add_failure(baseline, 'Block')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('change', ['none', 'title', 'question', 'button_id', 'button_text', 'disabled', 'extra_button'])
|
||||
def test_only_observed_path_error_dialog_can_be_acknowledged(change):
|
||||
from cbb_refresh_verify.native_add_failures import failure_ack_button
|
||||
title, text = 'Library Manager', '\n系统找不到指定的路径。\r\n'
|
||||
buttons = [{'hwnd': 123, 'id': 2, 'class': 'Button', 'text': '确定', 'enabled': True}]
|
||||
if change == 'title': title = 'Unrelated'
|
||||
if change == 'question': text += '是否继续?'
|
||||
if change == 'button_id': buttons[0]['id'] = 1
|
||||
if change == 'button_text': buttons[0]['text'] = '是'
|
||||
if change == 'disabled': buttons[0]['enabled'] = False
|
||||
if change == 'extra_button': buttons.append({**buttons[0], 'hwnd': 456})
|
||||
assert failure_ack_button(title, text, buttons) == (123 if change == 'none' else None)
|
||||
|
||||
|
||||
def test_native_path_guard_includes_runtime_files_and_projected_target(tmp_path):
|
||||
from cbb_refresh_verify.native_add_failures import validate_native_copy_paths
|
||||
source = tmp_path/'source/Block.prj'; source.parent.mkdir(); source.touch()
|
||||
validate_native_copy_paths(source, tmp_path/'library/lib.lmc', 'Block')
|
||||
runtime = source.parent/'CES/LogFiles'/('x'*120)/('y'*90+'.log')
|
||||
runtime.parent.mkdir(parents=True); runtime.touch()
|
||||
with pytest.raises(ValueError, match='NATIVE_PATH_TOO_LONG'):
|
||||
validate_native_copy_paths(source, tmp_path/'library/lib.lmc', 'Block')
|
||||
assert runtime.exists()
|
||||
runtime.unlink()
|
||||
# Even a short staged tree can exceed the legacy limit at its target.
|
||||
target = tmp_path/('library-'+'x'*150)/'lib.lmc'
|
||||
with pytest.raises(ValueError, match='目标'):
|
||||
validate_native_copy_paths(source, target, 'Block')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('kind', ['known', 'unknown', 'foreign'])
|
||||
def test_failure_observer_only_acknowledges_owned_known_dialog(tmp_path, monkeypatch, kind):
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
import win32gui, win32process
|
||||
from cbb_refresh_verify.native_add_failures import observe_add_failure, snapshot_add_log
|
||||
state = {'open': True, 'clicks': []}
|
||||
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda _: (1, 999 if kind == 'foreign' else 123))
|
||||
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda _: True)
|
||||
monkeypatch.setattr(win32gui, 'GetClassName', lambda h: {1: '#32770', 10: 'Button', 11: 'Static'}[h])
|
||||
monkeypatch.setattr(win32gui, 'GetWindowText', lambda h: {1: 'Library Manager', 10: '确定',
|
||||
11: '系统找不到指定的路径。' if kind == 'known' else '未确认的原生提示'}[h])
|
||||
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, callback, p: [callback(c, p) for c in [10, 11]])
|
||||
monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda h: 2 if h == 10 else 0)
|
||||
monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda _: True)
|
||||
monkeypatch.setattr(win32gui, 'IsWindow', lambda _: state['open'])
|
||||
def post(hwnd, message, *_):
|
||||
state['clicks'].append(hwnd); state['open'] = False
|
||||
monkeypatch.setattr(win32gui, 'PostMessage', post)
|
||||
monkeypatch.setattr(win32gui, 'SetForegroundWindow', lambda *_: pytest.fail('foreground input is forbidden'))
|
||||
args = (SimpleNamespace(EnumDesktopWindows=lambda: [1]), {123}, tmp_path,
|
||||
snapshot_add_log(tmp_path/'lib.lmc'), 'Block', {})
|
||||
if kind == 'known':
|
||||
with pytest.raises(RuntimeError, match='NATIVE_ADD_FAILED'): observe_add_failure(*args)
|
||||
result = json.loads((tmp_path/'native-add-output.json').read_text(encoding='utf-8'))
|
||||
assert result['success_message'] is None and result['failure']['acknowledged']
|
||||
assert state['clicks'] == [10]
|
||||
else:
|
||||
observe_add_failure(*args)
|
||||
assert not state['clicks'] and not (tmp_path/'native-add-output.json').exists()
|
||||
assert (tmp_path/'native-add-dialogs.jsonl').exists() is (kind == 'unknown')
|
||||
|
||||
|
||||
def test_path_rejection_returns_safe_before_any_native_identity_or_alignment(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.management import Capability, ManagementRequest
|
||||
from cbb_refresh_verify.native_management import NativeManagementAdapter
|
||||
import cbb_refresh_verify.management_transaction as authority
|
||||
source = tmp_path/'source/Block.prj'; source.parent.mkdir(); source.touch()
|
||||
runtime = source.parent/('x'*150)/('y'*100+'.log')
|
||||
runtime.parent.mkdir(); runtime.write_bytes(b'preserved history')
|
||||
adapter = NativeManagementAdapter(mentor_home=tmp_path)
|
||||
monkeypatch.setattr(adapter, 'capabilities', lambda: {'add': Capability(True, 'test boundary')})
|
||||
monkeypatch.setattr(authority, 'current_lease', lambda _: pytest.fail('long path reached native transaction setup'))
|
||||
request = ManagementRequest('update', tmp_path/'lib.lmc', 'Block', source=source)
|
||||
staged, result = adapter.prepare_add_transaction(request, tmp_path/'evidence', original_source=source)
|
||||
assert staged == request and result.identity is None and result.safe_to_continue
|
||||
assert 'NATIVE_PATH_TOO_LONG' in result.detail and runtime.read_bytes() == b'preserved history'
|
||||
@@ -0,0 +1,263 @@
|
||||
"""Native backup entry: user owns version choice, restoration and confirmation."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
import win32con
|
||||
import win32file
|
||||
|
||||
|
||||
def project_fixture(tmp_path):
|
||||
from cbb_refresh_verify.verify_runner import VerifyRequest
|
||||
library = tmp_path/'library/library.lmc'; library.parent.mkdir(); library.touch()
|
||||
work = library.parent/'ReusableBlocksLP/Block/Work'; work.mkdir(parents=True)
|
||||
project = work/'Block.prj'; project.write_text('KEY iCDBDir "database"\n')
|
||||
return VerifyRequest(library, 'Block', tmp_path/'evidence'), project
|
||||
|
||||
|
||||
def test_backup_entry_can_open_project_without_layout_data(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_backup as native
|
||||
request, project = project_fixture(tmp_path)
|
||||
executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch()
|
||||
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable)
|
||||
calls = []
|
||||
monkeypatch.setattr(native.subprocess, 'Popen', lambda command, **kw:
|
||||
(calls.append(command) or SimpleNamespace(pid=123, poll=lambda: None)))
|
||||
session = native.launch_native_backup(request)
|
||||
record = json.loads(session.job.read_text('utf-8'))
|
||||
assert record['project'] == str(project)
|
||||
assert calls[0][-2] == '--native-backup-worker'
|
||||
assert calls[0][-1] == str(session.job)
|
||||
assert session.library == request.lmc_file and session.block == 'Block'
|
||||
|
||||
|
||||
def test_native_session_closure_does_not_claim_restore_success(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_backup as native
|
||||
from cbb_refresh_verify.library_lock import LibraryLock
|
||||
request, project = project_fixture(tmp_path)
|
||||
executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch()
|
||||
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable)
|
||||
job = request.evidence_dir/'request.json'; job.parent.mkdir()
|
||||
job.write_text(json.dumps({'library': str(request.lmc_file), 'block': 'Block',
|
||||
'project': str(project), 'mentor_bin': ''}))
|
||||
calls = []
|
||||
class NativeProcess:
|
||||
pid = 456
|
||||
def __init__(self, command, cwd):
|
||||
calls.append(command)
|
||||
lock = LibraryLock(request.lmc_file)
|
||||
with pytest.raises(Exception):
|
||||
win32file.CreateFile(str(lock.path), win32con.GENERIC_READ | win32con.GENERIC_WRITE,
|
||||
0, None, win32con.OPEN_ALWAYS, 0, None)
|
||||
def poll(self): return 0
|
||||
def session_pids(self): return []
|
||||
def close(self): pass
|
||||
assert native.native_backup_worker_main(job, process_factory=NativeProcess) == 0
|
||||
assert calls == [[str(executable), '-p', str(project)]]
|
||||
result = json.loads(job.with_name('result.json').read_text('utf-8'))
|
||||
assert result['status'] == 'CLOSED' and result['restore_confirmed'] is False
|
||||
assert result['native_exit_code'] == 0 and result['safe_to_continue'] is True
|
||||
with LibraryLock(request.lmc_file): pass
|
||||
|
||||
|
||||
def test_changed_project_identity_is_rejected_before_native_launch(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_backup as native
|
||||
request, project = project_fixture(tmp_path)
|
||||
job = request.evidence_dir/'request.json'; job.parent.mkdir()
|
||||
job.write_text(json.dumps({'library': str(request.lmc_file), 'block': 'Block',
|
||||
'project': str(project.parent/'Other.prj'), 'mentor_bin': ''}))
|
||||
assert native.native_backup_worker_main(job,
|
||||
process_factory=lambda *a, **k: pytest.fail('native launch on mismatched project')) == 1
|
||||
assert json.loads(job.with_name('result.json').read_text('utf-8'))['status'] == 'FAILED'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('all_evidence_failed', [False, True])
|
||||
def test_state_log_failure_does_not_abandon_live_native_session(tmp_path, monkeypatch, all_evidence_failed):
|
||||
from cbb_refresh_verify import native_backup as native, management
|
||||
request, project = project_fixture(tmp_path)
|
||||
executable = tmp_path/'iCDBProjectBackup.exe'; executable.touch()
|
||||
monkeypatch.setattr(native, 'native_backup_executable', lambda *_: executable)
|
||||
job = request.evidence_dir/'request.json'; job.parent.mkdir()
|
||||
job.write_text(json.dumps({'library': str(request.lmc_file), 'block': 'Block',
|
||||
'project': str(project), 'mentor_bin': ''}))
|
||||
write = management._write_json
|
||||
def broken_state(path, value):
|
||||
if all_evidence_failed or Path(path).name == 'state.json': raise OSError('state log unavailable')
|
||||
write(path, value)
|
||||
monkeypatch.setattr(management, '_write_json', broken_state)
|
||||
observations = []
|
||||
class NativeProcess:
|
||||
pid = 456
|
||||
def __init__(self, *a): pass
|
||||
def poll(self): return 0
|
||||
def session_pids(self):
|
||||
observations.append(True)
|
||||
return [456] if len(observations) == 1 else []
|
||||
def close(self): pass
|
||||
assert native.native_backup_worker_main(job, process_factory=NativeProcess) == 0
|
||||
assert len(observations) >= 2
|
||||
if not all_evidence_failed:
|
||||
assert json.loads(job.with_name('result.json').read_text('utf-8'))['status'] == 'CLOSED'
|
||||
|
||||
|
||||
def test_native_backup_ui_keeps_catalog_interactive_and_refreshes_after_close(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
requests, refreshes = [], []
|
||||
result = tmp_path/'native/result.json'; result.parent.mkdir()
|
||||
job = result.with_name('request.json'); job.write_text('{}')
|
||||
session = SimpleNamespace(library=Path('test.lmc'), block='Beta', job=job,
|
||||
process=SimpleNamespace(poll=lambda: None))
|
||||
window = load_window(qtbot, tmp_path, monkeypatch,
|
||||
native_backup_launcher=lambda req: (requests.append(req) or session),
|
||||
verifier=lambda _: pytest.fail('native backup must not run Verify'),
|
||||
status_refresher=lambda names, _: (refreshes.append(names) or {n: 'PENDING' for n in names}))
|
||||
window.search_edit.setText('Beta'); window.select_visible_button.click()
|
||||
assert window.native_backup_button.isEnabled()
|
||||
window.native_backup_button.click(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert len(requests) == 1 and requests[0].block_name == 'Beta'
|
||||
assert window.search_edit.isEnabled() and window.section_tabs.isEnabled()
|
||||
window.section_tabs.setCurrentIndex(2)
|
||||
window.search_edit.setText('Gamma')
|
||||
assert not window.busy
|
||||
result.write_text(json.dumps({'status': 'CLOSED', 'safe_to_continue': True,
|
||||
'library': str(session.library), 'block': session.block, 'native_exit_code': 0,
|
||||
'restore_confirmed': False}))
|
||||
session.process.poll = lambda: 0
|
||||
window._poll_native_backup()
|
||||
qtbot.waitUntil(lambda: not window.busy)
|
||||
assert refreshes == [('Beta',)]
|
||||
assert window.search_edit.text() == 'Gamma'
|
||||
assert 'PASS' not in window.cbb_table.item(window._row_by_name['Beta'], 3).text()
|
||||
|
||||
|
||||
def test_native_backup_selection_requires_one_visible_non_ready_item(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
window = load_window(qtbot, tmp_path, monkeypatch,
|
||||
native_backup_launcher=lambda _: pytest.fail('unexpected launch'))
|
||||
assert not window.native_backup_button.isEnabled()
|
||||
|
||||
|
||||
def test_native_backup_can_use_current_non_ready_row_without_checkbox(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
window = load_window(qtbot, tmp_path, monkeypatch,
|
||||
native_backup_launcher=lambda _: pytest.fail('unexpected launch'))
|
||||
row = window._row_by_name['Beta']
|
||||
window.cbb_table.setCurrentCell(row, 0)
|
||||
window._update()
|
||||
assert window.native_backup_button.isEnabled()
|
||||
window.search_edit.setText('Alpha'); window.select_visible_button.click()
|
||||
assert not window.native_backup_button.isEnabled()
|
||||
window.search_edit.clear(); window.select_visible_button.click()
|
||||
assert not window.native_backup_button.isEnabled()
|
||||
|
||||
|
||||
def test_closing_app_keeps_manual_native_window_and_mapping(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
window = load_window(qtbot, tmp_path, monkeypatch)
|
||||
retained = []
|
||||
lease = SimpleNamespace(retain_for_recovery=lambda: retained.append(True),
|
||||
close=lambda: pytest.fail('mapping released before native session ends'))
|
||||
session = SimpleNamespace(library=Path('test.lmc'), block='Beta', path_lease=lease,
|
||||
process=SimpleNamespace(poll=lambda: None, terminate=lambda: pytest.fail('native UI terminated')))
|
||||
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
|
||||
|
||||
|
||||
def test_native_launch_failure_does_not_poison_other_features(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
def missing(_): raise ValueError('未找到 iCDB Project Backup')
|
||||
window = load_window(qtbot, tmp_path, monkeypatch, native_backup_launcher=missing)
|
||||
window.search_edit.setText('Beta'); window.select_visible_button.click()
|
||||
window.native_backup_button.click(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert '未找到' in window.status_label.text()
|
||||
assert window.selected_batch_button.isEnabled() and window.native_backup_button.isEnabled()
|
||||
assert not window._management_unsafe
|
||||
|
||||
|
||||
@pytest.mark.parametrize('evidence', ['unconfirmed', 'missing', 'malformed', 'wrong_identity'])
|
||||
def test_unconfirmed_supervisor_exit_keeps_session_conflict_and_mapping(qtbot, tmp_path, monkeypatch, evidence):
|
||||
from test_ui_filters import load_window
|
||||
window = load_window(qtbot, tmp_path, monkeypatch)
|
||||
job = tmp_path/'native/request.json'; job.parent.mkdir(); job.write_text('{}')
|
||||
if evidence != 'missing':
|
||||
job.with_name('result.json').write_text('invalid' if evidence == 'malformed' else json.dumps({
|
||||
'status': 'FAILED', 'safe_to_continue': evidence == 'wrong_identity',
|
||||
'library': 'other.lmc' if evidence == 'wrong_identity' else 'test.lmc',
|
||||
'block': 'Beta', 'detail': 'unconfirmed'}))
|
||||
session = SimpleNamespace(library=Path('test.lmc'), block='Beta', job=job,
|
||||
path_lease=window.library_lease, process=SimpleNamespace(poll=lambda: 1))
|
||||
window._native_backup_session = session
|
||||
window._poll_native_backup(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert window._native_backup_session is session
|
||||
assert not window.batch_button.isEnabled() and window.search_edit.isEnabled()
|
||||
assert not window.native_backup_timer.isActive()
|
||||
|
||||
|
||||
def test_confirmed_native_close_transfers_mapping_to_current_library(qtbot, tmp_path, monkeypatch):
|
||||
from test_ui_filters import load_window
|
||||
from test_network_path import FakeWNet
|
||||
from cbb_refresh_verify.network_path import LibraryPathLease
|
||||
window = load_window(qtbot, tmp_path, monkeypatch)
|
||||
backend = FakeWNet()
|
||||
old = LibraryPathLease.acquire(r'\\server\share\A\library.lmc', backend=backend)
|
||||
current = LibraryPathLease.acquire(r'\\server\share\B\library.lmc', backend=backend)
|
||||
window.library_lease = current
|
||||
job = tmp_path/'request.json'
|
||||
job.with_name('result.json').write_text(json.dumps({'status': 'CLOSED', 'safe_to_continue': True,
|
||||
'library': str(old.mentor_path), 'block': 'Beta'}))
|
||||
window._native_backup_session = SimpleNamespace(library=old.mentor_path, block='Beta', job=job,
|
||||
path_lease=old, process=SimpleNamespace(poll=lambda: 0))
|
||||
window._poll_native_backup(); qtbot.waitUntil(lambda: not window.busy)
|
||||
assert window._native_backup_session is None
|
||||
assert backend.cancelled == [] and current.created
|
||||
window.close()
|
||||
qtbot.waitUntil(lambda: backend.cancelled == [current.drive])
|
||||
|
||||
|
||||
def test_native_mapping_ownership_transfers_to_new_library_on_same_share():
|
||||
from test_network_path import FakeWNet
|
||||
from cbb_refresh_verify.network_path import LibraryPathLease
|
||||
backend = FakeWNet()
|
||||
old = LibraryPathLease.acquire(r'\\server\share\A\library.lmc', backend=backend)
|
||||
current = LibraryPathLease.acquire(r'\\server\share\B\library.lmc', backend=backend)
|
||||
assert old.created and not current.created
|
||||
old.transfer_ownership(current)
|
||||
old.close()
|
||||
assert backend.cancelled == [] and current.created
|
||||
current.close()
|
||||
assert backend.cancelled == [current.drive]
|
||||
|
||||
|
||||
def test_native_process_job_waits_for_its_children_without_killing_them(tmp_path):
|
||||
import sys, time
|
||||
from cbb_refresh_verify.native_backup import _NativeBackupProcess
|
||||
pythonw = Path(sys.executable).with_name('pythonw.exe')
|
||||
child = tmp_path/'child.pid'
|
||||
script = tmp_path/'parent.py'
|
||||
script.write_text('import subprocess,sys,time\nfrom pathlib import Path\n'
|
||||
'p=subprocess.Popen([sys.executable,"-c","import time;time.sleep(1)"])\n'
|
||||
f'Path({str(child)!r}).write_text(str(p.pid))\n'
|
||||
'time.sleep(.2)\n', encoding='utf-8')
|
||||
native = _NativeBackupProcess([str(pythonw), str(script)], tmp_path)
|
||||
try:
|
||||
deadline = time.monotonic()+10
|
||||
seen_child = False
|
||||
seen_parent_exit_before_child = False
|
||||
while time.monotonic() < deadline:
|
||||
pids = native.session_pids()
|
||||
assert isinstance(pids, (list, tuple))
|
||||
if child.exists():
|
||||
pid = int(child.read_text())
|
||||
seen_child = seen_child or pid in pids
|
||||
seen_parent_exit_before_child = seen_parent_exit_before_child or native.poll() == 0 and pid in pids
|
||||
if native.poll() == 0 and not pids:
|
||||
break
|
||||
time.sleep(.05)
|
||||
assert seen_child and seen_parent_exit_before_child
|
||||
assert not native.session_pids()
|
||||
finally:
|
||||
native.close()
|
||||
Loaded 100 of 133 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user