feat-ui-operation-history-and-durable-execution
Core regression / Python core and release configuration (push) Successful in 17s
Core regression / JavaScript syntax (push) Successful in 3s
Core regression / Python core and release configuration (pull_request) Successful in 16s
Core regression / JavaScript syntax (pull_request) Successful in 3s
Core regression / Python core and release configuration (push) Successful in 17s
Core regression / JavaScript syntax (push) Successful in 3s
Core regression / Python core and release configuration (pull_request) Successful in 16s
Core regression / JavaScript syntax (pull_request) Successful in 3s
This commit is contained in:
1 parent
4874c7e95a
commit
cf90ced825
44 files changed
+3165
-92
No files matched your search
@@ -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.
|
||||
@@ -1,6 +1,6 @@
|
||||
# 操作历史与防重复执行:详细执行逻辑
|
||||
|
||||
状态:设计细化,尚未实施。本文延续《operation-history-resume-research》,用户已明确重复执行场景为同一个任务继续或重启后;不修改 DSH 功能。
|
||||
状态:已实施,验证及兼容性决定见 `docs/testing/2026-11-03-operation-history-execution.md`。本文延续《operation-history-resume-research》,用户已明确重复执行场景为同一个任务继续或重启后;不修改 DSH 功能。
|
||||
|
||||
用户已确认:某个 CBB 失败后,只要已经证明没有残留改动且资源已释放,跳过本次失败,继续后面的工程;结果不确定或核验失败时仍停止。
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
# 操作历史与持久执行回执验收
|
||||
|
||||
范围: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。
|
||||
@@ -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 部分成功后的失败项重试会保留成功项。
|
||||
@@ -0,0 +1,23 @@
|
||||
# 操作历史与继续执行
|
||||
|
||||
点击顶部的“操作历史”(快捷键 `Ctrl+Shift+H`),查看任务、规则、位号计划的实际保存改动及执行、暂停、恢复结果。可按任务、工程、位号、新旧料号和说明搜索,按结果筛选,选中记录查看详情,点击“更早记录”翻页。搜索、翻页和刷新本身不生成记录。
|
||||
|
||||
“已核验完成”“未命中”“已恢复”“失败”“待核验”分别展示。恢复到旧状态不算替换成功;旧版本没有保存的字段不会补造为真实改动。
|
||||
|
||||
## 清空历史
|
||||
|
||||
“清空全部历史”清理全部历史范围,不受当前搜索条件影响。确认框显示本次清理的记录数;打开确认框后新增的记录保留。默认取消,执行或恢复期间不可清空。
|
||||
|
||||
清空不会删除任务、逐项完成回执、原生证据或工程备份,也不会使已执行项目重新运行。旧版本记录完成首次导入后才允许清空;清空后重启不会重新导入已清理记录。
|
||||
|
||||
## 同一任务继续或重启
|
||||
|
||||
每个独立工程任务保存本轮冻结范围和逐项回执。继续只处理尚未执行的项,不重复派发已完成或已确认未命中的项。只有“开始新一轮”会建立新的轮次。
|
||||
|
||||
续跑检查最近已核验图页的器件身份、属性、布局和连接状态,不拿已经合法修改过的工程与最初文件哈希机械比较。原生证据缺失、损坏或与数据库冲突时停止核验,不把进度归零后重跑。旧证据不足以证明安全时,也需要核验。
|
||||
|
||||
当前界面中的某个 CBB 明确安全失败后,保留失败项,暂缓该工程余下项,并继续后面的独立工程。结果不确定、核验失败或资源未释放时停止整队。普通“继续”不自动重试失败和暂缓项;处理原因并选择“准备失败项重试”后,保留原成功项,只执行失败和暂缓部分。
|
||||
|
||||
重试复用经校验的原始备份,不把已修改工程当作原始副本重新备份。重试队列先暂存、核验再发布;发布中断后重启会恢复原队列。已核验恢复的文件与数据库提交中断时,再次确认恢复会完成原提交,不再次调用替换。
|
||||
|
||||
新数据表随任务数据库自动升级到版本 8。更新前请正常退出工具;本次没有发布新的 EXE,也没有改变 DSH 功能。
|
||||
@@ -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", _
|
||||
|
||||
@@ -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,274 @@
|
||||
"""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_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',
|
||||
}
|
||||
# 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.
|
||||
_LABELS={'create_task':'新建任务','create_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':'扫描工程'}
|
||||
|
||||
_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))
|
||||
@@ -293,6 +303,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
|
||||
@@ -660,6 +675,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 (
|
||||
@@ -797,10 +834,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 +860,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 +952,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 +1055,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)})
|
||||
@@ -1134,7 +1233,8 @@ class TaskCenterService:
|
||||
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
|
||||
return self._operation(name, task_id, lambda: record_operation(self, name, task_id, action))
|
||||
|
||||
def _manual_operation(self, name: str, task_id: str, action: Callable[[], _Result]) -> _Result:
|
||||
def checked_action():
|
||||
@@ -1237,6 +1337,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:
|
||||
|
||||
@@ -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,6 +29,7 @@ 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',
|
||||
|
||||
@@ -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)
|
||||
@@ -254,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()}
|
||||
@@ -302,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()
|
||||
@@ -346,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()
|
||||
@@ -438,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,
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
"""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':'归档任务',
|
||||
'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()
|
||||
@@ -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"
|
||||
''')
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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()
|
||||
@@ -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,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):
|
||||
|
||||
@@ -30,6 +30,7 @@ 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')
|
||||
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):
|
||||
@@ -204,6 +205,41 @@ 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)
|
||||
panel.clear_button.click();wait_history()
|
||||
assert panel.table.rowCount()==0
|
||||
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()
|
||||
|
||||
@@ -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:])
|
||||
|
||||
Reference in new issue
Block a user