Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1340f6c233 | ||
|
|
5aeddacfc9 | ||
|
|
453d288a6c | ||
|
|
c86b0eefa0 | ||
|
|
56588a9203 | ||
|
|
cf90ced825 | ||
|
|
d36f3c2995 | ||
|
|
af46ee1617 | ||
|
|
374e1cd5bf | ||
|
|
4874c7e95a | ||
|
|
63835afba8 | ||
|
|
ba1d50cdfc | ||
|
|
e9454cf01f | ||
|
|
3cec9adc5f | ||
|
|
c1ad438591 | ||
|
|
2b2fdad56f | ||
|
|
c96c369161 | ||
|
|
4ef08e06d2 | ||
|
|
cd9431985e | ||
|
|
dba01d42d0 | ||
|
|
344a2797b0 | ||
|
|
caa79e7fcf | ||
|
|
df250e04ba | ||
|
|
efe7e8c813 | ||
|
|
b80988ccf7 |
No files matched your search
@@ -0,0 +1,65 @@
|
||||
name: Core regression
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main, 'codex/**']
|
||||
tags: ['v*']
|
||||
pull_request:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
portable-regression:
|
||||
name: Python core and release configuration
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: node:20-bookworm
|
||||
timeout-minutes: 20
|
||||
steps:
|
||||
- name: Checkout tested commit
|
||||
run: |
|
||||
git init .
|
||||
git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
|
||||
git fetch --depth=1 origin "${GITHUB_SHA}"
|
||||
git checkout --detach FETCH_HEAD
|
||||
- name: Install runner Python and test dependencies
|
||||
run: |
|
||||
apt-get update
|
||||
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3 python3-pytest
|
||||
python3 --version
|
||||
- name: Compile application and build helpers
|
||||
run: python3 -m compileall -q src packaging tools
|
||||
- name: Core regression (no CAD or production credentials)
|
||||
run: >-
|
||||
python3 -m pytest -q
|
||||
tests/dsh/test_core.py
|
||||
tests/dsh/test_failure_policy.py
|
||||
tests/dsh/test_decision_log.py
|
||||
tests/dsh/test_experience.py
|
||||
tests/dsh/test_issue_broker.py
|
||||
tests/packaging/test_dsh_runtime_staging.py
|
||||
tests/packaging/test_release_configuration.py
|
||||
tests/packaging/test_self_extracting_release.py
|
||||
--junitxml=build/ci-results.xml --tb=short
|
||||
- name: Windows release scope
|
||||
run: |
|
||||
echo 'Windows PyInstaller, Qt WebEngine startup, full runtime file hashes and extracted EXE smoke are validated on the Windows build host.'
|
||||
echo 'This Linux job does not claim Windows/CAD or live model acceptance.'
|
||||
|
||||
javascript-regression:
|
||||
name: JavaScript syntax
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: node:20-bookworm
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- name: Checkout tested commit
|
||||
run: |
|
||||
git init .
|
||||
git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
|
||||
git fetch --depth=1 origin "${GITHUB_SHA}"
|
||||
git checkout --detach FETCH_HEAD
|
||||
- name: Check runtime syntax
|
||||
run: |
|
||||
node --check integration/dsh/start.mjs
|
||||
node --check integration/dsh/task-plugin/host.mjs
|
||||
node --check integration/dsh/task-plugin/client.js
|
||||
@@ -4,14 +4,38 @@
|
||||
|
||||
面向 **Windows + DxDesigner / Expedition Enterprise 7.9.5** 的器件批量替换工具。一个工程对应一个任务,可以只替换同料号下的部分位号;支持直接编辑和 Excel BOM 导入,同批工程按顺序执行。
|
||||
|
||||
**最新版本:V1.1.4 Beta 4(预发布)** · [下载便携包](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/download/v1.1.4-beta.4/DxPartReplace-1.1.4-beta.4.zip) · [版本说明与附件](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/tag/v1.1.4-beta.4) · [完整使用说明](docs/user/v1.1.0-project-tasks.md)
|
||||
**最新版本:V1.2.2(预发布)** · [自解压 EXE](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/releases/download/v1.2.2/DxPartReplace-1.2.2.exe) · [ZIP 便携包](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/releases/download/v1.2.2/DxPartReplace-1.2.2.zip) · [版本说明与附件](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/releases/tag/v1.2.2) · [完整使用说明](docs/user/v1.1.0-project-tasks.md)
|
||||
|
||||
所有发布均标记为 **Beta 预发布**。V1.1.4 Beta 1 因 BOM 导入缺少 `win32timezone` 已撤回,请使用 Beta 4;旧 Git 标签保留供回溯。升级前备份 `data`,完整更新 EXE 与 `_internal`,不要只替换 EXE。
|
||||
V1.2.2 新增操作历史、安全续跑与内置 7-Zip 的自解压 EXE,同时提供完整 Node/Qt 运行时、DSH 任务功能和全部 UI 资源。升级前备份 data,完整更新 EXE 与 _internal,不要只替换 EXE。
|
||||
|
||||
Beta 4 面向停止与资源收尾复测:普通暂停等待当前项保存核验,写入中无进度不再自动杀进程;新增明确确认的强制结束,保留中断证据与恢复入口。[本版变更和复测说明](docs/releases/v1.1.4-beta.4.md)。
|
||||
本版合并 PR15 与 PR17,补齐操作历史和模型设置,提供自解压 EXE 与 ZIP 两种同内容发行包。[本版变更](docs/releases/v1.2.2.md)。
|
||||
|
||||
本版提供[目标封装选择](docs/user/target-cell-selection.md):目标封装可留空,此时工具不主动覆写封装,可后续与中心库同步;需要指定时,在料号规则中读取并选择合法封装。BOM 保持原 11 列。
|
||||
|
||||
## DSH助手:在工具内使用 AI
|
||||
|
||||
本项目已将 **DSH助手** 集成到任务中心主窗口。顶部固定显示两个入口:左侧 **任务中心**、右侧 **DSH助手**,单击即可切换,无需展开菜单;助手保留 DeepSeek Harness 官方三栏界面、SVG 动画、模型选择、会话和未发送草稿。也可以使用 `Ctrl+Shift+D` 打开助手。
|
||||
|
||||
DSH助手围绕真实任务中心工作,支持:
|
||||
|
||||
- 用自然语言创建任务、修改规则、复制或归档任务;工程选择和提交仍由原生窗口确认。
|
||||
- 查询任务快照、执行预检、影响范围和实际执行历史。
|
||||
- 在原生确认后按冻结范围执行任务,支持安全暂停、原队列恢复和排期授权。
|
||||
- 发生异常时自动介入,收集脱敏证据链、保留诊断记录,并在需要时提交受限 Git 工单。
|
||||
- 在右侧任务监管中查看运行、影响、排期、经验、工单和用量;审计损坏、证据缺失和未知计数会明确显示。
|
||||
|
||||
AI 不能直接写入 CAD、运行 Shell、访问任意文件或绕过确认门禁。自动异常诊断为只读;不确定执行不会自动重放,恢复必须重新核对冻结计划、检查点和业务证据。未配置模型时仍可使用任务监管和本地恢复功能,但不会产生真实模型回复。
|
||||
|
||||
首次接入打开 **工具设置 → 模型 → 添加模型提供商 → 自定义模型 API**,填写 API 地址、协议和密钥,点击 **获取可用模型** 并选择需要的模型,也可手动填写模型 ID。保存后在输入框下方选择模型。设置保存在任务中心数据目录的专用 Harness profile,重启会保留;主工具预配置的密钥继续由 Windows DPAPI 保护,模型设置页新增的密钥使用 Harness 本地凭据存储。开发环境也可通过 `DSH_API_BASE`、`DSH_MODEL`、`DSH_API_KEY` 临时提供配置。不要把数据目录、API key 或 Git 凭据提交到 Git。
|
||||
|
||||
详细的能力边界、异常介入、证据链、排期和工单说明见 [DSH助手使用与监管说明](docs/user/dsh-ai-supervision.md)。源码环境可用下面的命令检查运行时门禁:
|
||||
|
||||
```powershell
|
||||
$env:PYTHONPATH = 'src'
|
||||
python -X utf8 -m dx_productivity.dsh --doctor
|
||||
node tools/verify_dsh_runtime.mjs
|
||||
```
|
||||
|
||||
## 图文教程
|
||||
|
||||
按下面 **13 步**,完成“创建任务 → 填 BOM → 导入校验 → 保存计划 → 替换 → 查看结果”。本例有两个工程 `Audio_D2128`、`Audio_T605A`,各自只替换 `RV05`,保留同料号下的 `RV11`。
|
||||
@@ -20,7 +44,9 @@ Beta 4 面向停止与资源收尾复测:普通暂停等待当前项保存核
|
||||
|
||||
### 第 1 步:解压并打开应用
|
||||
|
||||
[下载 V1.1.4 Beta 4 便携包](https://git.simgreek.com/simgreek/dxdesigner-part-replacement/releases/download/v1.1.4-beta.4/DxPartReplace-1.1.4-beta.4.zip),**完整解压**,双击 `DxPartReplace.exe`。保留它与 `_internal` 的相对位置,无需安装 Python;本机需要可用的 DxDesigner、许可证和中央库。
|
||||
推荐下载上方自解压 EXE,双击后选择新的解压目录,完成后运行目录内的 DxPartReplace.exe。自解压引擎已内置,无需另外安装 7-Zip,也无需通过 Windows 资源管理器解压。
|
||||
|
||||
[下载 V1.2.2 便携包](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/releases/download/v1.2.2/DxPartReplace-1.2.2.zip),**完整解压**,双击 `DxPartReplace.exe`。保留它与 `_internal` 的相对位置,无需安装 Python;本机需要可用的 DxDesigner、许可证和中央库。
|
||||
|
||||
打开后看到下图主窗口。首次使用请准备完整的工程副本,执行前保存并关闭待处理工程。
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# PR #17 提供方迁移修复
|
||||
|
||||
目标:修复 PR #17 评论 #168 的旧地址与新密钥错配,保留原生自定义提供方及其独立凭据。
|
||||
|
||||
设计:`dx` 且引用启动环境 `DSH_API_KEY` 的配置归主工具管理,每次启动从当前主工具配置完整刷新;独立凭据的提供方保留。Harness 的插件 config 整块替换,因此用户 providers 存在时须同时纳入当前主工具提供方。迁移旧生成配置时,生成的 dx 默认模型回到当前主工具默认值;原生用户选定的有效 dx 模型和自定义提供方默认模型继续保留。没有主工具配置时清除旧应用管理的路由与默认选择。修改前保存原文件备份,重复初始化不重复改写。
|
||||
|
||||
凭据来源补充:上游本地保存的引用优先于环境变量。显式 dx 使用本地保存的 DSH_API_KEY 时,整条路由和密钥作为原生用户覆盖保留。启用了主工具模型、却只有孤立本地密钥时拒绝启动,不从历史 policy 猜测地址;没有主工具模型时不阻断其他独立提供方。首次正常启动即在原生用户配置中物化完整的应用 dx,确保此后模型页仅修改密钥也能保留它所属的地址和模型。配置准备不修改凭据文件,解析错误不包含密钥原文。
|
||||
|
||||
绑定证明以 Harness 真实 composition 为准,保留 name assertion / insert 语义,拒绝凭据、模型适配器和默认模型的重复 ID。从有效 credentials 配置解析实际文件路径。启动前确认最终应用路由与当前配置一致;桌面接入前再次核对实际凭据来源和仅在内存中的密钥摘要,避免环境与文件来源互换。
|
||||
|
||||
不采用只删除旧 dx 的方案:用户的 providers 整块覆盖会遮住新的应用提供方。不采用保留旧地址并换密钥的方案:会破坏凭据绑定。
|
||||
|
||||
执行步骤:
|
||||
|
||||
- [x] 新增真实 Harness/本机 HTTP 回归,覆盖旧 overlay、更换提供方、已有迁移结果、自定义提供方/默认模型、重复启动及主工具配置清除;先确认当前代码失败。
|
||||
- [x] 在启动配置准备阶段按来源刷新应用提供方,保留其他原生配置及凭据文件。
|
||||
- [x] 跑 HTTP 回归、现有 runtime/profile 检查、Python 回归和实际 Qt 模型设置点击;未复现点击失败,保留原脚本。
|
||||
- [x] 更新验收记录与 PR 描述,检查 diff 和秘密泄露后提交、推送到现有 PR;不合并。
|
||||
@@ -0,0 +1,12 @@
|
||||
# V1.2.1
|
||||
|
||||
本版合并 PR #14 的 DSH 启动错误提示,并提供完整 Windows 便携运行包。
|
||||
|
||||
- 包含整个已安装的 DSH Node 依赖目录、Node 可执行文件、官方前端、任务插件、Qt WebEngine 运行库和全部 UI 图标。
|
||||
- 按维护者的部署要求保留 issue reporter 配置;模型 API 密钥及个人任务数据不随包分发。
|
||||
- 应用标题、Python 项目元数据和 Windows 产品版本统一为 1.2.1。
|
||||
- 新增 Gitea Actions 回归,覆盖平台无关核心逻辑与发行配置。Windows 冻结程序另在构建机验收。
|
||||
|
||||
完整解压 `DxPartReplace-1.2.1.zip`,运行目录内的 `DxPartReplace.exe`。请保留 `_internal` 的完整目录结构,升级前备份旧版 `data`。
|
||||
|
||||
旧版在本版通过验证并公开后转为草稿下架,历史标签保留。运行和云端验证的实际结果见发行附件中的 build-validation.json。
|
||||
@@ -0,0 +1,7 @@
|
||||
# V1.2.2
|
||||
|
||||
本版本合并 PR15 的操作历史与安全续跑改进,并提供带内置 7-Zip 解压引擎的 Windows 自解压包。
|
||||
|
||||
下载 `DxPartReplace-1.2.2.exe` 可使用自解压界面选择目标目录;`DxPartReplace-1.2.2.zip` 作为同内容回退包。两种包都包含完整的 `DxPartReplace.exe`、`_internal`、DSH Node 运行时、Qt WebEngine 依赖和资源文件。
|
||||
|
||||
解压后请保留 `DxPartReplace.exe` 与 `_internal` 的相对位置。本机仍需可用的 DxDesigner、许可证和中央库。沿用旧任务前先安全退出旧版本并备份 `data`,不要同时使用不同版本打开同一数据目录。
|
||||
@@ -0,0 +1,78 @@
|
||||
# Operation History and Durable Resume Implementation Plan
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox syntax for tracking.
|
||||
|
||||
**Goal:** Add searchable, clearable operation history and prevent completed operations from replaying on continue/restart.
|
||||
|
||||
**Architecture:** Keep user history and durable execution receipts in separate tables of the task SQLite database. Reuse current command/status validation, owned runtime leases, attempt markers and native recovery. History is an asynchronous Qt page; it never decides execution admission.
|
||||
|
||||
**Tech Stack:** Python, SQLite, PySide6, existing Windows CAD supervisor.
|
||||
|
||||
**Spec:** `docs/superpowers/specs/2026-11-03-operation-history-execution-design.md`
|
||||
|
||||
## Global Constraints
|
||||
|
||||
- Preserve the dirty main workspace; work only in the isolated review worktree.
|
||||
- Do not change DSH functionality, protocols, providers or model tools.
|
||||
- A safe CBB failure continues to later independent engineering tasks; uncertain results stop the queue.
|
||||
- Continue, restart and clear-history never establish a new round or replay verified operations.
|
||||
- Never run real CAD or model requests for verification; use controlled workers and sample projects.
|
||||
|
||||
## Review Focus
|
||||
|
||||
- Missing/corrupt/truncated status or command files must not look like a fresh task.
|
||||
- A crash after worker success but before receipt commit requires reconciliation, never a second replacement.
|
||||
- Retry revisions cannot repackage successful targets into new operations.
|
||||
- Clear confirmation must bind count and global cutoff; new events and execution receipts survive.
|
||||
- First-launch legacy history migration cannot resurrect records after clear.
|
||||
|
||||
### Task 1: History repository and page
|
||||
|
||||
Files: create `history_store.py`, `ui/operation_history.py`, `tests/v1_1/test_operation_history.py`, `tests/ui/test_operation_history_page.py`.
|
||||
|
||||
Interfaces:
|
||||
- `create_history_schema(connection)` is called by StoreSchema inside its transaction.
|
||||
- `HistoryRepository(store).append(task_id, task_name, project_path, operation, state, summary, details, source_key)` returns a global event ID; keyword arguments, details default empty, source_key optional and permanently deduplicated.
|
||||
- `page(query='', state='', before_id=None, limit=100, read_control=None)` returns HistoryPage with `.rows` (tuple of dictionaries), `.next_cursor`, `.total`.
|
||||
- `clear_snapshot()` returns snapshot with `.cutoff_id`, `.count`; `clear(cutoff_id)` clears only that user-history range, retaining source tombstones and completion tables.
|
||||
- `OperationHistoryPage(service,parent=None)` consumes service methods `operation_history_page`, `operation_history_clear_snapshot`, `clear_operation_history`. Exposes `set_active(bool)`, `set_mutations_enabled(bool)`, `shutdown()`.
|
||||
|
||||
- [x] Write failing repository tests for Unicode search, pagination, deduplication, clear-cutoff concurrency, persistence and transaction rollback.
|
||||
- [x] Implement repository and asynchronous page; write UI tests for search, filtering, empty/error states, detail and confirmed/declined clear.
|
||||
- [x] Verify focused tests before integration.
|
||||
|
||||
### Task 2: Conservative incomplete-handoff admission
|
||||
|
||||
Files: modify `quick_run_snapshot.py`, `quick_run.py`; create `handoff_guard.py`, `tests/v1_1/test_resume_evidence.py`.
|
||||
|
||||
Interfaces: `assert_handoff_intact(root,task_id)` rejects missing/malformed/truncated/inconsistent evidence, leaves all bytes untouched. Snapshot returns interrupted evidence instead of executable zero progress.
|
||||
|
||||
- [x] Reproduce missing and truncated command/status cases, normal first prepare and normal resume.
|
||||
- [x] Add service-level preparation and dispatch guards, retaining existing error codes where required.
|
||||
- [x] Verify the original resume probe now blocks missing evidence without extra worker calls.
|
||||
|
||||
### Task 3: Persistent execution receipts
|
||||
|
||||
Files: create `execution_journal.py`, tests; modify `store_schema.py`, `service.py`, `quick_run.py`, `batch.py`, `new_round.py`.
|
||||
|
||||
Interfaces: `ExecutionJournal(store)` owns active rounds, immutable command manifests, per-item attempts, target claims and receipts. QuickRunPreparer receives a journal from its service and carries it through per-task clones. Before worker dispatch it validates/reconciles the frozen plan and claims one attempt; after validated worker result it commits the receipt and its history event before next dispatch. Late/mismatched attempts cannot commit. Snapshot checks journal/file consistency and pauses unfinished attempts.
|
||||
|
||||
- [x] RED/GREEN tests for normal pause/restart, missing evidence, prepared/uncertain attempts, stale callbacks, rollback, retry, new rounds and changed target grouping.
|
||||
- [x] Keep old evidence readable; migrate terminal legacy rows only from validated matching command/status evidence.
|
||||
- [x] Record receipt and history atomically; do not allow history clear to touch admission state.
|
||||
|
||||
### Task 4: Safe failure, history producers and UI integration
|
||||
|
||||
Files: modify `batch.py`, `task_queue.py`, `service.py`, `ui/main_window.py`, `ui/execution_state.py`, `ui/icons.py`, tests.
|
||||
|
||||
- [x] Add failing integration tests: safe failure stops the current CBB's remaining items and runs the next CBB; uncertainty stops all; ordinary continue excludes failed/held tasks; explicit failure retry only retries eligible work.
|
||||
- [x] Integrate the history schema, service calls and top-level navigation, with named controls and preserved task/DSH navigation.
|
||||
- [x] Record actual task/configuration changes and native results, not read-only polling or model suggestions.
|
||||
- [x] Import supported legacy task events once with durable source IDs; clearing does not reimport them.
|
||||
|
||||
### Task 5: Review and verification
|
||||
|
||||
- [x] Run focused safety, history, UI and new-round tests; run the complete project suite and report any skips/failures.
|
||||
- [x] Extend native sample-window acceptance for history/search/clear and 980×700 layout; repeat Chromium glass navigation.
|
||||
- [x] Fresh review of concrete diff against spec; fix critical/important findings with regressions.
|
||||
- [x] Commit and push the existing authorized PR branch, update PR scope/evidence without merging it.
|
||||
@@ -0,0 +1,232 @@
|
||||
# 操作历史与防重复执行:详细执行逻辑
|
||||
|
||||
状态:已实施,验证及兼容性决定见 `docs/testing/2026-11-03-operation-history-execution.md`。本文延续《operation-history-resume-research》,用户已明确重复执行场景为同一个任务继续或重启后;不修改 DSH 功能。
|
||||
|
||||
用户已确认:某个 CBB 失败后,只要已经证明没有残留改动且资源已释放,跳过本次失败,继续后面的工程;结果不确定或核验失败时仍停止。
|
||||
|
||||
## 1. 执行范围与基本约束
|
||||
|
||||
“CBB 工程执行过”不等于“该工程所有替换都做完”。不能按文件名、工程路径或任务状态一个字段去重。
|
||||
|
||||
使用三层范围:
|
||||
|
||||
1. 任务:绑定实际工程,保留用户的任务身份。
|
||||
2. 轮次:一次明确确认的工程改动目标。继续、重启、清空历史不创建新轮次。
|
||||
3. 执行项:一次原生执行器实际接受的完整范围,通常为某工程/图页的一条替换规则及其目标集合。一项可能涉及多个位号;页面展开后可显示逐位号结果。
|
||||
|
||||
现有领域模型是一任务绑定一个工程。多个 CBB 组成批次,每个工程分别对应自己的任务和轮次;批次层负责顺序、依赖与失败后继续策略,不把多个工程塞入同一个新任务。整图页规则模式和精确位号模式均适用,不能仅在 BOM 模式下防重复。
|
||||
|
||||
同一任务、同一轮次中,已核验成功的执行项永不因“继续”而再次派发。一个图页完成,不代表其他图页完成;一条规则完成,不代表该 CBB 已完成。
|
||||
|
||||
正常默认行为:前面的完整项跳过,当前未完成项接续,结果不确定时停止。不得为了让进度走完,把失败、未命中、用户跳过或核验失败统一标为“替换成功”。
|
||||
|
||||
## 2. 稳定身份与冻结计划
|
||||
|
||||
### 2.1 身份
|
||||
|
||||
`operation_id` 在确认计划时生成一次,保存后不重新编号。必须区分以下身份,不能只对随机 ID 做唯一约束:
|
||||
|
||||
- `plan_snapshot_hash`:包含计划版本的完整冻结快照,用于检查确认范围有没有变化。
|
||||
- `logical_operation_key`:可重算的同轮次逻辑项身份,**不包含计划版本、operation_id、attempt_id 或临时 job_id**,防止重复冻结/换版本绕过去重。
|
||||
- `target_claim`:已核验目标的占用记录,防止把同一目标重新拆组/合组后再次派发。精确位号执行须对 `(task_id, round_id, 原目标身份)` 做完成占用约束,成功后不能释放该占用来准备普通重试。
|
||||
|
||||
逻辑项身份输入至少包含:
|
||||
|
||||
- `task_id`、显式持久化的 `round_id`;
|
||||
- 规范工程身份与路径(使用现有 canonical/path identity 工具,不能只比较 CBB 名称);
|
||||
- 原理图、图页;
|
||||
- 执行模式:整图页规则或精确目标;
|
||||
- 旧料号、新料号;目标 Cell;
|
||||
- 全部目标 UID/位号及其必要的原始身份;目标集合按稳定顺序编码;
|
||||
- Value 策略及自定义 Value、NC 策略。
|
||||
|
||||
`attempt_id` 代表一次尝试,每次获准重试都不同;临时 `job_id` 只关联执行器,不是防重复的唯一身份。数据库对同任务/同轮次逻辑项以及原目标完成占用同时约束,另以 CAS 约束单项活动尝试,禁止相同执行项被并发领走。
|
||||
|
||||
整图页规则模式在不能提供稳定目标身份时,不允许把已派发项重新拆组或扩大后继续。必须复用原冻结范围;需要改变范围则先核验,再显式确认新轮次,不能因为另一个版本的逻辑项 key 不同就重新处理成功目标。
|
||||
|
||||
### 2.2 冻结
|
||||
|
||||
确认前展示真实待执行范围和计数,事务内持久化轮次、计划及全部执行项,再允许启动执行器。计划保存失败,不派发 CAD。
|
||||
|
||||
第一次派发后原计划保持冻结。筛选、排序、页面切换不能改变计划。需要修正失败项时生成明确的失败项修订计划,预览并重新确认;已成功项不纳入该重试范围。不能通过随手换一个计划 hash,让原成功项变成“新任务”。
|
||||
|
||||
新轮次通过现有“开始新一轮”的显式入口进行:核验旧轮次、归档旧证据、创建新的活动轮次身份、重新采集当前工程基线、重新确认。新轮次的数据库变更与归档日志沿用现有崩溃恢复机制,不在启动时自动创建。
|
||||
|
||||
## 3. 执行项状态与允许操作
|
||||
|
||||
下表为执行项的设计状态,不表示现有 `TaskStatus` 已新增这些枚举。
|
||||
|
||||
| 状态 | 判定依据 | 点击继续 | 能否调用替换 |
|
||||
| --- | --- | --- | --- |
|
||||
| 待执行 `pending` | 已冻结计划,无已派发尝试 | 检查准入后领取 | 可以 |
|
||||
| 已领取/准备 `prepared` | 派发身份已持久化,还未确认执行结果 | 核对进程、租约和尝试证据 | 仅明确证明未进入修改且旧执行器已退出后可重新领取 |
|
||||
| 可能已修改 `mutation_possible` | 修改前保护标记已落盘,或运行中断无法排除修改 | 进入核验,不能当待执行 | 不可以 |
|
||||
| 保存待核验 `saved_unverified` | 有保存结果,缺少完整重开回读证明 | 只做核验/恢复 | 不可以 |
|
||||
| 已核验完成 `verified` | 当前项身份匹配,保存、重开回读和范围核验通过 | 跳过,读取下一项 | 不可以 |
|
||||
| 未命中 `no_match` | 原确认范围的只读检查明确为零匹配 | 本轮跳过,不反复枚举 | 不可以 |
|
||||
| 已确认失败 `failed_safe` | 明确未修改,或已核验回滚到基线 | 普通继续不重试;用户显式准备失败项重试 | 通过重试准入后可以 |
|
||||
| 结果不确定 `uncertain` | 证据缺失/矛盾、租约不明、保存结果无法确认 | 阻断并指引核验 | 不可以 |
|
||||
| 用户跳过 `skipped` | 用户明确跳过并给出原因 | 本轮不执行,计为跳过 | 不可以 |
|
||||
|
||||
`restored_ok` 必须按其具体恢复证明解释:恢复到旧基线表示回滚已核验,不是替换成功;只有恢复流程明确证明目标结果已保存并重开核验,才能归并为 `verified`。
|
||||
|
||||
“未命中”只在当前冻结范围及有效工程状态下成立。它不是已保存修改,历史页不得将其列入替换成功数;要重新查找不同工程内容,走新范围确认,而不是在本轮继续中自动重置。
|
||||
|
||||
## 4. 一项操作的实际执行顺序
|
||||
|
||||
1. **准入**:取得现有队列/监督进程租约,核对项目身份、原计划、资源释放状态和停止意图。并发窗口不能同时执行一个工程或一个执行项。
|
||||
2. **核对回执**:命中本轮终态就跳过;遇到不完整尝试先恢复,不创建新队列绕过它。
|
||||
3. **领取**:用数据库比较并更新(CAS)将 `pending` 或已显式获准重试的项转为 `prepared`,保存 attempt 身份。领取失败或刷盘失败,不派发。
|
||||
4. **修改前检查**:确认旧执行器已结束,工程当前状态与预期基线一致,目标身份、料号和策略相符;完成备份。
|
||||
5. **写前保护**:沿用 `batch_attempt.prepare_attempt` / `mark_mutation_possible` 的身份和代次绑定。进入原生修改前必须可靠发布“可能已修改”证据,失败时拒绝修改。
|
||||
6. **执行与核验**:执行当前项,保存、关闭、重新打开回读;核对目标结果及现有非目标保护条件,关闭/释放本项资源。
|
||||
7. **提交回执**:在任务数据库同一事务中写入终态回执、推进轮次计数/工程预期状态、追加用户历史源事件。事务成功后才允许派发下一项。
|
||||
8. **更新页面**:UI 从已提交记录展示结果。页面刷新、回调或弹窗失败不会改变已提交终态,更不能触发重试。
|
||||
|
||||
任务数据库与外部 CAD 保存无法组成一个跨进程原子事务。因此只承诺“有可靠完成证据的不重放;不确定时先核验”,不承诺无法兑现的严格 exactly-once。
|
||||
|
||||
## 5. 暂停、继续、重启分别怎么走
|
||||
|
||||
### 5.1 完成当前项后暂停
|
||||
|
||||
- 先立即设置现有内存停止控制以阻止领取下一项,再持久化本轮暂停意图。
|
||||
- 暂停意图落盘失败时,当前进程仍保持停止状态并明确提示“暂停请求未能保存,请等待当前项收尾”;不能因保存异常而继续执行。重启后默认不自动执行,因此丢失暂停意图也不能成为自动派发依据。
|
||||
- 当前正在执行的项按现有安全机制收尾;其真实结果先提交,再停止领取下一项。
|
||||
- 暂停请求本身不代表当前项成功;如果当前项核验失败,显示待核验或安全失败。
|
||||
- 如果请求落在最后一项结束时,仍保留用户暂停意图,不能因“队列刚好耗尽”而自动开始失败项重试。
|
||||
- 重启后继续是用户对剩余范围的明确授权,不能由应用启动自动替代。
|
||||
|
||||
### 5.2 点击继续
|
||||
|
||||
1. 读取原任务、原轮次和冻结计划;显示上次完成/未命中/失败/待执行/待核验计数。
|
||||
2. 与持久化逐项回执及现有原生执行证据对账;原队列 ID 丢失不能导致逐项身份丢失。
|
||||
3. 验证当前工程是工具上次已核验后的预期状态。
|
||||
4. 汇总并提示本次实际待执行范围。例如:“原计划 12 项,已完成 7 项、未命中 2 项,本次继续 3 项”。
|
||||
5. 若存在待核验项,先阻断;否则只领取剩余合法 `pending` 项。
|
||||
|
||||
普通继续只处理可派发的待执行项,不包含失败项,也不解除“同工程失败后暂缓”的项。若只剩失败和暂缓项,显示“需处理失败项”,提供失败项重试/核验入口,而不是仍提供会反复空跑的继续按钮。
|
||||
|
||||
### 5.3 重启工具
|
||||
|
||||
启动只恢复显示和核对状态,不自动执行 CAD,不默认清空状态,不凭任务为 paused 就断言可重试。
|
||||
|
||||
- 原执行器仍存活或资源释放未证实:显示处理中/等待释放,不创建第二个执行器。
|
||||
- 记录完整且所有尝试均已收尾:保留原完成项,展示可继续的剩余范围。
|
||||
- 存在可能修改但未提交结果的项:转为待核验。
|
||||
- 数据库、文件、计划或步骤身份冲突:展示“恢复受阻”,保留原记录供核对。
|
||||
|
||||
### 5.4 强制结束
|
||||
|
||||
沿用原有强制结束确认与保护记录。不能把强制结束当作回滚成功,也不能把它之后的所有项统一标失败后自动重试。只把确实无法排除修改的当前项转为待核验;从未派发的后续项保持待执行。
|
||||
|
||||
## 6. 崩溃位置与恢复决定
|
||||
|
||||
| 中断位置/证据组合 | 恢复决定 |
|
||||
| --- | --- |
|
||||
| 冻结计划事务尚未提交,且没有派发/修改痕迹 | 不执行;重新确认计划 |
|
||||
| 已领取,有匹配的 prepared 证明,旧进程已退出且明确未进入修改 | 原执行项保留;在重新授权后创建新 attempt |
|
||||
| 修改标记已发布,未收到完整结果 | 待核验,不重放 |
|
||||
| 已保存或 worker 报成功,但终态回执尚未提交 | 从绑定的原生证据核验;证明通过后补记回执,不再次替换 |
|
||||
| 回执已提交,UI 尚未显示就退出 | 读取已提交终态,跳过该项 |
|
||||
| 命令还在但状态文件缺失/损坏 | 有有效新回执时对账;否则阻断,不返回“0 项、未执行” |
|
||||
| 数据库终态与文件终态冲突,或回执引用的证据无法验证 | 已终态项不重新派发;暂停续跑,核对冲突 |
|
||||
| 旧版本没有新回执,只有完整且可绑定的旧执行证据 | 经兼容校验导入同轮次记录;无法证明的项进入待核验,不按进度数字补造成功 |
|
||||
| 一次执行涵盖多个位号,结果部分通过 | 整项待核验,展开列出各位号观测;使用现有核验/恢复能力处理,禁止对整组盲目重试 |
|
||||
|
||||
任何“缺文件就创建新状态文件”“读取失败就归零”“找不到原队列就从头新建”的降级都不允许。
|
||||
|
||||
### 6.1 记录权威与旧回调隔离
|
||||
|
||||
- 已提交的数据库终态是调度依据;TSV、普通日志、UI 回调不能把它降级成待执行,也不能覆盖为另一次尝试的结果。
|
||||
- 提交必须匹配当前 `operation_id + attempt_id + generation`。旧尝试晚到的回调只保留为诊断证据,不推进新尝试、汇总进度或新的工程基线。
|
||||
- 证据与数据库冲突时阻断并核验,不任意挑一份覆盖另一份。恢复只能按证据补齐合法的单调状态转换。
|
||||
- 租约超时只意味着需要检查,不证明旧进程已退出。只有已证明旧进程退出或原执行代次被有效隔离,且修改风险已解决,才允许新的尝试。
|
||||
|
||||
## 7. 工程变化、失败与其他 CBB 的处理
|
||||
|
||||
### 7.1 工具自己改过的工程
|
||||
|
||||
不能把暂停后的工程与最初项目文件哈希直接比较:前面的合法替换必然会改变它。
|
||||
|
||||
保存基线链:初始已确认工程状态 → 第 1 项核验后状态 → 第 2 项核验后状态。继续时比较最近已提交回执对应的预期状态,核对将要执行的目标及现有工程/库安全身份。
|
||||
|
||||
工程语义检查沿用原生快照和目标身份核验。证据文件字节哈希用于检验证据完整性;不能把易变数据库时间戳直接等同于工程语义变化,也不能因路径相同而忽略实际工程被替换。
|
||||
|
||||
检测到额外修改时,先列出差异,阻断当前续跑,由用户核验并重新确认范围;不自动“接受新基线”来清掉旧回执。
|
||||
|
||||
### 7.2 失败后是否继续其他 CBB
|
||||
|
||||
用户已选择“跳过这次失败,继续后面的工程”。因此默认按以下规则处理:
|
||||
|
||||
1. 明确确认当前工程未修改,或已经完整回滚且核验通过,并确认资源释放后,该 CBB 本次收尾,进入确认范围内下一个可独立执行的工程。
|
||||
2. 原失败项仍记为失败,带错误和证据,不改成成功,也不把它伪装成普通的“用户跳过”。同一 CBB 中尚未派发的后续项保持“待执行/本次未执行”,带 `project_failure` 暂缓原因,不误标完成,不在该次失败后继续领走。只有该工程失败项已妥善解决、范围仍有效并重新确认,才解除暂缓。
|
||||
3. 如果后续工程对失败工程有已声明的依赖,则该后续工程记为依赖受阻;继续其他无依赖且可执行的工程,不自动忽略依赖。
|
||||
4. 未知结果、核验失败、资源未释放、证据冲突始终阻断整队,不能使用“继续后面的工程”覆盖该保护。
|
||||
5. 本次结束显示“X 个工程完成、Y 个工程失败、Z 个工程未执行/依赖受阻”。“队列收尾”不能等同于“所有改动成功”。
|
||||
|
||||
失败项重试通过明确的“准备失败项重试”入口,限定原失败项,建立新 attempt 并保留原失败记录。成功项、未命中项和未派发项不被整体清空后重新编号。
|
||||
|
||||
### 7.3 汇总状态
|
||||
|
||||
- 有活动执行项:执行中。
|
||||
- 有不确定/证据冲突项:待核验/恢复受阻。
|
||||
- 用户暂停且仍有待执行项:已暂停。
|
||||
- 所有项都有明确终态,但存在失败或用户跳过:执行已收尾、有失败/跳过,不能显示“全部替换成功”。
|
||||
- 所有项均为 verified 或有效 no_match:本轮完成,并分别显示保存与未命中数量。
|
||||
|
||||
补充任务/批次层级的收尾规则:
|
||||
|
||||
- 单工程任务存在 `failed_safe`,其余未执行项均因 `project_failure` 暂缓时,显示“需处理失败项”,禁用普通继续,提供失败项处理入口;不把它归成用户暂停。
|
||||
- 批次所有可独立派发工程已经收尾,剩余仅失败、暂缓或依赖受阻时,显示“本次执行已收尾,有失败/未执行”。它不是全部完成,也不反复提供会空跑的批次继续按钮。
|
||||
- 只有仍存在通过准入的独立待执行项时,才显示可执行的“继续剩余项”及其确切数量。
|
||||
|
||||
以上是汇总推导规则,映射到现有任务状态时保留兼容性;不能只依赖一个 `TaskStatus.COMPLETED` 字段代替逐项判定。
|
||||
|
||||
## 8. 历史记录与清空
|
||||
|
||||
执行回执、原始诊断证据、用户历史分别管理;“分别管理”不要求拆成不同数据库。
|
||||
|
||||
建议回执、历史源事件与汇总更新使用同一任务数据库事务,避免“CAD 已完成、页面显示成功但没有完成回执”。历史页是可查询的显示记录,不是继续执行的判定来源。
|
||||
|
||||
### 8.1 历史记录内容
|
||||
|
||||
保存动作发生时间、任务/轮次、工程、执行项、动作来源、尝试编号、实际结果、图页位号、新旧料号,以及有证据支持的 Value/NC/Cell 差异。开始请求和完成结果是不同事件。无改动和失败不能填造成功差异。
|
||||
|
||||
用户历史覆盖实际的新建/编辑/归档任务、规则及位号计划保存、执行/暂停/恢复、核验结果和因完成回执跳过的摘要。搜索、翻页、轮询、读取快照不生成新的操作历史,避免页面自己制造海量记录。配置改动保存有证据的前后差异;不记录模型密钥等凭据。
|
||||
|
||||
搜索参数化查询任务、工程、位号、新旧料号和说明;结果按数据库全局单调的 `history_event_id` 倒序做游标分页,同时间事件不漏页。现有 JSONL 的 `sequence` 是每任务序号,不能当全局分页或清空序号。详情展示原计划、真实改动、核验结论及失败原因。旧版本缺少的字段明确显示“旧版本未记录”。
|
||||
|
||||
### 8.2 清空的精确语义
|
||||
|
||||
- 默认明确命名“清空全部历史”,确认框展示即将清理的历史数量;即使搜索正在生效,也不能让按钮范围含糊成“只清当前页还是全部”。
|
||||
- 不清理任务、完成回执、检查点、原生证据及备份,不改变本轮任何执行项状态。
|
||||
- 打开确认框时,用同一数据库快照取得全局 `history_event_id` 截止值与将清理的数量。用户确认后严格按该截止值清理,不能重新读取更大的截止值,把确认期间的新事件也清掉。
|
||||
- 清空事务更新持久化 `cleared_through_id`;查询只显示该边界之后的记录。需要物理清理时只删除截止值以内的独立历史行。重建索引或重启不能清除这个水位;事件序号使用不会因删除全部历史而复用的序列(如 AUTOINCREMENT 或独立持久计数器),清空不重置序号。
|
||||
- 新历史源事件与执行回执同事务写入,因此不存在“成功回执很早提交,历史却以新时间延迟生成”导致旧记录复活的窗口。UI 延迟读取不会改变事件序号。
|
||||
- 旧 JSONL 兼容导入单独保存来源去重键、迁移完成状态及清理水位,不能仅因源文件轮转或重建索引再次导入。首次历史迁移未完成时禁用清空;正常查询刷新不禁用清空。
|
||||
- 建议活动执行/恢复期间禁用清空,以降低用户对范围的误解;并发新事件仍以事务边界保证:截止值之后的事件保留,不能误删用户确认后才完成的操作。
|
||||
- 清空失败时保留原页面及记录,提示重试;不能先把表格清掉再假装后端成功。
|
||||
|
||||
## 9. 示例
|
||||
|
||||
批次 G 包含任务 TA 与 TB,分别绑定 CBB 工程 A 和 B,各自保留轮次 RA/RB。每工程两个图页、每图页两条规则,共 8 项。
|
||||
|
||||
先完成 A 的第 1 页两项,再完成 A 第 2 页第一项,然后暂停:
|
||||
|
||||
- A:3 项已核验,1 项待执行;不能因“A 跑过了”跳过余下一项。
|
||||
- B:4 项待执行。
|
||||
- 重启后点击继续:显示跳过 3 项,本次执行 5 项;不再执行 A 已完成的前三项。
|
||||
- 如 A 第三项的保存核验尚未结束就强制退出:保留前 2 项已核验,第三项待核验,之后 5 项待执行;先恢复第三项,不能重新跑前 2 项或直接运行 B。
|
||||
- 清空操作历史:页面旧记录清理,但以上执行项状态原样保留。
|
||||
- 如 A 第三项明确安全失败:A 保留失败项和 1 项因工程失败暂缓的待执行项;跳转 B 执行其 4 项。再次普通继续不会偷偷重试 A;处理 A 的失败项后重新确认,才接续其剩余范围。
|
||||
- 全部完成后再次点击执行:显示“本轮已完成”,不启动执行器;用户明确开始新一轮后,重新确认基于当前工程的改动目标。
|
||||
|
||||
## 10. 实施与验收拆分
|
||||
|
||||
1. 最小保护补丁:复现状态文件丢失/损坏/截断,并在快照读取、准备队列、服务派发入口统一阻断;验证现有首次执行和正常续跑不被误阻断。
|
||||
2. 持久化计划与逐项回执:补齐任务/轮次身份、CAS 领取、attempt 绑定、终态原子提交,以及旧记录兼容对账。
|
||||
3. 恢复矩阵:对本文第 6 节逐点故障注入,实际统计同一逻辑项的执行器派发次数,不能仅检查按钮和进度数字。
|
||||
4. 历史页:接入真实事务事件、搜索分页、展开详情和明确范围的清空;覆盖“清空后重启不再现旧历史,但不重复执行”。
|
||||
5. UI 回归及原生窗口验收:保留玻璃隔离、分类/搜索、未保存编辑保护及 980×700 可见性。
|
||||
|
||||
验收底线:正常继续只派发剩余项;不确定结果没有替换派发;完成回执与清空历史互不影响;任何跨进程崩溃窗口都不能靠归零掩盖。
|
||||
@@ -0,0 +1,88 @@
|
||||
# 操作历史与同任务断点续跑:研究及建议方案
|
||||
|
||||
状态:研究完成,方案待实施;未修改产品代码、DSH 功能或真实工程。
|
||||
|
||||
执行项身份、状态机、暂停/重启、崩溃恢复矩阵和清空语义已进一步细化,见 [详细执行逻辑](2026-11-03-operation-history-execution-design.md)。
|
||||
|
||||
## 用户目标与已确认场景
|
||||
|
||||
- 增加独立的操作历史页面,让用户知道工具实际做过哪些改动。
|
||||
- 页面可以搜索、清空历史。
|
||||
- 用户已明确:重复执行发生在**同一个任务点击继续或重启工具后**,本轮不把它解释成重新导入 CBB 或重新新建任务。
|
||||
- 用户进一步确认:一个 CBB 已确认安全失败且资源已释放后,跳过这次失败并继续后面的工程;不确定结果仍停止。
|
||||
- 保留既有 UI 风格,DSH 功能仍不在本轮修改范围内。
|
||||
|
||||
## 当前实现与证据
|
||||
|
||||
1. 现有“替换结果/日志”按当前任务展示,数据来自 `tasks/<task_id>/events.jsonl`,已经提供分段读取和日志轮转;没有全局可搜索的操作历史页面,也没有独立的用户历史清理语义。
|
||||
2. `task_queue.execute_task_queue` 有持久化的任务级队列检查点。`QuickRunPreparer` 根据命令与状态文件重建每个工程/图页/规则的完成进度,正常情况下跳过已完成项。
|
||||
3. UI 的批次 `_project_queues` 为内存状态,重启后不保留,但**这本身不等于必然重放**:独立探针在不复用原任务队列 ID 时仍正确跳过了已完成步骤。
|
||||
4. 已复现保护缺口:`quick_run_snapshot._read_snapshot_once` 只认同时存在 `command.tsv` 和 `status.tsv` 的候选目录。状态文件缺失时,已有命令及任务的暂停状态没有阻止其返回“无证据、未中断、0 项完成”。后续继续可以重新建立执行队列。
|
||||
|
||||
### 独立复现
|
||||
|
||||
使用真实 TaskStore、TaskCenterService、QuickRunPreparer 和任务队列;CAD 执行器为受控替身。一个任务包含 2 个图页 × 2 条规则,共 4 项。先完成 2 项并暂停,关闭数据库,重新实例化服务和数据库,再继续。
|
||||
|
||||
| 重启场景 | 重启读取的完成项 | 总执行器调用 | 重复的逻辑项 |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| 原队列 ID,状态文件完整 | 2 | 4 | 0 |
|
||||
| 新队列 ID,状态文件完整 | 2 | 4 | 0 |
|
||||
| 原队列 ID,状态文件缺失 | 0 | 6 | 2 |
|
||||
| 新队列 ID,状态文件缺失 | 0 | 6 | 2 |
|
||||
|
||||
缺失文件用例中,原状态文件仅在临时夹具内改名保存,未删除用户数据。任务重启读取仍为 paused,`interrupted=False`,随后重放了前两项。不能据此断言用户现场的文件一定缺失;现场日志与任务证据仍需用于定位其具体触发原因。
|
||||
|
||||
探针:本地 `build/research_task_resume.py`。结果:本地 `build/task-resume-research.json`。这两项不是发布组件。
|
||||
|
||||
既有回归:`python -m pytest -q tests/v1_1/test_multi_project_regression.py tests/v1_1/test_task_queue.py tests/v1_1/test_new_round.py --tb=short`:**26 passed**。
|
||||
|
||||
## 实施方式比较
|
||||
|
||||
| 方式 | 优点 | 不足 |
|
||||
| --- | --- | --- |
|
||||
| 在现有日志弹窗上添加搜索及清空 | 改动小 | 仍按单任务查看;日志清理容易与恢复证据混淆;无法补足丢失状态文件的防重复保护 |
|
||||
| 独立操作历史页面+独立完成回执(建议) | 可跨任务查改动,重启后可查询,清空页面与续跑互不影响 | 需要明确区分用户历史、执行回执和诊断日志,并覆盖原子写入/恢复边界 |
|
||||
|
||||
建议采用第二种。本方案是实现建议,尚未由用户选择实现细节。
|
||||
|
||||
## 操作历史页面
|
||||
|
||||
- 顶部增加“操作历史”导航,和“任务中心”并列;沿用 SVG、玻璃导航与实色数据表,独立于 DSH 页面。
|
||||
- 默认按时间倒序。列表包含时间、任务/CBB 工程、操作、结果、改动摘要;展开显示位号、图页、旧料号→新料号、Value/NC 改动以及对应证据来源(有记录时才显示)。
|
||||
- 搜索覆盖任务名、工程名/路径、位号、新旧料号及改动说明;提供结果筛选及分页,避免大批记录加载阻塞 UI。
|
||||
- 区分“请求执行”“实际保存并核验”“未命中”“失败”“已跳过”,不能把按钮点击或 AI 建议当作已完成修改。旧日志缺少的配置差异不追溯编造。
|
||||
- 搜索为空时展示空态,异步读取失败可以重试;清空需要确认并展示影响数量。
|
||||
- “清空历史”只清理独立的用户可见历史记录。它不删除工程、任务、备份、诊断证据、执行检查点或完成回执。清空后新操作照常记录,旧记录不会在轮询或重启后自动重新导入页面。
|
||||
|
||||
## 防重复执行
|
||||
|
||||
### 第一层:立即补齐恢复门禁
|
||||
|
||||
- 已存在命令、活动步骤或原执行痕迹,但命令/状态文件缺失、损坏、截断或相互矛盾时,不将任务当作从未执行。
|
||||
- 显示“执行记录不完整,请核验后恢复”,阻止重新准备队列和写入派发。
|
||||
- 检查必须在服务端执行入口也生效,不能只隐藏/禁用 UI 按钮。
|
||||
- 旧任务没有足够证据时保持阻断,不能根据进度数字或一条普通日志补造“已核验完成”。
|
||||
|
||||
### 第二层:持久化到每个执行项的完成回执
|
||||
|
||||
- 保存任务、轮次、原确认计划指纹、规范工程路径、图页、目标范围/UID、新旧料号、Value/NC/Cell 策略、结果与证据关联。
|
||||
- 使用同一任务和轮次内稳定的操作身份;不能只靠每次重新生成的 job_id 或单独的 CBB 文件名去重。
|
||||
- `saved_ok`、明确核验通过的恢复结果和 `no_match` 应按各自含义保留终态;失败、未核验保存和未知结果不能冒充成功。
|
||||
- 继续前核对回执、原计划和现有证据:完整完成项跳过;待执行项接续;证据缺失或不一致时停止并提示核验。
|
||||
- 进程可能在 CAD 保存完成、回执落盘之前退出,因此不能承诺跨外部 CAD 写入与数据库的严格 exactly-once。该窗口必须依靠已有 attempt/快照证据核验,不能自动重放。
|
||||
- 只有用户明确启动“新一轮”并重新确认计划,才建立新的执行范围;清空历史、重启或点击继续都不产生新轮次。
|
||||
|
||||
## 验收重点
|
||||
|
||||
1. 同任务正常暂停/重启后,已完成项的执行器调用次数不增加;只执行余下项。
|
||||
2. 不带原队列 ID 的重启仍不能重放已完成项。
|
||||
3. 命令/状态缺失、文件损坏/截断、状态互相矛盾时阻止 CAD 派发,并保留原始证据。
|
||||
4. 写入成功但回执未落盘时,进入核验流程而不是再次调用替换。
|
||||
5. 未命中、失败和未核验保存分别显示,并遵守现有恢复/重试门禁。
|
||||
6. 清空页面历史后重启,完成回执与防重复保护仍有效,旧页面记录不重新出现。
|
||||
7. 全局搜索、筛选、翻页、失败重试和清空确认可用,大记录量下主界面保持响应。
|
||||
8. 页面切换、980×700 布局、玻璃及实色回退沿用现有验收。
|
||||
|
||||
## 下一步实施顺序
|
||||
|
||||
先补缺失/损坏状态文件的最小失败回归与服务端门禁,再增加独立历史记录和完成回执,最后接入可搜索/分页/清空的历史页面。分别验证,再提交可审查的改动;不在研究阶段修改 DSH 或运行真实 CAD。
|
||||
@@ -0,0 +1,17 @@
|
||||
# DSH 运行包与入口修复验证
|
||||
|
||||
## 问题
|
||||
|
||||
发布规格此前把 `integration/dsh/node_modules` 整个目录递归复制进 PyInstaller,包含非 Windows 平台二进制、实验包、测试目录、TypeScript 源码、source map 和文档,导致包体偏大。另一个问题是 DSH 懒启动发生导入、Qt WebEngine 或 Node 运行包异常时,异常从 QAction 槽冒出,桌面上看起来像点击没有反应。
|
||||
|
||||
## 修复
|
||||
|
||||
- 打包前生成 Windows x64 DSH 运行时暂存目录:保留当前平台依赖、JavaScript/JSON/原生运行文件和官方 Harness 包;去除其他平台包、测试/示例/缓存目录、TypeScript 源码、source map 和文档。优先使用硬链接暂存,跨卷时自动回退复制,不修改源 `node_modules`。
|
||||
- DSH助手入口捕获懒启动异常,写入现有应用错误日志,并在任务中心操作栏显示“DSH助手启动失败,请检查发布包中的运行时和 Node.js”,避免无反馈点击。
|
||||
|
||||
## 验证
|
||||
|
||||
- 精简运行时单元回归和 DSH UI/打包/原生运行时回归:**48 passed**。
|
||||
- 精简后的实际 Harness 暂存目录运行 `start.mjs --doctor`:返回 `status=ready`,官方前端、7 个受控工具和 Node 版本信息正常。
|
||||
- 源码真实 Qt/CDP 点击验收:报告 `build/dsh-click-after-fix-9267/desktop-report.json`,直接导航、官方三栏页面、SVG/动画、模型选择、原生取消确认、草稿保留和正常退出通过;`uncaughtErrors=false`、`unexpectedUiErrors=false`。
|
||||
- 本轮未构建发布 EXE,未操作真实 CAD 或远端工单;PyInstaller 发布时会使用新的暂存逻辑。
|
||||
@@ -0,0 +1,50 @@
|
||||
# 工单 #16:DSH 切换材质与模型设置
|
||||
|
||||
基线:`main@3cec9ad`。工单:[切换DSH工具遇到的一些问题](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/issues/16)。
|
||||
|
||||
## 修复内容
|
||||
|
||||
首次展示 WebEngine 时,Qt 将透明主窗口重建为 RHI 图形合成表面。返回任务中心后,重新调用 Acrylic API 或预设 alpha buffer 均不能恢复玻璃效果。DSH 现在通过 `WA_DontCreateNativeAncestors` 和 `WA_NativeWindow` 独立承载 Chromium 原生表面,保留任务中心的原 HWND 与 Acrylic。未改变系统透明度策略及“减少透明度”回退。
|
||||
|
||||
恢复官方 `ui-settings-models` 插件,提供模型提供商、自定义模型 API、手动模型列表和“获取可用模型”。通过上游公开的配置项关闭凭据引导,并使用固定 Harness 版本对应的欢迎说明配置,保留无账户/充值入口的任务工具界面。
|
||||
|
||||
重启验收发现旧启动脚本还会覆盖原生用户 `cordis.patch.yml`。应用配置现放在独立 `@dx/dsh-policy` bundle 中,原生用户配置作为上层保留。识别到旧应用生成的 overlay 时,先保存包含 SHA256 的逐字节备份,再迁移提供商与默认模型配置。原生模型页新增密钥使用 Harness 本地凭据存储,主工具预配置继续使用 DPAPI,文档分别说明。
|
||||
|
||||
## Windows 实际显示与点击
|
||||
|
||||
- 修复前屏幕截图:进入 DSH 再返回后,侧栏和右侧摘要对两种自有底图的差异均为 **0**;Acrylic 属性仍显示 true,因此属性不能作为显示成功证明。
|
||||
- 最终材质验收:`build/dsh-issue16-acrylic-final/acceptance.json`。主 HWND 全程相同,切换前后侧栏差异均为 **21.82**,右侧摘要差异均约 **9.30**;数据表差异为 **0**,保持实色。减少透明度、紧凑布局和正常关闭通过。
|
||||
- 模型页面实际输入/点击:`build/dsh-issue16-model-final/desktop-report.json`。添加自定义 API、调用自有本机测试服务器的 `GET /v1/models`、选择模型、保存提供商、重新打开编辑器读回模型均通过。未调用真实模型生成、CAD 或远端工单接口。
|
||||
- 独立新进程重启报告:`build/dsh-issue16-model-final/restart-report.json`。重启真实 Harness 后读取到页面创建的 `issue16-fixture-model`,未发起模型请求。
|
||||
- 同轮桌面验收还通过无登录/首次引导、官方页面、侧栏、SVG 动画、模型选择器、原生取消确认、导航后草稿保留及清洁退出。
|
||||
|
||||
## 回归
|
||||
|
||||
- DSH、UI、打包、队列与安全停止:412 passed(在原生子窗口修复后运行)。
|
||||
- `node tools/verify_dsh_profile_settings.mjs`:原生设置重复初始化不改写、真实提供方加载、旧配置迁移与原字节备份通过。
|
||||
- 官方 runtime CLI 门禁、模型结果裁剪与 dock 启动检查通过。
|
||||
- 首轮独立代码审查未发现新增阻断问题;后续 PR 评论 #168 发现的提供方迁移缺陷及补充验证见下文。
|
||||
|
||||
## PR #17 评论 #168:提供方与凭据绑定
|
||||
|
||||
原迁移按插件 ID 整行保存 `llm-pi-ai`,把旧版自动生成的 dx 地址、模型和默认选择提升为用户覆盖。更换主工具提供方后,旧地址/模型覆盖新策略,环境中的密钥却已经换新。原有 412 项回归及模型列表读取没有覆盖真正请求的地址与认证头。
|
||||
|
||||
修复后,引用启动环境 DSH_API_KEY 的应用 dx 每次完整刷新,并与同一插件中的自定义提供方共同保留。旧生成默认选择迁移到当前主工具默认值;原生有效选择和自定义默认模型保留。已迁移过一次的数据也会纠正;没有主工具配置时移除旧应用路由。改写前备份,重复准备不会再次改写原生配置。
|
||||
|
||||
上游本地凭据优先于环境变量,因此进一步按真实来源区分:显式 dx 使用已保存的 DSH_API_KEY 时,整条用户路由保留;主工具模型启用时,孤立保存密钥没有有效提供方地址证据便拒绝启动,不猜测历史地址。没有主工具模型时,无关孤立引用不阻断其他独立提供方。正常首次准备就物化应用 dx 的完整配置,确保原生模型页只保存密钥后仍有地址和模型绑定。配置准备不改写凭据文件,元数据解析错误不引用密钥原文。
|
||||
|
||||
凭据绑定使用 Harness 真正的 composition 结果校验,不能把被 name assertion 跳过或只执行 insert 的行当成绑定;保留旧模型行的原有语义。对 credentials、llm-pi-ai、agent-default-model 的重复 ID 明确拒绝,避免 Loader 的最后项挂载语义与检查结果不同。实际凭据路径从有效 credentials 配置解析,兼容备用 path / dshHome。桌面接入前通过真正挂载的凭据服务再次核对来源和仅内存保存的值摘要,环境管理路由也必须完整等于当前主工具配置。
|
||||
|
||||
验证:
|
||||
|
||||
- `node tools/verify_dsh_provider_switch.mjs`:先在旧地址断言处观察失败,再验证修复。最终 **18 个场景通过、14 次真实 Harness 本机 HTTP 请求**;逐一核对 `/v1/chat/completions` 目的服务器、模型与虚构认证头,含旧 overlay、自定义默认模型、已迁移数据、原生自定义提供方、有效 dx 选择、独立凭据 dx、本地保存密钥、只修改密钥、孤立密钥拒绝、被跳过的配置行、重复插件 ID、备用凭据路径及主配置移除。报告:`build/dsh-provider-switch-verification/run-HgwLis/report.json`。
|
||||
- 额外的本地保存密钥与孤立密钥用例均在修复前观察失败。孤立密钥拒绝不改变原用户补丁,也不发起模型请求。
|
||||
- `node tools/verify_dsh_profile_settings.mjs`:原生独立配置保留、真实提供方加载、迁移与逐字节备份通过。
|
||||
- `python -m pytest -q tests/v1_1 tests/ui tests/dsh tests/packaging --tb=short`:**1353 passed**,1 条 pywinauto COM 模式警告。
|
||||
- `python -X utf8 tools/accept_dsh_desktop.py --output build/pr17-review168-final --port 9290 --provider-config <本机配置目录> --model-settings-check`:最终源码的真实 Qt/CDP 点击、输入、获取模型、创建、编辑器读回和正常关闭通过,未复现评论中的 API 地址点击被遮挡。未修改点击脚本,本机验收通过不作为已定位该次失败原因的证明。
|
||||
- runtime 七项工具门禁、模型结果和 dock 启动验证通过。JS 语法与 `git diff --check` 通过。
|
||||
- 最终独立复审重新验证了备用文件显式绑定与重复 ID 的拒绝,未发现新增已验证 P1/P2;5 个提交文件扫描本机已知凭据,无匹配。
|
||||
|
||||
所有新增模型请求仅到 127.0.0.1 自有测试端点,使用虚构密钥;没有外部模型生成、CAD 写入、远端工单写入或 EXE 构建。
|
||||
|
||||
本轮没有构建冻结 EXE;发布包须从修复分支重新构建,源码验收不替代发布包验收。
|
||||
@@ -0,0 +1,57 @@
|
||||
# 操作历史与持久执行回执验收
|
||||
|
||||
范围:PR #15 的 UI 分支,新增独立操作历史页和同任务逐项执行保护;DSH 源码未改动。验证使用临时工程、受控执行器和原生 Qt 窗口,不调用真实 CAD 或模型。
|
||||
|
||||
## 可复现故障
|
||||
|
||||
原探针中,两个图页、每页两条规则,完成两项后暂停。完整状态下重启共派发四次;删除 `status.tsv` 后重启派发六次,其中两项重复。新回归在状态/命令缺失、空状态、截断或整目录缺失时阻断,原派发次数保持两次。
|
||||
|
||||
安全失败回归:A/B 两工程各四项,A 完成一项后安全失败,余下两项暂缓,B 四项执行,总派发六次。普通继续不增加派发;明确准备 A 重试后增加三次,A 的首项仍仅执行一次。
|
||||
|
||||
## 独立审查与修复
|
||||
|
||||
审查使用独立上下文,检查具体未提交源码及新增文件,发现并修复以下五项:
|
||||
|
||||
1. 精确位号重试使用冻结图页范围,避免用旧料号重新校验成功目标;R1 成功、R2 失败后的重试仅调用 R2。
|
||||
2. 最后一项原生成功但回执未提交时,完成短路前核验证据并补记回执;调用次数保持一次,之后可明确开启新轮次。
|
||||
3. 重试准备以暂存目录、原备份校验、持久转换标记及数据库提交记录发布;备份复制异常回滚原队列,目录切换中断可在重启恢复。
|
||||
4. 恢复使用可重放的审计提交标记。数据库写入异常后再次恢复,`restored_ok`、`retry_ready` 两种情况均不再调用执行器。
|
||||
5. 回执绑定快照、原生报告、原生尝试身份及完整图页语义状态,独立保留证明文件和摘要。恢复下一项前核对最近已核验状态;证据丢失、修改、读取期间变化或缺少保存关闭证明均阻断。
|
||||
|
||||
上述修复均有先失败后通过的回归。原生 VBScript 保护通过 Windows `cscript.exe` 执行提取的生产例程,验证语义差异被阻断、组件枚举顺序不影响比较。
|
||||
|
||||
## 原生窗口
|
||||
|
||||
运行:`python tools/accept_acrylic_ui.py --output build/history-execution-native --webengine-roundtrip --history`。
|
||||
|
||||
Windows 11 build 26200、125% 缩放:操作历史导航、真实配置改动、搜索、详情、Qt 清空确认、清空后任务保留通过;两轮 Chromium 页面往返后毛玻璃和不透明表格隔离通过;降低透明度、分类搜索及 980×700 布局通过。输出 `acceptance.json` 与 `native-app-history*.png`。CAD 启动标记为 false。
|
||||
|
||||
## 测试记录
|
||||
|
||||
- 默认全套第一轮:1229 passed,1 条现有 pywinauto STA 提示。
|
||||
- 定向安全、回执、恢复及 VBScript 例程:123 passed;另补证据读取竞态及缺少关闭证明回归,两项先失败后通过。
|
||||
- 最终默认全套:1233 passed,1 条现有 pywinauto STA 提示,255.82 秒。
|
||||
- 扩展 `tests/trial_task_center tests/dsh tests/packaging tests/integration`:1018 passed、1 skipped;首次另有 1 项旧夹具没有活动图页。补齐夹具并增加语义证明断言后,整个原生会话组 26 passed、1 skipped。跳过项是本机没有 7.9.5 生成的类型库。
|
||||
- 最后修复后的定向组:回执/证据/恢复 54 passed,原生保护 75 passed,并发与界面交接 46 passed。上述组包含扩展运行后新增的中断点和锁序回归。
|
||||
|
||||
边界:没有真实 CAD 工程端到端验收,没有构建或发布 EXE。原生保存、关闭、重开回读仍沿用现有执行器机制,本次增加持久回执、证明绑定和续跑准入。
|
||||
|
||||
## 实施决定
|
||||
|
||||
- 当前独立工程任务采用用户确认的 CBB 失败暂缓策略;旧版未绑定的多工程服务接口保留原有行为,以免改变历史 API 的兼容语义。当前 UI 将这类旧任务作为历史查看。
|
||||
- 真实执行的首次未命中也经过一次完整原生图页只读核验,建立语义基线;不再把仅含料号的发现结果直接升级为完整回执。代价是首次处理未命中页可能多一次只读会话,本轮继续不重复处理。
|
||||
- 本地 `codex/ui-update` 被保留给未提交的其他工作,本次在隔离工作树的 `codex/ui-history-execution` 上提交,再正常推送到已授权的远端 `codex/ui-update`,不触碰主工作区、不合并 PR。
|
||||
|
||||
最终复跑的临时目录调整:C: 空间不足导致 SQLite/pytest 建目录失败;同步工作树内的长临时路径又触发原生资源文件访问拒绝和旧桥接测试辅助脚本的路径限制。最终命令通过进程局部 TEMP/TMP/TMPDIR 以及 `--basetemp` 使用 F: 上独立、无空格的短路径,不改变用户环境。历史集成测试使用唯一搜索标记,避免初始位号配置历史同样包含 R1 时错误断言只有一条匹配。
|
||||
补充并发与恢复验收:
|
||||
|
||||
- 并发回归先复现“服务缓存锁内初始化历史记录、等待数据库锁”的反向锁序,再改为在缓存锁外构造任务服务,仅在短临界区发布缓存。并发/界面交接组 46 passed,DSH 源码未修改。
|
||||
- 恢复提交增加“数据库和文件已经绑定,但最终提交标记尚未删除”的中断点;补记保留原生结果原文,避免重放时把正常文件判为冲突。回执/证据/恢复组 54 passed。
|
||||
- 图页缺失、清单变化或读取异常统一按语义基线冲突处理,不能降级成普通安全失败后越过核验。生产 VBScript 与原生失败保护组 75 passed。
|
||||
## 同步主分支后的验证
|
||||
|
||||
主分支合入 #17 后产生两个冲突。`ui/dsh_native.py` 完整采用主分支版本,DSH 功能与主分支保持一致;验收脚本保留双方检查入口和历史页检查。没有合并 PR #15。
|
||||
|
||||
- 同步后默认全套再次 **1233 passed**(301.74 秒,1 条现有 STA 提示);DSH/打包 **214 passed**。
|
||||
- 原生两轮 WebEngine 导航差分复验:侧栏背景差分 20.18,不透明表格差分 0,窗口句柄不变,alpha=8,Acrylic 保持启用。证据:`build/history-execution-native-after-main/roundtrip-probe.json`。
|
||||
- 同步后的整套截图流在历史清空断言处未完成,后续截图复跑被“外部窗口遮挡”所有权检查阻止,没有抓取其他窗口。使用独立、无截图的 Windows 原生探针复验相同的 WebEngine 切换、搜索、详情及真实 QMessageBox 确认:clear_success=true、表格和数据库历史均为 0、任务保留、Acrylic=true、CAD=false。证据:`build/history-execution-native-after-main/native-history-result.json`。因此不把整套截图脚本宣称为同步后全量通过。
|
||||
@@ -0,0 +1,18 @@
|
||||
# Issue #16: Acrylic after returning from the browser workspace
|
||||
|
||||
Scope: UI rendering only. Provider/model configuration, runtime protocol, tools and task operations are unchanged.
|
||||
|
||||
The browser workspace previously shared its native composition parent with the Acrylic main window. Showing a real QWebEngineView made the parent surface opaque; hiding the browser did not restore the live backdrop. The alpha-buffer size, transparency attribute and Acrylic property still reported enabled. Reapplying Windows composition alone did not fix the rendering.
|
||||
|
||||
The browser workspace now uses `WA_NativeWindow` so Chromium composes into its own child native surface. This keeps its opaque browser background separate from the task-center backdrop and retains the existing stacked-page navigation.
|
||||
|
||||
Validation on Windows 11 build 26200, 125% scaling:
|
||||
|
||||
- Regression test uses a real QWebEngineView and checks its native composition parent. It failed before the fix and passed afterward.
|
||||
- `python -m pytest -q tests/ui --tb=short --junitxml=build/issue16-ui-tests.xml`: **172 passed**.
|
||||
- `python tools/accept_acrylic_ui.py --output build/issue16-final --webengine-roundtrip`: passed. The test uses sample projects and an owned changing backdrop. It displays a local HTML page in real Chromium, without launching the Harness server, model requests or CAD.
|
||||
- Before the fix, changing the background after returning produced a sidebar pixel difference of **0.0**. After the fix, two consecutive round trips each produced **20.79** (threshold > 2); the solid table difference stayed **0.0** (threshold < 1.5).
|
||||
- With reduced transparency enabled, the same navigation produced a sidebar difference of **0.0** as expected. Search, empty results, solid fallback, 980×700 layout and visible stop controls also passed.
|
||||
- An initial focused run reported one existing Qt fixture teardown race (`TrialTaskCenterWindow already deleted`); the subsequent complete UI run passed without errors.
|
||||
|
||||
Screenshots and machine-readable results are local artifacts under `build/issue16-final/`. No release package was built. PR #15 is synchronized with main `3cec9ad`; unrelated main-workspace changes were left untouched.
|
||||
@@ -0,0 +1,17 @@
|
||||
# Task status categories
|
||||
|
||||
The earlier task navigation retained a hidden, disconnected status selector. The visible selector below search now offers All tasks, Unfinished, Completed and Failed, with the existing SVG icon set. Unfinished includes Draft, Ready, Running, Paused and Failed; deleted tasks remain in the recycle bin.
|
||||
|
||||
Filtering operates on the cached task list and combines with search. Refreshing tasks reapplies the chosen category against the updated status. The current task is remembered when hidden, so changing display categories does not switch the right-hand task or discard its draft. The window retains the complete task collection for batch execution; filtering never narrows the execution scope. Moving tasks is disabled while a status filter hides part of the saved order.
|
||||
|
||||
The recycle bin ignores the normal status filter and disables that selector while open; returning restores the chosen category. The status selector is a display preference for this window and defaults to All tasks on a new launch.
|
||||
|
||||
Validation:
|
||||
|
||||
- Seven new cases first failed against the hidden/disconnected selector. They now cover category membership, combined search, completion on refresh, preserving task identity, cached reads, unchanged batch scope, ordering gates and recycle-bin independence.
|
||||
- `python -m pytest -q tests/ui tests/v1_1/test_task_navigation.py --tb=short --junitxml=build/task-category-ui-tests.xml`: **181 passed**.
|
||||
- After adding the status SVG icons, category and navigation tests: **9 passed**.
|
||||
- `python tools/accept_acrylic_ui.py --output build/task-categories-final --webengine-roundtrip`: passed on Windows 11 build 26200 at 125% scaling, including status category visibility, return from Chromium, reduced transparency and the 980×700 layout.
|
||||
- Python compilation and `git diff --check`: passed.
|
||||
|
||||
No DSH functionality or task execution implementation changed. No CAD or model requests were made. No release executable was built.
|
||||
@@ -0,0 +1,43 @@
|
||||
# PR #15:补齐业务变更的操作历史
|
||||
|
||||
针对[审查评论 179](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/pulls/15#issuecomment-179)。修复范围为操作历史接入,不修改 DSH 业务逻辑,也不执行真实 CAD、模型或 DSH 运行时。
|
||||
|
||||
## 根因及修复边界
|
||||
|
||||
原实现依赖 `_MUTATIONS` 手工登记入口,遗漏时直接执行业务动作,不生成历史。助手创建任务、采集位号和重新读取绑定工程因此存在历史缺口。同类入口检查还发现 Value 策略已登记,但调用路径没有经过捕获器。
|
||||
|
||||
- 助手创建:纳入普通历史事务;一次确认产生的任务、工程和规则按任务分组记录,重放同一请求不重复生成历史。
|
||||
- 采集位号:读取工程时保留原执行互斥,读取结束后仅对已有的位号保存事务捕获差异;默认料号、位号计划模式和新增位号一起记录。
|
||||
- 重新读取:文件状态检查和随后上下文读取在历史事务外,只捕获 `sync_projects` 对工程可用性等字段的实际修改。
|
||||
- Value 策略:复用统一的服务入口捕获逻辑,记录开关的前后值;重复保存相同值不生成记录。
|
||||
|
||||
采集和工程刷新使用显式的 `capture_history=False` 保留外层执行准入,并在内部短保存阶段调用历史捕获器,避免重复记录及长时间持有数据库访问锁。
|
||||
|
||||
## 回归证据
|
||||
|
||||
新增 13 项真实服务入口回归,覆盖四类操作的准确差异、助手请求重放、无变化刷新/轮询、重复采集、取消采集、四类历史写入失败时的事务回滚,以及 CAD 读取等待期间另一线程仍可写入独立历史。
|
||||
|
||||
断言按 `operation` 精确选择事件,检查实际字段前后值,避免全文搜索误命中临时工程路径造成假阳性。
|
||||
|
||||
在干净的 PR 提交 `56588a9` 源码快照上运行同一组精确记录断言,四项均因历史事件缺失失败;修复后的 13 项全部通过。未改动当前工作树源码来制造这个失败对照。
|
||||
|
||||
验证命令与结果:
|
||||
|
||||
- `python -m pytest -q tests/v1_1/test_history_service_entrypoints.py`:13 项通过。
|
||||
- 历史及执行相关定向测试组:92 项通过;随后增加的 Value 策略回归包含在上述 13 项中。
|
||||
- `python -m pytest -q tests/dsh tests/packaging tests/trial_task_center/test_service.py tests/trial_task_center/test_quick_run.py tests/trial_task_center/test_store.py`:282 项通过。
|
||||
- 独立代码审查未发现可操作问题,包括 Value 策略入口及采集、刷新事务边界的补充检查。
|
||||
|
||||
### 原生脚本时限对照
|
||||
|
||||
同步工作目录中单独运行原生保护测试时,正向用例触发 `subprocess.TimeoutExpired`:`cscript.exe` 等待 Python 标记辅助程序超过测试的 15 秒限制;其他 4 个保护用例通过。直接调用同一标记程序和已生成的 VBScript 则正常返回,分别耗时 7.69 秒和 6.16 秒。
|
||||
|
||||
为区分代码差异和运行位置影响,将干净提交的源码、测试及 VBS 导出至 `F:/dxpt-ui-history-verify-20261104/entrypoint-baseline-2`,再将相同提交加本次全部 3 个源码修改导出至独立的 `native-current-snapshot`。两份快照在同步目录外运行原测试、保持原有 15 秒时限,均为 **5 项通过**(57.05 秒 / 49.54 秒)。没有改动原生保护代码或放宽测试时限。这支持运行位置/本机耗时影响的判断,尚未定位具体的系统级延迟来源。
|
||||
|
||||
### 默认全套
|
||||
|
||||
`python -m pytest -q --tb=short`:**1245 项通过、1 项超时失败、1 条现有 pywinauto STA 提示**,耗时 1169.33 秒。
|
||||
|
||||
该次超时出现在同组的 `missing-path` 参数分支,并非单独复跑时超时的正向分支,异常同为 `cscript.exe` 的 15 秒等待超时。两个版本快照的整个 5 项参数组均已通过,不能将本次默认全套描述为全部通过。原生测试、VBS、辅助程序和 DSH 源码均未修改。
|
||||
|
||||
全套结束后,在原同步工作目录中以原时限单独复跑 `-k missing-path`:**1 项通过、4 项未选择**,耗时 6.86 秒。
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
- 旧 Part Number 未命中:黄色提示“未命中跳过”,不作替换,自动继续。未命中不等于替换成功。
|
||||
- 旧器件命中:执行原生替换,恢复实例信息,保存并关闭工程,再重新打开核验;核验和关闭完成后才处理下一项。
|
||||
- 目标器件不可用、Part Label 回读失败、保存或核验失败:记录工程、图页、位号和原因,关闭且不保存该失败工程,再继续后续项。结果状态不确定时才停止并要求核验。
|
||||
- 目标器件不可用、Part Label 回读失败、保存或核验失败:记录工程、图页、位号和原因。当前独立工程任务在确认没有残留改动、资源已释放后暂缓本 CBB 余下项,继续后面的独立工程;保存、核验或资源状态不确定时停止整队并要求核验。
|
||||
- 勾选“替换后将 Part Label 写入 Value”才额外改写 Value;未勾选不读取 Part Label,也不额外改写或创建 Value。旧 Value 已存在时,两种模式都继承其位置和显示样式。
|
||||
- 旧 Value 含独立的 `/NC` 段时,两种模式都会把 `/NC` 同步到替换后的 Value 且不重复追加;目标没有 Value 时创建 `/NC` 并继承旧 Value 样式。日志按位号记录替换与同步;工程中的位号本来为空时改用稳定的 `UID=...`,不会输出无法追踪的空白标识。
|
||||
|
||||
@@ -37,3 +37,7 @@
|
||||
`quick-run\quick-part-replace.log` 超过 5 MiB 后保留 3 个轮转备份,单个任务的 `events.jsonl` 超过 5 MiB 后保留 5 个轮转备份。`quick-run-history` 只清理由程序生成且名称可验证的 `batch-*`、`recovery-*`、`sheet-discovery-*` 和 `obsolete-*` 目录;未知目录、链接目录和当前活动的 `quick-run` 证据不会删除。
|
||||
|
||||
历史清理在新的准备、批量或恢复操作进入互斥执行区后运行。默认至少保留最新 10 份历史;其余记录在超过 30 天、总数超过 100 份或总容量超过 5 GiB 时从最旧开始删除。由于最近 10 份始终保留,单个超大工程可能使实际容量暂时高于 5 GiB,此时应人工转存需要长期留档的证据。
|
||||
|
||||
## 操作历史与逐项续跑
|
||||
|
||||
详见[操作历史与继续执行](operation-history.md):搜索及清空历史不会重置完成回执;同一 CBB 部分成功后的失败项重试会保留成功项。
|
||||
@@ -6,7 +6,15 @@
|
||||
|
||||
这里直接运行 [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) 的官方前端与运行时,固定 npm 版本 `0.2.1-rc.5`,通过 Qt WebEngine 嵌入主窗口。官方三栏框架、原生 SVG、鲸鱼悬停动画、输入编辑器、模型选择、侧栏与停靠交互均由上游代码提供。项目增加任务监管插件和“DSH助手”名称,不再使用 Qt 控件重画聊天页面。上游许可见 `licenses/DeepSeek-Harness-MIT.txt`。
|
||||
|
||||
不显示上游账户登录、注册、充值和首次产品引导,也不注册其账户模型提供方。左下角 **工具设置** 单击打开原生设置页中的“DSH工具”说明;首页提示和任务监管面板围绕器件替换任务。输入框下方仍可选择主工具已经配置的模型。
|
||||
不显示上游账户登录、注册、充值和首次产品引导,也不注册其账户模型提供方。左下角 **工具设置** 单击打开原生设置页,包含“DSH工具”“通用设置”和“模型”;首页提示和任务监管面板围绕器件替换任务。输入框下方选择已配置的模型。
|
||||
|
||||
在 **模型 → 添加模型提供商 → 自定义模型 API** 中填写 Provider ID、API 地址、协议及可选密钥,点击 **获取可用模型**,勾选后点 **添加所选**,最后 **创建提供商**。也可手动添加模型 ID。获取失败会保留表单,便于修正地址或手动填写。已有提供方点击 **编辑** 可修改其模型目录;保存后即时生效。
|
||||
|
||||
模型页面使用官方 Harness 设置与凭据服务:用户设置保存在 `dsh/harness-home/profiles/dx-task/cordis.patch.yml`,新增的密钥通过官方本地凭据服务保存在 `dsh/harness-home/.credentials.yaml`;此文件不使用主工具的 DPAPI 格式。主工具预配置的 `provider.json` 和 DPAPI 密钥仍可加载。应用固定策略作为独立 bundle 提供;应用管理的 `dx` 提供方随主工具配置完整更新,自定义提供方及其独立凭据保留。旧版生成的 `dx` 默认模型在迁移时回到当前主工具默认值;原生用户选定且仍有效的模型及自定义提供方默认选择保留。改写配置前逐字节备份,重复启动不重复改写。
|
||||
|
||||
在原生模型页为现有 `dx` 保存密钥后,该提供方作为用户覆盖保留其地址、模型和本地密钥,不再随主工具自动切换。如果当前启用了主工具模型,但旧数据中存在本地 `DSH_API_KEY` 密钥、却没有对应的有效 `dx` 配置,DSH 会明确拒绝启动,避免把未知来源的密钥发到当前地址。恢复时先备份整个数据目录,将密钥配置为具有独立凭据引用的自定义提供方;不要只搬运密钥文件。主工具模型未启用时,无关的孤立引用不会阻断独立提供方。不要将凭据文件分发或提交到仓库。
|
||||
|
||||
DSH 使用独立原生子窗口承载 WebEngine,切回任务中心后保留原有 Acrylic 玻璃材质;“减少透明度”与系统高对比度等回退规则继续生效。
|
||||
|
||||
模型由本机保存的主工具配置加载;也支持启动进程的 `DSH_API_BASE`、`DSH_MODEL`、`DSH_API_KEY`。未配置模型时可查看任务上下文和操作面板,不能获得真实模型回复。
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
# 操作历史与继续执行
|
||||
|
||||
点击顶部的“操作历史”(快捷键 `Ctrl+Shift+H`),查看任务、规则、位号计划的实际保存改动及执行、暂停、恢复结果。可按任务、工程、位号、新旧料号和说明搜索,按结果筛选,选中记录查看详情,点击“更早记录”翻页。搜索、翻页和刷新本身不生成记录。
|
||||
|
||||
“已核验完成”“未命中”“已恢复”“失败”“待核验”分别展示。恢复到旧状态不算替换成功;旧版本没有保存的字段不会补造为真实改动。
|
||||
|
||||
## 清空历史
|
||||
|
||||
“清空全部历史”清理全部历史范围,不受当前搜索条件影响。确认框显示本次清理的记录数;打开确认框后新增的记录保留。默认取消,执行或恢复期间不可清空。
|
||||
|
||||
清空不会删除任务、逐项完成回执、原生证据或工程备份,也不会使已执行项目重新运行。旧版本记录完成首次导入后才允许清空;清空后重启不会重新导入已清理记录。
|
||||
|
||||
## 同一任务继续或重启
|
||||
|
||||
每个独立工程任务保存本轮冻结范围和逐项回执。继续只处理尚未执行的项,不重复派发已完成或已确认未命中的项。只有“开始新一轮”会建立新的轮次。
|
||||
|
||||
续跑检查最近已核验图页的器件身份、属性、布局和连接状态,不拿已经合法修改过的工程与最初文件哈希机械比较。原生证据缺失、损坏或与数据库冲突时停止核验,不把进度归零后重跑。旧证据不足以证明安全时,也需要核验。
|
||||
|
||||
当前界面中的某个 CBB 明确安全失败后,保留失败项,暂缓该工程余下项,并继续后面的独立工程。结果不确定、核验失败或资源未释放时停止整队。普通“继续”不自动重试失败和暂缓项;处理原因并选择“准备失败项重试”后,保留原成功项,只执行失败和暂缓部分。
|
||||
|
||||
重试复用经校验的原始备份,不把已修改工程当作原始副本重新备份。重试队列先暂存、核验再发布;发布中断后重启会恢复原队列。已核验恢复的文件与数据库提交中断时,再次确认恢复会完成原提交,不再次调用替换。
|
||||
|
||||
新数据表随任务数据库自动升级到版本 8。更新前请正常退出工具;本次没有发布新的 EXE,也没有改变 DSH 功能。
|
||||
+156
-8
@@ -1,7 +1,9 @@
|
||||
/** Application-owned composition around the unmodified official Harness web UI. */
|
||||
import {createRequire} from 'node:module';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
import {mkdir, writeFile, readFile, cp} from 'node:fs/promises';
|
||||
import {mkdir, writeFile, readFile, cp, rename} from 'node:fs/promises';
|
||||
import {createHash,randomUUID} from 'node:crypto';
|
||||
import {isDeepStrictEqual} from 'node:util';
|
||||
import path from 'node:path';
|
||||
import {createInterface} from 'node:readline';
|
||||
|
||||
@@ -28,12 +30,14 @@ export const DISABLED = [
|
||||
// This embedded tool uses its application-owned API configuration, without
|
||||
// upstream accounts, billing, credential onboarding or product feedback.
|
||||
'ui-settings-account','account-controller','deepseek-account','llm-deepseek-account',
|
||||
'ui-settings-models','ui-message-feedback','message-feedback','command-feedback',
|
||||
'ui-message-feedback','message-feedback','command-feedback',
|
||||
];
|
||||
|
||||
export function overlay() {
|
||||
const rows = DISABLED.map(id => ({id, disabled:true}));
|
||||
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:'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'}}}});
|
||||
@@ -55,6 +59,144 @@ export function overlay() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
async function replaceJson(filename,value) {
|
||||
const temporary=filename+'.'+randomUUID()+'.tmp';
|
||||
const {unlink}=await import('node:fs/promises');
|
||||
try{await writeFile(temporary,JSON.stringify(value,null,2));await rename(temporary,filename);}
|
||||
finally{await unlink(temporary).catch(error=>{if(error.code!=='ENOENT')throw error;});}
|
||||
}
|
||||
|
||||
async function readOptionalModelMetadata(filename) {
|
||||
let text;
|
||||
try{text=await readFile(filename,'utf8');}catch(error){if(error.code==='ENOENT')return;throw error;}
|
||||
try{return require('js-yaml').load(text);}catch{
|
||||
// YAML parser errors can quote the line containing a credential.
|
||||
throw Error('Cannot parse stored model metadata: '+path.basename(filename));
|
||||
}
|
||||
}
|
||||
|
||||
function modelEntry(rows,id) {
|
||||
const entries=rows.filter(row=>row.id===id);
|
||||
if(entries.length>1)throw Error('Duplicate '+id+' prevents verifying model binding; startup refused.');
|
||||
return entries[0];
|
||||
}
|
||||
|
||||
function refreshApplicationModels(rows,application,{legacy=false,storedHostKey=false,previousApplication=[],boundDx}={}) {
|
||||
const current=application.find(row=>row.id==='llm-pi-ai')?.config.providers.dx;
|
||||
let userLlm=rows.filter(row=>row.id==='llm-pi-ai'&&row.config).at(-1);
|
||||
if(current&&storedHostKey&&!boundDx){
|
||||
// A generated policy alone cannot prove which endpoint a saved key belongs
|
||||
// to. Never attach an unbound local credential to today's host endpoint.
|
||||
throw Error('Stored DSH_API_KEY has no provider binding; configure a native provider with its own credential reference before starting DSH.');
|
||||
}
|
||||
if(!userLlm&¤t){
|
||||
userLlm={id:'llm-pi-ai',config:{providers:{dx:current}}};
|
||||
rows=[...rows,userLlm];
|
||||
}
|
||||
const prior=boundDx;
|
||||
if(storedHostKey&&prior?.apiKeyEnv==='DSH_API_KEY'&&!rows.some(row=>row.id==='agent-default-model')){
|
||||
const saved=previousApplication.find(row=>row.id==='agent-default-model')?.config;
|
||||
const config=saved?.provider==='dx'&&prior.models?.some(model=>model.id===saved.model)
|
||||
?saved:{provider:'dx',model:prior.models[0].id};
|
||||
rows=[...rows,{id:'agent-default-model',config}];
|
||||
}
|
||||
// A native provider with its own credential reference (or deliberately no
|
||||
// credential) owns its complete route. Only an environment-sourced
|
||||
// DSH_API_KEY belongs to the host; a saved native key owns its user route.
|
||||
const managed=prior===undefined||(prior.apiKeyEnv==='DSH_API_KEY'&&!storedHostKey);
|
||||
return rows.flatMap(row=>{
|
||||
if(row.id==='agent-default-model'&&row.config?.provider==='dx'&&managed
|
||||
&&(legacy||!current?.models.some(model=>model.id===row.config.model)))return [];
|
||||
if(row.id!=='llm-pi-ai'||!row.config)return [row];
|
||||
const providers={...row.config.providers};
|
||||
if(providers.dx===undefined||(providers.dx.apiKeyEnv==='DSH_API_KEY'&&!storedHostKey)){
|
||||
if(current)providers.dx=current;else delete providers.dx;
|
||||
}
|
||||
// Harness replaces an entire plugin config, rather than deep-merging it.
|
||||
// Keep the host route beside custom routes in any overriding user row.
|
||||
return [{...row,config:{...row.config,providers}}];
|
||||
});
|
||||
}
|
||||
|
||||
export async function prepareProfileDirectory(dir) {
|
||||
// The official settings page writes the user patch. Application defaults
|
||||
// belong to a bundle below it. Only host-owned model routes are refreshed;
|
||||
// independent native providers and their credential references remain intact.
|
||||
await mkdir(dir,{recursive:true});
|
||||
const application=overlay();
|
||||
const userPatch=path.join(dir,'cordis.patch.yml');
|
||||
const previousApplication=await readOptionalModelMetadata(path.join(dir,'node_modules/@dx/dsh-policy/cordis.patch.yml'))??[];
|
||||
const policyDir=path.join(dir,'node_modules','@dx','dsh-policy');
|
||||
await mkdir(policyDir,{recursive:true});
|
||||
await cp(path.join(root,'task-plugin'),path.join(dir,'node_modules','@dx','dsh-task'),{recursive:true});
|
||||
await replaceJson(path.join(policyDir,'package.json'),{name:'@dx/dsh-policy',version:VERSION,
|
||||
private:true,dsh:{bundle:{patch:'./cordis.patch.yml'}}});
|
||||
await replaceJson(path.join(policyDir,'cordis.patch.yml'),application);
|
||||
await replaceJson(path.join(dir,'package.json'),{private:true,type:'module',
|
||||
dependencies:{'@dx/dsh-task':'file:'+path.join(root,'task-plugin'),'@dx/dsh-policy':'file:'+policyDir},
|
||||
dsh:{profile:{bundles:['@deepseek-ai/dsh-base','@deepseek-ai/dsh-web-app','@dx/dsh-policy']}}});
|
||||
const {loadProfileDirectory,composeEntries}=await import('@deepseek-ai/dsh-app-boot');
|
||||
const profile=loadProfileDirectory('dx-dsh',dir,require.resolve('@deepseek-ai/dsh/package.json'),{userLayer:false});
|
||||
const layers=profile.layers.map(layer=>layer.patches);
|
||||
// Remove only the generated host route when testing whether a user binding
|
||||
// actually applies. Use Harness semantics, including name assertions/inserts.
|
||||
const withoutHost=profile.layers.map(layer=>layer.packageName==='@dx/dsh-policy'
|
||||
?layer.patches.filter(row=>row.id!=='llm-pi-ai'):layer.patches);
|
||||
let hostCredentialBinding;
|
||||
const refresh=async(rows,legacy=false)=>{
|
||||
const effective=composeEntries([...layers,rows]);
|
||||
const credentials=modelEntry(effective,'credentials');
|
||||
modelEntry(effective,'llm-pi-ai');modelEntry(effective,'agent-default-model');
|
||||
let storedHostKey=false,savedKey;
|
||||
if(credentials?.disabled!==true){
|
||||
if(credentials?.name!=='@deepseek-ai/dsh-credentials-local')
|
||||
throw Error('Cannot verify the configured model credential provider; startup refused.');
|
||||
const {resolveDshHome}=await import('@deepseek-ai/dsh-home-paths');
|
||||
const config=credentials.config??{};
|
||||
const home=resolveDshHome(config.dshHome,{DSH_HOME:path.resolve(dir,'../..')});
|
||||
const stored=await readOptionalModelMetadata(path.resolve(config.path??path.join(home,'.credentials.yaml')));
|
||||
savedKey=stored?.version===undefined?stored?.DSH_API_KEY:stored?.refs?.DSH_API_KEY;
|
||||
storedHostKey=typeof savedKey==='string'&&savedKey.length>0;
|
||||
}
|
||||
const boundDx=modelEntry(composeEntries([...withoutHost,rows]),'llm-pi-ai')?.config?.providers?.dx;
|
||||
const next=refreshApplicationModels(rows,application,{legacy,storedHostKey,previousApplication,boundDx});
|
||||
const actual=modelEntry(composeEntries([...layers,next]),'llm-pi-ai')?.config?.providers?.dx;
|
||||
if(storedHostKey&&actual?.apiKeyEnv==='DSH_API_KEY'
|
||||
&&(!boundDx||!isDeepStrictEqual(actual,boundDx)))
|
||||
throw Error('Stored DSH_API_KEY has no provider binding after Harness composition; startup refused.');
|
||||
if(!storedHostKey&&actual?.apiKeyEnv==='DSH_API_KEY'
|
||||
&&!isDeepStrictEqual(actual,application.find(row=>row.id==='llm-pi-ai')?.config.providers.dx))
|
||||
throw Error('Application model route did not match the current provider; startup refused.');
|
||||
if(actual?.apiKeyEnv==='DSH_API_KEY')hostCredentialBinding={source:storedHostKey?'file':'env',
|
||||
digest:createHash('sha256').update((storedHostKey?savedKey:process.env.DSH_API_KEY)??'').digest('hex')};
|
||||
return next;
|
||||
};
|
||||
let prior;
|
||||
try{prior=await readFile(userPatch,'utf8');}catch(error){if(error.code!=='ENOENT')throw error;}
|
||||
if(prior!==undefined){
|
||||
const {load}=require('js-yaml');
|
||||
const rows=load(prior);
|
||||
const legacy=Array.isArray(rows)&&rows.some(row=>Array.isArray(row?.insert)
|
||||
&&row.insert.some(entry=>entry.id==='dx-task'&&entry.name==='@dx/dsh-task')
|
||||
&&row.insert.some(entry=>entry.id==='preset-dx-task'));
|
||||
const editable=new Set(['llm-pi-ai','llm-deepseek','agent-default-model']);
|
||||
const userRows=legacy?rows.filter(row=>editable.has(row?.id)&&row.config):rows;
|
||||
const next=Array.isArray(userRows)?await refresh(userRows,legacy):userRows;
|
||||
if(legacy||JSON.stringify(next)!==JSON.stringify(rows)){
|
||||
const digest=createHash('sha256').update(prior).digest('hex');
|
||||
const backup=userPatch+(legacy?'.legacy-':'.models-')+digest+'.bak';
|
||||
try{await writeFile(backup,prior,{flag:'wx'});}catch(error){
|
||||
if(error.code!=='EEXIST'||await readFile(backup,'utf8')!==prior)throw error;
|
||||
}
|
||||
await replaceJson(userPatch,next);
|
||||
}
|
||||
}else if(application.some(row=>row.id==='llm-pi-ai')){
|
||||
const next=await refresh([]);
|
||||
await replaceJson(userPatch,next);
|
||||
}
|
||||
return {hostCredentialBinding};
|
||||
}
|
||||
|
||||
export async function bootRuntime({announce=true} = {}) {
|
||||
const installed = require('@deepseek-ai/dsh/package.json');
|
||||
if (installed.version !== VERSION) throw Error('Unsupported Harness version: ' + installed.version);
|
||||
@@ -73,12 +215,7 @@ export async function bootRuntime({announce=true} = {}) {
|
||||
process.env.DX_DSH_WORKSPACE = path.join(process.env.DX_DSH_HOME,'workspace');
|
||||
await mkdir(process.env.DX_DSH_WORKSPACE,{recursive:true});
|
||||
const dir = path.join(process.env.DX_DSH_HOME,'profiles','dx-task');
|
||||
await mkdir(path.join(dir,'node_modules','@dx'),{recursive:true});
|
||||
await cp(path.join(root,'task-plugin'),path.join(dir,'node_modules','@dx','dsh-task'),{recursive:true});
|
||||
await writeFile(path.join(dir,'package.json'), JSON.stringify({private:true,type:'module',
|
||||
dependencies:{'@dx/dsh-task':'file:'+path.join(root,'task-plugin')},
|
||||
dsh:{profile:{bundles:['@deepseek-ai/dsh-base','@deepseek-ai/dsh-web-app']}}}));
|
||||
await writeFile(path.join(dir,'cordis.patch.yml'),JSON.stringify(overlay(),null,2));
|
||||
const modelBinding=await prepareProfileDirectory(dir);
|
||||
const {runProfile, INSTALL_ANCHOR} = await import('@deepseek-ai/dsh/profile-boot');
|
||||
const {loadProfileDirectory,composeEntries} = await import('@deepseek-ai/dsh-app-boot');
|
||||
const {createLaunchEnvironmentSnapshot} = await import('@deepseek-ai/dsh-launch-environment');
|
||||
@@ -93,6 +230,17 @@ export async function bootRuntime({announce=true} = {}) {
|
||||
patchFiles:[],args:['--no-open','--host','127.0.0.1','--port','0'],
|
||||
environment:createLaunchEnvironmentSnapshot([{source:'process',values:process.env}])});
|
||||
await ctx.loader.await();
|
||||
// Check the mounted service as well as its composition: an unexpected
|
||||
// credential source must fail before exposing the runtime to the desktop.
|
||||
if(modelBinding.hostCredentialBinding){
|
||||
const actual=ctx.get('credentials')?await ctx.credentials.resolve('DSH_API_KEY')
|
||||
:{source:'env',value:process.env.DSH_API_KEY};
|
||||
if(actual?.source!==modelBinding.hostCredentialBinding.source||typeof actual?.value!=='string'
|
||||
||createHash('sha256').update(actual.value).digest('hex')!==modelBinding.hostCredentialBinding.digest){
|
||||
await shutdown.shutdown(1);
|
||||
throw Error('Host model credential source changed during startup; startup refused.');
|
||||
}
|
||||
}
|
||||
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))) {
|
||||
await shutdown.shutdown(1);
|
||||
|
||||
@@ -13,7 +13,7 @@ window.__ModuleLoader__.load({id:'@dx/dsh-task',factory:require=>{
|
||||
h('h3',null,'任务能力'),
|
||||
h('ul',null,...['在对话中创建和修改任务;具体工程与执行范围由原生窗口确认','实时监管当前执行、阶段、耗时与保存/失败/未命中计数','保存执行回执,重启后核对原队列并手动确认恢复','预览精确位号影响;仅有料号映射时明确标记未知匹配数量','检索历史异常经验,仅将持久化核验通过的记录标为已验证','只读同步已有 Git 工单状态与评论,查看本地证据','查看会话 Token、耗时与超时状态,配置响应时限并停止模型生成','设置任务优先级、执行时间与依赖,通过原生窗口确认排期'].map(text=>h('li',{key:text},text))),
|
||||
h('h3',null,'模型与数据'),
|
||||
h('p',null,'使用主工具配置的模型,在输入框下方选择。会话保存在本机任务中心数据目录。'),
|
||||
h('p',null,'在“模型”设置页添加模型提供商或自定义模型 API,填写地址后可获取可用模型,也可手动填写模型 ID。保存后在输入框下方选择,模型设置和会话在重启后保留。'),
|
||||
h('h3',null,'异常工单'),
|
||||
h('p',null,'随工具预置专用工单上报身份,无需安装 Git 或登录。未解决的异常自动提交项目工单,并上传脱敏诊断证据。'),
|
||||
h('p',null,'工程修改与执行核验由任务中心完成。模型不直接访问工程文件或执行命令。'));
|
||||
|
||||
@@ -159,6 +159,8 @@ Sub QPMain()
|
||||
Set Command = QPReadCommand(QP_Fso.BuildPath(QP_Root, "command.tsv"))
|
||||
QP_CurrentJobId = CStr(Command("job_id"))
|
||||
QPRequireProject Command
|
||||
If QP_BatchMode And Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root, "recovery.request")) Then _
|
||||
QPVerifyExpectedBaselines CStr(Command("project_path"))
|
||||
SnapshotPath = QPJobSnapshotPath(CStr(Command("job_id")))
|
||||
State = QPReadState(QP_Fso.BuildPath(QP_Root, "status.tsv"), _
|
||||
CStr(Command("job_id")))
|
||||
@@ -1003,7 +1005,10 @@ Sub QPFinishBatchItem()
|
||||
QPMarkBatchMutation Command
|
||||
QPCloseBatchProject TargetPath, True
|
||||
ElseIf Not QP_SessionMode Then
|
||||
QPWriteVerifiedState Command
|
||||
QPCloseBatchProject TargetPath, False
|
||||
Else
|
||||
QPWriteVerifiedState Command
|
||||
End If
|
||||
If FinalState = "restored_ok" Then
|
||||
QPProgress "verify_saved_project", 0, 1
|
||||
@@ -1019,6 +1024,7 @@ Sub QPFinishBatchItem()
|
||||
Plans.Add CStr(Index), Candidate
|
||||
Next
|
||||
QPVerifyRestoredPlans Command, Records, Plans, ValueStates
|
||||
QPWriteVerifiedState Command
|
||||
If Not QP_SessionMode Then QPCloseBatchProject TargetPath, False
|
||||
FinalState = "saved_ok"
|
||||
QPLog QP_Root, "INFO", "saved_readback_verified job=" & QP_CurrentJobId
|
||||
@@ -1046,6 +1052,74 @@ Sub QPFinishBatchItem()
|
||||
QPProgress "finished", 1, 1
|
||||
End Sub
|
||||
|
||||
Function QPCurrentSemanticState()
|
||||
Dim Components, Comp, State
|
||||
QPInvalidatePinIndex
|
||||
QPInvalidateComponentIndex
|
||||
Set Components = QP_App.ActiveView.Query(VDM_COMP, VD_ALL)
|
||||
State = ""
|
||||
For Each Comp In Components
|
||||
State = State & QPUntouchedFullState(Comp)
|
||||
Next
|
||||
QPCurrentSemanticState = "S" & vbTab & "full_sheet_v1" & vbCrLf & QPSortSnapshotLines(State)
|
||||
End Function
|
||||
|
||||
Sub QPRequireExpectedState(ByVal Expected, ByVal Actual)
|
||||
If CStr(Expected) <> CStr(Actual) Then Err.Raise vbObjectError + 879, _
|
||||
"QuickPartReplace.Baseline", "project changed since the last verified receipt; reconcile before continuing"
|
||||
End Sub
|
||||
|
||||
Sub QPWriteVerifiedState(ByRef Command)
|
||||
Dim Text, View
|
||||
Set View = QP_App.ActiveView
|
||||
Text = "job_id" & vbTab & "project_path" & vbTab & "schematic_name" & vbTab & _
|
||||
"sheet_number" & vbTab & "semantic_state" & vbCrLf
|
||||
Text = Text & QPEncode(CStr(Command("job_id"))) & vbTab & QPEncode(CStr(Command("project_path"))) & _
|
||||
vbTab & QPEncode(CStr(View.GetName(0))) & vbTab & QPEncode(CStr(View.Block.SheetNum)) & _
|
||||
vbTab & QPEncode(QPCurrentSemanticState()) & vbCrLf
|
||||
QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "post-state-" & CStr(Command("job_id")) & ".tsv"), Text
|
||||
End Sub
|
||||
|
||||
Sub QPVerifyExpectedBaselines(ByVal TargetPath)
|
||||
Dim ErrorNumber, Description
|
||||
On Error Resume Next
|
||||
QPCheckExpectedBaselines TargetPath
|
||||
ErrorNumber = Err.Number
|
||||
Description = Err.Description
|
||||
Err.Clear
|
||||
On Error GoTo 0
|
||||
If ErrorNumber <> 0 Then Err.Raise vbObjectError + 879, "QuickPartReplace.Baseline", Description
|
||||
End Sub
|
||||
|
||||
Sub QPCheckExpectedBaselines(ByVal TargetPath)
|
||||
Dim Path, Lines, Fields, Index, Documents, Document, OriginalSchematic, OriginalSheet
|
||||
Path = QP_Fso.BuildPath(QP_Root, "expected-baselines.tsv")
|
||||
If Not QP_Fso.FileExists(Path) Then Exit Sub
|
||||
Lines = Split(QPReadUtf8(Path), vbLf)
|
||||
If Replace(Lines(0), vbCr, "") <> "project_path" & vbTab & "schematic_name" & vbTab & "sheet_number" & vbTab & "semantic_state" Then _
|
||||
Err.Raise vbObjectError + 879, "QuickPartReplace.Baseline", "invalid expected semantic baseline"
|
||||
OriginalSchematic = CStr(QP_App.ActiveView.GetName(0))
|
||||
OriginalSheet = CStr(QP_App.ActiveView.Block.SheetNum)
|
||||
Set Documents = QP_App.SchematicSheetDocuments()
|
||||
For Index = 1 To UBound(Lines)
|
||||
If Len(Replace(Lines(Index), vbCr, "")) > 0 Then
|
||||
Fields = Split(Replace(Lines(Index), vbCr, ""), vbTab)
|
||||
If UBound(Fields) <> 3 Then Err.Raise vbObjectError + 879, "QuickPartReplace.Baseline", "incomplete expected semantic baseline"
|
||||
If QPCanonicalPath(QPDecode(Fields(0))) <> QPCanonicalPath(TargetPath) Then _
|
||||
Err.Raise vbObjectError + 879, "QuickPartReplace.Baseline", "semantic baseline project mismatch"
|
||||
QPRequireSheetManifest TargetPath, Documents, QPDecode(Fields(1)), QPDecode(Fields(2))
|
||||
Set Document = Documents.Open(QPDecode(Fields(1)), QPDecode(Fields(2)))
|
||||
Document.Activate
|
||||
QPRequireExpectedState QPDecode(Fields(3)), QPCurrentSemanticState()
|
||||
Document.Close False, ""
|
||||
End If
|
||||
Next
|
||||
Set Document = Documents.Open(OriginalSchematic, OriginalSheet)
|
||||
Document.Activate
|
||||
QPInvalidatePinIndex
|
||||
QPInvalidateComponentIndex
|
||||
End Sub
|
||||
|
||||
Sub QPMarkBatchMutation(ByRef Command)
|
||||
Dim Shell, Entry, Required, CommandLine, Key, Value, ExitCode
|
||||
Set Shell = CreateObject("WScript.Shell")
|
||||
@@ -1962,6 +2036,7 @@ Sub QPRecoverSavedJob(ByRef Command, ByVal SnapshotPath)
|
||||
Err.Raise vbObjectError + 865, "QuickPartReplace.Recovery", "mixed source and target components; no recovery writes performed"
|
||||
If OldCount = Plans.Count Then
|
||||
QPRequireCommandSnapshot Command, SnapshotPath
|
||||
If QP_BatchMode Then QPWriteVerifiedState Command
|
||||
Set RecoveryDocuments = QP_App.SchematicSheetDocuments()
|
||||
If RecoveryDocuments.Count <> 1 Then _
|
||||
Err.Raise vbObjectError + 865, "QuickPartReplace.Recovery", "read-only recovery document ownership changed"
|
||||
@@ -1989,6 +2064,7 @@ Sub QPRecoverSavedJob(ByRef Command, ByVal SnapshotPath)
|
||||
' A fresh owned recovery session has already verified the disk state.
|
||||
' Closing without saving avoids a second save/reopen cycle for no writes.
|
||||
QPRequireCommandSnapshot Command, SnapshotPath
|
||||
QPWriteVerifiedState Command
|
||||
QPCloseBatchProject CStr(Command("project_path")), False
|
||||
QP_BatchMode = False
|
||||
QPWriteStatus QP_Root, QP_CurrentJobId, "restored_ok", "recovery_saved_verified", _
|
||||
|
||||
@@ -18,7 +18,7 @@ if viewdraw_api is None:
|
||||
shutil.copyfile(viewdraw_api.__file__, generated_root / "viewdraw_api.py")
|
||||
legacy_runtime_datas = [
|
||||
(
|
||||
str(project_root / 'src/dx_productivity/trial_task_center/assets/app.ico'),
|
||||
str(project_root / 'src/dx_productivity/trial_task_center/assets'),
|
||||
'dx_productivity/trial_task_center/assets',
|
||||
),
|
||||
(
|
||||
@@ -33,16 +33,25 @@ legacy_runtime_datas = [
|
||||
|
||||
# The native assistant loads the official web UI; include its pinned Node runtime.
|
||||
dsh_root = project_root / 'integration' / 'dsh'
|
||||
for required in ('node_modules/@deepseek-ai/dsh/package.json', 'issue-bootstrap.json',
|
||||
'start.mjs', 'package.json', 'task-plugin/host.mjs'):
|
||||
if not (dsh_root / required).is_file():
|
||||
raise RuntimeError(f'Complete DSH release runtime is required; missing: {required}')
|
||||
if shutil.which('node') is None:
|
||||
raise RuntimeError('Node executable is required for the complete release')
|
||||
if (dsh_root / 'issue-bootstrap.json').is_file():
|
||||
legacy_runtime_datas.append((str(dsh_root / 'issue-bootstrap.json'), 'dsh-runtime'))
|
||||
if (dsh_root / 'node_modules' / '@deepseek-ai' / 'dsh' / 'package.json').is_file():
|
||||
if not (dsh_root / 'issue-bootstrap.json').is_file():
|
||||
raise RuntimeError('Provision the DSH issue-only reporter before packaging the native assistant.')
|
||||
# Release distribution keeps the complete installed runtime, including
|
||||
# lazily loaded packages, native binaries, frontend assets and metadata.
|
||||
staged_node_modules = dsh_root / 'node_modules'
|
||||
legacy_runtime_datas.extend([
|
||||
(str(dsh_root / 'start.mjs'), 'dsh-runtime'),
|
||||
(str(dsh_root / 'package.json'), 'dsh-runtime'),
|
||||
(str(dsh_root / 'task-plugin'), 'dsh-runtime/task-plugin'),
|
||||
(str(dsh_root / 'node_modules'), 'dsh-runtime/node_modules'),
|
||||
(str(staged_node_modules), 'dsh-runtime/node_modules'),
|
||||
(shutil.which('node'), 'dsh-runtime'),
|
||||
(str(project_root / 'licenses' / 'DeepSeek-Harness-MIT.txt'), 'licenses'),
|
||||
])
|
||||
@@ -65,19 +74,19 @@ a = Analysis(
|
||||
"pywinauto.controls.uiawrapper",
|
||||
"PySide6.QtWebEngineCore",
|
||||
"PySide6.QtWebEngineWidgets",
|
||||
"PySide6.QtWebChannel",
|
||||
"PySide6.QtQml",
|
||||
"PySide6.QtQuick",
|
||||
"PySide6.QtQuickWidgets",
|
||||
"PySide6.QtOpenGL",
|
||||
"PySide6.QtOpenGLWidgets",
|
||||
],
|
||||
excludes=[
|
||||
"PySide6.QtDesigner",
|
||||
"PySide6.QtMultimedia",
|
||||
"PySide6.QtMultimediaWidgets",
|
||||
"PySide6.QtOpenGL",
|
||||
"PySide6.QtOpenGLWidgets",
|
||||
"PySide6.QtPdf",
|
||||
"PySide6.QtPdfWidgets",
|
||||
"PySide6.QtQml",
|
||||
"PySide6.QtQuick",
|
||||
"PySide6.QtQuickWidgets",
|
||||
"PySide6.QtWebChannel",
|
||||
],
|
||||
noarchive=False,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
# Windows self-extracting release
|
||||
|
||||
Build the complete application with `Build-DxPartReplace.ps1` first. The spec now fails if the installed DSH package, reporter configuration or Node executable is missing. Keep the entire installed `integration/dsh/node_modules` tree, including lazy runtime dependencies.
|
||||
|
||||
Use the official **7-Zip 26.05 x64** tools from:
|
||||
|
||||
- Binary: https://github.com/ip7z/7zip/releases/download/26.05/7z2605-x64.exe
|
||||
- SHA256: `fd99e86f57d8f429ef5ed045bf98315af962e2309b0f1ba87a147a44c71647d0`
|
||||
- Source and license: https://github.com/ip7z/7zip/releases/tag/26.05 and https://www.7-zip.org/license.txt
|
||||
|
||||
Extract the installer with an existing 7-Zip tool into a build-only directory. No machine-wide installation is needed. Use that directory's `7z.exe`, `7z.dll`, `7z.sfx` and `License.txt`. The standard `7z.sfx` opens a destination selector and extracts to a persistent directory. It is not the install-to-temp SFX module; it takes no installer configuration and does not auto-launch or delete the extracted application.
|
||||
|
||||
```powershell
|
||||
python tools/build_self_extracting_release.py --source-dir dist/DxPartReplace --output-dir G:/dx-release-1.2.2 --version 1.2.2 --seven-zip build/7zip-2605/full/7z.exe --sfx-stub build/7zip-2605/full/7z.sfx --license build/7zip-2605/full/License.txt
|
||||
```
|
||||
|
||||
Use a fresh output directory and keep the completed build input unchanged during packaging. The tool rejects an existing directory, overlapping input/output paths and an application input containing `data`. It stages the complete input using same-volume hardlinks (copying when links are unavailable), includes the 7-Zip license, produces independent EXE + ZIP archives, and writes artifact and per-file SHA256 hashes. LZMA2 level 3 and bounded solid blocks keep decoding memory modest; tiny files still incur filesystem costs, so do not promise a fixed speedup across machines.
|
||||
|
||||
Before publishing, run the self-extracting EXE itself with `-y -o<fresh directory>`, compare every resulting file and every ZIP member against the manifest, and run `tools/verify_packaged_runtime.py` and Qt/DSH UI acceptance on that extracted EXE. Neither merely listing the archive nor the source UI tests replaces this check.
|
||||
|
||||
The optional Windows integration test exercises the real SFX with Chinese/spaced paths and empty/binary files. Set `DX_SFX_TEST_TOOLS` to the official tools directory and run `python -m pytest tests/packaging/test_self_extracting_release.py`. Linux CI runs the portable output-protection checks and explicitly skips the Windows integration.
|
||||
@@ -0,0 +1,71 @@
|
||||
"""Create a small, Windows-only copy of the installed DSH Node runtime."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
|
||||
_PRUNED_DIRS = frozenset({
|
||||
".bin", ".cache", ".github", "__tests__", "benchmark", "benchmarks",
|
||||
"coverage", "example", "examples", "test", "tests",
|
||||
})
|
||||
_PRUNED_SUFFIXES = frozenset({".map", ".ts", ".tsx"})
|
||||
_PRUNED_NAMES = frozenset({"changelog", "changelog.md", "history.md", "readme", "readme.md"})
|
||||
|
||||
|
||||
def _matches(value: str, rule: str) -> bool:
|
||||
return value.casefold() == rule.lstrip("!").casefold()
|
||||
|
||||
|
||||
def _compatible(package: Path, *, platform: str, arch: str) -> bool:
|
||||
try:
|
||||
value = json.loads((package / "package.json").read_text(encoding="utf8"))
|
||||
except (OSError, ValueError, TypeError):
|
||||
return True
|
||||
if not isinstance(value, dict):
|
||||
return True
|
||||
for field, current in (("os", platform), ("cpu", arch)):
|
||||
rules = value.get(field)
|
||||
if not isinstance(rules, list):
|
||||
continue
|
||||
if any(isinstance(rule, str) and rule.startswith("!") and _matches(current, rule)
|
||||
for rule in rules):
|
||||
return False
|
||||
positive = [rule for rule in rules if isinstance(rule, str) and not rule.startswith("!")]
|
||||
if positive and not any(_matches(current, rule) for rule in positive):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _ignore(directory: str, names: list[str], *, platform: str, arch: str) -> set[str]:
|
||||
ignored: set[str] = set()
|
||||
for name in names:
|
||||
path = Path(directory) / name
|
||||
if path.is_dir():
|
||||
if name.casefold() in _PRUNED_DIRS or not _compatible(path, platform=platform, arch=arch):
|
||||
ignored.add(name)
|
||||
elif Path(name).suffix.casefold() in _PRUNED_SUFFIXES or name.casefold() in _PRUNED_NAMES:
|
||||
ignored.add(name)
|
||||
return ignored
|
||||
|
||||
|
||||
def stage_node_modules(source: Path, destination: Path, *, platform: str = "win32", arch: str = "x64") -> Path:
|
||||
"""Stage runtime packages without other-platform binaries or documentation."""
|
||||
source = Path(source)
|
||||
destination = Path(destination)
|
||||
if not (source / "@deepseek-ai" / "dsh" / "package.json").is_file():
|
||||
raise FileNotFoundError("DSH runtime package is not installed")
|
||||
if destination.exists():
|
||||
shutil.rmtree(destination)
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
def link_or_copy(source_file: str, destination_file: str) -> None:
|
||||
try:
|
||||
os.link(source_file, destination_file)
|
||||
except OSError:
|
||||
shutil.copy2(source_file, destination_file)
|
||||
|
||||
shutil.copytree(source, destination, ignore=lambda directory, names: _ignore(
|
||||
directory, names, platform=platform, arch=arch), copy_function=link_or_copy,
|
||||
symlinks=False)
|
||||
return destination
|
||||
@@ -1,8 +1,8 @@
|
||||
# UTF-8
|
||||
VSVersionInfo(
|
||||
ffi=FixedFileInfo(
|
||||
filevers=(1, 1, 4, 4),
|
||||
prodvers=(1, 1, 4, 4),
|
||||
filevers=(1, 2, 2, 0),
|
||||
prodvers=(1, 2, 2, 0),
|
||||
mask=0x3f,
|
||||
flags=0x2,
|
||||
OS=0x40004,
|
||||
@@ -15,11 +15,11 @@ VSVersionInfo(
|
||||
StringTable('040904B0', [
|
||||
StringStruct('CompanyName', 'DxProductivity'),
|
||||
StringStruct('FileDescription', 'DxDesigner Part Replacement'),
|
||||
StringStruct('FileVersion', '1.1.4.4'),
|
||||
StringStruct('FileVersion', '1.2.2.0'),
|
||||
StringStruct('InternalName', 'DxPartReplace'),
|
||||
StringStruct('OriginalFilename', 'DxPartReplace.exe'),
|
||||
StringStruct('ProductName', 'DxDesigner Part Replacement'),
|
||||
StringStruct('ProductVersion', '1.1.4-beta.4')
|
||||
StringStruct('ProductVersion', '1.2.2')
|
||||
])
|
||||
]),
|
||||
VarFileInfo([VarStruct('Translation', [1033, 1200])])
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "dx-productivity"
|
||||
version = "1.1.4b4"
|
||||
version = "1.2.2"
|
||||
requires-python = ">=3.12,<3.13"
|
||||
dependencies = ["PySide6>=6.7,<7", "pywin32>=306", "pywinauto>=0.6.9,<1", "openpyxl>=3.1,<4"]
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ class BatchRunResult:
|
||||
last_result: QuickRunStepResult | None = None
|
||||
item_messages: tuple[str, ...] = ()
|
||||
unbacked_project_paths: tuple[Path, ...] = ()
|
||||
safe_failure: bool = False
|
||||
|
||||
|
||||
def execute_batch(
|
||||
@@ -44,6 +45,9 @@ def execute_batch(
|
||||
runtime=None,
|
||||
) -> BatchRunResult:
|
||||
preparer = service._quick_run_preparer
|
||||
journal = getattr(preparer, 'journal', None)
|
||||
if (journal is not None and journal.requires_retry(task_id)) or service._retryable_quick_run(task_id):
|
||||
raise TaskCenterError('quick_run_retry_required', '本工程有失败项,普通继续不会重试;请准备失败项重试。')
|
||||
retention = maintain_quick_run_storage(preparer.data_root)
|
||||
if retention.removed_entries or retention.skipped_entries:
|
||||
service._logging.logger.info(
|
||||
@@ -54,6 +58,7 @@ def execute_batch(
|
||||
task = service._store.get_task(task_id)
|
||||
projects = service._store.list_projects(task_id)
|
||||
rules = service._execution_rules(task_id)
|
||||
preparer.reconcile_terminal_receipts(task_id)
|
||||
progress = preparer.progress(task_id, projects, rules, copy_part_number_to_value=task.copy_part_number_to_value)
|
||||
mode = 'part_label' if task.copy_part_number_to_value else 'preserve'
|
||||
actual = (str(progress.next_project_path), progress.next_old_part_number, progress.next_new_part_number, mode)
|
||||
@@ -195,6 +200,14 @@ def execute_batch(
|
||||
service._logging.logger.info('quick_run_batch_item task_id=%s state=%s completed=%s total=%s',
|
||||
task_id, last_result.state, updated.completed_items, updated.total_items)
|
||||
progress = updated
|
||||
if task.primary_project_id and last_result.state in {BATCH_FAILED_STATE, 'batch_skipped'}:
|
||||
# execute_step has already rejected possible unverified native
|
||||
# mutation. Leave this engineering task's remaining work held,
|
||||
# but permit the outer serial queue to run the next project.
|
||||
backups.commit_prefetched(progress)
|
||||
service._record_safe_queue_failure(task_id,last_result.message)
|
||||
return BatchRunResult(progress, False, last_result, tuple(item_messages),
|
||||
tuple(backups.unbacked_project_paths), safe_failure=True)
|
||||
if last_result.state == BATCH_SAVED_UNVERIFIED_STATE:
|
||||
_write_uncertain_flag(preparer.data_root / 'quick-run/uncertain.flag',
|
||||
'saved_readback_unverified')
|
||||
|
||||
@@ -3,6 +3,7 @@ from concurrent.futures import ThreadPoolExecutor
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -16,6 +17,32 @@ _BACKUP_STATE_NAME = 'batch-backups.json'
|
||||
_BACKUP_DIRECTORY_NAME = 'batch-backups'
|
||||
|
||||
|
||||
def inherit_backup_for_retry(archive, root, task_id):
|
||||
"""Carry the verified original backup forward; never back up modified work as original."""
|
||||
if archive is None or not (archive / _BACKUP_STATE_NAME).is_file():
|
||||
return
|
||||
state = _read_backup_state(archive / _BACKUP_STATE_NAME, task_id)
|
||||
for key, entry in state.items():
|
||||
if entry.get('status') == 'backed_up' and not _reusable_backup(archive, key, entry):
|
||||
raise TaskCenterError('quick_run_backup_invalid', '原轮次备份不完整,不能准备重试。')
|
||||
from .quick_run import _read_commands
|
||||
eligible={_path_key(Path(row[2]).resolve(strict=False).parent)
|
||||
for row in _read_commands(root/'command.tsv')}
|
||||
state={key:entry for key,entry in state.items() if key in eligible}
|
||||
source = archive / _BACKUP_DIRECTORY_NAME
|
||||
if source.is_dir():
|
||||
destination = root / _BACKUP_DIRECTORY_NAME
|
||||
if destination.exists():
|
||||
raise TaskCenterError('quick_run_backup_invalid', '重试备份目标已存在,已停止以保留原证据。')
|
||||
temporary = root / f'.retry-backups-{uuid4().hex}'
|
||||
shutil.copytree(source, temporary)
|
||||
os.replace(temporary, destination)
|
||||
for key, entry in state.items():
|
||||
if entry.get('status') == 'backed_up' and not _reusable_backup(root, key, entry):
|
||||
raise TaskCenterError('quick_run_backup_invalid', '重试备份复制校验失败,原备份保留。')
|
||||
_write_backup_state(root / _BACKUP_STATE_NAME, task_id, state)
|
||||
|
||||
|
||||
class BatchBackupCoordinator:
|
||||
"""Publish backup outcomes on the queue thread before allowing CAD work.
|
||||
|
||||
|
||||
@@ -0,0 +1,342 @@
|
||||
"""Task-database receipts: display history is never execution authority."""
|
||||
from dataclasses import dataclass
|
||||
import csv
|
||||
import hashlib
|
||||
import json
|
||||
from uuid import uuid4
|
||||
|
||||
from .errors import TaskCenterError
|
||||
from .history_store import HistoryRepository
|
||||
from .path_access import canonical_path, internal_path
|
||||
|
||||
_SUCCESS={'verified','no_match','restored'}
|
||||
_TERMINAL={'saved_ok','no_match','restored_ok','batch_failed','batch_saved_unverified','batch_skipped'}
|
||||
|
||||
|
||||
def create_execution_schema(connection):
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS execution_transitions (
|
||||
transition_id TEXT PRIMARY KEY, task_id TEXT NOT NULL)''')
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS execution_baselines (
|
||||
task_id TEXT NOT NULL, round_id TEXT NOT NULL, project_path TEXT NOT NULL,
|
||||
schematic_name TEXT NOT NULL, sheet_number TEXT NOT NULL, baseline_json TEXT NOT NULL,
|
||||
PRIMARY KEY(task_id,round_id,project_path,schematic_name,sheet_number))''')
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS execution_rounds (
|
||||
task_id TEXT PRIMARY KEY, round_id TEXT NOT NULL, plan_json TEXT NOT NULL DEFAULT '[]',
|
||||
retry_authorized INTEGER NOT NULL DEFAULT 0, pause_requested INTEGER NOT NULL DEFAULT 0)''')
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS execution_operations (
|
||||
task_id TEXT NOT NULL, round_id TEXT NOT NULL, operation_key TEXT NOT NULL,
|
||||
command_json TEXT NOT NULL, state TEXT NOT NULL, attempt_id TEXT,
|
||||
generation INTEGER NOT NULL DEFAULT 0, receipt_json TEXT,
|
||||
PRIMARY KEY(task_id,round_id,operation_key))''')
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS execution_target_claims (
|
||||
task_id TEXT NOT NULL, round_id TEXT NOT NULL, target_key TEXT NOT NULL,
|
||||
operation_key TEXT NOT NULL, PRIMARY KEY(task_id,round_id,target_key))''')
|
||||
|
||||
|
||||
def _json(value):
|
||||
return json.dumps(value,ensure_ascii=False,sort_keys=True,separators=(',',':'))
|
||||
|
||||
|
||||
def _hash(value):
|
||||
return hashlib.sha256(_json(value).encode('utf8')).hexdigest()
|
||||
|
||||
|
||||
def command_identity(row):
|
||||
values={key:str(value) for key,value in row.items() if key not in {'task_id','job_id'}}
|
||||
values['project_path']=str(canonical_path(values['project_path'])).casefold()
|
||||
for field,default in (('schematic_name',''),('sheet_number',''),('target_scope','all'),
|
||||
('target_selectors',''),('target_cell','')):
|
||||
values.setdefault(field,default)
|
||||
values['target_selectors']='\x1e'.join(sorted(values['target_selectors'].split('\x1e')))
|
||||
return values
|
||||
|
||||
|
||||
def _targets(command):
|
||||
if command['target_scope']!='targets':return ()
|
||||
return tuple(_hash([command['project_path'],command['schematic_name'],command['sheet_number'],
|
||||
selector.split('\x1f')[0]])
|
||||
for selector in command['target_selectors'].split('\x1e') if selector)
|
||||
|
||||
|
||||
def read_command_rows(path):
|
||||
from .quick_run import _decode_vbs_field
|
||||
with internal_path(path).open(encoding='utf-8-sig',newline='') as stream:
|
||||
return tuple({key:_decode_vbs_field(value) for key,value in row.items()}
|
||||
for row in csv.DictReader(stream,delimiter='\t'))
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class JournalAttempt:
|
||||
task_id: str
|
||||
round_id: str
|
||||
operation_key: str
|
||||
attempt_id: str
|
||||
generation: int
|
||||
|
||||
|
||||
class ExecutionJournal:
|
||||
def __init__(self,store,progress_writer=None):
|
||||
self.store=store
|
||||
self.progress_writer=progress_writer
|
||||
|
||||
def _round(self,task_id,*,create=False):
|
||||
connection=self.store._connection
|
||||
row=connection.execute('SELECT * FROM execution_rounds WHERE task_id=?',(task_id,)).fetchone()
|
||||
if row is None and create:
|
||||
connection.execute('INSERT INTO execution_rounds(task_id,round_id) VALUES (?,?)',(task_id,uuid4().hex))
|
||||
row=connection.execute('SELECT * FROM execution_rounds WHERE task_id=?',(task_id,)).fetchone()
|
||||
return row
|
||||
|
||||
def start_round(self,task_id,round_id):
|
||||
with self.store.transaction():
|
||||
current=self._round(task_id)
|
||||
if current:
|
||||
unsafe=self.store._connection.execute('''SELECT 1 FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND state NOT IN ('verified','no_match','restored','superseded') LIMIT 1''',
|
||||
(task_id,current['round_id'])).fetchone()
|
||||
if unsafe:raise TaskCenterError('new_round_unverified','本轮尚未核验完成,不能重置执行回执。')
|
||||
self.store._connection.execute('''INSERT INTO execution_rounds(task_id,round_id) VALUES (?,?)
|
||||
ON CONFLICT(task_id) DO UPDATE SET round_id=excluded.round_id,plan_json='[]',
|
||||
retry_authorized=0,pause_requested=0''',(task_id,round_id))
|
||||
|
||||
def authorize_retry(self,task_id):
|
||||
with self.store.transaction():
|
||||
current=self._round(task_id)
|
||||
if current:
|
||||
unsafe=self.store._connection.execute('''SELECT 1 FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND state IN ('prepared','uncertain') LIMIT 1''',
|
||||
(task_id,current['round_id'])).fetchone()
|
||||
if unsafe:raise TaskCenterError('quick_run_uncertain','原执行项尚待核验,不能准备重试。')
|
||||
self.store._connection.execute('UPDATE execution_rounds SET retry_authorized=1 WHERE task_id=?',(task_id,))
|
||||
|
||||
def pause(self,task_id):
|
||||
with self.store.transaction():
|
||||
self.store._connection.execute('UPDATE execution_rounds SET pause_requested=1 WHERE task_id=?',(task_id,))
|
||||
|
||||
def requires_retry(self,task_id):
|
||||
with self.store._lock:
|
||||
current=self._round(task_id)
|
||||
return bool(current and not current['retry_authorized'] and self.store._connection.execute(
|
||||
"SELECT 1 FROM execution_operations WHERE task_id=? AND round_id=? AND state='failed' LIMIT 1",
|
||||
(task_id,current['round_id'])).fetchone())
|
||||
|
||||
def has_plan(self,task_id):
|
||||
with self.store._lock:
|
||||
current=self._round(task_id)
|
||||
return bool(current and json.loads(current['plan_json']))
|
||||
|
||||
def needs_reconciliation(self,task_id):
|
||||
with self.store._lock:
|
||||
current=self._round(task_id)
|
||||
return bool(current and self.store._connection.execute('''SELECT 1 FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND state='prepared' LIMIT 1''',
|
||||
(task_id,current['round_id'])).fetchone())
|
||||
|
||||
def unresolved_jobs(self,task_id,rows):
|
||||
with self.store._lock:
|
||||
current=self._round(task_id)
|
||||
if current is None:return ()
|
||||
keys={record[0] for record in self.store._connection.execute('''SELECT operation_key
|
||||
FROM execution_operations WHERE task_id=? AND round_id=? AND state IN ('prepared','uncertain')''',
|
||||
(task_id,current['round_id']))}
|
||||
return tuple(row['job_id'] for row in rows if _hash(command_identity(row)) in keys)
|
||||
|
||||
def assert_can_prepare(self,task_id):
|
||||
with self.store._lock:
|
||||
current=self._round(task_id)
|
||||
if current and json.loads(current['plan_json']) and not current['retry_authorized']:
|
||||
raise TaskCenterError('quick_run_pending','本轮已有冻结的执行记录,请继续原队列或核验恢复,不能重新准备覆盖。')
|
||||
|
||||
def bind(self,task_id,rows,latest,*,proof_reader=None):
|
||||
"""Freeze/reconcile identities; return authoritative outcomes keyed by new job ID."""
|
||||
identities=[command_identity(row) for row in rows]
|
||||
keys=[_hash(identity) for identity in identities]
|
||||
if not keys or len(keys)!=len(set(keys)):
|
||||
raise TaskCenterError('quick_run_uncertain','执行计划包含重复或空的逻辑项。')
|
||||
with self.store.transaction():
|
||||
current=self._round(task_id,create=True)
|
||||
round_id=current['round_id'];connection=self.store._connection
|
||||
prior=json.loads(current['plan_json']); retry=bool(current['retry_authorized'])
|
||||
if prior and prior!=keys and not retry:
|
||||
raise TaskCenterError('quick_run_uncertain','执行计划与本轮已保存回执不一致,请核验原范围。')
|
||||
restored={}
|
||||
for row,identity,key in zip(rows,identities,keys):
|
||||
for target in _targets(identity):
|
||||
claimed=connection.execute('''SELECT operation_key FROM execution_target_claims
|
||||
WHERE task_id=? AND round_id=? AND target_key=?''',(task_id,round_id,target)).fetchone()
|
||||
if claimed and claimed[0]!=key:
|
||||
raise TaskCenterError('quick_run_uncertain','该位号本轮已有完成回执,不能重新编组执行。')
|
||||
record=connection.execute('''SELECT * FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',(task_id,round_id,key)).fetchone()
|
||||
if record is None:
|
||||
if prior and retry and key not in prior:
|
||||
# A revision may only replace a known failed scope; it
|
||||
# cannot introduce fresh work under a retry authorization.
|
||||
scope=lambda item:tuple(item.get(name,'') for name in
|
||||
('project_path','schematic_name','sheet_number','old_part','target_scope','target_selectors'))
|
||||
failures=connection.execute('''SELECT operation_key,command_json FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND state='failed' ''',(task_id,round_id)).fetchall()
|
||||
replaced=[entry['operation_key'] for entry in failures if scope(json.loads(entry['command_json']))==scope(identity)]
|
||||
if not replaced:raise TaskCenterError('quick_run_uncertain','重试范围包含非失败项,请重新确认。')
|
||||
for old_key in replaced:
|
||||
connection.execute("UPDATE execution_operations SET state='superseded' WHERE task_id=? AND round_id=? AND operation_key=?",(task_id,round_id,old_key))
|
||||
connection.execute('''INSERT INTO execution_operations
|
||||
(task_id,round_id,operation_key,command_json,state) VALUES (?,?,?,?, 'pending')''',
|
||||
(task_id,round_id,key,_json(identity)))
|
||||
elif record['state'] in _SUCCESS:
|
||||
restored[row['job_id']]=tuple(json.loads(record['receipt_json'])[name] for name in ('native_state','code','message'))
|
||||
continue
|
||||
elif record['state']=='failed' and retry:
|
||||
connection.execute("UPDATE execution_operations SET state='pending',attempt_id=NULL WHERE task_id=? AND round_id=? AND operation_key=?",(task_id,round_id,key))
|
||||
continue # The old failed TSV must not cancel explicit retry.
|
||||
outcome=latest.get(row['job_id'])
|
||||
if outcome and outcome[0] in _TERMINAL:
|
||||
record=connection.execute('''SELECT * FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',(task_id,round_id,key)).fetchone()
|
||||
if record['state']=='prepared':
|
||||
attempt=JournalAttempt(task_id,round_id,key,record['attempt_id'],record['generation'])
|
||||
else:
|
||||
attempt=self.claim(task_id,row,importing=True)
|
||||
proof=proof_reader(row,outcome) if proof_reader else None
|
||||
self.finish(attempt,*outcome,proof=proof)
|
||||
elif record is not None and record['state'] in {'prepared','uncertain'}:
|
||||
raise TaskCenterError('quick_run_uncertain','上次执行尝试没有完整结果,不能重复派发。')
|
||||
connection.execute('UPDATE execution_rounds SET plan_json=?,retry_authorized=0,pause_requested=0 WHERE task_id=?',(_json(keys),task_id))
|
||||
return restored
|
||||
|
||||
def claim(self,task_id,row,*,importing=False):
|
||||
key=_hash(command_identity(row))
|
||||
with self.store.transaction():
|
||||
current=self._round(task_id)
|
||||
if current is None:raise TaskCenterError('quick_run_uncertain','执行计划尚未持久化。')
|
||||
record=self.store._connection.execute('''SELECT * FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',(task_id,current['round_id'],key)).fetchone()
|
||||
if record is None:raise TaskCenterError('quick_run_uncertain','执行项不属于冻结计划。')
|
||||
if record['state'] in _SUCCESS:raise TaskCenterError('quick_run_complete','本轮该项已有完成回执,不会重复执行。')
|
||||
# The current UI owns one CBB per bound task. Legacy unbound
|
||||
# multi-project API queues retain their existing failure policy.
|
||||
hold_project=bool(self.store.get_task(task_id).primary_project_id)
|
||||
if record['state']=='failed' or (hold_project and not importing and self.requires_retry(task_id)):
|
||||
raise TaskCenterError('quick_run_retry_required','本工程有失败项,请准备失败项重试;不会重新执行成功项。')
|
||||
if record['state']!='pending':raise TaskCenterError('quick_run_uncertain','上次尝试尚未核验,禁止再次派发。')
|
||||
identifier=uuid4().hex;generation=record['generation']+1
|
||||
self.store._connection.execute('''UPDATE execution_operations SET state='prepared',attempt_id=?,generation=?
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',(identifier,generation,task_id,current['round_id'],key))
|
||||
return JournalAttempt(task_id,current['round_id'],key,identifier,generation)
|
||||
|
||||
def finish(self,attempt,native_state,code,message,*,progress=None,proof=None):
|
||||
if native_state not in _TERMINAL:raise TaskCenterError('quick_run_uncertain','不能提交未知的执行结果。')
|
||||
state=({'saved_ok':'verified','no_match':'no_match','restored_ok':'restored',
|
||||
'batch_failed':'failed','batch_skipped':'failed','batch_saved_unverified':'uncertain'})[native_state]
|
||||
if native_state=='restored_ok' and code in {'recovery_verified','recovery_saved_verified'}:
|
||||
state='verified'
|
||||
with self.store.transaction():
|
||||
connection=self.store._connection
|
||||
record=connection.execute('''SELECT * FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',
|
||||
(attempt.task_id,attempt.round_id,attempt.operation_key)).fetchone()
|
||||
if (record is None or record['state']!='prepared' or record['attempt_id']!=attempt.attempt_id
|
||||
or record['generation']!=attempt.generation or self._round(attempt.task_id)['round_id']!=attempt.round_id):
|
||||
raise TaskCenterError('quick_run_uncertain','旧尝试的结果不能覆盖当前执行回执。')
|
||||
command=json.loads(record['command_json'])
|
||||
receipt={'native_state':native_state,'code':code,'message':message}
|
||||
if proof is not None:
|
||||
from .execution_proof import proof_intact
|
||||
if not proof_intact(proof):raise TaskCenterError('quick_run_uncertain','执行证明不完整,不能提交回执。')
|
||||
receipt['proof']=proof
|
||||
connection.execute('''UPDATE execution_operations SET state=?,receipt_json=?
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',
|
||||
(state,_json(receipt),attempt.task_id,attempt.round_id,attempt.operation_key))
|
||||
if state in _SUCCESS:
|
||||
if proof and proof.get('baseline'):
|
||||
baseline=dict(proof['baseline'])
|
||||
baseline['project_path']=command['project_path']
|
||||
baseline['files']=proof.get('files',[])
|
||||
connection.execute('''INSERT INTO execution_baselines VALUES (?,?,?,?,?,?)
|
||||
ON CONFLICT(task_id,round_id,project_path,schematic_name,sheet_number)
|
||||
DO UPDATE SET baseline_json=excluded.baseline_json''',
|
||||
(attempt.task_id,attempt.round_id,command['project_path'],baseline['schematic_name'],
|
||||
baseline['sheet_number'],_json(baseline)))
|
||||
for target in _targets(command):
|
||||
connection.execute('INSERT INTO execution_target_claims VALUES (?,?,?,?)',
|
||||
(attempt.task_id,attempt.round_id,target,attempt.operation_key))
|
||||
task=self.store.get_task(attempt.task_id)
|
||||
history_receipt={key:value for key,value in receipt.items() if key!='proof'}
|
||||
if proof:
|
||||
history_receipt['proof_file_count']=len(proof.get('files',()))
|
||||
HistoryRepository(self.store).append(task_id=attempt.task_id,task_name=task.name,
|
||||
project_path=command['project_path'],operation='器件替换',state=state,summary=message,
|
||||
details={**command,**history_receipt,'round_id':attempt.round_id,'attempt_id':attempt.attempt_id,
|
||||
'generation':attempt.generation},
|
||||
source_key=f'receipt:{attempt.task_id}:{attempt.round_id}:{attempt.operation_key}:{attempt.generation}')
|
||||
if progress is not None and self.progress_writer is not None:
|
||||
self.progress_writer(attempt.task_id,progress=progress)
|
||||
|
||||
def reconcile_recovery(self,task_id,row,state,code,message,*,recovery_id=None,proof=None):
|
||||
"""Only called after the existing explicitly confirmed native recovery succeeds."""
|
||||
if state not in {'restored_ok','retry_ready'}:
|
||||
raise TaskCenterError('quick_run_recovery','恢复没有返回明确核验结论。')
|
||||
key=_hash(command_identity(row))
|
||||
with self.store.transaction():
|
||||
if recovery_id:
|
||||
if self.store._connection.execute('SELECT 1 FROM execution_transitions WHERE transition_id=?',(recovery_id,)).fetchone():return
|
||||
self.store._connection.execute('INSERT INTO execution_transitions VALUES (?,?)',(recovery_id,task_id))
|
||||
current=self._round(task_id)
|
||||
if current is None:return # Legacy handoff is imported after recovery.
|
||||
record=self.store._connection.execute('''SELECT * FROM execution_operations
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',(task_id,current['round_id'],key)).fetchone()
|
||||
if record is None:raise TaskCenterError('quick_run_recovery','恢复项与原轮次身份不一致。')
|
||||
if record['state'] in _SUCCESS:return
|
||||
generation=record['generation']+1
|
||||
identifier=uuid4().hex
|
||||
self.store._connection.execute('''UPDATE execution_operations
|
||||
SET state=?,generation=?,attempt_id=?,receipt_json=NULL
|
||||
WHERE task_id=? AND round_id=? AND operation_key=?''',
|
||||
('pending' if state=='retry_ready' else 'prepared',generation,identifier,
|
||||
task_id,current['round_id'],key))
|
||||
if state=='restored_ok':
|
||||
self.finish(JournalAttempt(task_id,current['round_id'],key,identifier,generation),state,code,message,proof=proof)
|
||||
else:
|
||||
task=self.store.get_task(task_id)
|
||||
HistoryRepository(self.store).append(task_id=task_id,task_name=task.name,
|
||||
project_path=command_identity(row)['project_path'],operation='quick_run_recovered',
|
||||
state='restored',summary=message,
|
||||
details={'native_replacement_called':False,'round_id':current['round_id'],'generation':generation},
|
||||
source_key=f'recovery:{task_id}:{current["round_id"]}:{key}:{generation}')
|
||||
|
||||
def conflicts(self,task_id,rows,latest):
|
||||
"""Read-only admission check, including receipt/file rollback detection."""
|
||||
with self.store._lock:
|
||||
current=self._round(task_id)
|
||||
if current is None:return False
|
||||
keys=[_hash(command_identity(row)) for row in rows]
|
||||
if json.loads(current['plan_json'])!=keys and not current['retry_authorized']:return True
|
||||
records={record['operation_key']:record for record in self.store._connection.execute('''
|
||||
SELECT operation_key,state,receipt_json FROM execution_operations WHERE task_id=? AND round_id=?''',
|
||||
(task_id,current['round_id']))}
|
||||
for row,key in zip(rows,keys):
|
||||
record=records.get(key)
|
||||
if record is None:continue
|
||||
outcome=latest.get(row['job_id'])
|
||||
if record['state'] in _SUCCESS:
|
||||
receipt=json.loads(record['receipt_json'])
|
||||
from .execution_proof import proof_intact
|
||||
if not proof_intact(receipt.get('proof',{})):return True
|
||||
if not outcome or tuple(outcome[:2])!=(receipt['native_state'],receipt['code']):return True
|
||||
elif record['state']=='uncertain':return True
|
||||
elif record['state']=='prepared' and (not outcome or outcome[0] not in _TERMINAL):return True
|
||||
return False
|
||||
|
||||
def expected_baselines(self,task_id,project_path):
|
||||
from .execution_proof import proof_intact
|
||||
key=str(canonical_path(project_path)).casefold()
|
||||
with self.store._lock:
|
||||
current=self._round(task_id)
|
||||
if current is None:return ()
|
||||
records=self.store._connection.execute('''SELECT baseline_json FROM execution_baselines
|
||||
WHERE task_id=? AND round_id=? AND project_path=? ORDER BY schematic_name,sheet_number''',
|
||||
(task_id,current['round_id'],key)).fetchall()
|
||||
values=tuple(json.loads(row[0]) for row in records)
|
||||
if any(not proof_intact(value) for value in values):
|
||||
raise TaskCenterError('quick_run_uncertain','最近已核验的工程基线证据缺失或变化,已停止续跑。')
|
||||
return values
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Bind durable receipts to immutable native evidence and the last verified sheet."""
|
||||
import csv
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from .errors import TaskCenterError
|
||||
from .path_access import canonical_path, internal_path
|
||||
|
||||
|
||||
def proof_intact(proof):
|
||||
try:
|
||||
return all(hashlib.sha256(internal_path(item['path']).read_bytes()).hexdigest()==item['sha256']
|
||||
for item in proof.get('files',()))
|
||||
except (OSError,ValueError,KeyError,TypeError):return False
|
||||
|
||||
|
||||
def capture_result(data_root,root,row,outcome,*,strict=False,persist=True):
|
||||
from .quick_run import _decode_vbs_field, _safe_run_token
|
||||
from .native_helper import read_snapshot, require_resumable_transaction, snapshot_target_cell
|
||||
state,code,_=outcome
|
||||
proof={'files':[]}
|
||||
paths=[]
|
||||
try:
|
||||
_safe_run_token(row['job_id'])
|
||||
if strict and state in {'saved_ok','no_match'} and code not in {'batch_closed','session_verified'}:
|
||||
raise ValueError('save/close was not verified')
|
||||
checked={}
|
||||
baseline=internal_path(root/f'post-state-{row["job_id"]}.tsv')
|
||||
if baseline.is_file():
|
||||
checked[baseline]=baseline.read_bytes()
|
||||
with baseline.open(encoding='utf-8-sig',newline='') as stream:
|
||||
rows=list(csv.DictReader(stream,delimiter='\t'))
|
||||
if len(rows)!=1:raise ValueError('ambiguous semantic state')
|
||||
actual={key:_decode_vbs_field(value) for key,value in rows[0].items()}
|
||||
if (actual['job_id']!=row['job_id'] or
|
||||
canonical_path(actual['project_path'])!=canonical_path(row['project_path']) or
|
||||
(row.get('schematic_name') and (actual['schematic_name'],actual['sheet_number'])!=(row['schematic_name'],row['sheet_number'])) or
|
||||
not actual['semantic_state'].startswith('S\tfull_sheet_v1\r\n')):
|
||||
raise ValueError('semantic state identity mismatch')
|
||||
proof['baseline']={'project_path':str(canonical_path(row['project_path'])).casefold(),
|
||||
'schematic_name':actual['schematic_name'],'sheet_number':actual['sheet_number'],'state':actual['semantic_state']}
|
||||
paths.append(baseline)
|
||||
elif strict and state in {'saved_ok','no_match','restored_ok','recovery_readback'}:
|
||||
raise ValueError('missing verified semantic state')
|
||||
if state=='saved_ok' or (state=='restored_ok' and code in {'recovery_verified','recovery_saved_verified'}):
|
||||
snapshot=internal_path(root/f'snapshot-{row["job_id"]}.tsv')
|
||||
report_path=Path(str(snapshot)+'.native.json')
|
||||
if strict or snapshot.is_file() or report_path.is_file():
|
||||
checked[snapshot]=snapshot.read_bytes()
|
||||
checked[report_path]=report_path.read_bytes()
|
||||
metadata,components=read_snapshot(snapshot)
|
||||
report=json.loads(report_path.read_text(encoding='utf8'))
|
||||
require_resumable_transaction(snapshot,report,metadata,components)
|
||||
expected_sheet=f'{row["schematic_name"]}:{row["sheet_number"]}' if row.get('schematic_name') else metadata[4]
|
||||
if (canonical_path(metadata[1])!=canonical_path(row['project_path']) or
|
||||
metadata[2:4]!=[row['old_part'],row['new_part']] or metadata[4]!=expected_sheet or
|
||||
metadata[5]!=row.get('value_mode','preserve') or
|
||||
(metadata[6:8] if len(metadata)>=8 else ['', '1'])!=[row.get('custom_value',''),row.get('sync_nc','1')] or
|
||||
(metadata[9] if len(metadata)==10 else '')!=row.get('target_selectors','') or
|
||||
(snapshot_target_cell(snapshot,required=False) or '')!=row.get('target_cell','')):
|
||||
raise ValueError('snapshot command changed')
|
||||
attempt=report.get('batch_attempt')
|
||||
if strict:
|
||||
if report.get('stage')!='native_replace_verified' or report.get('ok') is not True:
|
||||
raise ValueError('native result is not verified')
|
||||
if (not isinstance(attempt,dict) or attempt.get('job_id')!=row['job_id'] or
|
||||
attempt.get('task_id')!=row['task_id'] or
|
||||
canonical_path(attempt.get('project_path',''))!=canonical_path(row['project_path']) or
|
||||
type(attempt.get('generation')) is not int or attempt['generation']<1 or
|
||||
not all(isinstance(attempt.get(name),str) and len(attempt[name])==32 for name in ('session_id','attempt_id'))):
|
||||
raise ValueError('native attempt differs from this job')
|
||||
proof['native_attempt']=attempt
|
||||
paths.extend((snapshot,report_path))
|
||||
if any(path.read_bytes()!=data for path,data in checked.items()):
|
||||
raise ValueError('execution evidence changed during receipt binding')
|
||||
if persist and paths:
|
||||
# Independent of active handoff/archive/retention paths.
|
||||
destination=internal_path(data_root/'execution-proof'/uuid4().hex)
|
||||
destination.mkdir(parents=True,exist_ok=False)
|
||||
for path in paths:
|
||||
data=checked[path]
|
||||
target=destination/path.name
|
||||
with target.open('xb') as stream:
|
||||
stream.write(data);stream.flush();os.fsync(stream.fileno())
|
||||
proof['files'].append({'path':str(canonical_path(target)), 'sha256':hashlib.sha256(data).hexdigest()})
|
||||
return proof
|
||||
except (OSError,ValueError,KeyError,TypeError,UnicodeError) as error:
|
||||
raise TaskCenterError('quick_run_uncertain','原生回执、尝试身份或工程语义基线无法核验,已停止以避免重复替换。') from error
|
||||
|
||||
|
||||
def capture_recovery(data_root,audit,row,outcome,*,strict=False):
|
||||
"""The recovery audit proves readback; it never asserts a second replacement."""
|
||||
worker=audit/'worker'
|
||||
proof=capture_result(data_root,worker,row,('recovery_readback','',outcome[2]),strict=strict)
|
||||
try:
|
||||
record=json.loads((audit/'recovery.json').read_text(encoding='utf8'))
|
||||
snapshot=audit/'evidence-before'/f'snapshot-{row["job_id"]}.tsv'
|
||||
if (record['task_id']!=row['task_id'] or record['job_id']!=row['job_id'] or
|
||||
tuple(record['worker_result'][1:3])!=tuple(outcome[:2]) or
|
||||
record['native_replacement_called'] is not False or
|
||||
hashlib.sha256(snapshot.read_bytes()).hexdigest()!=record['snapshot_sha256']):
|
||||
raise ValueError('recovery proof differs from original operation')
|
||||
destination=internal_path(data_root/'execution-proof'/uuid4().hex)
|
||||
destination.mkdir(parents=True,exist_ok=False)
|
||||
for path in (audit/'recovery.json',snapshot,Path(str(snapshot)+'.native.json'),worker/'status.tsv'):
|
||||
data=path.read_bytes();target=destination/path.name
|
||||
with target.open('xb') as stream:
|
||||
stream.write(data);stream.flush();os.fsync(stream.fileno())
|
||||
proof['files'].append({'path':str(canonical_path(target)),'sha256':hashlib.sha256(data).hexdigest()})
|
||||
proof['recovery_id']=audit.name
|
||||
return proof
|
||||
except (OSError,ValueError,KeyError,TypeError) as error:
|
||||
raise TaskCenterError('quick_run_uncertain','恢复审计无法与原执行项绑定,保护记录保留。') from error
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Incomplete execution evidence must never be interpreted as a fresh queue."""
|
||||
from .errors import TaskCenterError
|
||||
from .path_access import internal_path
|
||||
|
||||
|
||||
def assert_handoff_intact(root, task_id, *, files=None):
|
||||
from . import quick_run as api
|
||||
command=root/'command.tsv'; status=root/'status.tsv'
|
||||
exists=lambda path:internal_path(path).is_file()
|
||||
read=lambda path:files.read(path) if files is not None else internal_path(path).read_text(encoding='utf-8-sig')
|
||||
parser=lambda path:files.path(path) if files is not None else path
|
||||
try:
|
||||
command_exists,status_exists=exists(command),exists(status)
|
||||
if not command_exists and not status_exists:
|
||||
if (exists(root/'current-command.tsv') or exists(root/'uncertain.flag')
|
||||
or exists(root/'quick-part-replace.log')):
|
||||
raise ValueError('execution evidence is missing')
|
||||
return
|
||||
if not command_exists or not status_exists:
|
||||
raise ValueError('incomplete command/status pair')
|
||||
if not read(command).endswith('\n') or not read(status).endswith('\n'):
|
||||
raise ValueError('truncated handoff')
|
||||
commands=api._read_commands(parser(command))
|
||||
details=api._read_status_details(parser(status))
|
||||
jobs={row[1] for row in commands}
|
||||
if not commands or len(jobs)!=len(commands) or any(row[0] not in jobs for row in details):
|
||||
raise ValueError('inconsistent command/status identities')
|
||||
if {row[0] for row in commands}!={task_id}:
|
||||
return # Existing legacy routing validates ownership separately.
|
||||
current=root/'current-command.tsv'
|
||||
if exists(current):
|
||||
current_commands=api._read_commands(parser(current))
|
||||
latest={row[0]:row[1] for row in details}
|
||||
if (len(current_commands)!=1 or current_commands[0] not in commands
|
||||
or current_commands[0][1] not in latest):
|
||||
raise ValueError('dispatched item has no matching outcome')
|
||||
except (OSError, UnicodeError, ValueError, TaskCenterError) as error:
|
||||
raise TaskCenterError('quick_run_uncertain',
|
||||
'执行记录缺失、损坏或不一致,已停止以避免重复替换;请保留证据并核验恢复。') from error
|
||||
@@ -0,0 +1,277 @@
|
||||
"""Transactional user changes and one-time read-only import of legacy task events."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import defaultdict
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import stat
|
||||
from uuid import uuid4
|
||||
|
||||
from .errors import TaskCenterError
|
||||
from .history_store import HistoryRepository
|
||||
from .path_access import internal_path
|
||||
from .store_read_model import checkpoint, read_lock
|
||||
|
||||
|
||||
_MUTATIONS={
|
||||
'create_task','create_project_tasks','create_assistant_project_tasks','create_scanned_task','update_task','delete_task',
|
||||
'restore_task','purge_task','add_rule','update_rule','delete_rule','move_rule',
|
||||
'replace_target_plan','import_target_plan','save_bom_batch','save_plan_drafts',
|
||||
'set_value_policy','move_project_task','set_project_selected','select_all_projects',
|
||||
'clear_project_selection','scan_projects','refresh_bound_project','collect_target_inventory',
|
||||
}
|
||||
# start_new_round deliberately records inside its existing compensating
|
||||
# transaction. Wrapping its filesystem moves in another transaction would let
|
||||
# its inner savepoint report committed before the outer history COMMIT.
|
||||
# Inventory collection and bound-project refresh capture only their save phase;
|
||||
# their external reads and subsequent context reads stay outside this transaction.
|
||||
_LABELS={'create_task':'新建任务','create_project_tasks':'创建工程任务','create_assistant_project_tasks':'助手创建工程任务','create_scanned_task':'创建工程任务',
|
||||
'update_task':'编辑任务','delete_task':'归档任务','restore_task':'恢复任务','purge_task':'删除任务记录',
|
||||
'add_rule':'添加规则','update_rule':'修改规则','delete_rule':'删除规则','move_rule':'调整规则顺序',
|
||||
'replace_target_plan':'保存位号计划','import_target_plan':'导入位号计划','save_bom_batch':'保存 BOM 计划',
|
||||
'save_plan_drafts':'保存位号计划','set_value_policy':'修改 Value 策略','move_project_task':'调整任务顺序',
|
||||
'set_project_selected':'修改工程选择','select_all_projects':'全选工程',
|
||||
'clear_project_selection':'清空工程选择','scan_projects':'扫描工程','refresh_bound_project':'重新读取工程',
|
||||
'collect_target_inventory':'采集位号计划'}
|
||||
|
||||
_TABLES={
|
||||
'tasks':(('task_id',),('task_id','name','note','discovery_root','status','copy_part_number_to_value',
|
||||
'target_plan_mode','group_id','group_position','primary_project_id','summary')),
|
||||
'rules':(('rule_id',),('rule_id','task_id','position','old_part_number','new_part_number','enabled',
|
||||
'custom_value_enabled','custom_value','sync_nc','target_cell')),
|
||||
'projects':(('project_id',),('project_id','task_id','display_name','path','discovery_root',
|
||||
'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')),
|
||||
}
|
||||
_CUTOFF='history_import_cutoff_v1'
|
||||
_IMPORTED='history_import_done_v1'
|
||||
_LEGACY_OPERATIONS={'quick_run_executed','quick_run_recovered','quick_run_prepared',
|
||||
'quick_run_queue_paused','quick_run_retry_prepared','value_policy_changed','quick_run_inventory_prefiltered'}
|
||||
_LEGACY_DETAILS={'state','code','refdes','uid','schematic_name','sheet_number','project_path',
|
||||
'old_part_number','new_part_number','old_part','new_part','old_value','custom_value','sync_nc','target_cell',
|
||||
'completed_items','total_items','saved_items','failed_items','no_match_items','completed_projects',
|
||||
'prefiltered_items','copy_part_label_to_value','copy_part_number_to_value','project_ids'}
|
||||
_MAX_LEGACY_LINE=8*1024*1024
|
||||
|
||||
|
||||
def _json_object(prefix,fields):
|
||||
return 'json_object('+','.join(f"'{field}',{prefix}.{field}" for field in fields)+')'
|
||||
|
||||
|
||||
def _install_capture(connection, token):
|
||||
table='history_capture_'+token
|
||||
connection.execute(f'''CREATE TEMP TABLE {table} (
|
||||
sequence INTEGER PRIMARY KEY, table_name TEXT, item_key TEXT, task_id TEXT,
|
||||
before_json TEXT, after_json TEXT)''')
|
||||
triggers=[]
|
||||
for name,(keys,fields) in _TABLES.items():
|
||||
for operation in ('INSERT','UPDATE','DELETE'):
|
||||
trigger=f'{table}_{name}_{operation.lower()}'
|
||||
triggers.append(trigger)
|
||||
prefix='OLD' if operation=='DELETE' else 'NEW'
|
||||
identity='json_array('+','.join(f'{prefix}.{key}' for key in keys)+')'
|
||||
before='NULL' if operation=='INSERT' else _json_object('OLD',fields)
|
||||
after='NULL' if operation=='DELETE' else _json_object('NEW',fields)
|
||||
connection.execute(f'''CREATE TEMP TRIGGER {trigger} AFTER {operation} ON main.{name}
|
||||
BEGIN INSERT INTO {table}(table_name,item_key,task_id,before_json,after_json)
|
||||
VALUES ('{name}',{identity},{prefix}.task_id,{before},{after}); END''')
|
||||
return table,triggers
|
||||
|
||||
|
||||
def _changes(records):
|
||||
combined={}
|
||||
for row in records:
|
||||
key=(row['table_name'],row['item_key'])
|
||||
if key not in combined:
|
||||
combined[key]={'table':row['table_name'],'task_id':row['task_id'],
|
||||
'before':json.loads(row['before_json']) if row['before_json'] is not None else None,
|
||||
'after':None}
|
||||
combined[key]['after']=json.loads(row['after_json']) if row['after_json'] is not None else None
|
||||
result=[]
|
||||
for record in combined.values():
|
||||
before,after=record['before'],record['after']
|
||||
if before==after:continue
|
||||
source=after if after is not None else before
|
||||
keys=_TABLES[record['table']][0]
|
||||
identity={key:source[key] for key in keys}
|
||||
for name in ('refdes','old_part_number','project_path'):
|
||||
if name in source:identity[name]=source[name]
|
||||
if before is not None and after is not None:
|
||||
changed=[key for key in source if before.get(key)!=after.get(key)]
|
||||
before={key:before.get(key) for key in changed}
|
||||
after={key:after.get(key) for key in changed}
|
||||
result.append({'table':record['table'],'task_id':record['task_id'],'identity':identity,
|
||||
'before':before,'after':after})
|
||||
return result
|
||||
|
||||
|
||||
def _task_context(connection,task_id,changes):
|
||||
task=connection.execute('SELECT name FROM tasks WHERE task_id=?',(task_id,)).fetchone()
|
||||
name=task['name'] if task is not None else ''
|
||||
project=connection.execute('SELECT path FROM projects WHERE task_id=? ORDER BY path_identity LIMIT 1',
|
||||
(task_id,)).fetchone()
|
||||
path=project['path'] if project is not None else ''
|
||||
for change in changes:
|
||||
source=change['after'] if change['after'] is not None else change['before']
|
||||
if change['table']=='tasks' and not name:name=source.get('name','')
|
||||
if change['table']=='projects' and not path:path=source.get('path','')
|
||||
if change['table']=='target_plan' and not path:path=change['identity'].get('project_path','')
|
||||
return name or task_id,path
|
||||
|
||||
|
||||
def _summary(name,changes):
|
||||
parts=[]
|
||||
for change in changes[:3]:
|
||||
before,after=change['before'],change['after']
|
||||
identity=change['identity']
|
||||
if change['table']=='tasks' and before and after and 'name' in before:
|
||||
parts.append(f"名称 {before['name']} → {after['name']}")
|
||||
elif change['table']=='rules':
|
||||
old=identity.get('old_part_number','')
|
||||
previous=before.get('new_part_number','') if before else ''
|
||||
current=after.get('new_part_number','') if after else ''
|
||||
parts.append(f'{old}:{previous} → {current}' if previous or current else old)
|
||||
elif change['table']=='target_plan':parts.append(str(identity.get('refdes','位号')))
|
||||
base=_LABELS.get(name,name)
|
||||
return f'{base}:'+(';'.join(parts)+';' if parts else '')+f'记录 {len(changes)} 行实际改动'
|
||||
|
||||
|
||||
def record_operation(service,name,task_id,action):
|
||||
"""Capture only real database changes; history failure rolls them back too."""
|
||||
if name not in _MUTATIONS:return action()
|
||||
store=service._store
|
||||
with store.transaction():
|
||||
table,triggers=_install_capture(store._connection,uuid4().hex)
|
||||
try:
|
||||
result=action()
|
||||
changed=_changes(store._connection.execute(f'SELECT * FROM {table} ORDER BY sequence').fetchall())
|
||||
groups=defaultdict(list)
|
||||
for change in changed:groups[change.pop('task_id')].append(change)
|
||||
repository=HistoryRepository(store)
|
||||
operation_group=uuid4().hex
|
||||
for owner,changes in groups.items():
|
||||
task_name,project_path=_task_context(store._connection,owner,changes)
|
||||
chunks=[changes[index:index+100] for index in range(0,len(changes),100)]
|
||||
for index,chunk in enumerate(chunks):
|
||||
repository.append(task_id=owner,task_name=task_name,project_path=project_path,
|
||||
operation=name,state='recorded',summary=_summary(name,chunk),
|
||||
details={'changes':chunk,'operation_group':operation_group,
|
||||
'chunk_index':index+1,'chunk_count':len(chunks),'total_changed_rows':len(changes)},
|
||||
source_key=f'operation:{operation_group}:{owner}:{index}')
|
||||
return result
|
||||
finally:
|
||||
for trigger in triggers:store._connection.execute(f'DROP TRIGGER IF EXISTS {trigger}')
|
||||
store._connection.execute(f'DROP TABLE IF EXISTS {table}')
|
||||
|
||||
|
||||
def initialize_history_import(store):
|
||||
"""Persist the upgrade boundary once; per-task facades never move it."""
|
||||
cutoff=datetime.now(timezone.utc).isoformat(timespec='microseconds').replace('+00:00','Z')
|
||||
with store.transaction():
|
||||
store._connection.execute('INSERT OR IGNORE INTO metadata(key,value) VALUES (?,?)',(_CUTOFF,cutoff))
|
||||
|
||||
|
||||
def _instant(value):
|
||||
if not isinstance(value,str):raise ValueError('missing legacy event timestamp')
|
||||
result=datetime.fromisoformat(value.replace('Z','+00:00'))
|
||||
if result.tzinfo is None or result.utcoffset() is None:
|
||||
raise ValueError('legacy event timestamp lacks timezone')
|
||||
return result.astimezone(timezone.utc)
|
||||
|
||||
|
||||
def _state(operation,details):
|
||||
state=details.get('state')
|
||||
if operation=='quick_run_queue_paused':return 'paused'
|
||||
if operation=='quick_run_inventory_prefiltered':return 'no_match'
|
||||
if operation in {'quick_run_prepared','quick_run_retry_prepared','value_policy_changed'}:return 'recorded'
|
||||
return {'saved_ok':'verified','no_match':'no_match','restored_ok':'restored',
|
||||
'batch_failed':'failed','hard_failure':'failed','batch_skipped':'skipped',
|
||||
'batch_saved_unverified':'uncertain','awaiting_native_replace':'uncertain'}.get(state,'uncertain')
|
||||
|
||||
|
||||
def _reject_redirect(path):
|
||||
info=internal_path(path).lstat()
|
||||
if stat.S_ISLNK(info.st_mode) or getattr(info,'st_file_attributes',0)&0x400:
|
||||
raise OSError('redirected legacy history source')
|
||||
|
||||
|
||||
def _legacy_files(root,read_control):
|
||||
root=Path(root)/'tasks'
|
||||
if not internal_path(root).exists():return
|
||||
_reject_redirect(root)
|
||||
for directory in sorted(internal_path(root).iterdir(),key=lambda value:value.name):
|
||||
checkpoint(read_control)
|
||||
_reject_redirect(directory)
|
||||
if not directory.is_dir():continue
|
||||
for source in sorted(directory.iterdir(),key=lambda value:value.name):
|
||||
checkpoint(read_control)
|
||||
if not re.fullmatch(r'events\.jsonl(?:\.[1-9][0-9]*)?',source.name):continue
|
||||
_reject_redirect(source)
|
||||
if not source.is_file():continue
|
||||
yield directory.name,source
|
||||
|
||||
|
||||
def _import_file(service,repository,task_id,source,cutoff,read_control):
|
||||
connection=service._store._connection
|
||||
task=connection.execute('SELECT name FROM tasks WHERE task_id=?',(task_id,)).fetchone()
|
||||
task_name=task['name'] if task is not None else task_id
|
||||
with internal_path(source).open('r',encoding='utf-8-sig') as stream:
|
||||
while True:
|
||||
checkpoint(read_control)
|
||||
line=stream.readline(_MAX_LEGACY_LINE+1)
|
||||
if not line:break
|
||||
if len(line)>_MAX_LEGACY_LINE:raise ValueError('legacy event exceeds import limit')
|
||||
if not line.strip():continue
|
||||
event=json.loads(line)
|
||||
if not isinstance(event,dict):raise ValueError('legacy event is not an object')
|
||||
operation=event.get('operation')
|
||||
if operation not in _LEGACY_OPERATIONS:continue
|
||||
if event.get('task_id')!=task_id:raise ValueError('legacy event task identity mismatch')
|
||||
instant=_instant(event.get('timestamp_utc'))
|
||||
if instant>=cutoff:continue
|
||||
raw_details=event.get('details',{})
|
||||
if not isinstance(raw_details,dict):raise ValueError('legacy event details are not an object')
|
||||
details={key:value for key,value in raw_details.items() if key in _LEGACY_DETAILS}
|
||||
message=event.get('message','')
|
||||
if not isinstance(message,str):raise ValueError('legacy event message is not text')
|
||||
identity={'task_id':task_id,'sequence':event.get('sequence'),'timestamp_utc':instant.isoformat(),
|
||||
'operation':operation,'details':details,'message':message}
|
||||
source_key='legacy:v1:'+hashlib.sha256(json.dumps(identity,ensure_ascii=False,sort_keys=True,
|
||||
separators=(',',':'),allow_nan=False).encode('utf8')).hexdigest()
|
||||
details=dict(details,legacy_record=True)
|
||||
repository.append(task_id=task_id,task_name=task_name,
|
||||
project_path=str(details.get('project_path') or ''),operation=operation,
|
||||
state=_state(operation,details),summary=message or _LABELS.get(operation,'旧版本操作记录'),
|
||||
details=details,source_key=source_key,timestamp_utc=instant.isoformat())
|
||||
|
||||
|
||||
def ensure_legacy_history(service,read_control=None):
|
||||
"""Import pre-upgrade supported events; cancellation/error never marks done."""
|
||||
store=service._store
|
||||
checkpoint(read_control)
|
||||
with read_lock(store,read_control):
|
||||
complete=store._connection.execute('SELECT value FROM metadata WHERE key=?',(_IMPORTED,)).fetchone()
|
||||
if complete is not None and complete['value']=='1':return
|
||||
cutoff=store._connection.execute('SELECT value FROM metadata WHERE key=?',(_CUTOFF,)).fetchone()
|
||||
if cutoff is None:
|
||||
initialize_history_import(store)
|
||||
with read_lock(store,read_control):
|
||||
cutoff=store._connection.execute('SELECT value FROM metadata WHERE key=?',(_CUTOFF,)).fetchone()
|
||||
try:
|
||||
instant=_instant(cutoff['value'])
|
||||
repository=HistoryRepository(store)
|
||||
for task_id,source in _legacy_files(service._logging.root,read_control):
|
||||
# Commit bounded source files independently. Retried files are
|
||||
# source-deduplicated; an interrupted final file is rolled back.
|
||||
with store.transaction():_import_file(service,repository,task_id,source,instant,read_control)
|
||||
checkpoint(read_control)
|
||||
with store.transaction():
|
||||
checkpoint(read_control)
|
||||
store._connection.execute('INSERT OR REPLACE INTO metadata(key,value) VALUES (?,?)',(_IMPORTED,'1'))
|
||||
except (OSError,UnicodeError,ValueError,TypeError) as error:
|
||||
raise TaskCenterError('history_import','旧操作历史暂时无法完整读取,原文件已保留,可修复后重试。') from error
|
||||
@@ -0,0 +1,153 @@
|
||||
"""User-visible operation history, separate from durable execution admission."""
|
||||
from __future__ import annotations
|
||||
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
|
||||
from .store_read_model import checkpoint, read_lock
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class HistoryPage:
|
||||
rows: tuple[dict, ...]
|
||||
next_cursor: int | None
|
||||
total: int
|
||||
all_total: int | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class HistoryClearSnapshot:
|
||||
cutoff_id: int
|
||||
count: int
|
||||
|
||||
|
||||
def create_history_schema(connection) -> None:
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS operation_history (
|
||||
event_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
timestamp_utc TEXT NOT NULL, task_id TEXT NOT NULL, task_name TEXT NOT NULL,
|
||||
project_path TEXT NOT NULL, operation TEXT NOT NULL, state TEXT NOT NULL,
|
||||
summary TEXT NOT NULL, details_json TEXT NOT NULL, search_text TEXT NOT NULL)''')
|
||||
connection.execute('CREATE INDEX IF NOT EXISTS history_state_id ON operation_history(state,event_id)')
|
||||
connection.execute('CREATE INDEX IF NOT EXISTS history_time_id ON operation_history(timestamp_utc DESC,event_id DESC)')
|
||||
connection.execute('CREATE INDEX IF NOT EXISTS history_state_time ON operation_history(state,timestamp_utc DESC,event_id DESC)')
|
||||
# This table deliberately has no foreign key to operation_history: source
|
||||
# identities survive clearing so migration/reindexing cannot resurrect them.
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS operation_history_sources (
|
||||
source_key TEXT PRIMARY KEY, event_id INTEGER NOT NULL)''')
|
||||
connection.execute('''CREATE TABLE IF NOT EXISTS operation_history_meta (
|
||||
singleton INTEGER PRIMARY KEY CHECK(singleton=1), cleared_through_id INTEGER NOT NULL)''')
|
||||
connection.execute('INSERT OR IGNORE INTO operation_history_meta VALUES (1,0)')
|
||||
|
||||
|
||||
class HistoryRepository:
|
||||
def __init__(self, store):
|
||||
self.store = store
|
||||
|
||||
@contextmanager
|
||||
def _read(self, read_control=None):
|
||||
with read_lock(self.store, read_control):
|
||||
connection=self.store._connection
|
||||
owns_transaction=self.store._transaction_depth==0
|
||||
if owns_transaction: connection.execute('BEGIN')
|
||||
try:
|
||||
yield connection
|
||||
checkpoint(read_control)
|
||||
if owns_transaction: connection.execute('COMMIT')
|
||||
except BaseException:
|
||||
if owns_transaction: connection.execute('ROLLBACK')
|
||||
raise
|
||||
|
||||
def append(self, *, task_id='', task_name='', project_path='', operation='',
|
||||
state='recorded', summary='', details=None, source_key=None,
|
||||
timestamp_utc=None) -> int:
|
||||
fields=(task_id,task_name,project_path,operation,state,summary)
|
||||
if not all(isinstance(value,str) for value in fields):
|
||||
raise ValueError('history fields must be strings')
|
||||
if source_key is not None and (not isinstance(source_key,str) or not source_key):
|
||||
raise ValueError('history source identity must be a nonempty string')
|
||||
if details is not None and not isinstance(details,dict):
|
||||
raise ValueError('history details must be an object')
|
||||
if timestamp_utc is None:
|
||||
instant=datetime.now(timezone.utc)
|
||||
else:
|
||||
if not isinstance(timestamp_utc,str):
|
||||
raise ValueError('history timestamp must be an ISO string with timezone')
|
||||
try:instant=datetime.fromisoformat(timestamp_utc.replace('Z','+00:00'))
|
||||
except ValueError as error:raise ValueError('invalid history timestamp') from error
|
||||
if instant.tzinfo is None or instant.utcoffset() is None:
|
||||
raise ValueError('history timestamp requires a timezone')
|
||||
timestamp=instant.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00','Z')
|
||||
details_json=json.dumps(details or {},ensure_ascii=False,allow_nan=False,sort_keys=True)
|
||||
search_text=' '.join((*fields,details_json)).casefold()
|
||||
with self.store.transaction():
|
||||
connection=self.store._connection
|
||||
if source_key is not None:
|
||||
prior=connection.execute('SELECT event_id FROM operation_history_sources WHERE source_key=?',
|
||||
(source_key,)).fetchone()
|
||||
if prior is not None: return int(prior[0])
|
||||
cursor=connection.execute('''INSERT INTO operation_history
|
||||
(timestamp_utc,task_id,task_name,project_path,operation,state,summary,details_json,search_text)
|
||||
VALUES (?,?,?,?,?,?,?,?,?)''',(timestamp,*fields,details_json,search_text))
|
||||
identifier=int(cursor.lastrowid)
|
||||
if source_key is not None:
|
||||
connection.execute('INSERT INTO operation_history_sources VALUES (?,?)',(source_key,identifier))
|
||||
return identifier
|
||||
|
||||
def page(self, *, query='', state='', before_id=None, limit=100, read_control=None) -> HistoryPage:
|
||||
if not isinstance(query,str) or len(query)>1000 or not isinstance(state,str):
|
||||
raise ValueError('invalid history search')
|
||||
if type(limit) is not int or not 1<=limit<=500:
|
||||
raise ValueError('history page limit must be 1..500')
|
||||
if before_id is not None and (type(before_id) is not int or before_id<1):
|
||||
raise ValueError('invalid history page cursor')
|
||||
conditions=['event_id>(SELECT cleared_through_id FROM operation_history_meta WHERE singleton=1)']
|
||||
parameters=[]
|
||||
if state:
|
||||
conditions.append('state=?'); parameters.append(state)
|
||||
for term in query.casefold().split():
|
||||
conditions.append("search_text LIKE ? ESCAPE '\\'")
|
||||
parameters.append('%'+term.replace('\\','\\\\').replace('%','\\%').replace('_','\\_')+'%')
|
||||
where=' AND '.join(conditions)
|
||||
with self._read(read_control) as connection:
|
||||
total=int(connection.execute('SELECT COUNT(*) FROM operation_history WHERE '+where,parameters).fetchone()[0])
|
||||
all_total=(int(connection.execute('''SELECT COUNT(*) FROM operation_history
|
||||
WHERE event_id>(SELECT cleared_through_id FROM operation_history_meta WHERE singleton=1)''').fetchone()[0])
|
||||
if query.strip() or state else total)
|
||||
checkpoint(read_control)
|
||||
if before_id is not None:
|
||||
cursor=connection.execute('SELECT timestamp_utc FROM operation_history WHERE '+where+
|
||||
' AND event_id=?',(*parameters,before_id)).fetchone()
|
||||
if cursor is None:
|
||||
raise ValueError('history cursor is no longer available; refresh latest records')
|
||||
where+=' AND (timestamp_utc<? OR (timestamp_utc=? AND event_id<?))'
|
||||
parameters.extend((cursor[0],cursor[0],before_id))
|
||||
records=connection.execute('SELECT * FROM operation_history WHERE '+where+
|
||||
' ORDER BY timestamp_utc DESC,event_id DESC LIMIT ?',(*parameters,limit+1)).fetchall()
|
||||
rows=[]
|
||||
for row in records[:limit]:
|
||||
checkpoint(read_control)
|
||||
value={key:row[key] for key in row.keys() if key not in {'details_json','search_text'}}
|
||||
value['details']=json.loads(row['details_json'])
|
||||
rows.append(value)
|
||||
next_cursor=rows[-1]['event_id'] if len(records)>limit else None
|
||||
return HistoryPage(tuple(rows),next_cursor,total,all_total)
|
||||
|
||||
def clear_snapshot(self) -> HistoryClearSnapshot:
|
||||
with self._read() as connection:
|
||||
row=connection.execute('''SELECT COALESCE(MAX(event_id),0),COUNT(*) FROM operation_history
|
||||
WHERE event_id>(SELECT cleared_through_id FROM operation_history_meta WHERE singleton=1)''').fetchone()
|
||||
return HistoryClearSnapshot(int(row[0]),int(row[1]))
|
||||
|
||||
def clear(self, cutoff_id: int) -> int:
|
||||
if type(cutoff_id) is not int or cutoff_id<0:
|
||||
raise ValueError('invalid history clear boundary')
|
||||
with self.store.transaction():
|
||||
connection=self.store._connection
|
||||
sequence=connection.execute("SELECT seq FROM sqlite_sequence WHERE name='operation_history'").fetchone()
|
||||
if cutoff_id>(int(sequence[0]) if sequence is not None else 0):
|
||||
raise ValueError('history clear boundary is in the future')
|
||||
connection.execute('''UPDATE operation_history_meta
|
||||
SET cleared_through_id=MAX(cleared_through_id,?) WHERE singleton=1''',(cutoff_id,))
|
||||
return connection.execute('DELETE FROM operation_history WHERE event_id<=?',(cutoff_id,)).rowcount
|
||||
@@ -121,6 +121,12 @@ def _stage_new_round(service, task_id, transition):
|
||||
Path(source).rename(destination)
|
||||
for member in completed:
|
||||
store.reset_completed_round(member.task_id, summary)
|
||||
from .execution_journal import ExecutionJournal
|
||||
from .history_store import HistoryRepository
|
||||
ExecutionJournal(store).start_round(member.task_id, round_id)
|
||||
HistoryRepository(store).append(task_id=member.task_id, task_name=member.name,
|
||||
operation='start_new_round', summary='原轮次已归档,已明确开始新一轮;需重新采集并确认。',
|
||||
details={'round_id': round_id}, source_key=f'round:{member.task_id}:{round_id}')
|
||||
store.record_round_commit(round_id)
|
||||
return tuple(store.get_task(t.task_id) for t in completed), journal_root
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from contextlib import contextmanager
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timezone
|
||||
from functools import lru_cache
|
||||
import inspect
|
||||
@@ -64,6 +64,7 @@ class QuickRunBatchSession:
|
||||
command_header: str
|
||||
target_command_rows: dict[str, str]
|
||||
worker_paths: tuple[str, str]
|
||||
journal_rows: dict[str, dict] = field(default_factory=dict)
|
||||
|
||||
|
||||
class _ProcessRunner(Protocol):
|
||||
@@ -103,6 +104,14 @@ class QuickRunPreparer:
|
||||
discovery_matches: dict[str, dict[tuple[str, str], frozenset[str]]] | None = None,
|
||||
discovery_failures: dict[str, str] | None = None,
|
||||
) -> QuickRunResult:
|
||||
from .handoff_guard import assert_handoff_intact
|
||||
# Preserve the public preparation errors (and diagnostic entries) for
|
||||
# known pending/malformed status before checking the complete handoff.
|
||||
existing_root = self._data_root / 'quick-run'
|
||||
_assert_no_pending_status(existing_root / 'status.tsv', existing_root / 'quick-part-replace.log')
|
||||
assert_handoff_intact(self._data_root / 'quick-run', task_id)
|
||||
if getattr(self, 'journal', None) is not None:
|
||||
self.journal.assert_can_prepare(task_id)
|
||||
selected_projects = _queue_projects(projects)
|
||||
enabled_rules = tuple(rule for rule in rules if rule.enabled)
|
||||
if not selected_projects:
|
||||
@@ -142,7 +151,9 @@ class QuickRunPreparer:
|
||||
)
|
||||
initial_status = _initial_discovery_status(
|
||||
command_path,
|
||||
discovery_matches,
|
||||
# Native completion receipts need a full semantic readback, including
|
||||
# zero-match sheets; a part-number-only inventory cannot certify it.
|
||||
None if getattr(self,'journal',None) is not None and self._process_runner is _run_process else discovery_matches,
|
||||
discovery_failures,
|
||||
)
|
||||
_io_path(status_path).write_text(
|
||||
@@ -262,6 +273,7 @@ class QuickRunPreparer:
|
||||
source = self._required_script_source()
|
||||
if source.resolve() != handoff.script_path.resolve():
|
||||
copy_worker_script(source, handoff.script_path)
|
||||
journal_rows = self._bind_journal_handoff(task_id, handoff)
|
||||
commands = _read_commands(handoff.command_path)
|
||||
command_options = _read_command_options(handoff.command_path)
|
||||
statuses = _read_status_details(handoff.status_path)
|
||||
@@ -304,8 +316,52 @@ class QuickRunPreparer:
|
||||
command_lines[0],
|
||||
target_command_rows,
|
||||
_vbs_worker_paths(handoff.script_path, handoff.script_path.parent),
|
||||
journal_rows,
|
||||
)
|
||||
|
||||
def _bind_journal_handoff(self, task_id, handoff):
|
||||
journal = getattr(self, 'journal', None)
|
||||
if journal is None:
|
||||
return {}
|
||||
from .execution_journal import read_command_rows
|
||||
rows = read_command_rows(handoff.command_path)
|
||||
details = _read_status_details(handoff.status_path)
|
||||
latest = {job: (state, code, message) for job, state, code, message in details}
|
||||
restored = journal.bind(task_id, rows, latest,
|
||||
proof_reader=lambda row,outcome:self.result_proof(handoff.command_path.parent,row,outcome))
|
||||
additional = tuple((job, *outcome) for job, outcome in restored.items() if latest.get(job) != outcome)
|
||||
if additional:
|
||||
_write_status_details(handoff.status_path, (*details, *additional))
|
||||
return {row['job_id']: row for row in rows}
|
||||
|
||||
def reconcile_terminal_receipts(self, task_id):
|
||||
journal = getattr(self, 'journal', None)
|
||||
if journal is None or not journal.needs_reconciliation(task_id):
|
||||
return
|
||||
root = self._data_root/'quick-run'
|
||||
from .handoff_guard import assert_handoff_intact
|
||||
assert_handoff_intact(root, task_id)
|
||||
try:
|
||||
self._bind_journal_handoff(task_id, QuickRunResult(root/'QuickPartReplace.vbs',
|
||||
root/'command.tsv', root/'status.tsv', root/'quick-part-replace.log', self.vbs_root_compatible))
|
||||
except TaskCenterError:
|
||||
_write_uncertain_flag(root/'uncertain.flag','uncommitted_receipt_requires_verification')
|
||||
raise
|
||||
|
||||
def result_proof(self, root, row, outcome, *, persist=True):
|
||||
from .execution_proof import capture_result
|
||||
return capture_result(self._data_root,root,row,outcome,
|
||||
strict=self._process_runner is _run_process,persist=persist)
|
||||
|
||||
def write_expected_baselines(self, task_id, root, project_path):
|
||||
journal=getattr(self,'journal',None)
|
||||
if journal is None:return
|
||||
rows=journal.expected_baselines(task_id,project_path)
|
||||
text='project_path\tschematic_name\tsheet_number\tsemantic_state\r\n'
|
||||
text+=''.join('\t'.join(_encode_vbs_field(row[key]) for key in
|
||||
('project_path','schematic_name','sheet_number','state'))+'\r\n' for row in rows)
|
||||
_write_atomic_queue_text(root/'expected-baselines.tsv',text,'quick_run_uncertain','无法发布已核验工程基线。')
|
||||
|
||||
@contextmanager
|
||||
def project_session_scope(self, *, enabled=True, runtime=None):
|
||||
"""Retain only the built-in supervised worker; injected workers stay compatible."""
|
||||
@@ -384,6 +440,7 @@ class QuickRunPreparer:
|
||||
source = self._required_script_source()
|
||||
if source.resolve() != handoff.script_path.resolve():
|
||||
copy_worker_script(source, handoff.script_path)
|
||||
journal_rows = self._bind_journal_handoff(task_id, handoff)
|
||||
commands = _read_commands(handoff.command_path)
|
||||
before = _read_status_details(handoff.status_path)
|
||||
selected_command = _next_unfinished_command(commands, before)
|
||||
@@ -403,6 +460,12 @@ class QuickRunPreparer:
|
||||
selected_command[1],
|
||||
)
|
||||
batch_request = handoff.script_path.parent / "batch.request"
|
||||
journal = getattr(self, 'journal', None)
|
||||
journal_attempt = None
|
||||
if journal is not None:
|
||||
row = (batch_session.journal_rows if batch_session is not None else journal_rows)[selected_command[1]]
|
||||
self.write_expected_baselines(task_id,handoff.command_path.parent,selected_command[2])
|
||||
journal_attempt = journal.claim(task_id, row)
|
||||
current_command_stamp = None
|
||||
if batch_mode:
|
||||
if batch_session is not None:
|
||||
@@ -465,11 +528,17 @@ class QuickRunPreparer:
|
||||
if completed.returncode == 2 and after == before:
|
||||
_append_log(handoff.log_path, "quick_run_attach_failure exit_code=2")
|
||||
detail = completed.stderr.strip() or completed.stdout.strip() or "DxDesigner attachment failed"
|
||||
if journal_attempt is not None:
|
||||
self._record_rejected_attempt(journal_attempt, handoff, selected_command, before,
|
||||
projects, detail)
|
||||
raise TaskCenterError("quick_run_session", _session_failure_message(detail))
|
||||
if completed.returncode == 3:
|
||||
if after == before:
|
||||
_append_log(handoff.log_path, "quick_run_pre_mutation_failure exit_code=3")
|
||||
detail = completed.stderr.strip() or completed.stdout.strip() or "DxDesigner pre-mutation validation failed"
|
||||
if journal_attempt is not None:
|
||||
self._record_rejected_attempt(journal_attempt, handoff, selected_command, before,
|
||||
projects, detail)
|
||||
raise TaskCenterError("quick_run_session", detail)
|
||||
_write_uncertain_flag(uncertain_path, "contradictory_pre_mutation_status")
|
||||
_append_log(handoff.log_path, "quick_run_uncertain pre_mutation_exit_status_changed exit_code=3")
|
||||
@@ -565,6 +634,16 @@ class QuickRunPreparer:
|
||||
if next_command is not None and batch_session.sheets is not None
|
||||
else (None, None)
|
||||
)
|
||||
if journal_attempt is not None:
|
||||
if progress is None:
|
||||
progress = _progress_from_queue(tuple(project for project in projects if project.selected and project.available),
|
||||
commands, after, _read_command_sheet_targets(handoff.command_path))
|
||||
try:
|
||||
proof=self.result_proof(handoff.command_path.parent,row,(state,code,message))
|
||||
journal.finish(journal_attempt, state, code, message, progress=progress,proof=proof)
|
||||
except Exception:
|
||||
_write_uncertain_flag(uncertain_path, 'receipt_commit_failed')
|
||||
raise
|
||||
return QuickRunStepResult(
|
||||
handoff.script_path,
|
||||
handoff.command_path,
|
||||
@@ -586,6 +665,18 @@ class QuickRunPreparer:
|
||||
return read_snapshot(self, task_id, projects, rules,
|
||||
copy_part_number_to_value=copy_part_number_to_value, read_control=read_control)
|
||||
|
||||
def _record_rejected_attempt(self, attempt, handoff, command, before, projects, message):
|
||||
"""Known pre-mutation exit + unchanged status is a safe failure, not an unknown write."""
|
||||
outcome=(command[1], BATCH_FAILED_STATE, 'pre_mutation_rejected', message)
|
||||
_write_status_details(handoff.status_path, (*before, outcome))
|
||||
progress=_progress_from_queue(tuple(project for project in projects if project.selected and project.available),
|
||||
_read_commands(handoff.command_path), (*before,outcome), _read_command_sheet_targets(handoff.command_path))
|
||||
try:
|
||||
self.journal.finish(attempt, *outcome[1:], progress=progress)
|
||||
except Exception:
|
||||
_write_uncertain_flag(handoff.command_path.parent/'uncertain.flag', 'rejection_receipt_commit_failed')
|
||||
raise
|
||||
|
||||
def has_interrupted(self, task_id: str) -> bool:
|
||||
return self.read_snapshot(task_id, (), ()).interrupted
|
||||
|
||||
@@ -611,6 +702,8 @@ class QuickRunPreparer:
|
||||
value_mode: str,
|
||||
) -> QuickRunResult | None:
|
||||
root = self._data_root / "quick-run"
|
||||
from .handoff_guard import assert_handoff_intact
|
||||
assert_handoff_intact(root, task_id)
|
||||
command_path = root / "command.tsv"
|
||||
status_path = root / "status.tsv"
|
||||
if not _io_path(command_path).is_file() or not _io_path(status_path).is_file():
|
||||
|
||||
@@ -16,6 +16,9 @@ def failed_native_result_uncertain(root, commands, statuses, *, files=None):
|
||||
for _, job, *_ in commands:
|
||||
if latest.get(job) != api.BATCH_FAILED_STATE:
|
||||
continue
|
||||
if any(entry[0]==job and (entry[2]==str(-2147221504+879) or 'QuickPartReplace.Baseline' in entry[3])
|
||||
for entry in statuses):
|
||||
return True
|
||||
if api._rollback_marker_verified(root, job):
|
||||
continue
|
||||
path = root / f'snapshot-{job}.tsv.native.json'
|
||||
@@ -143,6 +146,17 @@ def _read_snapshot_once(preparer, task_id, projects, rules, *, copy_part_number_
|
||||
progress = QuickRunProgress(0, len(expected), api._optional_path(first[0]), first[1], first[2], (), False)
|
||||
current = preparer._data_root / 'quick-run'
|
||||
history = preparer._data_root / 'quick-run-history'
|
||||
from .handoff_guard import assert_handoff_intact
|
||||
files.track_root(current)
|
||||
if any(internal_path(preparer.data_root/name).exists() for name in ('recovery-commit.json','retry-preparation.json')):
|
||||
progress=replace(progress,next_project_path=None,next_old_part_number=None,next_new_part_number=None)
|
||||
return QuickRunSnapshot(progress,True,current,True,'retry')
|
||||
try:
|
||||
assert_handoff_intact(current, task_id, files=files)
|
||||
except TaskCenterError:
|
||||
progress = replace(progress, next_project_path=None, next_old_part_number=None,
|
||||
next_new_part_number=None)
|
||||
return QuickRunSnapshot(progress, True, current, True, 'selection')
|
||||
root = None
|
||||
commands = ()
|
||||
# Avoid enumerating history when the active handoff already matches.
|
||||
@@ -169,11 +183,26 @@ def _read_snapshot_once(preparer, task_id, projects, rules, *, copy_part_number_
|
||||
break
|
||||
if root is None:
|
||||
files.checkpoint()
|
||||
journal = getattr(preparer, 'journal', None)
|
||||
if journal is not None and journal.has_plan(task_id):
|
||||
progress = replace(progress, next_project_path=None, next_old_part_number=None, next_new_part_number=None)
|
||||
return QuickRunSnapshot(progress, True, current, True, 'selection')
|
||||
return QuickRunSnapshot(progress, False, None, False, 'selection')
|
||||
interrupted = root == current and (internal_path(root / 'uncertain.flag')).is_file()
|
||||
phase = 'selection'
|
||||
try:
|
||||
details = api._read_status_details(files.path(root / 'status.tsv'))
|
||||
journal = getattr(preparer, 'journal', None)
|
||||
if journal is not None:
|
||||
from .execution_journal import read_command_rows
|
||||
rows = read_command_rows(root / 'command.tsv')
|
||||
outcomes = {job: (state, code, message) for job, state, code, message in details}
|
||||
interrupted = interrupted or journal.conflicts(task_id, rows, outcomes)
|
||||
if journal.needs_reconciliation(task_id):
|
||||
for row in rows:
|
||||
outcome=outcomes.get(row['job_id'])
|
||||
if outcome and outcome[0] in api.TERMINAL_STATES:
|
||||
preparer.result_proof(root,row,outcome,persist=False)
|
||||
files.checkpoint()
|
||||
if root == current:
|
||||
latest = {job_id: state for job_id, state, _, _ in details}
|
||||
@@ -198,6 +227,6 @@ def _read_snapshot_once(preparer, task_id, projects, rules, *, copy_part_number_
|
||||
if progress.queue_complete:
|
||||
phase = 'retry' if progress.failed_items else 'complete'
|
||||
except TaskCenterError:
|
||||
pass
|
||||
interrupted=True
|
||||
files.checkpoint()
|
||||
return QuickRunSnapshot(progress, True, root, interrupted, phase)
|
||||
@@ -108,6 +108,9 @@ def require_current_cell_binding(snapshot, binding, project, metadata):
|
||||
def recover(preparer, task_id, projects, rules, *, copy_part_number_to_value=False, confirmed=False, runtime=None):
|
||||
if not confirmed:
|
||||
raise TaskCenterError('quick_run_recovery_confirmation', '核验恢复需要明确确认,不能作为自动重试执行。')
|
||||
from .recovery_commit import marker_path, publish, stage
|
||||
if marker_path(preparer).is_file():
|
||||
return publish(preparer,task_id,projects,rules,copy_part_number_to_value)
|
||||
maintain_quick_run_storage(preparer._data_root)
|
||||
root = preparer._data_root / 'quick-run'
|
||||
uncertain = root / 'uncertain.flag'
|
||||
@@ -124,6 +127,13 @@ def recover(preparer, task_id, projects, rules, *, copy_part_number_to_value=Fal
|
||||
if not _value_mode_matches_option(mode, copy_part_number_to_value) or len({command[1] for command in commands}) != len(commands):
|
||||
raise TaskCenterError('quick_run_recovery', 'Value 策略变化或队列编号重复,已停止恢复。')
|
||||
selected = _recovery_command(commands, before, root=root)
|
||||
journal=getattr(preparer,'journal',None)
|
||||
if journal is not None:
|
||||
from .execution_journal import read_command_rows
|
||||
unresolved=journal.unresolved_jobs(task_id,read_command_rows(root/'command.tsv'))
|
||||
if len(unresolved)>1:
|
||||
raise TaskCenterError('quick_run_recovery','多项尝试尚未核验,请保留原证据逐项核对。')
|
||||
if unresolved:selected=next(command for command in commands if command[1]==unresolved[0])
|
||||
if selected is None:
|
||||
raise TaskCenterError('quick_run_recovery', '队列记录已完成但仍有保护锁,需要单独核对,未自动解除。')
|
||||
job_id = _safe_run_token(selected[1])
|
||||
@@ -242,42 +252,10 @@ def recover(preparer, task_id, projects, rules, *, copy_part_number_to_value=Fal
|
||||
if _recovery_hashes(root) != evidence_before or (worker / snapshot.name).read_bytes() != snapshot.read_bytes():
|
||||
raise TaskCenterError('quick_run_recovery', '核验期间原始证据变化,未提交恢复结果。')
|
||||
state = after[0][1]
|
||||
command_bytes = (root / 'command.tsv').read_bytes()
|
||||
status_bytes = (root / 'status.tsv').read_bytes()
|
||||
recovery_record = {'task_id': task_id, 'job_id': job_id, 'state': state, 'worker_result': after[0],
|
||||
'snapshot_sha256': binding['snapshot_sha256'], 'evidence_before': evidence_before,
|
||||
'project_before': project_before, 'native_replacement_called': False}
|
||||
(audit / 'recovery.json').write_text(json.dumps(recovery_record, ensure_ascii=False, indent=2), encoding='utf-8')
|
||||
try:
|
||||
if state == 'retry_ready':
|
||||
lines = (root / 'command.tsv').read_text(encoding='utf-8-sig').splitlines()
|
||||
column = lines[0].split('\t').index('job_id')
|
||||
retry_id = uuid4().hex + '_retry'
|
||||
for index in range(1, len(lines)):
|
||||
fields = lines[index].split('\t')
|
||||
if fields[column] == job_id:
|
||||
fields[column] = retry_id
|
||||
lines[index] = '\t'.join(fields)
|
||||
temporary = root / ('command-' + uuid4().hex + '.tmp')
|
||||
temporary.write_text('\n'.join(lines) + '\n', encoding='utf-8')
|
||||
os.replace(temporary, root / 'command.tsv')
|
||||
_write_status_details(root / 'status.tsv', tuple(entry for entry in before if entry[0] != job_id))
|
||||
else:
|
||||
_write_status_details(root / 'status.tsv', (*before, after[0]))
|
||||
os.replace(uncertain, audit / 'resolved-uncertain.flag')
|
||||
except Exception:
|
||||
(root / 'command.tsv').write_bytes(command_bytes)
|
||||
(root / 'status.tsv').write_bytes(status_bytes)
|
||||
if not uncertain.exists():
|
||||
uncertain.write_text('recovery commit interrupted; reconciliation required', encoding='ascii')
|
||||
raise
|
||||
progress = preparer.progress(task_id, projects, rules, copy_part_number_to_value=copy_part_number_to_value)
|
||||
message = ('磁盘工程与替换前快照一致,只读核验工程已关闭,已准备新的执行编号;尚未再次替换,请确认后执行。'
|
||||
if state == 'retry_ready' else
|
||||
'已从磁盘核验本项结果并恢复任务记录,工程已关闭;没有重复执行原生替换。'
|
||||
if after[0][2] == 'recovery_saved_verified' else
|
||||
'已核验并恢复本项任务记录;没有重复执行原生替换。若补做了属性恢复,请保存当前工程。')
|
||||
return QuickRunStepResult(root / 'QuickPartReplace.vbs', root / 'command.tsv', root / 'status.tsv',
|
||||
audit / 'worker-output.txt', state, message, progress.next_project_path,
|
||||
progress.next_old_part_number, progress.next_new_part_number,
|
||||
progress.next_schematic_name, progress.next_sheet_number)
|
||||
from .batch_resources import write_record
|
||||
write_record(audit/'recovery.json',recovery_record)
|
||||
stage(preparer,task_id,audit,job_id,before,after[0])
|
||||
return publish(preparer,task_id,projects,rules,copy_part_number_to_value)
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Durable, replayable file/SQLite handoff after an explicitly verified recovery."""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from .batch_resources import write_record
|
||||
from .errors import TaskCenterError
|
||||
from .path_access import internal_path
|
||||
|
||||
|
||||
def marker_path(preparer):
|
||||
return internal_path(preparer.data_root/'recovery-commit.json')
|
||||
|
||||
|
||||
def stage(preparer, task_id, audit, job_id, before, outcome):
|
||||
root=internal_path(preparer.data_root/'quick-run')
|
||||
lines=(root/'command.tsv').read_text(encoding='utf-8-sig').splitlines()
|
||||
if outcome[1]=='retry_ready':
|
||||
column=lines[0].split('\t').index('job_id')
|
||||
retry=uuid4().hex+'_retry'
|
||||
for index in range(1,len(lines)):
|
||||
fields=lines[index].split('\t')
|
||||
if fields[column]==job_id:
|
||||
fields[column]=retry;lines[index]='\t'.join(fields)
|
||||
record={'task_id':task_id,'audit_name':audit.name,
|
||||
'audit_sha256':hashlib.sha256((audit/'recovery.json').read_bytes()).hexdigest(),
|
||||
'command_before_sha256':hashlib.sha256((root/'command.tsv').read_bytes()).hexdigest(),
|
||||
'status_before_sha256':hashlib.sha256((root/'status.tsv').read_bytes()).hexdigest(),
|
||||
'command_after':'\n'.join(lines)+'\n',
|
||||
'statuses_after':[entry for entry in before if entry[0]!=job_id] if outcome[1]=='retry_ready' else [*before,outcome]}
|
||||
write_record(marker_path(preparer),record)
|
||||
|
||||
|
||||
def publish(preparer, task_id, projects, rules, copy_part_number_to_value):
|
||||
from .quick_run import _write_atomic_queue_text, _write_status_details, QuickRunStepResult
|
||||
marker=marker_path(preparer)
|
||||
try:
|
||||
record=json.loads(marker.read_text(encoding='utf8'))
|
||||
name=record['audit_name']
|
||||
if record['task_id']!=task_id or not name.startswith('recovery-') or len(name)!=41 or any(c not in '0123456789abcdef' for c in name[9:]):
|
||||
raise ValueError('invalid recovery commit identity')
|
||||
audit=internal_path(preparer.data_root/'quick-run-history'/name)
|
||||
if hashlib.sha256((audit/'recovery.json').read_bytes()).hexdigest()!=record['audit_sha256']:
|
||||
raise ValueError('recovery audit changed')
|
||||
recovery=json.loads((audit/'recovery.json').read_text(encoding='utf8'))
|
||||
if recovery['task_id']!=task_id or recovery['native_replacement_called'] is not False:
|
||||
raise ValueError('invalid recovery proof')
|
||||
root=preparer.data_root/'quick-run'
|
||||
# Reject edits to either original or published files during the handoff.
|
||||
from .quick_run import _encode_vbs_field
|
||||
statuses='job_id\tstate\tcode\tmessage\r\n'+''.join('\t'.join(_encode_vbs_field(v) for v in row)+'\r\n' for row in record['statuses_after'])
|
||||
for filename,after,before_key in (('command.tsv',record['command_after'],'command_before_sha256'),
|
||||
('status.tsv',statuses,'status_before_sha256')):
|
||||
actual=hashlib.sha256((root/filename).read_bytes()).hexdigest()
|
||||
if actual not in {record[before_key],hashlib.sha256(after.encode('utf8')).hexdigest()}:
|
||||
raise ValueError('recovery publication evidence changed')
|
||||
command_before=(root/'command.tsv').read_bytes()
|
||||
status_before=(root/'status.tsv').read_bytes()
|
||||
uncertain=root/'uncertain.flag'
|
||||
try:
|
||||
_write_atomic_queue_text(root/'command.tsv',record['command_after'],'quick_run_recovery','无法发布已核验恢复记录。')
|
||||
_write_status_details(root/'status.tsv',tuple(tuple(row) for row in record['statuses_after']))
|
||||
if uncertain.exists():os.replace(uncertain,audit/'resolved-uncertain.flag')
|
||||
except BaseException:
|
||||
(root/'command.tsv').write_bytes(command_before)
|
||||
(root/'status.tsv').write_bytes(status_before)
|
||||
if not uncertain.exists():uncertain.write_text('recovery commit interrupted',encoding='ascii')
|
||||
raise
|
||||
from .quick_run import _progress_from_queue, _read_commands, _read_command_sheet_targets
|
||||
progress=_progress_from_queue(tuple(p for p in projects if p.selected and p.available),
|
||||
_read_commands(root/'command.tsv'),tuple(tuple(row) for row in record['statuses_after']),
|
||||
_read_command_sheet_targets(root/'command.tsv'))
|
||||
state=recovery['state']
|
||||
message=('磁盘工程与替换前快照一致,已准备新的执行编号;尚未再次替换,请确认后执行。'
|
||||
if state=='retry_ready' else '已从磁盘核验并恢复本项记录;没有重复执行原生替换。')
|
||||
if getattr(preparer,'journal',None) is None:marker.unlink()
|
||||
return QuickRunStepResult(root/'QuickPartReplace.vbs',root/'command.tsv',root/'status.tsv',
|
||||
audit/'worker-output.txt',state,message,progress.next_project_path,progress.next_old_part_number,
|
||||
progress.next_new_part_number,progress.next_schematic_name,progress.next_sheet_number)
|
||||
except (ValueError,KeyError,TypeError) as error:
|
||||
raise TaskCenterError('quick_run_recovery','恢复提交记录与证据不一致,已停止;请保留全部记录。') from error
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Recoverable publication of a retry queue; original evidence stays authoritative."""
|
||||
from contextlib import contextmanager
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
from uuid import uuid4
|
||||
|
||||
from .batch_resources import write_record
|
||||
from .errors import TaskCenterError
|
||||
from .path_access import internal_path
|
||||
|
||||
|
||||
def marker_path(preparer):
|
||||
return internal_path(preparer.data_root/'retry-preparation.json')
|
||||
|
||||
|
||||
def recover_startup(root,store):
|
||||
from types import SimpleNamespace
|
||||
root=internal_path(root)
|
||||
markers=(root/'retry-preparation.json',*(root/'tasks').glob('*/retry-preparation.json'))
|
||||
for marker in markers:
|
||||
if marker.is_file():
|
||||
record=json.loads(marker.read_text(encoding='utf8'))
|
||||
recover_publication(SimpleNamespace(data_root=marker.parent),store,record['task_id'])
|
||||
|
||||
|
||||
def recover_publication(preparer, store, task_id):
|
||||
marker = marker_path(preparer)
|
||||
if not marker.exists(): return
|
||||
try:
|
||||
record = json.loads(marker.read_text(encoding='utf8'))
|
||||
token = record['transition_id']
|
||||
if record['task_id'] != task_id or len(token)!=32 or any(c not in '0123456789abcdef' for c in token):
|
||||
raise ValueError('invalid retry transition identity')
|
||||
root = internal_path(preparer.data_root/'quick-run')
|
||||
stage = internal_path(preparer.data_root/('.retry-stage-'+token)/'quick-run')
|
||||
archive = internal_path(preparer.data_root/'quick-run-history'/('obsolete-retry-'+token))
|
||||
committed = store._connection.execute('SELECT 1 FROM execution_transitions WHERE transition_id=?', (token,)).fetchone()
|
||||
if not committed and archive.exists():
|
||||
if root.exists():
|
||||
if stage.exists(): raise ValueError('retry staging conflict')
|
||||
stage.parent.mkdir(parents=True,exist_ok=True)
|
||||
os.replace(root, stage)
|
||||
os.replace(archive, root)
|
||||
if not root.is_dir(): raise ValueError('original retry evidence missing')
|
||||
marker.unlink()
|
||||
except (OSError, ValueError, KeyError, TypeError) as error:
|
||||
raise TaskCenterError('quick_run_uncertain', '重试队列发布未收尾,请保留原队列和暂存记录后核验。') from error
|
||||
|
||||
|
||||
@contextmanager
|
||||
def stage_publication(preparer, store, task_id):
|
||||
"""Caller owns the outer SQL transaction and runs recovery after commit/rollback."""
|
||||
token = uuid4().hex
|
||||
stage = preparer.for_data_root(preparer.data_root/('.retry-stage-'+token))
|
||||
stage.journal = preparer.journal
|
||||
current = internal_path(preparer.data_root/'quick-run')
|
||||
archive = internal_path(preparer.data_root/'quick-run-history'/('obsolete-retry-'+token))
|
||||
write_record(marker_path(preparer), {'task_id':task_id, 'transition_id':token})
|
||||
yield stage, current
|
||||
archive.parent.mkdir(parents=True, exist_ok=True)
|
||||
os.replace(current, archive)
|
||||
os.replace(internal_path(stage.data_root/'quick-run'), current)
|
||||
store._connection.execute('INSERT INTO execution_transitions VALUES (?,?)', (token,task_id))
|
||||
@@ -132,7 +132,14 @@ class TaskCenterService:
|
||||
# another window must not start a batch while startup rewrites state.
|
||||
self._runner = runner or TrialRunner(store, logging_context, probe_source_resolver=probe_source_resolver,
|
||||
**({'recover_on_startup': False} if not recover_on_startup else {}))
|
||||
if runner is None and recover_on_startup:
|
||||
from .retry_preparation import recover_startup
|
||||
recover_startup(logging_context.root,store)
|
||||
self._quick_run_preparer = quick_run_preparer or QuickRunPreparer(logging_context.root)
|
||||
from .execution_journal import ExecutionJournal
|
||||
self._quick_run_preparer.journal = ExecutionJournal(store, self._synchronize_quick_run_progress)
|
||||
from .history_recording import initialize_history_import
|
||||
initialize_history_import(store)
|
||||
self._batch_feedback = None
|
||||
self._project_facades: dict[str, TaskCenterService] = {}
|
||||
self._facade_lock = threading.RLock()
|
||||
@@ -220,15 +227,18 @@ class TaskCenterService:
|
||||
if self._quick_run_preparer.data_root == child_root:
|
||||
return self
|
||||
with self._facade_lock:
|
||||
if task_id not in self._project_facades:
|
||||
child_preparer = self._quick_run_preparer.for_data_root(child_root)
|
||||
# Initializing a new runner replays startup recovery and mutates
|
||||
# the database. A facade shares the initialized runner instead.
|
||||
child = TaskCenterService(self._store, self._logging, path_opener=self._path_opener,
|
||||
clock=self._clock, quick_run_preparer=child_preparer, runner=self._runner)
|
||||
child._supervised = self._supervised
|
||||
self._project_facades[task_id] = child
|
||||
return self._project_facades[task_id]
|
||||
existing=self._project_facades.get(task_id)
|
||||
if existing is not None:
|
||||
return existing
|
||||
# Construction can acquire the database lock (history upgrade boundary).
|
||||
# Do not hold the cache lock: a DB snapshot reader can request a facade
|
||||
# while another thread is initializing one. Only publish under the lock.
|
||||
child_preparer = self._quick_run_preparer.for_data_root(child_root)
|
||||
child = TaskCenterService(self._store, self._logging, path_opener=self._path_opener,
|
||||
clock=self._clock, quick_run_preparer=child_preparer, runner=self._runner)
|
||||
child._supervised = self._supervised
|
||||
with self._facade_lock:
|
||||
return self._project_facades.setdefault(task_id,child)
|
||||
|
||||
def _project_task_service(self, task_id: str) -> "TaskCenterService":
|
||||
return self._facade_for_task_record(self._store.get_task(task_id))
|
||||
@@ -266,9 +276,11 @@ class TaskCenterService:
|
||||
paths = (project.path,)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
self._store.sync_projects(task_id, project.discovery_root, paths)
|
||||
from .history_recording import record_operation
|
||||
record_operation(self, 'refresh_bound_project', task_id,
|
||||
lambda: self._store.sync_projects(task_id, project.discovery_root, paths))
|
||||
return self._refresh_project_context(task_id, allow_legacy=False)
|
||||
return self._idle_quick_run_operation("refresh_bound_project", task_id, refresh)
|
||||
return self._idle_quick_run_operation("refresh_bound_project", task_id, refresh, capture_history=False)
|
||||
|
||||
def _refresh_project_context(self, task_id: str, *, allow_legacy: bool, read_control=None) -> ProjectTaskContext:
|
||||
def refresh() -> ProjectTaskContext:
|
||||
@@ -293,6 +305,11 @@ class TaskCenterService:
|
||||
interrupted = snapshot.interrupted
|
||||
has_evidence = snapshot.has_evidence
|
||||
phase = snapshot.phase
|
||||
if facade._quick_run_preparer.journal.requires_retry(task_id) and not interrupted:
|
||||
phase = 'retry'
|
||||
elif (not interrupted and task.summary == 'current-sheet quick-run queue completed with retryable failures'
|
||||
and any(project.status is ProjectStatus.FAILED for project in projects)):
|
||||
phase = 'retry'
|
||||
context = ProjectTaskContext(task, bound, tuple(rules), tuple(targets), progress,
|
||||
phase, has_evidence, interrupted, legacy)
|
||||
return context
|
||||
@@ -463,16 +480,19 @@ class TaskCenterService:
|
||||
runtime=runtime, **control_options(stop_event, on_progress))
|
||||
check_cancelled(stop_event)
|
||||
metric['completed_units'] = len(rows)
|
||||
with self._store.transaction():
|
||||
if self._store.list_target_plan(task_id) != prior:
|
||||
raise TaskCenterError('target_identity', '位号计划在采集时已改变,请重新采集')
|
||||
current = self._store.record_target_inventory(task_id, rows)
|
||||
known = {row.target.key: row for row in prior}
|
||||
seeded = seed_defaults(current, rules)
|
||||
result = tuple(known.get(row.target.key, row) for row in seeded)
|
||||
check_cancelled(stop_event)
|
||||
return self._store.replace_target_plan(task_id, result)
|
||||
return self._idle_quick_run_operation("collect_target_inventory", task_id, collect)
|
||||
from .history_recording import record_operation
|
||||
def save_inventory():
|
||||
with self._store.transaction():
|
||||
if self._store.list_target_plan(task_id) != prior:
|
||||
raise TaskCenterError('target_identity', '位号计划在采集时已改变,请重新采集')
|
||||
current = self._store.record_target_inventory(task_id, rows)
|
||||
known = {row.target.key: row for row in prior}
|
||||
seeded = seed_defaults(current, rules)
|
||||
result = tuple(known.get(row.target.key, row) for row in seeded)
|
||||
check_cancelled(stop_event)
|
||||
return self._store.replace_target_plan(task_id, result)
|
||||
return record_operation(self, 'collect_target_inventory', task_id, save_inventory)
|
||||
return self._idle_quick_run_operation("collect_target_inventory", task_id, collect, capture_history=False)
|
||||
|
||||
def scan_projects(self, task_id: str, root: Path) -> tuple[ProjectRecord, ...]:
|
||||
def scan_and_sync() -> tuple[ProjectRecord, ...]:
|
||||
@@ -660,6 +680,28 @@ class TaskCenterService:
|
||||
def layout_settings_path(self) -> Path:
|
||||
return self._logging.root / "ui-layout.ini"
|
||||
|
||||
def operation_history_page(self, *, query='', state='', before_id=None, limit=100, read_control=None):
|
||||
from .history_recording import ensure_legacy_history
|
||||
from .history_store import HistoryRepository
|
||||
ensure_legacy_history(self, read_control)
|
||||
return HistoryRepository(self._store).page(query=query, state=state, before_id=before_id,
|
||||
limit=limit, read_control=read_control)
|
||||
|
||||
def operation_history_clear_snapshot(self):
|
||||
from .history_recording import ensure_legacy_history
|
||||
from .history_store import HistoryRepository
|
||||
ensure_legacy_history(self)
|
||||
return HistoryRepository(self._store).clear_snapshot()
|
||||
|
||||
def clear_operation_history(self, cutoff_id):
|
||||
from .history_recording import ensure_legacy_history
|
||||
from .history_store import HistoryRepository
|
||||
from .task_queue import assert_task_queue_idle
|
||||
assert_task_queue_idle(self._logging.root)
|
||||
with _quick_run_lease(self._logging.root):
|
||||
ensure_legacy_history(self)
|
||||
return HistoryRepository(self._store).clear(cutoff_id)
|
||||
|
||||
def can_edit_value_options(self, task_id: str) -> bool:
|
||||
task = self._store.get_task(task_id)
|
||||
return (
|
||||
@@ -676,10 +718,7 @@ class TaskCenterService:
|
||||
details={"copy_part_label_to_value": enabled})
|
||||
return task
|
||||
|
||||
from .task_queue import assert_task_queue_idle
|
||||
assert_task_queue_idle(self._logging.root)
|
||||
with _quick_run_lease(self._logging.root):
|
||||
return self._operation("set_value_policy", task_id, update)
|
||||
return self._idle_quick_run_operation("set_value_policy", task_id, update)
|
||||
|
||||
def execute_quick_run(self, task_id: str, *, expected_item: tuple[str, str, str, str] | None = None, runtime=None) -> QuickRunStepResult:
|
||||
"""Execute one replacement rule through restoration and verification."""
|
||||
@@ -797,10 +836,12 @@ class TaskCenterService:
|
||||
if delegated is not self:
|
||||
return delegated.prepare_failed_quick_run(task_id, runtime=runtime)
|
||||
def prepare_retry() -> QuickRunProgress:
|
||||
from .retry_preparation import recover_publication
|
||||
recover_publication(self._quick_run_preparer, self._store, task_id)
|
||||
task = self._store.get_task(task_id)
|
||||
if self._quick_run_preparer.has_interrupted(task_id):
|
||||
raise TaskCenterError('quick_run_uncertain', '原生替换结果尚未核验,不能归档或重试。请先核验并恢复。')
|
||||
if task.status is not TaskStatus.RUNNING:
|
||||
if task.status is not TaskStatus.RUNNING and not self._quick_run_preparer.journal.requires_retry(task_id):
|
||||
raise TaskCenterError("quick_run_task_state", "当前任务没有可重试的失败项")
|
||||
progress = self._quick_run_preparer.progress(
|
||||
task_id,
|
||||
@@ -821,28 +862,38 @@ class TaskCenterService:
|
||||
)
|
||||
if not failed_projects:
|
||||
raise TaskCenterError("quick_run_complete", "当前队列没有可重试的失败工程")
|
||||
self._quick_run_preparer.archive_current_handoff(task_id)
|
||||
for project in failed_projects:
|
||||
self._store.set_project_status(
|
||||
project.project_id,
|
||||
ProjectStatus.PENDING,
|
||||
summary="project queued for retry",
|
||||
)
|
||||
append_task_event(
|
||||
self._logging,
|
||||
level="INFO",
|
||||
task_id=task_id,
|
||||
operation="quick_run_retry_prepared",
|
||||
message="失败工程已重新加入队列",
|
||||
details={"project_ids": [project.project_id for project in failed_projects]},
|
||||
)
|
||||
self._quick_run_preparer.ensure_batch_handoff(
|
||||
task_id,
|
||||
self._store.list_projects(task_id),
|
||||
self._execution_rules(task_id),
|
||||
copy_part_number_to_value=task.copy_part_number_to_value,
|
||||
runtime=runtime,
|
||||
)
|
||||
from .batch_backup import inherit_backup_for_retry
|
||||
from .history_store import HistoryRepository
|
||||
from .quick_run import _read_sheet_manifest, _queue_projects
|
||||
from .retry_preparation import stage_publication, recover_publication
|
||||
try:
|
||||
with self._store.transaction():
|
||||
self._quick_run_preparer.journal.authorize_retry(task_id)
|
||||
with stage_publication(self._quick_run_preparer, self._store, task_id) as (staged, original):
|
||||
# Use the frozen sheet scope. Successful exact targets
|
||||
# now have new parts and must never be rediscovered as old.
|
||||
sheets = (_read_sheet_manifest(original/'sheets.tsv')
|
||||
if (original/'sheets.tsv').is_file() else None)
|
||||
for project in failed_projects:
|
||||
self._store.set_project_status(project.project_id, ProjectStatus.PENDING,
|
||||
summary='project queued for retry')
|
||||
if sheets is not None:
|
||||
eligible={_path_key(project.path) for project in
|
||||
_queue_projects(self._store.list_projects(task_id))}
|
||||
sheets={key:value for key,value in sheets.items() if key in eligible}
|
||||
handoff = staged.prepare(task_id, self._store.list_projects(task_id),
|
||||
self._execution_rules(task_id), sheet_targets=sheets,
|
||||
copy_part_number_to_value=task.copy_part_number_to_value)
|
||||
inherit_backup_for_retry(original, handoff.command_path.parent, task_id)
|
||||
staged._bind_journal_handoff(task_id, handoff)
|
||||
task = self._store.set_task_status(task_id, TaskStatus.RUNNING,
|
||||
summary='failed items prepared; successful receipts preserved')
|
||||
HistoryRepository(self._store).append(task_id=task_id,task_name=task.name,
|
||||
operation='quick_run_retry_prepared',summary='已准备失败项及暂缓项,原成功项的完成回执保留。',
|
||||
details={'project_ids':[project.project_id for project in failed_projects]})
|
||||
finally:
|
||||
# Covers the actual SQL COMMIT boundary, including failed commits.
|
||||
recover_publication(self._quick_run_preparer, self._store, task_id)
|
||||
return self._quick_run_preparer.progress(
|
||||
task_id,
|
||||
self._store.list_projects(task_id),
|
||||
@@ -903,11 +954,35 @@ class TaskCenterService:
|
||||
**control_options(stop_event, on_progress)))
|
||||
|
||||
def _pause_quick_run_queue(self, task_id: str, message: str) -> None:
|
||||
from .history_store import HistoryRepository
|
||||
self._quick_run_preparer.journal.pause(task_id)
|
||||
if self._store.get_task(task_id).status is TaskStatus.RUNNING:
|
||||
self._store.set_task_status(task_id, TaskStatus.PAUSED, summary=message)
|
||||
with self._store.transaction():
|
||||
task = self._store.set_task_status(task_id, TaskStatus.PAUSED, summary=message)
|
||||
HistoryRepository(self._store).append(task_id=task_id, task_name=task.name,
|
||||
operation='quick_run_queue_paused', state='paused', summary=message)
|
||||
append_task_event(self._logging, level='INFO', task_id=task_id,
|
||||
operation='quick_run_queue_paused', message=message)
|
||||
|
||||
def _record_safe_queue_failure(self, task_id, message):
|
||||
"""Hold this project's untouched remainder; ordinary continue must not retry it."""
|
||||
from .history_store import HistoryRepository
|
||||
with self._store.transaction():
|
||||
task=self._store.get_task(task_id)
|
||||
summary='current-sheet quick-run queue completed with retryable failures'
|
||||
if task.summary==summary:return
|
||||
for project in self._store.list_projects(task_id):
|
||||
if project.selected and project.status is not ProjectStatus.COMPLETED:
|
||||
self._store.set_project_status(project.project_id,ProjectStatus.FAILED,
|
||||
summary='quick_run_result:failed | '+message)
|
||||
if task.status in {TaskStatus.DRAFT,TaskStatus.READY,TaskStatus.PAUSED}:
|
||||
self._store.set_task_status(task_id,TaskStatus.RUNNING,summary=summary)
|
||||
elif task.status is TaskStatus.RUNNING:
|
||||
self._store.set_task_status(task_id,TaskStatus.RUNNING,summary=summary)
|
||||
HistoryRepository(self._store).append(task_id=task_id,task_name=task.name,
|
||||
operation='工程执行收尾',state='failed',summary='本工程安全失败,剩余项暂缓;继续后续工程。',
|
||||
details={'reason':message})
|
||||
|
||||
def batch_feedback(self):
|
||||
return self._batch_feedback
|
||||
|
||||
@@ -982,8 +1057,34 @@ class TaskCenterService:
|
||||
result = recover(self._quick_run_preparer, task_id, projects,
|
||||
self._execution_rules(task_id), copy_part_number_to_value=task.copy_part_number_to_value,
|
||||
confirmed=confirmed, runtime=runtime)
|
||||
from .execution_journal import read_command_rows
|
||||
import json
|
||||
handoff_root = result.command_path.parent
|
||||
audit = json.loads((result.log_path.parent / 'recovery.json').read_text(encoding='utf8'))
|
||||
before_rows = read_command_rows(result.log_path.parent / 'evidence-before' / 'command.tsv')
|
||||
recovered_row = next(row for row in before_rows if row['job_id'] == audit['job_id'])
|
||||
native_result = audit['worker_result']
|
||||
from .execution_proof import capture_recovery
|
||||
from .quick_run import _run_process
|
||||
proof=capture_recovery(self._quick_run_preparer.data_root,result.log_path.parent,
|
||||
recovered_row,tuple(native_result[1:]),strict=self._quick_run_preparer._process_runner is _run_process)
|
||||
if not self._quick_run_preparer.journal.has_plan(task_id):
|
||||
# Upgrade legacy evidence into a frozen plan before committing
|
||||
# this verified recovery, so its proof is not re-imported from
|
||||
# the former active handoff (which has no new semantic sidecar).
|
||||
self._quick_run_preparer.journal.bind(task_id,before_rows,{})
|
||||
self._quick_run_preparer.journal.reconcile_recovery(task_id, recovered_row,
|
||||
result.state, native_result[2], native_result[3], recovery_id=result.log_path.parent.name,proof=proof)
|
||||
# The former current-command refers to the completed old attempt;
|
||||
# its preserved copy remains in the recovery audit directory.
|
||||
(handoff_root / 'current-command.tsv').unlink(missing_ok=True)
|
||||
self._quick_run_preparer._bind_journal_handoff(task_id,
|
||||
QuickRunResult(result.script_path, result.command_path, result.status_path,
|
||||
handoff_root/'quick-part-replace.log', True))
|
||||
if task.status in {TaskStatus.PAUSED, TaskStatus.FAILED}:
|
||||
self._store.set_task_status(task_id, TaskStatus.READY, summary="interrupted quick-run reconciled")
|
||||
from .recovery_commit import marker_path
|
||||
marker_path(self._quick_run_preparer).unlink(missing_ok=True)
|
||||
self._synchronize_quick_run_progress(task_id)
|
||||
append_task_event(self._logging, level="INFO", task_id=task_id, operation="quick_run_recovered",
|
||||
message=result.message, details={"state": result.state, "evidence": str(result.log_path.parent)})
|
||||
@@ -1130,11 +1231,17 @@ class TaskCenterService:
|
||||
name: str,
|
||||
task_id: str | None,
|
||||
action: Callable[[], _Result],
|
||||
*,
|
||||
capture_history: bool = True,
|
||||
) -> _Result:
|
||||
from .task_queue import assert_task_queue_idle
|
||||
assert_task_queue_idle(self._logging.root)
|
||||
with _quick_run_lease(self._logging.root):
|
||||
return self._operation(name, task_id, action)
|
||||
from .history_recording import record_operation
|
||||
# External reads can capture their short save phase explicitly,
|
||||
# retaining execution admission without holding the DB lock here.
|
||||
wrapped = (lambda: record_operation(self, name, task_id, action)) if capture_history else action
|
||||
return self._operation(name, task_id, wrapped)
|
||||
|
||||
def _manual_operation(self, name: str, task_id: str, action: Callable[[], _Result]) -> _Result:
|
||||
def checked_action():
|
||||
@@ -1237,6 +1344,8 @@ class TaskCenterService:
|
||||
def _retryable_quick_run(self, task_id: str) -> bool:
|
||||
task = self._store.get_task(task_id)
|
||||
return (
|
||||
self._project_task_service(task_id)._quick_run_preparer.journal.requires_retry(task_id)
|
||||
) or (
|
||||
task.status is TaskStatus.RUNNING
|
||||
and task.summary == "current-sheet quick-run queue completed with retryable failures"
|
||||
and any(project.status is ProjectStatus.FAILED for project in self._store.list_projects(task_id))
|
||||
|
||||
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
from .errors import TaskCenterError
|
||||
from .models import ProjectStatus, TaskStatus
|
||||
|
||||
_SCHEMA_VERSION = 7
|
||||
_SCHEMA_VERSION = 8
|
||||
_V1_ATTEMPTLESS_ACTIVE_STATES = {ProjectStatus.CAPTURING_BEFORE, ProjectStatus.AWAITING_OPERATOR, ProjectStatus.CAPTURING_AFTER, ProjectStatus.VERIFYING}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class StoreSchema:
|
||||
elif int(version_row["value"]) == 3:
|
||||
self._store._create_external_request_schema()
|
||||
self._store._connection.execute("UPDATE metadata SET value = ? WHERE key = 'schema_version'", (str(_SCHEMA_VERSION),))
|
||||
elif int(version_row["value"]) in {4, 5, 6}:
|
||||
elif int(version_row["value"]) in {4, 5, 6, 7}:
|
||||
self._store._connection.execute("UPDATE metadata SET value = ? WHERE key = 'schema_version'", (str(_SCHEMA_VERSION),))
|
||||
elif int(version_row["value"]) != _SCHEMA_VERSION:
|
||||
raise TaskCenterError("schema_version", "task database schema version is unsupported")
|
||||
@@ -56,6 +56,10 @@ class StoreSchema:
|
||||
request_id TEXT PRIMARY KEY, request_hash TEXT NOT NULL, result_json TEXT NOT NULL)''')
|
||||
self._store._connection.execute('''CREATE TABLE IF NOT EXISTS assistant_task_operations (
|
||||
request_id TEXT PRIMARY KEY, request_hash TEXT NOT NULL, result_json TEXT NOT NULL)''')
|
||||
from .history_store import create_history_schema
|
||||
from .execution_journal import create_execution_schema
|
||||
create_history_schema(self._store._connection)
|
||||
create_execution_schema(self._store._connection)
|
||||
|
||||
def _create_schema_v1(self) -> None:
|
||||
statements = (
|
||||
|
||||
@@ -17,7 +17,8 @@ from .quick_run import _write_atomic_queue_text
|
||||
_QUEUE_LOCK = Lock()
|
||||
_QUEUE_OWNER: int | None = None
|
||||
_SAFE_FAILURES = frozenset({'quick_run_preflight', 'quick_run_session', 'quick_run_backup',
|
||||
'target_identity', 'empty_target_scope', 'enabled_rule_required'})
|
||||
'target_identity', 'empty_target_scope', 'enabled_rule_required',
|
||||
'quick_run_retry_required'})
|
||||
_TERMINAL = frozenset({'completed', 'failed', 'skipped'})
|
||||
|
||||
|
||||
@@ -201,6 +202,8 @@ def execute_task_queue(service, task_ids: tuple[str, ...], *, stop_event: Event,
|
||||
try:
|
||||
if child.has_interrupted_quick_run(task_id):
|
||||
raise TaskCenterError('quick_run_uncertain', '该任务有待核验步骤。')
|
||||
if hasattr(child, '_quick_run_preparer'):
|
||||
child._quick_run_preparer.reconcile_terminal_receipts(task_id)
|
||||
progress = child.quick_run_progress(task_id)
|
||||
if progress.queue_complete:
|
||||
entry = {'state': 'failed' if getattr(progress, 'failed_items', 0) else 'completed',
|
||||
@@ -222,14 +225,21 @@ def execute_task_queue(service, task_ids: tuple[str, ...], *, stop_event: Event,
|
||||
result.last_result is not None and result.last_result.state == 'batch_saved_unverified'
|
||||
):
|
||||
raise TaskCenterError('quick_run_uncertain', '保存结果尚未确认,任务队列已停止。')
|
||||
incomplete = result.paused or not result.progress.queue_complete
|
||||
entry = {'state': 'paused' if incomplete else (
|
||||
safe_failure = bool(getattr(result, 'safe_failure', False))
|
||||
incomplete = not safe_failure and (result.paused or not result.progress.queue_complete)
|
||||
entry = {'state': 'failed' if safe_failure else 'paused' if incomplete else (
|
||||
'failed' if getattr(result.progress, 'failed_items', 0) else 'completed'),
|
||||
'message': '已安全暂停。' if incomplete else '任务已收尾。'}
|
||||
'message': '本工程安全失败,剩余项暂缓,继续后续工程。' if safe_failure else
|
||||
'已安全暂停。' if incomplete else '任务已收尾。'}
|
||||
if incomplete:
|
||||
paused = True
|
||||
except TaskCenterError as error:
|
||||
safe = error.code in _SAFE_FAILURES and not child.has_interrupted_quick_run(task_id)
|
||||
if safe and hasattr(child, '_record_safe_queue_failure'):
|
||||
try:
|
||||
child._record_safe_queue_failure(task_id, error.message)
|
||||
except Exception:
|
||||
safe = False # Failure admission must itself be durable before proceeding.
|
||||
entry = {'state': 'failed' if safe else 'blocked', 'message': error.message}
|
||||
paused = not safe
|
||||
except Exception as error:
|
||||
|
||||
@@ -5,7 +5,7 @@ import json
|
||||
import codecs
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import QProcess, QProcessEnvironment, QTimer, QUrl, Signal
|
||||
from PySide6.QtCore import QProcess, QProcessEnvironment, QTimer, QUrl, Signal, Qt
|
||||
from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile, QWebEngineSettings
|
||||
from PySide6.QtWebEngineWidgets import QWebEngineView
|
||||
from PySide6.QtWidgets import QLabel, QPushButton, QVBoxLayout, QWidget
|
||||
@@ -37,6 +37,11 @@ class NativeHarnessView(QWidget):
|
||||
|
||||
def __init__(self, data_root: Path, parent=None):
|
||||
super().__init__(parent)
|
||||
# Isolate Chromium's RHI surface in this child HWND. Otherwise showing
|
||||
# WebEngine recreates the translucent task-center HWND as an opaque
|
||||
# graphics surface and Acrylic stays white after the view is hidden.
|
||||
self.setAttribute(Qt.WidgetAttribute.WA_DontCreateNativeAncestors)
|
||||
self.setAttribute(Qt.WidgetAttribute.WA_NativeWindow)
|
||||
self.setObjectName('dshNativeWorkspace')
|
||||
self.setStyleSheet('QWidget#dshNativeWorkspace {background:white;}')
|
||||
self.data_root = Path(data_root)
|
||||
|
||||
@@ -8,7 +8,8 @@ def pending_batch_tasks(tasks, group_id):
|
||||
if not group_id:
|
||||
return ()
|
||||
return tuple(sorted((task for task in tasks if task.group_id == group_id
|
||||
and task.status not in {TaskStatus.DELETED, TaskStatus.COMPLETED}),
|
||||
and task.status not in {TaskStatus.DELETED, TaskStatus.COMPLETED}
|
||||
and getattr(task, 'summary', '') != 'current-sheet quick-run queue completed with retryable failures'),
|
||||
key=lambda task: task.group_position))
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,9 @@ def icon(name: str, primary: bool = False) -> QIcon:
|
||||
|
||||
BUTTON_ICONS = {
|
||||
'task_center_navigation': 'grid', 'dsh_navigation': 'chat',
|
||||
'history_navigation': 'clock',
|
||||
'newTaskButton': 'plus', 'editTaskButton': 'sliders', 'deleteTaskButton': 'trash',
|
||||
'selectAllTasks': 'check', 'clearTaskSelection': 'close',
|
||||
'restoreTaskButton': 'refresh', 'taskRecycleBin': 'trash',
|
||||
'moveTaskUp': 'arrow-up', 'moveTaskDown': 'sort', 'executeProjectTasks': 'play',
|
||||
'addMappingButton': 'plus', 'editMappingButton': 'sliders', 'deleteMappingButton': 'trash',
|
||||
|
||||
@@ -51,6 +51,9 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
self._dsh_authorized_tasks = set()
|
||||
self.task_center_action = QAction('任务中心', self)
|
||||
self.task_center_action.triggered.connect(lambda: self.content_stack.setCurrentIndex(0))
|
||||
self.history_action = QAction('操作历史', self)
|
||||
self.history_action.setShortcut('Ctrl+Shift+H')
|
||||
self.history_action.triggered.connect(lambda: self.content_stack.setCurrentWidget(self.history_panel))
|
||||
self.dsh_action = QAction('DSH助手', self)
|
||||
self.dsh_action.setShortcut('Ctrl+Shift+D')
|
||||
self.dsh_action.triggered.connect(self._open_dsh)
|
||||
@@ -63,7 +66,8 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
brand = QLabel('DxPartReplace', objectName='applicationBrand')
|
||||
navigation_layout.addWidget(brand)
|
||||
navigation_layout.addSpacing(24)
|
||||
for name, action in (('task_center_navigation', self.task_center_action), ('dsh_navigation', self.dsh_action)):
|
||||
for name, action in (('task_center_navigation', self.task_center_action),
|
||||
('history_navigation', self.history_action), ('dsh_navigation', self.dsh_action)):
|
||||
action.setCheckable(True)
|
||||
self._navigation_actions.addAction(action)
|
||||
self.addAction(action)
|
||||
@@ -117,7 +121,6 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
self.resize(1280, 880)
|
||||
self.setStyleSheet(APP_STYLESHEET)
|
||||
self.task_list = TaskList(self.open_task)
|
||||
self.task_list.filter.currentTextChanged.connect(self._refresh_task_list)
|
||||
self.task_list.recycle_button.toggled.connect(self._refresh_task_list)
|
||||
self.task_list.new_button.clicked.connect(self._new_task)
|
||||
self.task_list.edit_button.clicked.connect(self._edit_task)
|
||||
@@ -255,11 +258,24 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
layout.addWidget(self.workspace_splitter)
|
||||
self.content_stack = QStackedWidget()
|
||||
self.content_stack.addWidget(container)
|
||||
self.content_stack.currentChanged.connect(lambda index: (
|
||||
self.task_center_action if index == 0 else self.dsh_action).setChecked(True))
|
||||
from .operation_history import OperationHistoryPage
|
||||
self.history_panel = OperationHistoryPage(self.service, self)
|
||||
self.history_panel.busy_changed.connect(self._history_activity_changed)
|
||||
self.content_stack.addWidget(self.history_panel)
|
||||
self.content_stack.currentChanged.connect(self._workspace_changed)
|
||||
self.setCentralWidget(self.content_stack)
|
||||
self._reset_layout()
|
||||
|
||||
def _workspace_changed(self, index):
|
||||
history = self.content_stack.widget(index) is self.history_panel
|
||||
(self.task_center_action if index == 0 else self.history_action if history else self.dsh_action).setChecked(True)
|
||||
self.history_panel.set_active(history)
|
||||
|
||||
def _history_activity_changed(self):
|
||||
self._update_gates()
|
||||
if self._close_pending and self.history_panel._worker is None and not self._workers and not self._closed:
|
||||
QTimer.singleShot(0, self.close)
|
||||
|
||||
def _layout_widgets(self):
|
||||
return {'workspace': self.workspace_splitter, 'detail': self.detail_splitter,
|
||||
'rules': self.mapping_table.horizontalHeader(), 'targets': self.plan_panel.table.horizontalHeader()}
|
||||
@@ -303,7 +319,7 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
if self._close_saving:
|
||||
event.ignore()
|
||||
return
|
||||
if self._workers or (self._dsh_operations and self._dsh_operations.worker):
|
||||
if self._workers or self.history_panel._worker or (self._dsh_operations and self._dsh_operations.worker):
|
||||
self._close_pending = True
|
||||
self.plan_panel.cancel_operation()
|
||||
self._pause_queue()
|
||||
@@ -347,6 +363,7 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
self._layout_settings.setValue(f'single_project/{name}', widget.saveState())
|
||||
self._layout_settings.setValue('tasks/sort', self.task_list.sort.currentData())
|
||||
self._closed = True
|
||||
self.history_panel.shutdown()
|
||||
if self._dsh_operations:
|
||||
self._dsh_operations.close()
|
||||
self._context_query.close()
|
||||
@@ -370,7 +387,18 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
if deleted else self.service.tasks(read_control=control), received, self._show_error, cancellable=True)
|
||||
|
||||
def open_task(self, task_id):
|
||||
if not task_id or (self._workers and self._batch_stop is None):
|
||||
if not task_id:
|
||||
if self._workers and self._batch_stop is None:
|
||||
return
|
||||
self._task_id = None
|
||||
self.plan_panel.bind_task(None)
|
||||
self.project_summary.populate(None)
|
||||
self.mapping_table.populate(())
|
||||
self._message = ''
|
||||
self._log_presenter.bind(None)
|
||||
self._load_task_data()
|
||||
return
|
||||
if self._workers and self._batch_stop is None:
|
||||
return
|
||||
if self._task_id != task_id:
|
||||
self.plan_panel.bind_task(None)
|
||||
@@ -428,8 +456,9 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
self._task_context_failed(error)
|
||||
|
||||
def _update_gates(self):
|
||||
busy = bool(self._workers) or self._loading
|
||||
busy = bool(self._workers) or self._loading or self.history_panel._worker is not None
|
||||
resources_pending = self._resource_status.blocked
|
||||
self.history_panel.set_mutations_enabled(not busy and not resources_pending and self._batch_stop is None)
|
||||
self.run_panel.cleanup_resources_button.setVisible(resources_pending and self._resource_status.known)
|
||||
self.run_panel.cleanup_resources_button.setEnabled(resources_pending and self._resource_status.known and not busy)
|
||||
state = execution_state(self._context, busy=busy,
|
||||
@@ -473,9 +502,12 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
self.project_summary.refresh_button.setEnabled(not busy and bool(
|
||||
self._task_id if read_failed else context and not context.legacy))
|
||||
self.task_list.list.setEnabled(not self._workers or self._batch_stop is not None)
|
||||
self.task_list.filter.setEnabled(not self._workers or self._batch_stop is not None)
|
||||
self.task_list.filter.setEnabled((not self._workers or self._batch_stop is not None)
|
||||
and not self.task_list.recycle_button.isChecked())
|
||||
self.task_list.search.setEnabled(not self._workers or self._batch_stop is not None)
|
||||
self.task_list.recycle_button.setEnabled(not busy)
|
||||
self.task_list.select_all_button.setEnabled(not busy and bool(self.task_list.list.count()))
|
||||
self.task_list.clear_selection_button.setEnabled(not busy)
|
||||
self.task_list.sort.setEnabled(not self._workers or self._batch_stop is not None)
|
||||
self.task_list.new_button.setEnabled(not busy)
|
||||
exists = bool(context)
|
||||
@@ -495,8 +527,9 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
for button in (self.task_list.up_button, self.task_list.down_button):
|
||||
button.setEnabled(grouped and not busy and not group_active
|
||||
and self.task_list.sort.currentData() == 'execution'
|
||||
and self.task_list.filter.currentData() == 'all'
|
||||
and not self.task_list.search.text().strip())
|
||||
button.setToolTip('清空搜索并切换到执行顺序后,可调整本批任务的执行位置。')
|
||||
button.setToolTip('清空搜索、选择全部任务并切换到执行顺序后,可调整本批任务的执行位置。')
|
||||
if not self._message:
|
||||
self.run_panel.operator_band.setText(state.message)
|
||||
if self._loading and not self._message:
|
||||
@@ -1028,10 +1061,21 @@ class TrialTaskCenterWindow(MaterialMainWindow):
|
||||
self.run_panel.operator_band.style().polish(self.run_panel.operator_band)
|
||||
|
||||
def _open_dsh(self):
|
||||
self._ensure_dsh_view()
|
||||
self.content_stack.setCurrentWidget(self._dsh_view)
|
||||
if self._dsh_automation is not None and self._dsh_automation.latest:
|
||||
self._dsh_automation._publish_context()
|
||||
try:
|
||||
self._ensure_dsh_view()
|
||||
self.content_stack.setCurrentWidget(self._dsh_view)
|
||||
if self._dsh_automation is not None and self._dsh_automation.latest:
|
||||
self._dsh_automation._publish_context()
|
||||
except Exception as error:
|
||||
# QAction slots do not surface exceptions visibly in a frozen Qt
|
||||
# app. Route lazy import/WebEngine failures through the operator
|
||||
# error path instead of making the click appear to do nothing.
|
||||
try:
|
||||
self.service.report_ui_error(error)
|
||||
except Exception:
|
||||
pass
|
||||
self._show_error(TaskCenterError(
|
||||
'dsh_startup', 'DSH助手启动失败,请检查发布包中的运行时和 Node.js。'))
|
||||
|
||||
def _ensure_dsh_view(self):
|
||||
if self._dsh_view is None:
|
||||
|
||||
@@ -4,6 +4,7 @@ import json
|
||||
import re
|
||||
|
||||
ERROR_TEXT = {
|
||||
"dsh_startup": "DSH助手启动失败,请检查发布包中的运行时和 Node.js。",
|
||||
'user_forced_stop': '操作已强制中断,结果待核验。',
|
||||
'application_shutdown_failed': '业务结果已保留,但应用收尾异常。',
|
||||
"batch_resources_pending": "后台资源尚未确认释放,请使用“重新清理资源”后再开始下一批。",
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
"""Searchable user operation history; never participates in execution admission."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
import json
|
||||
|
||||
from PySide6.QtCore import QSignalBlocker, QThreadPool, QTimer, Qt, Slot, Signal
|
||||
from PySide6.QtWidgets import (QAbstractItemView, QComboBox, QHBoxLayout, QHeaderView,
|
||||
QLabel, QLineEdit, QMessageBox, QPlainTextEdit, QPushButton, QSplitter,
|
||||
QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget)
|
||||
|
||||
from .icons import icon
|
||||
from .queries import LatestQuery
|
||||
from .workers import ServiceWorker
|
||||
|
||||
|
||||
STATES={'verified':'已核验完成','no_match':'未命中','failed':'失败','failed_safe':'已确认安全失败',
|
||||
'uncertain':'待核验','saved_unverified':'保存待核验','skipped':'已跳过','recorded':'已记录',
|
||||
'running':'执行中','paused':'已暂停','pending':'待执行','restored':'已恢复'}
|
||||
OPERATIONS={'quick_run_executed':'替换执行','quick_run_prepared':'准备执行',
|
||||
'quick_run_recovered':'核验恢复','quick_run_queue_paused':'暂停执行',
|
||||
'quick_run_retry_prepared':'准备失败项重试','create_project_tasks':'创建工程任务',
|
||||
'create_task':'新建任务','update_task':'编辑任务','delete_task':'归档任务',
|
||||
'create_assistant_project_tasks':'助手创建工程任务','collect_target_inventory':'采集位号计划',
|
||||
'refresh_bound_project':'重新读取工程',
|
||||
'restore_task':'恢复任务','purge_task':'删除任务记录','add_rule':'添加规则',
|
||||
'update_rule':'修改规则','delete_rule':'删除规则','move_rule':'调整规则顺序',
|
||||
'replace_target_plan':'保存位号计划','save_plan_drafts':'保存位号计划',
|
||||
'save_bom_batch':'保存 BOM 计划','start_new_round':'开始新一轮',
|
||||
'set_value_policy':'修改 Value 策略','value_policy_changed':'修改 Value 策略',
|
||||
'execution_skipped':'跳过已完成项','execution_receipt':'执行结果'}
|
||||
DETAIL_LABELS={'refdes':'位号','old_part_number':'旧料号','new_part_number':'新料号',
|
||||
'old_part':'旧料号','new_part':'新料号','schematic_name':'原理图','sheet_number':'图页',
|
||||
'target_cell':'目标 Cell','custom_value':'自定义 Value','custom_value_enabled':'启用自定义 Value',
|
||||
'sync_nc':'同步 NC','project_path':'工程','before':'修改前','after':'修改后',
|
||||
'name':'任务名称','note':'备注','enabled':'启用','summary':'说明',
|
||||
'copy_part_number_to_value':'复制料号到 Value','copy_part_label_to_value':'复制料号到 Value'}
|
||||
|
||||
|
||||
def _timestamp(value):
|
||||
try:return datetime.fromisoformat(value.replace('Z','+00:00')).astimezone().strftime('%Y-%m-%d %H:%M:%S')
|
||||
except (TypeError,ValueError,AttributeError):return str(value or '')
|
||||
|
||||
|
||||
def _details_text(details):
|
||||
def translated(value):
|
||||
if isinstance(value,dict):
|
||||
return {DETAIL_LABELS.get(key,key):translated(item) for key,item in value.items()}
|
||||
if isinstance(value,list):return [translated(item) for item in value]
|
||||
return value
|
||||
return json.dumps(translated(details),ensure_ascii=False,indent=2) if details else '此记录没有更多改动详情。'
|
||||
|
||||
|
||||
class OperationHistoryPage(QWidget):
|
||||
busy_changed = Signal()
|
||||
def __init__(self, service, parent=None):
|
||||
super().__init__(parent)
|
||||
self.setObjectName('operationHistoryPage')
|
||||
self.service=service
|
||||
self._active=False
|
||||
self._closed=False
|
||||
self._mutations_enabled=True
|
||||
self._rows=()
|
||||
self._next_cursor=None
|
||||
self._before_id=None
|
||||
self._total=0
|
||||
self._all_total=0
|
||||
self._worker=None
|
||||
self._clear_success=False
|
||||
self._query=LatestQuery(self)
|
||||
self._clear_query=LatestQuery(self)
|
||||
self._query.settled.connect(self._buttons)
|
||||
self._clear_query.settled.connect(self._buttons)
|
||||
|
||||
title=QLabel('操作历史',objectName='projectTitle')
|
||||
description=QLabel('查看工具实际记录的操作与改动。清空历史不会重置任务进度或执行结果。')
|
||||
description.setWordWrap(True)
|
||||
self.search=QLineEdit(objectName='historySearch')
|
||||
self.search.setPlaceholderText('搜索任务、工程、位号、新旧料号或改动说明')
|
||||
self.search.setClearButtonEnabled(True)
|
||||
self.search.addAction(icon('search'),QLineEdit.ActionPosition.LeadingPosition)
|
||||
self.state_filter=QComboBox(objectName='historyStateFilter')
|
||||
self.state_filter.setAccessibleName('操作结果筛选')
|
||||
self.state_filter.addItem('全部结果','')
|
||||
for state in ('verified','no_match','failed','uncertain','restored','running','skipped','recorded'):
|
||||
self.state_filter.addItem(STATES[state],state)
|
||||
self.refresh_button=QPushButton('刷新/返回最新',objectName='historyRefresh')
|
||||
self.refresh_button.setIcon(icon('refresh'))
|
||||
self.clear_button=QPushButton('清空全部历史',objectName='historyClear')
|
||||
self.clear_button.setIcon(icon('trash'))
|
||||
self.clear_button.setToolTip('只清理操作历史,不删除任务、完成回执、工程或备份。执行/恢复期间不可清空。')
|
||||
toolbar=QHBoxLayout()
|
||||
toolbar.addWidget(self.search,1);toolbar.addWidget(self.state_filter)
|
||||
toolbar.addWidget(self.refresh_button);toolbar.addWidget(self.clear_button)
|
||||
self.summary=QLabel('还未读取操作历史',objectName='historySummary')
|
||||
self.table=QTableWidget(0,5,objectName='operationHistoryTable')
|
||||
self.table.setHorizontalHeaderLabels(['时间','任务/CBB','操作','结果','改动摘要'])
|
||||
self.table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
|
||||
self.table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
|
||||
self.table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
|
||||
self.table.setAlternatingRowColors(True)
|
||||
self.table.verticalHeader().hide()
|
||||
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Interactive)
|
||||
self.table.horizontalHeader().setStretchLastSection(True)
|
||||
for column,width in enumerate((170,180,130,105,330)):
|
||||
self.table.setColumnWidth(column,width)
|
||||
self.table.setMinimumHeight(150)
|
||||
self.empty_hint=QLabel('还没有操作历史。新的操作会在完成后记录在这里。',objectName='historyEmpty')
|
||||
self.empty_hint.setWordWrap(True)
|
||||
self.empty_hint.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.empty_hint.hide()
|
||||
self.details=QPlainTextEdit(objectName='historyDetails')
|
||||
self.details.setReadOnly(True)
|
||||
self.details.setPlaceholderText('选择一条记录,查看工程、操作结果和改动详情。')
|
||||
self.details.setMinimumHeight(70)
|
||||
splitter=QSplitter(Qt.Orientation.Vertical)
|
||||
splitter.setChildrenCollapsible(False)
|
||||
splitter.addWidget(self.table);splitter.addWidget(self.details)
|
||||
splitter.setStretchFactor(0,4);splitter.setStretchFactor(1,1)
|
||||
splitter.setSizes((400,130))
|
||||
self.status=QLabel(objectName='historyStatus')
|
||||
self.status.setWordWrap(True)
|
||||
self.older_button=QPushButton('更早记录',objectName='historyOlder')
|
||||
self.older_button.setIcon(icon('clock'))
|
||||
footer=QHBoxLayout();footer.addWidget(self.status,1);footer.addWidget(self.older_button)
|
||||
layout=QVBoxLayout(self)
|
||||
layout.setContentsMargins(24,18,24,16);layout.setSpacing(12)
|
||||
layout.addWidget(title);layout.addWidget(description);layout.addLayout(toolbar)
|
||||
layout.addWidget(self.summary);layout.addWidget(self.empty_hint)
|
||||
layout.addWidget(splitter,1);layout.addLayout(footer)
|
||||
|
||||
self._search_timer=QTimer(self)
|
||||
self._search_timer.setSingleShot(True);self._search_timer.setInterval(180)
|
||||
self._search_timer.timeout.connect(self.refresh)
|
||||
self.search.textChanged.connect(self._search_changed)
|
||||
self.state_filter.currentIndexChanged.connect(self.refresh)
|
||||
self.refresh_button.clicked.connect(self.refresh)
|
||||
self.older_button.clicked.connect(self._older)
|
||||
self.clear_button.clicked.connect(self._prepare_clear)
|
||||
self.table.itemSelectionChanged.connect(self._detail_selected)
|
||||
self._timer=QTimer(self);self._timer.setInterval(3000)
|
||||
self._timer.timeout.connect(self._poll)
|
||||
self._buttons()
|
||||
|
||||
def set_active(self, active):
|
||||
if self._closed:return
|
||||
self._active=bool(active)
|
||||
if active:
|
||||
self._timer.start()
|
||||
self._read(self._before_id)
|
||||
else:
|
||||
self._timer.stop();self._search_timer.stop()
|
||||
self._query.invalidate();self._clear_query.invalidate()
|
||||
|
||||
def set_mutations_enabled(self, enabled):
|
||||
self._mutations_enabled=bool(enabled)
|
||||
self._buttons()
|
||||
|
||||
def shutdown(self):
|
||||
self._closed=True;self._active=False
|
||||
self._timer.stop();self._search_timer.stop()
|
||||
self._query.close();self._clear_query.close()
|
||||
|
||||
def _buttons(self):
|
||||
if self._closed:return
|
||||
clearing=self._worker is not None or self._clear_query.current_busy
|
||||
self.clear_button.setEnabled(self._mutations_enabled and not clearing and self._all_total>0)
|
||||
self.older_button.setEnabled(not self._query.current_busy and self._next_cursor is not None and not clearing)
|
||||
self.refresh_button.setEnabled(not clearing)
|
||||
|
||||
def _search_changed(self):
|
||||
if self._closed:return
|
||||
self._query.invalidate()
|
||||
self._search_timer.start()
|
||||
|
||||
def refresh(self):
|
||||
if not self._active or self._closed or self._worker is not None:return
|
||||
self._search_timer.stop()
|
||||
self._read(None)
|
||||
|
||||
def _poll(self):
|
||||
if (self._before_id is None and not self._query.current_busy
|
||||
and not self._search_timer.isActive() and not self._clear_query.current_busy):
|
||||
self.refresh()
|
||||
|
||||
def _older(self):
|
||||
if self._next_cursor is not None:self._read(self._next_cursor)
|
||||
|
||||
def _read(self, before_id):
|
||||
if not self._active or self._closed:return
|
||||
query=self.search.text().strip();state=self.state_filter.currentData()
|
||||
self.status.setText('正在读取操作历史…')
|
||||
def received(page):
|
||||
self._before_id=before_id
|
||||
self._next_cursor=page.next_cursor
|
||||
self._total=page.total
|
||||
self._all_total=page.all_total if getattr(page,'all_total',None) is not None else page.total
|
||||
self._render(page.rows)
|
||||
self.summary.setText(f'共 {page.total} 条符合条件的记录 · 本页 {len(page.rows)} 条')
|
||||
self.status.setText('正在查看较早记录;点击刷新可返回最新。' if before_id is not None else '最新记录')
|
||||
self.empty_hint.setText('没有符合条件的操作历史。请调整搜索或结果筛选。' if query or state else
|
||||
'还没有操作历史。新的操作会在完成后记录在这里。')
|
||||
self.empty_hint.setVisible(not page.rows)
|
||||
self._buttons()
|
||||
def failed(error):
|
||||
self.status.setText('历史读取失败,可点击刷新重试。'+str(getattr(error,'message','')))
|
||||
self._buttons()
|
||||
self._query.request(lambda control:self.service.operation_history_page(query=query,state=state,
|
||||
before_id=before_id,limit=100,read_control=control),received,failed,cancellable=True)
|
||||
self._buttons()
|
||||
|
||||
def _render(self, rows):
|
||||
current=self.table.currentRow()
|
||||
selected_id=self._rows[current]['event_id'] if 0<=current<len(self._rows) else None
|
||||
self._rows=tuple(rows)
|
||||
with QSignalBlocker(self.table):
|
||||
self.table.setRowCount(len(rows))
|
||||
selected_row=None
|
||||
for row,record in enumerate(rows):
|
||||
values=(_timestamp(record['timestamp_utc']),record['task_name'] or record['task_id'],
|
||||
OPERATIONS.get(record['operation'],record['operation']),
|
||||
STATES.get(record['state'],record['state']),record['summary'])
|
||||
for column,value in enumerate(values):
|
||||
item=QTableWidgetItem(str(value));item.setToolTip(str(value))
|
||||
self.table.setItem(row,column,item)
|
||||
if record['event_id']==selected_id:selected_row=row
|
||||
if selected_row is not None:self.table.selectRow(selected_row)
|
||||
else:self.table.clearSelection();self.table.setCurrentCell(-1,-1)
|
||||
self._detail_selected()
|
||||
|
||||
def _detail_selected(self):
|
||||
selected=self.table.selectionModel().selectedRows()
|
||||
if not selected:
|
||||
self.details.clear();return
|
||||
row=selected[0].row()
|
||||
if not 0<=row<len(self._rows):return
|
||||
record=self._rows[row]
|
||||
self.details.setPlainText('\n'.join((f"时间:{_timestamp(record['timestamp_utc'])}",
|
||||
f"任务:{record['task_name'] or record['task_id']}",f"工程:{record['project_path'] or '此操作未绑定工程'}",
|
||||
f"操作:{OPERATIONS.get(record['operation'],record['operation'])}",
|
||||
f"结果:{STATES.get(record['state'],record['state'])}",f"说明:{record['summary']}",
|
||||
'',_details_text(record['details']))))
|
||||
|
||||
def _prepare_clear(self):
|
||||
if (self._closed or not self._active or not self._mutations_enabled
|
||||
or self._worker is not None or self._clear_query.current_busy):return
|
||||
self._clear_query.request(self.service.operation_history_clear_snapshot,self._confirm_clear,
|
||||
lambda _:self.status.setText('无法读取清空范围,历史记录保留,请重试。'))
|
||||
self._buttons()
|
||||
|
||||
def _confirm_clear(self, snapshot):
|
||||
if self._closed or not self._active or not self._mutations_enabled:return
|
||||
if snapshot.count==0:
|
||||
self.status.setText('没有需要清空的历史记录。');return
|
||||
answer=QMessageBox.question(self,'清空全部历史',
|
||||
f'将清空确认前的全部 {snapshot.count} 条操作历史,包括当前筛选之外的记录。\n'
|
||||
'任务进度、完成回执、工程、备份和诊断证据都会保留;之后的新记录不会被清空。',
|
||||
QMessageBox.StandardButton.Yes|QMessageBox.StandardButton.No,QMessageBox.StandardButton.No)
|
||||
if (answer!=QMessageBox.StandardButton.Yes or self._closed or not self._active
|
||||
or not self._mutations_enabled):return
|
||||
self._clear_success=False
|
||||
self._worker=ServiceWorker(lambda:self.service.clear_operation_history(snapshot.cutoff_id))
|
||||
self._worker.signals.succeeded.connect(self._clear_succeeded)
|
||||
self._worker.signals.failed.connect(self._clear_failed)
|
||||
self._worker.signals.finished.connect(self._clear_finished)
|
||||
self.busy_changed.emit()
|
||||
self._buttons()
|
||||
QThreadPool.globalInstance().start(self._worker)
|
||||
|
||||
@Slot(object)
|
||||
def _clear_succeeded(self, count):
|
||||
self._clear_success=True
|
||||
if not self._closed:self.status.setText(f'已清空 {count} 条历史,任务执行结果保留。')
|
||||
|
||||
@Slot(object)
|
||||
def _clear_failed(self, _error):
|
||||
if not self._closed:self.status.setText('清空失败,原历史记录保留,请重试。')
|
||||
|
||||
@Slot()
|
||||
def _clear_finished(self):
|
||||
self._worker=None
|
||||
self.busy_changed.emit()
|
||||
if self._closed:return
|
||||
self._buttons()
|
||||
if self._clear_success and self._active:self.refresh()
|
||||
@@ -8,7 +8,7 @@ from typing import Iterable
|
||||
|
||||
from PySide6.QtCore import Qt, QSignalBlocker, Signal, QSize, QRect
|
||||
from PySide6.QtGui import QColor,QFont,QIcon,QPalette
|
||||
from PySide6.QtWidgets import (QComboBox,QHBoxLayout,QLabel,QLineEdit,QListWidget,
|
||||
from PySide6.QtWidgets import (QAbstractItemView,QComboBox,QHBoxLayout,QLabel,QLineEdit,QListWidget,
|
||||
QListWidgetItem,QPushButton,QVBoxLayout,QWidget,QToolButton,QMenu,
|
||||
QStyledItemDelegate,QStyleOptionViewItem,QStyle,QApplication)
|
||||
from .command_menu import CommandMenu
|
||||
@@ -62,6 +62,7 @@ class TaskList(QWidget):
|
||||
super().__init__()
|
||||
self.setObjectName("taskNavigation")
|
||||
self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True)
|
||||
self._on_open = on_open
|
||||
self._tasks = ()
|
||||
self._selected_task_id = None
|
||||
from .icons import icon
|
||||
@@ -78,14 +79,19 @@ class TaskList(QWidget):
|
||||
self.recycle_button.setCheckable(True)
|
||||
self.count = QLabel(objectName='taskCount')
|
||||
self.filter = QComboBox(objectName="taskFilter")
|
||||
self.filter.addItems(("进行中", "已完成", "已删除"))
|
||||
self.filter.setVisible(False)
|
||||
self.filter.setMaximumHeight(0)
|
||||
for label, key, picture in (('全部任务', 'all', 'grid'), ('未完成', 'unfinished', 'clock'),
|
||||
('已完成', 'completed', 'check-circle'), ('失败', 'failed', 'warning')):
|
||||
self.filter.addItem(icon(picture), label, key)
|
||||
self.filter.setAccessibleName('任务状态分类')
|
||||
self.filter.setToolTip('未完成包括待配置、待执行、执行中、已暂停和失败的任务;分类只筛选显示。')
|
||||
self.list = QListWidget(objectName="taskList")
|
||||
self.list.setItemDelegate(TaskItemDelegate(self.list))
|
||||
self.list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
|
||||
self.list.setTextElideMode(Qt.TextElideMode.ElideRight)
|
||||
self.list.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
|
||||
self.new_button = QPushButton("新建", objectName="newTaskButton")
|
||||
self.select_all_button = QPushButton('全选', objectName='selectAllTasks')
|
||||
self.clear_selection_button = QPushButton('清空选择', objectName='clearTaskSelection')
|
||||
self.edit_button = QPushButton("编辑", objectName="editTaskButton")
|
||||
self.delete_button = QPushButton("删除", objectName="deleteTaskButton")
|
||||
self.restore_button = QPushButton("恢复", objectName="restoreTaskButton")
|
||||
@@ -96,6 +102,8 @@ class TaskList(QWidget):
|
||||
self.new_button.setProperty('appearance','quiet')
|
||||
self.new_button.setText('新建任务')
|
||||
self.new_button.setToolTip('从目录创建工程任务')
|
||||
for button in (self.select_all_button, self.clear_selection_button):
|
||||
button.setProperty('appearance', 'quiet')
|
||||
self.manage_menu=CommandMenu(self)
|
||||
for button in (self.edit_button,self.restore_button,self.up_button,self.down_button):
|
||||
self.manage_menu.add_button(button)
|
||||
@@ -121,6 +129,8 @@ class TaskList(QWidget):
|
||||
sort_button.setProperty('appearance','quiet')
|
||||
heading=QHBoxLayout();heading.addWidget(QLabel('工程任务',objectName='taskSectionTitle'));heading.addStretch();heading.addWidget(self.new_button)
|
||||
batch=QHBoxLayout();batch.addWidget(self.count,1);batch.addWidget(sort_button)
|
||||
selection=QHBoxLayout();selection.setSpacing(3)
|
||||
selection.addWidget(self.select_all_button);selection.addWidget(self.clear_selection_button);selection.addStretch()
|
||||
footer=QHBoxLayout();footer.addWidget(self.recycle_button);footer.addStretch();footer.addWidget(self.manage_button)
|
||||
self.recycle_button.setProperty('appearance','quiet')
|
||||
self.queue_button.setProperty('appearance','batch')
|
||||
@@ -129,15 +139,23 @@ class TaskList(QWidget):
|
||||
layout.setSpacing(11)
|
||||
layout.addLayout(heading)
|
||||
layout.addWidget(self.search)
|
||||
layout.addWidget(self.filter)
|
||||
layout.addLayout(batch)
|
||||
layout.addLayout(selection)
|
||||
layout.addWidget(self.list, 1)
|
||||
layout.addWidget(self.queue_button)
|
||||
layout.addLayout(footer)
|
||||
def selected():
|
||||
self._selected_task_id = self.current_task_id()
|
||||
on_open(self._selected_task_id or '')
|
||||
task_id = self.current_task_id()
|
||||
if task_id != self._selected_task_id:
|
||||
self._selected_task_id = task_id
|
||||
self._on_open(task_id or '')
|
||||
self.list.itemSelectionChanged.connect(selected)
|
||||
self.list.itemSelectionChanged.connect(self._update_selection_state)
|
||||
self.select_all_button.clicked.connect(self.select_all_tasks)
|
||||
self.clear_selection_button.clicked.connect(self.clear_task_selection)
|
||||
self.search.textChanged.connect(self._render)
|
||||
self.filter.currentIndexChanged.connect(self._render)
|
||||
self.sort.currentIndexChanged.connect(self._render)
|
||||
|
||||
def current_task_id(self) -> str | None:
|
||||
@@ -154,11 +172,45 @@ class TaskList(QWidget):
|
||||
self._selected_task_id = task_id
|
||||
self._render()
|
||||
|
||||
def select_all_tasks(self) -> None:
|
||||
if not self.list.count():
|
||||
return
|
||||
self.list.selectAll()
|
||||
if self.list.currentItem() is None:
|
||||
self.list.setCurrentRow(0)
|
||||
self._update_selection_state()
|
||||
|
||||
def clear_task_selection(self) -> None:
|
||||
with QSignalBlocker(self.list):
|
||||
self.list.clearSelection()
|
||||
self.list.setCurrentItem(None)
|
||||
for row in range(self.list.count()):
|
||||
self.list.item(row).setSelected(False)
|
||||
self._selected_task_id = None
|
||||
self._on_open('')
|
||||
self._update_selection_state()
|
||||
self.view_changed.emit()
|
||||
|
||||
def _update_selection_state(self) -> None:
|
||||
selected = len(self.list.selectedItems())
|
||||
total = self.list.count()
|
||||
suffix = f' · 已选 {selected}' if selected else ''
|
||||
current = self.count.text()
|
||||
if ' · 已选 ' in current:
|
||||
current = current.split(' · 已选 ', 1)[0]
|
||||
if current:
|
||||
self.count.setText(current + suffix)
|
||||
|
||||
def _render(self):
|
||||
selected = self._selected_task_id
|
||||
query = self.search.text().strip().casefold().replace('\\', '/')
|
||||
tasks = [task for task in self._tasks if query in ' '.join(str(getattr(task, name, '') or '')
|
||||
for name in ('name', 'discovery_root', 'note')).casefold().replace('\\', '/')]
|
||||
category = self.filter.currentData()
|
||||
if not self.recycle_button.isChecked() and category != 'all':
|
||||
tasks = [task for task in tasks if
|
||||
(task.status.value not in {'completed', 'deleted'} if category == 'unfinished'
|
||||
else task.status.value == category)]
|
||||
mode = self.sort.currentData()
|
||||
if mode.startswith('name_'):
|
||||
tasks.sort(key=lambda task: tuple(int(part) if part.isdigit() else part
|
||||
@@ -190,4 +242,5 @@ class TaskList(QWidget):
|
||||
self.list.setCurrentItem(item)
|
||||
hidden = selected and self.current_task_id() is None
|
||||
self.count.setText(f'{len(tasks)} / {len(self._tasks)} 个任务' + (' · 当前任务已隐藏' if hidden else ''))
|
||||
self._update_selection_state()
|
||||
self.view_changed.emit()
|
||||
@@ -1 +1 @@
|
||||
VERSION = "1.1.4-beta.4"
|
||||
VERSION = "1.2.2"
|
||||
@@ -173,6 +173,7 @@ def test_batch_no_match_commits_only_after_owned_project_close(tmp_path):
|
||||
run_session(tmp_path, '''
|
||||
Set QP_App=FakeApp
|
||||
FakeApp.ProjectPath=Target
|
||||
Set FakeApp.ActiveView=FakeApp
|
||||
QP_CurrentJobId="batch-job": QP_ExpectedJobId="batch-job"
|
||||
QPWriteUtf8 QP_Fso.BuildPath(QP_Root,"command.tsv"),"task_id" & vbTab & "job_id" & vbTab & "project_path" & vbTab & "old_part" & vbTab & "new_part" & vbTab & "value_mode" & vbCrLf & "task" & vbTab & "batch-job" & vbTab & Target & vbTab & "OLD" & vbTab & "NEW" & vbTab & "preserve" & vbCrLf
|
||||
QP_BatchMode=True: QP_OwnedProject=True: QP_OwnedProjectPath=Target
|
||||
@@ -182,6 +183,7 @@ Require QPReadState(QP_Fso.BuildPath(QP_Root,"status.tsv"),QP_CurrentJobId)="no_
|
||||
QPFinishBatchItem
|
||||
Require FakeApp.CloseCount=1,"project not closed exactly once"
|
||||
Require FakeApp.SaveCount=0,"no-match must close without saving"
|
||||
Require QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"post-state-batch-job.tsv")),"missing semantic readback proof"
|
||||
Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"status.tsv")),"batch_closed")>0,"no completion proof"
|
||||
''')
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
from pathlib import Path
|
||||
import importlib.util
|
||||
|
||||
_MODULE = Path(__file__).resolve().parents[2] / 'packaging' / 'dsh_runtime_staging.py'
|
||||
_SPEC = importlib.util.spec_from_file_location('dsh_runtime_staging', _MODULE)
|
||||
_LOADED = importlib.util.module_from_spec(_SPEC)
|
||||
assert _SPEC and _SPEC.loader
|
||||
_SPEC.loader.exec_module(_LOADED)
|
||||
stage_node_modules = _LOADED.stage_node_modules
|
||||
|
||||
|
||||
def _package(root: Path, name: str, *, os=None, cpu=None, files=()):
|
||||
path = root / name
|
||||
path.mkdir(parents=True)
|
||||
manifest = {'name': name, 'version': '1.0.0'}
|
||||
if os is not None:
|
||||
manifest['os'] = os
|
||||
if cpu is not None:
|
||||
manifest['cpu'] = cpu
|
||||
import json
|
||||
(path / 'package.json').write_text(json.dumps(manifest), encoding='utf8')
|
||||
for relative, content in files:
|
||||
target = path / relative
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_text(content, encoding='utf8')
|
||||
|
||||
|
||||
def test_stage_dsh_runtime_keeps_windows_runtime_and_prunes_other_platforms(tmp_path):
|
||||
source = tmp_path / 'node_modules'
|
||||
(source / '@deepseek-ai' / 'dsh').mkdir(parents=True)
|
||||
(source / '@deepseek-ai' / 'dsh' / 'package.json').write_text('{}', encoding='utf8')
|
||||
_package(source, 'win-addon', os=['win32'], cpu=['x64'], files=[('index.js', 'ok')])
|
||||
_package(source, 'linux-addon', os=['linux'], files=[('index.js', 'drop')])
|
||||
_package(source, 'docs-only', files=[('index.js', 'ok'), ('README.md', 'drop'), ('source.ts', 'drop')])
|
||||
staged = stage_node_modules(source, tmp_path / 'staged')
|
||||
assert (staged / 'win-addon' / 'index.js').read_text(encoding='utf8') == 'ok'
|
||||
assert not (staged / 'linux-addon').exists()
|
||||
assert (staged / 'docs-only' / 'index.js').is_file()
|
||||
assert not (staged / 'docs-only' / 'README.md').exists()
|
||||
assert not (staged / 'docs-only' / 'source.ts').exists()
|
||||
@@ -0,0 +1,36 @@
|
||||
"""Catch missing runtime assets and mismatched version identity before release."""
|
||||
import ast
|
||||
from pathlib import Path
|
||||
import tomllib
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
def test_release_version_matches_python_and_windows_resources():
|
||||
namespace = {}
|
||||
exec((ROOT / 'src/dx_productivity/trial_task_center/version.py').read_text(), namespace)
|
||||
version = namespace['VERSION']
|
||||
assert tomllib.loads((ROOT / 'pyproject.toml').read_text())['project']['version'] == version
|
||||
info = (ROOT / 'packaging/windows-version-info.txt').read_text()
|
||||
assert f"StringStruct('ProductVersion', '{version}')" in info
|
||||
parts = tuple(int(n) for n in version.split('.')) + (0,)
|
||||
assert f'filevers={parts}' in info and f'prodvers={parts}' in info
|
||||
|
||||
|
||||
def test_release_spec_includes_all_assets_and_complete_runtime():
|
||||
tree = ast.parse((ROOT / 'packaging/DxPartReplace.spec').read_text())
|
||||
constants = {node.value for node in ast.walk(tree) if isinstance(node, ast.Constant) and isinstance(node.value, str)}
|
||||
assert 'src/dx_productivity/trial_task_center/assets' in constants
|
||||
assert not any(isinstance(node, ast.Call) and isinstance(node.func, ast.Name)
|
||||
and node.func.id == 'stage_node_modules' for node in ast.walk(tree))
|
||||
assert {'dsh-runtime/node_modules', 'dsh-runtime/task-plugin',
|
||||
'issue-bootstrap.json', 'PySide6.QtWebEngineCore',
|
||||
'PySide6.QtWebEngineWidgets'} <= constants
|
||||
analysis = next(node for node in ast.walk(tree) if isinstance(node, ast.Call)
|
||||
and isinstance(node.func, ast.Name) and node.func.id == 'Analysis')
|
||||
options = {arg.arg: ast.literal_eval(arg.value) for arg in analysis.keywords
|
||||
if arg.arg in {'hiddenimports', 'excludes'}}
|
||||
web_dependencies = {'PySide6.QtWebChannel', 'PySide6.QtQml', 'PySide6.QtQuick',
|
||||
'PySide6.QtOpenGL', 'PySide6.QtOpenGLWidgets'}
|
||||
assert web_dependencies <= set(options['hiddenimports'])
|
||||
assert web_dependencies.isdisjoint(options['excludes'])
|
||||
@@ -0,0 +1,81 @@
|
||||
from pathlib import Path
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import zipfile
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / 'tools'))
|
||||
from build_self_extracting_release import build_release
|
||||
from verify_self_extracting_release import verify
|
||||
|
||||
|
||||
def test_release_refuses_existing_output_without_deleting_it(tmp_path):
|
||||
source = tmp_path / 'app'
|
||||
source.mkdir()
|
||||
output = tmp_path / 'valuable'
|
||||
output.mkdir()
|
||||
sentinel = output / 'keep.txt'
|
||||
sentinel.write_text('user files')
|
||||
tool = tmp_path / 'tool.exe'
|
||||
tool.write_bytes(b'MZ')
|
||||
with pytest.raises((FileExistsError, ValueError)):
|
||||
build_release(source, output, version='1.2.2', seven_zip=tool, sfx_stub=tool)
|
||||
assert sentinel.read_text() == 'user files'
|
||||
|
||||
|
||||
def test_release_rejects_output_inside_source(tmp_path):
|
||||
source = tmp_path / 'app'
|
||||
source.mkdir()
|
||||
tool = tmp_path / 'tool.exe'
|
||||
tool.write_bytes(b'MZ')
|
||||
with pytest.raises(ValueError, match='inside'):
|
||||
build_release(source, source / 'out', version='1.2.2', seven_zip=tool, sfx_stub=tool)
|
||||
assert not (source / 'out').exists()
|
||||
|
||||
|
||||
def test_release_refuses_user_data_in_payload(tmp_path):
|
||||
source = tmp_path / 'app'
|
||||
(source / 'data').mkdir(parents=True)
|
||||
(source / 'data' / 'tasks.db').write_bytes(b'private')
|
||||
tool = tmp_path / 'tool.exe'
|
||||
tool.write_bytes(b'MZ')
|
||||
with pytest.raises(ValueError, match='data'):
|
||||
build_release(source, tmp_path / 'out', version='1.2.2', seven_zip=tool, sfx_stub=tool)
|
||||
assert not (tmp_path / 'out').exists()
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt' or not os.environ.get('DX_SFX_TEST_TOOLS'),
|
||||
reason='Windows and explicit official 7-Zip tools required')
|
||||
def test_actual_sfx_and_zip_extract_identical_unicode_and_empty_files(tmp_path):
|
||||
tools = Path(os.environ['DX_SFX_TEST_TOOLS']).resolve()
|
||||
source = tmp_path / 'payload'
|
||||
source.mkdir()
|
||||
(source / 'DxPartReplace.exe').write_bytes(b'sample application')
|
||||
(source / '中文 目录').mkdir()
|
||||
(source / '中文 目录' / '空.txt').write_bytes(b'')
|
||||
(source / '中文 目录' / 'file.bin').write_bytes(bytes(range(256)) * 2048)
|
||||
(source / 'licenses').mkdir()
|
||||
original_license=source/'licenses'/'7-Zip-LICENSE.txt'
|
||||
original_license.write_text('original input must remain unchanged')
|
||||
output = tmp_path / 'release'
|
||||
manifest = build_release(source, output, version='1.2.2', seven_zip=tools/'7z.exe',
|
||||
sfx_stub=tools/'7z.sfx', license_path=tools/'License.txt')
|
||||
assert original_license.read_text() == 'original input must remain unchanged'
|
||||
destination = tmp_path / '解压 结果'
|
||||
result = verify(output/'release-manifest-1.2.2.json', destination)
|
||||
assert result['passed'] and result['files'] == 4
|
||||
with zipfile.ZipFile(output/'DxPartReplace-1.2.2.zip') as archive:
|
||||
assert archive.testzip() is None
|
||||
assert {p.relative_to(destination).as_posix() for p in destination.rglob('*') if p.is_file()} == set(archive.namelist())
|
||||
assert len(archive.namelist()) == 4 == manifest['file_count']
|
||||
for name in archive.namelist():
|
||||
assert (destination/name).read_bytes() == archive.read(name)
|
||||
assert (destination/'DxPartReplace'/'中文 目录'/'file.bin').read_bytes() == bytes(range(256))*2048
|
||||
with (output/'DxPartReplace-1.2.2.exe').open('ab') as stream:
|
||||
stream.write(b'corrupt download')
|
||||
with pytest.raises(ValueError, match='Artifact checksum'):
|
||||
verify(output/'release-manifest-1.2.2.json', tmp_path/'corrupt-extraction')
|
||||
assert not (tmp_path/'corrupt-extraction').exists()
|
||||
@@ -272,7 +272,7 @@ def test_failed_commit_restores_status_and_lock(interrupted, monkeypatch):
|
||||
|
||||
|
||||
@pytest.mark.parametrize('initial', ['ready', 'paused', 'failed'])
|
||||
def test_service_recovery_updates_queue_and_database(interrupted, initial):
|
||||
def test_service_recovery_updates_queue_and_database(interrupted, initial, monkeypatch):
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.models import TaskStatus
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
@@ -295,12 +295,53 @@ def test_service_recovery_updates_queue_and_database(interrupted, initial):
|
||||
elif initial == 'failed':
|
||||
store.set_task_status(task.task_id, TaskStatus.FAILED)
|
||||
interrupted.preparer._process_runner = worker_result(interrupted)
|
||||
def no_reimport(*args,**kwargs):
|
||||
raise AssertionError('verified recovery must commit its proof directly, not reimport old active files')
|
||||
monkeypatch.setattr(interrupted.preparer,'result_proof',no_reimport)
|
||||
result = service.recover_quick_run(task.task_id, confirmed=True)
|
||||
assert result.state == 'restored_ok'
|
||||
assert store.get_task(task.task_id).status is TaskStatus.COMPLETED
|
||||
assert service.quick_run_progress(task.task_id).queue_complete
|
||||
assert not service.has_interrupted_quick_run(task.task_id)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('state', ['restored_ok', 'retry_ready'])
|
||||
@pytest.mark.parametrize('fault', ['receipt','after_binding'])
|
||||
def test_verified_recovery_replays_receipt_commit_without_a_second_worker(interrupted, monkeypatch, state, fault):
|
||||
from dx_productivity.trial_task_center.execution_journal import ExecutionJournal
|
||||
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(interrupted.root/'tasks.db')
|
||||
try:
|
||||
service=TaskCenterService(store,configure_logging(interrupted.root),quick_run_preparer=interrupted.preparer)
|
||||
task=service.create_task('Crash during recovery commit')
|
||||
service.set_copy_part_number_to_value(task.task_id,True)
|
||||
project=service.scan_projects(task.task_id,interrupted.projects[0].path.parent)[0]
|
||||
service.set_project_selected(project.project_id,True)
|
||||
service.add_rule(task.task_id,'OLD','NEW',target_cell='CELL')
|
||||
path=interrupted.handoff.command_path
|
||||
path.write_text(path.read_text().replace('task\t',task.task_id+'\t'),encoding='utf8')
|
||||
calls=[]
|
||||
native=worker_result(interrupted,state)
|
||||
def worker(*args,**kwargs):calls.append(1);return native(*args,**kwargs)
|
||||
interrupted.preparer._process_runner=worker
|
||||
original=ExecutionJournal.reconcile_recovery
|
||||
original_unlink=Path.unlink
|
||||
def crash(*args,**kwargs):raise OSError('controlled database write failure')
|
||||
def crash_after_binding(path,*args,**kwargs):
|
||||
if path.name=='recovery-commit.json':raise OSError('controlled crash after file/database binding')
|
||||
return original_unlink(path,*args,**kwargs)
|
||||
if fault=='receipt':monkeypatch.setattr(ExecutionJournal,'reconcile_recovery',crash)
|
||||
else:monkeypatch.setattr(Path,'unlink',crash_after_binding)
|
||||
with pytest.raises(Exception):service.recover_quick_run(task.task_id,confirmed=True)
|
||||
monkeypatch.setattr(ExecutionJournal,'reconcile_recovery',original)
|
||||
monkeypatch.setattr(Path,'unlink',original_unlink)
|
||||
result=service.recover_quick_run(task.task_id,confirmed=True)
|
||||
assert result.state==state and len(calls)==1
|
||||
assert not service.has_interrupted_quick_run(task.task_id)
|
||||
finally:store.close()
|
||||
|
||||
@pytest.mark.parametrize('fault',['source_changed','choice_removed','proof_missing'])
|
||||
def test_cell_catalog_drift_blocks_recovery_before_worker(interrupted,fault):
|
||||
calls=[]
|
||||
|
||||
@@ -382,7 +382,7 @@ def test_v1_database_migrates_to_attempt_metadata_schema(tmp_path: Path) -> None
|
||||
|
||||
assert migrated.get_task(task.task_id).name == "Trial"
|
||||
with sqlite3.connect(database) as connection:
|
||||
assert connection.execute("SELECT value FROM metadata WHERE key = 'schema_version'").fetchone()[0] == "7"
|
||||
assert connection.execute("SELECT value FROM metadata WHERE key = 'schema_version'").fetchone()[0] == "8"
|
||||
assert connection.execute("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'attempts'").fetchone()
|
||||
assert connection.execute("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'external_requests'").fetchone()
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ def test_migrations_preserve_target_plan(tmp_path: Path, version: int):
|
||||
store.close()
|
||||
migrated = TaskStore(path)
|
||||
assert migrated.read_task_bundle(task.task_id).targets == original
|
||||
assert migrated._connection.execute("SELECT value FROM metadata WHERE key = 'schema_version'").fetchone()[0] == '7'
|
||||
assert migrated._connection.execute("SELECT value FROM metadata WHERE key = 'schema_version'").fetchone()[0] == '8'
|
||||
|
||||
|
||||
def test_failed_migration_rolls_back_version_and_schema(tmp_path: Path, monkeypatch):
|
||||
|
||||
@@ -93,6 +93,19 @@ def test_repeated_refresh_does_not_reconfigure_composition(qtbot):
|
||||
assert len(backend.calls)==1
|
||||
|
||||
|
||||
def test_webengine_has_its_own_native_composition_parent(qtbot, tmp_path):
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtWidgets import QVBoxLayout
|
||||
from dx_productivity.trial_task_center.ui.dsh_native import NativeHarnessView
|
||||
window=QWidget();qtbot.addWidget(window)
|
||||
window.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
|
||||
view=NativeHarnessView(tmp_path,window)
|
||||
QVBoxLayout(window).addWidget(view)
|
||||
window.show()
|
||||
# The browser must compose into its child HWND, not the Acrylic HWND.
|
||||
assert view.web.nativeParentWidget() is view
|
||||
|
||||
|
||||
def test_deferred_material_refresh_never_uses_a_destroyed_window(qtbot,qapp):
|
||||
from PySide6.QtCore import QCoreApplication,QEvent
|
||||
window=QWidget() # This test owns destruction rather than pytest-qt teardown.
|
||||
|
||||
@@ -58,6 +58,15 @@ def test_direct_navigation_opens_harness_and_returns_without_losing_task(ui, mon
|
||||
assert window._dsh_view.starts == 1
|
||||
|
||||
|
||||
def test_dsh_navigation_surfaces_lazy_startup_failure(ui, monkeypatch, qtbot):
|
||||
window = ui.window
|
||||
def fail(_self):
|
||||
raise RuntimeError('controlled missing WebEngine')
|
||||
monkeypatch.setattr(type(window), '_create_dsh_view', fail)
|
||||
qtbot.mouseClick(window.dsh_navigation, Qt.MouseButton.LeftButton)
|
||||
assert 'DSH助手启动失败' in window.run_panel.operator_band.text()
|
||||
|
||||
|
||||
def test_cross_task_harness_action_never_reaches_task_service(ui, monkeypatch):
|
||||
window = ui.window
|
||||
monkeypatch.setattr(type(window), '_create_dsh_view', lambda self: HarnessView(self), raising=False)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
from PySide6.QtCore import Qt
|
||||
|
||||
from dx_productivity.trial_task_center.history_store import HistoryRepository
|
||||
|
||||
|
||||
def test_history_navigation_search_and_return_preserve_current_task(ui,qtbot,monkeypatch):
|
||||
window=ui.window;task_id=window._task_id
|
||||
def no_dsh(_self):raise AssertionError('History must not launch DSH')
|
||||
monkeypatch.setattr(type(window),'_create_dsh_view',no_dsh)
|
||||
HistoryRepository(ui.store).append(task_id=task_id,task_name='Board-A',project_path='C:/CBB/A.prj',
|
||||
operation='器件替换',state='verified',summary='HISTORY-PROBE R1 OLD → NEW',details={'refdes':'R1','old_part':'OLD','new_part':'NEW'})
|
||||
qtbot.mouseClick(window.history_navigation,Qt.MouseButton.LeftButton)
|
||||
panel=window.history_panel
|
||||
qtbot.waitUntil(lambda:not panel._query.busy)
|
||||
assert window.content_stack.currentWidget() is panel
|
||||
assert window.history_action.isChecked() and not window.dsh_action.isChecked()
|
||||
assert panel.table.rowCount()>=1
|
||||
# Initial saved-plan history may also contain R1; isolate this known result.
|
||||
panel.search.setText('HISTORY-PROBE')
|
||||
qtbot.waitUntil(lambda:not panel._search_timer.isActive() and not panel._query.busy)
|
||||
assert panel.table.rowCount()==1
|
||||
panel.table.selectRow(0)
|
||||
assert 'OLD' in panel.details.toPlainText() and 'NEW' in panel.details.toPlainText()
|
||||
qtbot.mouseClick(window.task_center_navigation,Qt.MouseButton.LeftButton)
|
||||
assert window.content_stack.currentIndex()==0 and window._task_id==task_id
|
||||
assert window.task_center_action.isChecked() and not window.history_action.isChecked()
|
||||
|
||||
|
||||
def test_history_service_records_real_configuration_changes_and_ignores_reads(ui):
|
||||
task=ui.tasks[0]
|
||||
before=ui.service.operation_history_page().total
|
||||
for _ in range(3):ui.service.tasks();ui.service.task_context(task.task_id)
|
||||
assert ui.service.operation_history_page().total==before
|
||||
ui.service.update_task(task.task_id,name='History Test Board',note='changed note')
|
||||
page=ui.service.operation_history_page(query='History Test Board')
|
||||
assert page.rows and 'changed note' in str(page.rows[0]['details'])
|
||||
|
||||
|
||||
def test_history_clear_is_disabled_while_execution_is_active(ui,qtbot):
|
||||
window=ui.window
|
||||
window.history_action.trigger()
|
||||
qtbot.waitUntil(lambda:not window.history_panel._query.busy)
|
||||
window._begin_run(ui.tasks[0].task_id)
|
||||
window._update_gates()
|
||||
try:assert not window.history_panel.clear_button.isEnabled()
|
||||
finally:window._finish_run();window._update_gates()
|
||||
@@ -0,0 +1,167 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
|
||||
from dx_productivity.trial_task_center.history_store import HistoryRepository, create_history_schema
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.ui.operation_history import OperationHistoryPage
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def history_page(qtbot,tmp_path):
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
with store.transaction():create_history_schema(store._connection)
|
||||
repository=HistoryRepository(store)
|
||||
calls=[]
|
||||
class Service:
|
||||
def operation_history_page(self,**values):
|
||||
calls.append(values)
|
||||
return repository.page(**values)
|
||||
def operation_history_clear_snapshot(self):return repository.clear_snapshot()
|
||||
def clear_operation_history(self,cutoff_id):return repository.clear(cutoff_id)
|
||||
service=Service()
|
||||
page=OperationHistoryPage(service)
|
||||
qtbot.addWidget(page)
|
||||
page.resize(980,600);page.show()
|
||||
def append(**values):
|
||||
base=dict(task_id='task',task_name='电源 CBB',project_path='F:/Board/board.prj',
|
||||
operation='quick_run_executed',state='verified',summary='R101 OLD → NEW',
|
||||
details={'refdes':'R101','old_part_number':'OLD','new_part_number':'NEW'})
|
||||
base.update(values)
|
||||
return repository.append(**base)
|
||||
def settle():
|
||||
qtbot.waitUntil(lambda:not page._query.busy and not page._clear_query.busy and page._worker is None,timeout=5000)
|
||||
yield SimpleNamespace(page=page,append=append,repository=repository,service=service,calls=calls,settle=settle)
|
||||
page.shutdown();settle();page.close();store.close()
|
||||
|
||||
|
||||
def test_history_page_loads_on_navigation_and_shows_recorded_changes(history_page,qtbot):
|
||||
fixture=history_page
|
||||
fixture.append()
|
||||
assert not fixture.calls
|
||||
fixture.page.set_active(True);fixture.settle()
|
||||
assert fixture.page.table.rowCount()==1
|
||||
fixture.page.table.selectRow(0)
|
||||
assert 'R101' in fixture.page.details.toPlainText()
|
||||
assert 'OLD' in fixture.page.details.toPlainText()
|
||||
assert 'NEW' in fixture.page.details.toPlainText()
|
||||
assert not fixture.page.empty_hint.isVisible()
|
||||
assert fixture.page.clear_button.isEnabled()
|
||||
|
||||
|
||||
def test_search_and_state_filter_use_current_query(history_page,qtbot):
|
||||
fixture=history_page
|
||||
fixture.append()
|
||||
fixture.append(task_name='失败板',state='failed',summary='U2 缺少目标',details={'refdes':'U2'})
|
||||
fixture.page.set_active(True);fixture.settle()
|
||||
fixture.page.search.setText('U2')
|
||||
qtbot.wait(250);fixture.settle()
|
||||
assert fixture.page.table.rowCount()==1
|
||||
assert fixture.page.table.item(0,1).text()=='失败板'
|
||||
fixture.page.state_filter.setCurrentIndex(fixture.page.state_filter.findData('verified'))
|
||||
fixture.settle()
|
||||
assert fixture.page.table.rowCount()==0
|
||||
assert fixture.page.empty_hint.isVisible()
|
||||
assert '没有' in fixture.page.empty_hint.text()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('accepted',[True,False])
|
||||
def test_clear_binds_confirmation_cutoff_and_keeps_later_history(history_page,monkeypatch,accepted):
|
||||
fixture=history_page
|
||||
old=fixture.append(source_key='old')
|
||||
fixture.page.set_active(True);fixture.settle()
|
||||
seen=[]
|
||||
def choose(parent,title,text,buttons,default):
|
||||
seen.append((text,default))
|
||||
fixture.append(summary='确认期间新增',source_key='new')
|
||||
return QMessageBox.StandardButton.Yes if accepted else QMessageBox.StandardButton.No
|
||||
monkeypatch.setattr(QMessageBox,'question',choose)
|
||||
fixture.page.clear_button.click();fixture.settle()
|
||||
assert seen and '1' in seen[0][0]
|
||||
assert seen[0][1]==QMessageBox.StandardButton.No
|
||||
records=fixture.repository.page().rows
|
||||
assert len(records)==(1 if accepted else 2)
|
||||
assert records[0]['summary']=='确认期间新增'
|
||||
if accepted:assert all(row['event_id']!=old for row in records)
|
||||
|
||||
|
||||
def test_active_execution_disables_clear_without_blocking_search(history_page):
|
||||
fixture=history_page
|
||||
fixture.append();fixture.page.set_active(True);fixture.settle()
|
||||
fixture.page.set_mutations_enabled(False)
|
||||
assert not fixture.page.clear_button.isEnabled()
|
||||
assert fixture.page.search.isEnabled()
|
||||
fixture.page.set_mutations_enabled(True)
|
||||
assert fixture.page.clear_button.isEnabled()
|
||||
|
||||
|
||||
def test_read_failure_keeps_existing_results_and_refresh_can_retry(history_page,monkeypatch):
|
||||
fixture=history_page
|
||||
fixture.append();fixture.page.set_active(True);fixture.settle()
|
||||
real=fixture.service.operation_history_page
|
||||
def fail(**_):raise OSError('controlled read failure')
|
||||
monkeypatch.setattr(fixture.service,'operation_history_page',fail)
|
||||
fixture.page.refresh_button.click();fixture.settle()
|
||||
assert fixture.page.table.rowCount()==1
|
||||
assert '读取失败' in fixture.page.status.text()
|
||||
monkeypatch.setattr(fixture.service,'operation_history_page',real)
|
||||
fixture.page.refresh_button.click();fixture.settle()
|
||||
assert fixture.page.table.rowCount()==1
|
||||
assert '读取失败' not in fixture.page.status.text()
|
||||
|
||||
|
||||
def test_clear_failure_keeps_history_visible(history_page,monkeypatch):
|
||||
fixture=history_page
|
||||
fixture.append();fixture.page.set_active(True);fixture.settle()
|
||||
monkeypatch.setattr(QMessageBox,'question',lambda *args:QMessageBox.StandardButton.Yes)
|
||||
def fail(_):raise OSError('controlled commit failure')
|
||||
monkeypatch.setattr(fixture.service,'clear_operation_history',fail)
|
||||
fixture.page.clear_button.click();fixture.settle()
|
||||
assert fixture.page.table.rowCount()==1
|
||||
assert fixture.repository.page().total==1
|
||||
assert '清空失败' in fixture.page.status.text()
|
||||
|
||||
|
||||
def test_shutdown_does_not_publish_late_results(history_page):
|
||||
fixture=history_page
|
||||
fixture.append();fixture.page.set_active(True)
|
||||
fixture.page.shutdown();fixture.settle()
|
||||
assert fixture.page.table.rowCount()==0
|
||||
|
||||
|
||||
def test_older_page_survives_navigation_without_loading_unbounded_rows(history_page):
|
||||
fixture=history_page
|
||||
for index in range(103):fixture.append(summary=f'操作 {index}')
|
||||
fixture.page.set_active(True);fixture.settle()
|
||||
assert fixture.page.table.rowCount()==100
|
||||
fixture.page.older_button.click();fixture.settle()
|
||||
assert fixture.page.table.rowCount()==3
|
||||
assert fixture.page.table.item(0,4).text()=='操作 2'
|
||||
fixture.page.set_active(False)
|
||||
fixture.page.set_active(True);fixture.settle()
|
||||
assert fixture.page.table.rowCount()==3
|
||||
assert not fixture.page.older_button.isEnabled()
|
||||
fixture.page.refresh_button.click();fixture.settle()
|
||||
assert fixture.page.table.rowCount()==100
|
||||
|
||||
|
||||
def test_execution_start_during_confirmation_cancels_clear(history_page,monkeypatch):
|
||||
fixture=history_page
|
||||
fixture.append();fixture.page.set_active(True);fixture.settle()
|
||||
def choose(*args):
|
||||
fixture.page.set_mutations_enabled(False)
|
||||
return QMessageBox.StandardButton.Yes
|
||||
monkeypatch.setattr(QMessageBox,'question',choose)
|
||||
fixture.page.clear_button.click();fixture.settle()
|
||||
assert fixture.repository.page().total==1
|
||||
assert fixture.page._worker is None
|
||||
|
||||
|
||||
def test_clear_all_stays_available_when_current_filter_matches_nothing(history_page,qtbot):
|
||||
fixture=history_page
|
||||
fixture.append();fixture.page.set_active(True);fixture.settle()
|
||||
fixture.page.search.setText('not-found')
|
||||
qtbot.wait(250);fixture.settle()
|
||||
assert fixture.page.table.rowCount()==0
|
||||
assert fixture.page.clear_button.isEnabled()
|
||||
@@ -0,0 +1,91 @@
|
||||
from dataclasses import replace
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from PySide6.QtCore import Qt
|
||||
|
||||
from dx_productivity.trial_task_center.models import TaskRecord, TaskStatus
|
||||
from dx_productivity.trial_task_center.ui.task_list import TaskList
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def navigation(qtbot):
|
||||
opened=[]
|
||||
panel=TaskList(opened.append)
|
||||
qtbot.addWidget(panel)
|
||||
now=datetime.now(timezone.utc)
|
||||
tasks=tuple(TaskRecord(status.value, 'Board '+status.value, 'audio', Path('F:/Projects'),
|
||||
status, now, now, group_id='batch', group_position=index)
|
||||
for index,status in enumerate(TaskStatus) if status is not TaskStatus.DELETED)
|
||||
panel.populate(tasks,selected_task_id='ready')
|
||||
panel.show()
|
||||
return panel,tasks,opened
|
||||
|
||||
|
||||
def visible_ids(panel):
|
||||
return [panel.list.item(index).data(Qt.ItemDataRole.UserRole) for index in range(panel.list.count())]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('category','expected'),[
|
||||
('all',['draft','ready','running','paused','completed','failed']),
|
||||
('unfinished',['draft','ready','running','paused','failed']),
|
||||
('completed',['completed']),
|
||||
('failed',['failed']),
|
||||
])
|
||||
def test_visible_status_category_filters_task_list(navigation,category,expected):
|
||||
panel,tasks,opened=navigation
|
||||
assert panel.filter.isVisible()
|
||||
index=panel.filter.findData(category)
|
||||
assert index>=0
|
||||
panel.filter.setCurrentIndex(index)
|
||||
assert visible_ids(panel)==expected
|
||||
assert panel._tasks==tasks
|
||||
assert opened==[]
|
||||
|
||||
|
||||
def test_category_and_search_combine_and_refresh_tracks_new_status(navigation):
|
||||
panel,tasks,opened=navigation
|
||||
panel.filter.setCurrentIndex(panel.filter.findData('unfinished'))
|
||||
panel.search.setText('Board ready')
|
||||
assert visible_ids(panel)==['ready']
|
||||
updated=tuple(replace(task,status=TaskStatus.COMPLETED) if task.task_id=='ready' else task for task in tasks)
|
||||
panel.populate(updated,selected_task_id='ready')
|
||||
assert visible_ids(panel)==[]
|
||||
assert '当前任务已隐藏' in panel.count.text()
|
||||
panel.filter.setCurrentIndex(panel.filter.findData('completed'))
|
||||
assert visible_ids(panel)==['ready']
|
||||
assert panel.current_task_id()=='ready'
|
||||
assert opened==[]
|
||||
|
||||
|
||||
def test_category_is_local_and_never_changes_batch_scope(ui,monkeypatch):
|
||||
window=ui.window
|
||||
before=window._tasks
|
||||
def unexpected_read(**kwargs):
|
||||
raise AssertionError('Category changes must use cached tasks')
|
||||
monkeypatch.setattr(ui.service,'tasks',unexpected_read)
|
||||
panel=window.task_list
|
||||
panel.filter.setCurrentIndex(panel.filter.findData('completed'))
|
||||
assert panel.list.count()==0
|
||||
assert window._tasks==before
|
||||
assert window._task_id==ui.tasks[0].task_id
|
||||
assert not panel.up_button.isEnabled()
|
||||
assert not panel.down_button.isEnabled()
|
||||
panel.filter.setCurrentIndex(panel.filter.findData('all'))
|
||||
assert panel.list.count()==len(before)
|
||||
|
||||
|
||||
def test_recycle_bin_ignores_active_status_category(ui):
|
||||
panel=ui.window.task_list
|
||||
panel.filter.setCurrentIndex(panel.filter.findData('completed'))
|
||||
ui.service.delete_task(ui.tasks[1].task_id)
|
||||
panel.recycle_button.setChecked(True)
|
||||
ui.settle()
|
||||
assert not panel.filter.isEnabled()
|
||||
assert visible_ids(panel)==[ui.tasks[1].task_id]
|
||||
panel.recycle_button.setChecked(False)
|
||||
ui.settle()
|
||||
assert panel.filter.isEnabled()
|
||||
assert panel.filter.currentData()=='completed'
|
||||
assert visible_ids(panel)==[]
|
||||
@@ -11,7 +11,7 @@ def test_shell_has_stable_automation_controls_and_current_navigation(ui):
|
||||
'boundProjectPath','exportBomTemplate','importBomPlan','toggleLogButton'):
|
||||
assert w.findChild(QWidget,name) is not None,name
|
||||
assert w.windowTitle()==f'DxDesigner 器件替换 · {VERSION}'
|
||||
assert not w.task_list.filter.isVisible()
|
||||
assert w.task_list.filter.isVisible()
|
||||
assert w.workspace_splitter.orientation()==Qt.Orientation.Horizontal
|
||||
assert not w.workspace_splitter.childrenCollapsible()
|
||||
assert w.detail_splitter.count()==2
|
||||
|
||||
@@ -31,6 +31,27 @@ def test_task_management_is_a_menu_and_new_task_is_at_the_top(ui):
|
||||
assert panel.list.itemDelegate().__class__.__name__=='TaskItemDelegate'
|
||||
|
||||
|
||||
def test_task_list_has_explicit_select_all_clear_and_new_task_controls(ui):
|
||||
panel=ui.window.task_list
|
||||
assert panel.list.selectionMode().name=='ExtendedSelection'
|
||||
assert panel.select_all_button.isVisible()
|
||||
assert panel.clear_selection_button.isVisible()
|
||||
assert panel.new_button.isVisible()
|
||||
assert panel.select_all_button.isEnabled()
|
||||
assert panel.clear_selection_button.isEnabled()
|
||||
assert panel.list.count()>1
|
||||
|
||||
panel.select_all_button.click()
|
||||
assert len(panel.list.selectedItems())==panel.list.count()
|
||||
assert '已选' in panel.count.text()
|
||||
|
||||
panel.clear_selection_button.click()
|
||||
assert panel.list.selectedItems()==[]
|
||||
assert panel.current_task_id() is None
|
||||
assert ui.window._task_id is None
|
||||
assert panel.count.text().endswith('个任务')
|
||||
|
||||
|
||||
def test_plan_search_shares_toolbar_and_save_is_below_table(ui):
|
||||
window=ui.window;window.mapping_tabs.setCurrentIndex(1)
|
||||
plan=window.plan_panel
|
||||
|
||||
@@ -41,6 +41,77 @@ End Sub
|
||||
'''
|
||||
|
||||
|
||||
def test_resume_semantic_guard_rejects_external_sheet_changes(tmp_path):
|
||||
output=run_vbs(tmp_path,['QPRequireExpectedState'],'''
|
||||
On Error Resume Next
|
||||
QPRequireExpectedState "S" & vbTab & "full_sheet_v1" & vbCrLf & "R1=NEW", _
|
||||
"S" & vbTab & "full_sheet_v1" & vbCrLf & "R1=OLD"
|
||||
If Err.Number = 0 Then WScript.Quit 8
|
||||
Err.Clear
|
||||
QPRequireExpectedState "same-state", "same-state"
|
||||
If Err.Number <> 0 Then WScript.Quit 9
|
||||
WScript.Echo "blocked external change"
|
||||
''')
|
||||
assert output=='blocked external change'
|
||||
|
||||
|
||||
def test_baseline_read_failure_is_always_an_uncertain_result(tmp_path):
|
||||
output=run_vbs(tmp_path,['QPVerifyExpectedBaselines'],'''
|
||||
On Error Resume Next
|
||||
QPVerifyExpectedBaselines "board.prj"
|
||||
If Err.Number <> vbObjectError + 879 Or Err.Source <> "QuickPartReplace.Baseline" Then WScript.Quit 8
|
||||
WScript.Echo "requires reconciliation"
|
||||
''',prelude='''
|
||||
Sub QPCheckExpectedBaselines(ByVal TargetPath)
|
||||
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "recorded sheet was removed"
|
||||
End Sub
|
||||
''')
|
||||
assert output=='requires reconciliation'
|
||||
|
||||
|
||||
def test_resume_semantic_state_ignores_component_enumeration_order(tmp_path):
|
||||
output=run_vbs(tmp_path,['QPCurrentSemanticState','QPSortSnapshotLines'],'''
|
||||
Dim first, second
|
||||
Set QP_App = New FakeApp
|
||||
QP_App.Reverse = False
|
||||
first = QPCurrentSemanticState()
|
||||
QP_App.Reverse = True
|
||||
second = QPCurrentSemanticState()
|
||||
If first <> second Or InStr(first, "R1=NEW") = 0 Then WScript.Quit 8
|
||||
WScript.Echo "stable semantic state"
|
||||
''',prelude='''
|
||||
Const VDM_COMP = 128
|
||||
Const VD_ALL = 0
|
||||
Dim QP_App
|
||||
Class FakeApp
|
||||
Public Reverse
|
||||
Public Property Get ActiveView()
|
||||
Set ActiveView = Me
|
||||
End Property
|
||||
Public Function Query(kind, scope)
|
||||
Dim rows
|
||||
Set rows = CreateObject("Scripting.Dictionary")
|
||||
If Reverse Then
|
||||
rows.Add "R2=OLD" & vbCrLf, True
|
||||
rows.Add "R1=NEW" & vbCrLf, True
|
||||
Else
|
||||
rows.Add "R1=NEW" & vbCrLf, True
|
||||
rows.Add "R2=OLD" & vbCrLf, True
|
||||
End If
|
||||
Set Query = rows
|
||||
End Function
|
||||
End Class
|
||||
Sub QPInvalidatePinIndex()
|
||||
End Sub
|
||||
Sub QPInvalidateComponentIndex()
|
||||
End Sub
|
||||
Function QPUntouchedFullState(ByRef component)
|
||||
QPUntouchedFullState = component
|
||||
End Function
|
||||
''')
|
||||
assert output=='stable semantic state'
|
||||
|
||||
|
||||
def test_attribute_com_failure_propagates_instead_of_missing(tmp_path):
|
||||
output = run_vbs(tmp_path, ['QPFindCurrentAttribute'], '''
|
||||
Dim Comp, Attr
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
from copy import deepcopy
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.execution_journal import ExecutionJournal, create_execution_schema
|
||||
from dx_productivity.trial_task_center.history_store import HistoryRepository, create_history_schema
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def journal_case(tmp_path):
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
with store.transaction():
|
||||
create_execution_schema(store._connection);create_history_schema(store._connection)
|
||||
task=store.create_task('Test Board')
|
||||
journal=ExecutionJournal(store)
|
||||
row={'task_id':task.task_id,'job_id':'first-job','project_path':str(tmp_path/'board.prj'),
|
||||
'schematic_name':'MAIN','sheet_number':'1','old_part':'OLD','new_part':'NEW',
|
||||
'target_scope':'targets','target_selectors':'uid1\x1fR1\x1f10K','value_mode':'preserve','sync_nc':'1'}
|
||||
yield store,journal,task,row
|
||||
store.close()
|
||||
|
||||
|
||||
def test_receipt_survives_rebinding_new_job_and_history_clear(journal_case):
|
||||
store,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
attempt=journal.claim(task.task_id,row)
|
||||
journal.finish(attempt,'saved_ok','batch_closed','R1 OLD -> NEW')
|
||||
history=HistoryRepository(store)
|
||||
snapshot=history.clear_snapshot();history.clear(snapshot.cutoff_id)
|
||||
changed=dict(row,job_id='regenerated-job')
|
||||
saved=journal.bind(task.task_id,[changed],{})
|
||||
assert saved['regenerated-job'][0]=='saved_ok'
|
||||
with pytest.raises(TaskCenterError,match='quick_run_complete'):journal.claim(task.task_id,changed)
|
||||
assert history.page().rows==()
|
||||
|
||||
|
||||
def test_unfinished_attempt_cannot_be_claimed_again(journal_case):
|
||||
_,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
journal.claim(task.task_id,row)
|
||||
with pytest.raises(TaskCenterError,match='quick_run_uncertain'):journal.claim(task.task_id,row)
|
||||
|
||||
|
||||
def test_late_attempt_cannot_override_committed_receipt(journal_case):
|
||||
_,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
attempt=journal.claim(task.task_id,row)
|
||||
journal.finish(attempt,'saved_ok','batch_closed','verified')
|
||||
with pytest.raises(TaskCenterError):journal.finish(attempt,'batch_failed','batch_closed','late failure')
|
||||
assert journal.bind(task.task_id,[row],{})[row['job_id']][0]=='saved_ok'
|
||||
|
||||
|
||||
def test_successful_targets_cannot_be_regrouped_or_revised(journal_case):
|
||||
_,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
journal.finish(journal.claim(task.task_id,row),'saved_ok','batch_closed','verified')
|
||||
revised=dict(row,new_part='NEW2',target_selectors='uid1\x1fR1\x1f10K\x1euid2\x1fR2\x1f10K')
|
||||
journal.authorize_retry(task.task_id)
|
||||
with pytest.raises(TaskCenterError):journal.bind(task.task_id,[revised],{})
|
||||
|
||||
|
||||
def test_retry_never_unlocks_success_and_only_releases_safe_failure(journal_case):
|
||||
_,journal,task,row=journal_case
|
||||
second=dict(row,job_id='second',sheet_number='2',target_selectors='uid2\x1fR2\x1f10K')
|
||||
journal.bind(task.task_id,[row,second],{})
|
||||
journal.finish(journal.claim(task.task_id,row),'saved_ok','batch_closed','saved')
|
||||
journal.finish(journal.claim(task.task_id,second),'batch_failed','batch_closed','preflight failed')
|
||||
with pytest.raises(TaskCenterError,match='quick_run_retry_required'):journal.claim(task.task_id,second)
|
||||
journal.authorize_retry(task.task_id)
|
||||
journal.bind(task.task_id,[row,second],{})
|
||||
with pytest.raises(TaskCenterError,match='quick_run_complete'):journal.claim(task.task_id,row)
|
||||
retry=journal.claim(task.task_id,second)
|
||||
assert retry.generation==2
|
||||
|
||||
|
||||
def test_new_round_is_the_only_way_to_repeat_success(journal_case):
|
||||
_,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
journal.finish(journal.claim(task.task_id,row),'saved_ok','batch_closed','saved')
|
||||
journal.start_round(task.task_id,'new-explicit-round')
|
||||
journal.bind(task.task_id,[row],{})
|
||||
assert journal.claim(task.task_id,row).generation==1
|
||||
|
||||
|
||||
def test_receipt_and_history_commit_roll_back_together(journal_case,monkeypatch):
|
||||
store,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
attempt=journal.claim(task.task_id,row)
|
||||
def fail(*args,**kwargs):raise OSError('history storage failure')
|
||||
monkeypatch.setattr(HistoryRepository,'append',fail)
|
||||
with pytest.raises(OSError):journal.finish(attempt,'saved_ok','batch_closed','saved')
|
||||
assert HistoryRepository(store).page().rows==()
|
||||
with pytest.raises(TaskCenterError,match='quick_run_uncertain'):journal.claim(task.task_id,row)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('result',['restored_ok','retry_ready'])
|
||||
def test_explicit_native_recovery_can_resolve_an_uncertain_receipt(journal_case,result):
|
||||
_,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
journal.finish(journal.claim(task.task_id,row),'batch_saved_unverified','batch_closed','unverified')
|
||||
journal.reconcile_recovery(task.task_id,row,result,'recovery_saved_verified','verified recovery')
|
||||
if result=='restored_ok':
|
||||
with pytest.raises(TaskCenterError,match='quick_run_complete'):journal.claim(task.task_id,row)
|
||||
else:
|
||||
assert journal.claim(task.task_id,dict(row,job_id='new-recovery-job')).generation==3
|
||||
|
||||
|
||||
def test_explicit_failed_scope_revision_does_not_prevent_a_later_new_round(journal_case):
|
||||
_,journal,task,row=journal_case
|
||||
journal.bind(task.task_id,[row],{})
|
||||
journal.finish(journal.claim(task.task_id,row),'batch_failed','batch_closed','safe failure')
|
||||
revised=dict(row,new_part='FIXED')
|
||||
journal.authorize_retry(task.task_id)
|
||||
journal.bind(task.task_id,[revised],{})
|
||||
journal.finish(journal.claim(task.task_id,revised),'saved_ok','batch_closed','saved')
|
||||
journal.start_round(task.task_id,'confirmed-next-round')
|
||||
assert not journal.has_plan(task.task_id)
|
||||
|
||||
|
||||
def test_latest_verified_semantic_state_advances_atomically_and_proof_damage_blocks(journal_case,tmp_path):
|
||||
import hashlib
|
||||
_,journal,task,row=journal_case
|
||||
second=dict(row,job_id='second',old_part='OTHER',target_selectors='uid2\x1fR2\x1f1K')
|
||||
journal.bind(task.task_id,[row,second],{})
|
||||
evidence=tmp_path/'proof';evidence.write_text('immutable proof')
|
||||
proof={'files':[{'path':str(evidence),'sha256':hashlib.sha256(evidence.read_bytes()).hexdigest()}],
|
||||
'baseline':dict(project_path=row['project_path'],schematic_name='MAIN',sheet_number='1',state='first verified state')}
|
||||
journal.finish(journal.claim(task.task_id,row),'saved_ok','batch_closed','saved',proof=proof)
|
||||
assert journal.expected_baselines(task.task_id,row['project_path'])[0]['state']=='first verified state'
|
||||
proof=deepcopy(proof);proof['baseline']['state']='second verified state'
|
||||
journal.finish(journal.claim(task.task_id,second),'saved_ok','batch_closed','saved',proof=proof)
|
||||
assert journal.expected_baselines(task.task_id,row['project_path'])[0]['state']=='second verified state'
|
||||
assert 'proof' not in HistoryRepository(journal.store).page().rows[0]['details']
|
||||
evidence.unlink()
|
||||
assert journal.conflicts(task.task_id,[row,second],{r['job_id']:('saved_ok','batch_closed','saved') for r in (row,second)})
|
||||
with pytest.raises(TaskCenterError):journal.expected_baselines(task.task_id,row['project_path'])
|
||||
@@ -0,0 +1,76 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.native_helper import read_snapshot, transaction_binding
|
||||
from dx_productivity.trial_task_center.quick_run import _encode_vbs_field
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def proof_case(tmp_path):
|
||||
project=tmp_path/'board.prj';project.touch()
|
||||
root=tmp_path/'data'/'quick-run';root.mkdir(parents=True)
|
||||
row=dict(task_id='task',job_id='job',project_path=str(project),schematic_name='MAIN',sheet_number='1',
|
||||
old_part='OLD',new_part='NEW',value_mode='preserve',target_scope='all')
|
||||
snapshot=root/'snapshot-job.tsv'
|
||||
snapshot.write_text(f'M\t{project}\tOLD\tNEW\tMAIN:1\tpreserve\nC\tuid\tR1\t0\t0\t0\t1\tpins\n',encoding='utf8')
|
||||
meta,components=read_snapshot(snapshot)
|
||||
report={'ok':True,'stage':'native_replace_verified','transaction':transaction_binding(
|
||||
snapshot,meta,components,['lib','NEW','sym','','name','label'],'sym'),
|
||||
'batch_attempt':dict(task_id='task',job_id='job',project_path=str(project),
|
||||
session_id='a'*32,attempt_id='b'*32,generation=1)}
|
||||
Path(str(snapshot)+'.native.json').write_text(json.dumps(report),encoding='utf8')
|
||||
(root/'post-state-job.tsv').write_text('job_id\tproject_path\tschematic_name\tsheet_number\tsemantic_state\n'+
|
||||
'\t'.join(_encode_vbs_field(row[key]) for key in ('job_id','project_path','schematic_name','sheet_number'))+
|
||||
'\t'+_encode_vbs_field('S\tfull_sheet_v1\r\nR1=NEW\r\n')+'\n',encoding='utf8')
|
||||
return tmp_path/'data',root,row
|
||||
|
||||
|
||||
def test_receipt_proof_binds_native_snapshot_attempt_and_semantic_state(proof_case):
|
||||
from dx_productivity.trial_task_center.execution_proof import capture_result, proof_intact
|
||||
data,root,row=proof_case
|
||||
proof=capture_result(data,root,row,('saved_ok','batch_closed','verified'),strict=True)
|
||||
assert proof['baseline']['state']=='S\tfull_sheet_v1\r\nR1=NEW\r\n'
|
||||
assert proof['native_attempt']['attempt_id']=='b'*32
|
||||
assert proof_intact(proof)
|
||||
# Active handoff can be archived without breaking committed proof identity.
|
||||
root.rename(root.with_name('archived'))
|
||||
assert proof_intact(proof)
|
||||
Path(proof['files'][0]['path']).write_text('changed',encoding='utf8')
|
||||
assert not proof_intact(proof)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('damage',['missing_report','foreign_attempt','snapshot_changed','missing_baseline'])
|
||||
def test_unbound_result_cannot_be_imported_as_a_completion_receipt(proof_case,damage):
|
||||
from dx_productivity.trial_task_center.execution_proof import capture_result
|
||||
data,root,row=proof_case
|
||||
report=root/'snapshot-job.tsv.native.json'
|
||||
if damage=='missing_report':report.unlink()
|
||||
elif damage=='foreign_attempt':
|
||||
record=json.loads(report.read_text());record['batch_attempt']['job_id']='other';report.write_text(json.dumps(record))
|
||||
elif damage=='snapshot_changed':
|
||||
with (root/'snapshot-job.tsv').open('a') as stream:stream.write('changed\n')
|
||||
else:(root/'post-state-job.tsv').unlink()
|
||||
with pytest.raises(TaskCenterError,match='quick_run_uncertain'):
|
||||
capture_result(data,root,row,('saved_ok','batch_closed','verified'),strict=True)
|
||||
|
||||
|
||||
def test_proof_changed_during_binding_is_never_committed(proof_case,monkeypatch):
|
||||
from dx_productivity.trial_task_center.execution_proof import capture_result
|
||||
from dx_productivity.trial_task_center import native_helper
|
||||
data,root,row=proof_case
|
||||
original=native_helper.require_resumable_transaction
|
||||
def changed(snapshot,*args):
|
||||
result=original(snapshot,*args)
|
||||
with Path(snapshot).open('a') as stream:stream.write('changed after validation\n')
|
||||
return result
|
||||
monkeypatch.setattr(native_helper,'require_resumable_transaction',changed)
|
||||
with pytest.raises(TaskCenterError):capture_result(data,root,row,('saved_ok','batch_closed','verified'),strict=True)
|
||||
|
||||
|
||||
def test_success_without_save_close_code_is_not_a_verified_native_receipt(proof_case):
|
||||
from dx_productivity.trial_task_center.execution_proof import capture_result
|
||||
data,root,row=proof_case
|
||||
with pytest.raises(TaskCenterError):capture_result(data,root,row,('saved_ok','','unverified close'),strict=True)
|
||||
@@ -0,0 +1,31 @@
|
||||
from threading import Event, Thread
|
||||
|
||||
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
|
||||
|
||||
|
||||
def test_history_facade_initialization_does_not_invert_store_and_cache_locks(tmp_path,monkeypatch):
|
||||
from dx_productivity.trial_task_center import history_recording
|
||||
project=tmp_path/'projects'/'Board'/'board.prj'
|
||||
project.parent.mkdir(parents=True);project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
try:
|
||||
service=TaskCenterService(store,configure_logging(tmp_path/'data'))
|
||||
task=service.create_project_tasks(tmp_path/'projects')[0]
|
||||
entered=Event();errors=[]
|
||||
original=history_recording.initialize_history_import
|
||||
def initialize(store):entered.set();original(store)
|
||||
monkeypatch.setattr(history_recording,'initialize_history_import',initialize)
|
||||
def read():
|
||||
try:service._facade_for_task_record(task)
|
||||
except BaseException as error:errors.append(error)
|
||||
with store._lock:
|
||||
thread=Thread(target=read,daemon=True);thread.start()
|
||||
assert entered.wait(2)
|
||||
acquired=service._facade_lock.acquire(timeout=.2)
|
||||
if acquired:service._facade_lock.release()
|
||||
thread.join(3)
|
||||
assert acquired,'A DB owner reading the facade would deadlock with facade initialization waiting on DB'
|
||||
assert not thread.is_alive() and not errors
|
||||
finally:store.close()
|
||||
@@ -0,0 +1,181 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.history_store import HistoryRepository, create_history_schema
|
||||
from dx_productivity.trial_task_center.history_recording import (
|
||||
ensure_legacy_history, initialize_history_import, record_operation)
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def recording(tmp_path):
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
with store.transaction():create_history_schema(store._connection)
|
||||
service=SimpleNamespace(_store=store,_logging=SimpleNamespace(root=tmp_path/'data'))
|
||||
yield service,HistoryRepository(store)
|
||||
store.close()
|
||||
|
||||
|
||||
def test_task_creation_and_metadata_changes_are_recorded_with_actual_differences(recording):
|
||||
service,history=recording
|
||||
task=record_operation(service,'create_task',None,lambda:service._store.create_task('原名称','原备注'))
|
||||
record_operation(service,'update_task',task.task_id,
|
||||
lambda:service._store.update_task(task.task_id,name='新名称',note='新备注'))
|
||||
rows=history.page().rows
|
||||
assert len(rows)==2 and rows[0]['task_name']=='新名称'
|
||||
change=next(change for change in rows[0]['details']['changes'] if change['table']=='tasks')
|
||||
assert change['before']=={'name':'原名称','note':'原备注'}
|
||||
assert change['after']=={'name':'新名称','note':'新备注'}
|
||||
assert '原名称' in rows[0]['summary'] and '新名称' in rows[0]['summary']
|
||||
|
||||
|
||||
def test_rule_mutation_without_task_id_records_only_final_values(recording):
|
||||
service,history=recording
|
||||
task=service._store.create_task('规则任务')
|
||||
rule=service._store.add_rule(task.task_id,'OLD','NEW')
|
||||
def update():
|
||||
service._store.update_rule(rule.rule_id,old_part_number='OLD',new_part_number='TEMP',enabled=False)
|
||||
return service._store.update_rule(rule.rule_id,old_part_number='OLD',new_part_number='FINAL',enabled=True)
|
||||
record_operation(service,'update_rule',None,update)
|
||||
row=history.page().rows[0]
|
||||
assert row['task_id']==task.task_id
|
||||
changes=[change for change in row['details']['changes'] if change['table']=='rules']
|
||||
assert len(changes)==1
|
||||
assert changes[0]['before']=={'new_part_number':'NEW'}
|
||||
assert changes[0]['after']=={'new_part_number':'FINAL'}
|
||||
assert history.page(query='TEMP').total==0
|
||||
|
||||
|
||||
def test_no_change_and_read_only_actions_do_not_manufacture_history(recording):
|
||||
service,history=recording
|
||||
task=service._store.create_task('相同值')
|
||||
record_operation(service,'update_task',task.task_id,
|
||||
lambda:service._store.update_task(task.task_id,name=task.name,note=task.note))
|
||||
assert record_operation(service,'list_tasks',None,lambda:service._store.list_tasks())
|
||||
assert history.page().total==0
|
||||
|
||||
|
||||
def test_history_failure_rolls_back_mutation_and_cleans_temporary_capture(recording,monkeypatch):
|
||||
service,history=recording
|
||||
task=service._store.create_task('原任务')
|
||||
def fail(*args,**kwargs):raise OSError('history commit failed')
|
||||
with monkeypatch.context() as patch:
|
||||
patch.setattr(HistoryRepository,'append',fail)
|
||||
with pytest.raises(OSError):
|
||||
record_operation(service,'update_task',task.task_id,
|
||||
lambda:service._store.update_task(task.task_id,name='不应提交',note=''))
|
||||
assert service._store.get_task(task.task_id).name=='原任务'
|
||||
record_operation(service,'update_task',task.task_id,
|
||||
lambda:service._store.update_task(task.task_id,name='可以提交',note=''))
|
||||
assert history.page().total==1
|
||||
|
||||
|
||||
def test_deletion_preserves_task_name_and_real_removed_rows(recording):
|
||||
service,history=recording
|
||||
task=service._store.create_task('待删除任务')
|
||||
service._store.add_rule(task.task_id,'OLD','NEW')
|
||||
service._store.soft_delete_task(task.task_id)
|
||||
record_operation(service,'purge_task',task.task_id,lambda:service._store.purge_task(task.task_id))
|
||||
row=history.page().rows[0]
|
||||
assert row['task_name']=='待删除任务'
|
||||
assert any(change['table']=='rules' and change['after'] is None for change in row['details']['changes'])
|
||||
|
||||
|
||||
def test_large_target_plan_records_exact_changed_rows_in_bounded_chunks(recording,tmp_path):
|
||||
service,history=recording
|
||||
task=service._store.create_task('大量位号')
|
||||
project=tmp_path/'board.prj';project.write_text('sample')
|
||||
rows=tuple(TargetPlanRow(ComponentTarget(project,'MAIN','1',f'uid-{i}',f'R{i}','OLD','10K'),True,'NEW')
|
||||
for i in range(230))
|
||||
# The capture concerns the real target table writes; store's public guards
|
||||
# are covered separately. Use its persisted row shape in this fixture.
|
||||
def save():
|
||||
for row in rows:
|
||||
t=row.target
|
||||
service._store._connection.execute('''INSERT INTO target_plan
|
||||
(task_id,project_path,schematic_name,sheet_number,uid,refdes,old_part_number,old_value,enabled,new_part_number)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?)''',
|
||||
(task.task_id,str(project),t.schematic_name,t.sheet_number,t.uid,t.refdes,t.old_part_number,t.old_value,1,'NEW'))
|
||||
return rows
|
||||
record_operation(service,'save_plan_drafts',None,save)
|
||||
page=history.page()
|
||||
assert len(page.rows)==3
|
||||
changes=[change for event in page.rows for change in event['details']['changes'] if change['table']=='target_plan']
|
||||
assert len(changes)==230
|
||||
assert all(len(event['details']['changes'])<=100 for event in page.rows)
|
||||
assert history.page(query='R229').total==1
|
||||
|
||||
|
||||
def legacy(service,task,*,name='events.jsonl',timestamp=None,state='saved_ok',operation='quick_run_executed',message='R1 OLD → NEW',details=None):
|
||||
path=service._logging.root/'tasks'/task.task_id/name
|
||||
path.parent.mkdir(parents=True,exist_ok=True)
|
||||
payload={'task_id':task.task_id,'sequence':1,'timestamp_utc':timestamp or
|
||||
(datetime.now(timezone.utc)-timedelta(days=1)).isoformat(),
|
||||
'operation':operation,'message':message,'details':dict(state=state,**(details or {}))}
|
||||
path.write_text(json.dumps(payload,ensure_ascii=False)+'\n',encoding='utf8')
|
||||
return path
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('state','expected'),[
|
||||
('saved_ok','verified'),('no_match','no_match'),('batch_failed','failed'),
|
||||
('hard_failure','failed'),('batch_saved_unverified','uncertain'),('restored_ok','restored'),
|
||||
('awaiting_native_replace','uncertain')])
|
||||
def test_legacy_native_states_are_imported_without_inventing_success(recording,state,expected):
|
||||
service,history=recording
|
||||
task=service._store.create_task('旧任务')
|
||||
source=legacy(service,task,state=state)
|
||||
original=source.read_bytes()
|
||||
initialize_history_import(service._store)
|
||||
ensure_legacy_history(service)
|
||||
rows=history.page().rows
|
||||
assert len(rows)==1 and rows[0]['state']==expected
|
||||
assert rows[0]['timestamp_utc']<datetime.now(timezone.utc).isoformat()
|
||||
assert source.read_bytes()==original
|
||||
|
||||
|
||||
def test_import_cutoff_and_completion_survive_clear_and_repeated_initialization(recording):
|
||||
service,history=recording
|
||||
task=service._store.create_task('旧任务')
|
||||
legacy(service,task,name='events.jsonl.1')
|
||||
initialize_history_import(service._store)
|
||||
legacy(service,task,timestamp=(datetime.now(timezone.utc)+timedelta(seconds=2)).isoformat(),message='新回执对应事件')
|
||||
initialize_history_import(service._store)
|
||||
ensure_legacy_history(service)
|
||||
assert history.page().total==1
|
||||
history.clear(history.clear_snapshot().cutoff_id)
|
||||
initialize_history_import(service._store)
|
||||
ensure_legacy_history(service)
|
||||
assert history.page().total==0
|
||||
|
||||
|
||||
def test_cancelled_or_failed_import_does_not_mark_migration_complete(recording):
|
||||
service,history=recording
|
||||
task=service._store.create_task('待迁移')
|
||||
source=legacy(service,task)
|
||||
initialize_history_import(service._store)
|
||||
class Cancel:
|
||||
def checkpoint(self):raise RuntimeError('cancelled')
|
||||
with pytest.raises(RuntimeError,match='cancelled'):ensure_legacy_history(service,Cancel())
|
||||
source.write_text('{"truncated":\n',encoding='utf8')
|
||||
with pytest.raises(TaskCenterError):ensure_legacy_history(service)
|
||||
legacy(service,task)
|
||||
ensure_legacy_history(service)
|
||||
assert history.page().total==1
|
||||
|
||||
|
||||
def test_import_excludes_new_or_unrelated_diagnostics_and_credentials(recording):
|
||||
service,history=recording
|
||||
task=service._store.create_task('旧任务')
|
||||
legacy(service,task,details={'api_key':'must-not-import','refdes':'R10'})
|
||||
legacy(service,task,name='events.jsonl.1',operation='dx_dsh_response',message='must-not-import')
|
||||
initialize_history_import(service._store)
|
||||
ensure_legacy_history(service)
|
||||
assert history.page().total==1
|
||||
assert history.page(query='must-not-import').total==0
|
||||
assert history.page(query='R10').total==1
|
||||
@@ -0,0 +1,173 @@
|
||||
"""Real service mutations must commit their history at the same SQL boundary."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from threading import Event
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.dsh.task_creation import validate_creation_spec
|
||||
from dx_productivity.trial_task_center.errors import TaskCenterError
|
||||
from dx_productivity.trial_task_center.history_store import HistoryRepository
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def case(tmp_path):
|
||||
root=tmp_path/'projects'
|
||||
paths=[]
|
||||
for name in ('A','B'):
|
||||
path=root/name/'board.prj';path.parent.mkdir(parents=True)
|
||||
path.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
paths.append(path)
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
service=TaskCenterService(store,configure_logging(tmp_path/'data'))
|
||||
spec=validate_creation_spec({'name':'Assistant history','note':'sample',
|
||||
'rules':[{'old_part_number':'OLD','new_part_number':'NEW'}]})
|
||||
yield SimpleNamespace(store=store,service=service,root=root,paths=tuple(paths),spec=spec,
|
||||
history=HistoryRepository(store))
|
||||
store.close()
|
||||
|
||||
|
||||
def bound_task(case):
|
||||
task=case.service.create_project_tasks(case.root,selected_paths=(case.paths[0],))[0]
|
||||
case.service.add_rule(task.task_id,'OLD','NEW')
|
||||
target=ComponentTarget(case.paths[0],'MAIN','1','uid-1','R1','OLD','10K')
|
||||
case.service.for_project_task(task.task_id)._quick_run_preparer.collect_inventory=lambda *args,**kwargs:(target,)
|
||||
return task
|
||||
|
||||
|
||||
def events(case,operation):
|
||||
return tuple(row for row in case.history.page().rows if row['operation']==operation)
|
||||
|
||||
|
||||
def test_assistant_creation_records_task_project_and_rule_changes_per_task(case):
|
||||
result=case.service.create_assistant_project_tasks('a'*32,case.spec,case.root,case.paths)
|
||||
rows=events(case,'create_assistant_project_tasks')
|
||||
assert len(rows)==2 and {row['task_id'] for row in rows}==set(result['task_ids'])
|
||||
for row in rows:
|
||||
changes=row['details']['changes']
|
||||
assert {change['table'] for change in changes}=={'tasks','projects','rules'}
|
||||
assert all(change['before'] is None for change in changes)
|
||||
rule=next(change for change in changes if change['table']=='rules')
|
||||
assert rule['after']['old_part_number']=='OLD' and rule['after']['new_part_number']=='NEW'
|
||||
|
||||
|
||||
def test_assistant_request_replay_does_not_duplicate_history(case):
|
||||
first=case.service.create_assistant_project_tasks('a'*32,case.spec,case.root,case.paths)
|
||||
before=case.history.page().rows
|
||||
replay=case.service.create_assistant_project_tasks('a'*32,case.spec,case.root,case.paths)
|
||||
assert replay['replayed'] and replay['task_ids']==first['task_ids']
|
||||
assert case.history.page().rows==before
|
||||
|
||||
|
||||
def test_refresh_bound_project_records_exact_availability_change(case):
|
||||
task=bound_task(case)
|
||||
case.paths[0].rename(case.paths[0].with_suffix('.moved'))
|
||||
case.service.refresh_bound_project(task.task_id)
|
||||
rows=events(case,'refresh_bound_project')
|
||||
assert len(rows)==1
|
||||
change=next(change for change in rows[0]['details']['changes'] if change['table']=='projects')
|
||||
assert change['before']['available']==1 and change['after']['available']==0
|
||||
assert not case.store.list_projects(task.task_id)[0].available
|
||||
|
||||
|
||||
def test_unchanged_refresh_and_context_polling_do_not_manufacture_history(case):
|
||||
task=bound_task(case)
|
||||
before=case.history.page().rows
|
||||
for _ in range(2):
|
||||
case.service.refresh_bound_project(task.task_id)
|
||||
case.service.task_context(task.task_id)
|
||||
assert case.history.page().rows==before
|
||||
|
||||
|
||||
def test_collect_inventory_records_plan_defaults_and_mode_in_one_event(case):
|
||||
task=bound_task(case)
|
||||
case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object())
|
||||
rows=events(case,'collect_target_inventory')
|
||||
assert len(rows)==1 and rows[0]['task_id']==task.task_id
|
||||
changes=rows[0]['details']['changes']
|
||||
target=next(change for change in changes if change['table']=='target_plan')
|
||||
assert target['before'] is None
|
||||
assert target['after']['refdes']=='R1' and target['after']['new_part_number']=='NEW'
|
||||
task_change=next(change for change in changes if change['table']=='tasks')
|
||||
assert task_change['before']['target_plan_mode']==0 and task_change['after']['target_plan_mode']==1
|
||||
|
||||
|
||||
def test_identical_inventory_collection_does_not_duplicate_history(case):
|
||||
task=bound_task(case)
|
||||
case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object())
|
||||
before=case.history.page().rows
|
||||
case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object())
|
||||
assert case.history.page().rows==before
|
||||
|
||||
|
||||
def test_value_policy_records_actual_change_and_ignores_repeated_value(case):
|
||||
task=bound_task(case)
|
||||
case.service.set_copy_part_number_to_value(task.task_id,True)
|
||||
rows=events(case,'set_value_policy')
|
||||
assert len(rows)==1
|
||||
change=next(change for change in rows[0]['details']['changes'] if change['table']=='tasks')
|
||||
assert change['before']['copy_part_number_to_value']==0 and change['after']['copy_part_number_to_value']==1
|
||||
before=case.history.page().rows
|
||||
case.service.set_copy_part_number_to_value(task.task_id,True)
|
||||
assert case.history.page().rows==before
|
||||
|
||||
|
||||
@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)
|
||||
before=case.history.page().rows
|
||||
def fail(*args,**kwargs):raise OSError('controlled history write failure')
|
||||
monkeypatch.setattr(HistoryRepository,'append',fail)
|
||||
with pytest.raises(TaskCenterError):
|
||||
if operation=='create_assistant_project_tasks':
|
||||
case.service.create_assistant_project_tasks('b'*32,case.spec,case.root,case.paths)
|
||||
elif operation=='collect_target_inventory':
|
||||
case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object())
|
||||
elif operation=='set_value_policy':
|
||||
case.service.set_copy_part_number_to_value(task.task_id,True)
|
||||
else:
|
||||
case.paths[0].rename(case.paths[0].with_suffix('.moved'))
|
||||
case.service.refresh_bound_project(task.task_id)
|
||||
assert case.history.page().rows==before
|
||||
if task is None:
|
||||
assert case.store.list_tasks()==() and case.store.assistant_creation_receipt('b'*32) is None
|
||||
elif operation=='collect_target_inventory':
|
||||
assert case.store.list_target_plan(task.task_id)==() and not case.store.get_task(task.task_id).target_plan_mode
|
||||
elif operation=='set_value_policy':
|
||||
assert not case.store.get_task(task.task_id).copy_part_number_to_value
|
||||
else:
|
||||
assert case.store.list_projects(task.task_id)[0].available
|
||||
|
||||
|
||||
def test_cancelled_inventory_read_saves_neither_plan_nor_history(case):
|
||||
task=bound_task(case)
|
||||
before=case.history.page().rows;stop=Event()
|
||||
def collect(*args,**kwargs):stop.set();return ()
|
||||
case.service.for_project_task(task.task_id)._quick_run_preparer.collect_inventory=collect
|
||||
with pytest.raises(TaskCenterError) as error:
|
||||
case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object(),stop_event=stop)
|
||||
assert error.value.code=='operation_cancelled'
|
||||
assert case.history.page().rows==before and not case.store.list_target_plan(task.task_id)
|
||||
|
||||
|
||||
def test_history_writes_remain_available_while_cad_inventory_read_is_waiting(case):
|
||||
task=bound_task(case);started=Event();release=Event()
|
||||
def collect(*args,**kwargs):
|
||||
started.set()
|
||||
if not release.wait(5):raise RuntimeError('inventory test was not released')
|
||||
return (ComponentTarget(case.paths[0],'MAIN','1','uid-1','R1','OLD','10K'),)
|
||||
case.service.for_project_task(task.task_id)._quick_run_preparer.collect_inventory=collect
|
||||
with ThreadPoolExecutor(max_workers=2) as pool:
|
||||
inventory=pool.submit(case.service.collect_target_inventory,task.task_id,('OLD',),runtime=object())
|
||||
try:
|
||||
assert started.wait(3)
|
||||
other=pool.submit(case.history.append,task_id=task.task_id,task_name=task.name,
|
||||
operation='probe',summary='concurrent history write')
|
||||
other.result(timeout=2)
|
||||
finally:release.set()
|
||||
assert inventory.result(timeout=5)
|
||||
assert len(events(case,'collect_target_inventory'))==1 and len(events(case,'probe'))==1
|
||||
@@ -0,0 +1,137 @@
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.history_store import HistoryRepository, create_history_schema
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def history(tmp_path):
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
with store.transaction():
|
||||
create_history_schema(store._connection)
|
||||
yield HistoryRepository(store),store
|
||||
store.close()
|
||||
|
||||
|
||||
def event(repository, **changes):
|
||||
values=dict(task_id='task-1',task_name='电源 CBB',project_path='F:/Board/board.prj',
|
||||
operation='quick_run_executed',state='verified',summary='R101 OLD-1 → NEW-1',
|
||||
details={'refdes':'R101','old_part_number':'OLD-1','new_part_number':'NEW-1'})
|
||||
values.update(changes)
|
||||
return repository.append(**values)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('query',['电源','cbb','f:/board','R101','old-1','NEW-1'])
|
||||
def test_history_searches_unicode_identity_and_recorded_changes(history,query):
|
||||
repository,_=history
|
||||
identifier=event(repository)
|
||||
event(repository,task_name='无关任务',project_path='F:/Other.prj',summary='未命中',details={},state='no_match')
|
||||
page=repository.page(query=query)
|
||||
assert page.total==1
|
||||
assert [row['event_id'] for row in page.rows]==[identifier]
|
||||
assert page.rows[0]['details']['refdes']=='R101'
|
||||
|
||||
|
||||
def test_search_treats_wildcards_as_literal_text(history):
|
||||
repository,_=history
|
||||
literal=event(repository,summary='100%_done')
|
||||
event(repository,summary='100xxxAdone',details={})
|
||||
assert [row['event_id'] for row in repository.page(query='100%_').rows]==[literal]
|
||||
|
||||
|
||||
def test_keyset_paging_and_state_filter_have_no_duplicates(history):
|
||||
repository,_=history
|
||||
identifiers=[event(repository,state='failed' if index%2 else 'verified') for index in range(7)]
|
||||
first=repository.page(state='verified',limit=2)
|
||||
assert first.total==4
|
||||
assert [row['event_id'] for row in first.rows]==[identifiers[6],identifiers[4]]
|
||||
second=repository.page(state='verified',limit=2,before_id=first.next_cursor)
|
||||
assert [row['event_id'] for row in second.rows]==[identifiers[2],identifiers[0]]
|
||||
assert second.next_cursor is None
|
||||
|
||||
|
||||
def test_clear_uses_confirmation_cutoff_and_preserves_new_events_and_other_tables(history):
|
||||
repository,store=history
|
||||
with store.transaction():
|
||||
store._connection.execute('CREATE TABLE completion_probe (value TEXT)')
|
||||
store._connection.execute("INSERT INTO completion_probe VALUES ('complete')")
|
||||
old=event(repository,source_key='native:event:1')
|
||||
snapshot=repository.clear_snapshot()
|
||||
assert snapshot.cutoff_id==old and snapshot.count==1
|
||||
newer=event(repository,summary='确认期间完成的新操作',source_key='native:event:2')
|
||||
assert repository.clear(snapshot.cutoff_id)==1
|
||||
assert [row['event_id'] for row in repository.page().rows]==[newer]
|
||||
assert store._connection.execute('SELECT value FROM completion_probe').fetchone()[0]=='complete'
|
||||
assert event(repository,source_key='native:event:1')==old
|
||||
assert [row['event_id'] for row in repository.page().rows]==[newer]
|
||||
|
||||
|
||||
def test_clear_survives_reopen_without_reusing_ids_or_reimporting(history):
|
||||
repository,store=history
|
||||
old=event(repository,source_key='legacy:task-1:1')
|
||||
repository.clear(repository.clear_snapshot().cutoff_id)
|
||||
second=TaskStore(store.path)
|
||||
try:
|
||||
with second.transaction():create_history_schema(second._connection)
|
||||
other=HistoryRepository(second)
|
||||
assert other.page().rows==()
|
||||
assert event(other,source_key='legacy:task-1:1')==old
|
||||
assert other.page().rows==()
|
||||
assert event(other,source_key='new')>old
|
||||
finally:second.close()
|
||||
|
||||
|
||||
def test_history_and_receipt_transaction_roll_back_together(history):
|
||||
repository,store=history
|
||||
with pytest.raises(RuntimeError):
|
||||
with store.transaction():
|
||||
event(repository,source_key='rolled-back')
|
||||
raise RuntimeError('receipt commit failed')
|
||||
assert repository.page().rows==()
|
||||
assert event(repository,source_key='rolled-back')>0
|
||||
|
||||
|
||||
def test_empty_state_and_invalid_paging_are_explicit(history):
|
||||
repository,_=history
|
||||
assert repository.clear_snapshot().count==0
|
||||
assert repository.page().total==0 and repository.page().next_cursor is None
|
||||
for values in ({'limit':0},{'limit':501},{'before_id':-1}):
|
||||
with pytest.raises(ValueError):repository.page(**values)
|
||||
|
||||
|
||||
def test_legacy_import_preserves_event_time_in_normalized_utc(history):
|
||||
repository,_=history
|
||||
identifier=event(repository,timestamp_utc='2026-11-01T08:02:03+08:00',source_key='old-timestamp')
|
||||
row=repository.page().rows[0]
|
||||
assert row['event_id']==identifier
|
||||
assert row['timestamp_utc']=='2026-11-01T00:02:03.000000Z'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('value',['2026-11-01T08:00:00','not-a-time',14])
|
||||
def test_import_rejects_invalid_or_ambiguous_event_time(history,value):
|
||||
repository,_=history
|
||||
with pytest.raises(ValueError):event(repository,timestamp_utc=value)
|
||||
assert repository.page().total==0
|
||||
|
||||
|
||||
def test_late_legacy_import_keeps_chronological_keyset_order(history):
|
||||
repository,_=history
|
||||
newest=event(repository,timestamp_utc='2026-11-03T12:00:00Z',summary='新操作')
|
||||
oldest=event(repository,timestamp_utc='2026-10-30T12:00:00Z',summary='旧导入')
|
||||
middle=event(repository,timestamp_utc='2026-11-01T12:00:00Z',summary='较旧操作')
|
||||
tied=event(repository,timestamp_utc='2026-11-01T12:00:00Z',summary='同时间操作')
|
||||
first=repository.page(limit=2)
|
||||
assert [row['event_id'] for row in first.rows]==[newest,tied]
|
||||
second=repository.page(limit=2,before_id=first.next_cursor)
|
||||
assert [row['event_id'] for row in second.rows]==[middle,oldest]
|
||||
assert second.next_cursor is None
|
||||
|
||||
|
||||
def test_cleared_page_cursor_requires_explicit_refresh(history):
|
||||
repository,_=history
|
||||
for index in range(3):event(repository)
|
||||
first=repository.page(limit=1)
|
||||
repository.clear(repository.clear_snapshot().cutoff_id)
|
||||
event(repository)
|
||||
with pytest.raises(ValueError,match='cursor'):
|
||||
repository.page(before_id=first.next_cursor)
|
||||
@@ -0,0 +1,96 @@
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
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.quick_run import QuickRunPreparer
|
||||
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
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def resume_case(tmp_path):
|
||||
project=tmp_path/'projects'/'Board'/'board.prj'
|
||||
project.parent.mkdir(parents=True)
|
||||
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
calls=[];stop=Event()
|
||||
def worker(command,timeout):
|
||||
job=command[-1];calls.append(job)
|
||||
with (Path(command[3])/'status.tsv').open('a',encoding='utf8') as stream:
|
||||
stream.write(f'{job}\tawaiting_native_replace\t\tSimulated capture\n')
|
||||
stream.write(f'{job}\tsaved_ok\tbatch_closed\tSimulated verified result\n')
|
||||
if len(calls)==2:stop.set()
|
||||
return subprocess.CompletedProcess(command,0,'','')
|
||||
stores=[]
|
||||
def reopen():
|
||||
store=TaskStore(tmp_path/'tasks.db');stores.append(store)
|
||||
return TaskCenterService(store,configure_logging(tmp_path/'data'),
|
||||
quick_run_preparer=QuickRunPreparer(tmp_path/'data',process_runner=worker,
|
||||
cscript_resolver=lambda:Path('simulated-cscript'),
|
||||
sheet_discoverer=lambda _: (('MAIN','1'),('MAIN','2'))))
|
||||
service=reopen()
|
||||
task=service.create_project_tasks(tmp_path/'projects')[0]
|
||||
service.add_rule(task.task_id,'OLD1','NEW1');service.add_rule(task.task_id,'OLD2','NEW2')
|
||||
result=execute_task_queue(service,(task.task_id,),stop_event=stop)
|
||||
assert result.paused and len(calls)==2
|
||||
root=service.for_project_task(task.task_id)._quick_run_preparer.data_root/'quick-run'
|
||||
yield service,task,result,root,calls,reopen
|
||||
for store in stores:store.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('damage',['missing_status','missing_command','empty_status','truncated_status','truncated_command'])
|
||||
def test_restart_with_incomplete_handoff_never_replays_worker(resume_case,damage):
|
||||
service,task,result,root,calls,reopen=resume_case
|
||||
path=root/('command.tsv' if 'command' in damage else 'status.tsv')
|
||||
if damage.startswith('missing'):path.unlink()
|
||||
elif damage=='empty_status':path.write_text('job_id\tstate\tcode\tmessage\n',encoding='utf8')
|
||||
else:path.write_text(path.read_text(encoding='utf8')[:-12],encoding='utf8')
|
||||
restarted=reopen()
|
||||
context=restarted.task_context(task.task_id)
|
||||
assert context.interrupted
|
||||
assert context.has_evidence
|
||||
outcome=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
||||
assert outcome.paused and outcome.entries[0].state=='blocked'
|
||||
assert len(calls)==2
|
||||
|
||||
|
||||
def test_ordinary_restart_only_dispatches_remaining_items(resume_case):
|
||||
service,task,result,root,calls,reopen=resume_case
|
||||
restarted=reopen()
|
||||
context=restarted.task_context(task.task_id)
|
||||
assert not context.interrupted and context.progress.completed_items==2
|
||||
result=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
||||
assert not result.paused and result.entries[0].state=='completed'
|
||||
assert len(calls)==4 and len(set(calls))==4
|
||||
|
||||
|
||||
def test_direct_prepare_cannot_overwrite_incomplete_handoff(resume_case):
|
||||
service,task,result,root,calls,reopen=resume_case
|
||||
(root/'status.tsv').unlink()
|
||||
command=(root/'command.tsv').read_bytes()
|
||||
with pytest.raises(TaskCenterError):service.prepare_quick_run(task.task_id)
|
||||
assert (root/'command.tsv').read_bytes()==command
|
||||
assert len(calls)==2
|
||||
|
||||
|
||||
def test_receipts_block_rollback_to_an_empty_status_even_without_sidecars(resume_case):
|
||||
service,task,result,root,calls,reopen=resume_case
|
||||
(root/'status.tsv').write_text('job_id\tstate\tcode\tmessage\n',encoding='utf8')
|
||||
(root/'current-command.tsv').unlink(missing_ok=True)
|
||||
restarted=reopen()
|
||||
assert restarted.task_context(task.task_id).interrupted
|
||||
result=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
||||
assert result.paused and len(calls)==2
|
||||
|
||||
|
||||
def test_missing_entire_handoff_cannot_erase_database_receipts(resume_case):
|
||||
service,task,result,root,calls,reopen=resume_case
|
||||
root.rename(root.with_name('preserved-probe-handoff'))
|
||||
restarted=reopen()
|
||||
assert restarted.task_context(task.task_id).interrupted
|
||||
result=execute_task_queue(restarted,(task.task_id,),stop_event=Event())
|
||||
assert result.paused and len(calls)==2
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Dispatch-count regressions for interruptions in retry/receipt publication."""
|
||||
import csv
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center.execution_journal import ExecutionJournal
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
|
||||
from dx_productivity.trial_task_center.quick_run_protocol import ProjectDiscovery
|
||||
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, TargetPlanRow
|
||||
from dx_productivity.trial_task_center.task_queue import execute_task_queue
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def partial_case(tmp_path, monkeypatch):
|
||||
project = tmp_path/'projects'/'Board'/'board.prj'
|
||||
project.parent.mkdir(parents=True)
|
||||
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
targets = tuple(ComponentTarget(project, 'MAIN', '1', f'uid{i}', f'R{i}', f'OLD{i}', '')
|
||||
for i in (1, 2))
|
||||
calls = []
|
||||
def discover(self, path, parts, **kwargs):
|
||||
rows = tuple(replace(row, old_part_number='NEW1') if row.uid=='uid1' and calls else row
|
||||
for row in targets)
|
||||
return ProjectDiscovery((('MAIN','1'),), {('MAIN','1'):frozenset(r.old_part_number.casefold() for r in rows)}, rows)
|
||||
monkeypatch.setattr(QuickRunPreparer, 'discover_project', discover)
|
||||
def worker(command, timeout):
|
||||
root = Path(command[3])
|
||||
with (root/'current-command.tsv').open(encoding='utf-8-sig') as stream:
|
||||
row = next(csv.DictReader(stream, delimiter='\t'))
|
||||
calls.append(row)
|
||||
state = 'batch_failed' if len(calls)==2 else 'saved_ok'
|
||||
with (root/'status.tsv').open('a', encoding='utf8') as stream:
|
||||
if state=='saved_ok':stream.write(f'{command[-1]}\tawaiting_native_replace\t\tcaptured\n')
|
||||
stream.write(f'{command[-1]}\t{state}\tbatch_closed\tControlled result\n')
|
||||
return subprocess.CompletedProcess(command, 0, '', '')
|
||||
store = TaskStore(tmp_path/'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(tmp_path/'data'), quick_run_preparer=QuickRunPreparer(
|
||||
tmp_path/'data', process_runner=worker, cscript_resolver=lambda:Path('simulated')))
|
||||
task = service.create_project_tasks(tmp_path/'projects')[0]
|
||||
store.record_target_inventory(task.task_id, targets)
|
||||
service.replace_target_plan(task.task_id, tuple(TargetPlanRow(row, True, f'NEW{i}')
|
||||
for i,row in enumerate(targets,1)))
|
||||
outcome = execute_task_queue(service, (task.task_id,), stop_event=Event())
|
||||
assert outcome.entries[0].state=='failed' and len(calls)==2
|
||||
yield service, task, calls
|
||||
store.close()
|
||||
|
||||
|
||||
def test_exact_target_retry_preserves_completed_targets(partial_case):
|
||||
service, task, calls = partial_case
|
||||
service.prepare_failed_quick_run(task.task_id)
|
||||
result = execute_task_queue(service, (task.task_id,), stop_event=Event())
|
||||
assert not result.paused and result.entries[0].state=='completed'
|
||||
assert [row['old_part'] for row in calls]==['OLD1','OLD2','OLD2']
|
||||
|
||||
|
||||
def test_retry_backup_failure_can_resume_preparation_without_dispatch(partial_case, monkeypatch):
|
||||
service, task, calls = partial_case
|
||||
from dx_productivity.trial_task_center import batch_backup
|
||||
original = batch_backup.inherit_backup_for_retry
|
||||
def fail(*args): raise OSError('controlled backup publication failure')
|
||||
monkeypatch.setattr(batch_backup, 'inherit_backup_for_retry', fail)
|
||||
with pytest.raises(Exception): service.prepare_failed_quick_run(task.task_id)
|
||||
assert len(calls)==2
|
||||
assert not service.task_context(task.task_id).interrupted
|
||||
monkeypatch.setattr(batch_backup, 'inherit_backup_for_retry', original)
|
||||
service.prepare_failed_quick_run(task.task_id)
|
||||
result = execute_task_queue(service, (task.task_id,), stop_event=Event())
|
||||
assert result.entries[0].state=='completed' and len(calls)==3
|
||||
|
||||
|
||||
def test_final_success_before_receipt_commit_reconciles_without_dispatch(tmp_path, monkeypatch):
|
||||
project=tmp_path/'projects'/'Board'/'board.prj'
|
||||
project.parent.mkdir(parents=True)
|
||||
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
calls=[]
|
||||
def worker(command,timeout):
|
||||
calls.append(command[-1])
|
||||
with (Path(command[3])/'status.tsv').open('a',encoding='utf8') as stream:
|
||||
stream.write(f'{command[-1]}\tawaiting_native_replace\t\tcaptured\n')
|
||||
stream.write(f'{command[-1]}\tsaved_ok\tbatch_closed\tControlled result\n')
|
||||
return subprocess.CompletedProcess(command,0,'','')
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
try:
|
||||
service=TaskCenterService(store,configure_logging(tmp_path/'data'),quick_run_preparer=QuickRunPreparer(
|
||||
tmp_path/'data',process_runner=worker,cscript_resolver=lambda:Path('simulated'),
|
||||
sheet_discoverer=lambda _:(('MAIN','1'),)))
|
||||
task=service.create_project_tasks(tmp_path/'projects')[0]
|
||||
service.add_rule(task.task_id,'OLD','NEW')
|
||||
original=ExecutionJournal.finish
|
||||
def crash(*args,**kwargs):raise KeyboardInterrupt('controlled process exit')
|
||||
monkeypatch.setattr(ExecutionJournal,'finish',crash)
|
||||
with pytest.raises(KeyboardInterrupt):execute_task_queue(service,(task.task_id,),stop_event=Event())
|
||||
monkeypatch.setattr(ExecutionJournal,'finish',original)
|
||||
result=execute_task_queue(service,(task.task_id,),stop_event=Event())
|
||||
assert result.entries[0].state=='completed' and len(calls)==1
|
||||
receipt=store._connection.execute('SELECT state,receipt_json FROM execution_operations').fetchone()
|
||||
assert receipt['state']=='verified' and receipt['receipt_json']
|
||||
service.start_new_round(task.task_id)
|
||||
finally:store.close()
|
||||
|
||||
|
||||
def test_retry_publication_crash_restores_original_handoff_on_restart(partial_case):
|
||||
import json
|
||||
service,task,calls=partial_case
|
||||
preparer=service.for_project_task(task.task_id)._quick_run_preparer
|
||||
root=preparer.data_root/'quick-run'
|
||||
original=(root/'command.tsv').read_bytes()
|
||||
token='c'*32
|
||||
archive=preparer.data_root/'quick-run-history'/('obsolete-retry-'+token)
|
||||
archive.parent.mkdir(exist_ok=True)
|
||||
(preparer.data_root/'retry-preparation.json').write_text(json.dumps({'task_id':task.task_id,'transition_id':token}))
|
||||
root.rename(archive)
|
||||
root.mkdir()
|
||||
(root/'command.tsv').write_text('partially published retry')
|
||||
TaskCenterService(service._store,service._logging,quick_run_preparer=service._quick_run_preparer)
|
||||
assert (root/'command.tsv').read_bytes()==original
|
||||
assert not (preparer.data_root/'retry-preparation.json').exists()
|
||||
assert len(calls)==2
|
||||
@@ -0,0 +1,54 @@
|
||||
import csv
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
from threading import Event
|
||||
import pytest
|
||||
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.parametrize('failure',['batch_failed','attach_failed'])
|
||||
def test_safe_cbb_failure_holds_remaining_items_and_continues_next_engineering_task(tmp_path,failure):
|
||||
for name in ('A','B'):
|
||||
project=tmp_path/'projects'/name/'board.prj'
|
||||
project.parent.mkdir(parents=True)
|
||||
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
|
||||
calls=[]
|
||||
def worker(command,timeout):
|
||||
directory=Path(command[3])
|
||||
with (directory/'current-command.tsv').open(encoding='utf-8-sig') as stream:
|
||||
row=next(csv.DictReader(stream,delimiter='\t'))
|
||||
calls.append(row)
|
||||
if len(calls)==2 and failure=='attach_failed':
|
||||
return subprocess.CompletedProcess(command,2,'','Controlled pre-mutation attachment failure')
|
||||
state='batch_failed' if len(calls)==2 else 'saved_ok'
|
||||
with (directory/'status.tsv').open('a',encoding='utf8') as stream:
|
||||
if state=='saved_ok':stream.write(f'{command[-1]}\tawaiting_native_replace\t\tcaptured\n')
|
||||
stream.write(f'{command[-1]}\t{state}\tbatch_closed\tControlled result, no CAD\n')
|
||||
return subprocess.CompletedProcess(command,0,'','')
|
||||
store=TaskStore(tmp_path/'tasks.db')
|
||||
try:
|
||||
service=TaskCenterService(store,configure_logging(tmp_path/'data'),quick_run_preparer=QuickRunPreparer(
|
||||
tmp_path/'data',process_runner=worker,cscript_resolver=lambda:Path('simulated-cscript'),
|
||||
sheet_discoverer=lambda _:(('MAIN','1'),('MAIN','2'))))
|
||||
tasks=service.create_project_tasks(tmp_path/'projects')
|
||||
service.add_rule(tasks[0].task_id,'OLD1','NEW1');service.add_rule(tasks[0].task_id,'OLD2','NEW2')
|
||||
ids=tuple(task.task_id for task in tasks)
|
||||
result=execute_task_queue(service,ids,stop_event=Event())
|
||||
assert not result.paused
|
||||
assert [entry.state for entry in result.entries]==['failed','completed']
|
||||
assert [row['task_id'] for row in calls]==[ids[0]]*2+[ids[1]]*4
|
||||
assert service.task_context(ids[0]).phase=='retry'
|
||||
execute_task_queue(service,ids,stop_event=Event())
|
||||
assert len(calls)==6,'ordinary continue must not repeat a safe failure or prior success'
|
||||
service.prepare_failed_quick_run(ids[0])
|
||||
retried=execute_task_queue(service,(ids[0],),stop_event=Event())
|
||||
assert not retried.paused, retried
|
||||
assert len(calls)==9,'explicit retry runs the failed item and held items, preserving the successful item'
|
||||
assert sum(row['task_id']==ids[0] and row['sheet_number']=='1' and row['old_part']=='OLD1' for row in calls)==1
|
||||
finally:
|
||||
store.close()
|
||||
@@ -42,7 +42,7 @@ def test_recycle_bin_has_independent_toggle(qtbot):
|
||||
view = TaskList(lambda _: None)
|
||||
qtbot.addWidget(view)
|
||||
assert view.recycle_button.isCheckable()
|
||||
assert view.filter.isHidden()
|
||||
assert not view.filter.isHidden() # Status categories are now a visible, independent filter.
|
||||
|
||||
|
||||
def test_creating_tasks_from_recycle_bin_returns_to_active_list(window, qtbot):
|
||||
|
||||
+131
-3
@@ -16,8 +16,8 @@ import time
|
||||
|
||||
os.environ.pop('QT_QPA_PLATFORM',None)
|
||||
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'src'))
|
||||
from PySide6.QtCore import Qt,QRectF
|
||||
from PySide6.QtGui import QColor,QFont,QPainter,QLinearGradient,QPen
|
||||
from PySide6.QtCore import Qt,QRectF,QPoint
|
||||
from PySide6.QtGui import QColor,QFont,QPainter,QLinearGradient,QPen,QCursor
|
||||
from PySide6.QtWidgets import QApplication,QWidget,QToolTip
|
||||
from PIL import ImageGrab,ImageChops,ImageStat
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
@@ -29,6 +29,9 @@ from dx_productivity.trial_task_center.target_plan import ComponentTarget,Target
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--output',type=Path,default=Path('build/acrylic-acceptance'))
|
||||
parser.add_argument('--webengine-roundtrip',action='store_true')
|
||||
parser.add_argument('--history',action='store_true')
|
||||
parser.add_argument('--dsh-switch',action='store_true',help='Measure live Acrylic after entering the actual DSH WebEngine page')
|
||||
args=parser.parse_args();out=args.output.resolve();out.mkdir(parents=True,exist_ok=True)
|
||||
app=QApplication([]);app.setFont(QFont('Microsoft YaHei UI',9))
|
||||
def settle(seconds=.2):
|
||||
@@ -80,6 +83,10 @@ def main():
|
||||
assert window.property('acrylicEnabled'),'Native Acrylic not available under current system policy'
|
||||
report['native_enabled']=True
|
||||
def capture(name):
|
||||
QCursor.setPos(window.mapToGlobal(QPoint(12,12)))
|
||||
report.setdefault('surfaces',{})[name]={
|
||||
'hwnd':int(window.winId()),'translucent':window.testAttribute(Qt.WidgetAttribute.WA_TranslucentBackground),
|
||||
'alpha':window.windowHandle().format().alphaBufferSize(),'acrylic':window.property('acrylicEnabled')}
|
||||
settle(.7)
|
||||
QToolTip.hideText();settle(.12)
|
||||
geo=window.geometry();scale=screen.devicePixelRatio()
|
||||
@@ -121,6 +128,65 @@ def main():
|
||||
assert report['right_header_backdrop_difference']>2,report
|
||||
assert report['right_execution_backdrop_difference']>2,report
|
||||
assert report['opaque_table_backdrop_difference']<1.5,report
|
||||
if args.webengine_roundtrip:
|
||||
from dx_productivity.trial_task_center.ui.dsh_native import NativeHarnessView
|
||||
report['before_webengine_surface']={'alpha':window.windowHandle().format().alphaBufferSize(),
|
||||
'translucent':window.testAttribute(Qt.WidgetAttribute.WA_TranslucentBackground),
|
||||
'acrylic':window.property('acrylicEnabled'),'contrast':window.property('highContrastFallback')}
|
||||
def create_view():
|
||||
view=NativeHarnessView(root/'dsh',window)
|
||||
view.start=lambda:None # Real Chromium surface; no server, model or CAD.
|
||||
view.web.setHtml('<html><body>Owned DSH WebEngine surface</body></html>')
|
||||
return view
|
||||
window._create_dsh_view=create_view
|
||||
for index in range(2):
|
||||
window.dsh_action.trigger();settle(2)
|
||||
window.task_center_action.trigger();settle(1)
|
||||
report['after_webengine_surface']={'alpha':window.windowHandle().format().alphaBufferSize(),
|
||||
'translucent':window.testAttribute(Qt.WidgetAttribute.WA_TranslucentBackground),
|
||||
'acrylic':window.property('acrylicEnabled'),'contrast':window.property('highContrastFallback')}
|
||||
background.phase=0;background.update();capture(f'roundtrip-{index}-a')
|
||||
background.phase=1;background.update();capture(f'roundtrip-{index}-b')
|
||||
delta=ImageChops.difference(captures[f'roundtrip-{index}-a'],captures[f'roundtrip-{index}-b'])
|
||||
report[f'roundtrip_{index}_sidebar_difference']=mean(delta.crop(sidebar))
|
||||
report[f'roundtrip_{index}_table_difference']=mean(delta.crop(table))
|
||||
(out/'roundtrip-probe.json').write_text(json.dumps(report,indent=2),encoding='utf8')
|
||||
assert report[f'roundtrip_{index}_sidebar_difference']>2,report
|
||||
assert report[f'roundtrip_{index}_table_difference']<1.5,report
|
||||
if args.dsh_switch:
|
||||
from PySide6.QtTest import QTest
|
||||
QTest.mouseClick(window.dsh_navigation,Qt.MouseButton.LeftButton)
|
||||
deadline=time.monotonic()+120
|
||||
while time.monotonic()<deadline:
|
||||
settle(.1)
|
||||
if window._dsh_view is not None and window._dsh_view._started:break
|
||||
assert window._dsh_view is not None and window._dsh_view._started,'DSH did not start'
|
||||
# READY precedes the Chromium load; require the real visible page.
|
||||
loaded=[]
|
||||
window._dsh_view.web.page().runJavaScript("document.body?.innerText?.includes('DSH助手')",loaded.append)
|
||||
deadline=time.monotonic()+60
|
||||
while time.monotonic()<deadline:
|
||||
settle(.1)
|
||||
if loaded and loaded[-1]:break
|
||||
if loaded:
|
||||
loaded.clear()
|
||||
window._dsh_view.web.page().runJavaScript("document.body?.innerText?.includes('DSH助手')",loaded.append)
|
||||
assert loaded and loaded[-1],'Official DSH UI did not load'
|
||||
capture('dsh-before-return')
|
||||
QTest.mouseClick(window.task_center_navigation,Qt.MouseButton.LeftButton)
|
||||
settle(.5)
|
||||
background.phase=0;background.update();capture('returned-backdrop-a')
|
||||
background.phase=1;background.update();capture('returned-backdrop-b')
|
||||
returned=ImageChops.difference(captures['returned-backdrop-a'],captures['returned-backdrop-b'])
|
||||
assert report['surfaces']['native-app-backdrop-a']['hwnd']==report['surfaces']['returned-backdrop-a']['hwnd'],report
|
||||
report['after_dsh_sidebar_difference']=mean(returned.crop(sidebar))
|
||||
report['after_dsh_header_difference']=mean(returned.crop(header))
|
||||
report['after_dsh_table_difference']=mean(returned.crop(table))
|
||||
(out/'surface-probe.json').write_text(json.dumps(report,indent=2),encoding='utf8')
|
||||
assert report['after_dsh_sidebar_difference']>2,report
|
||||
assert report['after_dsh_header_difference']>2,report
|
||||
assert report['after_dsh_table_difference']<1.5,report
|
||||
report['real_dsh_switch_restored_acrylic']=True
|
||||
window.plan_panel.find_text.setText('RV');window.plan_panel.find()
|
||||
report['visible_search_feedback']=window.plan_panel.find_result.isVisible()
|
||||
assert '2 / 6'in window.plan_panel.find_result.text()
|
||||
@@ -130,18 +196,39 @@ def main():
|
||||
assert report['empty_search_feedback']
|
||||
capture('native-app-empty-search')
|
||||
window.plan_panel.clear_find();window.plan_panel.table.clearSelection()
|
||||
task_id=window._task_id
|
||||
window.task_list.filter.setCurrentIndex(window.task_list.filter.findData('completed'))
|
||||
assert window.task_list.list.count()==0 and window._task_id==task_id
|
||||
capture('native-app-completed-category')
|
||||
window.task_list.filter.setCurrentIndex(window.task_list.filter.findData('unfinished'))
|
||||
assert window.task_list.list.count()==len(tasks)
|
||||
window.task_list.filter.setCurrentIndex(window.task_list.filter.findData('all'))
|
||||
assert window.task_list.current_task_id()==task_id
|
||||
report['status_categories']=True
|
||||
window.reduced_transparency_button.setChecked(True);capture('native-app-solid')
|
||||
assert not window.property('acrylicEnabled')
|
||||
solid=captures['native-app-solid']
|
||||
report['solid_sidebar_mean']=ImageStat.Stat(solid.crop(sidebar)).mean
|
||||
assert min(report['solid_sidebar_mean'])>200,report
|
||||
report['solid_fallback']=True
|
||||
if args.webengine_roundtrip:
|
||||
window.dsh_action.trigger();settle(.3)
|
||||
window.task_center_action.trigger();settle(.3)
|
||||
assert not window.property('acrylicEnabled')
|
||||
background.phase=0;background.update();capture('roundtrip-solid-a')
|
||||
background.phase=1;background.update();capture('roundtrip-solid-b')
|
||||
delta=ImageChops.difference(captures['roundtrip-solid-a'],captures['roundtrip-solid-b'])
|
||||
report['roundtrip_reduced_sidebar_difference']=mean(delta.crop(sidebar))
|
||||
assert report['roundtrip_reduced_sidebar_difference']<1.5,report
|
||||
window.reduced_transparency_button.setChecked(False);settle(.3)
|
||||
window.resize(980,700);capture('native-app-compact')
|
||||
report['compact_size']=[window.width(),window.height()]
|
||||
report['compact_table_height']=window.plan_panel.table.viewport().height()
|
||||
assert report['compact_size']==[980,700]
|
||||
assert report['compact_table_height']>=100
|
||||
assert window.task_list.filter.isVisible()
|
||||
point=window.task_list.filter.mapTo(window,window.task_list.filter.rect().bottomRight())
|
||||
assert 0<=point.x()<window.width() and 0<=point.y()<window.height()
|
||||
# Preview UI gates with an owned StopControl; never dispatch a worker.
|
||||
window._begin_run(tasks[0].task_id);window._update_gates();settle(.2)
|
||||
report['force_stop_direct']=window.run_panel.force_stop_button.isVisible()
|
||||
@@ -156,9 +243,50 @@ def main():
|
||||
assert report['force_stop_available_while_pausing']
|
||||
capture('native-app-pausing-controls')
|
||||
window._finish_run();window._update_gates()
|
||||
if args.history:
|
||||
from PySide6.QtCore import QTimer
|
||||
from PySide6.QtTest import QTest
|
||||
from PySide6.QtWidgets import QMessageBox
|
||||
window.history_action.trigger()
|
||||
panel=window.history_panel
|
||||
def wait_history():
|
||||
deadline=time.monotonic()+10
|
||||
while time.monotonic()<deadline:
|
||||
settle(.05)
|
||||
if (not panel._query.busy and not panel._clear_query.busy and panel._worker is None
|
||||
and not panel._search_timer.isActive()):return
|
||||
raise AssertionError('History operation did not finish')
|
||||
wait_history()
|
||||
assert panel.table.rowCount()>0
|
||||
capture('native-app-history')
|
||||
panel.search.setText('4100-CA0000-T0');wait_history()
|
||||
assert panel.table.rowCount()>0
|
||||
panel.table.selectRow(0)
|
||||
assert '4100-CA0000-T0' in panel.details.toPlainText()
|
||||
capture('native-app-history-search')
|
||||
panel.search.clear();wait_history()
|
||||
def confirm_history():
|
||||
modal=QApplication.activeModalWidget()
|
||||
if not isinstance(modal,QMessageBox):
|
||||
QTimer.singleShot(100,confirm_history);return
|
||||
modal.grab().save(str(out/'native-app-history-clear-confirm.png'))
|
||||
QTest.mouseClick(modal.button(QMessageBox.StandardButton.Yes),Qt.MouseButton.LeftButton)
|
||||
QTimer.singleShot(100,confirm_history)
|
||||
assert panel.clear_button.isEnabled(),{'status':panel.status.text(),'mutation_enabled':panel._mutations_enabled}
|
||||
panel.clear_button.click();wait_history()
|
||||
assert panel.table.rowCount()==0,{'status':panel.status.text(),'clear_success':panel._clear_success,
|
||||
'mutation_enabled':panel._mutations_enabled,'rows':panel.table.rowCount()}
|
||||
assert len(service.tasks())==2
|
||||
capture('native-app-history-cleared')
|
||||
report['history_search_clear_and_task_preservation']=True
|
||||
window.task_center_action.trigger();settle(.2)
|
||||
window.project_options_button.setChecked(True);settle(.2)
|
||||
report['options_exposed']=window.run_panel.copy_value_check.isVisible()
|
||||
window.plan_panel.discard_all();window.close();settle(.3);store.close();logging.shutdown()
|
||||
window.plan_panel.discard_all();window.close()
|
||||
deadline=time.monotonic()+15
|
||||
while window.isVisible() and time.monotonic()<deadline:settle(.1)
|
||||
assert not window.isVisible(),'Window did not finish shutdown'
|
||||
settle(.3);store.close();logging.shutdown()
|
||||
background.close();app.processEvents()
|
||||
(out/'acceptance.json').write_text(json.dumps(report,ensure_ascii=False,indent=2)+'\n',encoding='utf-8')
|
||||
print(json.dumps(report,ensure_ascii=True))
|
||||
|
||||
@@ -32,6 +32,7 @@ def main():
|
||||
parser.add_argument('--port', type=int, default=9227)
|
||||
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('--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')
|
||||
@@ -146,6 +147,8 @@ def main():
|
||||
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.outbox_only:
|
||||
command.append('--outbox-only')
|
||||
state['child'] = subprocess.Popen(command, cwd=REPO, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
"""Create the ZIP and 7-Zip self-extracting artifacts for a release."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import zipfile
|
||||
|
||||
from self_extracting_release import build_sfx
|
||||
|
||||
|
||||
def _sha256(path: Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as stream:
|
||||
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _zip_tree(source: Path, output: Path) -> int:
|
||||
files = 0
|
||||
with zipfile.ZipFile(output, "x", compression=zipfile.ZIP_DEFLATED, compresslevel=6, allowZip64=True) as archive:
|
||||
for path in sorted(source.rglob("*")):
|
||||
if path.is_file():
|
||||
archive.write(path, path.relative_to(source.parent).as_posix())
|
||||
files += 1
|
||||
return files
|
||||
|
||||
|
||||
def _stage_file(source, destination):
|
||||
# Release input is immutable during packaging. Same-volume links avoid a
|
||||
# second copy of tens of thousands of runtime files; archives are independent.
|
||||
try:
|
||||
os.link(source, destination)
|
||||
except OSError:
|
||||
shutil.copyfile(source, destination)
|
||||
return destination
|
||||
|
||||
|
||||
def build_release(source_dir: Path, output_dir: Path, *, version: str, seven_zip: Path, sfx_stub: Path,
|
||||
license_path: Path | None = None) -> dict:
|
||||
source_dir = Path(source_dir).resolve(strict=True)
|
||||
output_dir = Path(output_dir).resolve()
|
||||
if not source_dir.is_dir():
|
||||
raise ValueError("source_dir must be a directory")
|
||||
if output_dir.is_relative_to(source_dir) or source_dir.is_relative_to(output_dir):
|
||||
raise ValueError('Output must not be inside or contain the source directory')
|
||||
if (source_dir / 'data').exists():
|
||||
raise ValueError('Release input must not include user data')
|
||||
if not re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z0-9.]+)?', version):
|
||||
raise ValueError('Invalid release version')
|
||||
seven_zip = Path(seven_zip).resolve(strict=True)
|
||||
sfx_stub = Path(sfx_stub).resolve(strict=True)
|
||||
output_dir.mkdir(parents=True, exist_ok=False)
|
||||
|
||||
staging = output_dir / "staging" / "DxPartReplace"
|
||||
shutil.copytree(source_dir, staging, copy_function=_stage_file)
|
||||
if license_path is not None:
|
||||
license_target = staging / "licenses" / "7-Zip-LICENSE.txt"
|
||||
license_target.parent.mkdir(parents=True, exist_ok=True)
|
||||
# Break a possible staging hardlink before replacing a pre-existing notice.
|
||||
license_target.unlink(missing_ok=True)
|
||||
shutil.copyfile(Path(license_path).resolve(strict=True), license_target)
|
||||
|
||||
archive_7z = output_dir / f"DxPartReplace-{version}.7z"
|
||||
subprocess.run([
|
||||
str(seven_zip), "a", "-t7z", "-mx=3", "-m0=LZMA2", "-mmt=4", "-ms=64m",
|
||||
str(archive_7z), "DxPartReplace"
|
||||
], cwd=staging.parent, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
|
||||
timeout=3600)
|
||||
|
||||
zip_path = output_dir / f"DxPartReplace-{version}.zip"
|
||||
file_count = _zip_tree(staging, zip_path)
|
||||
sfx_path = output_dir / f"DxPartReplace-{version}.exe"
|
||||
build_sfx(sfx_stub, archive_7z, sfx_path)
|
||||
archive_7z.unlink()
|
||||
|
||||
manifest = {
|
||||
"version": version,
|
||||
"file_count": file_count,
|
||||
"artifacts": {
|
||||
zip_path.name: {"bytes": zip_path.stat().st_size, "sha256": _sha256(zip_path)},
|
||||
sfx_path.name: {"bytes": sfx_path.stat().st_size, "sha256": _sha256(sfx_path)},
|
||||
},
|
||||
"extractor": {
|
||||
"kind": "7zip-sfx",
|
||||
"stub": sfx_stub.name,
|
||||
"stub_sha256": _sha256(sfx_stub),
|
||||
"compressor": seven_zip.name,
|
||||
"compressor_sha256": _sha256(seven_zip),
|
||||
"version_output": subprocess.check_output([str(seven_zip)], text=True, errors='replace')[:500],
|
||||
"settings": "LZMA2 level 3, 4 threads, 64 MiB solid blocks",
|
||||
},
|
||||
"files": {path.relative_to(staging.parent).as_posix(): _sha256(path)
|
||||
for path in sorted(staging.rglob('*')) if path.is_file()},
|
||||
}
|
||||
(output_dir / f"release-manifest-{version}.json").write_text(
|
||||
json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
|
||||
)
|
||||
(output_dir / f"SHA256SUMS-{version}.txt").write_text(
|
||||
"".join(f"{item['sha256']} {name}\n" for name, item in manifest["artifacts"].items()),
|
||||
encoding="ascii",
|
||||
)
|
||||
return manifest
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--source-dir", type=Path, required=True)
|
||||
parser.add_argument("--output-dir", type=Path, required=True)
|
||||
parser.add_argument("--version", required=True)
|
||||
parser.add_argument("--seven-zip", type=Path, required=True)
|
||||
parser.add_argument("--sfx-stub", type=Path, required=True)
|
||||
parser.add_argument("--license", type=Path)
|
||||
args = parser.parse_args()
|
||||
manifest = build_release(args.source_dir, args.output_dir, version=args.version,
|
||||
seven_zip=args.seven_zip, sfx_stub=args.sfx_stub,
|
||||
license_path=args.license)
|
||||
print(json.dumps(manifest, ensure_ascii=False, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -132,7 +132,9 @@ def run_scenario(output: Path, *, projects=3, sheets=2, rules=2, behavior='succe
|
||||
stop.clear()
|
||||
result = measured('queue_resume_wall', lambda: execute_task_queue(service, ids, stop_event=stop, queue_id=result.queue_id))
|
||||
expected_steps = projects * sheets * rules
|
||||
assert len(calls) == (1 if behavior == 'uncertain_stop' else expected_steps)
|
||||
expected_calls = (1 if behavior == 'uncertain_stop' else
|
||||
1 + (projects-1)*sheets*rules if behavior == 'failure_continue' else expected_steps)
|
||||
assert len(calls) == expected_calls
|
||||
if behavior == 'uncertain_stop':
|
||||
assert result.paused and result.entries[0].state == 'blocked'
|
||||
assert all(entry.state == 'pending' for entry in result.entries[1:])
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
"""Build a 7-Zip SFX wrapper around a prepared .7z release archive."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
|
||||
|
||||
def build_sfx(
|
||||
stub: Path,
|
||||
archive: Path,
|
||||
output: Path,
|
||||
) -> Path:
|
||||
"""Use the official 7z.sfx extractor (not the temporary installer stub).
|
||||
|
||||
Its native dialog selects a persistent destination. No installer config,
|
||||
registry modifications, temporary extraction or automatic launch is needed.
|
||||
"""
|
||||
stub = Path(stub)
|
||||
archive = Path(archive)
|
||||
output = Path(output)
|
||||
if not stub.is_file() or not archive.is_file():
|
||||
raise FileNotFoundError("SFX stub and archive must both exist")
|
||||
if output.exists():
|
||||
raise FileExistsError(output)
|
||||
with stub.open('rb') as stream:
|
||||
if stream.read(2) != b'MZ':
|
||||
raise ValueError('Expected Windows SFX executable')
|
||||
with archive.open('rb') as stream:
|
||||
if stream.read(6) != b'7z\xbc\xaf\x27\x1c':
|
||||
raise ValueError('Expected 7z archive')
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
temporary = output.with_name(f".{output.name}.{os.getpid()}.tmp")
|
||||
try:
|
||||
with temporary.open("xb") as target:
|
||||
for source in (stub, archive):
|
||||
with source.open('rb') as stream:
|
||||
shutil.copyfileobj(stream, target, 1024 * 1024)
|
||||
target.flush()
|
||||
os.fsync(target.fileno())
|
||||
os.replace(temporary, output)
|
||||
finally:
|
||||
temporary.unlink(missing_ok=True)
|
||||
return output
|
||||
@@ -0,0 +1,69 @@
|
||||
/** Real settings form, provider discovery and persistence against an owned fixture. */
|
||||
import assert from 'node:assert/strict';
|
||||
import {createServer} from 'node:http';
|
||||
import path from 'node:path';
|
||||
|
||||
export async function verifyModelSettings(c, output) {
|
||||
const requests=[];
|
||||
const server=createServer((req,res)=>{
|
||||
requests.push({method:req.method,path:req.url});
|
||||
res.writeHead(req.url==='/v1/models'?200:404,{'Content-Type':'application/json'});
|
||||
res.end(JSON.stringify({object:'list',data:[{id:'issue16-fixture-model',object:'model',owned_by:'acceptance'}]}));
|
||||
});
|
||||
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
|
||||
const endpoint=`http://127.0.0.1:${server.address().port}/v1`;
|
||||
const visible=selector=>`[...document.querySelectorAll(${JSON.stringify(selector)})].find(e=>e.getBoundingClientRect().width>0)`;
|
||||
const button=text=>`[...document.querySelectorAll('button')].find(e=>e.getBoundingClientRect().width>0&&e.textContent.trim()===${JSON.stringify(text)})`;
|
||||
async function wait(expression){
|
||||
const end=Date.now()+20000;
|
||||
while(Date.now()<end){if(await c.evaluate(expression))return;await new Promise(r=>setTimeout(r,100));}
|
||||
throw Error('Models acceptance timed out: '+expression);
|
||||
}
|
||||
async function type(selector,text){
|
||||
await c.click(visible(selector));
|
||||
await c.call('Input.insertText',{text});
|
||||
await wait(`(${visible(selector)})?.value===${JSON.stringify(text)}`);
|
||||
}
|
||||
try{
|
||||
// The caller has reached the actual upstream Custom model API form.
|
||||
await type('input[aria-label="Provider ID"]','issue16-fixture');
|
||||
await type('input[aria-label="API 地址"]',endpoint);
|
||||
const protocol='select[aria-label="API 协议"]';
|
||||
// This is a native keyboard selection, not a direct settings RPC.
|
||||
const choices=await c.evaluate(`[...(${visible(protocol)}).options].map(x=>x.value)`);
|
||||
const target=choices.indexOf('openai-completions');
|
||||
assert.ok(target>=0);
|
||||
await c.click(visible(protocol));
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyDown',key:'Home',code:'Home',windowsVirtualKeyCode:36});
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'Home',code:'Home',windowsVirtualKeyCode:36});
|
||||
for(let i=0;i<target;i++){
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyDown',key:'ArrowDown',code:'ArrowDown',windowsVirtualKeyCode:40});
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'ArrowDown',code:'ArrowDown',windowsVirtualKeyCode:40});
|
||||
}
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyDown',key:'Enter',code:'Enter',windowsVirtualKeyCode:13});
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'Enter',code:'Enter',windowsVirtualKeyCode:13});
|
||||
await wait(`(${visible(protocol)}).value==='openai-completions'`);
|
||||
await c.click(button('获取可用模型'));
|
||||
await wait(`document.body.innerText.includes('issue16-fixture-model')`);
|
||||
assert.deepEqual(requests,[{method:'GET',path:'/v1/models'}]);
|
||||
const candidate=`[...document.querySelectorAll('[role="dialog"] label')].find(e=>e.textContent.includes('issue16-fixture-model'))?.querySelector('input[type="checkbox"]')`;
|
||||
if(!await c.evaluate(`(${candidate})?.checked`))await c.click(candidate);
|
||||
assert.equal(await c.evaluate(`(${candidate})?.checked`),true);
|
||||
await c.click(button('添加所选'));
|
||||
await wait(`!![...document.querySelectorAll('input')].find(x=>x.value==='issue16-fixture-model')`);
|
||||
await c.screenshot(path.join(output,'custom-model-discovery.png'));
|
||||
await c.click(button('创建提供商'));
|
||||
await wait(`!document.querySelector('input[aria-label="Provider ID"]')`);
|
||||
await wait(`document.body.innerText.includes('issue16-fixture')`);
|
||||
// Reopen the real editor to read committed fields, not the submitted draft.
|
||||
await c.click(`[...document.querySelectorAll('li')].find(e=>e.textContent.includes('issue16-fixture'))?.querySelector('button')`);
|
||||
await wait(`document.body.innerText.includes('自定义设置')`);
|
||||
await c.click(`[...document.querySelectorAll('summary')].find(e=>e.textContent.includes('自定义设置'))`);
|
||||
await wait(`!![...document.querySelectorAll('input')].find(x=>x.value==='issue16-fixture-model')`);
|
||||
await c.screenshot(path.join(output,'custom-model-persisted.png'));
|
||||
return {status:'passed',provider:'issue16-fixture',model:'issue16-fixture-model',requests,
|
||||
credentialsUsed:false,remoteModelCalls:false};
|
||||
}finally{
|
||||
server.closeAllConnections();await new Promise(resolve=>server.close(resolve));
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,21 @@ try {
|
||||
assert.equal(await c.evaluate(`${accountEntries}.length`),0);
|
||||
report.checks.push('one click opens native tool settings with DSH capabilities and no account entry');
|
||||
await c.screenshot(path.join(output,'dsh-tool-settings.png'));
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyDown',key:'Escape',code:'Escape'});
|
||||
await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'Escape',code:'Escape'});
|
||||
await wait(`!document.querySelector('[data-testid="dx-tool-settings-panel"]')`);
|
||||
await c.click(`[...document.querySelectorAll('[data-shortcut-modal="settings"] nav button')].find(e=>e.textContent.trim()==='模型')`);
|
||||
await wait(`document.body.innerText.includes('自定义模型 API')||document.body.innerText.includes('添加模型提供商')`);
|
||||
if(!await c.evaluate(`document.body.innerText.includes('自定义模型 API')`))
|
||||
await c.click(`[...document.querySelectorAll('button')].find(e=>e.textContent.includes('添加模型提供商'))`);
|
||||
await wait(`document.body.innerText.includes('自定义模型 API')`);
|
||||
await c.click(`[...document.querySelectorAll('button')].find(e=>e.textContent.trim()==='自定义模型 API')`);
|
||||
await wait(`!!document.querySelector('input[aria-label="Provider ID"]')&&document.body.innerText.includes('获取可用模型')`);
|
||||
report.checks.push('actual model settings clicks reveal custom API and fetch-model controls');
|
||||
await c.screenshot(path.join(output,'dsh-custom-model-settings.png'));
|
||||
if(process.argv.includes('--model-settings-check')){
|
||||
const {verifyModelSettings}=await import('./verify_dsh_model_settings.mjs');
|
||||
report.modelSettings=await verifyModelSettings(c,output);
|
||||
}
|
||||
await c.click(`[...document.querySelectorAll('[data-shortcut-modal="settings"] button')].find(e=>e.textContent.trim()==='关闭')`);
|
||||
await wait(`!document.querySelector('[role="dialog"]')`);
|
||||
await c.screenshot(path.join(output,'dsh-tool-home.png'));
|
||||
const context=await c.evaluate(`fetch('/dx/context').then(r=>r.json())`);
|
||||
assert.ok(context.task_id,'Select a task in the task center before running this test.');
|
||||
@@ -92,7 +104,7 @@ try {
|
||||
report.checks.push('real API reply rendered through the official Harness conversation');
|
||||
await c.click(`document.querySelector('[contenteditable="true"]')`);
|
||||
await c.call('Input.insertText',{text:'请分析当前任务的下一步。'});
|
||||
} else report.reason='No model credential configured in this isolated acceptance profile.';
|
||||
} else report.reason='Live model generation was not requested for this acceptance run.';
|
||||
await c.screenshot(path.join(output,'dsh-native-final.png'));
|
||||
if(process.argv.includes('--confirm-cancel')) {
|
||||
await c.click(`[...document.querySelectorAll('button')].find(e=>e.textContent.trim()==='准备失败项重试')`);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/** Boot the actual Harness over native user settings and a legacy profile. */
|
||||
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';
|
||||
|
||||
const output=path.resolve('build/dsh-profile-settings-verification');
|
||||
await mkdir(output,{recursive:true});
|
||||
const base=await mkdtemp(path.join(output,'run-'));
|
||||
process.env.DX_DSH_HOME=path.join(base,'home');
|
||||
process.env.DX_DSH_CONTEXT_FILE=path.join(base,'context.json');
|
||||
await writeFile(process.env.DX_DSH_CONTEXT_FILE,JSON.stringify({task_id:'',revision:'settings-test',status:'observe'}));
|
||||
const dir=path.join(process.env.DX_DSH_HOME,'profiles/dx-task');
|
||||
await mkdir(dir,{recursive:true});
|
||||
const profile=[{id:'llm-pi-ai',config:{providers:{'profile-fixture':{
|
||||
api:'openai-completions',baseURL:'http://127.0.0.1:1/v1',models:[{id:'persisted-fixture-model'}]}}}}];
|
||||
const patch=path.join(dir,'cordis.patch.yml');
|
||||
const original='# Native user settings\n'+JSON.stringify(profile,null,2)+'\n';
|
||||
await writeFile(patch,original);
|
||||
await prepareProfileDirectory(dir);await prepareProfileDirectory(dir);
|
||||
assert.equal(await readFile(patch,'utf8'),original,'application preparation must not rewrite a native user layer');
|
||||
const legacyDir=path.join(base,'legacy');
|
||||
await mkdir(legacyDir,{recursive:true});
|
||||
const old=JSON.stringify([...overlay(),...profile],null,2);
|
||||
await writeFile(path.join(legacyDir,'cordis.patch.yml'),old);
|
||||
await prepareProfileDirectory(legacyDir);
|
||||
const migrated=await readFile(path.join(legacyDir,'cordis.patch.yml'),'utf8');
|
||||
assert.ok(JSON.parse(migrated).some(x=>x.id==='llm-pi-ai'&&x.config.providers['profile-fixture']));
|
||||
const backups=(await readdir(legacyDir)).filter(x=>x.includes('.legacy-'));
|
||||
assert.equal(backups.length,1);
|
||||
assert.equal(await readFile(path.join(legacyDir,backups[0]),'utf8'),old);
|
||||
await prepareProfileDirectory(legacyDir);
|
||||
assert.equal(await readFile(path.join(legacyDir,'cordis.patch.yml'),'utf8'),migrated);
|
||||
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);
|
||||
console.log(JSON.stringify({status:'passed',nativeSettingsPreserved:true,actualProviderRestored:true,
|
||||
legacyMigrationPreservedModels:true,byteBackup:true,modelRequests:0}));
|
||||
}finally{await shutdown.shutdown(0);}
|
||||
@@ -0,0 +1,200 @@
|
||||
/** Real Harness requests must keep each provider endpoint, model and credential together. */
|
||||
import assert from 'node:assert/strict';
|
||||
import {createServer} from 'node:http';
|
||||
import {spawn} from 'node:child_process';
|
||||
import {mkdir,mkdtemp,readFile,writeFile,readdir} from 'node:fs/promises';
|
||||
import {createRequire} from 'node:module';
|
||||
import path from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
import {bootRuntime,overlay,prepareProfileDirectory} from '../integration/dsh/start.mjs';
|
||||
|
||||
const script=fileURLToPath(import.meta.url);
|
||||
const require=createRequire(new URL('../integration/dsh/start.mjs',import.meta.url));
|
||||
const {load}=require('js-yaml');
|
||||
const appKeys=['DSH_API_BASE','DSH_MODEL','DSH_API_KEY','DX_DSH_MODELS','DSH_HOME','DX_DSH_HOME','DX_DSH_CONTEXT_FILE','DX_DSH_PARENT_PIPE'];
|
||||
const selection=ctx=>ctx.agentDefaultModel.currentSelection();
|
||||
|
||||
async function runChild(specPath,env){
|
||||
return new Promise(resolve=>{
|
||||
const child=spawn(process.execPath,[script,'--child',specPath],{env,stdio:['ignore','pipe','pipe'],windowsHide:true});
|
||||
let stdout='',stderr='';child.stdout.on('data',x=>stdout+=x);child.stderr.on('data',x=>stderr+=x);
|
||||
const timeout=setTimeout(()=>child.kill(),60000);
|
||||
child.on('close',code=>{clearTimeout(timeout);resolve({code,stdout,stderr});});
|
||||
});
|
||||
}
|
||||
|
||||
async function stream(ctx,provider,model){
|
||||
const chunks=[];
|
||||
for await(const chunk of ctx.llm.stream({provider,model,messages:[{role:'user',content:[{type:'text',text:'fixture request'}]}],
|
||||
maxTokens:8,signal:AbortSignal.timeout(15000)}))chunks.push(chunk);
|
||||
assert.equal(chunks.find(x=>x.type==='finish')?.reason.kind,'stop','owned fixture stream must finish successfully');
|
||||
assert.equal(chunks.filter(x=>x.type==='text-delta').map(x=>x.text).join(''),'ok');
|
||||
}
|
||||
|
||||
if(process.argv[2]==='--child'){
|
||||
const spec=JSON.parse(await readFile(process.argv[3],'utf8'));
|
||||
if(spec.orphanKey){
|
||||
await assert.rejects(bootRuntime({announce:false}),/Stored DSH_API_KEY has no provider binding|Duplicate .* prevents verifying model binding|Application model route did not match/);
|
||||
console.log('DX_SWITCH_REPORT '+JSON.stringify({status:'passed',unboundCredentialRefused:true}));
|
||||
process.exit(0);
|
||||
}
|
||||
const {ctx,shutdown}=await bootRuntime({announce:false});
|
||||
try{
|
||||
if(spec.hostAbsent){
|
||||
assert.ok(!ctx.llm.listProviders().some(x=>x.id==='dx'));
|
||||
await stream(ctx,'custom','custom-model');
|
||||
console.log('DX_SWITCH_REPORT '+JSON.stringify({status:'passed',customWorksWithoutHost:true}));
|
||||
}else{
|
||||
// Send even when the model catalogue is stale: detect the actual HTTP credential destination.
|
||||
const dxModels=(await ctx.llm.listModels('dx')).map(x=>x.id);
|
||||
await stream(ctx,'dx',dxModels[0]);
|
||||
if(spec.custom){
|
||||
assert.deepEqual((await ctx.llm.listModels('custom')).map(x=>x.id),['custom-model']);
|
||||
await stream(ctx,'custom','custom-model');
|
||||
}
|
||||
assert.deepEqual(dxModels,spec.customDx?['custom-dx-model']:['new-model','alternate-model']);
|
||||
assert.deepEqual(selection(ctx),spec.expectedDefault);
|
||||
console.log('DX_SWITCH_REPORT '+JSON.stringify({status:'passed',dxModels,selection:selection(ctx)}));
|
||||
}
|
||||
}finally{await shutdown.shutdown(0);}
|
||||
}else{
|
||||
const output=path.resolve('build/dsh-provider-switch-verification');await mkdir(output,{recursive:true});
|
||||
const base=await mkdtemp(path.join(output,'run-'));
|
||||
const requests=[];
|
||||
async function endpoint(name){
|
||||
const server=createServer(async(req,res)=>{
|
||||
let text='';for await(const chunk of req)text+=chunk;
|
||||
const body=text?JSON.parse(text):{};
|
||||
requests.push({server:name,path:req.url,authorization:req.headers.authorization,model:body.model});
|
||||
res.writeHead(200,{'Content-Type':'text/event-stream'});
|
||||
res.end('data: '+JSON.stringify({id:'fixture',object:'chat.completion.chunk',created:1,model:body.model,
|
||||
choices:[{index:0,delta:{role:'assistant',content:'ok'},finish_reason:null}]})+'\n\n'+
|
||||
'data: '+JSON.stringify({id:'fixture',object:'chat.completion.chunk',created:1,model:body.model,
|
||||
choices:[{index:0,delta:{},finish_reason:'stop'}]})+'\n\ndata: [DONE]\n\n');
|
||||
});
|
||||
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));
|
||||
return {server,url:`http://127.0.0.1:${server.address().port}/v1`};
|
||||
}
|
||||
const old=await endpoint('old'),next=await endpoint('new'),custom=await endpoint('custom');
|
||||
const cleanEnv={...process.env};for(const key of appKeys)delete cleanEnv[key];
|
||||
const nextEnv={DSH_API_BASE:next.url,DSH_MODEL:'new-model',DSH_API_KEY:'fixture-new-key',DX_DSH_MODELS:'["new-model","alternate-model"]'};
|
||||
const cases=[
|
||||
{name:'legacy-switch',legacy:true},
|
||||
{name:'legacy-custom-default',legacy:true,custom:true,expectedDefault:{provider:'custom',model:'custom-model'}},
|
||||
{name:'already-migrated-switch',custom:true},
|
||||
{name:'native-custom-provider',custom:true,omitDx:true},
|
||||
{name:'native-dx-selection',expectedDefault:{provider:'dx',model:'alternate-model'},selected:'alternate-model'},
|
||||
{name:'native-independent-dx',customDx:true,expectedDefault:{provider:'dx',model:'custom-dx-model'}},
|
||||
{name:'native-stored-dx-key',customDx:true,storedHostKey:true,expectedDefault:{provider:'dx',model:'custom-dx-model'}},
|
||||
{name:'legacy-stored-dx-key',legacy:true,customDx:true,storedHostKey:true,expectedDefault:{provider:'dx',model:'custom-dx-model'}},
|
||||
{name:'native-key-only-edit',customDx:true,storedHostKey:true,keyOnly:true,expectedDefault:{provider:'dx',model:'custom-dx-model'}},
|
||||
{name:'unbound-stored-key',storedHostKey:true,orphanKey:true},
|
||||
{name:'ignored-name-binding',storedHostKey:true,orphanKey:true,ignoredName:true},
|
||||
{name:'legacy-ignored-name-binding',legacy:true,storedHostKey:true,orphanKey:true,ignoredName:true},
|
||||
{name:'insert-is-not-a-binding',storedHostKey:true,orphanKey:true,insertOnly:true},
|
||||
{name:'alternate-unbound-key',alternateCredentials:true,storedHostKey:true,orphanKey:true},
|
||||
{name:'alternate-bound-key',alternateCredentials:true,customDx:true,storedHostKey:true,expectedDefault:{provider:'dx',model:'custom-dx-model'}},
|
||||
{name:'duplicate-credentials',storedHostKey:true,orphanKey:true,duplicate:'credentials'},
|
||||
{name:'duplicate-model-adapter',orphanKey:true,duplicate:'llm-pi-ai'},
|
||||
];
|
||||
const reports=[];
|
||||
try{
|
||||
for(const spec of cases){
|
||||
if(process.argv[2]==='--case'&&process.argv[3]!==spec.name)continue;
|
||||
const home=path.join(base,spec.name),dir=path.join(home,'profiles/dx-task');await mkdir(dir,{recursive:true});
|
||||
const credentialsFile=path.join(home,spec.alternateCredentials?'alternate-credentials.yaml':'.credentials.yaml');
|
||||
Object.assign(process.env,{DSH_API_BASE:old.url,DSH_MODEL:'old-model',DSH_API_KEY:'fixture-old-key',DX_DSH_MODELS:'["old-model"]'});
|
||||
let rows=overlay();
|
||||
if(!spec.legacy)rows=rows.filter(x=>['llm-pi-ai','agent-default-model'].includes(x.id));
|
||||
const llm=rows.find(x=>x.id==='llm-pi-ai');
|
||||
if(spec.omitDx)delete llm.config.providers.dx;
|
||||
if(spec.custom)llm.config.providers.custom={api:'openai-completions',baseURL:custom.url,apiKeyEnv:'FIXTURE_CUSTOM_KEY',models:[{id:'custom-model'}]};
|
||||
if(spec.customDx)llm.config.providers.dx={api:'openai-completions',baseURL:custom.url,
|
||||
apiKeyEnv:spec.storedHostKey?'DSH_API_KEY':'FIXTURE_CUSTOM_KEY',models:[{id:'custom-dx-model'}]};
|
||||
if(spec.keyOnly){
|
||||
rows=[];
|
||||
Object.assign(process.env,{DSH_API_BASE:custom.url,DSH_MODEL:'custom-dx-model',DX_DSH_MODELS:'["custom-dx-model"]'});
|
||||
}
|
||||
if(spec.orphanKey&&!spec.ignoredName&&!spec.insertOnly&&!spec.duplicate)rows=[];
|
||||
if(spec.ignoredName)llm.name='@deepseek-ai/previous-llm-name';
|
||||
if(spec.insertOnly)llm.insert=[];
|
||||
if(spec.alternateCredentials)rows.push({id:'credentials',config:{path:credentialsFile}});
|
||||
if(spec.duplicate)rows.push({insert:[{id:spec.duplicate,
|
||||
name:spec.duplicate==='credentials'?'@deepseek-ai/dsh-credentials-local':'@deepseek-ai/dsh-llm-pi-ai',
|
||||
config:spec.duplicate==='credentials'?{path:path.join(home,'empty-store.yaml')}:structuredClone(llm.config)}]});
|
||||
const defaultRow=rows.find(x=>x.id==='agent-default-model');
|
||||
if(defaultRow)defaultRow.config=spec.expectedDefault??{provider:'dx',model:spec.selected??'old-model'};
|
||||
const patch=path.join(dir,'cordis.patch.yml');
|
||||
if(spec.keyOnly){
|
||||
await writeFile(patch,'[]');await prepareProfileDirectory(dir);
|
||||
rows=load(await readFile(patch,'utf8'));
|
||||
assert.equal(rows.find(x=>x.id==='llm-pi-ai').config.providers.dx.baseURL,custom.url,
|
||||
'host route must be materialized before the official UI can save a key-only edit');
|
||||
}
|
||||
const original=JSON.stringify(rows,null,2);
|
||||
await writeFile(patch,original);
|
||||
const refs={FIXTURE_CUSTOM_KEY:'fixture-custom-key',...spec.storedHostKey?{DSH_API_KEY:'fixture-custom-key'}:{}};
|
||||
const credentials=JSON.stringify({version:1,refs});
|
||||
await writeFile(credentialsFile,credentials,{mode:0o600});
|
||||
const context=path.join(home,'context.json');await writeFile(context,'{"status":"observe","revision":"switch-fixture"}');
|
||||
spec.expectedDefault??={provider:'dx',model:'new-model'};
|
||||
const specPath=path.join(home,'spec.json');await writeFile(specPath,JSON.stringify(spec));
|
||||
const env={...cleanEnv,...nextEnv,DX_DSH_HOME:home,DX_DSH_CONTEXT_FILE:context};
|
||||
const firstRequest=requests.length;
|
||||
const result=await runChild(specPath,env);
|
||||
const calls=requests.slice(firstRequest);
|
||||
if(spec.orphanKey){
|
||||
assert.deepEqual(calls,[],'unbound stored key must not make requests');
|
||||
assert.equal(result.code,0,spec.name+': '+result.stderr);
|
||||
assert.equal(await readFile(patch,'utf8'),original,'refused profile must stay unchanged');
|
||||
reports.push({name:spec.name,status:'passed',unboundCredentialRefused:true});
|
||||
continue;
|
||||
}
|
||||
assert.equal(calls.length,spec.custom?2:1,spec.name+': request count; '+result.stderr);
|
||||
const dx=calls[0];
|
||||
assert.equal(dx.server,spec.customDx?'custom':'new',spec.name+': wrong server received the current key');
|
||||
assert.equal(dx.authorization,spec.customDx?'Bearer fixture-custom-key':'Bearer fixture-new-key',spec.name+': credential source');
|
||||
assert.equal(dx.model,spec.customDx?'custom-dx-model':'new-model');
|
||||
assert.equal(dx.path,'/v1/chat/completions');
|
||||
if(spec.custom)assert.deepEqual(calls[1],{server:'custom',path:'/v1/chat/completions',authorization:'Bearer fixture-custom-key',model:'custom-model'});
|
||||
assert.equal(result.code,0,spec.name+': '+result.stderr+'\n'+result.stdout);
|
||||
// Harness itself adds a browser-session record; model references must survive unchanged.
|
||||
assert.deepEqual(load(await readFile(credentialsFile,'utf8')).refs,
|
||||
refs,'stored provider credentials must be untouched');
|
||||
Object.assign(process.env,nextEnv);
|
||||
const migrated=await readFile(patch,'utf8');
|
||||
const credentialsAfterBoot=await readFile(credentialsFile,'utf8');
|
||||
await prepareProfileDirectory(dir);
|
||||
assert.equal(await readFile(credentialsFile,'utf8'),credentialsAfterBoot,'profile preparation must not rewrite credentials');
|
||||
assert.equal(await readFile(patch,'utf8'),migrated,'second boot must not rewrite the same profile');
|
||||
if(spec.legacy){
|
||||
const backup=(await readdir(dir)).find(x=>x.includes('.legacy-'));
|
||||
assert.equal(await readFile(path.join(dir,backup),'utf8'),original);
|
||||
}
|
||||
reports.push({name:spec.name,status:'passed',calls:calls.map(({authorization,...rest})=>({...rest,credentialMatched:true}))});
|
||||
}
|
||||
// Removing host configuration must not leave a stale application route backed by a new key later.
|
||||
if(process.argv[2]!=='--case'){
|
||||
const dir=path.join(base,'already-migrated-switch/profiles/dx-task');
|
||||
for(const key of ['DSH_API_BASE','DSH_API_KEY','DSH_MODEL','DX_DSH_MODELS'])delete process.env[key];
|
||||
await prepareProfileDirectory(dir);
|
||||
const cleared=load(await readFile(path.join(dir,'cordis.patch.yml'),'utf8'));
|
||||
assert.equal(cleared.find(x=>x.id==='llm-pi-ai')?.config.providers.dx,undefined);
|
||||
assert.ok(cleared.find(x=>x.id==='llm-pi-ai').config.providers.custom);
|
||||
assert.equal(cleared.find(x=>x.id==='agent-default-model'),undefined);
|
||||
const home=path.resolve(dir,'../..'),credentialsFile=path.join(home,'.credentials.yaml');
|
||||
const stored=load(await readFile(credentialsFile,'utf8'));stored.refs.DSH_API_KEY='fixture-orphan-key';
|
||||
await writeFile(credentialsFile,JSON.stringify(stored));
|
||||
const specPath=path.join(home,'no-host-spec.json');await writeFile(specPath,'{"hostAbsent":true}');
|
||||
const count=requests.length;
|
||||
const result=await runChild(specPath,{...cleanEnv,DX_DSH_HOME:home,DX_DSH_CONTEXT_FILE:path.join(home,'context.json')});
|
||||
assert.equal(result.code,0,'independent provider must work without host configuration: '+result.stderr);
|
||||
assert.deepEqual(requests.slice(count),[{server:'custom',path:'/v1/chat/completions',authorization:'Bearer fixture-custom-key',model:'custom-model'}]);
|
||||
reports.push({name:'no-host-with-unused-stored-key',status:'passed',calls:[{server:'custom',path:'/v1/chat/completions',model:'custom-model',credentialMatched:true}]});
|
||||
}
|
||||
const report={status:'passed',realHarness:true,externalModelCalls:false,cases:reports,hostRemoval:process.argv[2]!=='--case'};
|
||||
await writeFile(path.join(base,'report.json'),JSON.stringify(report,null,2));console.log(JSON.stringify(report));
|
||||
}finally{
|
||||
for(const {server} of [old,next,custom]){server.closeAllConnections();await new Promise(resolve=>server.close(resolve));}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Run the delivered SFX and verify its files and every ZIP member against SHA256."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import time
|
||||
import zipfile
|
||||
|
||||
|
||||
def digest(stream):
|
||||
return hashlib.file_digest(stream, 'sha256').hexdigest()
|
||||
|
||||
|
||||
def verify(manifest_path: Path, destination: Path):
|
||||
manifest = json.loads(manifest_path.read_text(encoding='utf-8'))
|
||||
root = manifest_path.resolve().parent
|
||||
destination = destination.resolve()
|
||||
if destination.exists():
|
||||
raise FileExistsError('Use a fresh extraction destination')
|
||||
for name, expected in manifest['artifacts'].items():
|
||||
with (root/name).open('rb') as stream:
|
||||
if digest(stream) != expected['sha256']:
|
||||
raise ValueError('Artifact checksum mismatch: '+name)
|
||||
version = manifest['version']
|
||||
started = time.monotonic()
|
||||
subprocess.run([str(root/f'DxPartReplace-{version}.exe'), '-y', '-o'+str(destination)],
|
||||
check=True, timeout=1800, creationflags=getattr(subprocess, 'CREATE_NO_WINDOW', 0))
|
||||
elapsed = time.monotonic()-started
|
||||
actual = {p.relative_to(destination).as_posix(): p for p in destination.rglob('*') if p.is_file()}
|
||||
if set(actual) != set(manifest['files']):
|
||||
raise ValueError('Self-extracted file set differs from manifest')
|
||||
for name, path in actual.items():
|
||||
with path.open('rb') as stream:
|
||||
if digest(stream) != manifest['files'][name]:
|
||||
raise ValueError('Self-extracted checksum mismatch: '+name)
|
||||
with zipfile.ZipFile(root/f'DxPartReplace-{version}.zip') as archive:
|
||||
if len(archive.namelist()) != len(actual) or set(archive.namelist()) != set(actual):
|
||||
raise ValueError('ZIP file set differs from manifest')
|
||||
# Reading to EOF also checks ZIP CRC for every member.
|
||||
for name in archive.namelist():
|
||||
with archive.open(name) as stream:
|
||||
if digest(stream) != manifest['files'][name]:
|
||||
raise ValueError('ZIP checksum mismatch: '+name)
|
||||
return dict(passed=True, files=len(actual), sfx_extraction_seconds=round(elapsed, 3),
|
||||
actual_sfx_executed=True, zip_crc_and_sha256=True,
|
||||
destination=str(destination))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('manifest', type=Path)
|
||||
parser.add_argument('destination', type=Path)
|
||||
parser.add_argument('--report', type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
result = verify(args.manifest, args.destination)
|
||||
args.report.write_text(json.dumps(result, ensure_ascii=False, indent=2)+'\n', encoding='utf-8')
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
Reference in new issue
Block a user