from types import SimpleNamespace import pytest from dx_productivity.trial_task_center.ui.dsh_blockers import DshBlockerCoordinator pytestmark = pytest.mark.usefixtures('ai_ready') def test_blocker_report_is_deduplicated_until_cleared(): events = [] window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: True)) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(value) or True) assert coordinator.report('bom', 'task-1', 'bom_validation_failed', 'bad row', {'failed_rows': 1}) assert not coordinator.report('bom', 'task-1', 'bom_validation_failed', 'bad row', {'failed_rows': 1}) assert len(events) == 1 coordinator.clear('bom', 'task-1') assert coordinator.report('bom', 'task-1', 'bom_validation_failed', 'bad row', {'failed_rows': 1}) assert len(events) == 2 def test_disabled_switch_keeps_blocker_until_enabled(): events = [] state = {'enabled': False} window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: state['enabled'])) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(value) or True) assert not coordinator.report('resources', '', 'batch_resources_pending', 'still held') assert not events state['enabled'] = True coordinator.replay_pending() assert len(events) == 1 assert events[0]['issue_code'] == 'batch_resources_pending' def test_different_blockers_and_tasks_get_separate_events(): events = [] window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: True)) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(value) or True) coordinator.report('preflight', 'task-1', 'preflight_blocked', 'missing plan') coordinator.report('preflight', 'task-2', 'preflight_blocked', 'missing plan') coordinator.report('resources', '', 'batch_resources_pending', 'held') assert [(e['source'], e['task_id']) for e in events] == [ ('preflight', 'task-1'), ('preflight', 'task-2'), ('resources', '')] def test_progress_prevents_stall_but_stuck_worker_reports_once(): events = [] window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: True)) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(value) or True) worker = object() coordinator.watch(worker, '导入 BOM', 'task-1', 'bom', now=0) coordinator.progress('reading row 1', worker, now=100) coordinator.check_stalled(now=121) assert not events coordinator.check_stalled(now=221) coordinator.check_stalled(now=230) assert len(events) == 1 assert events[0]['read_only'] is True assert events[0]['details']['area'] == 'bom' coordinator.unwatch(worker) assert not coordinator.active @pytest.mark.parametrize('area', ['query', 'operations', 'task-creation']) def test_operation_progress_does_not_hide_stuck_query_and_recovery_starts_new_episode(area): events = [] window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: True)) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(value) or True) operation, query = object(), object() coordinator.watch(operation, '导入 BOM', 'task-1', 'bom', now=0) coordinator.watch(query, '读取资源', '', area, now=0) coordinator.progress('row 2', operation, now=100) coordinator.check_stalled(now=121) assert len(events) == 1 and events[0]['task_id'] == '' coordinator.progress('resource reply received', query, now=130) assert not coordinator.active coordinator.progress('row 3', operation, now=230) coordinator.check_stalled(now=251) assert len(events) == 2 assert events[0]['run_id'] != events[1]['run_id'] def test_resource_states_enter_ai_once_and_recover(ui): window = ui.window events = [] window._dsh_blockers.enqueue = lambda **value: events.append(value) or True window._resource_status.known = True window._resource_status.records = ({'batch_id': 'owned-batch', 'pid': 123},) window._observe_dsh_resource_blockers() window._observe_dsh_resource_blockers() assert len(events) == 1 assert events[0]['issue_code'] == 'batch_resources_pending' assert events[0]['cleanup_allowed'] is True window._resource_status.records = () window._observe_dsh_resource_blockers() window._resource_status._failed(OSError('resource query failed')) assert len(events) == 1 window._resource_status._failed(OSError('resource query failed')) assert events[-1]['issue_code'] == 'resource_inspection_failed' assert len(events) == 2 window._resource_status.error = None window._observe_dsh_resource_blockers() assert not window._dsh_blockers.active def test_resources_owned_by_running_operation_are_not_release_failures(ui): from dx_productivity.trial_task_center.ui.operation_state import OperationState window = ui.window events = [] window._dsh_blockers.enqueue = lambda **value: events.append(value) or True active = object() window._workers.add(active) window.operation_state = OperationState.start('导入 BOM', window._task_id, 'bom') window._resource_status.known = True window._resource_status.records = ({'state': 'ready', 'session_id': 'active-owned-session'},) try: window._observe_dsh_resource_blockers() assert not events window._resource_status.records = ({'state': 'blocked', 'session_id': 'active-owned-session'},) window._observe_dsh_resource_blockers() assert len(events) == 1 finally: window._workers.discard(active) window._resource_status.records = () def test_ai_retry_healthy_resources_are_not_release_failures(ui, monkeypatch): window = ui.window events = [] window._dsh_blockers.enqueue = lambda **value: events.append(value) or True active = object() monkeypatch.setattr(window, '_dsh_automation', SimpleNamespace(worker=active, _owns_execution_lane=True, latest={'status': 'retrying'}, cancel_pending=lambda value: None, can_cancel=False, cancel=lambda: None)) window._workers.add(active) window._resource_status.known = True window._resource_status.records = ({'state': 'ready', 'session_id': 'auto-owned-session'},) try: window._observe_dsh_resource_blockers() assert not events finally: window._workers.discard(active) window._resource_status.records = () def test_clear_removes_queued_blocker_and_disabled_replay_retains_episode(): events, cancelled = [], [] window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: True)) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(value) or True, cancelled.append) coordinator.report('resources', '', 'batch_resources_pending', 'held') run_id = events[0]['run_id'] coordinator.set_enabled(False) coordinator.set_enabled(True) assert events[-1]['run_id'] == run_id coordinator.clear('resources') assert cancelled == [run_id] def test_readonly_diagnosis_starts_while_worker_is_stuck_and_keeps_controls(ui, monkeypatch, qtbot): from dx_productivity.trial_task_center.stop_control import StopControl from dx_productivity.trial_task_center.ui import dsh_automation as module ui.settle() window = ui.window original = object() stop = StopControl() window._workers.add(original) window._batch_stop = stop window._active_quick_run_task_id = window._task_id monkeypatch.setattr(window, '_open_dsh', lambda: None) monkeypatch.setattr(module, 'GitIssuePublisher', lambda path: lambda *args: {'status': 'unavailable'}) automation = module.DshAutomation(window) window._dsh_automation = automation try: assert automation.enqueue(window._task_id, 'operation_stalled', 'stuck', read_only=True, snapshot_override={'task_id': window._task_id, 'status': 'running', 'issue_code': 'operation_stalled', 'issue': 'stuck'}) automation._drain() assert automation.worker is not None qtbot.waitUntil(lambda: automation.worker is None, timeout=10000) assert window._workers == {original} assert window._batch_stop is stop assert window._active_quick_run_task_id == window._task_id assert not stop.is_set() finally: window._workers.discard(original) window._batch_stop = None window._active_quick_run_task_id = None def test_resource_diagnosis_keeps_previous_cad_stop_intent(ui, monkeypatch, qtbot): from dx_productivity.trial_task_center.stop_control import StopControl from dx_productivity.trial_task_center.ui import dsh_automation as module ui.settle() window = ui.window previous_stop = StopControl() previous_stop.set() window._dsh_run_stop = previous_stop monkeypatch.setattr(window, '_open_dsh', lambda: None) monkeypatch.setattr(module, 'GitIssuePublisher', lambda path: lambda *args: {'status': 'unavailable'}) automation = module.DshAutomation(window) window._dsh_automation = automation assert automation.enqueue('', 'batch_resources_pending', 'held', snapshot_override={ 'task_id': '', 'status': 'blocked'}, cleanup_allowed=True) automation._drain() qtbot.waitUntil(lambda: automation.worker is None, timeout=10000) assert window._dsh_run_stop is previous_stop assert window._dsh_run_stop.is_set() @pytest.mark.parametrize('complete_while_disabled', [False, True]) def test_completed_blocker_restarts_only_after_explicit_switch_cycle(complete_while_disabled): events = [] state = {'enabled': True} window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: state['enabled'])) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(dict(value)) or True) coordinator.report('bom', 'task-1', 'bom_validation_failed', 'bad row') original_run = events[0]['run_id'] if not complete_while_disabled: coordinator.completed(original_run) for _ in range(3): coordinator.report('bom', 'task-1', 'bom_validation_failed', 'bad row') coordinator.replay_pending() assert len(events) == 1 state['enabled'] = False coordinator.set_enabled(False) if complete_while_disabled: coordinator.completed(original_run) state['enabled'] = True coordinator.set_enabled(True) assert len(events) == 2 and events[-1]['run_id'] != original_run coordinator.report('bom', 'task-1', 'bom_validation_failed', 'bad row') assert len(events) == 2 def test_completed_while_disabled_restarts_once_but_healthy_clear_cancels(): events, cancelled = [], [] state = {'enabled': True} window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: state['enabled'])) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(dict(value)) or True, cancelled.append) coordinator.report('resources', '', 'batch_resources_pending', 'held') run_id = events[0]['run_id'] state['enabled'] = False coordinator.set_enabled(False) coordinator.completed(run_id) coordinator.clear('resources') state['enabled'] = True coordinator.set_enabled(True) assert len(events) == 1 and cancelled == [run_id] def test_progress_only_resets_actual_worker_even_in_same_area(): events = [] window = SimpleNamespace(auto_intervention_check=SimpleNamespace(isChecked=lambda: True)) coordinator = DshBlockerCoordinator(window, lambda **value: events.append(dict(value)) or True) first, second = object(), object() coordinator.watch(first, 'first BOM', 'task-1', 'bom', now=0) coordinator.watch(second, 'second BOM', 'task-2', 'bom', now=0) coordinator.progress('row 2', first, now=100) coordinator.check_stalled(now=121) assert [event['task_id'] for event in events] == ['task-2'] with pytest.raises(TypeError): coordinator.progress('unscoped progress', now=130) def test_global_resource_evidence_actions_work_with_a_task_selected(ui, monkeypatch): window = ui.window calls = [] monkeypatch.setattr(window, '_dsh_view', SimpleNamespace(context={'revision': 'r'}, shutdown=lambda: None)) monkeypatch.setattr(window, '_dsh_automation', SimpleNamespace( latest={'task_id': '', 'incident_id': 'a' * 24}, resubmit=lambda value: calls.append(value), can_cancel=False, cancel=lambda: None)) window._handle_dsh_action({'task_id': window._task_id, 'revision': 'r', 'action': 'retry_issue'}) assert calls == ['a' * 24]