From dba01d42d0471687be6835d58d2e96f424bb5181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Mon, 2 Nov 2026 14:03:34 +0800 Subject: [PATCH 1/6] fix-ui-task-selection-controls --- .../trial_task_center/ui/icons.py | 1 + .../trial_task_center/ui/main_window.py | 15 +++++- .../trial_task_center/ui/task_list.py | 50 +++++++++++++++++-- tests/ui/test_workspace_redesign.py | 21 ++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/dx_productivity/trial_task_center/ui/icons.py b/src/dx_productivity/trial_task_center/ui/icons.py index 52d9a32..eb8b9ad 100644 --- a/src/dx_productivity/trial_task_center/ui/icons.py +++ b/src/dx_productivity/trial_task_center/ui/icons.py @@ -30,6 +30,7 @@ def icon(name: str, primary: bool = False) -> QIcon: BUTTON_ICONS = { 'task_center_navigation': 'grid', 'dsh_navigation': 'chat', 'newTaskButton': 'plus', 'editTaskButton': 'sliders', 'deleteTaskButton': 'trash', + 'selectAllTasks': 'check', 'clearTaskSelection': 'close', 'restoreTaskButton': 'refresh', 'taskRecycleBin': 'trash', 'moveTaskUp': 'arrow-up', 'moveTaskDown': 'sort', 'executeProjectTasks': 'play', 'addMappingButton': 'plus', 'editMappingButton': 'sliders', 'deleteMappingButton': 'trash', diff --git a/src/dx_productivity/trial_task_center/ui/main_window.py b/src/dx_productivity/trial_task_center/ui/main_window.py index faa7a66..013f17d 100644 --- a/src/dx_productivity/trial_task_center/ui/main_window.py +++ b/src/dx_productivity/trial_task_center/ui/main_window.py @@ -360,7 +360,18 @@ class TrialTaskCenterWindow(MaterialMainWindow): if deleted else self.service.tasks(read_control=control), received, self._show_error, cancellable=True) def open_task(self, task_id): - if not task_id or (self._workers and self._batch_stop is None): + if not task_id: + if self._workers and self._batch_stop is None: + return + self._task_id = None + self.plan_panel.bind_task(None) + self.project_summary.populate(None) + self.mapping_table.populate(()) + self._message = '' + self._log_presenter.bind(None) + self._load_task_data() + return + if self._workers and self._batch_stop is None: return if self._task_id != task_id: self.plan_panel.bind_task(None) @@ -466,6 +477,8 @@ class TrialTaskCenterWindow(MaterialMainWindow): self.task_list.filter.setEnabled(not self._workers or self._batch_stop is not None) self.task_list.search.setEnabled(not self._workers or self._batch_stop is not None) self.task_list.recycle_button.setEnabled(not busy) + self.task_list.select_all_button.setEnabled(not busy and bool(self.task_list.list.count())) + self.task_list.clear_selection_button.setEnabled(not busy) self.task_list.sort.setEnabled(not self._workers or self._batch_stop is not None) self.task_list.new_button.setEnabled(not busy) exists = bool(context) diff --git a/src/dx_productivity/trial_task_center/ui/task_list.py b/src/dx_productivity/trial_task_center/ui/task_list.py index e717c95..7ff86bb 100644 --- a/src/dx_productivity/trial_task_center/ui/task_list.py +++ b/src/dx_productivity/trial_task_center/ui/task_list.py @@ -8,7 +8,7 @@ from typing import Iterable from PySide6.QtCore import Qt, QSignalBlocker, Signal, QSize, QRect from PySide6.QtGui import QColor,QFont,QIcon,QPalette -from PySide6.QtWidgets import (QComboBox,QHBoxLayout,QLabel,QLineEdit,QListWidget, +from PySide6.QtWidgets import (QAbstractItemView,QComboBox,QHBoxLayout,QLabel,QLineEdit,QListWidget, QListWidgetItem,QPushButton,QVBoxLayout,QWidget,QToolButton,QMenu, QStyledItemDelegate,QStyleOptionViewItem,QStyle,QApplication) from .command_menu import CommandMenu @@ -62,6 +62,7 @@ class TaskList(QWidget): super().__init__() self.setObjectName("taskNavigation") self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) + self._on_open = on_open self._tasks = () self._selected_task_id = None from .icons import icon @@ -83,9 +84,12 @@ class TaskList(QWidget): self.filter.setMaximumHeight(0) self.list = QListWidget(objectName="taskList") self.list.setItemDelegate(TaskItemDelegate(self.list)) + self.list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) self.list.setTextElideMode(Qt.TextElideMode.ElideRight) self.list.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff) self.new_button = QPushButton("新建", objectName="newTaskButton") + self.select_all_button = QPushButton('全选', objectName='selectAllTasks') + self.clear_selection_button = QPushButton('清空选择', objectName='clearTaskSelection') self.edit_button = QPushButton("编辑", objectName="editTaskButton") self.delete_button = QPushButton("删除", objectName="deleteTaskButton") self.restore_button = QPushButton("恢复", objectName="restoreTaskButton") @@ -96,6 +100,8 @@ class TaskList(QWidget): self.new_button.setProperty('appearance','quiet') self.new_button.setText('新建任务') self.new_button.setToolTip('从目录创建工程任务') + for button in (self.select_all_button, self.clear_selection_button): + button.setProperty('appearance', 'quiet') self.manage_menu=CommandMenu(self) for button in (self.edit_button,self.restore_button,self.up_button,self.down_button): self.manage_menu.add_button(button) @@ -121,6 +127,8 @@ class TaskList(QWidget): sort_button.setProperty('appearance','quiet') heading=QHBoxLayout();heading.addWidget(QLabel('工程任务',objectName='taskSectionTitle'));heading.addStretch();heading.addWidget(self.new_button) batch=QHBoxLayout();batch.addWidget(self.count,1);batch.addWidget(sort_button) + selection=QHBoxLayout();selection.setSpacing(3) + selection.addWidget(self.select_all_button);selection.addWidget(self.clear_selection_button);selection.addStretch() footer=QHBoxLayout();footer.addWidget(self.recycle_button);footer.addStretch();footer.addWidget(self.manage_button) self.recycle_button.setProperty('appearance','quiet') self.queue_button.setProperty('appearance','batch') @@ -130,13 +138,19 @@ class TaskList(QWidget): layout.addLayout(heading) layout.addWidget(self.search) layout.addLayout(batch) + layout.addLayout(selection) layout.addWidget(self.list, 1) layout.addWidget(self.queue_button) layout.addLayout(footer) def selected(): - self._selected_task_id = self.current_task_id() - on_open(self._selected_task_id or '') + task_id = self.current_task_id() + if task_id != self._selected_task_id: + self._selected_task_id = task_id + self._on_open(task_id or '') self.list.itemSelectionChanged.connect(selected) + self.list.itemSelectionChanged.connect(self._update_selection_state) + self.select_all_button.clicked.connect(self.select_all_tasks) + self.clear_selection_button.clicked.connect(self.clear_task_selection) self.search.textChanged.connect(self._render) self.sort.currentIndexChanged.connect(self._render) @@ -154,6 +168,35 @@ class TaskList(QWidget): self._selected_task_id = task_id self._render() + def select_all_tasks(self) -> None: + if not self.list.count(): + return + self.list.selectAll() + if self.list.currentItem() is None: + self.list.setCurrentRow(0) + self._update_selection_state() + + def clear_task_selection(self) -> None: + with QSignalBlocker(self.list): + self.list.clearSelection() + self.list.setCurrentItem(None) + for row in range(self.list.count()): + self.list.item(row).setSelected(False) + self._selected_task_id = None + self._on_open('') + self._update_selection_state() + self.view_changed.emit() + + def _update_selection_state(self) -> None: + selected = len(self.list.selectedItems()) + total = self.list.count() + suffix = f' · 已选 {selected}' if selected else '' + current = self.count.text() + if ' · 已选 ' in current: + current = current.split(' · 已选 ', 1)[0] + if current: + self.count.setText(current + suffix) + def _render(self): selected = self._selected_task_id query = self.search.text().strip().casefold().replace('\\', '/') @@ -190,4 +233,5 @@ class TaskList(QWidget): self.list.setCurrentItem(item) hidden = selected and self.current_task_id() is None self.count.setText(f'{len(tasks)} / {len(self._tasks)} 个任务' + (' · 当前任务已隐藏' if hidden else '')) + self._update_selection_state() self.view_changed.emit() diff --git a/tests/ui/test_workspace_redesign.py b/tests/ui/test_workspace_redesign.py index 8f94c61..f07efa0 100644 --- a/tests/ui/test_workspace_redesign.py +++ b/tests/ui/test_workspace_redesign.py @@ -31,6 +31,27 @@ def test_task_management_is_a_menu_and_new_task_is_at_the_top(ui): assert panel.list.itemDelegate().__class__.__name__=='TaskItemDelegate' +def test_task_list_has_explicit_select_all_clear_and_new_task_controls(ui): + panel=ui.window.task_list + assert panel.list.selectionMode().name=='ExtendedSelection' + assert panel.select_all_button.isVisible() + assert panel.clear_selection_button.isVisible() + assert panel.new_button.isVisible() + assert panel.select_all_button.isEnabled() + assert panel.clear_selection_button.isEnabled() + assert panel.list.count()>1 + + panel.select_all_button.click() + assert len(panel.list.selectedItems())==panel.list.count() + assert '已选' in panel.count.text() + + panel.clear_selection_button.click() + assert panel.list.selectedItems()==[] + assert panel.current_task_id() is None + assert ui.window._task_id is None + assert panel.count.text().endswith('个任务') + + def test_plan_search_shares_toolbar_and_save_is_below_table(ui): window=ui.window;window.mapping_tabs.setCurrentIndex(1) plan=window.plan_panel -- 2.39.5 From ba1d50cdfcccc5893c9f837de7260e4eb684da3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Tue, 3 Nov 2026 10:53:40 +0800 Subject: [PATCH 2/6] fix-ui-preserve-acrylic-after-browser-navigation --- docs/testing/2026-11-03-ui-issue16.md | 18 +++++++++ .../trial_task_center/ui/dsh_native.py | 6 ++- tests/ui/test_acrylic_material.py | 13 ++++++ tools/accept_acrylic_ui.py | 40 ++++++++++++++++++- 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 docs/testing/2026-11-03-ui-issue16.md diff --git a/docs/testing/2026-11-03-ui-issue16.md b/docs/testing/2026-11-03-ui-issue16.md new file mode 100644 index 0000000..7022ba0 --- /dev/null +++ b/docs/testing/2026-11-03-ui-issue16.md @@ -0,0 +1,18 @@ +# Issue #16: Acrylic after returning from the browser workspace + +Scope: UI rendering only. Provider/model configuration, runtime protocol, tools and task operations are unchanged. + +The browser workspace previously shared its native composition parent with the Acrylic main window. Showing a real QWebEngineView made the parent surface opaque; hiding the browser did not restore the live backdrop. The alpha-buffer size, transparency attribute and Acrylic property still reported enabled. Reapplying Windows composition alone did not fix the rendering. + +The browser workspace now uses `WA_NativeWindow` so Chromium composes into its own child native surface. This keeps its opaque browser background separate from the task-center backdrop and retains the existing stacked-page navigation. + +Validation on Windows 11 build 26200, 125% scaling: + +- Regression test uses a real QWebEngineView and checks its native composition parent. It failed before the fix and passed afterward. +- `python -m pytest -q tests/ui --tb=short --junitxml=build/issue16-ui-tests.xml`: **172 passed**. +- `python tools/accept_acrylic_ui.py --output build/issue16-final --webengine-roundtrip`: passed. The test uses sample projects and an owned changing backdrop. It displays a local HTML page in real Chromium, without launching the Harness server, model requests or CAD. +- Before the fix, changing the background after returning produced a sidebar pixel difference of **0.0**. After the fix, two consecutive round trips each produced **20.79** (threshold > 2); the solid table difference stayed **0.0** (threshold < 1.5). +- With reduced transparency enabled, the same navigation produced a sidebar difference of **0.0** as expected. Search, empty results, solid fallback, 980×700 layout and visible stop controls also passed. +- An initial focused run reported one existing Qt fixture teardown race (`TrialTaskCenterWindow already deleted`); the subsequent complete UI run passed without errors. + +Screenshots and machine-readable results are local artifacts under `build/issue16-final/`. No release package was built. PR #15 is synchronized with main `3cec9ad`; unrelated main-workspace changes were left untouched. diff --git a/src/dx_productivity/trial_task_center/ui/dsh_native.py b/src/dx_productivity/trial_task_center/ui/dsh_native.py index 826a4eb..61a2ee1 100644 --- a/src/dx_productivity/trial_task_center/ui/dsh_native.py +++ b/src/dx_productivity/trial_task_center/ui/dsh_native.py @@ -5,7 +5,7 @@ import json import codecs from pathlib import Path -from PySide6.QtCore import QProcess, QProcessEnvironment, QTimer, QUrl, Signal +from PySide6.QtCore import QProcess, QProcessEnvironment, QTimer, QUrl, Signal, Qt from PySide6.QtWebEngineCore import QWebEnginePage, QWebEngineProfile, QWebEngineSettings from PySide6.QtWebEngineWidgets import QWebEngineView from PySide6.QtWidgets import QLabel, QPushButton, QVBoxLayout, QWidget @@ -37,6 +37,10 @@ class NativeHarnessView(QWidget): def __init__(self, data_root: Path, parent=None): super().__init__(parent) + # Give Chromium its own native composition boundary. Otherwise its + # texture compositor turns the shared Acrylic top-level surface opaque, + # even after this workspace is hidden again. + self.setAttribute(Qt.WidgetAttribute.WA_NativeWindow) self.setObjectName('dshNativeWorkspace') self.setStyleSheet('QWidget#dshNativeWorkspace {background:white;}') self.data_root = Path(data_root) diff --git a/tests/ui/test_acrylic_material.py b/tests/ui/test_acrylic_material.py index a7bc773..25cb114 100644 --- a/tests/ui/test_acrylic_material.py +++ b/tests/ui/test_acrylic_material.py @@ -93,6 +93,19 @@ def test_repeated_refresh_does_not_reconfigure_composition(qtbot): assert len(backend.calls)==1 +def test_webengine_has_its_own_native_composition_parent(qtbot, tmp_path): + from PySide6.QtCore import Qt + from PySide6.QtWidgets import QVBoxLayout + from dx_productivity.trial_task_center.ui.dsh_native import NativeHarnessView + window=QWidget();qtbot.addWidget(window) + window.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) + view=NativeHarnessView(tmp_path,window) + QVBoxLayout(window).addWidget(view) + window.show() + # The browser must compose into its child HWND, not the Acrylic HWND. + assert view.web.nativeParentWidget() is view + + def test_deferred_material_refresh_never_uses_a_destroyed_window(qtbot,qapp): from PySide6.QtCore import QCoreApplication,QEvent window=QWidget() # This test owns destruction rather than pytest-qt teardown. diff --git a/tools/accept_acrylic_ui.py b/tools/accept_acrylic_ui.py index 3fe01bb..b4cf31d 100644 --- a/tools/accept_acrylic_ui.py +++ b/tools/accept_acrylic_ui.py @@ -16,8 +16,8 @@ import time os.environ.pop('QT_QPA_PLATFORM',None) sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'src')) -from PySide6.QtCore import Qt,QRectF -from PySide6.QtGui import QColor,QFont,QPainter,QLinearGradient,QPen +from PySide6.QtCore import Qt,QRectF,QPoint +from PySide6.QtGui import QColor,QFont,QPainter,QLinearGradient,QPen,QCursor from PySide6.QtWidgets import QApplication,QWidget,QToolTip from PIL import ImageGrab,ImageChops,ImageStat from dx_productivity.trial_task_center.store import TaskStore @@ -29,6 +29,7 @@ from dx_productivity.trial_task_center.target_plan import ComponentTarget,Target def main(): parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('--output',type=Path,default=Path('build/acrylic-acceptance')) + parser.add_argument('--webengine-roundtrip',action='store_true') args=parser.parse_args();out=args.output.resolve();out.mkdir(parents=True,exist_ok=True) app=QApplication([]);app.setFont(QFont('Microsoft YaHei UI',9)) def settle(seconds=.2): @@ -80,6 +81,7 @@ def main(): assert window.property('acrylicEnabled'),'Native Acrylic not available under current system policy' report['native_enabled']=True def capture(name): + QCursor.setPos(window.mapToGlobal(QPoint(12,12))) settle(.7) QToolTip.hideText();settle(.12) geo=window.geometry();scale=screen.devicePixelRatio() @@ -121,6 +123,31 @@ def main(): assert report['right_header_backdrop_difference']>2,report assert report['right_execution_backdrop_difference']>2,report assert report['opaque_table_backdrop_difference']<1.5,report + if args.webengine_roundtrip: + from dx_productivity.trial_task_center.ui.dsh_native import NativeHarnessView + report['before_webengine_surface']={'alpha':window.windowHandle().format().alphaBufferSize(), + 'translucent':window.testAttribute(Qt.WidgetAttribute.WA_TranslucentBackground), + 'acrylic':window.property('acrylicEnabled'),'contrast':window.property('highContrastFallback')} + def create_view(): + view=NativeHarnessView(root/'dsh',window) + view.start=lambda:None # Real Chromium surface; no server, model or CAD. + view.web.setHtml('Owned DSH WebEngine surface') + return view + window._create_dsh_view=create_view + for index in range(2): + window.dsh_action.trigger();settle(2) + window.task_center_action.trigger();settle(1) + report['after_webengine_surface']={'alpha':window.windowHandle().format().alphaBufferSize(), + 'translucent':window.testAttribute(Qt.WidgetAttribute.WA_TranslucentBackground), + 'acrylic':window.property('acrylicEnabled'),'contrast':window.property('highContrastFallback')} + background.phase=0;background.update();capture(f'roundtrip-{index}-a') + background.phase=1;background.update();capture(f'roundtrip-{index}-b') + delta=ImageChops.difference(captures[f'roundtrip-{index}-a'],captures[f'roundtrip-{index}-b']) + report[f'roundtrip_{index}_sidebar_difference']=mean(delta.crop(sidebar)) + report[f'roundtrip_{index}_table_difference']=mean(delta.crop(table)) + (out/'roundtrip-probe.json').write_text(json.dumps(report,indent=2),encoding='utf8') + assert report[f'roundtrip_{index}_sidebar_difference']>2,report + assert report[f'roundtrip_{index}_table_difference']<1.5,report window.plan_panel.find_text.setText('RV');window.plan_panel.find() report['visible_search_feedback']=window.plan_panel.find_result.isVisible() assert '2 / 6'in window.plan_panel.find_result.text() @@ -136,6 +163,15 @@ def main(): report['solid_sidebar_mean']=ImageStat.Stat(solid.crop(sidebar)).mean assert min(report['solid_sidebar_mean'])>200,report report['solid_fallback']=True + if args.webengine_roundtrip: + window.dsh_action.trigger();settle(.3) + window.task_center_action.trigger();settle(.3) + assert not window.property('acrylicEnabled') + background.phase=0;background.update();capture('roundtrip-solid-a') + background.phase=1;background.update();capture('roundtrip-solid-b') + delta=ImageChops.difference(captures['roundtrip-solid-a'],captures['roundtrip-solid-b']) + report['roundtrip_reduced_sidebar_difference']=mean(delta.crop(sidebar)) + assert report['roundtrip_reduced_sidebar_difference']<1.5,report window.reduced_transparency_button.setChecked(False);settle(.3) window.resize(980,700);capture('native-app-compact') report['compact_size']=[window.width(),window.height()] -- 2.39.5 From 63835afba8e4c1fbf15749fb3415a1d9c68b4743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Tue, 3 Nov 2026 11:02:27 +0800 Subject: [PATCH 3/6] fix-ui-restore-task-status-categories --- docs/testing/2026-11-03-ui-task-categories.md | 17 ++++ .../trial_task_center/ui/main_window.py | 7 +- .../trial_task_center/ui/task_list.py | 15 ++- tests/ui/test_task_categories.py | 91 +++++++++++++++++++ tests/ui/test_trial_task_center_window.py | 2 +- tools/accept_acrylic_ui.py | 12 +++ 6 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 docs/testing/2026-11-03-ui-task-categories.md create mode 100644 tests/ui/test_task_categories.py diff --git a/docs/testing/2026-11-03-ui-task-categories.md b/docs/testing/2026-11-03-ui-task-categories.md new file mode 100644 index 0000000..9f69040 --- /dev/null +++ b/docs/testing/2026-11-03-ui-task-categories.md @@ -0,0 +1,17 @@ +# Task status categories + +The earlier task navigation retained a hidden, disconnected status selector. The visible selector below search now offers All tasks, Unfinished, Completed and Failed, with the existing SVG icon set. Unfinished includes Draft, Ready, Running, Paused and Failed; deleted tasks remain in the recycle bin. + +Filtering operates on the cached task list and combines with search. Refreshing tasks reapplies the chosen category against the updated status. The current task is remembered when hidden, so changing display categories does not switch the right-hand task or discard its draft. The window retains the complete task collection for batch execution; filtering never narrows the execution scope. Moving tasks is disabled while a status filter hides part of the saved order. + +The recycle bin ignores the normal status filter and disables that selector while open; returning restores the chosen category. The status selector is a display preference for this window and defaults to All tasks on a new launch. + +Validation: + +- Seven new cases first failed against the hidden/disconnected selector. They now cover category membership, combined search, completion on refresh, preserving task identity, cached reads, unchanged batch scope, ordering gates and recycle-bin independence. +- `python -m pytest -q tests/ui tests/v1_1/test_task_navigation.py --tb=short --junitxml=build/task-category-ui-tests.xml`: **181 passed**. +- After adding the status SVG icons, category and navigation tests: **9 passed**. +- `python tools/accept_acrylic_ui.py --output build/task-categories-final --webengine-roundtrip`: passed on Windows 11 build 26200 at 125% scaling, including status category visibility, return from Chromium, reduced transparency and the 980×700 layout. +- Python compilation and `git diff --check`: passed. + +No DSH functionality or task execution implementation changed. No CAD or model requests were made. No release executable was built. diff --git a/src/dx_productivity/trial_task_center/ui/main_window.py b/src/dx_productivity/trial_task_center/ui/main_window.py index be1aaec..529fb8a 100644 --- a/src/dx_productivity/trial_task_center/ui/main_window.py +++ b/src/dx_productivity/trial_task_center/ui/main_window.py @@ -117,7 +117,6 @@ class TrialTaskCenterWindow(MaterialMainWindow): self.resize(1280, 880) self.setStyleSheet(APP_STYLESHEET) self.task_list = TaskList(self.open_task) - self.task_list.filter.currentTextChanged.connect(self._refresh_task_list) self.task_list.recycle_button.toggled.connect(self._refresh_task_list) self.task_list.new_button.clicked.connect(self._new_task) self.task_list.edit_button.clicked.connect(self._edit_task) @@ -484,7 +483,8 @@ class TrialTaskCenterWindow(MaterialMainWindow): self.project_summary.refresh_button.setEnabled(not busy and bool( self._task_id if read_failed else context and not context.legacy)) self.task_list.list.setEnabled(not self._workers or self._batch_stop is not None) - self.task_list.filter.setEnabled(not self._workers or self._batch_stop is not None) + self.task_list.filter.setEnabled((not self._workers or self._batch_stop is not None) + and not self.task_list.recycle_button.isChecked()) self.task_list.search.setEnabled(not self._workers or self._batch_stop is not None) self.task_list.recycle_button.setEnabled(not busy) self.task_list.select_all_button.setEnabled(not busy and bool(self.task_list.list.count())) @@ -508,8 +508,9 @@ class TrialTaskCenterWindow(MaterialMainWindow): for button in (self.task_list.up_button, self.task_list.down_button): button.setEnabled(grouped and not busy and not group_active and self.task_list.sort.currentData() == 'execution' + and self.task_list.filter.currentData() == 'all' and not self.task_list.search.text().strip()) - button.setToolTip('清空搜索并切换到执行顺序后,可调整本批任务的执行位置。') + button.setToolTip('清空搜索、选择全部任务并切换到执行顺序后,可调整本批任务的执行位置。') if not self._message: self.run_panel.operator_band.setText(state.message) if self._loading and not self._message: diff --git a/src/dx_productivity/trial_task_center/ui/task_list.py b/src/dx_productivity/trial_task_center/ui/task_list.py index 7ff86bb..fdbacbe 100644 --- a/src/dx_productivity/trial_task_center/ui/task_list.py +++ b/src/dx_productivity/trial_task_center/ui/task_list.py @@ -79,9 +79,11 @@ class TaskList(QWidget): self.recycle_button.setCheckable(True) self.count = QLabel(objectName='taskCount') self.filter = QComboBox(objectName="taskFilter") - self.filter.addItems(("进行中", "已完成", "已删除")) - self.filter.setVisible(False) - self.filter.setMaximumHeight(0) + for label, key, picture in (('全部任务', 'all', 'grid'), ('未完成', 'unfinished', 'clock'), + ('已完成', 'completed', 'check-circle'), ('失败', 'failed', 'warning')): + self.filter.addItem(icon(picture), label, key) + self.filter.setAccessibleName('任务状态分类') + self.filter.setToolTip('未完成包括待配置、待执行、执行中、已暂停和失败的任务;分类只筛选显示。') self.list = QListWidget(objectName="taskList") self.list.setItemDelegate(TaskItemDelegate(self.list)) self.list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection) @@ -137,6 +139,7 @@ class TaskList(QWidget): layout.setSpacing(11) layout.addLayout(heading) layout.addWidget(self.search) + layout.addWidget(self.filter) layout.addLayout(batch) layout.addLayout(selection) layout.addWidget(self.list, 1) @@ -152,6 +155,7 @@ class TaskList(QWidget): self.select_all_button.clicked.connect(self.select_all_tasks) self.clear_selection_button.clicked.connect(self.clear_task_selection) self.search.textChanged.connect(self._render) + self.filter.currentIndexChanged.connect(self._render) self.sort.currentIndexChanged.connect(self._render) def current_task_id(self) -> str | None: @@ -202,6 +206,11 @@ class TaskList(QWidget): query = self.search.text().strip().casefold().replace('\\', '/') tasks = [task for task in self._tasks if query in ' '.join(str(getattr(task, name, '') or '') for name in ('name', 'discovery_root', 'note')).casefold().replace('\\', '/')] + category = self.filter.currentData() + if not self.recycle_button.isChecked() and category != 'all': + tasks = [task for task in tasks if + (task.status.value not in {'completed', 'deleted'} if category == 'unfinished' + else task.status.value == category)] mode = self.sort.currentData() if mode.startswith('name_'): tasks.sort(key=lambda task: tuple(int(part) if part.isdigit() else part diff --git a/tests/ui/test_task_categories.py b/tests/ui/test_task_categories.py new file mode 100644 index 0000000..8364023 --- /dev/null +++ b/tests/ui/test_task_categories.py @@ -0,0 +1,91 @@ +from dataclasses import replace +from datetime import datetime, timezone +from pathlib import Path + +import pytest +from PySide6.QtCore import Qt + +from dx_productivity.trial_task_center.models import TaskRecord, TaskStatus +from dx_productivity.trial_task_center.ui.task_list import TaskList + + +@pytest.fixture +def navigation(qtbot): + opened=[] + panel=TaskList(opened.append) + qtbot.addWidget(panel) + now=datetime.now(timezone.utc) + tasks=tuple(TaskRecord(status.value, 'Board '+status.value, 'audio', Path('F:/Projects'), + status, now, now, group_id='batch', group_position=index) + for index,status in enumerate(TaskStatus) if status is not TaskStatus.DELETED) + panel.populate(tasks,selected_task_id='ready') + panel.show() + return panel,tasks,opened + + +def visible_ids(panel): + return [panel.list.item(index).data(Qt.ItemDataRole.UserRole) for index in range(panel.list.count())] + + +@pytest.mark.parametrize(('category','expected'),[ + ('all',['draft','ready','running','paused','completed','failed']), + ('unfinished',['draft','ready','running','paused','failed']), + ('completed',['completed']), + ('failed',['failed']), +]) +def test_visible_status_category_filters_task_list(navigation,category,expected): + panel,tasks,opened=navigation + assert panel.filter.isVisible() + index=panel.filter.findData(category) + assert index>=0 + panel.filter.setCurrentIndex(index) + assert visible_ids(panel)==expected + assert panel._tasks==tasks + assert opened==[] + + +def test_category_and_search_combine_and_refresh_tracks_new_status(navigation): + panel,tasks,opened=navigation + panel.filter.setCurrentIndex(panel.filter.findData('unfinished')) + panel.search.setText('Board ready') + assert visible_ids(panel)==['ready'] + updated=tuple(replace(task,status=TaskStatus.COMPLETED) if task.task_id=='ready' else task for task in tasks) + panel.populate(updated,selected_task_id='ready') + assert visible_ids(panel)==[] + assert '当前任务已隐藏' in panel.count.text() + panel.filter.setCurrentIndex(panel.filter.findData('completed')) + assert visible_ids(panel)==['ready'] + assert panel.current_task_id()=='ready' + assert opened==[] + + +def test_category_is_local_and_never_changes_batch_scope(ui,monkeypatch): + window=ui.window + before=window._tasks + def unexpected_read(**kwargs): + raise AssertionError('Category changes must use cached tasks') + monkeypatch.setattr(ui.service,'tasks',unexpected_read) + panel=window.task_list + panel.filter.setCurrentIndex(panel.filter.findData('completed')) + assert panel.list.count()==0 + assert window._tasks==before + assert window._task_id==ui.tasks[0].task_id + assert not panel.up_button.isEnabled() + assert not panel.down_button.isEnabled() + panel.filter.setCurrentIndex(panel.filter.findData('all')) + assert panel.list.count()==len(before) + + +def test_recycle_bin_ignores_active_status_category(ui): + panel=ui.window.task_list + panel.filter.setCurrentIndex(panel.filter.findData('completed')) + ui.service.delete_task(ui.tasks[1].task_id) + panel.recycle_button.setChecked(True) + ui.settle() + assert not panel.filter.isEnabled() + assert visible_ids(panel)==[ui.tasks[1].task_id] + panel.recycle_button.setChecked(False) + ui.settle() + assert panel.filter.isEnabled() + assert panel.filter.currentData()=='completed' + assert visible_ids(panel)==[] diff --git a/tests/ui/test_trial_task_center_window.py b/tests/ui/test_trial_task_center_window.py index a95e966..493eb20 100644 --- a/tests/ui/test_trial_task_center_window.py +++ b/tests/ui/test_trial_task_center_window.py @@ -11,7 +11,7 @@ def test_shell_has_stable_automation_controls_and_current_navigation(ui): 'boundProjectPath','exportBomTemplate','importBomPlan','toggleLogButton'): assert w.findChild(QWidget,name) is not None,name assert w.windowTitle()==f'DxDesigner 器件替换 · {VERSION}' - assert not w.task_list.filter.isVisible() + assert w.task_list.filter.isVisible() assert w.workspace_splitter.orientation()==Qt.Orientation.Horizontal assert not w.workspace_splitter.childrenCollapsible() assert w.detail_splitter.count()==2 diff --git a/tools/accept_acrylic_ui.py b/tools/accept_acrylic_ui.py index b4cf31d..98fbd35 100644 --- a/tools/accept_acrylic_ui.py +++ b/tools/accept_acrylic_ui.py @@ -157,6 +157,15 @@ def main(): assert report['empty_search_feedback'] capture('native-app-empty-search') window.plan_panel.clear_find();window.plan_panel.table.clearSelection() + task_id=window._task_id + window.task_list.filter.setCurrentIndex(window.task_list.filter.findData('completed')) + assert window.task_list.list.count()==0 and window._task_id==task_id + capture('native-app-completed-category') + window.task_list.filter.setCurrentIndex(window.task_list.filter.findData('unfinished')) + assert window.task_list.list.count()==len(tasks) + window.task_list.filter.setCurrentIndex(window.task_list.filter.findData('all')) + assert window.task_list.current_task_id()==task_id + report['status_categories']=True window.reduced_transparency_button.setChecked(True);capture('native-app-solid') assert not window.property('acrylicEnabled') solid=captures['native-app-solid'] @@ -178,6 +187,9 @@ def main(): report['compact_table_height']=window.plan_panel.table.viewport().height() assert report['compact_size']==[980,700] assert report['compact_table_height']>=100 + assert window.task_list.filter.isVisible() + point=window.task_list.filter.mapTo(window,window.task_list.filter.rect().bottomRight()) + assert 0<=point.x() Date: Tue, 3 Nov 2026 11:34:46 +0800 Subject: [PATCH 4/6] docs-detail-history-and-resume-execution-logic --- ...1-03-operation-history-execution-design.md | 232 ++++++++++++++++++ ...11-03-operation-history-resume-research.md | 88 +++++++ 2 files changed, 320 insertions(+) create mode 100644 docs/superpowers/specs/2026-11-03-operation-history-execution-design.md create mode 100644 docs/superpowers/specs/2026-11-03-operation-history-resume-research.md diff --git a/docs/superpowers/specs/2026-11-03-operation-history-execution-design.md b/docs/superpowers/specs/2026-11-03-operation-history-execution-design.md new file mode 100644 index 0000000..b82cf31 --- /dev/null +++ b/docs/superpowers/specs/2026-11-03-operation-history-execution-design.md @@ -0,0 +1,232 @@ +# 操作历史与防重复执行:详细执行逻辑 + +状态:设计细化,尚未实施。本文延续《operation-history-resume-research》,用户已明确重复执行场景为同一个任务继续或重启后;不修改 DSH 功能。 + +用户已确认:某个 CBB 失败后,只要已经证明没有残留改动且资源已释放,跳过本次失败,继续后面的工程;结果不确定或核验失败时仍停止。 + +## 1. 执行范围与基本约束 + +“CBB 工程执行过”不等于“该工程所有替换都做完”。不能按文件名、工程路径或任务状态一个字段去重。 + +使用三层范围: + +1. 任务:绑定实际工程,保留用户的任务身份。 +2. 轮次:一次明确确认的工程改动目标。继续、重启、清空历史不创建新轮次。 +3. 执行项:一次原生执行器实际接受的完整范围,通常为某工程/图页的一条替换规则及其目标集合。一项可能涉及多个位号;页面展开后可显示逐位号结果。 + +现有领域模型是一任务绑定一个工程。多个 CBB 组成批次,每个工程分别对应自己的任务和轮次;批次层负责顺序、依赖与失败后继续策略,不把多个工程塞入同一个新任务。整图页规则模式和精确位号模式均适用,不能仅在 BOM 模式下防重复。 + +同一任务、同一轮次中,已核验成功的执行项永不因“继续”而再次派发。一个图页完成,不代表其他图页完成;一条规则完成,不代表该 CBB 已完成。 + +正常默认行为:前面的完整项跳过,当前未完成项接续,结果不确定时停止。不得为了让进度走完,把失败、未命中、用户跳过或核验失败统一标为“替换成功”。 + +## 2. 稳定身份与冻结计划 + +### 2.1 身份 + +`operation_id` 在确认计划时生成一次,保存后不重新编号。必须区分以下身份,不能只对随机 ID 做唯一约束: + +- `plan_snapshot_hash`:包含计划版本的完整冻结快照,用于检查确认范围有没有变化。 +- `logical_operation_key`:可重算的同轮次逻辑项身份,**不包含计划版本、operation_id、attempt_id 或临时 job_id**,防止重复冻结/换版本绕过去重。 +- `target_claim`:已核验目标的占用记录,防止把同一目标重新拆组/合组后再次派发。精确位号执行须对 `(task_id, round_id, 原目标身份)` 做完成占用约束,成功后不能释放该占用来准备普通重试。 + +逻辑项身份输入至少包含: + +- `task_id`、显式持久化的 `round_id`; +- 规范工程身份与路径(使用现有 canonical/path identity 工具,不能只比较 CBB 名称); +- 原理图、图页; +- 执行模式:整图页规则或精确目标; +- 旧料号、新料号;目标 Cell; +- 全部目标 UID/位号及其必要的原始身份;目标集合按稳定顺序编码; +- Value 策略及自定义 Value、NC 策略。 + +`attempt_id` 代表一次尝试,每次获准重试都不同;临时 `job_id` 只关联执行器,不是防重复的唯一身份。数据库对同任务/同轮次逻辑项以及原目标完成占用同时约束,另以 CAS 约束单项活动尝试,禁止相同执行项被并发领走。 + +整图页规则模式在不能提供稳定目标身份时,不允许把已派发项重新拆组或扩大后继续。必须复用原冻结范围;需要改变范围则先核验,再显式确认新轮次,不能因为另一个版本的逻辑项 key 不同就重新处理成功目标。 + +### 2.2 冻结 + +确认前展示真实待执行范围和计数,事务内持久化轮次、计划及全部执行项,再允许启动执行器。计划保存失败,不派发 CAD。 + +第一次派发后原计划保持冻结。筛选、排序、页面切换不能改变计划。需要修正失败项时生成明确的失败项修订计划,预览并重新确认;已成功项不纳入该重试范围。不能通过随手换一个计划 hash,让原成功项变成“新任务”。 + +新轮次通过现有“开始新一轮”的显式入口进行:核验旧轮次、归档旧证据、创建新的活动轮次身份、重新采集当前工程基线、重新确认。新轮次的数据库变更与归档日志沿用现有崩溃恢复机制,不在启动时自动创建。 + +## 3. 执行项状态与允许操作 + +下表为执行项的设计状态,不表示现有 `TaskStatus` 已新增这些枚举。 + +| 状态 | 判定依据 | 点击继续 | 能否调用替换 | +| --- | --- | --- | --- | +| 待执行 `pending` | 已冻结计划,无已派发尝试 | 检查准入后领取 | 可以 | +| 已领取/准备 `prepared` | 派发身份已持久化,还未确认执行结果 | 核对进程、租约和尝试证据 | 仅明确证明未进入修改且旧执行器已退出后可重新领取 | +| 可能已修改 `mutation_possible` | 修改前保护标记已落盘,或运行中断无法排除修改 | 进入核验,不能当待执行 | 不可以 | +| 保存待核验 `saved_unverified` | 有保存结果,缺少完整重开回读证明 | 只做核验/恢复 | 不可以 | +| 已核验完成 `verified` | 当前项身份匹配,保存、重开回读和范围核验通过 | 跳过,读取下一项 | 不可以 | +| 未命中 `no_match` | 原确认范围的只读检查明确为零匹配 | 本轮跳过,不反复枚举 | 不可以 | +| 已确认失败 `failed_safe` | 明确未修改,或已核验回滚到基线 | 普通继续不重试;用户显式准备失败项重试 | 通过重试准入后可以 | +| 结果不确定 `uncertain` | 证据缺失/矛盾、租约不明、保存结果无法确认 | 阻断并指引核验 | 不可以 | +| 用户跳过 `skipped` | 用户明确跳过并给出原因 | 本轮不执行,计为跳过 | 不可以 | + +`restored_ok` 必须按其具体恢复证明解释:恢复到旧基线表示回滚已核验,不是替换成功;只有恢复流程明确证明目标结果已保存并重开核验,才能归并为 `verified`。 + +“未命中”只在当前冻结范围及有效工程状态下成立。它不是已保存修改,历史页不得将其列入替换成功数;要重新查找不同工程内容,走新范围确认,而不是在本轮继续中自动重置。 + +## 4. 一项操作的实际执行顺序 + +1. **准入**:取得现有队列/监督进程租约,核对项目身份、原计划、资源释放状态和停止意图。并发窗口不能同时执行一个工程或一个执行项。 +2. **核对回执**:命中本轮终态就跳过;遇到不完整尝试先恢复,不创建新队列绕过它。 +3. **领取**:用数据库比较并更新(CAS)将 `pending` 或已显式获准重试的项转为 `prepared`,保存 attempt 身份。领取失败或刷盘失败,不派发。 +4. **修改前检查**:确认旧执行器已结束,工程当前状态与预期基线一致,目标身份、料号和策略相符;完成备份。 +5. **写前保护**:沿用 `batch_attempt.prepare_attempt` / `mark_mutation_possible` 的身份和代次绑定。进入原生修改前必须可靠发布“可能已修改”证据,失败时拒绝修改。 +6. **执行与核验**:执行当前项,保存、关闭、重新打开回读;核对目标结果及现有非目标保护条件,关闭/释放本项资源。 +7. **提交回执**:在任务数据库同一事务中写入终态回执、推进轮次计数/工程预期状态、追加用户历史源事件。事务成功后才允许派发下一项。 +8. **更新页面**:UI 从已提交记录展示结果。页面刷新、回调或弹窗失败不会改变已提交终态,更不能触发重试。 + +任务数据库与外部 CAD 保存无法组成一个跨进程原子事务。因此只承诺“有可靠完成证据的不重放;不确定时先核验”,不承诺无法兑现的严格 exactly-once。 + +## 5. 暂停、继续、重启分别怎么走 + +### 5.1 完成当前项后暂停 + +- 先立即设置现有内存停止控制以阻止领取下一项,再持久化本轮暂停意图。 +- 暂停意图落盘失败时,当前进程仍保持停止状态并明确提示“暂停请求未能保存,请等待当前项收尾”;不能因保存异常而继续执行。重启后默认不自动执行,因此丢失暂停意图也不能成为自动派发依据。 +- 当前正在执行的项按现有安全机制收尾;其真实结果先提交,再停止领取下一项。 +- 暂停请求本身不代表当前项成功;如果当前项核验失败,显示待核验或安全失败。 +- 如果请求落在最后一项结束时,仍保留用户暂停意图,不能因“队列刚好耗尽”而自动开始失败项重试。 +- 重启后继续是用户对剩余范围的明确授权,不能由应用启动自动替代。 + +### 5.2 点击继续 + +1. 读取原任务、原轮次和冻结计划;显示上次完成/未命中/失败/待执行/待核验计数。 +2. 与持久化逐项回执及现有原生执行证据对账;原队列 ID 丢失不能导致逐项身份丢失。 +3. 验证当前工程是工具上次已核验后的预期状态。 +4. 汇总并提示本次实际待执行范围。例如:“原计划 12 项,已完成 7 项、未命中 2 项,本次继续 3 项”。 +5. 若存在待核验项,先阻断;否则只领取剩余合法 `pending` 项。 + +普通继续只处理可派发的待执行项,不包含失败项,也不解除“同工程失败后暂缓”的项。若只剩失败和暂缓项,显示“需处理失败项”,提供失败项重试/核验入口,而不是仍提供会反复空跑的继续按钮。 + +### 5.3 重启工具 + +启动只恢复显示和核对状态,不自动执行 CAD,不默认清空状态,不凭任务为 paused 就断言可重试。 + +- 原执行器仍存活或资源释放未证实:显示处理中/等待释放,不创建第二个执行器。 +- 记录完整且所有尝试均已收尾:保留原完成项,展示可继续的剩余范围。 +- 存在可能修改但未提交结果的项:转为待核验。 +- 数据库、文件、计划或步骤身份冲突:展示“恢复受阻”,保留原记录供核对。 + +### 5.4 强制结束 + +沿用原有强制结束确认与保护记录。不能把强制结束当作回滚成功,也不能把它之后的所有项统一标失败后自动重试。只把确实无法排除修改的当前项转为待核验;从未派发的后续项保持待执行。 + +## 6. 崩溃位置与恢复决定 + +| 中断位置/证据组合 | 恢复决定 | +| --- | --- | +| 冻结计划事务尚未提交,且没有派发/修改痕迹 | 不执行;重新确认计划 | +| 已领取,有匹配的 prepared 证明,旧进程已退出且明确未进入修改 | 原执行项保留;在重新授权后创建新 attempt | +| 修改标记已发布,未收到完整结果 | 待核验,不重放 | +| 已保存或 worker 报成功,但终态回执尚未提交 | 从绑定的原生证据核验;证明通过后补记回执,不再次替换 | +| 回执已提交,UI 尚未显示就退出 | 读取已提交终态,跳过该项 | +| 命令还在但状态文件缺失/损坏 | 有有效新回执时对账;否则阻断,不返回“0 项、未执行” | +| 数据库终态与文件终态冲突,或回执引用的证据无法验证 | 已终态项不重新派发;暂停续跑,核对冲突 | +| 旧版本没有新回执,只有完整且可绑定的旧执行证据 | 经兼容校验导入同轮次记录;无法证明的项进入待核验,不按进度数字补造成功 | +| 一次执行涵盖多个位号,结果部分通过 | 整项待核验,展开列出各位号观测;使用现有核验/恢复能力处理,禁止对整组盲目重试 | + +任何“缺文件就创建新状态文件”“读取失败就归零”“找不到原队列就从头新建”的降级都不允许。 + +### 6.1 记录权威与旧回调隔离 + +- 已提交的数据库终态是调度依据;TSV、普通日志、UI 回调不能把它降级成待执行,也不能覆盖为另一次尝试的结果。 +- 提交必须匹配当前 `operation_id + attempt_id + generation`。旧尝试晚到的回调只保留为诊断证据,不推进新尝试、汇总进度或新的工程基线。 +- 证据与数据库冲突时阻断并核验,不任意挑一份覆盖另一份。恢复只能按证据补齐合法的单调状态转换。 +- 租约超时只意味着需要检查,不证明旧进程已退出。只有已证明旧进程退出或原执行代次被有效隔离,且修改风险已解决,才允许新的尝试。 + +## 7. 工程变化、失败与其他 CBB 的处理 + +### 7.1 工具自己改过的工程 + +不能把暂停后的工程与最初项目文件哈希直接比较:前面的合法替换必然会改变它。 + +保存基线链:初始已确认工程状态 → 第 1 项核验后状态 → 第 2 项核验后状态。继续时比较最近已提交回执对应的预期状态,核对将要执行的目标及现有工程/库安全身份。 + +工程语义检查沿用原生快照和目标身份核验。证据文件字节哈希用于检验证据完整性;不能把易变数据库时间戳直接等同于工程语义变化,也不能因路径相同而忽略实际工程被替换。 + +检测到额外修改时,先列出差异,阻断当前续跑,由用户核验并重新确认范围;不自动“接受新基线”来清掉旧回执。 + +### 7.2 失败后是否继续其他 CBB + +用户已选择“跳过这次失败,继续后面的工程”。因此默认按以下规则处理: + +1. 明确确认当前工程未修改,或已经完整回滚且核验通过,并确认资源释放后,该 CBB 本次收尾,进入确认范围内下一个可独立执行的工程。 +2. 原失败项仍记为失败,带错误和证据,不改成成功,也不把它伪装成普通的“用户跳过”。同一 CBB 中尚未派发的后续项保持“待执行/本次未执行”,带 `project_failure` 暂缓原因,不误标完成,不在该次失败后继续领走。只有该工程失败项已妥善解决、范围仍有效并重新确认,才解除暂缓。 +3. 如果后续工程对失败工程有已声明的依赖,则该后续工程记为依赖受阻;继续其他无依赖且可执行的工程,不自动忽略依赖。 +4. 未知结果、核验失败、资源未释放、证据冲突始终阻断整队,不能使用“继续后面的工程”覆盖该保护。 +5. 本次结束显示“X 个工程完成、Y 个工程失败、Z 个工程未执行/依赖受阻”。“队列收尾”不能等同于“所有改动成功”。 + +失败项重试通过明确的“准备失败项重试”入口,限定原失败项,建立新 attempt 并保留原失败记录。成功项、未命中项和未派发项不被整体清空后重新编号。 + +### 7.3 汇总状态 + +- 有活动执行项:执行中。 +- 有不确定/证据冲突项:待核验/恢复受阻。 +- 用户暂停且仍有待执行项:已暂停。 +- 所有项都有明确终态,但存在失败或用户跳过:执行已收尾、有失败/跳过,不能显示“全部替换成功”。 +- 所有项均为 verified 或有效 no_match:本轮完成,并分别显示保存与未命中数量。 + +补充任务/批次层级的收尾规则: + +- 单工程任务存在 `failed_safe`,其余未执行项均因 `project_failure` 暂缓时,显示“需处理失败项”,禁用普通继续,提供失败项处理入口;不把它归成用户暂停。 +- 批次所有可独立派发工程已经收尾,剩余仅失败、暂缓或依赖受阻时,显示“本次执行已收尾,有失败/未执行”。它不是全部完成,也不反复提供会空跑的批次继续按钮。 +- 只有仍存在通过准入的独立待执行项时,才显示可执行的“继续剩余项”及其确切数量。 + +以上是汇总推导规则,映射到现有任务状态时保留兼容性;不能只依赖一个 `TaskStatus.COMPLETED` 字段代替逐项判定。 + +## 8. 历史记录与清空 + +执行回执、原始诊断证据、用户历史分别管理;“分别管理”不要求拆成不同数据库。 + +建议回执、历史源事件与汇总更新使用同一任务数据库事务,避免“CAD 已完成、页面显示成功但没有完成回执”。历史页是可查询的显示记录,不是继续执行的判定来源。 + +### 8.1 历史记录内容 + +保存动作发生时间、任务/轮次、工程、执行项、动作来源、尝试编号、实际结果、图页位号、新旧料号,以及有证据支持的 Value/NC/Cell 差异。开始请求和完成结果是不同事件。无改动和失败不能填造成功差异。 + +用户历史覆盖实际的新建/编辑/归档任务、规则及位号计划保存、执行/暂停/恢复、核验结果和因完成回执跳过的摘要。搜索、翻页、轮询、读取快照不生成新的操作历史,避免页面自己制造海量记录。配置改动保存有证据的前后差异;不记录模型密钥等凭据。 + +搜索参数化查询任务、工程、位号、新旧料号和说明;结果按数据库全局单调的 `history_event_id` 倒序做游标分页,同时间事件不漏页。现有 JSONL 的 `sequence` 是每任务序号,不能当全局分页或清空序号。详情展示原计划、真实改动、核验结论及失败原因。旧版本缺少的字段明确显示“旧版本未记录”。 + +### 8.2 清空的精确语义 + +- 默认明确命名“清空全部历史”,确认框展示即将清理的历史数量;即使搜索正在生效,也不能让按钮范围含糊成“只清当前页还是全部”。 +- 不清理任务、完成回执、检查点、原生证据及备份,不改变本轮任何执行项状态。 +- 打开确认框时,用同一数据库快照取得全局 `history_event_id` 截止值与将清理的数量。用户确认后严格按该截止值清理,不能重新读取更大的截止值,把确认期间的新事件也清掉。 +- 清空事务更新持久化 `cleared_through_id`;查询只显示该边界之后的记录。需要物理清理时只删除截止值以内的独立历史行。重建索引或重启不能清除这个水位;事件序号使用不会因删除全部历史而复用的序列(如 AUTOINCREMENT 或独立持久计数器),清空不重置序号。 +- 新历史源事件与执行回执同事务写入,因此不存在“成功回执很早提交,历史却以新时间延迟生成”导致旧记录复活的窗口。UI 延迟读取不会改变事件序号。 +- 旧 JSONL 兼容导入单独保存来源去重键、迁移完成状态及清理水位,不能仅因源文件轮转或重建索引再次导入。首次历史迁移未完成时禁用清空;正常查询刷新不禁用清空。 +- 建议活动执行/恢复期间禁用清空,以降低用户对范围的误解;并发新事件仍以事务边界保证:截止值之后的事件保留,不能误删用户确认后才完成的操作。 +- 清空失败时保留原页面及记录,提示重试;不能先把表格清掉再假装后端成功。 + +## 9. 示例 + +批次 G 包含任务 TA 与 TB,分别绑定 CBB 工程 A 和 B,各自保留轮次 RA/RB。每工程两个图页、每图页两条规则,共 8 项。 + +先完成 A 的第 1 页两项,再完成 A 第 2 页第一项,然后暂停: + +- A:3 项已核验,1 项待执行;不能因“A 跑过了”跳过余下一项。 +- B:4 项待执行。 +- 重启后点击继续:显示跳过 3 项,本次执行 5 项;不再执行 A 已完成的前三项。 +- 如 A 第三项的保存核验尚未结束就强制退出:保留前 2 项已核验,第三项待核验,之后 5 项待执行;先恢复第三项,不能重新跑前 2 项或直接运行 B。 +- 清空操作历史:页面旧记录清理,但以上执行项状态原样保留。 +- 如 A 第三项明确安全失败:A 保留失败项和 1 项因工程失败暂缓的待执行项;跳转 B 执行其 4 项。再次普通继续不会偷偷重试 A;处理 A 的失败项后重新确认,才接续其剩余范围。 +- 全部完成后再次点击执行:显示“本轮已完成”,不启动执行器;用户明确开始新一轮后,重新确认基于当前工程的改动目标。 + +## 10. 实施与验收拆分 + +1. 最小保护补丁:复现状态文件丢失/损坏/截断,并在快照读取、准备队列、服务派发入口统一阻断;验证现有首次执行和正常续跑不被误阻断。 +2. 持久化计划与逐项回执:补齐任务/轮次身份、CAS 领取、attempt 绑定、终态原子提交,以及旧记录兼容对账。 +3. 恢复矩阵:对本文第 6 节逐点故障注入,实际统计同一逻辑项的执行器派发次数,不能仅检查按钮和进度数字。 +4. 历史页:接入真实事务事件、搜索分页、展开详情和明确范围的清空;覆盖“清空后重启不再现旧历史,但不重复执行”。 +5. UI 回归及原生窗口验收:保留玻璃隔离、分类/搜索、未保存编辑保护及 980×700 可见性。 + +验收底线:正常继续只派发剩余项;不确定结果没有替换派发;完成回执与清空历史互不影响;任何跨进程崩溃窗口都不能靠归零掩盖。 diff --git a/docs/superpowers/specs/2026-11-03-operation-history-resume-research.md b/docs/superpowers/specs/2026-11-03-operation-history-resume-research.md new file mode 100644 index 0000000..b64a0f5 --- /dev/null +++ b/docs/superpowers/specs/2026-11-03-operation-history-resume-research.md @@ -0,0 +1,88 @@ +# 操作历史与同任务断点续跑:研究及建议方案 + +状态:研究完成,方案待实施;未修改产品代码、DSH 功能或真实工程。 + +执行项身份、状态机、暂停/重启、崩溃恢复矩阵和清空语义已进一步细化,见 [详细执行逻辑](2026-11-03-operation-history-execution-design.md)。 + +## 用户目标与已确认场景 + +- 增加独立的操作历史页面,让用户知道工具实际做过哪些改动。 +- 页面可以搜索、清空历史。 +- 用户已明确:重复执行发生在**同一个任务点击继续或重启工具后**,本轮不把它解释成重新导入 CBB 或重新新建任务。 +- 用户进一步确认:一个 CBB 已确认安全失败且资源已释放后,跳过这次失败并继续后面的工程;不确定结果仍停止。 +- 保留既有 UI 风格,DSH 功能仍不在本轮修改范围内。 + +## 当前实现与证据 + +1. 现有“替换结果/日志”按当前任务展示,数据来自 `tasks//events.jsonl`,已经提供分段读取和日志轮转;没有全局可搜索的操作历史页面,也没有独立的用户历史清理语义。 +2. `task_queue.execute_task_queue` 有持久化的任务级队列检查点。`QuickRunPreparer` 根据命令与状态文件重建每个工程/图页/规则的完成进度,正常情况下跳过已完成项。 +3. UI 的批次 `_project_queues` 为内存状态,重启后不保留,但**这本身不等于必然重放**:独立探针在不复用原任务队列 ID 时仍正确跳过了已完成步骤。 +4. 已复现保护缺口:`quick_run_snapshot._read_snapshot_once` 只认同时存在 `command.tsv` 和 `status.tsv` 的候选目录。状态文件缺失时,已有命令及任务的暂停状态没有阻止其返回“无证据、未中断、0 项完成”。后续继续可以重新建立执行队列。 + +### 独立复现 + +使用真实 TaskStore、TaskCenterService、QuickRunPreparer 和任务队列;CAD 执行器为受控替身。一个任务包含 2 个图页 × 2 条规则,共 4 项。先完成 2 项并暂停,关闭数据库,重新实例化服务和数据库,再继续。 + +| 重启场景 | 重启读取的完成项 | 总执行器调用 | 重复的逻辑项 | +| --- | ---: | ---: | ---: | +| 原队列 ID,状态文件完整 | 2 | 4 | 0 | +| 新队列 ID,状态文件完整 | 2 | 4 | 0 | +| 原队列 ID,状态文件缺失 | 0 | 6 | 2 | +| 新队列 ID,状态文件缺失 | 0 | 6 | 2 | + +缺失文件用例中,原状态文件仅在临时夹具内改名保存,未删除用户数据。任务重启读取仍为 paused,`interrupted=False`,随后重放了前两项。不能据此断言用户现场的文件一定缺失;现场日志与任务证据仍需用于定位其具体触发原因。 + +探针:本地 `build/research_task_resume.py`。结果:本地 `build/task-resume-research.json`。这两项不是发布组件。 + +既有回归:`python -m pytest -q tests/v1_1/test_multi_project_regression.py tests/v1_1/test_task_queue.py tests/v1_1/test_new_round.py --tb=short`:**26 passed**。 + +## 实施方式比较 + +| 方式 | 优点 | 不足 | +| --- | --- | --- | +| 在现有日志弹窗上添加搜索及清空 | 改动小 | 仍按单任务查看;日志清理容易与恢复证据混淆;无法补足丢失状态文件的防重复保护 | +| 独立操作历史页面+独立完成回执(建议) | 可跨任务查改动,重启后可查询,清空页面与续跑互不影响 | 需要明确区分用户历史、执行回执和诊断日志,并覆盖原子写入/恢复边界 | + +建议采用第二种。本方案是实现建议,尚未由用户选择实现细节。 + +## 操作历史页面 + +- 顶部增加“操作历史”导航,和“任务中心”并列;沿用 SVG、玻璃导航与实色数据表,独立于 DSH 页面。 +- 默认按时间倒序。列表包含时间、任务/CBB 工程、操作、结果、改动摘要;展开显示位号、图页、旧料号→新料号、Value/NC 改动以及对应证据来源(有记录时才显示)。 +- 搜索覆盖任务名、工程名/路径、位号、新旧料号及改动说明;提供结果筛选及分页,避免大批记录加载阻塞 UI。 +- 区分“请求执行”“实际保存并核验”“未命中”“失败”“已跳过”,不能把按钮点击或 AI 建议当作已完成修改。旧日志缺少的配置差异不追溯编造。 +- 搜索为空时展示空态,异步读取失败可以重试;清空需要确认并展示影响数量。 +- “清空历史”只清理独立的用户可见历史记录。它不删除工程、任务、备份、诊断证据、执行检查点或完成回执。清空后新操作照常记录,旧记录不会在轮询或重启后自动重新导入页面。 + +## 防重复执行 + +### 第一层:立即补齐恢复门禁 + +- 已存在命令、活动步骤或原执行痕迹,但命令/状态文件缺失、损坏、截断或相互矛盾时,不将任务当作从未执行。 +- 显示“执行记录不完整,请核验后恢复”,阻止重新准备队列和写入派发。 +- 检查必须在服务端执行入口也生效,不能只隐藏/禁用 UI 按钮。 +- 旧任务没有足够证据时保持阻断,不能根据进度数字或一条普通日志补造“已核验完成”。 + +### 第二层:持久化到每个执行项的完成回执 + +- 保存任务、轮次、原确认计划指纹、规范工程路径、图页、目标范围/UID、新旧料号、Value/NC/Cell 策略、结果与证据关联。 +- 使用同一任务和轮次内稳定的操作身份;不能只靠每次重新生成的 job_id 或单独的 CBB 文件名去重。 +- `saved_ok`、明确核验通过的恢复结果和 `no_match` 应按各自含义保留终态;失败、未核验保存和未知结果不能冒充成功。 +- 继续前核对回执、原计划和现有证据:完整完成项跳过;待执行项接续;证据缺失或不一致时停止并提示核验。 +- 进程可能在 CAD 保存完成、回执落盘之前退出,因此不能承诺跨外部 CAD 写入与数据库的严格 exactly-once。该窗口必须依靠已有 attempt/快照证据核验,不能自动重放。 +- 只有用户明确启动“新一轮”并重新确认计划,才建立新的执行范围;清空历史、重启或点击继续都不产生新轮次。 + +## 验收重点 + +1. 同任务正常暂停/重启后,已完成项的执行器调用次数不增加;只执行余下项。 +2. 不带原队列 ID 的重启仍不能重放已完成项。 +3. 命令/状态缺失、文件损坏/截断、状态互相矛盾时阻止 CAD 派发,并保留原始证据。 +4. 写入成功但回执未落盘时,进入核验流程而不是再次调用替换。 +5. 未命中、失败和未核验保存分别显示,并遵守现有恢复/重试门禁。 +6. 清空页面历史后重启,完成回执与防重复保护仍有效,旧页面记录不重新出现。 +7. 全局搜索、筛选、翻页、失败重试和清空确认可用,大记录量下主界面保持响应。 +8. 页面切换、980×700 布局、玻璃及实色回退沿用现有验收。 + +## 下一步实施顺序 + +先补缺失/损坏状态文件的最小失败回归与服务端门禁,再增加独立历史记录和完成回执,最后接入可搜索/分页/清空的历史页面。分别验证,再提交可审查的改动;不在研究阶段修改 DSH 或运行真实 CAD。 -- 2.39.5 From cf90ced8250de34c14f7adc9670cd282bb66c094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Tue, 3 Nov 2026 17:54:17 +0800 Subject: [PATCH 5/6] feat-ui-operation-history-and-durable-execution --- .../2026-11-03-operation-history-execution.md | 78 ++++ ...1-03-operation-history-execution-design.md | 2 +- .../2026-11-03-operation-history-execution.md | 50 +++ docs/user/continuous-queue.md | 6 +- docs/user/operation-history.md | 23 ++ .../dxdesigner795/QuickPartReplace.vbs | 76 ++++ .../trial_task_center/batch.py | 13 + .../trial_task_center/batch_backup.py | 27 ++ .../trial_task_center/execution_journal.py | 342 ++++++++++++++++++ .../trial_task_center/execution_proof.py | 116 ++++++ .../trial_task_center/handoff_guard.py | 39 ++ .../trial_task_center/history_recording.py | 274 ++++++++++++++ .../trial_task_center/history_store.py | 153 ++++++++ .../trial_task_center/new_round.py | 6 + .../trial_task_center/quick_run.py | 97 ++++- .../trial_task_center/quick_run_snapshot.py | 31 +- .../trial_task_center/recovery.py | 50 +-- .../trial_task_center/recovery_commit.py | 83 +++++ .../trial_task_center/retry_preparation.py | 64 ++++ .../trial_task_center/service.py | 170 +++++++-- .../trial_task_center/store_schema.py | 8 +- .../trial_task_center/task_queue.py | 18 +- .../trial_task_center/ui/execution_state.py | 3 +- .../trial_task_center/ui/icons.py | 1 + .../trial_task_center/ui/main_window.py | 29 +- .../trial_task_center/ui/operation_history.py | 283 +++++++++++++++ .../integration/test_quick_session_startup.py | 2 + tests/trial_task_center/test_recovery.py | 43 ++- tests/trial_task_center/test_store.py | 2 +- .../test_store_read_model.py | 2 +- .../ui/test_operation_history_integration.py | 46 +++ tests/ui/test_operation_history_page.py | 167 +++++++++ tests/v1_1/test_accuracy_vbs_guards.py | 71 ++++ tests/v1_1/test_execution_journal.py | 139 +++++++ tests/v1_1/test_execution_proof.py | 76 ++++ tests/v1_1/test_history_facade_concurrency.py | 31 ++ tests/v1_1/test_history_recording.py | 181 +++++++++ tests/v1_1/test_operation_history.py | 137 +++++++ tests/v1_1/test_resume_evidence.py | 96 +++++ .../v1_1/test_retry_recovery_transactions.py | 126 +++++++ tests/v1_1/test_safe_cbb_continue.py | 54 +++ tests/v1_1/test_ui_remediation.py | 2 +- tools/accept_acrylic_ui.py | 36 ++ tools/measure_task_queue.py | 4 +- 44 files changed, 3165 insertions(+), 92 deletions(-) create mode 100644 docs/superpowers/plans/2026-11-03-operation-history-execution.md create mode 100644 docs/testing/2026-11-03-operation-history-execution.md create mode 100644 docs/user/operation-history.md create mode 100644 src/dx_productivity/trial_task_center/execution_journal.py create mode 100644 src/dx_productivity/trial_task_center/execution_proof.py create mode 100644 src/dx_productivity/trial_task_center/handoff_guard.py create mode 100644 src/dx_productivity/trial_task_center/history_recording.py create mode 100644 src/dx_productivity/trial_task_center/history_store.py create mode 100644 src/dx_productivity/trial_task_center/recovery_commit.py create mode 100644 src/dx_productivity/trial_task_center/retry_preparation.py create mode 100644 src/dx_productivity/trial_task_center/ui/operation_history.py create mode 100644 tests/ui/test_operation_history_integration.py create mode 100644 tests/ui/test_operation_history_page.py create mode 100644 tests/v1_1/test_execution_journal.py create mode 100644 tests/v1_1/test_execution_proof.py create mode 100644 tests/v1_1/test_history_facade_concurrency.py create mode 100644 tests/v1_1/test_history_recording.py create mode 100644 tests/v1_1/test_operation_history.py create mode 100644 tests/v1_1/test_resume_evidence.py create mode 100644 tests/v1_1/test_retry_recovery_transactions.py create mode 100644 tests/v1_1/test_safe_cbb_continue.py diff --git a/docs/superpowers/plans/2026-11-03-operation-history-execution.md b/docs/superpowers/plans/2026-11-03-operation-history-execution.md new file mode 100644 index 0000000..d882e34 --- /dev/null +++ b/docs/superpowers/plans/2026-11-03-operation-history-execution.md @@ -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. diff --git a/docs/superpowers/specs/2026-11-03-operation-history-execution-design.md b/docs/superpowers/specs/2026-11-03-operation-history-execution-design.md index b82cf31..4c35523 100644 --- a/docs/superpowers/specs/2026-11-03-operation-history-execution-design.md +++ b/docs/superpowers/specs/2026-11-03-operation-history-execution-design.md @@ -1,6 +1,6 @@ # 操作历史与防重复执行:详细执行逻辑 -状态:设计细化,尚未实施。本文延续《operation-history-resume-research》,用户已明确重复执行场景为同一个任务继续或重启后;不修改 DSH 功能。 +状态:已实施,验证及兼容性决定见 `docs/testing/2026-11-03-operation-history-execution.md`。本文延续《operation-history-resume-research》,用户已明确重复执行场景为同一个任务继续或重启后;不修改 DSH 功能。 用户已确认:某个 CBB 失败后,只要已经证明没有残留改动且资源已释放,跳过本次失败,继续后面的工程;结果不确定或核验失败时仍停止。 diff --git a/docs/testing/2026-11-03-operation-history-execution.md b/docs/testing/2026-11-03-operation-history-execution.md new file mode 100644 index 0000000..199b3b3 --- /dev/null +++ b/docs/testing/2026-11-03-operation-history-execution.md @@ -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。 \ No newline at end of file diff --git a/docs/user/continuous-queue.md b/docs/user/continuous-queue.md index badf278..bbd564d 100644 --- a/docs/user/continuous-queue.md +++ b/docs/user/continuous-queue.md @@ -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 部分成功后的失败项重试会保留成功项。 diff --git a/docs/user/operation-history.md b/docs/user/operation-history.md new file mode 100644 index 0000000..8bbd735 --- /dev/null +++ b/docs/user/operation-history.md @@ -0,0 +1,23 @@ +# 操作历史与继续执行 + +点击顶部的“操作历史”(快捷键 `Ctrl+Shift+H`),查看任务、规则、位号计划的实际保存改动及执行、暂停、恢复结果。可按任务、工程、位号、新旧料号和说明搜索,按结果筛选,选中记录查看详情,点击“更早记录”翻页。搜索、翻页和刷新本身不生成记录。 + +“已核验完成”“未命中”“已恢复”“失败”“待核验”分别展示。恢复到旧状态不算替换成功;旧版本没有保存的字段不会补造为真实改动。 + +## 清空历史 + +“清空全部历史”清理全部历史范围,不受当前搜索条件影响。确认框显示本次清理的记录数;打开确认框后新增的记录保留。默认取消,执行或恢复期间不可清空。 + +清空不会删除任务、逐项完成回执、原生证据或工程备份,也不会使已执行项目重新运行。旧版本记录完成首次导入后才允许清空;清空后重启不会重新导入已清理记录。 + +## 同一任务继续或重启 + +每个独立工程任务保存本轮冻结范围和逐项回执。继续只处理尚未执行的项,不重复派发已完成或已确认未命中的项。只有“开始新一轮”会建立新的轮次。 + +续跑检查最近已核验图页的器件身份、属性、布局和连接状态,不拿已经合法修改过的工程与最初文件哈希机械比较。原生证据缺失、损坏或与数据库冲突时停止核验,不把进度归零后重跑。旧证据不足以证明安全时,也需要核验。 + +当前界面中的某个 CBB 明确安全失败后,保留失败项,暂缓该工程余下项,并继续后面的独立工程。结果不确定、核验失败或资源未释放时停止整队。普通“继续”不自动重试失败和暂缓项;处理原因并选择“准备失败项重试”后,保留原成功项,只执行失败和暂缓部分。 + +重试复用经校验的原始备份,不把已修改工程当作原始副本重新备份。重试队列先暂存、核验再发布;发布中断后重启会恢复原队列。已核验恢复的文件与数据库提交中断时,再次确认恢复会完成原提交,不再次调用替换。 + +新数据表随任务数据库自动升级到版本 8。更新前请正常退出工具;本次没有发布新的 EXE,也没有改变 DSH 功能。 diff --git a/integration/dxdesigner795/QuickPartReplace.vbs b/integration/dxdesigner795/QuickPartReplace.vbs index efffc55..3dc88c3 100644 --- a/integration/dxdesigner795/QuickPartReplace.vbs +++ b/integration/dxdesigner795/QuickPartReplace.vbs @@ -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", _ diff --git a/src/dx_productivity/trial_task_center/batch.py b/src/dx_productivity/trial_task_center/batch.py index 8210ae6..901b53e 100644 --- a/src/dx_productivity/trial_task_center/batch.py +++ b/src/dx_productivity/trial_task_center/batch.py @@ -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') diff --git a/src/dx_productivity/trial_task_center/batch_backup.py b/src/dx_productivity/trial_task_center/batch_backup.py index afd5e80..c736555 100644 --- a/src/dx_productivity/trial_task_center/batch_backup.py +++ b/src/dx_productivity/trial_task_center/batch_backup.py @@ -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. diff --git a/src/dx_productivity/trial_task_center/execution_journal.py b/src/dx_productivity/trial_task_center/execution_journal.py new file mode 100644 index 0000000..64587f1 --- /dev/null +++ b/src/dx_productivity/trial_task_center/execution_journal.py @@ -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 diff --git a/src/dx_productivity/trial_task_center/execution_proof.py b/src/dx_productivity/trial_task_center/execution_proof.py new file mode 100644 index 0000000..bad7299 --- /dev/null +++ b/src/dx_productivity/trial_task_center/execution_proof.py @@ -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 diff --git a/src/dx_productivity/trial_task_center/handoff_guard.py b/src/dx_productivity/trial_task_center/handoff_guard.py new file mode 100644 index 0000000..fad3975 --- /dev/null +++ b/src/dx_productivity/trial_task_center/handoff_guard.py @@ -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 diff --git a/src/dx_productivity/trial_task_center/history_recording.py b/src/dx_productivity/trial_task_center/history_recording.py new file mode 100644 index 0000000..511ad3e --- /dev/null +++ b/src/dx_productivity/trial_task_center/history_recording.py @@ -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 diff --git a/src/dx_productivity/trial_task_center/history_store.py b/src/dx_productivity/trial_task_center/history_store.py new file mode 100644 index 0000000..3c83d28 --- /dev/null +++ b/src/dx_productivity/trial_task_center/history_store.py @@ -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_utclimit 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 diff --git a/src/dx_productivity/trial_task_center/new_round.py b/src/dx_productivity/trial_task_center/new_round.py index 25422e2..f9d0276 100644 --- a/src/dx_productivity/trial_task_center/new_round.py +++ b/src/dx_productivity/trial_task_center/new_round.py @@ -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 diff --git a/src/dx_productivity/trial_task_center/quick_run.py b/src/dx_productivity/trial_task_center/quick_run.py index d400eed..ed975b6 100644 --- a/src/dx_productivity/trial_task_center/quick_run.py +++ b/src/dx_productivity/trial_task_center/quick_run.py @@ -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(): diff --git a/src/dx_productivity/trial_task_center/quick_run_snapshot.py b/src/dx_productivity/trial_task_center/quick_run_snapshot.py index c5493f6..0d4d5a7 100644 --- a/src/dx_productivity/trial_task_center/quick_run_snapshot.py +++ b/src/dx_productivity/trial_task_center/quick_run_snapshot.py @@ -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) diff --git a/src/dx_productivity/trial_task_center/recovery.py b/src/dx_productivity/trial_task_center/recovery.py index c0df6ac..f37c721 100644 --- a/src/dx_productivity/trial_task_center/recovery.py +++ b/src/dx_productivity/trial_task_center/recovery.py @@ -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) diff --git a/src/dx_productivity/trial_task_center/recovery_commit.py b/src/dx_productivity/trial_task_center/recovery_commit.py new file mode 100644 index 0000000..2b17a8c --- /dev/null +++ b/src/dx_productivity/trial_task_center/recovery_commit.py @@ -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 diff --git a/src/dx_productivity/trial_task_center/retry_preparation.py b/src/dx_productivity/trial_task_center/retry_preparation.py new file mode 100644 index 0000000..77b28ef --- /dev/null +++ b/src/dx_productivity/trial_task_center/retry_preparation.py @@ -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)) diff --git a/src/dx_productivity/trial_task_center/service.py b/src/dx_productivity/trial_task_center/service.py index a56701b..8234eb3 100644 --- a/src/dx_productivity/trial_task_center/service.py +++ b/src/dx_productivity/trial_task_center/service.py @@ -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)) diff --git a/src/dx_productivity/trial_task_center/store_schema.py b/src/dx_productivity/trial_task_center/store_schema.py index 705c2e0..c3b229d 100644 --- a/src/dx_productivity/trial_task_center/store_schema.py +++ b/src/dx_productivity/trial_task_center/store_schema.py @@ -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 = ( diff --git a/src/dx_productivity/trial_task_center/task_queue.py b/src/dx_productivity/trial_task_center/task_queue.py index 807b226..35906ef 100644 --- a/src/dx_productivity/trial_task_center/task_queue.py +++ b/src/dx_productivity/trial_task_center/task_queue.py @@ -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: diff --git a/src/dx_productivity/trial_task_center/ui/execution_state.py b/src/dx_productivity/trial_task_center/ui/execution_state.py index cb3dbd5..d16eb8a 100644 --- a/src/dx_productivity/trial_task_center/ui/execution_state.py +++ b/src/dx_productivity/trial_task_center/ui/execution_state.py @@ -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)) diff --git a/src/dx_productivity/trial_task_center/ui/icons.py b/src/dx_productivity/trial_task_center/ui/icons.py index eb8b9ad..90f14c2 100644 --- a/src/dx_productivity/trial_task_center/ui/icons.py +++ b/src/dx_productivity/trial_task_center/ui/icons.py @@ -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', diff --git a/src/dx_productivity/trial_task_center/ui/main_window.py b/src/dx_productivity/trial_task_center/ui/main_window.py index 529fb8a..b8bc651 100644 --- a/src/dx_productivity/trial_task_center/ui/main_window.py +++ b/src/dx_productivity/trial_task_center/ui/main_window.py @@ -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, diff --git a/src/dx_productivity/trial_task_center/ui/operation_history.py b/src/dx_productivity/trial_task_center/ui/operation_history.py new file mode 100644 index 0000000..05aec16 --- /dev/null +++ b/src/dx_productivity/trial_task_center/ui/operation_history.py @@ -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<=current0,"no completion proof" ''') diff --git a/tests/trial_task_center/test_recovery.py b/tests/trial_task_center/test_recovery.py index fa37ced..e20eb0a 100644 --- a/tests/trial_task_center/test_recovery.py +++ b/tests/trial_task_center/test_recovery.py @@ -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=[] diff --git a/tests/trial_task_center/test_store.py b/tests/trial_task_center/test_store.py index 605ea86..c0ed9d3 100644 --- a/tests/trial_task_center/test_store.py +++ b/tests/trial_task_center/test_store.py @@ -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() diff --git a/tests/trial_task_center/test_store_read_model.py b/tests/trial_task_center/test_store_read_model.py index 6f1c0ac..ff39d15 100644 --- a/tests/trial_task_center/test_store_read_model.py +++ b/tests/trial_task_center/test_store_read_model.py @@ -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): diff --git a/tests/ui/test_operation_history_integration.py b/tests/ui/test_operation_history_integration.py new file mode 100644 index 0000000..b3a4757 --- /dev/null +++ b/tests/ui/test_operation_history_integration.py @@ -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() diff --git a/tests/ui/test_operation_history_page.py b/tests/ui/test_operation_history_page.py new file mode 100644 index 0000000..bd8f857 --- /dev/null +++ b/tests/ui/test_operation_history_page.py @@ -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() diff --git a/tests/v1_1/test_accuracy_vbs_guards.py b/tests/v1_1/test_accuracy_vbs_guards.py index bdd0d40..1780254 100644 --- a/tests/v1_1/test_accuracy_vbs_guards.py +++ b/tests/v1_1/test_accuracy_vbs_guards.py @@ -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 diff --git a/tests/v1_1/test_execution_journal.py b/tests/v1_1/test_execution_journal.py new file mode 100644 index 0000000..bee3231 --- /dev/null +++ b/tests/v1_1/test_execution_journal.py @@ -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']) diff --git a/tests/v1_1/test_execution_proof.py b/tests/v1_1/test_execution_proof.py new file mode 100644 index 0000000..8f7b106 --- /dev/null +++ b/tests/v1_1/test_execution_proof.py @@ -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) diff --git a/tests/v1_1/test_history_facade_concurrency.py b/tests/v1_1/test_history_facade_concurrency.py new file mode 100644 index 0000000..065bdb3 --- /dev/null +++ b/tests/v1_1/test_history_facade_concurrency.py @@ -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() diff --git a/tests/v1_1/test_history_recording.py b/tests/v1_1/test_history_recording.py new file mode 100644 index 0000000..a04a727 --- /dev/null +++ b/tests/v1_1/test_history_recording.py @@ -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']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) diff --git a/tests/v1_1/test_resume_evidence.py b/tests/v1_1/test_resume_evidence.py new file mode 100644 index 0000000..92c7673 --- /dev/null +++ b/tests/v1_1/test_resume_evidence.py @@ -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 diff --git a/tests/v1_1/test_retry_recovery_transactions.py b/tests/v1_1/test_retry_recovery_transactions.py new file mode 100644 index 0000000..85dbab4 --- /dev/null +++ b/tests/v1_1/test_retry_recovery_transactions.py @@ -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 diff --git a/tests/v1_1/test_safe_cbb_continue.py b/tests/v1_1/test_safe_cbb_continue.py new file mode 100644 index 0000000..772060c --- /dev/null +++ b/tests/v1_1/test_safe_cbb_continue.py @@ -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() diff --git a/tests/v1_1/test_ui_remediation.py b/tests/v1_1/test_ui_remediation.py index 1b01174..b11f6e3 100644 --- a/tests/v1_1/test_ui_remediation.py +++ b/tests/v1_1/test_ui_remediation.py @@ -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): diff --git a/tools/accept_acrylic_ui.py b/tools/accept_acrylic_ui.py index 98fbd35..33a0cee 100644 --- a/tools/accept_acrylic_ui.py +++ b/tools/accept_acrylic_ui.py @@ -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()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() diff --git a/tools/measure_task_queue.py b/tools/measure_task_queue.py index bc99f21..e033460 100644 --- a/tools/measure_task_queue.py +++ b/tools/measure_task_queue.py @@ -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:]) -- 2.39.5 From c86b0eefa0a14274762b662b0a99550c3fda7995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Wed, 4 Nov 2026 14:14:25 +0800 Subject: [PATCH 6/6] fix-history-recording-at-service-commit-boundaries --- .../2026-11-04-pr15-history-entrypoints.md | 43 +++++ .../trial_task_center/history_recording.py | 11 +- .../trial_task_center/service.py | 41 +++-- .../trial_task_center/ui/operation_history.py | 2 + .../v1_1/test_history_service_entrypoints.py | 173 ++++++++++++++++++ 5 files changed, 249 insertions(+), 21 deletions(-) create mode 100644 docs/testing/2026-11-04-pr15-history-entrypoints.md create mode 100644 tests/v1_1/test_history_service_entrypoints.py diff --git a/docs/testing/2026-11-04-pr15-history-entrypoints.md b/docs/testing/2026-11-04-pr15-history-entrypoints.md new file mode 100644 index 0000000..2f724a6 --- /dev/null +++ b/docs/testing/2026-11-04-pr15-history-entrypoints.md @@ -0,0 +1,43 @@ +# PR #15:补齐业务变更的操作历史 + +针对[审查评论 179](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/pulls/15#issuecomment-179)。修复范围为操作历史接入,不修改 DSH 业务逻辑,也不执行真实 CAD、模型或 DSH 运行时。 + +## 根因及修复边界 + +原实现依赖 `_MUTATIONS` 手工登记入口,遗漏时直接执行业务动作,不生成历史。助手创建任务、采集位号和重新读取绑定工程因此存在历史缺口。同类入口检查还发现 Value 策略已登记,但调用路径没有经过捕获器。 + +- 助手创建:纳入普通历史事务;一次确认产生的任务、工程和规则按任务分组记录,重放同一请求不重复生成历史。 +- 采集位号:读取工程时保留原执行互斥,读取结束后仅对已有的位号保存事务捕获差异;默认料号、位号计划模式和新增位号一起记录。 +- 重新读取:文件状态检查和随后上下文读取在历史事务外,只捕获 `sync_projects` 对工程可用性等字段的实际修改。 +- Value 策略:复用统一的服务入口捕获逻辑,记录开关的前后值;重复保存相同值不生成记录。 + +采集和工程刷新使用显式的 `capture_history=False` 保留外层执行准入,并在内部短保存阶段调用历史捕获器,避免重复记录及长时间持有数据库访问锁。 + +## 回归证据 + +新增 13 项真实服务入口回归,覆盖四类操作的准确差异、助手请求重放、无变化刷新/轮询、重复采集、取消采集、四类历史写入失败时的事务回滚,以及 CAD 读取等待期间另一线程仍可写入独立历史。 + +断言按 `operation` 精确选择事件,检查实际字段前后值,避免全文搜索误命中临时工程路径造成假阳性。 + +在干净的 PR 提交 `56588a9` 源码快照上运行同一组精确记录断言,四项均因历史事件缺失失败;修复后的 13 项全部通过。未改动当前工作树源码来制造这个失败对照。 + +验证命令与结果: + +- `python -m pytest -q tests/v1_1/test_history_service_entrypoints.py`:13 项通过。 +- 历史及执行相关定向测试组:92 项通过;随后增加的 Value 策略回归包含在上述 13 项中。 +- `python -m pytest -q tests/dsh tests/packaging tests/trial_task_center/test_service.py tests/trial_task_center/test_quick_run.py tests/trial_task_center/test_store.py`:282 项通过。 +- 独立代码审查未发现可操作问题,包括 Value 策略入口及采集、刷新事务边界的补充检查。 + +### 原生脚本时限对照 + +同步工作目录中单独运行原生保护测试时,正向用例触发 `subprocess.TimeoutExpired`:`cscript.exe` 等待 Python 标记辅助程序超过测试的 15 秒限制;其他 4 个保护用例通过。直接调用同一标记程序和已生成的 VBScript 则正常返回,分别耗时 7.69 秒和 6.16 秒。 + +为区分代码差异和运行位置影响,将干净提交的源码、测试及 VBS 导出至 `F:/dxpt-ui-history-verify-20261104/entrypoint-baseline-2`,再将相同提交加本次全部 3 个源码修改导出至独立的 `native-current-snapshot`。两份快照在同步目录外运行原测试、保持原有 15 秒时限,均为 **5 项通过**(57.05 秒 / 49.54 秒)。没有改动原生保护代码或放宽测试时限。这支持运行位置/本机耗时影响的判断,尚未定位具体的系统级延迟来源。 + +### 默认全套 + +`python -m pytest -q --tb=short`:**1245 项通过、1 项超时失败、1 条现有 pywinauto STA 提示**,耗时 1169.33 秒。 + +该次超时出现在同组的 `missing-path` 参数分支,并非单独复跑时超时的正向分支,异常同为 `cscript.exe` 的 15 秒等待超时。两个版本快照的整个 5 项参数组均已通过,不能将本次默认全套描述为全部通过。原生测试、VBS、辅助程序和 DSH 源码均未修改。 + +全套结束后,在原同步工作目录中以原时限单独复跑 `-k missing-path`:**1 项通过、4 项未选择**,耗时 6.86 秒。 diff --git a/src/dx_productivity/trial_task_center/history_recording.py b/src/dx_productivity/trial_task_center/history_recording.py index 511ad3e..8dd9852 100644 --- a/src/dx_productivity/trial_task_center/history_recording.py +++ b/src/dx_productivity/trial_task_center/history_recording.py @@ -17,22 +17,25 @@ from .store_read_model import checkpoint, read_lock _MUTATIONS={ - 'create_task','create_project_tasks','create_scanned_task','update_task','delete_task', + 'create_task','create_project_tasks','create_assistant_project_tasks','create_scanned_task','update_task','delete_task', 'restore_task','purge_task','add_rule','update_rule','delete_rule','move_rule', 'replace_target_plan','import_target_plan','save_bom_batch','save_plan_drafts', 'set_value_policy','move_project_task','set_project_selected','select_all_projects', - 'clear_project_selection','scan_projects', + 'clear_project_selection','scan_projects','refresh_bound_project','collect_target_inventory', } # start_new_round deliberately records inside its existing compensating # transaction. Wrapping its filesystem moves in another transaction would let # its inner savepoint report committed before the outer history COMMIT. -_LABELS={'create_task':'新建任务','create_project_tasks':'创建工程任务','create_scanned_task':'创建工程任务', +# Inventory collection and bound-project refresh capture only their save phase; +# their external reads and subsequent context reads stay outside this transaction. +_LABELS={'create_task':'新建任务','create_project_tasks':'创建工程任务','create_assistant_project_tasks':'助手创建工程任务','create_scanned_task':'创建工程任务', 'update_task':'编辑任务','delete_task':'归档任务','restore_task':'恢复任务','purge_task':'删除任务记录', 'add_rule':'添加规则','update_rule':'修改规则','delete_rule':'删除规则','move_rule':'调整规则顺序', 'replace_target_plan':'保存位号计划','import_target_plan':'导入位号计划','save_bom_batch':'保存 BOM 计划', 'save_plan_drafts':'保存位号计划','set_value_policy':'修改 Value 策略','move_project_task':'调整任务顺序', 'set_project_selected':'修改工程选择','select_all_projects':'全选工程', - 'clear_project_selection':'清空工程选择','scan_projects':'扫描工程'} + 'clear_project_selection':'清空工程选择','scan_projects':'扫描工程','refresh_bound_project':'重新读取工程', + 'collect_target_inventory':'采集位号计划'} _TABLES={ 'tasks':(('task_id',),('task_id','name','note','discovery_root','status','copy_part_number_to_value', diff --git a/src/dx_productivity/trial_task_center/service.py b/src/dx_productivity/trial_task_center/service.py index 8234eb3..a31a2a4 100644 --- a/src/dx_productivity/trial_task_center/service.py +++ b/src/dx_productivity/trial_task_center/service.py @@ -276,9 +276,11 @@ class TaskCenterService: paths = (project.path,) except FileNotFoundError: pass - self._store.sync_projects(task_id, project.discovery_root, paths) + from .history_recording import record_operation + record_operation(self, 'refresh_bound_project', task_id, + lambda: self._store.sync_projects(task_id, project.discovery_root, paths)) return self._refresh_project_context(task_id, allow_legacy=False) - return self._idle_quick_run_operation("refresh_bound_project", task_id, refresh) + return self._idle_quick_run_operation("refresh_bound_project", task_id, refresh, capture_history=False) def _refresh_project_context(self, task_id: str, *, allow_legacy: bool, read_control=None) -> ProjectTaskContext: def refresh() -> ProjectTaskContext: @@ -478,16 +480,19 @@ class TaskCenterService: runtime=runtime, **control_options(stop_event, on_progress)) check_cancelled(stop_event) metric['completed_units'] = len(rows) - with self._store.transaction(): - if self._store.list_target_plan(task_id) != prior: - raise TaskCenterError('target_identity', '位号计划在采集时已改变,请重新采集') - current = self._store.record_target_inventory(task_id, rows) - known = {row.target.key: row for row in prior} - seeded = seed_defaults(current, rules) - result = tuple(known.get(row.target.key, row) for row in seeded) - check_cancelled(stop_event) - return self._store.replace_target_plan(task_id, result) - return self._idle_quick_run_operation("collect_target_inventory", task_id, collect) + from .history_recording import record_operation + def save_inventory(): + with self._store.transaction(): + if self._store.list_target_plan(task_id) != prior: + raise TaskCenterError('target_identity', '位号计划在采集时已改变,请重新采集') + current = self._store.record_target_inventory(task_id, rows) + known = {row.target.key: row for row in prior} + seeded = seed_defaults(current, rules) + result = tuple(known.get(row.target.key, row) for row in seeded) + check_cancelled(stop_event) + return self._store.replace_target_plan(task_id, result) + return record_operation(self, 'collect_target_inventory', task_id, save_inventory) + return self._idle_quick_run_operation("collect_target_inventory", task_id, collect, capture_history=False) def scan_projects(self, task_id: str, root: Path) -> tuple[ProjectRecord, ...]: def scan_and_sync() -> tuple[ProjectRecord, ...]: @@ -713,10 +718,7 @@ class TaskCenterService: details={"copy_part_label_to_value": enabled}) return task - from .task_queue import assert_task_queue_idle - assert_task_queue_idle(self._logging.root) - with _quick_run_lease(self._logging.root): - return self._operation("set_value_policy", task_id, update) + return self._idle_quick_run_operation("set_value_policy", task_id, update) def execute_quick_run(self, task_id: str, *, expected_item: tuple[str, str, str, str] | None = None, runtime=None) -> QuickRunStepResult: """Execute one replacement rule through restoration and verification.""" @@ -1229,12 +1231,17 @@ class TaskCenterService: name: str, task_id: str | None, action: Callable[[], _Result], + *, + capture_history: bool = True, ) -> _Result: from .task_queue import assert_task_queue_idle assert_task_queue_idle(self._logging.root) with _quick_run_lease(self._logging.root): from .history_recording import record_operation - return self._operation(name, task_id, lambda: record_operation(self, name, task_id, action)) + # External reads can capture their short save phase explicitly, + # retaining execution admission without holding the DB lock here. + wrapped = (lambda: record_operation(self, name, task_id, action)) if capture_history else action + return self._operation(name, task_id, wrapped) def _manual_operation(self, name: str, task_id: str, action: Callable[[], _Result]) -> _Result: def checked_action(): diff --git a/src/dx_productivity/trial_task_center/ui/operation_history.py b/src/dx_productivity/trial_task_center/ui/operation_history.py index 05aec16..21a2e99 100644 --- a/src/dx_productivity/trial_task_center/ui/operation_history.py +++ b/src/dx_productivity/trial_task_center/ui/operation_history.py @@ -21,6 +21,8 @@ OPERATIONS={'quick_run_executed':'替换执行','quick_run_prepared':'准备执 'quick_run_recovered':'核验恢复','quick_run_queue_paused':'暂停执行', 'quick_run_retry_prepared':'准备失败项重试','create_project_tasks':'创建工程任务', 'create_task':'新建任务','update_task':'编辑任务','delete_task':'归档任务', + 'create_assistant_project_tasks':'助手创建工程任务','collect_target_inventory':'采集位号计划', + 'refresh_bound_project':'重新读取工程', 'restore_task':'恢复任务','purge_task':'删除任务记录','add_rule':'添加规则', 'update_rule':'修改规则','delete_rule':'删除规则','move_rule':'调整规则顺序', 'replace_target_plan':'保存位号计划','save_plan_drafts':'保存位号计划', diff --git a/tests/v1_1/test_history_service_entrypoints.py b/tests/v1_1/test_history_service_entrypoints.py new file mode 100644 index 0000000..0c157e4 --- /dev/null +++ b/tests/v1_1/test_history_service_entrypoints.py @@ -0,0 +1,173 @@ +"""Real service mutations must commit their history at the same SQL boundary.""" +from concurrent.futures import ThreadPoolExecutor +from threading import Event +from types import SimpleNamespace + +import pytest + +from dx_productivity.dsh.task_creation import validate_creation_spec +from dx_productivity.trial_task_center.errors import TaskCenterError +from dx_productivity.trial_task_center.history_store import HistoryRepository +from dx_productivity.trial_task_center.logging_setup import configure_logging +from dx_productivity.trial_task_center.service import TaskCenterService +from dx_productivity.trial_task_center.store import TaskStore +from dx_productivity.trial_task_center.target_plan import ComponentTarget + + +@pytest.fixture +def case(tmp_path): + root=tmp_path/'projects' + paths=[] + for name in ('A','B'): + path=root/name/'board.prj';path.parent.mkdir(parents=True) + path.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n') + paths.append(path) + store=TaskStore(tmp_path/'tasks.db') + service=TaskCenterService(store,configure_logging(tmp_path/'data')) + spec=validate_creation_spec({'name':'Assistant history','note':'sample', + 'rules':[{'old_part_number':'OLD','new_part_number':'NEW'}]}) + yield SimpleNamespace(store=store,service=service,root=root,paths=tuple(paths),spec=spec, + history=HistoryRepository(store)) + store.close() + + +def bound_task(case): + task=case.service.create_project_tasks(case.root,selected_paths=(case.paths[0],))[0] + case.service.add_rule(task.task_id,'OLD','NEW') + target=ComponentTarget(case.paths[0],'MAIN','1','uid-1','R1','OLD','10K') + case.service.for_project_task(task.task_id)._quick_run_preparer.collect_inventory=lambda *args,**kwargs:(target,) + return task + + +def events(case,operation): + return tuple(row for row in case.history.page().rows if row['operation']==operation) + + +def test_assistant_creation_records_task_project_and_rule_changes_per_task(case): + result=case.service.create_assistant_project_tasks('a'*32,case.spec,case.root,case.paths) + rows=events(case,'create_assistant_project_tasks') + assert len(rows)==2 and {row['task_id'] for row in rows}==set(result['task_ids']) + for row in rows: + changes=row['details']['changes'] + assert {change['table'] for change in changes}=={'tasks','projects','rules'} + assert all(change['before'] is None for change in changes) + rule=next(change for change in changes if change['table']=='rules') + assert rule['after']['old_part_number']=='OLD' and rule['after']['new_part_number']=='NEW' + + +def test_assistant_request_replay_does_not_duplicate_history(case): + first=case.service.create_assistant_project_tasks('a'*32,case.spec,case.root,case.paths) + before=case.history.page().rows + replay=case.service.create_assistant_project_tasks('a'*32,case.spec,case.root,case.paths) + assert replay['replayed'] and replay['task_ids']==first['task_ids'] + assert case.history.page().rows==before + + +def test_refresh_bound_project_records_exact_availability_change(case): + task=bound_task(case) + case.paths[0].rename(case.paths[0].with_suffix('.moved')) + case.service.refresh_bound_project(task.task_id) + rows=events(case,'refresh_bound_project') + assert len(rows)==1 + change=next(change for change in rows[0]['details']['changes'] if change['table']=='projects') + assert change['before']['available']==1 and change['after']['available']==0 + assert not case.store.list_projects(task.task_id)[0].available + + +def test_unchanged_refresh_and_context_polling_do_not_manufacture_history(case): + task=bound_task(case) + before=case.history.page().rows + for _ in range(2): + case.service.refresh_bound_project(task.task_id) + case.service.task_context(task.task_id) + assert case.history.page().rows==before + + +def test_collect_inventory_records_plan_defaults_and_mode_in_one_event(case): + task=bound_task(case) + case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object()) + rows=events(case,'collect_target_inventory') + assert len(rows)==1 and rows[0]['task_id']==task.task_id + changes=rows[0]['details']['changes'] + target=next(change for change in changes if change['table']=='target_plan') + assert target['before'] is None + assert target['after']['refdes']=='R1' and target['after']['new_part_number']=='NEW' + task_change=next(change for change in changes if change['table']=='tasks') + assert task_change['before']['target_plan_mode']==0 and task_change['after']['target_plan_mode']==1 + + +def test_identical_inventory_collection_does_not_duplicate_history(case): + task=bound_task(case) + case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object()) + before=case.history.page().rows + case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object()) + assert case.history.page().rows==before + + +def test_value_policy_records_actual_change_and_ignores_repeated_value(case): + task=bound_task(case) + case.service.set_copy_part_number_to_value(task.task_id,True) + rows=events(case,'set_value_policy') + assert len(rows)==1 + change=next(change for change in rows[0]['details']['changes'] if change['table']=='tasks') + assert change['before']['copy_part_number_to_value']==0 and change['after']['copy_part_number_to_value']==1 + before=case.history.page().rows + case.service.set_copy_part_number_to_value(task.task_id,True) + assert case.history.page().rows==before + + +@pytest.mark.parametrize('operation',['create_assistant_project_tasks','collect_target_inventory','refresh_bound_project','set_value_policy']) +def test_history_write_failure_rolls_back_each_real_service_mutation(case,monkeypatch,operation): + task=None if operation=='create_assistant_project_tasks' else bound_task(case) + before=case.history.page().rows + def fail(*args,**kwargs):raise OSError('controlled history write failure') + monkeypatch.setattr(HistoryRepository,'append',fail) + with pytest.raises(TaskCenterError): + if operation=='create_assistant_project_tasks': + case.service.create_assistant_project_tasks('b'*32,case.spec,case.root,case.paths) + elif operation=='collect_target_inventory': + case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object()) + elif operation=='set_value_policy': + case.service.set_copy_part_number_to_value(task.task_id,True) + else: + case.paths[0].rename(case.paths[0].with_suffix('.moved')) + case.service.refresh_bound_project(task.task_id) + assert case.history.page().rows==before + if task is None: + assert case.store.list_tasks()==() and case.store.assistant_creation_receipt('b'*32) is None + elif operation=='collect_target_inventory': + assert case.store.list_target_plan(task.task_id)==() and not case.store.get_task(task.task_id).target_plan_mode + elif operation=='set_value_policy': + assert not case.store.get_task(task.task_id).copy_part_number_to_value + else: + assert case.store.list_projects(task.task_id)[0].available + + +def test_cancelled_inventory_read_saves_neither_plan_nor_history(case): + task=bound_task(case) + before=case.history.page().rows;stop=Event() + def collect(*args,**kwargs):stop.set();return () + case.service.for_project_task(task.task_id)._quick_run_preparer.collect_inventory=collect + with pytest.raises(TaskCenterError) as error: + case.service.collect_target_inventory(task.task_id,('OLD',),runtime=object(),stop_event=stop) + assert error.value.code=='operation_cancelled' + assert case.history.page().rows==before and not case.store.list_target_plan(task.task_id) + + +def test_history_writes_remain_available_while_cad_inventory_read_is_waiting(case): + task=bound_task(case);started=Event();release=Event() + def collect(*args,**kwargs): + started.set() + if not release.wait(5):raise RuntimeError('inventory test was not released') + return (ComponentTarget(case.paths[0],'MAIN','1','uid-1','R1','OLD','10K'),) + case.service.for_project_task(task.task_id)._quick_run_preparer.collect_inventory=collect + with ThreadPoolExecutor(max_workers=2) as pool: + inventory=pool.submit(case.service.collect_target_inventory,task.task_id,('OLD',),runtime=object()) + try: + assert started.wait(3) + other=pool.submit(case.history.append,task_id=task.task_id,task_name=task.name, + operation='probe',summary='concurrent history write') + other.result(timeout=2) + finally:release.set() + assert inventory.result(timeout=5) + assert len(events(case,'collect_target_inventory'))==1 and len(events(case,'probe'))==1 -- 2.39.5