Compare commits
7
Commits
v1.2.2
...
08ddf4cd6a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08ddf4cd6a | ||
|
|
37a458e3c3 | ||
|
|
94a873f929 | ||
|
|
8b5509d64f | ||
|
|
9316cf4fc4 | ||
|
|
b00ca7dac9 | ||
|
|
705c15eb93 |
No files matched your search
@@ -24,7 +24,7 @@ jobs:
|
||||
- name: Install runner Python and test dependencies
|
||||
run: |
|
||||
apt-get update
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3 python3-pytest
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3 python3-pytest python3-openpyxl
|
||||
python3 --version
|
||||
- name: Compile application and build helpers
|
||||
run: python3 -m compileall -q src packaging tools
|
||||
@@ -39,6 +39,8 @@ jobs:
|
||||
tests/packaging/test_dsh_runtime_staging.py
|
||||
tests/packaging/test_release_configuration.py
|
||||
tests/packaging/test_self_extracting_release.py
|
||||
tests/trial_task_center/test_bom_template_contract.py
|
||||
tests/trial_task_center/test_bom_import_review.py
|
||||
--junitxml=build/ci-results.xml --tb=short
|
||||
- name: Windows release scope
|
||||
run: |
|
||||
|
||||
@@ -24,10 +24,12 @@ DSH助手围绕真实任务中心工作,支持:
|
||||
- 发生异常时自动介入,收集脱敏证据链、保留诊断记录,并在需要时提交受限 Git 工单。
|
||||
- 在右侧任务监管中查看运行、影响、排期、经验、工单和用量;审计损坏、证据缺失和未知计数会明确显示。
|
||||
|
||||
AI 不能直接写入 CAD、运行 Shell、访问任意文件或绕过确认门禁。自动异常诊断为只读;不确定执行不会自动重放,恢复必须重新核对冻结计划、检查点和业务证据。未配置模型时仍可使用任务监管和本地恢复功能,但不会产生真实模型回复。
|
||||
AI 不能直接写入 CAD、运行 Shell 或绕过确认门禁。普通对话可按用户要求读取本地文件;自动异常诊断为只读事件快照。不确定执行不会自动重放,恢复必须重新核对冻结计划、检查点和业务证据。未配置模型时仍可使用任务监管和本地恢复功能,但不会产生真实模型回复。
|
||||
|
||||
首次接入打开 **工具设置 → 模型 → 添加模型提供商 → 自定义模型 API**,填写 API 地址、协议和密钥,点击 **获取可用模型** 并选择需要的模型,也可手动填写模型 ID。保存后在输入框下方选择模型。设置保存在任务中心数据目录的专用 Harness profile,重启会保留;主工具预配置的密钥继续由 Windows DPAPI 保护,模型设置页新增的密钥使用 Harness 本地凭据存储。开发环境也可通过 `DSH_API_BASE`、`DSH_MODEL`、`DSH_API_KEY` 临时提供配置。不要把数据目录、API key 或 Git 凭据提交到 Git。
|
||||
|
||||
对话输入框的 **+ → 文件** 可上传文本、ZIP 等文件;也可通过官方工作区入口选择本地文件夹,或直接提供路径让助手读取、搜索。ZIP 会解压到新的文件夹,返回真实路径后继续分析内容,不覆盖已有文件。启动展示直接使用 Harness 官方加载动画。
|
||||
|
||||
详细的能力边界、异常介入、证据链、排期和工单说明见 [DSH助手使用与监管说明](docs/user/dsh-ai-supervision.md)。源码环境可用下面的命令检查运行时门禁:
|
||||
|
||||
```powershell
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# Item Value and Migrated BOM Import
|
||||
|
||||
Date: 2026-11-05
|
||||
|
||||
## Approved scope
|
||||
|
||||
- Task-level Value policy remains selectable. `inherit` follows the task setting (`part_label` when enabled, otherwise `preserve`).
|
||||
- Every target row can explicitly choose `preserve`, `part_label`, `part_number`, or `custom`; explicit choices override the task default.
|
||||
- BOM import accepts multiple Excel files. Existing exact project paths take precedence; stale paths can resolve through an exact, unique project name. Ambiguous names block import.
|
||||
- Preserve source workbook, original Excel row, original project path, current path, and match reason.
|
||||
- Verify each function independently using generated fixtures.
|
||||
|
||||
## Implementation checklist
|
||||
|
||||
- [x] Add and validate persisted item Value mode; preserve legacy custom Value inputs.
|
||||
- [x] Resolve inherited modes using the task setting during execution, context snapshots, BOM validation, and new-round checks.
|
||||
- [x] Group rules by concrete item mode and freeze each mode/custom text into execution commands.
|
||||
- [x] Preserve the existing recovery comparison of queue policy, custom text, and target identities.
|
||||
- [x] Add optional BOM Value mode and plan export/import support; accept legacy 11-column BOM and 13-column target plans.
|
||||
- [x] Parse all selected workbooks before inspection and preserve original source coordinates.
|
||||
- [x] Route exact paths and uniquely matching migrated names; isolate projects with different names even when erroneous old paths are identical.
|
||||
- [x] Retain cross-file duplicate blocking and atomic plan saving.
|
||||
- [x] Add item dropdown selection and synchronize legacy custom checkboxes; commit normally and cancel with Esc.
|
||||
- [x] Switch the BOM picker and its UI tests to multi-selection.
|
||||
- [x] Reuse supervised inspection, cancellation, phase reporting, and cleanup failure handling.
|
||||
- [x] Keep editable mapping-rule IDs separate from compiled execution rules.
|
||||
- [x] Preserve explicit item policies when batch custom defaults change; disabling a matching seeded custom default restores inheritance.
|
||||
- [x] Add an integration fixture covering two workbooks, migration routing, four Value modes, save, database reopen, frozen commands, and recovery readback/mismatch rejection.
|
||||
|
||||
## Verification
|
||||
|
||||
See `docs/testing/2026-11-05-item-value-bom-import.md` for generated samples, independent acceptance cases, commands, results, and the real-CAD boundary.
|
||||
|
||||
Existing unrelated DSH work in this checkout is retained. No commit, remote branch deletion, merge, or deployment is part of this implementation.
|
||||
@@ -0,0 +1,40 @@
|
||||
# DSH 原生文件能力与加载页
|
||||
|
||||
基线:同步项目 `main@1340f6c`,包含 V1.2.2、操作历史与 UI 更新;保留已合并的 PR #17 修复。
|
||||
|
||||
## 变更
|
||||
|
||||
- 恢复官方附件栏、文件引用、目录选择、文件侧栏及其后端;普通对话开放原生 read/read_image、glob/grep、working_directory。启动检查从固定七项任务工具改为七项任务工具加六项文件能力。自动异常会话仍只接受其事件快照。
|
||||
- 增加 extract_zip,复用运行时已有的 fflate 并显式固定依赖;向新目录解压后返回真实路径、条目与截断标记。解压前检查目录越界、数量、大小、重复条目和 CRC。路径、校验或写入失败不保留本次半成品,不覆盖已有文件。支持普通 ZIP;不支持加密、多卷、ZIP64。
|
||||
- 启动直接打开固定官方前端的原版 index.html,通过其公开 __DSH_BOOT_READY__ 等待信号展示原版 HARNESS 动画。运行时就绪后进入官方 HTTP 页面并关闭本地资源访问;删除额外首页标语。没有改写上游 HTML、SVG、CSS 或动画。
|
||||
- 实测一次冷启动需 139.59 秒,原 120 秒计时器提前终止正常启动;等待上限调整到 300 秒。最终验收启动等待 30.49 秒。
|
||||
- 模型调用的文件操作追加既有工具审计,记录名称、回执和错误状态,不记录文件内容。
|
||||
|
||||
## 验证
|
||||
|
||||
- CLI:`node tools/verify_dsh_files.mjs`。先在缺少 read 能力的断言处观察失败;修复后验证中文/空格目录、读取、搜索、切换目录、ZIP 文本和二进制、重复解压不覆盖、路径越界、损坏包及 CRC 拒绝。独立审查也验证了单字节损坏的包在创建输出前被拒绝。
|
||||
- 真正上传与模型操作:`python -X utf8 tools/accept_dsh_desktop.py --output build/dsh-files-live-verified --port 9298 --provider-config <本机配置目录> --file-check --live-model`。
|
||||
- 实际点击官方 + → 文件,在真实 Qt 文件选择框输入自有文本和 ZIP 路径并点击打开;在 Harness 存储中核对两份文件字节一致。自然语言提示未指定工具名,真实测试 API 模型调用 read → extract_zip → read,三次均成功,并准确返回 `DSH_UPLOAD_CONTENT_中文`、`DSH_ZIP_UPLOAD_CONTENT_中文` 和真实解压目录。没有 CAD 操作或用户原始文件修改。
|
||||
- `build/dsh-files-live-verified/desktop-report.json`:status=passed、storedBytesMatched=true、modelToolSucceeded=true、cleanShutdown=true,uncaughtErrors=false、unexpectedUiErrors=false。官方加载、鲸鱼 SVG 动画、模型选择器、原生取消确认及导航后草稿保留通过。
|
||||
- 原版加载页实际截图:`build/dsh-files-live-verified/official-loading.png`,使用 Chromium Page.captureScreenshot 验证 HARNESS 与官方 spinner。附件截图 `file-uploaded.png`,真实回复截图 `dsh-native-final.png`。
|
||||
- Python 回归:`python -m pytest -q tests/dsh tests/packaging --tb=short` 为 217 passed、1 skipped(当前环境未指定官方 7-Zip 发布工具);`python -m pytest -q tests/ui --tb=short` 为 193 passed。合计 410 passed、1 skipped。
|
||||
- 最终 `verify_dsh_files.mjs` 还实际创建自动诊断会话,确认 read 与 extract_zip 在执行前被拒绝,避免继承普通对话的文件访问;该隔离用例不配置模型,不作自动诊断成功验证。`verify_dsh_profile_settings.mjs` 的原生配置保留、迁移与原字节备份通过。`verify_dsh_runtime.mjs` 确认 13 项预期工具、非预期工具拒绝、会话/上下文接口认证及任务中心确认门禁。
|
||||
- 最终独立代码审查未发现新增阻断问题;Python 编译、变更 JS 语法及 diff 检查通过。
|
||||
|
||||
## 验收脚本兼容
|
||||
|
||||
官方文件菜单实际名称为“文件 file”,修正选择器;CDP 发现页在总时限内重试短暂的连接超时。pytest-qt 会在 fixture 结束前删除注册窗口,UI 夹具现在检查 isValid,避免再次关闭已删除窗口;原失败单项复验通过。
|
||||
|
||||
既有协议单测的轻量 UI stub 同步增加 hide、官方加载入口和 WebEngine settings;保留原 UTF-8 分块、重启及坏行恢复断言,并检查真实 HTTP 握手后关闭本地文件访问。
|
||||
|
||||
本次未构建或分发新 EXE,不把源码与本机验收等同于发布包验收。
|
||||
|
||||
## 合并前评论 #197:文件名完整性与解压线程
|
||||
|
||||
原 fflate 异步 unzip 使用普通对象接收文件,根目录 `__proto__` 会被静默漏掉;同时每个较大的条目都可能启动一个 Worker,文件总大小上限无法限制线程数量。新增真实 Harness 回归先在“特殊文件应明确拒绝”的断言处观察失败。
|
||||
|
||||
解压现由专用 Node Worker 执行,全进程最多同时处理两个压缩包,其余排队。每包先做不解压的整包元数据预检,明确拒绝无法可靠表示的根目录 `__proto__`;再在该 Worker 中解压,核对完整名称集合、长度和 CRC 后才传回主进程写入新目录。没有在主服务线程同步解压,也不再按 ZIP 条目创建线程。活动调用取消时终止 Worker,等待退出才结算并释放槽位;排队调用取消后直接移出队列。
|
||||
|
||||
`node tools/verify_dsh_zip_review.mjs`:合法与损坏的特殊文件名均被明确拒绝,无输出目录;4 个压缩包并行、每包 20 个 600 KiB 文件,全部文件完整,恰好创建 4 个 Worker,峰值为 2;活动取消后 Worker 全部退出且不产生半成品。独立审查还验证了 5 个请求中取消一个排队请求,4 个成功、峰值 2、全部线程退出。Worker 文件会随 task-plugin 目录递归打包。
|
||||
|
||||
最终 `python -m pytest -q tests/v1_1 tests/ui tests/dsh tests/packaging --tb=short`:1463 passed、1 skipped(未指定官方 7-Zip 发布工具)、1 条 pywinauto COM 模式警告。原有真实 Harness 文件/目录/ZIP/CRC/自动会话隔离回归再次通过;最终独立审查无新增阻断发现。
|
||||
@@ -0,0 +1,70 @@
|
||||
# 单项 Value 与多文件 BOM 导入验收
|
||||
|
||||
日期:2026-11-05。范围:用户确认的单项 Value 策略、迁移工程 BOM 导入、多选 Excel,以及各功能的独立模拟验证。
|
||||
|
||||
## 模拟样本
|
||||
|
||||
测试每次在 pytest 临时目录生成 Excel、SQLite 数据库和工程占位文件,不改真实工程。
|
||||
|
||||
- 两个工程:`BoardA/BoardA.prj`、`BoardB/BoardB.prj`,各包含 R1、R2,旧料号为 OLD,原 Value 为 10nF。
|
||||
- 两份 Excel:BoardA.xlsx 使用继承任务默认与 Preserve;BoardB.xlsx 使用 Custom=47k 与 Part Number。
|
||||
- Excel 中的旧工程路径为 `Z:/old/...`,当前工程路径位于临时目录;按工程名称精确路由。
|
||||
- 负向样本:同名不同目录、两份文件重复 R1、公式、Excel 错误单元格、旧计划的文本 FALSE、自定义 Value 为空、相同错误旧路径但不同工程名称。
|
||||
- 实际调用 BOM 解析、匹配、计划保存、数据库重开、队列生成和恢复解析;CAD 运行环境用测试对象替代。
|
||||
|
||||
## 独立验收项
|
||||
|
||||
以下各项均已通过对应的独立模拟用例;完整流程另有两工作簿的集成闭环验证。
|
||||
|
||||
| 功能 | 验证内容与预期 |
|
||||
| --- | --- |
|
||||
| 单项显式策略 | Preserve、Part Label、Part Number、Custom 分别进入对应规则和执行命令 |
|
||||
| 默认值与覆盖 | 继承项随任务默认使用 Part Label;显式 Preserve 保持原 Value |
|
||||
| 批量规则传播 | 修改批量自定义默认值时保留单项显式 Preserve、Part Label、Part Number;关闭匹配的旧自定义默认值后恢复继承 |
|
||||
| 自定义一致性 | 旧自定义输入保留;取消勾选回到继承;空自定义阻止生成执行规则 |
|
||||
| 数据库存储 | 旧表增加字段后可读取;保存并重新打开数据库后策略仍正确 |
|
||||
| 恢复绑定 | 读取冻结的单项策略和自定义内容;改变策略或自定义内容后旧队列不再匹配 |
|
||||
| 多选 Excel | 两个工作簿一次预览;跨工程编辑后一次保存;未确认前不修改计划 |
|
||||
| 迁移路径 | 路径失效且工程名称唯一一致时使用当前路径;不同工程名称不会因相同错误旧路径而混淆 |
|
||||
| 名称歧义 | 相同名称对应多个工程时停止自动路由 |
|
||||
| 重复阻塞 | 两份 Excel 声明同一位号时整批预览失败,已有计划保持不变 |
|
||||
| 来源追踪 | 每条结果附原文件与行号;source-map.json 保留原路径、当前路径及匹配原因 |
|
||||
| 旧格式兼容 | 11 列 BOM 的自定义内容继续生效;13 列计划的文本 FALSE 正确解析 |
|
||||
| 输入校验 | 第二份文件含公式或错误料号时,在进入工程检查前阻止本次导入 |
|
||||
| 编辑操作 | 下拉选择提交后生效;Esc 取消;Value 列提示可读取 |
|
||||
| 进度与失败处理 | 多文件导入沿用监督进程、阶段通知、取消和资源清理保护;失败报告仍保留来源 |
|
||||
| 原有规则编辑 | 单项覆盖不替换规则表中的可编辑规则 ID |
|
||||
|
||||
专项用例位于 `tests/trial_task_center/test_item_value_and_bom_sources.py`;界面用例位于 `tests/ui/test_value_options_layout.py` 及 `tests/v1_1/test_bom_batch.py`、`test_bom_import_report.py`、`test_ui_workspace.py`。
|
||||
|
||||
## 最终执行结果
|
||||
|
||||
| 检查 | 实际结果 |
|
||||
| --- | --- |
|
||||
| Trial Task Center 全目录 | 355 passed,1 项 pywinauto COM 线程模式警告 |
|
||||
| 新增专项 24 项与原有批量规则 9 项 | 33 passed |
|
||||
| BOM、计划、目标执行、新回合及相关 UI 回归 | 165 passed |
|
||||
| 打包契约检查 | 3 passed |
|
||||
| Python compileall、git diff --check | 退出码 0;Git 提示已有文件的换行转换警告 |
|
||||
| DSH 运行时契约检查 | status=passed;不代表真实 CAD 操作 |
|
||||
|
||||
专项与批量规则的重跑命令:
|
||||
|
||||
```powershell
|
||||
python -m pytest -q tests/trial_task_center/test_item_value_and_bom_sources.py tests/v1_1/test_batch_rules.py
|
||||
python -m pytest -q tests/trial_task_center
|
||||
python -m pytest -q tests/packaging/test_dx_part_replace_build.py
|
||||
```
|
||||
|
||||
165 项回归的重跑命令(Qt 使用 offscreen):
|
||||
|
||||
```powershell
|
||||
$env:QT_QPA_PLATFORM='offscreen'
|
||||
python -m pytest -q tests/v1_1/test_bom_import_report.py tests/v1_1/test_bom_batch.py tests/v1_1/test_ui_workspace.py tests/v1_1/test_bom_plan.py tests/v1_1/test_bom_part_number_types.py tests/v1_1/test_spreadsheet_plan.py tests/v1_1/test_target_cell_rules.py tests/v1_1/test_target_execution.py tests/v1_1/test_backend_targets.py tests/v1_1/test_new_round.py tests/ui/test_value_options_layout.py tests/ui/test_ui_polish.py tests/ui/test_trial_task_center_workflow.py
|
||||
```
|
||||
|
||||
独立只读复核复现了两项批量默认值传播问题:覆盖单项显式策略,以及关闭旧自定义默认值失败。4 项新增回归先复现失败,再验证修复;复核已确认两项问题解决。专项也补验了旧 BOM 自定义内容、文本 FALSE、错误单元格、Esc 取消、可编辑规则 ID 和来源行号偏移。
|
||||
|
||||
## 验证边界
|
||||
|
||||
本记录的模拟验收覆盖输入、规则、持久化、冻结队列、恢复读取和界面交互。后续已按用户要求补充真实 DxDesigner 替换、保存重开、核验恢复及独立磁盘读取,见 [真实替换实测](2026-11-05-item-value-bom-native.md);该实测未执行 PCB。全仓库测试与完整安装包构建没有作为本次通过结论。
|
||||
@@ -0,0 +1,45 @@
|
||||
# 单项 Value 与迁移 BOM 的真实替换实测
|
||||
|
||||
日期:2026-11-05。按用户要求只验证原理图替换、保存和回读,未执行 PCB、Packager 或前向标注。
|
||||
|
||||
## 结果
|
||||
|
||||
两个真实工程的新副本、两份旧路径 Excel、四个位号已完成实际 DxDesigner Replace Part。四项均有保存及关闭重开回读记录;最终另启全新 CAD 进程独立读取两份磁盘工程,取得以下结果。
|
||||
|
||||
全部由 `4100-CA0000-T7` 替换为 `4100-CA0000-T0`,目标 Cell 为 `RESC1608N_0603`。
|
||||
|
||||
| 工程 | 位号 | 单项策略 | 独立读回的 Value | Part Label |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| Audio_D2128 | RV05 | 继承任务默认 Part Label | 0R | 0R |
|
||||
| Audio_D2128 | RV11 | 显式 Preserve,覆盖任务默认 | 空,无 Value 属性 | 0R |
|
||||
| Audio_T605A | RV05 | 显式 Part Number | 4100-CA0000-T0 | 0R |
|
||||
| Audio_T605A | RV11 | 显式 Custom | 47k | 0R |
|
||||
|
||||
Preserve 沿用既有语义:保留目标器件原生 Value 状态,不复制旧器件的 Value。本库目标没有 Value 属性,因此本例读回为空。
|
||||
|
||||
两份 Excel 均使用 `Z:/migrated-old-location/...`,按唯一同名工程成功路由;4 行导入通过,来源文件和原行号均保留。每个目标 UID 未变,并独立取得两个引脚。
|
||||
|
||||
## 实测发现及处理
|
||||
|
||||
本轮并非一次无报错运行。首次执行在第一个工程两项保存回读成功后,资源状态文件原子替换遇到 WinError 5,队列安全停止。定位到 `batch_resources.write_record` 没有向已有的共享占用识别逻辑传入目标路径;补齐该参数,真实 Windows 读句柄占用复现用例由失败转为通过,永久权限错误仍不重试。
|
||||
|
||||
恢复原队列时只执行尚未开始的第二个工程。第二个工程两项也保存回读成功,但最后写 `project-session-result.tsv` 时,ADODB.Stream 遇到读占用,触发保护及资源清理核验。为这一特定回执文件增加最多 20 次、间隔 50ms 的文件写入重试;不重试其他 CAD 操作。真实 cscript + Windows 文件句柄用例验证短暂占用成功、持续占用失败。
|
||||
|
||||
之后通过已有资源清理及核验恢复接口核验磁盘结果,取得 `restored_ok / recovery_saved_verified`,两任务均完成。恢复记录的 `native_replacement_called=false`;两份任务原生日志合计恰好 4 次替换,没有重做已完成步骤。最后由全新 CAD 进程独立读取上表结果。
|
||||
|
||||
修复后没有从头再替换四个位号;两个文件占用修复以对应的真实文件操作复现验证,原生业务结果通过核验恢复和独立磁盘读取收尾。
|
||||
|
||||
## 证据
|
||||
|
||||
证据根:`build/item-value-native-20261105-103328/`。
|
||||
|
||||
- 最终报告:`reconciliation-report-105332.json`,`verified=true`。
|
||||
- 首次及续行失败记录:`report.json`、`continuation-report.json`,保留原始故障。
|
||||
- 独立磁盘读取:`Audio_D2128-saved.json`、`Audio_T605A-saved.json`,每工程 57 个器件。
|
||||
- 恢复证据:`data/tasks/53a0e391-6fc2-4685-90bf-66e44000e2fa/quick-run-history/recovery-f4264066ce8c4cf8b4b36d91d790205f/recovery.json`。
|
||||
- 原工程目录哈希、原中央库 LMC 哈希均未变;测试副本 PCB 文件哈希未变,`pcb_execution=false`。
|
||||
- 自有 CAD 均退出,四个私有桌面均释放;未抢占用户输入桌面。
|
||||
|
||||
实测工具:`tools/probe_item_value_bom_live.py`,复用现有生产服务、原生脚本、私有 CAD 会话与独立读取器,没有使用模拟 CAD。
|
||||
|
||||
本轮仅额外运行与两个现场文件占用故障直接相关的 4 个小用例,没有重复运行整套回归。
|
||||
@@ -0,0 +1,32 @@
|
||||
# PR #21 审查修复与独立验证
|
||||
|
||||
审查来源:[评论 217](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/pulls/21#issuecomment-217)。本轮修复基于 `8b5509d`,覆盖 1 项 P2、5 项 P3,以及 CI 覆盖建议。保留 PR,未执行合并。
|
||||
|
||||
## 修复与验收对应
|
||||
|
||||
| 审查项 | 最终行为 | 独立验证 |
|
||||
| --- | --- | --- |
|
||||
| 任务显示名与工程文件名不一致 | 精确路径优先;接受唯一的任务显示名或工程文件名;显示名冲突仍阻断。单工程路径留空时保留旧的唯一工程绑定行为,显式错误路径不能任意兜底 | 改名“音频板”后导出、修改为旧路径、真实 XLSX 重新导入;显示名重复与精确路径优先;单工程任意显示名空路径 |
|
||||
| 跨文件重复提示行号矛盾 | 报告提供 `source_file`、`source_row`;重复原因同时列出全部原文件与原行;界面显示原始行号 | 第一份第 2 行与第二份第 5 行重复,报告两行均失败且无预览;UI 格式验证显示第二份第 5 行 |
|
||||
| 来源证据无引用、无导出、无限累积 | 来源映射存于锚点任务的 `quick-run-history/bom-import-<uuid>/source-map.json`;报告和任务事件引用;诊断包只纳入 JSON 元数据并沿用脱敏;复用现有历史数量、大小、时间限制;临时规范化 Excel 在 `finally` 删除 | 真实服务导入后检查报告、事件、诊断 ZIP 与 Excel 删除;两次导入后限制保留 1 份,旧映射删除、新映射保留 |
|
||||
| 匹配原因失真与非原子写入 | 区分精确路径、任务显示名、工程名称、迁移名称、唯一工程兜底;相对路径在规范化前拒绝;映射使用现有原子 `write_record` 和共享占用处理 | 各匹配原因断言;相对路径失败;模拟替换 JSON 失败后原内容完整、无遗留临时 JSON |
|
||||
| 用户模板缺少 Value 来源列 | 有任务/无任务模板均导出第 12 列,提供全部五种策略下拉、样式与批注;底层旧写入接口默认保持 11 列 | 真实 openpyxl 检查两个模板、五种策略读入和编译、旧 11 列自定义值推断;空任务 UI 下载 |
|
||||
| 两个操作之间释放占用,读源无进度 | `preview_bom_sources` 在任何本地源读取前进入监督进程;本地读取、路由、检查全程持有一个快速执行租约;读取前通知 READING,并逐文件报告名称与序号 | 在读取与检查内部尝试竞争租约,均被拒;监督入口禁止本地读源;逐文件进度;真实监督子进程端到端返回来源元数据与预览 |
|
||||
| CI 未覆盖功能 | Linux 工作流安装 openpyxl,加入模板/策略契约与本轮来源导入回归用例,无 Qt/CAD 依赖 | 本机定向执行;远端 CI 状态另由 PR 检查项记录,不用 Windows 本机结果冒充 Linux 结果 |
|
||||
|
||||
同时修正监督清理失败的一个边界:业务失败报告由 `primary_error` 继承时,也必须清除其可执行预览。覆盖业务成功/失败 × 清理待处理/异常退出四种组合;报告保留,预览均为空。
|
||||
|
||||
## 本轮验证记录
|
||||
|
||||
本轮仅验证新增修复及直接相关功能,不重跑整个仓库,也不重复真实 CAD/PCB。原始真实替换验收仍见 `2026-11-05-item-value-bom-native.md`。
|
||||
|
||||
- 修复前先运行新复现文件:`test_bom_import_review.py` 原有 9 项全部失败;修复后原有 9 项全部通过,并补齐连续占用、名称冲突和匹配原因用例。当前该文件共 **14 个用例**。
|
||||
- `python -m pytest -q tests/trial_task_center/test_bom_import_review.py tests/trial_task_center/test_item_value_and_bom_sources.py tests/v1_1/test_bom_sources_supervision.py --tb=short`:**44 passed**,涵盖 14 项审查回归、24 项既有策略/多来源验证、7 项监督验证(测试集合存在重叠,不累加)。
|
||||
- 模板、旧 BOM、料号类型、CBB 和两个模板导出入口的定向命令:**34 passed**,其中新增模板契约 8 项。
|
||||
- 监督协议、结果处理和新监督入口:**22 passed**。与上面的 44 项存在 6 项重叠,不累加。
|
||||
- 报告、诊断包和历史保留相邻用例首轮:**37 passed、1 failed、1 error**。第一项 UI 用例仍拦截旧的检查接口,首轮异步等待超时,未结束的任务影响了后一项的占用检查;单独定位还发现按钮尚未启用就点击的问题。两项现已通过真实的新入口、使用无 CAD 的运行时替身,并明确等待按钮可用。
|
||||
- 仅重跑上述两项 UI 及新增原始来源格式用例:**3 passed**(`--basetemp=build/pr21-ui-review-confirm`);已通过的相邻用例没有重复跑。
|
||||
- 最后新增真实监督进程完整导入测试:`test_real_supervised_sources_roundtrip_origin_metadata_and_preview`:**1 passed**(`--basetemp=build/pr21-supervised-import-confirm`)。父进程调用真实服务,子进程读取两份 XLSX 和预先保存的库存,返回完整预览、原文件/行号和 `Path` 类型的映射路径;未打开 CAD,未保存新计划,临时 Excel 已删除。
|
||||
- 独立只读代码复核覆盖 service/workspace/report/supervisor/diagnostics/retention/CI,未发现阻塞问题。
|
||||
|
||||
各条记录属于不同范围,存在重叠,不能相加作为独立总数。未构建安装包,未重复真实 CAD 替换,未执行 PCB。
|
||||
@@ -16,6 +16,16 @@
|
||||
|
||||
DSH 使用独立原生子窗口承载 WebEngine,切回任务中心后保留原有 Acrylic 玻璃材质;“减少透明度”与系统高对比度等回退规则继续生效。
|
||||
|
||||
启动时直接展示官方前端的 HARNESS 加载动画,不再绘制额外的 Qt 加载页面;运行时就绪后进入官方对话界面。
|
||||
|
||||
### 文件、文件夹与 ZIP
|
||||
|
||||
点击输入框 **+ → 文件**,在文件选择框中选取文件。上传卡片显示进度,失败可重试;上传成功后可连同问题发送。文本由助手实际读取,图片还需要所选模型支持图片输入。
|
||||
|
||||
侧栏的官方工作区入口可选择本机文件夹;也可以在对话中直接提供绝对路径,或用 `@` 引用文件。助手可以读取 UTF-8 文本、查看目录中的文件、搜索内容,并切换会话的当前目录。选择文件夹不会自动执行 CAD 或修改工程。
|
||||
|
||||
例如:“读取这个文件夹里的说明,搜索失败原因”;或上传 ZIP 后说:“解压附件,读取其中说明文件并告诉我内容”。ZIP 解压到当前目录下新建的 `dsh-extracted-*` 文件夹,也可指定一个已有输出父目录;返回绝对目录与文件清单,重复解压会创建新目录。解压前校验路径、大小和 CRC,失败不会留下可误认为成功的输出。本轮支持普通 ZIP,不支持加密、多卷及 ZIP64;压缩文件上限 64 MiB、解压总量 256 MiB、最多 10000 项。上传其他格式不等于模型已经能够解析其内容。
|
||||
|
||||
模型由本机保存的主工具配置加载;也支持启动进程的 `DSH_API_BASE`、`DSH_MODEL`、`DSH_API_KEY`。未配置模型时可查看任务上下文和操作面板,不能获得真实模型回复。
|
||||
|
||||
本机已接入测试端点 `https://wecom-test.skyworth-cloud.cn/v1/`,注册 `skyworth-deepseek-v4-flash-0731` 和 `skyworth-deepseek-v4.1-flash`,默认使用后者。本机配置位于任务中心数据目录的 `dsh/provider.json`,密钥以 Windows DPAPI 加密保存在该配置引用的 `provider-key-<版本号>.dpapi` 中,启动时自动加载;不写入仓库或 Windows 用户环境变量。旧版无版本号的 JSON 与固定 `provider-key.dpapi` 组合仍可读取,下次保存时使用新格式。
|
||||
@@ -56,7 +66,7 @@ DSH 使用独立原生子窗口承载 WebEngine,切回任务中心后保留原
|
||||
|
||||
用户在原执行轮次请求安全暂停或强制结束后,该停止意图会随轮次传递到自动诊断;即使最后一个失败项已收尾、队列结果显示“已结束”,也不会自动准备或执行重试。诊断和证据上报仍可继续。只有用户重新确认执行新轮次,才会创建新的执行控制对象;原轮次的停止记录不会被清除。
|
||||
|
||||
- 普通对话开放快照、创建、任务列表及详情、预检、任务管理、受控执行、综合监管七项能力;综合监管包含影响、运行恢复、排期、经验与工单查询。自动异常诊断会话仍只允许事件快照,其他调用在执行前被拒绝。Shell、通用文件访问、插件安装、子智能体及模型直接 CAD 写入工具均未注册;额外工具会被 guard 阻断。
|
||||
- 普通对话除七项任务能力外,开放本地文件读取、图片读取、文件发现与内容搜索、切换目录和 ZIP 解压;附件、文件引用、文件夹选择使用官方界面。自动异常诊断会话仍只允许事件快照。通用 Shell、插件安装、子智能体及模型直接 CAD 写入工具未注册;任务修改和工程执行继续走任务中心原有确认。
|
||||
- **刷新任务上下文**重新发布当前任务快照。
|
||||
- 手动点击 **准备失败项重试**须在任务中心确认,并通过现有重试按钮的状态检查;该按钮只准备重试。自动介入使用上文限定的一次重试闭环。
|
||||
- **安全暂停**须确认且当前确有执行控制器,使用原队列的安全边界暂停。
|
||||
|
||||
Generated
+2
-1
@@ -5,7 +5,8 @@
|
||||
"packages": {
|
||||
"": {
|
||||
"dependencies": {
|
||||
"@deepseek-ai/dsh": "0.2.1-rc.5"
|
||||
"@deepseek-ai/dsh": "0.2.1-rc.5",
|
||||
"fflate": "0.8.3"
|
||||
},
|
||||
"name": "dxdesigner-dsh-runtime"
|
||||
},
|
||||
|
||||
@@ -4,5 +4,5 @@
|
||||
"type": "module",
|
||||
"scripts": {"doctor": "node start.mjs --doctor", "start": "node start.mjs"},
|
||||
"engines": {"node": ">=24.9.0"},
|
||||
"dependencies": {"@deepseek-ai/dsh": "0.2.1-rc.5"}
|
||||
"dependencies": {"@deepseek-ai/dsh": "0.2.1-rc.5", "fflate": "0.8.3"}
|
||||
}
|
||||
@@ -7,6 +7,9 @@ import {isDeepStrictEqual} from 'node:util';
|
||||
import path from 'node:path';
|
||||
import {createInterface} from 'node:readline';
|
||||
|
||||
export const TASK_TOOLS=['dx_task_snapshot','dx_task_create','dx_task_catalog','dx_task_preflight','dx_task_manage','dx_task_execute','dx_task_operations'];
|
||||
export const FILE_TOOLS=['read','read_image','glob','grep','working_directory','extract_zip'];
|
||||
|
||||
const root = path.dirname(fileURLToPath(import.meta.url));
|
||||
const require = createRequire(import.meta.url);
|
||||
export const VERSION = '0.2.1-rc.5';
|
||||
@@ -14,13 +17,12 @@ export const DISABLED = [
|
||||
'preset-standard','preset-cordis','preset-minimal','preset-ptc',
|
||||
'tool-plugin-manager','plugin-manager','ui-plugin-manager','ui-settings-plugins',
|
||||
'ui-settings-plugin-inventory','ui-agent-preset','cordis-host-runner','cordis-client-runner',
|
||||
'ui-cordis','ui-sidebar-terminal','terminal-controller','ui-sidebar-files',
|
||||
'workspace-files','ui-sidebar-documentpreview','ui-sidebar-browser','office-to-pdf',
|
||||
'open-in-app','ui-open-in-app','directory-picker','ui-directory-picker-native',
|
||||
'ui-directory-picker-browse','ui-attachment','file-reference-local',
|
||||
'ui-reference','ui-skill','ui-subagent','ui-schedule','ui-goal','ui-jobs',
|
||||
'ui-cordis','ui-sidebar-terminal','terminal-controller',
|
||||
'ui-sidebar-documentpreview','ui-sidebar-browser','office-to-pdf',
|
||||
'open-in-app','ui-open-in-app',
|
||||
'ui-skill','ui-subagent','ui-schedule','ui-goal','ui-jobs',
|
||||
'ui-settings-shell','ui-settings-subagent','ui-settings-web-search','ui-settings-agent-loop',
|
||||
'ui-permission','ui-plan','plan-mode','agent-instructions','tool-working-directory',
|
||||
'ui-permission','ui-plan','plan-mode','agent-instructions',
|
||||
'tool-bash','tool-pwsh','tool-fs','tool-fs-search','tool-web','tool-skill','tool-jobs',
|
||||
'tool-schedule','tool-goal','tool-todo','tool-present','tool-workflow',
|
||||
'tool-subagent','tool-subagent-control','tool-subagent-fork',
|
||||
@@ -38,6 +40,8 @@ export function overlay() {
|
||||
rows.push({id:'agent-preset-registry',config:{default:'dx-task'}});
|
||||
rows.push({id:'ui-settings-general',config:{welcomeNoticeVersion:'2026-09-28.1'}});
|
||||
rows.push({id:'ui-settings-models',config:{credentialOnboarding:false}});
|
||||
rows.push({id:'tool-fs',disabled:false,config:{read:true,readImage:true,write:false,edit:false}});
|
||||
rows.push({id:'tool-fs-search',disabled:false,config:{sampleOverCapGlobResults:false}});
|
||||
rows.push({id:'web-runtime',config:{openBrowser:false,printUrl:false,surfaceContext:false}});
|
||||
rows.push({id:'sandbox-policy',config:{mode:'read-only',workspaceRoot:process.env.DX_DSH_WORKSPACE}});
|
||||
rows.push({id:'permission',config:{presets:{'read-only':{sandbox:'read-only',approval:'ask'}}}});
|
||||
@@ -206,7 +210,7 @@ export async function bootRuntime({announce=true} = {}) {
|
||||
await readFile(path.join(root,'task-plugin/client.js'));
|
||||
if (process.argv.includes('--doctor')) {
|
||||
console.log(JSON.stringify({status:'ready',version:VERSION,frontend:'official',
|
||||
tools:['dx_task_snapshot','dx_task_create','dx_task_catalog','dx_task_preflight','dx_task_manage','dx_task_execute','dx_task_operations'],genericTools:false,node:process.version}));
|
||||
tools:[...TASK_TOOLS,...FILE_TOOLS],fileTools:true,genericShell:false,node:process.version}));
|
||||
return;
|
||||
}
|
||||
if (!process.env.DX_DSH_HOME || !process.env.DX_DSH_CONTEXT_FILE) throw Error('Task center context is required');
|
||||
@@ -242,7 +246,7 @@ export async function bootRuntime({announce=true} = {}) {
|
||||
}
|
||||
}
|
||||
const names=ctx.tools.schemas().map(tool=>tool.name);
|
||||
if(names.length!==7 || !['dx_task_snapshot','dx_task_create','dx_task_catalog','dx_task_preflight','dx_task_manage','dx_task_execute','dx_task_operations'].every(name=>names.includes(name))) {
|
||||
if(names.length!==TASK_TOOLS.length+FILE_TOOLS.length || ![...TASK_TOOLS,...FILE_TOOLS].every(name=>names.includes(name))) {
|
||||
await shutdown.shutdown(1);
|
||||
throw Error('The task capability guard did not initialize; assistant startup refused.');
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@ window.__ModuleLoader__.load({id:'@dx/dsh-task',factory:require=>{
|
||||
h('p',null,'在“模型”设置页添加模型提供商或自定义模型 API,填写地址后可获取可用模型,也可手动填写模型 ID。保存后在输入框下方选择,模型设置和会话在重启后保留。'),
|
||||
h('h3',null,'异常工单'),
|
||||
h('p',null,'随工具预置专用工单上报身份,无需安装 Git 或登录。未解决的异常自动提交项目工单,并上传脱敏诊断证据。'),
|
||||
h('p',null,'工程修改与执行核验由任务中心完成。模型不直接访问工程文件或执行命令。'));
|
||||
h('h3',null,'文件与文件夹'),
|
||||
h('p',null,'可上传文件、选择本地文件夹,或在对话中提供路径。助手可读取和搜索文件,ZIP 解压到新的文件夹后继续分析;重复解压不会覆盖已有文件。'),
|
||||
h('p',null,'工程修改与执行核验由任务中心完成。'));
|
||||
}
|
||||
function SettingsLauncher({wide,openSettings}){
|
||||
return h('button',{'data-testid':'dx-tool-settings',type:'button','aria-label':'工具设置',onClick:()=>openSettings(),
|
||||
@@ -276,8 +278,6 @@ window.__ModuleLoader__.load({id:'@dx/dsh-task',factory:require=>{
|
||||
},'dx-task: automatic incident conversation');
|
||||
ctx.slots.inject('settings.launcher',()=>ctx.slots.register({name:'settings.launcher',priority:-10},SettingsLauncher));
|
||||
ctx.slots.inject('settings.section',()=>ctx.slots.register({name:'settings.section',id:'dx-tool',order:-100,label:()=>'DSH工具'},ToolSettings));
|
||||
ctx.slots.inject('conversation.hero.options',()=>ctx.slots.register({name:'conversation.hero.options',id:'dx-tool-hint'},
|
||||
()=>h('p',{style:{fontSize:13,color:'var(--dsw-alias-label-secondary)',textAlign:'center',margin:'4px 0 12px'}},'任务异常分析 · 规则内处理 · 执行监管')));
|
||||
ctx.sidebarRightTabs.register({id:'@dx/dsh-task',kind:'dx-task',priority:'extension',title:()=>'任务监管',guide:[{id:'dx-task',order:0,title:()=>'任务监管',description:()=>'当前任务与受控操作'}]});
|
||||
ctx.slots.inject('sidebar.right.pane.tab',()=>ctx.slots.register({name:'sidebar.right.pane.tab',key:'@dx/dsh-task'},TaskPanel));
|
||||
ctx.slots.inject('sidebar.brand.name',()=>ctx.slots.register({name:'sidebar.brand.name',priority:-10},()=>h('span',null,'DSH助手')));
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import {mkdir,mkdtemp,writeFile,realpath,rm} from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {Worker} from 'node:worker_threads';
|
||||
import {defineTool} from '@deepseek-ai/dsh-tools';
|
||||
|
||||
export const fileToolNames=['read','read_image','glob','grep','working_directory','extract_zip'];
|
||||
|
||||
const pending=[];
|
||||
let running=0;
|
||||
function startPending(){
|
||||
while(running<2&&pending.length){
|
||||
const job=pending.shift();
|
||||
if(job.signal.aborted){job.abort();continue;}
|
||||
running++;
|
||||
try{
|
||||
const bytes=Uint8Array.from(job.bytes);
|
||||
job.worker=new Worker(new URL('./zip-worker.mjs',import.meta.url),{
|
||||
workerData:{bytes,fflateUrl:import.meta.resolve('fflate')},transferList:[bytes.buffer],env:{},execArgv:[]});
|
||||
let result,failure;
|
||||
job.worker.once('message',value=>{result=value;});
|
||||
job.worker.once('error',error=>{failure=error;});
|
||||
job.worker.once('exit',code=>{
|
||||
running--;job.signal.removeEventListener('abort',job.abort);
|
||||
if(job.signal.aborted)job.reject(job.signal.reason??Error('ZIP extraction cancelled'));
|
||||
else if(failure||result?.error||code!==0||!result?.entries)
|
||||
job.reject(failure??Error(result?.error??'ZIP worker exited without a result'));
|
||||
else job.resolve(result.entries);
|
||||
startPending();
|
||||
});
|
||||
}catch(error){
|
||||
running--;job.signal.removeEventListener('abort',job.abort);job.reject(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function decompress(bytes,signal){
|
||||
signal.throwIfAborted();
|
||||
return new Promise((resolve,reject)=>{
|
||||
const job={bytes,signal,resolve,reject};
|
||||
job.abort=()=>{
|
||||
if(job.worker){void job.worker.terminate();return;}
|
||||
const index=pending.indexOf(job);if(index>=0)pending.splice(index,1);
|
||||
reject(signal.reason??Error('ZIP extraction cancelled'));
|
||||
};
|
||||
signal.addEventListener('abort',job.abort,{once:true});pending.push(job);startPending();
|
||||
});
|
||||
}
|
||||
|
||||
export function registerFileTools(ctx){
|
||||
ctx.tools.register(defineTool({name:'extract_zip',
|
||||
description:'Extract a local or uploaded ZIP archive into a new folder, returning its absolute path and file names. Relative paths use the conversation working directory. output_directory optionally selects an existing parent folder; defaults to the conversation working directory. Never overwrites existing files. Read the returned paths to inspect extracted content. ZIP only; encrypted archives are unsupported.',
|
||||
parameters:{file_path:{type:'string',required:true},output_directory:{type:'string'}},
|
||||
output:{schema:{type:'object',additionalProperties:true},render:(_args,value)=>[{type:'text',text:JSON.stringify(value)}]},
|
||||
execute:async(args,exec)=>{
|
||||
if(typeof args.file_path!=='string'||!args.file_path.trim())throw Error('file_path is required');
|
||||
const cwd=exec.agent?ctx.workingDirectory.get(exec.agent.session):process.env.DX_DSH_WORKSPACE;
|
||||
const target=await ctx.fs.resolve(args.file_path,{cwd,signal:exec.signal});
|
||||
const bytes=await ctx.fs.readBytes(target,exec.signal,64*1024*1024);
|
||||
const files=await decompress(bytes,exec.signal);
|
||||
exec.signal.throwIfAborted();
|
||||
const parent=await realpath(path.resolve(cwd,args.output_directory||'.'));
|
||||
const directory=await mkdtemp(path.join(parent,'dsh-extracted-'));
|
||||
try{
|
||||
for(const [name,data] of files){
|
||||
exec.signal.throwIfAborted();
|
||||
const output=path.resolve(directory,name.replaceAll('\\','/'));
|
||||
const relative=path.relative(directory,output);
|
||||
if(relative.startsWith('..')||path.isAbsolute(relative))throw Error('Unsafe ZIP entry path');
|
||||
if(name.endsWith('/'))await mkdir(output,{recursive:true});
|
||||
else{await mkdir(path.dirname(output),{recursive:true});await writeFile(output,data,{flag:'wx'});}
|
||||
}
|
||||
return {directory,files:files.slice(0,200).map(([name])=>name),total_entries:files.length,truncated:files.length>200};
|
||||
}catch(error){
|
||||
// Only this invocation's newly created temporary output is removed.
|
||||
if(path.dirname(directory)===parent&&path.basename(directory).startsWith('dsh-extracted-'))
|
||||
await rm(directory,{recursive:true,force:true});
|
||||
throw error;
|
||||
}
|
||||
}}));
|
||||
}
|
||||
@@ -5,8 +5,9 @@ import {defineTool} from '@deepseek-ai/dsh-tools';
|
||||
import {registerLifecycle,lifecycleNames} from './lifecycle.mjs';
|
||||
import {registerRuntimeUsage} from './runtime_usage.mjs';
|
||||
import {modelSnapshot} from './model_results.mjs';
|
||||
import {registerFileTools,fileToolNames} from './files.mjs';
|
||||
|
||||
export const inject=['webServer','connection','tools','workspaceRegistry','sessionController','agents','llm'];
|
||||
export const inject=['webServer','connection','tools','workspaceRegistry','sessionController','agents','llm','fs','workingDirectory'];
|
||||
const actions=new Set(['retry_failed','pause_task','refresh_snapshot','retry_issue','open_evidence']);
|
||||
export async function readContext(){
|
||||
const value=JSON.parse(await readFile(process.env.DX_DSH_CONTEXT_FILE,'utf8'));
|
||||
@@ -19,7 +20,7 @@ export async function apply(ctx){
|
||||
const creations=new Map();
|
||||
let auditTail=Promise.resolve();
|
||||
ctx.on('tools/result',(exec,result)=>{
|
||||
if(!['dx_task_snapshot','dx_task_create',...lifecycleNames].includes(exec.name))return;
|
||||
if(!['dx_task_snapshot','dx_task_create',...lifecycleNames,...fileToolNames].includes(exec.name))return;
|
||||
const row={at:new Date().toISOString(),name:exec.name,call_id:exec.callId,is_error:result.isError,
|
||||
task_id:result.value?.task_id||'',revision:result.value?.revision||'',
|
||||
status:result.value?.status,task_ids:result.value?.task_ids||result.value?.tasks?.map(t=>t.task_id)||[]};
|
||||
@@ -112,7 +113,8 @@ export async function apply(ctx){
|
||||
}finally{clearTimeout(timer);exec.signal.removeEventListener('abort',cancel);creations.delete(request_id);}
|
||||
}}));
|
||||
registerLifecycle(ctx);
|
||||
ctx.tools.guard(exec=>exec.name==='dx_task_snapshot'||(['dx_task_create',...lifecycleNames].includes(exec.name)&&!incidentSnapshots.has(exec.agent?.id))?
|
||||
registerFileTools(ctx);
|
||||
ctx.tools.guard(exec=>exec.name==='dx_task_snapshot'||(['dx_task_create',...lifecycleNames,...fileToolNames].includes(exec.name)&&!incidentSnapshots.has(exec.agent?.id))?
|
||||
undefined:'This tool is not enabled for this session. Automatic incident sessions may only read their snapshot.');
|
||||
const workspace=await ctx.workspaceRegistry.create(process.env.DX_DSH_WORKSPACE,'DxDesigner 任务中心');
|
||||
ctx.webServer.register({kind:'exact',path:'/dx/workspace',handler:async(req,res)=>{
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import {parentPort,workerData} from 'node:worker_threads';
|
||||
import {crc32} from 'node:zlib';
|
||||
|
||||
function zipChecksums(bytes){
|
||||
const data=Buffer.from(bytes.buffer,bytes.byteOffset,bytes.byteLength);
|
||||
let end=data.length-22;
|
||||
for(;end>=Math.max(0,data.length-65557);end--)
|
||||
if(data.readUInt32LE(end)===0x06054b50&&end+22+data.readUInt16LE(end+20)===data.length)break;
|
||||
if(end<0||data.readUInt32LE(end)!==0x06054b50)throw Error('Invalid ZIP central directory');
|
||||
if(data.readUInt16LE(end+4)||data.readUInt16LE(end+6))throw Error('Multi-volume ZIP is unsupported');
|
||||
const count=data.readUInt16LE(end+10),entries=[];
|
||||
if(count===65535||count>10000)throw Error('ZIP exceeds 10000 entries');
|
||||
let offset=data.readUInt32LE(end+16);
|
||||
for(let i=0;i<count;i++){
|
||||
if(offset+46>end||data.readUInt32LE(offset)!==0x02014b50)throw Error('Invalid ZIP entry');
|
||||
if(data.readUInt16LE(offset+8)&1)throw Error('Encrypted ZIP is unsupported');
|
||||
const size=data.readUInt32LE(offset+24);
|
||||
if(size===0xffffffff)throw Error('ZIP64 entries are unsupported');
|
||||
entries.push({crc:data.readUInt32LE(offset+16),size});
|
||||
offset+=46+data.readUInt16LE(offset+28)+data.readUInt16LE(offset+30)+data.readUInt16LE(offset+32);
|
||||
if(offset>end)throw Error('Invalid ZIP entry length');
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
try{
|
||||
const {unzipSync}=await import(workerData.fflateUrl);
|
||||
const bytes=workerData.bytes,checksums=zipChecksums(bytes),expected=new Map();
|
||||
let expanded=0,count=0;
|
||||
// A metadata-only pass finishes before any entry is decompressed.
|
||||
unzipSync(bytes,{filter:entry=>{
|
||||
const name=entry.name.replaceAll('\\','/');
|
||||
if(name.startsWith('/')||name.split('/').some(part=>part==='..'||/[\x00-\x1f:]/.test(part)))
|
||||
throw Error('Unsafe ZIP entry path');
|
||||
if(entry.name==='__proto__')throw Error('Unsupported ZIP entry name: __proto__');
|
||||
if(expected.has(entry.name))throw Error('Duplicate ZIP entry');
|
||||
const checksum=checksums[count];
|
||||
if(!checksum||checksum.size!==entry.originalSize)throw Error('Invalid ZIP entry size');
|
||||
expected.set(entry.name,checksum);
|
||||
expanded+=entry.originalSize;
|
||||
if(++count>10000||expanded>256*1024*1024)throw Error('ZIP exceeds 10000 entries or 256 MiB expanded size');
|
||||
return false;
|
||||
}});
|
||||
if(count!==checksums.length)throw Error('Invalid ZIP entry count');
|
||||
// Synchronous inflation is confined to this single, terminable worker.
|
||||
const files=unzipSync(bytes),entries=Object.entries(files);
|
||||
if(entries.length!==expected.size||[...expected.keys()].some(name=>!Object.hasOwn(files,name)))
|
||||
throw Error('ZIP decompressed file names do not match the archive');
|
||||
for(const [name,data] of entries){
|
||||
const checksum=expected.get(name);
|
||||
if(!checksum||data.length!==checksum.size||crc32(data)!==checksum.crc)throw Error('ZIP checksum mismatch: '+name);
|
||||
}
|
||||
parentPort.postMessage({entries},[...new Set(entries.map(([,data])=>data.buffer))]);
|
||||
}catch(error){parentPort.postMessage({error:error.message});}
|
||||
finally{parentPort.close();}
|
||||
@@ -3371,14 +3371,29 @@ Function QPReadContinuation(ByVal Path)
|
||||
End Function
|
||||
|
||||
Sub QPWriteUtf8(ByVal Path, ByVal Text)
|
||||
Dim Stream
|
||||
Dim Stream, Attempt, Limit, Number, Source, Description
|
||||
Set Stream = CreateObject("ADODB.Stream")
|
||||
Stream.Type = 2
|
||||
Stream.Charset = "utf-8"
|
||||
Stream.Open
|
||||
Stream.WriteText CStr(Text)
|
||||
Stream.SaveToFile CStr(Path), 2
|
||||
Limit = 1
|
||||
' The session driver polls this receipt; retry only its file write, never CAD.
|
||||
If LCase(QP_Fso.GetFileName(CStr(Path))) = "project-session-result.tsv" Then Limit = 20
|
||||
For Attempt = 1 To Limit
|
||||
On Error Resume Next
|
||||
Err.Clear
|
||||
Stream.SaveToFile CStr(Path), 2
|
||||
Number = Err.Number
|
||||
Source = Err.Source
|
||||
Description = Err.Description
|
||||
Err.Clear
|
||||
On Error GoTo 0
|
||||
If Number = 0 Then Exit For
|
||||
If Attempt < Limit Then WScript.Sleep 50
|
||||
Next
|
||||
Stream.Close
|
||||
If Number <> 0 Then Err.Raise Number, Source, Description
|
||||
End Sub
|
||||
|
||||
Sub QPAppendAscii(ByVal Path, ByVal Text)
|
||||
|
||||
@@ -35,7 +35,7 @@ def write_record(path, record):
|
||||
json.dump(record, stream, ensure_ascii=False, indent=2)
|
||||
stream.flush()
|
||||
os.fsync(stream.fileno())
|
||||
retry_sharing_violation(lambda: os.replace(temporary, path))
|
||||
retry_sharing_violation(lambda: os.replace(temporary, path), target=path)
|
||||
finally:
|
||||
temporary.unlink(missing_ok=True)
|
||||
|
||||
|
||||
@@ -82,15 +82,18 @@ def _propagate(store, task_id, before, after):
|
||||
for field in ('new_part_number', 'sync_nc'):
|
||||
if getattr(row, field) == getattr(prior, field):
|
||||
changes[field] = getattr(rule, field)
|
||||
if (row.custom_value_enabled, row.custom_value) == (prior.custom_value_enabled, prior.custom_value):
|
||||
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value)
|
||||
if (row.value_mode in {'inherit', 'custom'} and
|
||||
(row.custom_value_enabled, row.custom_value) == (prior.custom_value_enabled, prior.custom_value)):
|
||||
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value,
|
||||
value_mode='custom' if rule.custom_value_enabled else 'inherit')
|
||||
elif not row.new_part_number:
|
||||
changes['new_part_number'] = rule.new_part_number
|
||||
changes['target_cell'] = rule.target_cell
|
||||
if row.sync_nc:
|
||||
changes['sync_nc'] = rule.sync_nc
|
||||
if not row.custom_value_enabled and not row.custom_value:
|
||||
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value)
|
||||
if row.value_mode == 'inherit' and not row.custom_value_enabled and not row.custom_value:
|
||||
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value,
|
||||
value_mode='custom' if rule.custom_value_enabled else 'inherit')
|
||||
if (not changes.get('target_cell', row.target_cell)
|
||||
and changes.get('new_part_number', row.new_part_number) == rule.new_part_number):
|
||||
# Saved BOM/manual mappings can predate their explicit rule Cell.
|
||||
|
||||
@@ -89,7 +89,7 @@ def run(request_path):
|
||||
from .stop_control import StopControl
|
||||
stop = StopControl() if request.get('interactive_stop', False) else threading.Event()
|
||||
finished, output_lock = threading.Event(), threading.Lock()
|
||||
kind = {'inspect_bom_batch': 'bom_import', 'preview_bom_batch': 'bom_import',
|
||||
kind = {'inspect_bom_batch': 'bom_import', 'preview_bom_batch': 'bom_import', 'preview_bom_sources': 'bom_import',
|
||||
'execute_task_queue': 'task_queue', 'execute_quick_run_queue': 'quick_run_queue'}.get(request['operation'], request['operation'])
|
||||
runtime = BatchCadRuntime(root, kind, stop, session_id=batch_id)
|
||||
sequence = 0
|
||||
@@ -156,9 +156,9 @@ def run(request_path):
|
||||
elif operation == 'execute_task_queue':
|
||||
from .task_queue import execute_task_queue
|
||||
result = execute_task_queue(service, **arguments, stop_event=stop, runtime=runtime)
|
||||
elif operation in {'inspect_bom_batch', 'preview_bom_batch'}:
|
||||
elif operation in {'inspect_bom_batch', 'preview_bom_batch', 'preview_bom_sources'}:
|
||||
controls = {'stop_event': stop, 'on_progress': progress, 'runtime': runtime}
|
||||
if operation == 'inspect_bom_batch':
|
||||
if operation in {'inspect_bom_batch', 'preview_bom_sources'}:
|
||||
controls['on_phase'] = phase
|
||||
result = getattr(service, operation)(**arguments, **controls)
|
||||
elif operation == 'collect_target_inventory':
|
||||
@@ -174,7 +174,7 @@ def run(request_path):
|
||||
response.update(status='ok', business_succeeded=True, result=encode(result))
|
||||
write_record(directory / 'business-result.json', {'version': VERSION, 'batch_id': batch_id,
|
||||
'attempt_id': attempt_id, 'generation': max(1, runtime.generation), 'payload': response})
|
||||
if operation == 'inspect_bom_batch':
|
||||
if operation in {'inspect_bom_batch', 'preview_bom_sources'}:
|
||||
from .bom_import_state import BomImportPhase
|
||||
phase(BomImportPhase.RELEASING, '本批位号核验结束,正在释放后台资源…')
|
||||
except Exception as error:
|
||||
|
||||
@@ -45,7 +45,7 @@ def _identity(scope):
|
||||
def export_template(store, task_id, path):
|
||||
records = () if task_id is None else tuple({'project_name': task.name, 'project_path': str(project.path),
|
||||
'enabled': True, 'sync_nc': True} for task, project in _scope(store, task_id))
|
||||
return write_bom_template(path, records)
|
||||
return write_bom_template(path, records, include_value_mode=True)
|
||||
|
||||
|
||||
def _assert_editable(service, task_id):
|
||||
|
||||
@@ -23,6 +23,9 @@ class BomRowResult:
|
||||
project_name: str
|
||||
status: str # passed, failed, unchecked
|
||||
reason: str = ''
|
||||
source_file: str = ''
|
||||
source_row: int | None = None
|
||||
duplicate_rows: tuple[int, ...] = ()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -39,6 +42,7 @@ class BomImportReport:
|
||||
projects: tuple[BomProjectResult, ...]
|
||||
preview: BomBatchPreview | None = None
|
||||
errors: tuple[str, ...] = ()
|
||||
source_map_path: Path | None = None
|
||||
|
||||
@property
|
||||
def total_rows(self):
|
||||
@@ -120,7 +124,8 @@ def _check_record_conflicts(rows, records, indices):
|
||||
reason = '位号在 BOM 中重复(Excel 行 ' + ', '.join(
|
||||
str(rows[item].excel_row) for item in dict.fromkeys(claimed)) + ')'
|
||||
for index in claimed:
|
||||
rows[index] = replace(rows[index], status='failed', reason=reason)
|
||||
rows[index] = replace(rows[index], status='failed', reason=reason,
|
||||
duplicate_rows=tuple(rows[item].excel_row for item in dict.fromkeys(claimed)))
|
||||
|
||||
|
||||
def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None):
|
||||
@@ -253,6 +258,7 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
|
||||
for position, (key, indices) in enumerate(grouped.items(), 1):
|
||||
check_cancelled()
|
||||
task, project = owners[key]
|
||||
task_default_value_mode = 'part_label' if task.copy_part_number_to_value else 'preserve'
|
||||
def progress(message, phase=None):
|
||||
notify(f'工程 {position}/{len(grouped)} · {message}', phase)
|
||||
progress(f'正在核验「{task.name}」的位号库存…', BomImportPhase.INVENTORY)
|
||||
@@ -304,7 +310,7 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
|
||||
if matcher is None:
|
||||
raise TaskCenterError('target_inventory', '未采集到可匹配的工程位号')
|
||||
matched = matcher.match_record(records[index])
|
||||
compile_target_rules(tuple(matched.values()))
|
||||
compile_target_rules(tuple(matched.values()), default_value_mode=task_default_value_mode)
|
||||
for target_key, row in matched.items():
|
||||
touched.setdefault(target_key, []).append(index)
|
||||
matched_rows[target_key] = row
|
||||
@@ -315,10 +321,11 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
|
||||
if len(indices_for_target) > 1:
|
||||
for index in indices_for_target:
|
||||
rows[index] = replace(rows[index], status='failed', reason='位号在 BOM 中重复(Excel 行 ' +
|
||||
', '.join(str(rows[item].excel_row) for item in indices_for_target) + ')')
|
||||
', '.join(str(rows[item].excel_row) for item in indices_for_target) + ')',
|
||||
duplicate_rows=tuple(rows[item].excel_row for item in indices_for_target))
|
||||
combined = tuple(matched_rows.get(key, row) for key, row in matcher.by_key.items()) if matcher else ()
|
||||
try:
|
||||
compile_target_rules(combined)
|
||||
compile_target_rules(combined, default_value_mode=task_default_value_mode)
|
||||
except TaskCenterError as error:
|
||||
for index in indices:
|
||||
if rows[index].status == 'passed':
|
||||
|
||||
@@ -29,6 +29,8 @@ _HEADER_HELP = (
|
||||
'是否替换:TRUE/FALSE 或 是/否;留空默认替换。未列出的位号保留。',
|
||||
'自定义 Value,可选;留空沿用工具原有 Value 策略。',
|
||||
'同步 NC:TRUE/FALSE 或 是/否;留空默认开启。',
|
||||
'Value 来源:inherit 沿用任务策略,preserve 保留原值,part_label 使用 Part Label,'
|
||||
'part_number 使用新料号,custom 使用自定义 Value 列;留空时有自定义值则使用 custom,否则 inherit。',
|
||||
)
|
||||
_REFDES_SPLIT = re.compile(r'[,,;;、\s]+')
|
||||
_ALIASES = {
|
||||
@@ -41,16 +43,19 @@ _ALIASES = {
|
||||
'uid': 'uid', '替换': 'enabled', '是否替换': 'enabled', 'enabled': 'enabled',
|
||||
'新value': 'custom_value', '自定义value': 'custom_value', 'custom_value': 'custom_value',
|
||||
'同步nc': 'sync_nc', '同步_nc': 'sync_nc', 'sync_nc': 'sync_nc',
|
||||
'value_mode': 'value_mode', 'value来源': 'value_mode', 'value策略': 'value_mode',
|
||||
}
|
||||
|
||||
|
||||
def write_bom_template(path: Path, records=()) -> Path:
|
||||
def write_bom_template(path: Path, records=(), *, include_value_mode=False) -> Path:
|
||||
records = tuple(records)
|
||||
headers = BOM_HEADERS + (('value_mode',) if include_value_mode or any('value_mode' in record for record in records) else ())
|
||||
workbook = Workbook()
|
||||
sheet = workbook.active
|
||||
sheet.title = 'BOM Replacement'
|
||||
sheet.append(BOM_HEADERS)
|
||||
sheet.append(headers)
|
||||
for record in records:
|
||||
sheet.append(tuple(record.get(header, '') for header in BOM_HEADERS))
|
||||
sheet.append(tuple(record.get(header, '') for header in headers))
|
||||
wrapped = Alignment(wrap_text=True, vertical='top')
|
||||
for row in sheet.iter_rows():
|
||||
for cell in row:
|
||||
@@ -66,8 +71,12 @@ def write_bom_template(path: Path, records=()) -> Path:
|
||||
validation = DataValidation(type='list', formula1='"TRUE,FALSE"', allow_blank=False)
|
||||
sheet.add_data_validation(validation)
|
||||
validation.add(f'{column}2:{column}{max(2, sheet.max_row + 500)}')
|
||||
if 'value_mode' in headers:
|
||||
validation = DataValidation(type='list', formula1='"inherit,preserve,part_label,part_number,custom"', allow_blank=False)
|
||||
sheet.add_data_validation(validation)
|
||||
validation.add(f'L2:L{max(2, sheet.max_row + 500)}')
|
||||
for column, width in {'A': 30, 'B': 64, 'C': 22, 'D': 22, 'E': 28, 'F': 18, 'G': 14,
|
||||
'H': 28, 'I': 12, 'J': 24, 'K': 12}.items():
|
||||
'H': 28, 'I': 12, 'J': 24, 'K': 12, 'L': 16}.items():
|
||||
sheet.column_dimensions[column].width = width
|
||||
sheet.column_dimensions[column].alignment = wrapped
|
||||
writable_last_row = max(2, sheet.max_row)
|
||||
@@ -97,6 +106,7 @@ class BomRecord:
|
||||
enabled: bool
|
||||
custom: str
|
||||
sync_nc: bool
|
||||
value_mode: str = 'inherit'
|
||||
|
||||
|
||||
def read_bom_records(path: Path) -> tuple[BomRecord, ...]:
|
||||
@@ -151,13 +161,18 @@ def parse_bom_row(cells, positions):
|
||||
enabled = _bool(_value(values, positions, 'enabled', True))
|
||||
sync_nc = _bool(_value(values, positions, 'sync_nc', True))
|
||||
custom = _text(values, positions, 'custom_value')
|
||||
value_mode = _text(values, positions, 'value_mode') or ('custom' if custom else 'inherit')
|
||||
if value_mode not in {'inherit', 'preserve', 'part_label', 'part_number', 'custom'}:
|
||||
raise ValueError('Value 来源只能是 inherit、preserve、part_label、part_number 或 custom')
|
||||
if value_mode == 'custom' and not custom:
|
||||
raise ValueError('Value 来源为 custom 时必须填写自定义 Value')
|
||||
schematic = _text(values, positions, 'schematic_name')
|
||||
sheet_number = _text(values, positions, 'sheet_number')
|
||||
uid = _text(values, positions, 'uid')
|
||||
project = _text(values, positions, 'project_path')
|
||||
if project and not Path(project).is_absolute():
|
||||
raise ValueError('工程路径必须使用模板中的完整路径')
|
||||
return BomRecord(project, old, new, refs, schematic, sheet_number, uid, enabled, custom, sync_nc)
|
||||
return BomRecord(project, old, new, refs, schematic, sheet_number, uid, enabled, custom, sync_nc, value_mode)
|
||||
|
||||
|
||||
class BomMatcher:
|
||||
@@ -196,9 +211,10 @@ class BomMatcher:
|
||||
if key in matched:
|
||||
raise TaskCenterError('bom_import', f'位号 {refdes} 在 BOM 中重复')
|
||||
matched[key] = TargetPlanRow(target, record.enabled, record.new,
|
||||
bool(record.custom), record.custom, record.sync_nc,
|
||||
record.value_mode == 'custom', record.custom, record.sync_nc,
|
||||
(self.by_key[key].target_cell if self.by_key[key].new_part_number == record.new else '')
|
||||
or self.cell_defaults.get((target.old_part_number.casefold(), record.new), ''))
|
||||
or self.cell_defaults.get((target.old_part_number.casefold(), record.new), ''),
|
||||
record.value_mode)
|
||||
return matched
|
||||
|
||||
|
||||
|
||||
@@ -52,6 +52,9 @@ def _scrub_json(value):
|
||||
|
||||
|
||||
def _allowed(parts):
|
||||
if (len(parts) == 5 and parts[0] == 'tasks' and parts[2] == 'quick-run-history'
|
||||
and re.fullmatch(r'bom-import-[A-Za-z0-9-]+', parts[3])):
|
||||
return parts[-1] == 'source-map.json'
|
||||
if len(parts) == 2 and parts[0] == 'logs':
|
||||
return bool(re.fullmatch(r'[^/]+\.log(?:\.\d+)?', parts[-1]))
|
||||
if len(parts) == 3 and parts[0] == 'batch-resources':
|
||||
|
||||
@@ -46,7 +46,7 @@ _TABLES={
|
||||
'selected','available','status','summary')),
|
||||
'target_plan':(('task_id','project_path','schematic_name','sheet_number','uid'),
|
||||
('task_id','project_path','schematic_name','sheet_number','uid','refdes','old_part_number','old_value',
|
||||
'enabled','new_part_number','custom_value_enabled','custom_value','sync_nc','target_cell')),
|
||||
'enabled','new_part_number','custom_value_enabled','custom_value','sync_nc','target_cell','value_mode')),
|
||||
}
|
||||
_CUTOFF='history_import_cutoff_v1'
|
||||
_IMPORTED='history_import_done_v1'
|
||||
|
||||
@@ -81,7 +81,9 @@ def _stage_new_round(service, task_id, transition):
|
||||
for member in members:
|
||||
facade = service._project_task_service(member.task_id)
|
||||
root = facade._quick_run_preparer.data_root / 'quick-run'
|
||||
rules = (compile_target_rules(store.list_target_plan(member.task_id))
|
||||
rules = (compile_target_rules(
|
||||
store.list_target_plan(member.task_id),
|
||||
default_value_mode=('part_label' if member.copy_part_number_to_value else 'preserve'))
|
||||
if member.target_plan_mode else store.list_rules(member.task_id))
|
||||
snapshot = facade._quick_run_preparer.read_snapshot(member.task_id,
|
||||
store.list_projects(member.task_id), rules,
|
||||
|
||||
@@ -1999,7 +1999,10 @@ def _rule_command_options(rule: RuleRecord, base_value_mode: str) -> tuple[str,
|
||||
sync_nc = bool(getattr(rule, "sync_nc", True))
|
||||
if bool(getattr(rule, "custom_value_enabled", False)):
|
||||
return "custom", str(getattr(rule, "custom_value", "")), sync_nc, base_value_mode
|
||||
return base_value_mode, "", sync_nc, base_value_mode
|
||||
item_mode = getattr(rule, "value_mode", base_value_mode)
|
||||
if item_mode not in {"preserve", "part_label", "part_number"}:
|
||||
raise TaskCenterError("value_option", "queue Value mode is invalid")
|
||||
return item_mode, "", sync_nc, base_value_mode
|
||||
|
||||
|
||||
def _command_text(
|
||||
|
||||
@@ -16,7 +16,7 @@ HISTORY_MAX_AGE_DAYS = 30
|
||||
HISTORY_MAX_ENTRIES = 100
|
||||
HISTORY_MAX_BYTES = 5 * 1024 * 1024 * 1024
|
||||
HISTORY_MIN_ENTRIES = 10
|
||||
_HISTORY_NAME = re.compile(r"(?:batch|recovery|sheet-discovery|obsolete)-[A-Za-z0-9-]+\Z")
|
||||
_HISTORY_NAME = re.compile(r"(?:batch|recovery|sheet-discovery|obsolete|bom-import)-[A-Za-z0-9-]+\Z")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
|
||||
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
from collections.abc import Callable
|
||||
from contextlib import contextmanager, ExitStack
|
||||
from datetime import datetime
|
||||
from dataclasses import replace
|
||||
import os
|
||||
from pathlib import Path
|
||||
import threading
|
||||
@@ -194,6 +195,83 @@ class TaskCenterService:
|
||||
lambda: preview_batch(self, task_id, path, **control_options(stop_event, on_progress),
|
||||
**({'runtime': runtime} if runtime is not None else {})))
|
||||
|
||||
def preview_bom_sources(self, task_id: str, sources, *, stop_event=None, on_progress=None,
|
||||
on_phase=None, runtime=None):
|
||||
"""Normalize several BOM files and exact-name migrated project paths before CAD reads."""
|
||||
sources = tuple(Path(source) for source in sources)
|
||||
if runtime is None and self._supervised:
|
||||
return self._supervise('preview_bom_sources', {'task_id': task_id, 'sources': sources},
|
||||
stop_event=stop_event, on_progress=on_progress, on_phase=on_phase)
|
||||
from .bom_batch import _scope
|
||||
from .bom_import_report import BomImportReport, inspect_batch
|
||||
from .bom_import_state import BomImportPhase
|
||||
from .retention import maintain_quick_run_storage
|
||||
from .workspace_bom import load_sources, route_sources, write_routed_bom
|
||||
from uuid import uuid4
|
||||
|
||||
def preview():
|
||||
message = '正在读取 BOM 来源表格…'
|
||||
if on_phase is not None:
|
||||
on_phase(BomImportPhase.READING, message)
|
||||
if on_progress is not None:
|
||||
on_progress(message)
|
||||
rows = load_sources(sources, stop_event=stop_event, on_progress=on_progress)
|
||||
scope = _scope(self._store, task_id)
|
||||
paths = tuple(project.path for _, project in scope)
|
||||
names = {project.path: task.name for task, project in scope}
|
||||
routes = route_sources(rows, paths, single=len(paths) == 1, project_names=names)
|
||||
unresolved = [(row.source.name, row.excel_row, row.project_name or row.project_path)
|
||||
for row in rows if routes.get(row.project_key) is None]
|
||||
if unresolved:
|
||||
details = '; '.join(f'{name} 第 {number} 行:{key}' for name, number, key in unresolved[:20])
|
||||
raise TaskCenterError('project_binding', f'无法唯一匹配 BOM 工程,请确认迁移工程对应关系:{details}')
|
||||
task_root = self._logging.root / 'tasks' / task_id
|
||||
maintain_quick_run_storage(task_root)
|
||||
root = task_root / 'quick-run-history' / ('bom-import-' + uuid4().hex)
|
||||
normalized = root / 'normalized-bom.xlsx'
|
||||
mapping = root / 'source-map.json'
|
||||
|
||||
def annotate(report):
|
||||
if report is None:
|
||||
return report
|
||||
annotated = []
|
||||
for row in report.rows:
|
||||
if not 2 <= row.excel_row <= len(rows) + 1:
|
||||
annotated.append(row)
|
||||
continue
|
||||
source = rows[row.excel_row - 2]
|
||||
reason = row.reason
|
||||
if row.duplicate_rows:
|
||||
locations = (f'{rows[number - 2].source.name} 第 {rows[number - 2].excel_row} 行'
|
||||
for number in row.duplicate_rows)
|
||||
reason = '位号在 BOM 中重复(' + ';'.join(locations) + ')'
|
||||
annotated.append(replace(row, source_file=str(source.source), source_row=source.excel_row,
|
||||
reason=reason + f';来源:{source.source} 第 {source.excel_row} 行'))
|
||||
return replace(report, rows=tuple(annotated), source_map_path=mapping)
|
||||
|
||||
def inspection_phase(phase, message):
|
||||
if on_phase is not None and phase is not BomImportPhase.READING:
|
||||
on_phase(phase, message)
|
||||
|
||||
try:
|
||||
write_routed_bom(normalized, rows, routes, project_names=names)
|
||||
append_task_event(self._logging, level='INFO', task_id=task_id,
|
||||
operation='preview_bom_sources', message='BOM 导入来源记录已保存',
|
||||
details={'source_map_path': str(mapping), 'source_count': len({row.source for row in rows})})
|
||||
report = inspect_batch(self, task_id, normalized, stop_event=stop_event,
|
||||
on_progress=on_progress, on_phase=inspection_phase, runtime=runtime)
|
||||
return annotate(report)
|
||||
except TaskCenterError as error:
|
||||
if getattr(error, 'bom_report', None) is not None:
|
||||
error.bom_report = annotate(error.bom_report)
|
||||
if isinstance(getattr(error, 'completed_result', None), BomImportReport):
|
||||
error.completed_result = annotate(error.completed_result)
|
||||
raise
|
||||
finally:
|
||||
normalized.unlink(missing_ok=True)
|
||||
|
||||
return self._idle_quick_run_operation('preview_bom_sources', task_id, preview)
|
||||
|
||||
def save_bom_batch(self, preview, rows_by_task):
|
||||
from .bom_batch import save_batch
|
||||
return self._idle_quick_run_operation('save_bom_batch', preview.anchor_task_id,
|
||||
@@ -298,7 +376,7 @@ class TaskCenterService:
|
||||
raise TaskCenterError("project_binding", "bound task must own exactly one project")
|
||||
rules, targets = (bundle.batch_rules if bundle.batch_rules is not None else bundle.rules), bundle.targets
|
||||
facade = self._facade_for_task_record(task)
|
||||
effective_rules = compile_target_rules(tuple(targets)) if task.target_plan_mode else rules
|
||||
effective_rules = (compile_target_rules(tuple(targets), default_value_mode=("part_label" if task.copy_part_number_to_value else "preserve")) if task.target_plan_mode else rules)
|
||||
snapshot = facade._quick_run_preparer.read_snapshot(task_id, projects, effective_rules,
|
||||
copy_part_number_to_value=task.copy_part_number_to_value, read_control=read_control)
|
||||
progress = snapshot.progress
|
||||
@@ -350,7 +428,7 @@ class TaskCenterService:
|
||||
task = self._store.get_task(task_id)
|
||||
if task.target_plan_mode:
|
||||
rows = self._store.list_target_plan(task_id)
|
||||
rules = compile_target_rules(tuple(rows))
|
||||
rules = compile_target_rules(tuple(rows), default_value_mode=("part_label" if task.copy_part_number_to_value else "preserve"))
|
||||
if not rules:
|
||||
raise TaskCenterError("enabled_rule_required", "target plan has no enabled targets")
|
||||
return rules
|
||||
|
||||
@@ -14,7 +14,8 @@ from .target_plan import TargetPlanRow, compile_target_rules
|
||||
|
||||
HEADERS = ("project_path", "project_name", "schematic_name", "sheet_number", "uid",
|
||||
"refdes", "old_part_number", "old_value", "enabled", "new_part_number",
|
||||
"custom_value_enabled", "custom_value", "sync_nc")
|
||||
"custom_value_enabled", "custom_value", "sync_nc", "value_mode")
|
||||
LEGACY_HEADERS = HEADERS[:-1]
|
||||
|
||||
|
||||
def export_target_plan(store, task_id: str, path: Path) -> Path:
|
||||
@@ -28,7 +29,7 @@ def export_target_plan(store, task_id: str, path: Path) -> Path:
|
||||
values = (str(target.project_path), target.project_path.name, target.schematic_name,
|
||||
target.sheet_number, target.uid, target.refdes, target.old_part_number,
|
||||
target.old_value, row.enabled, row.new_part_number, row.custom_value_enabled,
|
||||
row.custom_value, row.sync_nc)
|
||||
row.custom_value, row.sync_nc, row.value_mode)
|
||||
sheet.append(values)
|
||||
for cell in sheet[sheet.max_row]:
|
||||
if isinstance(cell.value, str):
|
||||
@@ -44,7 +45,7 @@ def export_target_plan(store, task_id: str, path: Path) -> Path:
|
||||
check.add(f"{column}2:{column}{max(sheet.max_row, 2)}")
|
||||
for column, width in {"A": 45, "B": 22, "C": 20, "D": 12, "E": 26,
|
||||
"F": 14, "G": 20, "H": 20, "I": 12, "J": 20,
|
||||
"K": 22, "L": 20, "M": 12}.items():
|
||||
"K": 22, "L": 20, "M": 12, "N": 16}.items():
|
||||
sheet.column_dimensions[column].width = width
|
||||
path = Path(path)
|
||||
workbook.save(path)
|
||||
@@ -66,14 +67,16 @@ def read_target_plan(store, task_id: str, path: Path) -> tuple[TargetPlanRow, ..
|
||||
if len(workbook.worksheets) != 1:
|
||||
raise ValueError("workbook must have exactly one plan sheet")
|
||||
records = list(workbook.active.iter_rows(values_only=False))
|
||||
if not records or tuple(cell.value for cell in records[0]) != HEADERS:
|
||||
header = tuple(cell.value for cell in records[0]) if records else ()
|
||||
if header not in {HEADERS, LEGACY_HEADERS}:
|
||||
raise ValueError("plan columns changed")
|
||||
legacy = header == LEGACY_HEADERS
|
||||
by_key: dict[tuple[str, str, str, str], TargetPlanRow] = {}
|
||||
for cells in records[1:]:
|
||||
values = tuple(cell.value for cell in cells)
|
||||
if all(value is None for value in values):
|
||||
continue
|
||||
if len(values) != len(HEADERS) or any(cell.data_type == "f" for cell in cells):
|
||||
if len(values) != len(header) or any(cell.data_type == "f" for cell in cells):
|
||||
raise ValueError("invalid or formula plan row")
|
||||
identity = tuple("" if value is None else str(value) for value in values[:8])
|
||||
key = (str(Path(identity[0]).resolve(strict=False)).casefold(),
|
||||
@@ -90,13 +93,15 @@ def read_target_plan(store, task_id: str, path: Path) -> tuple[TargetPlanRow, ..
|
||||
enabled = _excel_bool(enabled)
|
||||
custom_enabled = _excel_bool(custom_enabled)
|
||||
sync_nc = _excel_bool(sync_nc)
|
||||
value_mode = 'custom' if custom_enabled else ('inherit' if legacy else str(values[13] or 'inherit'))
|
||||
if not isinstance(new_part, str) and new_part is not None:
|
||||
raise ValueError("new Part Number must be text")
|
||||
if not isinstance(custom_value, str) and custom_value is not None:
|
||||
raise ValueError("custom Value must be text")
|
||||
by_key[key] = TargetPlanRow(target, enabled, new_part or "", custom_enabled,
|
||||
custom_value or "", sync_nc,
|
||||
original.target_cell if original.new_part_number == (new_part or '') else '')
|
||||
original.target_cell if original.new_part_number == (new_part or '') else '',
|
||||
value_mode)
|
||||
if set(by_key) != set(expected):
|
||||
raise ValueError("plan rows are missing")
|
||||
result = tuple(by_key.values())
|
||||
|
||||
@@ -84,7 +84,7 @@ def read_task_bundle(store, task_id: str, read_control=None) -> TaskBundle:
|
||||
row['uid'], row['refdes'], row['old_part_number'], row['old_value']),
|
||||
bool(row['enabled']), row['new_part_number'], bool(row['custom_value_enabled']),
|
||||
row['custom_value'], bool(row['sync_nc']),
|
||||
row['target_cell'],
|
||||
row['target_cell'], value_mode=row['value_mode'],
|
||||
)
|
||||
targets = records(
|
||||
"SELECT * FROM target_plan WHERE task_id = ? ORDER BY project_path, schematic_name, sheet_number, refdes, uid",
|
||||
|
||||
@@ -238,6 +238,8 @@ class StoreSchema:
|
||||
columns = {row['name'] for row in self._store._connection.execute('PRAGMA table_info(target_plan)')}
|
||||
if 'target_cell' not in columns:
|
||||
self._store._connection.execute("ALTER TABLE target_plan ADD COLUMN target_cell TEXT NOT NULL DEFAULT ''")
|
||||
if 'value_mode' not in columns:
|
||||
self._store._connection.execute("ALTER TABLE target_plan ADD COLUMN value_mode TEXT NOT NULL DEFAULT 'inherit'")
|
||||
|
||||
def _ensure_rule_columns(self) -> None:
|
||||
columns = {row["name"] for row in self._store._connection.execute("PRAGMA table_info(rules)")}
|
||||
|
||||
@@ -86,11 +86,11 @@ class TargetPlanRepository:
|
||||
self._store._connection.execute("DELETE FROM target_plan WHERE task_id = ?", (task_id,))
|
||||
for row in rows:
|
||||
target = row.target
|
||||
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
||||
(task_id, identities.project(target.project_path), target.schematic_name,
|
||||
target.sheet_number, target.uid, target.refdes, target.old_part_number, target.old_value,
|
||||
int(row.enabled), row.new_part_number, int(row.custom_value_enabled), row.custom_value,
|
||||
int(row.sync_nc), row.target_cell))
|
||||
int(row.sync_nc), row.target_cell, row.value_mode))
|
||||
self._store._connection.execute("UPDATE tasks SET target_plan_mode = 1 WHERE task_id = ?", (task_id,))
|
||||
return self._store.list_target_plan(task_id)
|
||||
|
||||
@@ -115,7 +115,7 @@ class TargetPlanRepository:
|
||||
raise TaskCenterError("target_identity", "inventory project is not in task")
|
||||
for row in rows:
|
||||
target = row.target
|
||||
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, '', 0, '', 1, '')""",
|
||||
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, '', 0, '', 1, '', 'inherit')""",
|
||||
(task_id, identities.project(target.project_path), target.schematic_name,
|
||||
target.sheet_number, target.uid, target.refdes, target.old_part_number, target.old_value))
|
||||
self._store._connection.execute("UPDATE tasks SET target_plan_mode = 1 WHERE task_id = ?", (task_id,))
|
||||
@@ -130,6 +130,6 @@ class TargetPlanRepository:
|
||||
ComponentTarget(Path(row["project_path"]), row["schematic_name"], row["sheet_number"],
|
||||
row["uid"], row["refdes"], row["old_part_number"], row["old_value"]),
|
||||
bool(row["enabled"]), row["new_part_number"], bool(row["custom_value_enabled"]),
|
||||
row["custom_value"], bool(row["sync_nc"]), row['target_cell']) for row in rows)
|
||||
row["custom_value"], bool(row["sync_nc"]), row['target_cell'], row['value_mode']) for row in rows)
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import re
|
||||
from .errors import TaskCenterError
|
||||
|
||||
VERSION = 1
|
||||
OPERATIONS = frozenset({'probe', 'runtime_smoke', 'inspect_bom_batch', 'preview_bom_batch',
|
||||
OPERATIONS = frozenset({'probe', 'runtime_smoke', 'inspect_bom_batch', 'preview_bom_batch', 'preview_bom_sources',
|
||||
'collect_target_inventory', 'execute_task_queue', 'execute_quick_run_queue', 'execute_quick_run',
|
||||
'prepare_quick_run', 'prepare_failed_quick_run', 'recover_quick_run', 'cleanup_batch_resources',
|
||||
'target_cell_choices'})
|
||||
|
||||
@@ -13,7 +13,8 @@ def validated_outcome(operation, body):
|
||||
or body.get('status') not in {'ok', 'error'}):
|
||||
raise ValueError('Invalid supervisor business outcome')
|
||||
result = decode(body['result']) if body['business_succeeded'] else None
|
||||
expected = {'inspect_bom_batch': BomImportReport, 'preview_bom_batch': BomBatchPreview,
|
||||
expected = {'inspect_bom_batch': BomImportReport, 'preview_bom_sources': BomImportReport,
|
||||
'preview_bom_batch': BomBatchPreview,
|
||||
'execute_task_queue': TaskQueueResult, 'execute_quick_run_queue': BatchRunResult,
|
||||
'execute_quick_run': QuickRunStepResult, 'recover_quick_run': QuickRunStepResult,
|
||||
'prepare_quick_run': QuickRunResult, 'prepare_failed_quick_run': QuickRunProgress,
|
||||
@@ -43,10 +44,11 @@ def resource_pending_error(operation, result, error, primary_error=None):
|
||||
result = replace(result, preview=None)
|
||||
failure = BatchResourceCleanupError('后台资源仍待清理;报告和已保存核验结果保留。' + (str(error) if error else ''),
|
||||
completed_result=result, primary_error=primary_error, cleanup_error=error)
|
||||
if isinstance(result, BomImportReport):
|
||||
failure.bom_report = result
|
||||
elif getattr(error, 'bom_report', None) is not None:
|
||||
failure.bom_report = replace(error.bom_report, preview=None)
|
||||
for candidate in (result, getattr(error, 'bom_report', None),
|
||||
getattr(primary_error, 'bom_report', None)):
|
||||
if isinstance(candidate, BomImportReport):
|
||||
failure.bom_report = replace(candidate, preview=None)
|
||||
break
|
||||
return failure
|
||||
|
||||
|
||||
|
||||
@@ -54,13 +54,20 @@ class TargetPlanRow:
|
||||
custom_value: str = ""
|
||||
sync_nc: bool = True
|
||||
target_cell: str = ""
|
||||
value_mode: str = "inherit"
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if any(type(value) is not bool for value in (self.enabled, self.custom_value_enabled, self.sync_nc)):
|
||||
raise TaskCenterError("target_plan", "plan switches must be Boolean")
|
||||
if any(not isinstance(value, str) for value in (self.new_part_number, self.custom_value, self.target_cell)):
|
||||
if any(not isinstance(value, str) for value in (self.new_part_number, self.custom_value, self.target_cell, self.value_mode)):
|
||||
raise TaskCenterError("target_plan", "Part Number and Value must be text")
|
||||
if any(character in value for value in (self.new_part_number, self.custom_value, self.target_cell)
|
||||
if self.value_mode not in {"inherit", "preserve", "part_label", "part_number", "custom"}:
|
||||
raise TaskCenterError("value_option", "Value mode must be inherit, preserve, part_label, part_number, or custom")
|
||||
if self.custom_value_enabled and self.value_mode != "custom":
|
||||
object.__setattr__(self, "value_mode", "custom")
|
||||
if self.value_mode == "custom":
|
||||
object.__setattr__(self, "custom_value_enabled", True)
|
||||
if any(character in value for value in (self.new_part_number, self.custom_value, self.target_cell, self.value_mode)
|
||||
for character in "\t\r\n"):
|
||||
raise TaskCenterError("target_plan", "Part Number and Value cannot contain TSV controls")
|
||||
|
||||
@@ -79,16 +86,21 @@ class TargetRule:
|
||||
position: int = 0
|
||||
scope: str = "targets"
|
||||
target_cell: str = ""
|
||||
value_mode: str = "preserve"
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.targets:
|
||||
raise TaskCenterError("empty_target_scope", "target rule needs at least one component")
|
||||
if any(target.old_part_number.casefold() != self.old_part_number.casefold() for target in self.targets):
|
||||
raise TaskCenterError("target_identity", "target old Part Number differs from rule")
|
||||
if self.value_mode not in {"preserve", "part_label", "part_number", "custom"}:
|
||||
raise TaskCenterError("value_option", "compiled Value mode must be concrete")
|
||||
|
||||
|
||||
def compile_target_rules(rows: tuple[TargetPlanRow, ...]) -> tuple[TargetRule, ...]:
|
||||
def compile_target_rules(rows: tuple[TargetPlanRow, ...], *, default_value_mode: str = "preserve") -> tuple[TargetRule, ...]:
|
||||
"""Group selected rows by sheet and replacement options, retaining UID scope."""
|
||||
if default_value_mode not in {"preserve", "part_label", "part_number"}:
|
||||
raise TaskCenterError("value_option", "task default Value mode is invalid")
|
||||
seen: set[tuple[str, str, str, str]] = set()
|
||||
grouped: dict[tuple, list[ComponentTarget]] = {}
|
||||
old_parts: set[str] = set()
|
||||
@@ -109,15 +121,21 @@ def compile_target_rules(rows: tuple[TargetPlanRow, ...]) -> tuple[TargetRule, .
|
||||
old = target.old_part_number.strip()
|
||||
if not new or old.casefold() == new.casefold():
|
||||
raise TaskCenterError("mapping_rule", "source and target Part Numbers must differ")
|
||||
if row.custom_value_enabled and not row.custom_value:
|
||||
value_mode = "custom" if row.custom_value_enabled else row.value_mode
|
||||
if value_mode == "inherit":
|
||||
value_mode = default_value_mode
|
||||
if value_mode not in {"preserve", "part_label", "part_number", "custom"}:
|
||||
raise TaskCenterError("value_option", "Value mode is invalid")
|
||||
if value_mode == "custom" and not row.custom_value:
|
||||
raise TaskCenterError("value_option", "custom Value is empty")
|
||||
old_parts.add(old.casefold())
|
||||
new_parts.add(new.casefold())
|
||||
key = (target_key[0], target.schematic_name,
|
||||
target.sheet_number, old, new, row.custom_value_enabled,
|
||||
row.custom_value if row.custom_value_enabled else "", row.sync_nc, row.target_cell)
|
||||
target.sheet_number, old, new, value_mode,
|
||||
row.custom_value if value_mode == "custom" else "", row.sync_nc, row.target_cell)
|
||||
grouped.setdefault(key, []).append(target)
|
||||
if old_parts & new_parts:
|
||||
raise TaskCenterError("mapping_chain", "chain or cycle replacement is not allowed")
|
||||
return tuple(TargetRule(key[3], key[4], tuple(targets), key[5], key[6], key[7],
|
||||
position=index, target_cell=key[8]) for index, (key, targets) in enumerate(grouped.items()))
|
||||
return tuple(TargetRule(key[3], key[4], tuple(targets), key[5] == "custom", key[6], key[7],
|
||||
position=index, target_cell=key[8], value_mode=key[5])
|
||||
for index, (key, targets) in enumerate(grouped.items()))
|
||||
@@ -6,7 +6,7 @@ import codecs
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import QProcess, QProcessEnvironment, QTimer, QUrl, Signal, Qt
|
||||
from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile, QWebEngineSettings
|
||||
from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile, QWebEngineSettings, QWebEngineScript
|
||||
from PySide6.QtWebEngineWidgets import QWebEngineView
|
||||
from PySide6.QtWidgets import QLabel, QPushButton, QVBoxLayout, QWidget
|
||||
from shiboken6 import isValid
|
||||
@@ -17,9 +17,10 @@ from ...dsh.snapshot import TaskSnapshot
|
||||
|
||||
class LocalHarnessPage(QWebEnginePage):
|
||||
allowed_port = None
|
||||
loading_url = QUrl()
|
||||
|
||||
def acceptNavigationRequest(self, url, kind, is_main_frame):
|
||||
return url.scheme() == 'about' or (url.scheme() == 'http' and url.host() == '127.0.0.1' and url.port() == self.allowed_port)
|
||||
return (url == self.loading_url and url.scheme() == 'file') or url.scheme() == 'about' or (url.scheme() == 'http' and url.host() == '127.0.0.1' and url.port() == self.allowed_port)
|
||||
|
||||
def createWindow(self, _kind):
|
||||
return None
|
||||
@@ -64,10 +65,17 @@ class NativeHarnessView(QWidget):
|
||||
self.profile.setCachePath(str(self.data_root / 'browser/cache'))
|
||||
self.profile.setPersistentCookiesPolicy(QWebEngineProfile.PersistentCookiesPolicy.NoPersistentCookies)
|
||||
self.web.setPage(LocalHarnessPage(self.profile, self.web))
|
||||
bootstrap = QWebEngineScript()
|
||||
bootstrap.setName('Official Harness loading until local runtime is ready')
|
||||
bootstrap.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentCreation)
|
||||
bootstrap.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
|
||||
bootstrap.setSourceCode("if(location.protocol==='file:'){globalThis.__DSH_BOOT_READY__={promise:new Promise(()=>{})};}")
|
||||
self.web.page().scripts().insert(bootstrap)
|
||||
self.web.settings().setAttribute(QWebEngineSettings.WebAttribute.JavascriptCanOpenWindows, False)
|
||||
self.web.settings().setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, False)
|
||||
self.status = QLabel('正在启动 DSH助手…')
|
||||
self.status.setWordWrap(True)
|
||||
self.status.hide()
|
||||
self.retry = QPushButton('重试启动')
|
||||
self.retry.hide()
|
||||
self.retry.clicked.connect(self.start)
|
||||
@@ -140,9 +148,9 @@ class NativeHarnessView(QWidget):
|
||||
return
|
||||
self._stopping = False
|
||||
self._started = False
|
||||
self.status.setText('正在加载 DeepSeek Harness 官方界面…')
|
||||
self.status.show()
|
||||
self.status.hide()
|
||||
self.retry.hide()
|
||||
self._show_official_loading(root)
|
||||
env = QProcessEnvironment()
|
||||
try:
|
||||
for key, value in launch_environment(self.data_root).items():
|
||||
@@ -156,7 +164,16 @@ class NativeHarnessView(QWidget):
|
||||
self.process.setProgram(node)
|
||||
self.process.setArguments([str(root / 'start.mjs')])
|
||||
self.process.start()
|
||||
self.timeout.start(120000)
|
||||
# Cold startup of the official file plugins can exceed two minutes.
|
||||
self.timeout.start(300000)
|
||||
|
||||
def _show_official_loading(self, root: Path):
|
||||
frontend = root / 'node_modules/@deepseek-ai/dsh-web-frontend/dist/index.html'
|
||||
if frontend.is_file():
|
||||
self.web.page().loading_url = QUrl.fromLocalFile(str(frontend.resolve()))
|
||||
self.web.settings().setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, True)
|
||||
self.web.show()
|
||||
self.web.load(self.web.page().loading_url)
|
||||
|
||||
def _reset_output(self):
|
||||
self._buffer = ''
|
||||
@@ -187,6 +204,7 @@ class NativeHarnessView(QWidget):
|
||||
self.web.page().allowed_port = url.port()
|
||||
self.timeout.stop()
|
||||
self.status.hide()
|
||||
self.web.settings().setAttribute(QWebEngineSettings.WebAttribute.LocalContentCanAccessFileUrls, False)
|
||||
self.web.load(url)
|
||||
self.ready.emit()
|
||||
queued, self._diagnosis_queue = self._diagnosis_queue, []
|
||||
@@ -220,6 +238,7 @@ class NativeHarnessView(QWidget):
|
||||
self.timeout.stop()
|
||||
self.status.setText(text)
|
||||
self.status.show()
|
||||
self.web.hide()
|
||||
self.retry.show()
|
||||
|
||||
def _finished(self, *_):
|
||||
|
||||
@@ -637,12 +637,12 @@ class ProjectPlanPanel(QWidget):
|
||||
def import_bom(self):
|
||||
if not self.task_id:
|
||||
return
|
||||
path, _ = QFileDialog.getOpenFileName(self, '导入 BOM 模板', '', 'BOM Excel (*.xlsx)')
|
||||
if path:
|
||||
paths, _ = QFileDialog.getOpenFileNames(self, '导入 BOM 模板(可多选)', '', 'BOM Excel (*.xlsx)')
|
||||
if paths:
|
||||
task_id = self.task_id
|
||||
scope = self._report_scope(task_id)
|
||||
self._dispatch_cancellable(lambda stop, progress, phase: self.window.service.inspect_bom_batch(
|
||||
task_id, Path(path), stop_event=stop, on_progress=progress, on_phase=phase),
|
||||
self._dispatch_cancellable(lambda stop, progress, phase: self.window.service.preview_bom_sources(
|
||||
task_id, tuple(Path(path) for path in paths), stop_event=stop, on_progress=progress, on_phase=phase),
|
||||
lambda report: self._received_bom_report(report, scope=scope), self.import_bom_button,
|
||||
bom_scope=scope)
|
||||
|
||||
@@ -697,7 +697,10 @@ class ProjectPlanPanel(QWidget):
|
||||
f'共 {report.total_projects} 个工程,通过 {report.passed_projects} 个、'
|
||||
f'失败 {report.failed_projects} 个、未检查 {report.unchecked_projects} 个。')
|
||||
details = [summary, *report.errors]
|
||||
details.extend(f'Excel 第 {row.excel_row} 行 · {row.project_name or "未指定工程"} '
|
||||
if report.source_map_path is not None:
|
||||
details.append(f'导入来源记录:{report.source_map_path}')
|
||||
details.extend(f'{Path(row.source_file).name + " · " if row.source_file else ""}'
|
||||
f'Excel 第 {row.source_row if row.source_row is not None else row.excel_row} 行 · {row.project_name or "未指定工程"} '
|
||||
f'({row.project_path or "无工程路径"})· '
|
||||
f'{ {"passed": "通过", "failed": "失败", "unchecked": "未检查"}[row.status]}:{row.reason}'
|
||||
for row in report.rows)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
"""Model-based editable target plan; filtering never changes saved identities."""
|
||||
from dataclasses import replace
|
||||
from PySide6.QtCore import QAbstractTableModel, QModelIndex, QSortFilterProxyModel, Qt, Signal,QPersistentModelIndex
|
||||
from PySide6.QtWidgets import QAbstractItemView, QHeaderView, QTableView,QLabel,QStyledItemDelegate,QLineEdit,QAbstractItemDelegate
|
||||
from PySide6.QtWidgets import QAbstractItemView, QHeaderView, QTableView,QLabel,QStyledItemDelegate,QLineEdit,QAbstractItemDelegate,QComboBox
|
||||
|
||||
HEADERS = ('替换', '原理图', '图页', '位号', '旧料号', '原 Value', '新料号', '自定义 Value', '自定义内容', '同步 NC')
|
||||
HEADERS = ('替换', '原理图', '图页', '位号', '旧料号', '原 Value', '新料号', '自定义 Value', '自定义内容', '同步 NC', 'Value 来源')
|
||||
CHECK_FIELDS = {0: 'enabled', 7: 'custom_value_enabled', 9: 'sync_nc'}
|
||||
TEXT_FIELDS = {6: 'new_part_number', 8: 'custom_value'}
|
||||
VALUE_MODE_COLUMN = 10
|
||||
VALUE_MODES = ('inherit', 'preserve', 'part_label', 'part_number', 'custom')
|
||||
|
||||
class TargetPlanModel(QAbstractTableModel):
|
||||
changed = Signal()
|
||||
@@ -38,7 +40,8 @@ class TargetPlanModel(QAbstractTableModel):
|
||||
'原理图名称,只读。','图页编号,只读。','器件位号,只读;完整 UID 在单元格提示中。',
|
||||
'当前工程的原料号,只读。','原始 Value,只读。','可编辑的新料号;修改后需要保存计划。',
|
||||
'启用本行自定义 Value,优先于工程级 Value 策略。',
|
||||
'本行自定义 Value 内容。','按本行配置是否同步 NC。')[section]
|
||||
'本行自定义 Value 内容。','按本行配置是否同步 NC。',
|
||||
'inherit 跟随任务默认;其余选项显式指定本行的 Value 来源。')[section]
|
||||
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
|
||||
if not index.isValid():
|
||||
return None
|
||||
@@ -49,7 +52,7 @@ class TargetPlanModel(QAbstractTableModel):
|
||||
return Qt.CheckState.Checked if getattr(row, CHECK_FIELDS[col]) else Qt.CheckState.Unchecked
|
||||
if role in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole):
|
||||
return ('', t.schematic_name, t.sheet_number, t.refdes, t.old_part_number,
|
||||
t.old_value, row.new_part_number, '', row.custom_value, '')[col]
|
||||
t.old_value, row.new_part_number, '', row.custom_value, '', row.value_mode)[col]
|
||||
if role == Qt.ItemDataRole.ToolTipRole:
|
||||
content=self.data(index,Qt.ItemDataRole.DisplayRole)
|
||||
if col in CHECK_FIELDS:content='开启'if getattr(row,CHECK_FIELDS[col])else'关闭'
|
||||
@@ -62,7 +65,7 @@ class TargetPlanModel(QAbstractTableModel):
|
||||
if self.editable:
|
||||
if index.column() in CHECK_FIELDS:
|
||||
flags |= Qt.ItemFlag.ItemIsUserCheckable
|
||||
elif index.column() in TEXT_FIELDS:
|
||||
elif index.column() in TEXT_FIELDS or index.column() == VALUE_MODE_COLUMN:
|
||||
flags |= Qt.ItemFlag.ItemIsEditable
|
||||
return flags
|
||||
def setData(self, index, value, role=Qt.ItemDataRole.EditRole):
|
||||
@@ -71,8 +74,13 @@ class TargetPlanModel(QAbstractTableModel):
|
||||
col = index.column()
|
||||
if role == Qt.ItemDataRole.CheckStateRole and col in CHECK_FIELDS:
|
||||
change = {CHECK_FIELDS[col]: value in (Qt.CheckState.Checked, Qt.CheckState.Checked.value)}
|
||||
if col == 7:
|
||||
change['value_mode'] = 'custom' if change['custom_value_enabled'] else 'inherit'
|
||||
elif role == Qt.ItemDataRole.EditRole and col in TEXT_FIELDS:
|
||||
change = {TEXT_FIELDS[col]: str(value).strip()}
|
||||
elif role == Qt.ItemDataRole.EditRole and col == VALUE_MODE_COLUMN:
|
||||
mode = str(value).strip()
|
||||
change = {'value_mode': mode, 'custom_value_enabled': mode == 'custom'}
|
||||
else:
|
||||
return False
|
||||
number = index.row()
|
||||
@@ -84,7 +92,11 @@ class TargetPlanModel(QAbstractTableModel):
|
||||
self.enabled_count += int(updated.enabled) - int(self.rows[number].enabled)
|
||||
self.rows[number] = updated
|
||||
self.search_text[number] = self.searchable(updated)
|
||||
self.dataChanged.emit(index, index, [role, Qt.ItemDataRole.DisplayRole])
|
||||
roles = [role, Qt.ItemDataRole.DisplayRole]
|
||||
if 'custom_value_enabled' in change:
|
||||
roles.append(Qt.ItemDataRole.CheckStateRole)
|
||||
first, last = (self.index(number, 7), self.index(number, VALUE_MODE_COLUMN)) if 'value_mode' in change else (index, index)
|
||||
self.dataChanged.emit(first, last, roles)
|
||||
self.changed.emit()
|
||||
return True
|
||||
|
||||
@@ -92,10 +104,14 @@ class TargetPlanModel(QAbstractTableModel):
|
||||
"""Notify the view and controller once for a bulk operation."""
|
||||
if not self.editable:
|
||||
return
|
||||
field = CHECK_FIELDS[column] if role == Qt.ItemDataRole.CheckStateRole else TEXT_FIELDS[column]
|
||||
field = CHECK_FIELDS[column] if role == Qt.ItemDataRole.CheckStateRole else ('value_mode' if column == VALUE_MODE_COLUMN else TEXT_FIELDS[column])
|
||||
changed = []
|
||||
for number in numbers:
|
||||
changes = {field: value}
|
||||
if field == 'value_mode':
|
||||
changes['custom_value_enabled'] = str(value).strip() == 'custom'
|
||||
elif field == 'custom_value_enabled':
|
||||
changes['value_mode'] = 'custom' if value else 'inherit'
|
||||
if field == 'new_part_number' and value != self.rows[number].new_part_number:
|
||||
changes['target_cell'] = ''
|
||||
updated = replace(self.rows[number], **changes)
|
||||
@@ -105,8 +121,11 @@ class TargetPlanModel(QAbstractTableModel):
|
||||
self.search_text[number] = self.searchable(updated)
|
||||
changed.append(number)
|
||||
if changed:
|
||||
self.dataChanged.emit(self.index(min(changed), column), self.index(max(changed), column),
|
||||
[role, Qt.ItemDataRole.DisplayRole])
|
||||
roles = [role, Qt.ItemDataRole.DisplayRole]
|
||||
if field in {'value_mode', 'custom_value_enabled'}:
|
||||
roles.append(Qt.ItemDataRole.CheckStateRole)
|
||||
first_col, last_col = (7, VALUE_MODE_COLUMN) if field in {'value_mode', 'custom_value_enabled'} else (column, column)
|
||||
self.dataChanged.emit(self.index(min(changed), first_col), self.index(max(changed), last_col), roles)
|
||||
self.changed.emit()
|
||||
|
||||
class TargetFilter(QSortFilterProxyModel):
|
||||
@@ -121,6 +140,13 @@ class TargetFilter(QSortFilterProxyModel):
|
||||
class PlanEditorDelegate(QStyledItemDelegate):
|
||||
"""Expose uncommitted editor text without changing Escape/cancel semantics."""
|
||||
def createEditor(self,parent,option,index):
|
||||
if index.column() == VALUE_MODE_COLUMN:
|
||||
editor = QComboBox(parent)
|
||||
editor.addItems(VALUE_MODES)
|
||||
view = self.parent()
|
||||
view._active_editor = (editor, QPersistentModelIndex(index))
|
||||
editor.currentTextChanged.connect(lambda _: view.editor_activity.emit())
|
||||
return editor
|
||||
editor=super().createEditor(parent,option,index)
|
||||
if isinstance(editor,QLineEdit):
|
||||
view=self.parent()
|
||||
@@ -169,7 +195,7 @@ class TargetTable(QTableView):
|
||||
header.setStretchLastSection(False)
|
||||
header.setMinimumSectionSize(48)
|
||||
header.setDefaultAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
|
||||
for col, width in enumerate((55, 90, 50, 70, 145, 115, 145, 90, 125, 75)):
|
||||
for col, width in enumerate((55, 90, 50, 70, 145, 115, 145, 90, 125, 75, 105)):
|
||||
self.setColumnWidth(col, width)
|
||||
self.dirty = False
|
||||
self._next_match = -1
|
||||
@@ -191,7 +217,8 @@ class TargetTable(QTableView):
|
||||
def has_pending_editor(self):
|
||||
if not self._active_editor:return False
|
||||
editor,index=self._active_editor
|
||||
return index.isValid()and editor.isVisible()and editor.text()!=str(index.data(Qt.ItemDataRole.EditRole)or'')
|
||||
value = editor.currentText() if isinstance(editor, QComboBox) else editor.text()
|
||||
return index.isValid()and editor.isVisible()and value!=str(index.data(Qt.ItemDataRole.EditRole)or'')
|
||||
def cancel_editor(self):
|
||||
if self._active_editor:
|
||||
self.itemDelegate().closeEditor.emit(self._active_editor[0],QAbstractItemDelegate.EndEditHint.RevertModelCache)
|
||||
|
||||
@@ -0,0 +1,204 @@
|
||||
"""Safe multi-file BOM loading and project routing for workspace imports."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path, PureWindowsPath
|
||||
|
||||
from openpyxl import load_workbook
|
||||
|
||||
from .bom_plan import BOM_HEADERS, _normal_headers, _part_number_text, write_bom_template
|
||||
from .errors import TaskCenterError
|
||||
|
||||
|
||||
def _path_key(value) -> str:
|
||||
return str(value or '').replace('\\', '/').strip().rstrip('/').casefold()
|
||||
|
||||
|
||||
def _project_name(value) -> str:
|
||||
raw = _path_key(value)
|
||||
return raw.rsplit('/', 1)[-1]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SourceRow:
|
||||
source: Path
|
||||
excel_row: int
|
||||
values: tuple
|
||||
value_mode: str = 'inherit'
|
||||
|
||||
@property
|
||||
def project_name(self):
|
||||
return str(self.values[0] or '').strip()
|
||||
|
||||
@property
|
||||
def project_path(self):
|
||||
return str(self.values[1] or '').strip()
|
||||
|
||||
@property
|
||||
def project_key(self):
|
||||
return (_path_key(self.project_path or self.source), _path_key(self.project_name))
|
||||
|
||||
@property
|
||||
def refdes(self):
|
||||
return str(self.values[4] or '').strip()
|
||||
|
||||
|
||||
def _selected_xlsx(sources):
|
||||
paths = set()
|
||||
for source in sources:
|
||||
source = Path(source)
|
||||
candidates = source.rglob('*.xlsx') if source.is_dir() else (source,)
|
||||
for path in candidates:
|
||||
if path.suffix.casefold() == '.xlsx' and not path.name.startswith('~$'):
|
||||
paths.add(path.resolve())
|
||||
return tuple(sorted(paths, key=lambda path: str(path).casefold()))
|
||||
|
||||
|
||||
def load_sources(sources, *, stop_event=None, on_progress=None):
|
||||
"""Read every selected workbook before any CAD operation starts."""
|
||||
from .operation_control import check_cancelled
|
||||
|
||||
paths = _selected_xlsx(sources)
|
||||
if not paths:
|
||||
raise TaskCenterError('bom_import', '未找到可导入的 .xlsx 替换清单')
|
||||
result = []
|
||||
for number, path in enumerate(paths, 1):
|
||||
check_cancelled(stop_event)
|
||||
if on_progress is not None:
|
||||
on_progress(f'正在读取 BOM {number}/{len(paths)}:{path.name}')
|
||||
workbook = None
|
||||
try:
|
||||
workbook = load_workbook(path, read_only=True, data_only=False)
|
||||
if len(workbook.worksheets) != 1:
|
||||
raise ValueError('BOM 只能包含一个工作表')
|
||||
rows = iter(workbook.active.iter_rows())
|
||||
header_cells = next(rows, ())
|
||||
headers = _normal_headers(tuple(cell.value for cell in header_cells))
|
||||
names = tuple(name for name in headers if name)
|
||||
if len(names) != len(set(names)):
|
||||
raise ValueError('BOM 列名重复')
|
||||
required = {'old_part_number', 'new_part_number', 'refdes'}
|
||||
if not required <= set(names):
|
||||
raise ValueError('BOM 必须包含旧料号、新料号和位号列')
|
||||
if any(cell.data_type == 'f' for cell in header_cells):
|
||||
raise ValueError('BOM 表头不接受公式')
|
||||
positions = {name: index for index, name in enumerate(headers) if name}
|
||||
for excel_row, cells in enumerate(rows, 2):
|
||||
check_cancelled(stop_event)
|
||||
if any(cell.data_type == 'f' for cell in cells):
|
||||
raise ValueError(f'Excel 第 {excel_row} 行不接受公式')
|
||||
values = tuple(
|
||||
cells[positions[name]].value if name in positions and positions[name] < len(cells) else ''
|
||||
for name in BOM_HEADERS
|
||||
)
|
||||
if not any(values[index] not in (None, '') for index in (2, 3, 4, 9)):
|
||||
continue
|
||||
project_path = str(values[1] or '').strip()
|
||||
if project_path and not (Path(project_path).is_absolute() or PureWindowsPath(project_path).is_absolute()):
|
||||
raise ValueError(f'Excel 第 {excel_row} 行工程路径必须是完整路径')
|
||||
try:
|
||||
_part_number_text(cells, positions, 'old_part_number', '旧料号')
|
||||
_part_number_text(cells, positions, 'new_part_number', '新料号')
|
||||
except ValueError as error:
|
||||
raise ValueError(f'Excel 第 {excel_row} 行:{error}') from error
|
||||
default_mode = 'custom' if str(values[9] or '').strip() else 'inherit'
|
||||
value_mode = str(cells[positions['value_mode']].value or default_mode).strip() if 'value_mode' in positions else default_mode
|
||||
if value_mode not in {'inherit', 'preserve', 'part_label', 'part_number', 'custom'}:
|
||||
raise ValueError(f'Excel 第 {excel_row} 行 Value 来源无效')
|
||||
result.append(SourceRow(path, excel_row, values, value_mode))
|
||||
except TaskCenterError:
|
||||
raise
|
||||
except Exception as error:
|
||||
raise TaskCenterError('bom_import', f'{path.name}:{error}') from error
|
||||
finally:
|
||||
if workbook is not None:
|
||||
workbook.close()
|
||||
if not result:
|
||||
raise TaskCenterError('bom_import', '替换清单尚未填写旧料号、新料号和位号')
|
||||
return tuple(result)
|
||||
|
||||
|
||||
def _exact_name_matches(project_name, paths):
|
||||
name = _project_name(project_name)
|
||||
if not name:
|
||||
return ()
|
||||
return tuple(path for path in paths if _project_name(path) == name)
|
||||
|
||||
|
||||
def resolve_project(project_path, project_name, paths, *, project_names=None):
|
||||
"""Resolve exact path first, then an exact unique migrated project name."""
|
||||
paths = tuple(Path(path) for path in paths)
|
||||
raw = _path_key(project_path)
|
||||
exact_path = tuple(path for path in paths if _path_key(path.resolve()) == raw) if raw else ()
|
||||
if len(exact_path) == 1:
|
||||
return exact_path[0]
|
||||
if project_name:
|
||||
aliases = tuple(path for path in paths
|
||||
if _path_key((project_names or {}).get(path)) == _path_key(project_name))
|
||||
# A project_name containing directories is stronger than a basename.
|
||||
name_key = _path_key(project_name)
|
||||
suffix = tuple(path for path in paths if _path_key(path).endswith('/' + name_key.lstrip('/')))
|
||||
matches = tuple(dict.fromkeys((*aliases, *suffix)))
|
||||
if len(matches) == 1:
|
||||
return matches[0]
|
||||
if len(matches) > 1:
|
||||
return None
|
||||
matches = _exact_name_matches(project_name or project_path, paths)
|
||||
return matches[0] if len(matches) == 1 else None
|
||||
|
||||
|
||||
def route_sources(rows, paths, *, manual=None, single=False, project_names=None):
|
||||
"""Route source rows; stale paths are allowed only on exact unique names."""
|
||||
paths, manual = tuple(Path(path) for path in paths), manual or {}
|
||||
routes = {}
|
||||
for row in rows:
|
||||
key = row.project_key
|
||||
if key in routes:
|
||||
continue
|
||||
chosen = manual.get(key)
|
||||
if chosen is not None:
|
||||
chosen = Path(chosen)
|
||||
if chosen not in paths:
|
||||
raise TaskCenterError('project_binding', '人工选择的工程不在本次工程列表中')
|
||||
else:
|
||||
chosen = resolve_project(row.project_path, row.project_name, paths, project_names=project_names)
|
||||
if chosen is None and single and len(paths) == 1 and not row.project_path:
|
||||
chosen = paths[0]
|
||||
routes[key] = chosen
|
||||
return routes
|
||||
|
||||
|
||||
def write_routed_bom(destination, rows, routes, *, project_names=None):
|
||||
"""Write canonical project paths while preserving row order and values."""
|
||||
records = []
|
||||
source_map = []
|
||||
for row in rows:
|
||||
target = routes.get(row.project_key)
|
||||
if target is None:
|
||||
raise TaskCenterError('project_binding', f'请先确认工程匹配:{row.project_key}')
|
||||
record = dict(zip(BOM_HEADERS, row.values))
|
||||
record['project_path'] = str(Path(target).resolve())
|
||||
record['value_mode'] = row.value_mode
|
||||
records.append(record)
|
||||
if _path_key(row.project_path) == _path_key(target.resolve()):
|
||||
reason = 'exact_path'
|
||||
elif row.project_name and _path_key(row.project_name) == _path_key((project_names or {}).get(target)):
|
||||
reason = 'migrated_task_name' if row.project_path else 'task_name'
|
||||
elif row.project_name and _path_key(target).endswith('/' + _path_key(row.project_name).lstrip('/')):
|
||||
reason = 'migrated_exact_name' if row.project_path else 'exact_name'
|
||||
elif row.project_name and _project_name(row.project_name) == _project_name(target):
|
||||
reason = 'migrated_basename' if row.project_path else 'basename'
|
||||
elif row.project_path:
|
||||
reason = 'migrated_basename'
|
||||
else:
|
||||
reason = 'single_project'
|
||||
source_map.append({'source_file': str(row.source), 'source_row': row.excel_row,
|
||||
'original_project_path': row.project_path,
|
||||
'project_name': row.project_name,
|
||||
'matched_project_path': str(Path(target).resolve()),
|
||||
'match_reason': reason})
|
||||
destination = Path(destination)
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
result = write_bom_template(destination, records)
|
||||
from .batch_resources import write_record
|
||||
write_record(destination.with_name('source-map.json'), source_map)
|
||||
return result
|
||||
@@ -0,0 +1,225 @@
|
||||
"""PR21 routing, source evidence and operation-boundary regressions; no CAD."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import zipfile
|
||||
|
||||
from openpyxl import Workbook, load_workbook
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center.bom_import_report import BomImportReport
|
||||
from dx_productivity.trial_task_center.bom_plan import BOM_HEADERS
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.target_plan import ComponentTarget
|
||||
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources, write_routed_bom
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def case(tmp_path):
|
||||
root = tmp_path / 'projects'
|
||||
for name in ('BoardA', 'BoardB'):
|
||||
project = root / name / (name + '.prj')
|
||||
project.parent.mkdir(parents=True)
|
||||
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
tasks = service.create_project_tasks(root)
|
||||
paths = tuple(service.projects(t.task_id)[0].path for t in tasks)
|
||||
for task, path in zip(tasks, paths):
|
||||
store.record_target_inventory(task.task_id, (
|
||||
ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF'),))
|
||||
yield SimpleNamespace(service=service, store=store, tasks=tasks, paths=paths,
|
||||
runtime=SimpleNamespace(bind_logs=lambda *_: None), root=tmp_path / 'data')
|
||||
store.close()
|
||||
|
||||
|
||||
def source(path, *, name='BoardA.prj', project='Z:/old/BoardA.prj', row=2):
|
||||
book = Workbook()
|
||||
sheet = book.active
|
||||
sheet.append(BOM_HEADERS)
|
||||
for _ in range(2, row):
|
||||
sheet.append([None] * len(BOM_HEADERS))
|
||||
sheet.append([name, project, 'OLD', 'NEW', 'R1', '', '', '', True, '', True])
|
||||
book.save(path)
|
||||
book.close()
|
||||
return path
|
||||
|
||||
|
||||
def test_renamed_task_template_routes_stale_path_in_multi_project_batch(case, tmp_path):
|
||||
task = case.tasks[0]
|
||||
case.service.update_task(task.task_id, name='音频板', note='')
|
||||
path = case.service.export_bom_template(task.task_id, tmp_path / 'renamed.xlsx')
|
||||
book = load_workbook(path)
|
||||
sheet = book.active
|
||||
sheet['B2'] = 'Z:/moved/BoardA.prj'
|
||||
sheet['C2'], sheet['D2'], sheet['E2'] = 'OLD', 'NEW', 'R1'
|
||||
book.save(path)
|
||||
book.close()
|
||||
report = case.service.preview_bom_sources(task.task_id, (path,), runtime=case.runtime)
|
||||
assert report.passed_rows == 1 and report.preview
|
||||
assert report.preview.projects[0].project_path == case.paths[0]
|
||||
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == 'migrated_task_name'
|
||||
|
||||
|
||||
def test_single_project_blank_path_keeps_legacy_display_name_binding(case, tmp_path):
|
||||
task_id = case.tasks[0].task_id
|
||||
case.store._connection.execute('UPDATE tasks SET group_id = NULL WHERE task_id = ?', (task_id,))
|
||||
path = source(tmp_path / 'blank-path.xlsx', name='display name from another machine', project='')
|
||||
report = case.service.preview_bom_sources(task_id, (path,), runtime=case.runtime)
|
||||
assert report.passed_rows == 1 and report.preview
|
||||
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == 'single_project'
|
||||
|
||||
|
||||
def test_duplicate_messages_use_both_original_workbook_coordinates(case, tmp_path):
|
||||
first = source(tmp_path / 'first.xlsx')
|
||||
second = source(tmp_path / 'second.xlsx', row=5)
|
||||
report = case.service.preview_bom_sources(case.tasks[0].task_id, (first, second), runtime=case.runtime)
|
||||
assert report.preview is None and report.failed_rows == 2
|
||||
assert [row.source_row for row in report.rows] == [2, 5]
|
||||
for row in report.rows:
|
||||
assert 'first.xlsx 第 2 行' in row.reason
|
||||
assert 'second.xlsx 第 5 行' in row.reason
|
||||
assert 'Excel 行 2, 3' not in row.reason
|
||||
|
||||
|
||||
def test_import_evidence_is_referenced_exportable_and_discards_bom_copy(case, tmp_path):
|
||||
from dx_productivity.trial_task_center.diagnostics_bundle import export_diagnostic_bundle
|
||||
path = source(tmp_path / 'original.xlsx')
|
||||
report = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
|
||||
mapping = report.source_map_path
|
||||
assert mapping.is_file()
|
||||
assert not mapping.with_name('normalized-bom.xlsx').exists()
|
||||
task_events = case.root / 'tasks' / case.tasks[0].task_id / 'events.jsonl'
|
||||
assert any(json.loads(line).get('details', {}).get('source_map_path') == str(mapping)
|
||||
for line in task_events.read_text(encoding='utf-8').splitlines())
|
||||
bundle = export_diagnostic_bundle(case.root, tmp_path / 'diagnostics.zip')
|
||||
with zipfile.ZipFile(bundle) as archive:
|
||||
assert mapping.relative_to(case.root).as_posix() in archive.namelist()
|
||||
assert not any(name.endswith('.xlsx') for name in archive.namelist())
|
||||
|
||||
|
||||
def test_source_map_has_bounded_existing_history_retention(case, tmp_path):
|
||||
from dx_productivity.trial_task_center.retention import maintain_quick_run_storage
|
||||
path = source(tmp_path / 'source.xlsx')
|
||||
first = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
|
||||
second = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
|
||||
task_root = case.root / 'tasks' / case.tasks[0].task_id
|
||||
maintain_quick_run_storage(task_root, max_entries=1, min_entries=0)
|
||||
assert not first.source_map_path.exists()
|
||||
assert second.source_map_path.is_file()
|
||||
|
||||
|
||||
def test_relative_project_path_is_rejected_before_normalization(tmp_path):
|
||||
path = source(tmp_path / 'relative.xlsx', project='sub/BoardA.prj')
|
||||
with pytest.raises(TaskCenterError, match='完整路径'):
|
||||
load_sources((path,))
|
||||
|
||||
|
||||
def test_failed_source_map_replace_preserves_previous_valid_json(tmp_path, monkeypatch):
|
||||
import dx_productivity.trial_task_center.batch_resources as resources
|
||||
path = source(tmp_path / 'source.xlsx')
|
||||
rows = load_sources((path,))
|
||||
routes = route_sources(rows, (tmp_path / 'BoardA.prj',))
|
||||
mapping = tmp_path / 'source-map.json'
|
||||
mapping.write_text('[{"previous":true}]', encoding='utf-8')
|
||||
real_replace = resources.os.replace
|
||||
def deny(source_path, destination_path):
|
||||
if Path(destination_path).name == 'source-map.json':
|
||||
raise OSError('controlled disk write failure')
|
||||
return real_replace(source_path, destination_path)
|
||||
monkeypatch.setattr(resources.os, 'replace', deny)
|
||||
with pytest.raises(OSError, match='controlled disk'):
|
||||
write_routed_bom(tmp_path / 'normalized.xlsx', rows, routes)
|
||||
assert json.loads(mapping.read_text(encoding='utf-8')) == [{'previous': True}]
|
||||
assert not list(tmp_path.glob('source-map.json.*.tmp'))
|
||||
|
||||
|
||||
def test_supervised_import_dispatches_before_reading_any_source(case, tmp_path, monkeypatch):
|
||||
import dx_productivity.trial_task_center.workspace_bom as workspace
|
||||
case.service._supervised = True
|
||||
path = source(tmp_path / 'source.xlsx')
|
||||
monkeypatch.setattr(workspace, 'load_sources', lambda *_a, **_kw: pytest.fail('source read escaped supervisor'))
|
||||
def supervise(operation, arguments, **controls):
|
||||
assert operation == 'preview_bom_sources'
|
||||
assert arguments['sources'] == (path,)
|
||||
return BomImportReport((), ())
|
||||
monkeypatch.setattr(case.service, '_supervise', supervise)
|
||||
assert case.service.preview_bom_sources(case.tasks[0].task_id, (path,)).total_rows == 0
|
||||
|
||||
|
||||
def test_each_source_reports_read_progress_before_inspection(case, tmp_path, monkeypatch):
|
||||
import dx_productivity.trial_task_center.bom_import_report as inspection
|
||||
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
|
||||
paths = (source(tmp_path / 'first.xlsx'), source(tmp_path / 'second.xlsx', name='BoardB.prj', project='Z:/old/BoardB.prj'))
|
||||
progress, phases = [], []
|
||||
original = inspection.inspect_batch
|
||||
def inspect(*args, **kwargs):
|
||||
assert any('first.xlsx' in message for message in progress)
|
||||
assert any('second.xlsx' in message for message in progress)
|
||||
assert phases[0] is BomImportPhase.READING
|
||||
return original(*args, **kwargs)
|
||||
monkeypatch.setattr(inspection, 'inspect_batch', inspect)
|
||||
report = case.service.preview_bom_sources(case.tasks[0].task_id, paths, runtime=case.runtime,
|
||||
on_progress=progress.append, on_phase=lambda phase, _: phases.append(phase))
|
||||
assert report.passed_rows == 2
|
||||
|
||||
|
||||
def test_single_lease_covers_source_read_and_inspection(case, tmp_path, monkeypatch):
|
||||
import dx_productivity.trial_task_center.workspace_bom as workspace
|
||||
import dx_productivity.trial_task_center.bom_import_report as inspection
|
||||
from dx_productivity.trial_task_center.service import _quick_run_lease
|
||||
|
||||
checked = []
|
||||
operations = []
|
||||
def assert_busy():
|
||||
with pytest.raises(TaskCenterError) as failure:
|
||||
with _quick_run_lease(case.root):
|
||||
pytest.fail('import operation released its lease')
|
||||
assert failure.value.code == 'quick_run_busy'
|
||||
|
||||
original_load, original_inspect = workspace.load_sources, inspection.inspect_batch
|
||||
def load(*args, **kwargs):
|
||||
assert_busy()
|
||||
checked.append('read')
|
||||
return original_load(*args, **kwargs)
|
||||
def inspect(*args, **kwargs):
|
||||
assert_busy()
|
||||
checked.append('inspect')
|
||||
return original_inspect(*args, **kwargs)
|
||||
monkeypatch.setattr(workspace, 'load_sources', load)
|
||||
monkeypatch.setattr(inspection, 'inspect_batch', inspect)
|
||||
original_operation = case.service._idle_quick_run_operation
|
||||
def operation(name, task_id, action, **kwargs):
|
||||
operations.append(name)
|
||||
return original_operation(name, task_id, action, **kwargs)
|
||||
monkeypatch.setattr(case.service, '_idle_quick_run_operation', operation)
|
||||
report = case.service.preview_bom_sources(case.tasks[0].task_id,
|
||||
(source(tmp_path / 'source.xlsx'),), runtime=case.runtime)
|
||||
assert report.passed_rows == 1 and checked == ['read', 'inspect']
|
||||
assert operations == ['preview_bom_sources']
|
||||
with _quick_run_lease(case.root):
|
||||
pass
|
||||
|
||||
|
||||
def test_duplicate_task_alias_blocks_but_exact_path_wins(case, tmp_path):
|
||||
from dx_productivity.trial_task_center.workspace_bom import resolve_project
|
||||
names = dict.fromkeys(case.paths, 'same display name')
|
||||
assert resolve_project('Z:/old/BoardA.prj', 'same display name', case.paths, project_names=names) is None
|
||||
assert resolve_project(str(case.paths[0]), 'same display name', case.paths, project_names=names) == case.paths[0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('name,project,reason', [
|
||||
('BoardA.prj', '', 'exact_name'),
|
||||
('', '', 'single_project'),
|
||||
('BoardA.prj', 'Z:/old/BoardA.prj', 'migrated_exact_name'),
|
||||
])
|
||||
def test_source_map_distinguishes_name_migration_from_blank_fallback(case, tmp_path, name, project, reason):
|
||||
task_id = case.tasks[0].task_id
|
||||
case.store._connection.execute('UPDATE tasks SET group_id = NULL WHERE task_id = ?', (task_id,))
|
||||
path = source(tmp_path / 'source.xlsx', name=name, project=project)
|
||||
report = case.service.preview_bom_sources(task_id, (path,), runtime=case.runtime)
|
||||
assert report.passed_rows == 1
|
||||
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == reason
|
||||
@@ -0,0 +1,81 @@
|
||||
"""Portable XLSX and item Value contracts; no CAD, Qt, or Windows runtime."""
|
||||
|
||||
import pytest
|
||||
from openpyxl import load_workbook
|
||||
|
||||
from dx_productivity.trial_task_center.bom_batch import export_template
|
||||
from dx_productivity.trial_task_center.bom_plan import (
|
||||
BOM_HEADERS, match_bom_records, read_bom_records, write_bom_template,
|
||||
)
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow, compile_target_rules
|
||||
|
||||
|
||||
@pytest.mark.parametrize('with_task', [False, True])
|
||||
def test_user_template_exposes_editable_value_source_without_inventory(tmp_path, with_task):
|
||||
store = TaskStore(tmp_path / 'tasks.db')
|
||||
try:
|
||||
task_id = None
|
||||
if with_task:
|
||||
task_id = store.create_task('Board').task_id
|
||||
store.sync_projects(task_id, tmp_path, (tmp_path / 'Board.prj',))
|
||||
path = export_template(store, task_id, tmp_path / 'template.xlsx')
|
||||
book = load_workbook(path)
|
||||
try:
|
||||
sheet = book.active
|
||||
assert tuple(cell.value for cell in sheet[1]) == BOM_HEADERS + ('value_mode',)
|
||||
header = sheet['L1']
|
||||
assert header.font.bold and header.font.color.rgb == '00FFFFFF'
|
||||
assert header.fill.patternType == 'solid' and header.fill.fgColor.rgb == '001D3557'
|
||||
assert header.comment and all(mode in header.comment.text for mode in (
|
||||
'inherit', 'preserve', 'part_label', 'part_number', 'custom'))
|
||||
assert any(validation.formula1 == '"inherit,preserve,part_label,part_number,custom"'
|
||||
and 'L2' in validation for validation in sheet.data_validations.dataValidation)
|
||||
assert sheet['C2'].number_format == sheet['D2'].number_format == '@'
|
||||
if with_task:
|
||||
assert sheet['B2'].value == str(tmp_path / 'Board.prj')
|
||||
assert not store.list_target_plan(task_id)
|
||||
finally:
|
||||
book.close()
|
||||
assert read_bom_records(path) == ()
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
def test_legacy_writer_keeps_eleven_columns_and_custom_value_inference(tmp_path):
|
||||
path = write_bom_template(tmp_path / 'legacy.xlsx', ({
|
||||
'old_part_number': '001', 'new_part_number': '002', 'refdes': 'R1', 'custom_value': '47k',
|
||||
},))
|
||||
book = load_workbook(path)
|
||||
try:
|
||||
assert len(BOM_HEADERS) == 11
|
||||
assert tuple(cell.value for cell in book.active[1]) == BOM_HEADERS
|
||||
finally:
|
||||
book.close()
|
||||
record, = read_bom_records(path)
|
||||
assert (record.old, record.new, record.value_mode, record.custom) == ('001', '002', 'custom', '47k')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode,custom,expected', [
|
||||
('inherit', '', 'part_label'),
|
||||
('preserve', '', 'preserve'),
|
||||
('part_label', '', 'part_label'),
|
||||
('part_number', '', 'part_number'),
|
||||
('custom', '47k', 'custom'),
|
||||
])
|
||||
def test_record_value_source_roundtrips_and_compiles_with_task_default(tmp_path, mode, custom, expected):
|
||||
path = write_bom_template(tmp_path / 'values.xlsx', ({
|
||||
'old_part_number': '001', 'new_part_number': '002', 'refdes': 'R1',
|
||||
'value_mode': mode, 'custom_value': custom,
|
||||
},))
|
||||
book = load_workbook(path)
|
||||
try:
|
||||
assert book.active['L1'].comment is not None
|
||||
assert book.active['L1'].font.bold
|
||||
finally:
|
||||
book.close()
|
||||
target = ComponentTarget(tmp_path / 'Board.prj', 'MAIN', '1', 'u1', 'R1', '001', '10nF')
|
||||
records = read_bom_records(path)
|
||||
rows = match_bom_records((TargetPlanRow(target),), records)
|
||||
rule, = compile_target_rules(rows, default_value_mode='part_label')
|
||||
assert (rows[0].value_mode, rule.value_mode, rule.custom_value) == (mode, expected, custom)
|
||||
@@ -0,0 +1,481 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from openpyxl import Workbook
|
||||
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.target_plan import (
|
||||
ComponentTarget,
|
||||
TargetPlanRow,
|
||||
compile_target_rules,
|
||||
)
|
||||
from dx_productivity.trial_task_center.models import ProjectRecord, ProjectStatus
|
||||
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
|
||||
|
||||
|
||||
def _target(tmp_path, uid, refdes):
|
||||
return ComponentTarget(tmp_path / 'migrated.prj', 'MAIN', '1', uid, refdes, 'OLD', '10nF')
|
||||
|
||||
|
||||
def test_each_enabled_item_freezes_its_own_value_mode(tmp_path):
|
||||
rows = (
|
||||
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='preserve'),
|
||||
TargetPlanRow(_target(tmp_path, 'u2', 'R2'), True, 'NEW', value_mode='part_label'),
|
||||
TargetPlanRow(_target(tmp_path, 'u3', 'R3'), True, 'NEW', value_mode='custom', custom_value='47k'),
|
||||
)
|
||||
|
||||
rules = compile_target_rules(rows)
|
||||
|
||||
assert rows[2].custom_value_enabled
|
||||
assert {(rule.targets[0].refdes, rule.value_mode, rule.custom_value) for rule in rules} == {
|
||||
('R1', 'preserve', ''), ('R2', 'part_label', ''), ('R3', 'custom', '47k')
|
||||
}
|
||||
|
||||
|
||||
def test_inherit_item_uses_task_default_but_explicit_item_override_wins(tmp_path):
|
||||
rows = (
|
||||
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='inherit'),
|
||||
TargetPlanRow(_target(tmp_path, 'u2', 'R2'), True, 'NEW', value_mode='preserve'),
|
||||
)
|
||||
|
||||
rules = compile_target_rules(rows, default_value_mode='part_label')
|
||||
|
||||
assert [(rule.targets[0].refdes, rule.value_mode) for rule in rules] == [
|
||||
('R1', 'part_label'), ('R2', 'preserve')
|
||||
]
|
||||
|
||||
|
||||
def test_service_execution_rules_resolve_task_default_for_inherited_items(tmp_path):
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
store = TaskStore(tmp_path / 'execution-default.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_task('default policy')
|
||||
project = tmp_path / 'board.prj'
|
||||
store.sync_projects(task.task_id, tmp_path, (project,))
|
||||
targets = (
|
||||
ComponentTarget(project, 'MAIN', '1', 'u1', 'R1', 'OLD1', '10nF'),
|
||||
ComponentTarget(project, 'MAIN', '1', 'u2', 'R2', 'OLD2', '10nF'),
|
||||
)
|
||||
store.record_target_inventory(task.task_id, targets)
|
||||
store.replace_target_plan(task.task_id, (
|
||||
TargetPlanRow(targets[0], True, 'NEW1', value_mode='inherit'),
|
||||
TargetPlanRow(targets[1], True, 'NEW2', value_mode='preserve'),
|
||||
))
|
||||
service.set_copy_part_number_to_value(task.task_id, True)
|
||||
try:
|
||||
rules = service._execution_rules(task.task_id)
|
||||
assert [(rule.targets[0].refdes, rule.value_mode) for rule in rules] == [
|
||||
('R1', 'part_label'), ('R2', 'preserve')
|
||||
]
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
def test_execution_command_preserves_item_value_modes(tmp_path):
|
||||
projects = (ProjectRecord('p1', 'task', 'Board', tmp_path / 'migrated.prj', tmp_path,
|
||||
True, True, ProjectStatus.PENDING, 0),)
|
||||
rows = (
|
||||
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='preserve'),
|
||||
TargetPlanRow(_target(tmp_path, 'u2', 'R2'), True, 'NEW', value_mode='part_label'),
|
||||
)
|
||||
rules = compile_target_rules(rows)
|
||||
command = QuickRunPreparer(tmp_path / 'data', script_source=Path(__file__)).prepare(
|
||||
'task', projects, rules, copy_part_number_to_value=False,
|
||||
sheet_targets={str(projects[0].path).casefold(): (('MAIN', '1'),)},
|
||||
).command_path.read_text(encoding='utf-8')
|
||||
header, first, second = command.splitlines()
|
||||
mode = header.split('\t').index('value_mode')
|
||||
assert [first.split('\t')[mode], second.split('\t')[mode]] == ['preserve', 'part_label']
|
||||
|
||||
|
||||
def test_invalid_item_value_options_are_rejected(tmp_path):
|
||||
with pytest.raises(TaskCenterError, match='Value'):
|
||||
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='automatic')
|
||||
with pytest.raises(TaskCenterError, match='custom Value is empty'):
|
||||
compile_target_rules((TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW',
|
||||
value_mode='custom'),))
|
||||
|
||||
|
||||
def test_legacy_bom_custom_value_remains_custom_after_normalization(tmp_path):
|
||||
from dx_productivity.trial_task_center.bom_plan import read_bom_records, write_bom_template
|
||||
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources, write_routed_bom
|
||||
|
||||
source = write_bom_template(tmp_path / 'legacy.xlsx', ({
|
||||
'project_name': 'Board.prj', 'project_path': r'Z:\old\Board.prj',
|
||||
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': 'R1',
|
||||
'custom_value': '47k',
|
||||
},))
|
||||
rows = load_sources((source,))
|
||||
normalized = write_routed_bom(tmp_path / 'normalized.xlsx', rows,
|
||||
route_sources(rows, (tmp_path / 'Board.prj',)))
|
||||
record, = read_bom_records(normalized)
|
||||
assert record.value_mode == 'custom' and record.custom == '47k'
|
||||
|
||||
|
||||
def test_target_context_keeps_editable_mapping_rules_after_item_overrides(tmp_path):
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
store = TaskStore(tmp_path / 'context.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
project = tmp_path / 'board.prj'
|
||||
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
task, = service.create_project_tasks(tmp_path, selected_paths=(project,))
|
||||
original = service.add_rule(task.task_id, 'OLD', 'NEW')
|
||||
target = ComponentTarget(project, 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF')
|
||||
store.record_target_inventory(task.task_id, (target,))
|
||||
store.replace_target_plan(task.task_id, (TargetPlanRow(target, True, 'OTHER', value_mode='part_label'),))
|
||||
try:
|
||||
context = service.task_context(task.task_id)
|
||||
assert [(r.rule_id, r.new_part_number) for r in context.rules] == [(original.rule_id, 'NEW')]
|
||||
assert service._execution_rules(task.task_id)[0].new_part_number == 'OTHER'
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
def _write_bom(path, project_name, project_path, refdes):
|
||||
book = Workbook()
|
||||
sheet = book.active
|
||||
sheet.append(['project_name', 'project_path', 'old_part_number', 'new_part_number', 'refdes', 'value_mode'])
|
||||
sheet.append([project_name, project_path, 'OLD', 'NEW', refdes, 'part_label'])
|
||||
book.save(path)
|
||||
|
||||
|
||||
def test_multi_file_bom_load_preserves_sources_and_migrates_exact_project_name(tmp_path):
|
||||
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources
|
||||
|
||||
first = tmp_path / 'first.xlsx'
|
||||
second = tmp_path / 'second.xlsx'
|
||||
_write_bom(first, 'Board.prj', r'Z:\old\Board.prj', 'R1')
|
||||
_write_bom(second, 'Board.prj', r'Z:\old\Board.prj', 'R2')
|
||||
current = tmp_path / 'Board.prj'
|
||||
|
||||
rows = load_sources((first, second))
|
||||
routes = route_sources(rows, (current,))
|
||||
|
||||
assert [row.source.name for row in rows] == ['first.xlsx', 'second.xlsx']
|
||||
assert [row.excel_row for row in rows] == [2, 2]
|
||||
assert routes[rows[0].project_key] == current
|
||||
|
||||
|
||||
def test_migrated_name_match_is_ambiguous_without_manual_route(tmp_path):
|
||||
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources
|
||||
|
||||
source = tmp_path / 'one.xlsx'
|
||||
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
|
||||
candidates = (tmp_path / 'a' / 'Board.prj', tmp_path / 'b' / 'Board.prj')
|
||||
for candidate in candidates:
|
||||
candidate.parent.mkdir()
|
||||
candidate.touch()
|
||||
|
||||
rows = load_sources((source,))
|
||||
routes = route_sources(rows, candidates)
|
||||
|
||||
assert routes[rows[0].project_key] is None
|
||||
|
||||
|
||||
def test_distinct_project_names_route_independently_when_stale_paths_are_identical(tmp_path):
|
||||
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources
|
||||
|
||||
sources = (tmp_path / 'first.xlsx', tmp_path / 'second.xlsx')
|
||||
_write_bom(sources[0], 'BoardA.prj', r'Z:\old\wrong.prj', 'R1')
|
||||
_write_bom(sources[1], 'BoardB.prj', r'Z:\old\wrong.prj', 'R1')
|
||||
paths = (tmp_path / 'BoardA.prj', tmp_path / 'BoardB.prj')
|
||||
rows = load_sources(sources)
|
||||
routes = route_sources(rows, paths)
|
||||
assert tuple(routes[row.project_key] for row in rows) == paths
|
||||
|
||||
|
||||
def test_routed_bom_writes_source_map_for_closed_loop_diagnostics(tmp_path):
|
||||
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources, write_routed_bom
|
||||
|
||||
source = tmp_path / 'one.xlsx'
|
||||
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
|
||||
current = tmp_path / 'Board.prj'
|
||||
rows = load_sources((source,))
|
||||
routes = route_sources(rows, (current,))
|
||||
destination = tmp_path / 'normalized' / 'bom.xlsx'
|
||||
|
||||
write_routed_bom(destination, rows, routes)
|
||||
|
||||
source_map = destination.with_name('source-map.json').read_text(encoding='utf-8')
|
||||
assert 'migrated_exact_name' in source_map
|
||||
assert 'one.xlsx' in source_map and '"source_row": 2' in source_map
|
||||
|
||||
|
||||
def test_target_plan_schema_migrates_value_mode_for_existing_inventory(tmp_path):
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
path = tmp_path / 'tasks.db'
|
||||
store = TaskStore(path)
|
||||
task = store.create_task('migration')
|
||||
project = tmp_path / 'board.prj'
|
||||
store.sync_projects(task.task_id, tmp_path, (project,))
|
||||
store.record_target_inventory(task.task_id, (ComponentTarget(project, 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF'),))
|
||||
store.close()
|
||||
import sqlite3
|
||||
with sqlite3.connect(path) as connection:
|
||||
connection.execute('ALTER TABLE target_plan DROP COLUMN value_mode')
|
||||
store = TaskStore(path)
|
||||
try:
|
||||
# Existing inventory must inherit the task policy after migration.
|
||||
assert store.list_target_plan(task.task_id)[0].value_mode == 'inherit'
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
def test_closed_loop_persists_plan_reopens_and_freezes_command_modes(tmp_path):
|
||||
from dx_productivity.trial_task_center.models import ProjectStatus, ProjectRecord
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
database_path = tmp_path / 'closed-loop.db'
|
||||
project = tmp_path / 'board.prj'
|
||||
store = TaskStore(database_path)
|
||||
task = store.create_task('closed-loop')
|
||||
store.sync_projects(task.task_id, tmp_path, (project,))
|
||||
targets = (_target(tmp_path, 'u1', 'R1'), _target(tmp_path, 'u2', 'R2'))
|
||||
targets = tuple(ComponentTarget(project, t.schematic_name, t.sheet_number, t.uid, t.refdes,
|
||||
t.old_part_number, t.old_value) for t in targets)
|
||||
store.record_target_inventory(task.task_id, targets)
|
||||
rows = (TargetPlanRow(targets[0], True, 'NEW', value_mode='preserve'),
|
||||
TargetPlanRow(targets[1], True, 'NEW', value_mode='part_label'))
|
||||
store.replace_target_plan(task.task_id, rows)
|
||||
store.close()
|
||||
|
||||
reopened = TaskStore(database_path)
|
||||
try:
|
||||
frozen = reopened.list_target_plan(task.task_id)
|
||||
rules = compile_target_rules(frozen)
|
||||
command = QuickRunPreparer(tmp_path / 'evidence', script_source=Path(__file__)).prepare(
|
||||
task.task_id,
|
||||
(ProjectRecord('p1', task.task_id, 'Board', project, tmp_path, True, True,
|
||||
ProjectStatus.PENDING, 0),),
|
||||
rules,
|
||||
copy_part_number_to_value=False,
|
||||
sheet_targets={str(project).casefold(): (('MAIN', '1'),)},
|
||||
).command_path.read_text(encoding='utf-8')
|
||||
header, *command_rows = command.splitlines()
|
||||
mode_index = header.split('\t').index('value_mode')
|
||||
assert [row.split('\t')[mode_index] for row in command_rows] == ['preserve', 'part_label']
|
||||
finally:
|
||||
reopened.close()
|
||||
|
||||
|
||||
def test_service_multi_file_preview_normalizes_migrated_routes_before_cad(tmp_path, monkeypatch):
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
import dx_productivity.trial_task_center.bom_import_report as report_module
|
||||
|
||||
source = tmp_path / 'migrated.xlsx'
|
||||
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
|
||||
current = tmp_path / 'Board.prj'
|
||||
store = TaskStore(tmp_path / 'service.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
|
||||
task = service.create_task('BOM')
|
||||
store.sync_projects(task.task_id, tmp_path, (current,))
|
||||
seen = {}
|
||||
|
||||
def fake_inspect(_service, _task_id, path, **_options):
|
||||
seen['path'] = Path(path)
|
||||
assert seen['path'].is_file()
|
||||
return report_module.BomImportReport(
|
||||
rows=(report_module.BomRowResult(2, str(current), 'Board', 'passed', 'validated'),),
|
||||
projects=(report_module.BomProjectResult(str(current), 'Board', 'passed', (2,)),),
|
||||
)
|
||||
|
||||
monkeypatch.setattr(report_module, 'inspect_batch', fake_inspect)
|
||||
try:
|
||||
report = service.preview_bom_sources(task.task_id, (source,))
|
||||
assert report.rows[0].reason.startswith('validated;来源:')
|
||||
assert 'migrated.xlsx' in report.rows[0].reason and '第 2 行' in report.rows[0].reason
|
||||
assert not seen['path'].exists()
|
||||
assert report.source_map_path == seen['path'].parent / 'source-map.json'
|
||||
assert (seen['path'].parent / 'source-map.json').is_file()
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def simulated_bom_workspace(tmp_path):
|
||||
from types import SimpleNamespace
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
root = tmp_path / 'projects'
|
||||
for name in ('BoardA', 'BoardB'):
|
||||
project = root / name / (name + '.prj')
|
||||
project.parent.mkdir(parents=True)
|
||||
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
store = TaskStore(tmp_path / 'simulation.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'evidence'))
|
||||
tasks = service.create_project_tasks(root)
|
||||
for task in tasks:
|
||||
project = service.projects(task.task_id)[0].path
|
||||
targets = tuple(ComponentTarget(project, 'MAIN', '1', f'u{i}', f'R{i}', 'OLD', '10nF')
|
||||
for i in (1, 2))
|
||||
store.record_target_inventory(task.task_id, targets)
|
||||
runtime = SimpleNamespace(bind_logs=lambda *_: None)
|
||||
yield service, store, tasks, runtime
|
||||
store.close()
|
||||
|
||||
|
||||
def test_two_excel_imports_save_reopen_freeze_and_recovery_readback(simulated_bom_workspace, tmp_path):
|
||||
from dataclasses import replace
|
||||
from dx_productivity.trial_task_center.bom_plan import write_bom_template
|
||||
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase as Phase
|
||||
from dx_productivity.trial_task_center.quick_run import _read_command_options, _read_commands, _queue_matches_inputs
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
service, store, tasks, runtime = simulated_bom_workspace
|
||||
service.set_copy_part_number_to_value(tasks[0].task_id, True)
|
||||
sources = []
|
||||
for task, modes in zip(tasks, (('inherit', 'preserve'), ('custom', 'part_number'))):
|
||||
project = service.projects(task.task_id)[0].path
|
||||
sources.append(write_bom_template(tmp_path / (project.stem + '.xlsx'), tuple({
|
||||
'project_name': task.name, 'project_path': 'Z:/old/' + project.name,
|
||||
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': f'R{i}',
|
||||
'value_mode': mode, 'custom_value': '47k' if mode == 'custom' else '',
|
||||
} for i, mode in enumerate(modes, 1))))
|
||||
phases = []
|
||||
report = service.preview_bom_sources(tasks[0].task_id, sources, runtime=runtime,
|
||||
on_phase=lambda phase, _: phases.append(phase))
|
||||
assert report.passed_rows == 4 and report.preview is not None
|
||||
assert phases == [Phase.READING, Phase.VALIDATING, Phase.INVENTORY, Phase.MATCHING,
|
||||
Phase.INVENTORY, Phase.MATCHING]
|
||||
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
|
||||
for row, filename, number in zip(report.rows, ('BoardA.xlsx', 'BoardA.xlsx', 'BoardB.xlsx', 'BoardB.xlsx'), (2, 3, 2, 3)):
|
||||
assert filename in row.reason and f'第 {number} 行' in row.reason
|
||||
service.save_bom_batch(report.preview, {p.task_id: p.rows for p in report.preview.projects})
|
||||
reopened = TaskStore(store.path)
|
||||
try:
|
||||
for task, expected in zip(tasks, ((('part_label', ''), ('preserve', '')),
|
||||
(('custom', '47k'), ('part_number', '')))):
|
||||
policy = reopened.get_task(task.task_id).copy_part_number_to_value
|
||||
rules = compile_target_rules(reopened.list_target_plan(task.task_id),
|
||||
default_value_mode='part_label' if policy else 'preserve')
|
||||
projects = reopened.list_projects(task.task_id)
|
||||
command = QuickRunPreparer(tmp_path / task.task_id, script_source=Path(__file__)).prepare(
|
||||
task.task_id, projects, rules, copy_part_number_to_value=policy,
|
||||
sheet_targets={str(projects[0].path).casefold(): (('MAIN', '1'),)},
|
||||
).command_path
|
||||
options = _read_command_options(command)
|
||||
assert tuple((mode, custom) for mode, custom, _, _ in options.values()) == expected
|
||||
commands = _read_commands(command)
|
||||
assert _queue_matches_inputs(command, commands, projects, rules)
|
||||
changed = (replace(rules[0], custom_value='changed') if rules[0].value_mode == 'custom'
|
||||
else replace(rules[0], value_mode='part_number'))
|
||||
assert not _queue_matches_inputs(command, commands, projects, (changed, *rules[1:]))
|
||||
finally:
|
||||
reopened.close()
|
||||
|
||||
|
||||
def test_cross_file_duplicate_blocks_every_plan_with_original_sources(simulated_bom_workspace, tmp_path):
|
||||
service, store, tasks, runtime = simulated_bom_workspace
|
||||
sources = (tmp_path / 'first.xlsx', tmp_path / 'second.xlsx')
|
||||
for source in sources:
|
||||
_write_bom(source, 'BoardA.prj', r'Z:\old\BoardA.prj', 'R1')
|
||||
report = service.preview_bom_sources(tasks[0].task_id, sources, runtime=runtime)
|
||||
assert report.preview is None and report.failed_rows == 2
|
||||
assert 'first.xlsx' in report.rows[0].reason and 'second.xlsx' in report.rows[1].reason
|
||||
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
|
||||
|
||||
|
||||
def test_multi_source_import_reuses_supervised_inspection(tmp_path, monkeypatch):
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.bom_import_report import BomImportReport
|
||||
|
||||
store = TaskStore(tmp_path / 'supervised.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path / 'data'), supervised=True)
|
||||
task = store.create_task('board')
|
||||
store.sync_projects(task.task_id, tmp_path, (tmp_path / 'Board.prj',))
|
||||
source = tmp_path / 'one.xlsx'
|
||||
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
|
||||
phases = []
|
||||
def supervised(operation, arguments, **controls):
|
||||
assert operation == 'preview_bom_sources'
|
||||
assert arguments['sources'] == (source,)
|
||||
controls['on_phase']('validating', 'normalized')
|
||||
return BomImportReport((), ())
|
||||
monkeypatch.setattr(service, '_supervise', supervised)
|
||||
try:
|
||||
service.preview_bom_sources(task.task_id, (source,), on_phase=lambda phase, _: phases.append(phase))
|
||||
assert phases == ['validating']
|
||||
finally:
|
||||
store.close()
|
||||
|
||||
|
||||
def test_legacy_plan_text_false_inherits_without_enabling_custom(simulated_bom_workspace, tmp_path):
|
||||
from openpyxl import load_workbook
|
||||
from dx_productivity.trial_task_center.spreadsheet_plan import export_target_plan, read_target_plan
|
||||
|
||||
_, store, tasks, _ = simulated_bom_workspace
|
||||
task_id = tasks[0].task_id
|
||||
path = export_target_plan(store, task_id, tmp_path / 'legacy-plan.xlsx')
|
||||
book = load_workbook(path)
|
||||
book.active.delete_cols(14)
|
||||
book.active['I2'] = True
|
||||
book.active['J2'] = 'NEW'
|
||||
book.active['K2'] = 'FALSE'
|
||||
book.save(path)
|
||||
book.close()
|
||||
imported = read_target_plan(store, task_id, path)
|
||||
assert imported[0].enabled and imported[0].value_mode == 'inherit'
|
||||
assert not imported[0].custom_value_enabled
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid', ['=1+1', '#N/A'])
|
||||
def test_malformed_second_excel_is_blocked_before_inspection(simulated_bom_workspace, tmp_path, monkeypatch, invalid):
|
||||
from openpyxl import load_workbook
|
||||
|
||||
service, store, tasks, _ = simulated_bom_workspace
|
||||
first, second = tmp_path / 'first.xlsx', tmp_path / 'second.xlsx'
|
||||
for source in (first, second):
|
||||
_write_bom(source, 'BoardA.prj', r'Z:\old\BoardA.prj', 'R1')
|
||||
book = load_workbook(second)
|
||||
book.active['D2'] = invalid
|
||||
book.save(second)
|
||||
book.close()
|
||||
monkeypatch.setattr(service, 'inspect_bom_batch', lambda *_a, **_k: pytest.fail('invalid source reached inspection'))
|
||||
with pytest.raises(TaskCenterError, match='second.xlsx.*2'):
|
||||
service.preview_bom_sources(tasks[0].task_id, (first, second))
|
||||
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('mode', ['preserve', 'part_label', 'part_number'])
|
||||
def test_batch_custom_default_does_not_overwrite_explicit_item_mode(simulated_bom_workspace, mode):
|
||||
from dataclasses import replace
|
||||
|
||||
service, store, tasks, _ = simulated_bom_workspace
|
||||
task_id = tasks[0].task_id
|
||||
rule = service.add_rule(task_id, 'OLD', 'NEW')
|
||||
rows = store.list_target_plan(task_id)
|
||||
store.replace_target_plan(task_id, (replace(rows[0], enabled=True, value_mode=mode), rows[1]))
|
||||
service.update_rule(rule.rule_id, old_part_number='OLD', new_part_number='NEW', enabled=True,
|
||||
custom_value_enabled=True, custom_value='47k')
|
||||
row = store.list_target_plan(task_id)[0]
|
||||
assert row.value_mode == mode and not row.custom_value_enabled
|
||||
assert service._execution_rules(task_id)[0].value_mode == mode
|
||||
|
||||
|
||||
def test_disabling_seeded_custom_default_restores_inherited_policy(simulated_bom_workspace):
|
||||
from dataclasses import replace
|
||||
|
||||
service, store, tasks, _ = simulated_bom_workspace
|
||||
task_id = tasks[0].task_id
|
||||
rule = service.add_rule(task_id, 'OLD', 'NEW', custom_value_enabled=True, custom_value='47k')
|
||||
rows = store.list_target_plan(task_id)
|
||||
store.replace_target_plan(task_id, (replace(rows[0], enabled=True), rows[1]))
|
||||
service.set_copy_part_number_to_value(task_id, True)
|
||||
service.update_rule(rule.rule_id, old_part_number='OLD', new_part_number='NEW', enabled=True,
|
||||
custom_value_enabled=False, custom_value='')
|
||||
row = store.list_target_plan(task_id)[0]
|
||||
assert row.value_mode == 'inherit' and not row.custom_value_enabled
|
||||
assert service._execution_rules(task_id)[0].value_mode == 'part_label'
|
||||
@@ -5,6 +5,7 @@ from types import SimpleNamespace
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import pytest
|
||||
from shiboken6 import isValid
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
@@ -45,4 +46,9 @@ def ui(qtbot, tmp_path):
|
||||
open_task(tasks[0])
|
||||
yield SimpleNamespace(window=window,service=service,store=store,tasks=tasks,
|
||||
calls=calls,settle=settle,open=open_task,data=data)
|
||||
settle();window.plan_panel.discard_all();window.close();store.close()
|
||||
if isValid(window):
|
||||
settle()
|
||||
if isValid(window):
|
||||
window.plan_panel.discard_all()
|
||||
window.close()
|
||||
store.close()
|
||||
@@ -201,7 +201,8 @@ def test_runtime_restart_discards_partial_protocol_and_utf8_characters(qtbot,tmp
|
||||
setattr(state.process,method,lambda *args:None)
|
||||
state.is_running=False
|
||||
state.data_root=tmp_path
|
||||
state.status=SimpleNamespace(setText=lambda *args:None,show=lambda:None)
|
||||
state.status=SimpleNamespace(setText=lambda *args:None,show=lambda:None,hide=lambda:None)
|
||||
state._show_official_loading=lambda root:None
|
||||
state.retry=SimpleNamespace(hide=lambda:None)
|
||||
state.timeout=SimpleNamespace(start=lambda *args:None)
|
||||
package=tmp_path/'node_modules/@deepseek-ai/dsh/package.json'
|
||||
@@ -264,7 +265,9 @@ def test_bad_line_does_not_block_following_local_runtime_handshake(qtbot):
|
||||
state,chunks=protocol_state(qtbot)
|
||||
loaded=[];ready=[]
|
||||
page=SimpleNamespace(allowed_port=None)
|
||||
state.web=SimpleNamespace(page=lambda:page,load=loaded.append)
|
||||
settings_calls=[]
|
||||
state.web=SimpleNamespace(page=lambda:page,load=loaded.append,
|
||||
settings=lambda:SimpleNamespace(setAttribute=lambda *args:settings_calls.append(args)))
|
||||
state.timeout=SimpleNamespace(stop=lambda:None)
|
||||
state.status=SimpleNamespace(hide=lambda:None)
|
||||
state._diagnosis_queue=[]
|
||||
@@ -273,4 +276,5 @@ def test_bad_line_does_not_block_following_local_runtime_handshake(qtbot):
|
||||
NativeHarnessView._stdout(state)
|
||||
assert state._started and ready==[True]
|
||||
assert page.allowed_port==12345
|
||||
assert settings_calls[-1][1] is False
|
||||
assert [url.toString()for url in loaded]==['http://127.0.0.1:12345/']
|
||||
@@ -21,6 +21,57 @@ def test_mapping_dialog_exposes_custom_value_nc_and_optional_cell(qtbot):
|
||||
assert dialog.target_cell=='' and '可选' in dialog.cell_combo.itemText(0)
|
||||
|
||||
|
||||
def test_target_table_exposes_item_value_mode_selector(qtbot):
|
||||
from pathlib import Path
|
||||
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow
|
||||
from dx_productivity.trial_task_center.ui.target_table import TargetTable
|
||||
|
||||
table = TargetTable()
|
||||
qtbot.addWidget(table)
|
||||
row = TargetPlanRow(ComponentTarget(Path('C:/board.prj'), 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF'),
|
||||
True, 'NEW')
|
||||
table.populate((row,))
|
||||
assert table.model().index(0, 10).data() == 'inherit'
|
||||
assert table.model().setData(table.model().index(0, 10), 'part_label')
|
||||
assert table.rows()[0].value_mode == 'part_label'
|
||||
assert not table.rows()[0].custom_value_enabled
|
||||
assert table.model().setData(table.model().index(0, 10), 'custom')
|
||||
assert table.rows()[0].value_mode == 'custom' and table.rows()[0].custom_value_enabled
|
||||
|
||||
from PySide6.QtCore import Qt
|
||||
assert table.model().setData(table.model().index(0, 7), Qt.CheckState.Unchecked,
|
||||
Qt.ItemDataRole.CheckStateRole)
|
||||
assert table.rows()[0].value_mode == 'inherit' and not table.rows()[0].custom_value_enabled
|
||||
assert table.plan_model.headerData(10, Qt.Orientation.Horizontal, Qt.ItemDataRole.ToolTipRole)
|
||||
|
||||
|
||||
def test_target_value_editor_commits_selection_and_escape_cancels(qtbot):
|
||||
from pathlib import Path
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QComboBox
|
||||
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow
|
||||
from dx_productivity.trial_task_center.ui.target_table import TargetTable
|
||||
|
||||
table = TargetTable()
|
||||
qtbot.addWidget(table)
|
||||
table.resize(1400, 200)
|
||||
table.populate((TargetPlanRow(ComponentTarget(Path('C:/board.prj'), 'MAIN', '1', 'u1',
|
||||
'R1', 'OLD', '10nF'), True, 'NEW'),))
|
||||
table.show()
|
||||
index = table.model().index(0, 10)
|
||||
table.edit(index)
|
||||
editor = table.findChild(QComboBox)
|
||||
assert editor.currentText() == 'inherit'
|
||||
editor.setCurrentText('part_number')
|
||||
qtbot.keyClick(editor, Qt.Key.Key_Tab)
|
||||
assert table.rows()[0].value_mode == 'part_number'
|
||||
table.edit(index)
|
||||
editor = next(e for e in table.findChildren(QComboBox) if e.isVisible())
|
||||
editor.setCurrentText('custom')
|
||||
qtbot.keyClick(editor, Qt.Key.Key_Escape)
|
||||
assert table.rows()[0].value_mode == 'part_number'
|
||||
|
||||
|
||||
def test_small_window_keeps_splitters_and_opens_log_in_disposable_window(ui,qtbot):
|
||||
w=ui.window;w.resize(980,700);qtbot.wait(30)
|
||||
assert w.width()==980 and w.height()==700
|
||||
|
||||
@@ -160,12 +160,13 @@ def test_ui_previews_each_project_and_confirms_the_batch_once(batch, tmp_path, q
|
||||
from PySide6.QtWidgets import QFileDialog
|
||||
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
|
||||
service, store, tasks, targets, calls = batch
|
||||
path = write_bom_template(tmp_path / 'bom.xlsx', records_for(tasks, targets))
|
||||
paths = [write_bom_template(tmp_path / f'bom-{i}.xlsx', (record,))
|
||||
for i, record in enumerate(records_for(tasks, targets))]
|
||||
window = TrialTaskCenterWindow(service)
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(tasks[0].task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: (str(path), 'Excel (*.xlsx)'))
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path) for path in paths], 'Excel (*.xlsx)'))
|
||||
window.plan_panel.import_bom_button.click()
|
||||
qtbot.waitUntil(lambda: not window._workers, timeout=5000)
|
||||
assert window.plan_panel.table.rows()[0].new_part_number == 'NEW-0'
|
||||
|
||||
@@ -236,12 +236,14 @@ def test_cancellation_during_excel_parsing_keeps_operation_cancelled(batch, tmp_
|
||||
|
||||
|
||||
def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtbot, monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
from PySide6.QtWidgets import QFileDialog
|
||||
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
|
||||
service, store, tasks, targets, calls = batch
|
||||
# Exercise the real async import callback without any blocking message box.
|
||||
monkeypatch.setattr(service, 'inspect_bom_batch', lambda *args, **kwargs: inspect_batch(
|
||||
service, *args, **kwargs), raising=False)
|
||||
preview = service.preview_bom_sources
|
||||
monkeypatch.setattr(service, 'preview_bom_sources', lambda *args, **kwargs: preview(
|
||||
*args, runtime=SimpleNamespace(bind_logs=lambda *_: None), **kwargs))
|
||||
records = list(records_for(tasks, targets))
|
||||
records[1]['new_part_number'] = ''
|
||||
path = write_bom_template(tmp_path / 'bad-ui.xlsx', records)
|
||||
@@ -249,7 +251,8 @@ def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtb
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(tasks[0].task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: (str(path), 'Excel (*.xlsx)'))
|
||||
qtbot.waitUntil(window.plan_panel.import_bom_button.isEnabled)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)'))
|
||||
window.plan_panel.import_bom_button.click()
|
||||
qtbot.waitUntil(lambda: not window._workers)
|
||||
panel = window.plan_panel
|
||||
@@ -264,10 +267,14 @@ def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtb
|
||||
|
||||
|
||||
def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch, tmp_path, qtbot, monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
from PySide6.QtWidgets import QFileDialog
|
||||
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
|
||||
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
|
||||
service, store, tasks, targets, calls = batch
|
||||
preview = service.preview_bom_sources
|
||||
monkeypatch.setattr(service, 'preview_bom_sources', lambda *args, **kwargs: preview(
|
||||
*args, runtime=SimpleNamespace(bind_logs=lambda *_: None), **kwargs))
|
||||
preparer = service.for_project_task(tasks[0].task_id)._quick_run_preparer
|
||||
original_reader = preparer.collect_inventory
|
||||
def failed_reader(*args, **kwargs):
|
||||
@@ -278,7 +285,8 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
|
||||
qtbot.addWidget(window)
|
||||
window.open_task(tasks[0].task_id)
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: (str(path), 'Excel (*.xlsx)'))
|
||||
qtbot.waitUntil(window.plan_panel.import_bom_button.isEnabled)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)'))
|
||||
panel = window.plan_panel
|
||||
panel.import_bom_button.click()
|
||||
qtbot.waitUntil(lambda: not window._workers)
|
||||
@@ -287,6 +295,7 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
|
||||
assert 'UI-CLEANUP-1' in panel.report_text and panel.report_toggle.isVisibleTo(panel)
|
||||
assert not panel._bom_batches and not calls
|
||||
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
|
||||
qtbot.waitUntil(panel.import_bom_button.isEnabled)
|
||||
preparer.collect_inventory = original_reader
|
||||
panel.import_bom_button.click()
|
||||
assert panel.last_bom_report is None and not panel.report_toggle.isVisibleTo(panel)
|
||||
@@ -296,3 +305,19 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
|
||||
assert all(not store.list_target_plan(task.task_id) for task in tasks)
|
||||
panel.discard_all()
|
||||
window.close()
|
||||
|
||||
|
||||
def test_ui_report_uses_original_workbook_row_and_links_source_evidence(tmp_path):
|
||||
from types import SimpleNamespace
|
||||
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
|
||||
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
|
||||
from dx_productivity.trial_task_center.ui.project_plan import ProjectPlanPanel
|
||||
|
||||
report = BomImportReport((BomRowResult(3, 'board.prj', 'Board', 'failed', 'duplicate',
|
||||
source_file=str(tmp_path / 'second.xlsx'), source_row=5),), (),
|
||||
source_map_path=tmp_path / 'source-map.json')
|
||||
state = SimpleNamespace(report=report, phase=BomImportPhase.FAILED, notice='')
|
||||
_, text, _ = ProjectPlanPanel._format_bom_report(state)
|
||||
assert 'second.xlsx · Excel 第 5 行' in text
|
||||
assert 'Excel 第 3 行' not in text
|
||||
assert str(report.source_map_path) in text
|
||||
@@ -0,0 +1,126 @@
|
||||
import json
|
||||
import os
|
||||
from contextlib import closing
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
import pytest
|
||||
from test_bom_batch import batch
|
||||
|
||||
|
||||
def test_real_supervised_sources_roundtrip_origin_metadata_and_preview(batch, tmp_path):
|
||||
from dx_productivity.trial_task_center.bom_plan import write_bom_template
|
||||
service, store, tasks, targets, calls = batch
|
||||
for task in tasks:
|
||||
store.record_target_inventory(task.task_id, targets[task.task_id])
|
||||
sources = tuple(write_bom_template(tmp_path / f'source-{index}.xlsx', ({
|
||||
'project_name': task.name, 'project_path': str(targets[task.task_id][0].project_path),
|
||||
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': 'R1', 'value_mode': 'part_number',
|
||||
},)) for index, task in enumerate(tasks))
|
||||
service._supervised = True
|
||||
report = service.preview_bom_sources(tasks[0].task_id, sources)
|
||||
assert report.passed_rows == 2 and report.preview is not None
|
||||
assert tuple(row.source_file for row in report.rows) == tuple(str(path.resolve()) for path in sources)
|
||||
assert all(row.source_row == 2 for row in report.rows)
|
||||
assert report.source_map_path.is_file()
|
||||
assert not report.source_map_path.with_name('normalized-bom.xlsx').exists()
|
||||
assert all(project.rows[0].value_mode == 'part_number' for project in report.preview.projects)
|
||||
assert not calls
|
||||
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
|
||||
|
||||
|
||||
def test_bom_sources_run_in_one_worker_with_import_controls(tmp_path, monkeypatch):
|
||||
"""Exercise real request admission and resource release, without opening CAD."""
|
||||
from dx_productivity.trial_task_center import batch_supervisor
|
||||
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
bootstrap = textwrap.dedent('''
|
||||
import os, sys
|
||||
from pathlib import Path
|
||||
from dx_productivity.trial_task_center.batch_worker import run
|
||||
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
|
||||
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
def business(self, task_id, sources, *, runtime, stop_event, on_progress, on_phase):
|
||||
assert task_id == 'task'
|
||||
assert type(sources) is tuple and len(sources) == 2
|
||||
assert all(isinstance(source, Path) for source in sources)
|
||||
assert tuple(source.name for source in sources) == ('first.xlsx', 'second.xlsx')
|
||||
assert runtime.kind == 'bom_import'
|
||||
assert runtime.stop_event is stop_event
|
||||
on_progress('read both sources')
|
||||
on_phase(BomImportPhase.READING, 'reading sources')
|
||||
return BomImportReport((BomRowResult(2, str(os.getpid()), 'Board', 'passed'),), ())
|
||||
TaskCenterService.preview_bom_sources = business
|
||||
run(Path(sys.argv[1]))
|
||||
''')
|
||||
monkeypatch.setattr(batch_supervisor, '_command',
|
||||
lambda request: (sys.executable, '-c', bootstrap, str(request)))
|
||||
progress, phases = [], []
|
||||
with closing(TaskStore(tmp_path / 'task-center.db')) as store:
|
||||
service = TaskCenterService(store, configure_logging(tmp_path))
|
||||
report = batch_supervisor.run_supervised(service, 'preview_bom_sources',
|
||||
{'task_id': 'task', 'sources': (tmp_path / 'first.xlsx', tmp_path / 'second.xlsx')},
|
||||
on_progress=progress.append, on_phase=lambda phase, message: phases.append(phase))
|
||||
assert report.rows[0].project_name == 'Board'
|
||||
assert report.rows[0].project_path != str(os.getpid())
|
||||
assert progress == ['read both sources']
|
||||
assert phases == [BomImportPhase.READING, BomImportPhase.RELEASING]
|
||||
directory = next((tmp_path / 'batch-resources').iterdir())
|
||||
journal = json.loads((directory / 'supervisor.json').read_text(encoding='utf-8'))
|
||||
assert journal['state'] == 'finished'
|
||||
assert journal['operation'] == 'preview_bom_sources'
|
||||
assert journal['task_ids'] == ['task']
|
||||
batch_supervisor.assert_supervisor_idle(tmp_path)
|
||||
|
||||
|
||||
def test_bom_sources_supervisor_rejects_wrong_result_type():
|
||||
from dx_productivity.trial_task_center.supervisor_protocol import encode
|
||||
from dx_productivity.trial_task_center.supervisor_result import validated_outcome
|
||||
|
||||
with pytest.raises(ValueError, match='result type'):
|
||||
validated_outcome('preview_bom_sources',
|
||||
{'business_succeeded': True, 'status': 'ok', 'result': encode(())})
|
||||
|
||||
|
||||
@pytest.mark.parametrize('shutdown_failed', [False, True])
|
||||
@pytest.mark.parametrize('business_succeeded', [False, True])
|
||||
def test_bom_sources_cleanup_preserves_rows_without_preview(
|
||||
tmp_path, monkeypatch, shutdown_failed, business_succeeded):
|
||||
from dx_productivity.trial_task_center import batch_resources
|
||||
from dx_productivity.trial_task_center.batch_supervisor import _finish_response
|
||||
from dx_productivity.trial_task_center.bom_batch import BomBatchPreview
|
||||
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.supervisor_protocol import encode, encode_error
|
||||
|
||||
report = BomImportReport((BomRowResult(4, 'board.prj', 'Board', 'passed'),), (),
|
||||
BomBatchPreview('task', (), ()))
|
||||
journal = {'operation': 'preview_bom_sources', 'batch_id': 'a' * 32, 'state': 'running'}
|
||||
directory = tmp_path / 'batch-resources' / journal['batch_id']
|
||||
if shutdown_failed:
|
||||
batch_resources.write_record(directory / 'session.json',
|
||||
{'cad_shutdown': {'orderly_shutdown': False}})
|
||||
pending = () if shutdown_failed else ({'session_id': journal['batch_id']},)
|
||||
monkeypatch.setattr(batch_resources, 'cleanup_pending_resources', lambda *args, **kwargs: pending)
|
||||
response = {'business_succeeded': business_succeeded, 'status': 'error',
|
||||
'error': encode_error(TaskCenterError('batch_resources_pending', 'Quit failed'))}
|
||||
if business_succeeded:
|
||||
response['result'] = encode(report)
|
||||
else:
|
||||
primary = TaskCenterError('inventory_cleanup', 'Reader failed')
|
||||
primary.bom_report = report
|
||||
response['operation_error'] = encode_error(primary)
|
||||
with pytest.raises(TaskCenterError) as caught:
|
||||
_finish_response(tmp_path, journal, response, configure_logging(tmp_path))
|
||||
assert caught.value.code == ('application_shutdown_failed' if shutdown_failed else 'batch_resources_pending')
|
||||
assert caught.value.bom_report.rows == report.rows
|
||||
assert caught.value.bom_report.preview is None
|
||||
if business_succeeded:
|
||||
assert caught.value.completed_result.preview is None
|
||||
else:
|
||||
assert caught.value.primary_error.code == 'inventory_cleanup'
|
||||
@@ -116,6 +116,21 @@ def test_value_policy_records_actual_change_and_ignores_repeated_value(case):
|
||||
assert case.history.page().rows==before
|
||||
|
||||
|
||||
def test_item_value_only_change_is_recorded_in_history(case):
|
||||
from dataclasses import replace
|
||||
task = bound_task(case)
|
||||
case.service.collect_target_inventory(task.task_id, ('OLD',), runtime=object())
|
||||
row, = case.service.target_plan(task.task_id)
|
||||
before = len(events(case, 'replace_target_plan'))
|
||||
case.service.replace_target_plan(task.task_id, (replace(row, value_mode='part_label'),))
|
||||
rows = events(case, 'replace_target_plan')
|
||||
assert len(rows) == before + 1
|
||||
change, = rows[0]['details']['changes']
|
||||
assert change['table'] == 'target_plan'
|
||||
assert change['before'] == {'value_mode': 'inherit'}
|
||||
assert change['after'] == {'value_mode': 'part_label'}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('operation',['create_assistant_project_tasks','collect_target_inventory','refresh_bound_project','set_value_policy'])
|
||||
def test_history_write_failure_rolls_back_each_real_service_mutation(case,monkeypatch,operation):
|
||||
task=None if operation=='create_assistant_project_tasks' else bound_task(case)
|
||||
|
||||
@@ -68,3 +68,41 @@ def test_native_short_path_never_passes_extended_prefix_to_wsh(tmp_path):
|
||||
if result is None:
|
||||
pytest.skip('filesystem does not provide short paths')
|
||||
assert not result.startswith(chr(92) * 2 + '?')
|
||||
|
||||
|
||||
def test_batch_resource_record_retries_reader_denied_atomic_replace(tmp_path, monkeypatch):
|
||||
import json
|
||||
import os
|
||||
from threading import Timer
|
||||
if os.name != 'nt':
|
||||
pytest.skip('Windows delete-sharing boundary')
|
||||
import win32con
|
||||
import win32file
|
||||
from dx_productivity.trial_task_center import batch_resources
|
||||
|
||||
path = tmp_path / 'session.json'
|
||||
path.write_text('{"stage": "before"}', encoding='utf-8')
|
||||
reader = win32file.CreateFile(str(path), win32con.GENERIC_READ,
|
||||
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None,
|
||||
win32con.OPEN_EXISTING, 0, None)
|
||||
real_replace = os.replace
|
||||
denials, timers = [], []
|
||||
def replace(source, target):
|
||||
try:
|
||||
return real_replace(source, target)
|
||||
except PermissionError as error:
|
||||
denials.append(error.winerror)
|
||||
timer = Timer(.02, reader.Close)
|
||||
timers.append(timer)
|
||||
timer.start()
|
||||
raise
|
||||
monkeypatch.setattr(batch_resources.os, 'replace', replace)
|
||||
try:
|
||||
batch_resources.write_record(path, {'stage': 'saved'})
|
||||
assert denials == [5]
|
||||
assert json.loads(path.read_text(encoding='utf-8')) == {'stage': 'saved'}
|
||||
assert not list(tmp_path.glob('*.tmp'))
|
||||
finally:
|
||||
for timer in timers:
|
||||
timer.join()
|
||||
reader.Close()
|
||||
@@ -134,3 +134,48 @@ def test_completed_failure_is_delivered_and_next_step_starts_fresh_worker(tmp_pa
|
||||
assert scope.run_step(project, command, 3).returncode == 0
|
||||
assert scope.worker is not previous
|
||||
assert (tmp_path / 'closed.txt').exists()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('release_reader', [True, False])
|
||||
def test_real_vbs_session_result_write_handles_reader_contention(tmp_path, release_reader):
|
||||
import os
|
||||
import re
|
||||
from threading import Timer
|
||||
if os.name != 'nt':
|
||||
pytest.skip('Windows ADODB.Stream sharing boundary')
|
||||
import win32con
|
||||
import win32file
|
||||
source = Path(__file__).resolve().parents[2] / 'integration/dxdesigner795/QuickPartReplace.vbs'
|
||||
writer = re.search(r'(?ms)^Sub QPWriteUtf8\(.*?^End Sub', source.read_text(encoding='utf-8')).group()
|
||||
script = tmp_path / 'write-result.vbs'
|
||||
script.write_text('Set QP_Fso = CreateObject("Scripting.FileSystemObject")\n'
|
||||
'WScript.Echo "ready"\nOn Error Resume Next\nQPWriteUtf8 WScript.Arguments(0), "verified"\n'
|
||||
'If Err.Number <> 0 Then\nWScript.StdErr.WriteLine Err.Description\nWScript.Quit 1\nEnd If\n'
|
||||
'WScript.Quit 0\n' + writer,
|
||||
encoding='ascii')
|
||||
path = tmp_path / 'project-session-result.tsv'
|
||||
path.write_text('before', encoding='utf-8')
|
||||
reader = win32file.CreateFile(str(path), win32con.GENERIC_READ, win32con.FILE_SHARE_READ,
|
||||
None, win32con.OPEN_EXISTING, 0, None)
|
||||
timer = None
|
||||
process = subprocess.Popen((r'C:\Windows\SysWOW64\cscript.exe', '//nologo', str(script), str(path)),
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
|
||||
try:
|
||||
assert process.stdout.readline().strip() == b'ready'
|
||||
if release_reader:
|
||||
timer = Timer(.15, reader.Close)
|
||||
timer.start()
|
||||
_, stderr = process.communicate(timeout=5)
|
||||
if release_reader:
|
||||
assert process.returncode == 0, stderr
|
||||
assert path.read_text(encoding='utf-8-sig') == 'verified'
|
||||
else:
|
||||
assert process.returncode != 0
|
||||
assert path.read_text(encoding='utf-8') == 'before'
|
||||
finally:
|
||||
if timer:
|
||||
timer.join()
|
||||
reader.Close()
|
||||
if process.poll() is None:
|
||||
process.kill()
|
||||
process.wait()
|
||||
@@ -152,7 +152,7 @@ def test_blank_bom_can_be_downloaded_without_task_or_inventory(qtbot, tmp_path,
|
||||
qtbot.waitUntil(lambda: not window._workers and output.exists())
|
||||
workbook = load_workbook(output)
|
||||
try:
|
||||
assert tuple(cell.value for cell in workbook.active[1]) == BOM_HEADERS
|
||||
assert tuple(cell.value for cell in workbook.active[1]) == BOM_HEADERS + ('value_mode',)
|
||||
# The first editing row is explicitly text-formatted to preserve zeroes.
|
||||
assert workbook.active.max_row == 2
|
||||
assert all(cell.value is None for cell in workbook.active[2])
|
||||
|
||||
@@ -104,8 +104,8 @@ def test_bom_retry_replaces_old_report_with_live_state(window, qtbot, monkeypatc
|
||||
if outcome == 'error':
|
||||
raise TaskCenterError('bom_import', '本次文件无法读取')
|
||||
return new
|
||||
monkeypatch.setattr(window.service, 'inspect_bom_batch', inspect)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: ('retry.xlsx', ''))
|
||||
monkeypatch.setattr(window.service, 'preview_bom_sources', inspect)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: (['retry.xlsx'], ''))
|
||||
panel.import_bom()
|
||||
assert 'Excel' in panel.summary.text()
|
||||
try:
|
||||
@@ -145,7 +145,7 @@ def test_cancelling_bom_file_picker_keeps_previous_report(window, qtbot, monkeyp
|
||||
panel = window.plan_panel
|
||||
report = BomImportReport((), (), errors=('原有报告',))
|
||||
panel._received_bom_report(report)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: ('', ''))
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([], ''))
|
||||
panel.import_bom()
|
||||
assert panel.last_bom_report is report
|
||||
assert '原有报告' in panel.report_text
|
||||
@@ -168,8 +168,8 @@ def test_import_phases_drive_ui_and_cancelling_cannot_be_overwritten(window, qtb
|
||||
assert release.wait(5)
|
||||
phase(BomImportPhase.INVENTORY, 'late next project')
|
||||
return BomImportReport((), (), errors=('late report',))
|
||||
monkeypatch.setattr(window.service, 'inspect_bom_batch', inspect)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: ('phased.xlsx', ''))
|
||||
monkeypatch.setattr(window.service, 'preview_bom_sources', inspect)
|
||||
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: (['phased.xlsx'], ''))
|
||||
panel.import_bom()
|
||||
try:
|
||||
qtbot.waitUntil(lambda: panel.bom_import_state.phase is BomImportPhase.MATCHING)
|
||||
|
||||
@@ -33,6 +33,7 @@ def main():
|
||||
parser.add_argument('--provider-config', type=Path)
|
||||
parser.add_argument('--live-model', action='store_true')
|
||||
parser.add_argument('--model-settings-check', action='store_true', help='Create an isolated custom provider using a local model-list fixture')
|
||||
parser.add_argument('--file-check', action='store_true', help='Pick and upload a real local text file and ZIP in the official UI')
|
||||
parser.add_argument('--incident', action='store_true', help='Inject a read-only controlled failure and verify automatic diagnosis and Git escalation')
|
||||
parser.add_argument('--outbox-only', action='store_true', help='Verify failed publication stays in local outbox without creating another real issue')
|
||||
parser.add_argument('--without-git', action='store_true', help='Simulate a client with no Git credentials or executable')
|
||||
@@ -40,6 +41,13 @@ def main():
|
||||
args = parser.parse_args()
|
||||
output = args.output.resolve()
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
if args.file_check:
|
||||
import zipfile
|
||||
fixtures = output / 'file-fixtures'
|
||||
fixtures.mkdir(exist_ok=True)
|
||||
(fixtures / 'dsh-upload.txt').write_text('DSH_UPLOAD_CONTENT_中文\n', encoding='utf8')
|
||||
with zipfile.ZipFile(fixtures / 'dsh-upload.zip', 'w', zipfile.ZIP_DEFLATED) as archive:
|
||||
archive.writestr('中文目录/说明.txt', 'DSH_ZIP_UPLOAD_CONTENT_中文')
|
||||
data = output / ('data-' + time.strftime('%Y%m%d-%H%M%S'))
|
||||
project_root = output / 'source-projects'
|
||||
source_hashes = {}
|
||||
@@ -65,14 +73,16 @@ def main():
|
||||
os.environ.pop('QT_QPA_PLATFORM', None)
|
||||
from PySide6.QtCore import QTimer, Qt
|
||||
from PySide6.QtTest import QTest
|
||||
from PySide6.QtWidgets import QApplication, QMessageBox, QDialogButtonBox
|
||||
from PySide6.QtWidgets import QApplication, QMessageBox, QDialogButtonBox, QFileDialog, QLineEdit
|
||||
if args.file_check:
|
||||
QApplication.setAttribute(Qt.ApplicationAttribute.AA_DontUseNativeDialogs)
|
||||
from dx_productivity.trial_task_center import application
|
||||
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
|
||||
from dx_productivity.trial_task_center.ui.dsh_task_creation import DshTaskCreationDialog
|
||||
|
||||
report = {'driver': 'Qt QTest mouse clicks + Chromium CDP Input events', 'checks': []}
|
||||
state = {'phase': 'select', 'child': None, 'creation_step': 'input',
|
||||
'deadline': time.monotonic() + (420 if args.incident else 300 if args.create_task else 240 if args.live_model else 150)}
|
||||
'deadline': time.monotonic() + (600 if args.file_check else 420 if args.incident else 300 if args.create_task else 240 if args.live_model else 150)}
|
||||
|
||||
def build_window(service):
|
||||
if not args.create_task:
|
||||
@@ -91,6 +101,8 @@ def main():
|
||||
|
||||
def failed(error):
|
||||
report.update(status='failed', error=type(error).__name__ + ': ' + str(error))
|
||||
if window._dsh_view is not None:
|
||||
report['runtimeStatus'] = window._dsh_view.status.text()
|
||||
(output / 'desktop-report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf8')
|
||||
child = state['child']
|
||||
if child and child.poll() is None:
|
||||
@@ -102,7 +114,43 @@ def main():
|
||||
try:
|
||||
if time.monotonic() > state['deadline']:
|
||||
raise TimeoutError('Native desktop acceptance timed out')
|
||||
if state['phase'] == 'ready' and window._dsh_view is not None and window._dsh_view.retry.isVisible():
|
||||
from dx_productivity.dsh.provider_config import load_provider
|
||||
key = load_provider(data / 'dsh').get('DSH_API_KEY', '')
|
||||
diagnostic = window._dsh_view.last_error
|
||||
if key:
|
||||
diagnostic = diagnostic.replace(key, '[redacted]')
|
||||
report['startupDiagnostic'] = diagnostic[-8000:]
|
||||
raise RuntimeError(window._dsh_view.status.text())
|
||||
modal = QApplication.activeModalWidget()
|
||||
if state['phase'] == 'ready' and window._dsh_view is not None and not report.get('officialLoadingPassed') and not state.get('checkingLoading'):
|
||||
state['checkingLoading'] = True
|
||||
def loading_ready(value):
|
||||
state['checkingLoading'] = False
|
||||
if value and not report.get('officialLoadingPassed'):
|
||||
assert not window._dsh_view.status.isVisible()
|
||||
report['officialLoadingPassed'] = True
|
||||
state['loadingCapture'] = subprocess.Popen(['node', str(REPO / 'tools/dsh_cdp.mjs'),
|
||||
'--capture-loading', str(args.port), str(output / 'official-loading.png')],
|
||||
cwd=REPO, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0)
|
||||
report['checks'].append('unmodified official frontend HARNESS loading spinner rendered before local runtime readiness')
|
||||
window._dsh_view.web.page().runJavaScript("!!document.querySelector('[data-dsh-boot] [data-dsh-boot-spinner]')", loading_ready)
|
||||
if args.file_check and isinstance(modal, QFileDialog) and not modal.property('dshAcceptancePicked'):
|
||||
modal.setProperty('dshAcceptancePicked', True)
|
||||
selected = (output / 'file-picker-request.json')
|
||||
assert selected.is_file(), 'Unexpected file picker'
|
||||
files = json.loads(selected.read_text(encoding='utf8'))
|
||||
edit = modal.findChild(QLineEdit, 'fileNameEdit')
|
||||
assert edit is not None
|
||||
QApplication.clipboard().setText(' '.join('"' + str(Path(f).resolve()) + '"' for f in files))
|
||||
edit.setFocus()
|
||||
QTest.keyClick(edit, Qt.Key.Key_A, Qt.KeyboardModifier.ControlModifier)
|
||||
QTest.keyClick(edit, Qt.Key.Key_V, Qt.KeyboardModifier.ControlModifier)
|
||||
buttons = modal.findChild(QDialogButtonBox)
|
||||
assert buttons is not None
|
||||
QTest.mouseClick(buttons.button(QDialogButtonBox.StandardButton.Open), Qt.MouseButton.LeftButton)
|
||||
report['checks'].append('selected owned text and ZIP fixtures in the real Qt file picker and clicked Open')
|
||||
if args.create_task and isinstance(modal, DshTaskCreationDialog):
|
||||
if state['creation_step'] == 'input':
|
||||
assert modal.name_edit.text() == 'DSH对话换料验收'
|
||||
@@ -124,6 +172,7 @@ def main():
|
||||
assert window.task_list.list.count() == 0
|
||||
click_navigation(window.dsh_action)
|
||||
state['phase'] = 'ready'
|
||||
state['runtimeWaitStarted'] = time.monotonic()
|
||||
report['checks'].append('opened native assistant from an empty task center')
|
||||
elif state['phase'] == 'select' and window.task_list.list.count() and not window._task_query.busy:
|
||||
item = window.task_list.list.item(0)
|
||||
@@ -142,13 +191,18 @@ def main():
|
||||
assert window.content_stack.currentWidget() is window._dsh_view
|
||||
report['checks'].append('clicked task row and opened DSH with one click on the right navigation item')
|
||||
state['phase'] = 'ready'
|
||||
state['runtimeWaitStarted'] = time.monotonic()
|
||||
elif state['phase'] == 'ready' and window._dsh_view is not None and window._dsh_view._started:
|
||||
if 'runtimeWaitStarted' in state:
|
||||
report['runtimeWaitSeconds'] = round(time.monotonic() - state['runtimeWaitStarted'], 2)
|
||||
script = 'verify_dsh_task_creation_ui.mjs' if args.create_task else 'verify_dsh_incident_ui.mjs' if args.incident else 'verify_dsh_native_ui.mjs'
|
||||
command = ['node', str(REPO / 'tools' / script), str(output), str(args.port), '--confirm-cancel']
|
||||
if args.live_model:
|
||||
command.append('--live-model')
|
||||
if args.model_settings_check:
|
||||
command.append('--model-settings-check')
|
||||
if args.file_check:
|
||||
command.append('--file-check')
|
||||
if args.outbox_only:
|
||||
command.append('--outbox-only')
|
||||
state['child'] = subprocess.Popen(command, cwd=REPO, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
@@ -215,6 +269,14 @@ def main():
|
||||
|
||||
application._load_window = lambda: build_window
|
||||
code = application.main(['--data-root', str(data)])
|
||||
capture = state.get('loadingCapture')
|
||||
if capture is not None:
|
||||
try:
|
||||
capture.communicate(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
capture.terminate()
|
||||
capture.communicate(timeout=5)
|
||||
report['officialLoadingScreenshot'] = capture.returncode == 0
|
||||
if code or report.get('status') != 'passed':
|
||||
print(json.dumps(report, ensure_ascii=False))
|
||||
return 1
|
||||
@@ -226,8 +288,8 @@ def main():
|
||||
audit_file = data / 'dsh/harness-home/task-tool-audit.jsonl'
|
||||
audit = [json.loads(line) for line in audit_file.read_text(encoding='utf8').splitlines()] if audit_file.is_file() else []
|
||||
report['modelToolCalls'] = audit
|
||||
expected_tool = 'dx_task_create' if args.create_task else 'dx_task_snapshot'
|
||||
report['modelToolSucceeded'] = any(row['name'] == expected_tool and row['is_error'] is False for row in audit)
|
||||
expected_tools = ['read', 'extract_zip'] if args.file_check else ['dx_task_create' if args.create_task else 'dx_task_snapshot']
|
||||
report['modelToolSucceeded'] = all(any(row['name'] == name and row['is_error'] is False for row in audit) for name in expected_tools)
|
||||
(output / 'desktop-report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf8')
|
||||
print(json.dumps(report, ensure_ascii=True))
|
||||
return 0 if report['cleanShutdown'] and not report['uncaughtErrors'] and not report['unexpectedUiErrors'] and (not (args.live_model or args.incident or args.create_task) or report['modelToolSucceeded']) else 1
|
||||
|
||||
+15
-5
@@ -1,12 +1,14 @@
|
||||
/** Tiny CDP driver for the real Qt Chromium page; no browser mocks. */
|
||||
import {writeFile} from 'node:fs/promises';
|
||||
|
||||
export async function connect(port=9226) {
|
||||
export async function connect(port=9226,{loading=false}={}) {
|
||||
let target;
|
||||
const deadline=Date.now()+45000;
|
||||
const deadline=Date.now()+60000;
|
||||
while(Date.now()<deadline){
|
||||
const targets=await fetch(`http://127.0.0.1:${port}/json`,{signal:AbortSignal.timeout(3000)}).then(r=>r.json());
|
||||
target=targets.find(x=>x.type==='page'&&x.url?.startsWith('http://127.0.0.1:'));
|
||||
let targets;
|
||||
try{targets=await fetch(`http://127.0.0.1:${port}/json`,{signal:AbortSignal.timeout(10000)}).then(r=>r.json());}
|
||||
catch{await new Promise(resolve=>setTimeout(resolve,100));continue;}
|
||||
target=targets.find(x=>x.type==='page'&&(loading?x.url?.startsWith('file:'):x.url?.startsWith('http://127.0.0.1:')));
|
||||
if(target)break;
|
||||
await new Promise(resolve=>setTimeout(resolve,100));
|
||||
}
|
||||
@@ -60,11 +62,19 @@ export async function connect(port=9226) {
|
||||
}
|
||||
|
||||
if(process.argv[1]?.endsWith('dsh_cdp.mjs')) {
|
||||
const client=await connect(Number(process.argv[2] || 9226));
|
||||
const loading=process.argv[2]==='--capture-loading';
|
||||
const client=await connect(Number(process.argv[loading?3:2] || 9226),{loading});
|
||||
try {
|
||||
if(loading){
|
||||
const actual=await client.evaluate(`({official:!!document.querySelector('[data-dsh-boot-spinner]'),text:document.body.innerText})`);
|
||||
if(!actual.official||!actual.text.includes('HARNESS'))throw Error('Official loading page was not rendered');
|
||||
await client.screenshot(process.argv[4]);
|
||||
console.log(JSON.stringify({officialLoading:true}));
|
||||
}else{
|
||||
console.log(JSON.stringify(await client.evaluate(`({title:document.title,text:document.body.innerText,
|
||||
buttons:[...document.querySelectorAll('button')].map(x=>({text:x.innerText,label:x.getAttribute('aria-label'),title:x.title})),
|
||||
svg:document.querySelectorAll('svg').length,animations:document.getAnimations().length,
|
||||
animatedSvg:document.querySelectorAll('animate,animateTransform,animateMotion').length,canvas:document.querySelectorAll('canvas').length})`)));
|
||||
}
|
||||
}finally{client.close();}
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
"""One real schematic-only acceptance run on new copies; no PCB execution."""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from dataclasses import asdict
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
from threading import Event
|
||||
import time
|
||||
import traceback
|
||||
|
||||
REPO = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(REPO / 'src'))
|
||||
from probe_batch_runtime_live import hashes, inspect_native
|
||||
|
||||
OLD, NEW = '4100-CA0000-T7', '4100-CA0000-T0'
|
||||
CASES = {'Audio_D2128': (('RV05', 'inherit', ''), ('RV11', 'preserve', '')),
|
||||
'Audio_T605A': (('RV05', 'part_number', ''), ('RV11', 'custom', '47k'))}
|
||||
|
||||
|
||||
def save(path, value):
|
||||
path.write_text(json.dumps(value, ensure_ascii=False, indent=2, default=str), encoding='utf-8')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--inspect-batch', type=Path)
|
||||
parser.add_argument('--continue-run', type=Path, help='Continue only pending entries of the saved queue')
|
||||
parser.add_argument('--reconcile-saved', type=Path, help='Verify saved uncertain result without native replacement')
|
||||
args = parser.parse_args()
|
||||
if args.inspect_batch:
|
||||
for spec in json.loads(args.inspect_batch.read_text(encoding='utf-8')):
|
||||
inspect_native(Path(spec))
|
||||
return 0
|
||||
|
||||
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
|
||||
from dx_productivity.trial_task_center.bom_plan import write_bom_template
|
||||
from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession
|
||||
from dx_productivity.trial_task_center.legacy_paths import cleanup_worker_aliases
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.task_queue import execute_task_queue
|
||||
from private_desktop_observer import PrivateDesktopObserver
|
||||
|
||||
assert not (args.continue_run and args.reconcile_saved)
|
||||
reconciling = args.reconcile_saved is not None
|
||||
continuing = args.continue_run is not None or reconciling
|
||||
output = ((args.continue_run or args.reconcile_saved).resolve(strict=True) if continuing else
|
||||
REPO / 'build' / ('item-value-native-' + datetime.now().strftime('%Y%m%d-%H%M%S')))
|
||||
assert output.is_relative_to(REPO / 'build')
|
||||
if not continuing:
|
||||
output.mkdir(exist_ok=False)
|
||||
source_root = REPO / 'build/cbb-bom-import-live-20261015/projects'
|
||||
library = Path('F:/Sync Tool Skyworth/Work Maps/Skyworth_CBB_Lib/Mentor_lib_EE7.9/mentor_lib.lmc')
|
||||
previous = json.loads((output / 'report.json').read_text(encoding='utf-8')) if continuing else None
|
||||
source_hashes = previous['source_hashes'] if continuing else {name: hashes(source_root / name) for name in CASES}
|
||||
library_hash = hashlib.sha256(library.read_bytes()).hexdigest()
|
||||
report = {'output': str(output), 'verified': False, 'pcb_execution': False,
|
||||
'steps': [], 'source_hashes': source_hashes}
|
||||
report_path = output / ('reconciliation-report.json' if reconciling else
|
||||
'continuation-report.json' if continuing else 'report.json')
|
||||
if reconciling and report_path.exists():
|
||||
report_path = output / ('reconciliation-report-' + datetime.now().strftime('%H%M%S') + '.json')
|
||||
if continuing:
|
||||
assert not report_path.exists(), 'do not replay a previous continuation'
|
||||
report['initial_failure'] = previous.get('error')
|
||||
observation_copy = output / ('desktop-observation-before-reconciliation.json' if reconciling else 'desktop-observation-first-attempt.json')
|
||||
if not observation_copy.exists():
|
||||
shutil.copy2(output / 'desktop-observation.json', observation_copy)
|
||||
if reconciling:
|
||||
report['continuation_failure'] = json.loads((output / 'continuation-report.json').read_text(encoding='utf-8')).get('error')
|
||||
save(report_path, report)
|
||||
print('EVIDENCE=' + str(output), flush=True)
|
||||
started_all = time.monotonic()
|
||||
|
||||
def record(stage, started=None, **details):
|
||||
entry = {'stage': stage, **details}
|
||||
if started is not None:
|
||||
entry['seconds'] = round(time.monotonic() - started, 3)
|
||||
report['steps'].append(entry)
|
||||
save(report_path, report)
|
||||
print(json.dumps(entry, ensure_ascii=False, default=str), flush=True)
|
||||
|
||||
projects = []
|
||||
for name in CASES:
|
||||
destination = output / 'projects' / name
|
||||
if not continuing:
|
||||
shutil.copytree(source_root / name, destination)
|
||||
project = destination / (name + '.prj')
|
||||
text, count = re.subn(r'(?m)^KEY CentralLibrary .*$',
|
||||
lambda _: f'KEY CentralLibrary "{library.as_posix()}"', project.read_text(encoding='utf-8-sig'))
|
||||
assert count == 1
|
||||
if not continuing:
|
||||
project.write_text(text, encoding='utf-8')
|
||||
projects.append(project)
|
||||
pcb_before = {str(p.relative_to(output)): hashlib.sha256(p.read_bytes()).hexdigest()
|
||||
for project in projects for p in project.parent.rglob('*')
|
||||
if p.is_file() and ('PCB' in p.relative_to(project.parent).parts or p.suffix.casefold() == '.pcb')}
|
||||
store = TaskStore(output / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(output / 'data'))
|
||||
stop = Event()
|
||||
try:
|
||||
queue_id = None
|
||||
completed_statuses = {}
|
||||
if continuing:
|
||||
queue_path, = tuple((output / 'data/task-queues').glob('*.json'))
|
||||
queue_id = queue_path.stem
|
||||
queue = json.loads(queue_path.read_text(encoding='utf-8'))
|
||||
tasks = tuple(service.get_task(task_id) for task_id in queue['task_ids'])
|
||||
if reconciling:
|
||||
assert service.cleanup_batch_resources() == (), 'owned resources still need cleanup'
|
||||
for task in tasks:
|
||||
progress = service.quick_run_progress(task.task_id)
|
||||
if progress.queue_complete and not service.has_interrupted_quick_run(task.task_id):
|
||||
assert not progress.failed_items
|
||||
status = service.for_project_task(task.task_id)._quick_run_preparer.data_root / 'quick-run/status.tsv'
|
||||
completed_statuses[status] = status.read_bytes()
|
||||
assert len(completed_statuses) == 1, 'continuation expects one completed and one pending project'
|
||||
record('completed_project_will_not_be_replayed', queue_id=queue_id,
|
||||
completed_status_paths=list(map(str, completed_statuses)))
|
||||
else:
|
||||
tasks = service.create_project_tasks(output / 'projects', selected_paths=tuple(projects))
|
||||
assert len(tasks) == 2
|
||||
for task in tasks:
|
||||
if not continuing:
|
||||
service.set_copy_part_number_to_value(task.task_id, True)
|
||||
cache = REPO / 'build/explicit-cell-final-bom-20261019/data/cell-catalog'
|
||||
if not continuing and cache.is_dir():
|
||||
shutil.copytree(cache, output / 'data/cell-catalog')
|
||||
if not continuing:
|
||||
started = time.monotonic()
|
||||
choices = service.target_cell_choices(tasks[0].task_id, NEW)
|
||||
assert 'RESC1608N_0603' in choices, choices
|
||||
service.add_rule(tasks[0].task_id, OLD, NEW, target_cell='RESC1608N_0603')
|
||||
record('validated_target_cell_choices', started, choices=choices)
|
||||
sources = []
|
||||
for task in tasks:
|
||||
project = service.projects(task.task_id)[0].path
|
||||
records = tuple({'project_name': project.name,
|
||||
'project_path': 'Z:/migrated-old-location/' + project.name,
|
||||
'old_part_number': OLD, 'new_part_number': NEW, 'refdes': ref,
|
||||
'enabled': True, 'sync_nc': True, 'value_mode': mode, 'custom_value': custom}
|
||||
for ref, mode, custom in CASES[project.stem])
|
||||
if not continuing:
|
||||
sources.append(write_bom_template(output / (project.stem + '.xlsx'), records))
|
||||
with PrivateDesktopObserver(output):
|
||||
with BatchCadRuntime(output / 'data', 'item_value_schematic_live', stop) as runtime:
|
||||
if reconciling:
|
||||
uncertain = tuple(task for task in tasks if service.has_interrupted_quick_run(task.task_id))
|
||||
assert len(uncertain) == 1
|
||||
started = time.monotonic()
|
||||
recovered = service.recover_quick_run(uncertain[0].task_id, confirmed=True, runtime=runtime)
|
||||
assert recovered.state == 'restored_ok', recovered
|
||||
record('saved_result_reconciled_without_native_replacement', started,
|
||||
task_id=uncertain[0].task_id, result=asdict(recovered))
|
||||
if not continuing:
|
||||
started = time.monotonic()
|
||||
imported = service.preview_bom_sources(tasks[0].task_id, sources, runtime=runtime,
|
||||
stop_event=stop, on_progress=lambda message: print(message, flush=True))
|
||||
assert imported.preview is not None and imported.passed_rows == 4 and not imported.failed_rows, imported
|
||||
assert all('.xlsx' in row.reason for row in imported.rows)
|
||||
service.save_bom_batch(imported.preview, {p.task_id: p.rows for p in imported.preview.projects})
|
||||
record('two_migrated_excel_files_imported_and_saved', started, rows=[asdict(row) for row in imported.rows])
|
||||
before = {task.task_id: service.target_plan(task.task_id) for task in tasks}
|
||||
assert all(sum(row.enabled for row in rows) == 2 for rows in before.values())
|
||||
started = time.monotonic()
|
||||
result = execute_task_queue(service, tuple(task.task_id for task in tasks), stop_event=stop,
|
||||
runtime=runtime, queue_id=queue_id)
|
||||
runtime.result = result
|
||||
record('real_schematic_replacement_saved_and_reopened', started, result=asdict(result))
|
||||
assert not result.paused and all(entry.state == 'completed' for entry in result.entries), result
|
||||
assert all(path.read_bytes() == content for path, content in completed_statuses.items()), 'completed steps were replayed'
|
||||
for task in tasks:
|
||||
facade = service.for_project_task(task.task_id)
|
||||
command = facade._quick_run_preparer.data_root / 'quick-run/command.tsv'
|
||||
lines = command.read_text(encoding='utf-8-sig').splitlines()
|
||||
commands = [dict(zip(lines[0].split('\t'), line.split('\t'))) for line in lines[1:]]
|
||||
record('frozen_item_commands', task_id=task.task_id, commands=commands)
|
||||
|
||||
# A fresh CAD process independently reads the saved projects once.
|
||||
specs, destinations = [], {}
|
||||
evidence = output / 'independent-read/quick-run-history/sheet-discovery-item-values'
|
||||
evidence.mkdir(parents=True)
|
||||
for task in tasks:
|
||||
project = service.projects(task.task_id)[0].path
|
||||
spec, destination = evidence / (project.stem + '-spec.json'), output / (project.stem + '-saved.json')
|
||||
sheets = sorted({(row.target.schematic_name, row.target.sheet_number) for row in before[task.task_id]})
|
||||
save(spec, {'project': str(project), 'sheets': sheets, 'destination': str(destination)})
|
||||
specs.append(str(spec))
|
||||
destinations[task.task_id] = destination
|
||||
spec_list = evidence / 'specs.json'
|
||||
save(spec_list, specs)
|
||||
started = time.monotonic()
|
||||
with IsolatedCadSession(evidence) as cad:
|
||||
process = cad.start_worker((sys.executable, '-X', 'utf8', str(Path(__file__).resolve()),
|
||||
'--inspect-batch', str(spec_list)), stdout=-1, stderr=-1)
|
||||
try:
|
||||
code = process.wait(timeout=180)
|
||||
(evidence / 'worker-output.txt').write_bytes(process.stdout.read() + b'\n' + process.stderr.read())
|
||||
assert code == 0, (evidence / 'worker-output.txt').read_text(encoding='utf-8', errors='replace')
|
||||
finally:
|
||||
process.close()
|
||||
verified = []
|
||||
for task in tasks:
|
||||
actual = json.loads(destinations[task.task_id].read_text(encoding='utf-8'))
|
||||
selected = {row['refdes']: row for row in actual.values() if row['refdes'] in {'RV05', 'RV11'}}
|
||||
assert len(selected) == 2
|
||||
for row in before[task.task_id]:
|
||||
if not row.enabled:
|
||||
continue
|
||||
current = selected[row.target.refdes]
|
||||
assert current['uid'] == row.target.uid and current['part_number'] == NEW, current
|
||||
mode = 'part_label' if row.value_mode == 'inherit' else row.value_mode
|
||||
expected = {'part_label': current['part_label'], 'part_number': NEW,
|
||||
'custom': row.custom_value, 'preserve': ''}[mode]
|
||||
assert current['value'] == expected, (mode, current)
|
||||
if mode == 'preserve':
|
||||
assert 'Value' not in current['attributes'], current
|
||||
assert current['part_label'] == '0R' and current['pins']
|
||||
verified.append({'project': task.name, 'refdes': row.target.refdes, 'uid': current['uid'],
|
||||
'mode': mode, 'part_number': current['part_number'], 'value': current['value'],
|
||||
'part_label': current['part_label'], 'pins': len(current['pins'])})
|
||||
record('fresh_cad_independent_saved_readback', started, targets=verified)
|
||||
observation = json.loads((output / 'desktop-observation.json').read_text(encoding='utf-8'))
|
||||
assert not observation['observer_errors'] and not observation['owned_cad_became_foreground']
|
||||
assert not observation['owned_cad_on_input_desktop']
|
||||
assert all(observation['cad_exit_confirmed'].values()) and all(observation['desktop_absent'].values())
|
||||
report['verified'] = True
|
||||
except BaseException as error:
|
||||
report['error'] = f'{type(error).__name__}: {error}'
|
||||
report['traceback'] = traceback.format_exc()
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
report['source_projects_unchanged'] = all(hashes(source_root / name) == source_hashes[name] for name in CASES)
|
||||
report['library_lmc_unchanged'] = hashlib.sha256(library.read_bytes()).hexdigest() == library_hash
|
||||
report['pcb_files_unchanged'] = all((output / name).is_file() and hashlib.sha256((output / name).read_bytes()).hexdigest() == digest
|
||||
for name, digest in pcb_before.items())
|
||||
store.close()
|
||||
report['alias_cleanup_errors'] = list(map(str, cleanup_worker_aliases()))
|
||||
report['elapsed_seconds'] = round(time.monotonic() - started_all, 3)
|
||||
save(report_path, report)
|
||||
print(json.dumps({key: report.get(key) for key in ('verified', 'error', 'source_projects_unchanged',
|
||||
'library_lmc_unchanged', 'pcb_execution', 'pcb_files_unchanged', 'elapsed_seconds')}, ensure_ascii=False), flush=True)
|
||||
return 0 if (report['verified'] and report['source_projects_unchanged'] and report['library_lmc_unchanged']
|
||||
and report['pcb_files_unchanged'] and not report['alias_cleanup_errors']) else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,44 @@
|
||||
/** Upload owned files via the real composer menu and Qt's actual file chooser. */
|
||||
import assert from 'node:assert/strict';
|
||||
import {writeFile,readdir,readFile} from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
|
||||
export async function verifyFileUi(c,output){
|
||||
const wait=async(expression)=>{
|
||||
const end=Date.now()+30000;
|
||||
while(Date.now()<end){if(await c.evaluate(expression))return;await new Promise(r=>setTimeout(r,100));}
|
||||
throw Error('File UI timed out: '+expression);
|
||||
};
|
||||
const fixtures=path.join(output,'file-fixtures');
|
||||
const files=['dsh-upload.txt','dsh-upload.zip'].map(name=>path.join(fixtures,name));
|
||||
await writeFile(path.join(output,'file-picker-request.json'),JSON.stringify(files));
|
||||
// Plus opens the official command menu; choose its native attachment action.
|
||||
const plus=`[...document.querySelectorAll('button[aria-label="添加文件或调用指令"]')].find(e=>e.getBoundingClientRect().width>0)`;
|
||||
await c.click(plus);
|
||||
const fileOption=`[...document.querySelectorAll('[role="option"],button')].find(e=>e.getBoundingClientRect().width>0&&/^文件\\s*file/.test(e.textContent.trim()))`;
|
||||
await wait(`!!(${fileOption})`);
|
||||
await c.click(fileOption);
|
||||
await wait(`document.body.innerText.includes('dsh-upload.txt')&&document.body.innerText.includes('dsh-upload.zip')`);
|
||||
await wait(`![...document.querySelectorAll('[role="progressbar"]')].some(e=>e.getBoundingClientRect().width>0)`);
|
||||
await c.screenshot(path.join(output,'file-uploaded.png'));
|
||||
// Validate the uploaded bytes in Harness's actual owned storage, not the DOM echo.
|
||||
const roots=(await readdir(output)).filter(x=>x.startsWith('data-'));
|
||||
assert.equal(roots.length,1);
|
||||
const home=path.join(output,roots[0],'dsh/harness-home');
|
||||
const expected=await Promise.all(files.map(f=>readFile(f)));
|
||||
const found=new Set();
|
||||
async function scan(dir){
|
||||
for(const entry of await readdir(dir,{withFileTypes:true})){
|
||||
const filename=path.join(dir,entry.name);
|
||||
if(entry.isDirectory())await scan(filename);
|
||||
else if(entry.isFile()){
|
||||
const data=await readFile(filename);
|
||||
expected.forEach((bytes,i)=>{if(data.equals(bytes))found.add(i);});
|
||||
}
|
||||
}
|
||||
}
|
||||
const end=Date.now()+20000;
|
||||
while(found.size<files.length&&Date.now()<end){await scan(home);if(found.size<files.length)await new Promise(r=>setTimeout(r,150));}
|
||||
assert.equal(found.size,2,'text and ZIP must reach actual Harness file storage unchanged');
|
||||
return {status:'passed',realQtFilePicker:true,textAndZipUploaded:true,storedBytesMatched:true};
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/** Exercise actual Harness file tools against owned folders and ZIP archives. */
|
||||
import assert from 'node:assert/strict';
|
||||
import {mkdir,mkdtemp,writeFile,readFile,readdir} from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {createRequire} from 'node:module';
|
||||
import {bootRuntime} from '../integration/dsh/start.mjs';
|
||||
const require=createRequire(new URL('../integration/dsh/start.mjs',import.meta.url));
|
||||
const {zipSync,strToU8}=require('fflate');
|
||||
const output=path.resolve('build/dsh-files-verification');await mkdir(output,{recursive:true});
|
||||
const root=await mkdtemp(path.join(output,'run-'));
|
||||
for(const key of ['DSH_API_KEY','DSH_API_BASE','DSH_MODEL','DX_DSH_MODELS'])delete process.env[key];
|
||||
process.env.DX_DSH_HOME=path.join(root,'home');process.env.DX_DSH_CONTEXT_FILE=path.join(root,'context.json');
|
||||
await writeFile(process.env.DX_DSH_CONTEXT_FILE,'{"task_id":"files-fixture","revision":"files","status":"observe"}');
|
||||
const source=path.join(root,'输入 文件夹');await mkdir(source);
|
||||
await writeFile(path.join(source,'说明.txt'),'DSH_FILE_CONTENT_中文\n');
|
||||
const archive=path.join(source,'资料.zip');
|
||||
await writeFile(archive,zipSync({'子目录/说明.txt':strToU8('DSH_ZIP_CONTENT_中文'),'binary.bin':new Uint8Array([0,1,2,255])}));
|
||||
const {ctx,shutdown}=await bootRuntime({announce:false});
|
||||
try{
|
||||
const names=ctx.tools.schemas().map(x=>x.name);
|
||||
for(const name of ['read','read_image','glob','grep','working_directory','extract_zip'])
|
||||
assert.ok(names.includes(name),'Missing file capability: '+name);
|
||||
const workspace=await ctx.workspaceRegistry.create(process.env.DX_DSH_WORKSPACE,'File fixture');
|
||||
const {sessionId}=await ctx.sessionController.create({workspaceId:workspace.id,agentPreset:'dx-task'});
|
||||
const agent=ctx.agents.get(sessionId);
|
||||
const call=async(name,args)=>ctx.tools.execute({name,arguments:args,callId:crypto.randomUUID(),agent,signal:AbortSignal.timeout(20000)});
|
||||
const ok=async(name,args)=>{
|
||||
const result=await call(name,args);assert.equal(result.isError,false,name+': '+JSON.stringify(result));return result;
|
||||
};
|
||||
assert.match(JSON.stringify(await ok('read',{file_path:path.join(source,'说明.txt')})),/DSH_FILE_CONTENT_中文/);
|
||||
assert.match(JSON.stringify(await ok('glob',{pattern:'*',path:source})),/说明.txt/);
|
||||
assert.match(JSON.stringify(await ok('grep',{pattern:'DSH_FILE_CONTENT',path:source})),/DSH_FILE_CONTENT/);
|
||||
await ok('working_directory',{cd:source});
|
||||
assert.match(JSON.stringify(await ok('read',{file_path:'说明.txt'})),/DSH_FILE_CONTENT/);
|
||||
const extracted=await ok('extract_zip',{file_path:'资料.zip'});
|
||||
const destination=extracted.value.directory;
|
||||
assert.equal(await readFile(path.join(destination,'子目录/说明.txt'),'utf8'),'DSH_ZIP_CONTENT_中文');
|
||||
assert.deepEqual(new Uint8Array(await readFile(path.join(destination,'binary.bin'))),new Uint8Array([0,1,2,255]));
|
||||
assert.match(JSON.stringify(await ok('read',{file_path:path.join(destination,'子目录/说明.txt')})),/DSH_ZIP_CONTENT/);
|
||||
assert.notEqual((await ok('extract_zip',{file_path:archive})).value.directory,destination,'repeated extraction must not overwrite');
|
||||
await writeFile(path.join(source,'unsafe.zip'),zipSync({'../escaped.txt':strToU8('must not escape')}));
|
||||
const before=await readdir(source);
|
||||
assert.equal((await call('extract_zip',{file_path:'unsafe.zip'})).isError,true);
|
||||
assert.deepEqual(await readdir(source),before,'unsafe archive must not publish a partial directory');
|
||||
await writeFile(path.join(source,'broken.zip'),'not a zip');
|
||||
assert.equal((await call('extract_zip',{file_path:'broken.zip'})).isError,true);
|
||||
const corrupt=Buffer.from(zipSync({'evidence.txt':strToU8('ORIGINAL_CONTENT')},{level:0}));
|
||||
const payload=corrupt.indexOf(Buffer.from('ORIGINAL_CONTENT'));assert.ok(payload>0);corrupt[payload]=88;
|
||||
await writeFile(path.join(source,'corrupt.zip'),corrupt);
|
||||
assert.equal((await call('extract_zip',{file_path:'corrupt.zip'})).isError,true,'CRC mismatch must not report successful extraction');
|
||||
assert.equal(await readFile(path.join(source,'说明.txt'),'utf8'),'DSH_FILE_CONTENT_中文\n');
|
||||
ctx.emit('dx/diagnose-request',{request_id:'files-incident-probe',incident_id:'a'.repeat(24),phase:'diagnose',
|
||||
snapshot:{task_id:'files-fixture'},evidence:'Owned fixture; no model is configured.'});
|
||||
let incident;
|
||||
const deadline=Date.now()+20000;
|
||||
while(Date.now()<deadline){
|
||||
incident=ctx.agents.list().find(value=>value.id!==agent.id);
|
||||
if(incident)break;
|
||||
await new Promise(resolve=>setTimeout(resolve,50));
|
||||
}
|
||||
assert.ok(incident,'automatic incident session must be created');
|
||||
let marked=false;
|
||||
while(Date.now()<deadline){
|
||||
const snapshot=await ctx.tools.execute({name:'dx_task_snapshot',arguments:{},callId:crypto.randomUUID(),agent:incident,signal:AbortSignal.timeout(2000)});
|
||||
if(snapshot.value?.revision==='a'.repeat(24)){marked=true;break;}
|
||||
await new Promise(resolve=>setTimeout(resolve,50));
|
||||
}
|
||||
assert.ok(marked,'incident snapshot must be bound before checking admission');
|
||||
for(const [name,args] of [['read',{file_path:path.join(source,'说明.txt')}],['extract_zip',{file_path:archive}]]){
|
||||
const denied=await ctx.tools.execute({name,arguments:args,callId:crypto.randomUUID(),agent:incident,signal:AbortSignal.timeout(2000)});
|
||||
assert.equal(denied.isError,true,'automatic incident must not inherit interactive file access');
|
||||
}
|
||||
const report={status:'passed',realHarness:true,read:true,folderSearch:true,workingDirectory:true,
|
||||
zipTextAndBinary:true,repeatedExtractionNoOverwrite:true,zipTraversalRejected:true,invalidZipRejected:true,corruptZipRejected:true,
|
||||
automaticSessionIsolation:true};
|
||||
await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));console.log(JSON.stringify(report));
|
||||
}finally{await shutdown.shutdown(0);}
|
||||
@@ -58,6 +58,10 @@ try {
|
||||
await c.click(`document.querySelectorAll('button[aria-label="新建会话"]')[1]`);
|
||||
await wait(`!!document.querySelector('[data-testid="dx-task-panel"]')`);
|
||||
report.checks.push('native new conversation retains task dock');
|
||||
if(process.argv.includes('--file-check')){
|
||||
const {verifyFileUi}=await import('./verify_dsh_file_ui.mjs');
|
||||
report.files=await verifyFileUi(c,output);
|
||||
}
|
||||
if(!liveModel) {
|
||||
await c.click(`document.querySelector('button[aria-label="收起侧边栏"]')`);
|
||||
await wait(`!!document.querySelector('button[aria-label="打开侧边栏"]')`);
|
||||
@@ -86,7 +90,8 @@ try {
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyDown',key:'Escape',code:'Escape'});
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'Escape',code:'Escape'});
|
||||
await c.click(`document.querySelector('[contenteditable="true"]')`);
|
||||
const prompt=liveModel ? '请分析当前任务的状态和异常,告诉我任务名称、现在进展到哪一步以及接下来可以做什么。不要修改工程。' : '请分析当前任务的异常,并说明允许的处理方式。';
|
||||
const fileCheck=process.argv.includes('--file-check');
|
||||
const prompt=liveModel ? (fileCheck?'请分析当前任务的附件:读取上传的 dsh-upload.txt,再解压 dsh-upload.zip 并读取其中说明.txt,分别告诉我这两份文本的完整内容和解压后的真实目录。不要修改原文件或运行 CAD。':'请分析当前任务的状态和异常,告诉我任务名称、现在进展到哪一步以及接下来可以做什么。不要修改工程。') : '请分析当前任务的异常,并说明允许的处理方式。';
|
||||
report.prompt=prompt;
|
||||
report.explicitToolNamesInPrompt=false;
|
||||
await c.call('Input.insertText',{text:prompt});
|
||||
@@ -96,8 +101,8 @@ try {
|
||||
if(liveModel) {
|
||||
await c.click(`document.querySelector('button[aria-label="发送消息"]')`);
|
||||
const replies=`[...document.querySelectorAll('[data-chat-group-part="response"]')].map(e=>e.innerText).join('\\n')`;
|
||||
await wait(`(${replies}).length>30 && /任务|状态/.test(${replies})`,100000);
|
||||
await wait(`![...document.querySelectorAll('button[aria-label="停止生成"]')].some(e=>e.getBoundingClientRect().width>0)`,100000);
|
||||
await wait(fileCheck?`(${replies}).includes('DSH_UPLOAD_CONTENT')&&(${replies}).includes('DSH_ZIP_UPLOAD_CONTENT')`:`(${replies}).length>30 && /任务|状态/.test(${replies})`,fileCheck?180000:100000);
|
||||
await wait(`![...document.querySelectorAll('button[aria-label="停止生成"]')].some(e=>e.getBoundingClientRect().width>0)`,fileCheck?180000:100000);
|
||||
report.reply=await c.evaluate(replies);
|
||||
report.liveModelReplyTested=true;
|
||||
report.liveTurnCompleted=true;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import {mkdtemp,mkdir,readFile,writeFile,readdir} from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {bootRuntime,overlay,prepareProfileDirectory} from '../integration/dsh/start.mjs';
|
||||
import {bootRuntime,overlay,prepareProfileDirectory,TASK_TOOLS,FILE_TOOLS} from '../integration/dsh/start.mjs';
|
||||
|
||||
const output=path.resolve('build/dsh-profile-settings-verification');
|
||||
await mkdir(output,{recursive:true});
|
||||
@@ -34,7 +34,7 @@ assert.equal(await readFile(path.join(legacyDir,'cordis.patch.yml'),'utf8'),migr
|
||||
const {ctx,shutdown}=await bootRuntime({announce:false});
|
||||
try{
|
||||
assert.deepEqual((await ctx.llm.listModels('profile-fixture')).map(x=>x.id),['persisted-fixture-model']);
|
||||
assert.equal(ctx.tools.schemas().length,7);
|
||||
assert.equal(ctx.tools.schemas().length,TASK_TOOLS.length+FILE_TOOLS.length);
|
||||
console.log(JSON.stringify({status:'passed',nativeSettingsPreserved:true,actualProviderRestored:true,
|
||||
legacyMigrationPreservedModels:true,byteBackup:true,modelRequests:0}));
|
||||
}finally{await shutdown.shutdown(0);}
|
||||
@@ -4,7 +4,7 @@ import {mkdir,writeFile} from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import {fileURLToPath,pathToFileURL} from 'node:url';
|
||||
import {createRequire} from 'node:module';
|
||||
import {bootRuntime} from '../integration/dsh/start.mjs';
|
||||
import {bootRuntime,TASK_TOOLS,FILE_TOOLS} from '../integration/dsh/start.mjs';
|
||||
|
||||
const repo=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
|
||||
const output=path.join(repo,'build/dsh-native-cli-verification');
|
||||
@@ -18,7 +18,7 @@ const report={};
|
||||
try {
|
||||
const schemas=ctx.tools.schemas();
|
||||
report.tools=schemas.map(x=>x.name);
|
||||
assert.deepEqual(report.tools,['dx_task_snapshot','dx_task_create','dx_task_catalog','dx_task_preflight','dx_task_manage','dx_task_execute','dx_task_operations']);
|
||||
assert.deepEqual([...report.tools].sort(),[...TASK_TOOLS,...FILE_TOOLS].sort());
|
||||
const presets=await ctx.agentPresets.list();
|
||||
report.presets=presets.map(x=>x.id);
|
||||
assert.deepEqual(report.presets,['dx-task']);
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/** PR18 #197: no silently omitted names, bounded workers, real cancellation. */
|
||||
import assert from 'node:assert/strict';
|
||||
import {mkdir,mkdtemp,writeFile,readdir,readFile} from 'node:fs/promises';
|
||||
import {spawnSync} from 'node:child_process';
|
||||
import {createRequire,syncBuiltinESMExports} from 'node:module';
|
||||
import path from 'node:path';
|
||||
import {bootRuntime} from '../integration/dsh/start.mjs';
|
||||
const output=path.resolve('build/dsh-zip-review-verification');await mkdir(output,{recursive:true});
|
||||
const root=await mkdtemp(path.join(output,'run-'));
|
||||
const fixture=spawnSync('python',['-c',
|
||||
`import sys,zipfile,struct\nfrom pathlib import Path\np=Path(sys.argv[1])\nwith zipfile.ZipFile(p/'proto.zip','w',zipfile.ZIP_STORED) as z:z.writestr('__proto__','ORIGINAL_CONTENT')\ndata=bytearray((p/'proto.zip').read_bytes());n,e=struct.unpack_from('<HH',data,26);data[30+n+e]^=1;(p/'proto-bad.zip').write_bytes(data)\nwith zipfile.ZipFile(p/'many.zip','w',zipfile.ZIP_DEFLATED) as z:\n for i in range(20):z.writestr(str(i)+'.txt',b'x'*(600*1024))\n`,root],{encoding:'utf8'});
|
||||
assert.equal(fixture.status,0,fixture.stderr);
|
||||
const require=createRequire(import.meta.url),threads=require('node:worker_threads');
|
||||
const NativeWorker=threads.Worker;
|
||||
let active=0,peak=0,created=0,abortOnWorker;
|
||||
threads.Worker=new Proxy(NativeWorker,{construct(target,args){
|
||||
const worker=Reflect.construct(target,args);active++;created++;peak=Math.max(peak,active);
|
||||
worker.once('exit',()=>active--);
|
||||
if(abortOnWorker){const cancel=abortOnWorker;abortOnWorker=undefined;queueMicrotask(cancel);}
|
||||
return worker;
|
||||
}});
|
||||
syncBuiltinESMExports();
|
||||
for(const key of ['DSH_API_BASE','DSH_MODEL','DSH_API_KEY','DX_DSH_MODELS'])delete process.env[key];
|
||||
process.env.DX_DSH_HOME=path.join(root,'home');process.env.DX_DSH_CONTEXT_FILE=path.join(root,'context.json');
|
||||
await writeFile(process.env.DX_DSH_CONTEXT_FILE,'{"task_id":"zip-review","revision":"zip-review","status":"observe"}');
|
||||
const {ctx,shutdown}=await bootRuntime({announce:false});
|
||||
try{
|
||||
const call=(file,signal=AbortSignal.timeout(30000))=>ctx.tools.execute({name:'extract_zip',
|
||||
arguments:{file_path:path.join(root,file),output_directory:root},callId:crypto.randomUUID(),signal});
|
||||
const before=await readdir(root);
|
||||
for(const file of ['proto.zip','proto-bad.zip']){
|
||||
const result=await call(file);
|
||||
assert.equal(result.isError,true,'unsupported __proto__ entry must be explicitly rejected, including corrupt payload');
|
||||
assert.deepEqual(await readdir(root),before,'rejected archive must not publish an empty success directory');
|
||||
}
|
||||
peak=0;created=0;
|
||||
const results=await Promise.all(Array.from({length:4},()=>call('many.zip')));
|
||||
for(const result of results){
|
||||
assert.equal(result.isError,false,JSON.stringify(result));
|
||||
assert.equal(result.value.total_entries,20);
|
||||
for(let i=0;i<20;i++)assert.equal((await readFile(path.join(result.value.directory,i+'.txt'))).length,600*1024);
|
||||
}
|
||||
assert.equal(created,4,'each archive must use one worker, with no per-entry worker fan-out');
|
||||
assert.ok(peak<=2,'ZIP decompression must use at most two workers across concurrent calls, actual '+peak);
|
||||
assert.equal(active,0,'completed calls must release all workers');
|
||||
const cancel=new AbortController(),prior=await readdir(root);
|
||||
abortOnWorker=()=>cancel.abort(new Error('owned cancellation fixture'));
|
||||
const cancelled=await call('many.zip',cancel.signal).catch(error=>({isError:true,error}));
|
||||
assert.equal(cancelled.isError,true);
|
||||
assert.equal(active,0,'cancelled call must settle only after its worker exits');
|
||||
assert.deepEqual(await readdir(root),prior,'cancel must not publish a partial directory');
|
||||
const report={status:'passed',realHarness:true,protoEntryExplicitlyRejected:true,corruptProtoRejected:true,
|
||||
parallelArchives:4,filesPerArchive:20,peakWorkers:peak,cancelledWorkerStopped:true};
|
||||
await writeFile(path.join(root,'report.json'),JSON.stringify(report,null,2));console.log(JSON.stringify(report));
|
||||
}finally{threads.Worker=NativeWorker;syncBuiltinESMExports();await shutdown.shutdown(0);}
|
||||
Reference in new issue
Block a user