227 lines
8.9 KiB
Python
227 lines
8.9 KiB
Python
import json
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
from PySide6.QtCore import Qt, Signal
|
|
|
|
from dx_productivity.trial_task_center.ui import dsh_automation as module
|
|
from .test_dsh_native_menu import HarnessView
|
|
|
|
pytestmark = pytest.mark.usefixtures('ai_ready')
|
|
|
|
class SlowHarness(HarnessView):
|
|
diagnosis_event = Signal(object)
|
|
|
|
def __init__(self, parent):
|
|
super().__init__(parent)
|
|
self.requests, self.cancellations = [], []
|
|
self.is_running = True
|
|
|
|
def request_diagnosis(self, request):
|
|
self.requests.append(request)
|
|
|
|
def cancel_diagnosis(self, request_id):
|
|
self.cancellations.append(request_id)
|
|
|
|
def shutdown(self):
|
|
self.is_running = False
|
|
self.stopped.emit()
|
|
|
|
|
|
@pytest.fixture
|
|
def slow_diagnosis(ui, qtbot, monkeypatch):
|
|
ui.settle()
|
|
window = ui.window
|
|
published = []
|
|
monkeypatch.setattr(window, '_create_dsh_view', lambda: SlowHarness(window))
|
|
monkeypatch.setattr(module, 'GitIssuePublisher', lambda path: lambda *args: published.append(args) or {'status': 'unavailable'})
|
|
automation = module.DshAutomation(window)
|
|
window._dsh_automation = automation
|
|
|
|
def start(*, read_only=True, cleanup=False):
|
|
automation.enqueue('', 'batch_resources_pending' if cleanup else 'operation_stalled', 'controlled slow diagnosis',
|
|
read_only=read_only, cleanup_allowed=cleanup, snapshot_override={'task_id': '', 'status': 'unknown'})
|
|
automation._drain()
|
|
qtbot.waitUntil(lambda: bool(window._dsh_view.requests), timeout=5000)
|
|
return window._dsh_view
|
|
|
|
yield window, automation, published, start
|
|
if automation.worker is not None:
|
|
if hasattr(automation, 'cancel'):
|
|
automation.cancel()
|
|
else:
|
|
for request_id in list(automation.waiters):
|
|
automation._diagnosis_event({'request_id': request_id, 'status': 'failed', 'error': 'test_cleanup'})
|
|
qtbot.waitUntil(lambda: automation.worker is None, timeout=5000)
|
|
if not window._closed:
|
|
ui.settle()
|
|
|
|
|
|
def test_readonly_diagnosis_keeps_task_controls_usable_and_can_be_cancelled(slow_diagnosis, qtbot, ui):
|
|
window, automation, published, start = slow_diagnosis
|
|
view = start()
|
|
assert automation.worker not in window._workers
|
|
assert window.task_list.list.isEnabled()
|
|
assert window.task_list.new_button.isEnabled()
|
|
assert window.ai_status_indicator.property('state') == 'working'
|
|
assert window.ai_cancel_action.isVisible()
|
|
window.ai_cancel_action.trigger()
|
|
qtbot.waitUntil(lambda: automation.worker is None, timeout=5000)
|
|
assert view.cancellations
|
|
assert automation.latest['status'] == 'cancelled'
|
|
assert not published
|
|
assert not window.ai_cancel_action.isVisible()
|
|
ui.settle()
|
|
|
|
|
|
def test_readonly_diagnosis_does_not_prevent_window_close(slow_diagnosis, qtbot):
|
|
window, automation, published, start = slow_diagnosis
|
|
start()
|
|
window.close()
|
|
qtbot.waitUntil(lambda: window._closed and automation.worker is None, timeout=5000)
|
|
assert not window._close_pending
|
|
assert not published
|
|
|
|
|
|
@pytest.mark.parametrize('reason', ['unsaved', 'saving'])
|
|
def test_cancelled_close_confirmation_keeps_diagnosis_running(slow_diagnosis, qtbot, monkeypatch, ui, reason):
|
|
window, automation, _, start = slow_diagnosis
|
|
start()
|
|
if reason == 'unsaved':
|
|
monkeypatch.setattr(window.plan_panel, 'has_unsaved_plans', lambda: True)
|
|
monkeypatch.setattr(window, '_close_plan_confirmer', lambda: 'cancel')
|
|
else:
|
|
window._close_saving = True
|
|
window.close()
|
|
assert not window._closed
|
|
assert not automation.cancel_event.is_set()
|
|
window._close_saving = False
|
|
automation.cancel()
|
|
qtbot.waitUntil(lambda: automation.worker is None, timeout=5000)
|
|
ui.settle()
|
|
|
|
|
|
def test_resource_diagnosis_has_no_cad_pause_or_force_controls(slow_diagnosis, qtbot, ui):
|
|
window, automation, published, start = slow_diagnosis
|
|
view = start(read_only=False, cleanup=True)
|
|
assert automation.worker not in window._workers
|
|
assert window._batch_stop is None
|
|
assert not window.run_panel.stop_queue_button.isVisible()
|
|
assert not window.run_panel.force_stop_button.isVisible()
|
|
window.ai_cancel_action.trigger()
|
|
qtbot.waitUntil(lambda: automation.worker is None, timeout=5000)
|
|
assert view.cancellations and not published
|
|
ui.settle()
|
|
|
|
|
|
def test_model_wait_is_limited_by_remaining_total_budget(slow_diagnosis, monkeypatch, ui):
|
|
window, automation, _, _ = slow_diagnosis
|
|
window._open_dsh()
|
|
waits = []
|
|
|
|
class Complete:
|
|
def wait(self, timeout):
|
|
waits.append(timeout)
|
|
return False
|
|
|
|
monkeypatch.setattr(module, 'Event', Complete)
|
|
monkeypatch.setattr(module, 'monotonic', lambda: 100.0)
|
|
automation._analysis_deadline = 100.1
|
|
with pytest.raises(TimeoutError):
|
|
automation._analyze('a' * 24, {'task_id': ''}, 'evidence', 'reflect')
|
|
assert 0 < waits[0] <= 0.1
|
|
ui.settle()
|
|
|
|
|
|
def test_model_budget_starts_when_harness_session_is_ready(slow_diagnosis, monkeypatch):
|
|
_, automation, _, _ = slow_diagnosis
|
|
complete = module.Event()
|
|
automation.waiters['request'] = (complete, {})
|
|
monkeypatch.setattr(module, 'monotonic', lambda: 100.0)
|
|
automation._analysis_deadline = None
|
|
automation._diagnosis_event({'request_id': 'request', 'status': 'started', 'session_id': 'session'})
|
|
assert automation._analysis_deadline == 100.0 + automation._diagnosis_budget_seconds()
|
|
|
|
|
|
def test_diagnosis_pins_the_same_route_as_readiness(slow_diagnosis):
|
|
window, automation, _, _ = slow_diagnosis
|
|
window._open_dsh()
|
|
selection = {'provider': 'configured', 'model': 'saved-model'}
|
|
automation._analysis_model_selection = selection
|
|
request = {'request_id': 'controlled-model-route'}
|
|
automation._request_model(request)
|
|
assert window._dsh_view.requests[-1]['model_selection'] == selection
|
|
assert 'model_selection' not in request
|
|
|
|
|
|
def test_cold_session_wait_does_not_consume_shared_model_budget(slow_diagnosis, monkeypatch, ui):
|
|
window, automation, _, _ = slow_diagnosis
|
|
monkeypatch.setattr(automation, '_diagnosis_budget_seconds', lambda: 180)
|
|
window._open_dsh()
|
|
clock, waits = [100.0], []
|
|
|
|
class ControlledEvent:
|
|
def __init__(self):
|
|
self.set_flag = False
|
|
|
|
def set(self):
|
|
self.set_flag = True
|
|
|
|
def wait(self, timeout):
|
|
waits.append(timeout)
|
|
if timeout == 300:
|
|
clock[0] += 75 # Controlled cold session creation.
|
|
request_id = next(iter(automation.waiters))
|
|
automation._diagnosis_event({'request_id': request_id, 'status': 'started', 'session_id': 'session'})
|
|
automation._diagnosis_event({'request_id': request_id, 'status': 'completed', 'reply': '{"action":"none"}'})
|
|
return self.set_flag
|
|
|
|
monkeypatch.setattr(module, 'Event', ControlledEvent)
|
|
monkeypatch.setattr(module, 'monotonic', lambda: clock[0])
|
|
result = automation._analyze('a' * 24, {'task_id': ''}, 'evidence', 'diagnose')
|
|
assert waits == [300, 180]
|
|
assert '_ready' not in result
|
|
clock[0] += 40
|
|
with pytest.raises(TimeoutError):
|
|
automation._analyze('a' * 24, {'task_id': ''}, 'evidence', 'reflect')
|
|
assert waits[-1] == 140
|
|
assert window._dsh_view.requests[-1]['model_budget_ms'] == 140000
|
|
ui.settle()
|
|
|
|
|
|
def test_native_cancel_removes_queued_request_and_sends_only_target_id():
|
|
from dx_productivity.trial_task_center.ui.dsh_native import NativeHarnessView
|
|
sent = []
|
|
native = SimpleNamespace(_diagnosis_queue=[{'request_id': 'cancel'}, {'request_id': 'keep'}],
|
|
_started=False, is_running=True, process=SimpleNamespace(write=sent.append))
|
|
NativeHarnessView.cancel_diagnosis(native, 'cancel')
|
|
assert native._diagnosis_queue == [{'request_id': 'keep'}]
|
|
assert not sent
|
|
native._started = True
|
|
NativeHarnessView.cancel_diagnosis(native, 'active')
|
|
assert json.loads(sent[0]) == {'type': 'diagnose_cancel', 'request_id': 'active'}
|
|
|
|
|
|
def test_completed_incident_removes_embedded_cancel_action(slow_diagnosis, qtbot, ui):
|
|
from dx_productivity.trial_task_center.ui.workers import ServiceWorker
|
|
window, automation, _, _ = slow_diagnosis
|
|
window._open_dsh()
|
|
automation._current_identity = ('', 'operation_stalled', 'blocker-completed')
|
|
automation._owns_execution_lane = False
|
|
automation._start_worker(ServiceWorker(lambda: {'status': 'observed_cleared', 'task_id': ''}))
|
|
qtbot.waitUntil(lambda: automation.worker is None)
|
|
assert window._dsh_view.automation['can_cancel'] is False
|
|
assert not window.ai_cancel_action.isVisible()
|
|
ui.settle()
|
|
|
|
|
|
def test_optional_automation_without_cancel_api_keeps_gates_and_close_usable(ui, monkeypatch, qtbot):
|
|
ui.settle()
|
|
window = ui.window
|
|
monkeypatch.setattr(window, '_dsh_automation', SimpleNamespace(enqueue=lambda *a, **kw: True))
|
|
window._update_gates()
|
|
assert not window.ai_cancel_action.isVisible()
|
|
window._cancel_dsh_diagnosis()
|
|
window.close()
|
|
qtbot.waitUntil(lambda: window._closed)
|