feat: embed native Harness assistant and verify live task model

This commit is contained in:
阿白叔 committed 2026-10-30 11:32:16 +08:00
1 parent 9c508333bb
commit 96227eed2e
29 files changed
+11110 -582

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
import json
from dx_productivity.dsh.native_runtime import launch_environment, write_context
from dx_productivity.dsh.snapshot import TaskSnapshot
def test_context_publication_has_revision_and_no_unrelated_progress_fields(tmp_path):
value = write_context(tmp_path, TaskSnapshot('t', 'paused', {'completed': 2, 'api_key': 'secret'}))
assert value['revision']
assert 'secret' not in json.dumps(value)
assert json.loads((tmp_path / 'task-context.json').read_text(encoding='utf8')) == value
def test_launch_does_not_inherit_unrelated_credentials_or_user_harness_home(tmp_path, monkeypatch):
monkeypatch.setenv('OPENAI_API_KEY', 'unrelated-secret')
monkeypatch.setenv('DSH_HOME', 'unrelated-home')
env = launch_environment(tmp_path)
assert 'OPENAI_API_KEY' not in env
assert 'DSH_HOME' not in env
assert env['DX_DSH_HOME'].startswith(str(tmp_path))
+12
View File
@@ -0,0 +1,12 @@
from dx_productivity.dsh.provider_config import load_provider, save_provider
def test_provider_key_is_encrypted_and_models_survive_reload(tmp_path):
key = 'test-credential-not-a-real-key'
save_provider(tmp_path, base_url='https://example.test/v1/', models=['a', 'b'], default_model='b', api_key=key)
assert key not in (tmp_path / 'provider.json').read_text(encoding='utf8')
assert key.encode() not in (tmp_path / 'provider-key.dpapi').read_bytes()
restored = load_provider(tmp_path)
assert restored['DSH_API_KEY'] == key
assert restored['DSH_MODEL'] == 'b'
assert restored['DSH_API_BASE'] == 'https://example.test/v1'
+46
View File
@@ -0,0 +1,46 @@
from PySide6.QtCore import Signal
from PySide6.QtWidgets import QWidget
class HarnessView(QWidget):
action_requested = Signal(object)
def __init__(self, parent):
super().__init__(parent)
self.starts = 0
self.binds = []
def start(self):
self.starts += 1
def bind(self, snapshot, **kwargs):
self.binds.append(snapshot)
self.context = {'task_id': snapshot.task_id, 'revision': 'rev'}
def shutdown(self):
pass
def test_menu_opens_embedded_harness_and_returns_without_losing_task(ui, monkeypatch):
window = ui.window
monkeypatch.setattr(type(window), '_create_dsh_view', lambda self: HarnessView(self), raising=False)
task_id = window._task_id
window.dsh_action.trigger()
assert window.content_stack.currentWidget() is window._dsh_view
assert window._dsh_view.binds[-1].task_id == task_id
assert window._dsh_view.starts == 1
window.task_center_action.trigger()
assert window.content_stack.currentIndex() == 0
assert window._task_id == task_id
window.dsh_action.trigger()
assert window._dsh_view.starts == 1
def test_cross_task_harness_action_never_reaches_task_service(ui, monkeypatch):
window = ui.window
monkeypatch.setattr(type(window), '_create_dsh_view', lambda self: HarnessView(self), raising=False)
window.dsh_action.trigger()
called = []
monkeypatch.setattr(window, '_retry_failed', lambda: called.append('retry'))
window._handle_dsh_action({'action': 'retry_failed', 'task_id': 'another', 'revision': 'rev'})
assert not called
-104
View File
@@ -1,104 +0,0 @@
"""DSH desktop layout and usable conversation lifecycle without model/CAD access."""
from threading import Event
from PySide6.QtCore import Qt
from dx_productivity.dsh.snapshot import TaskSnapshot
from dx_productivity.trial_task_center.ui.dsh_panel import DshPanel
def make_panel(qtbot, **kwargs):
panel = DshPanel(None, snapshot=TaskSnapshot('board-a', 'paused'), **kwargs)
qtbot.addWidget(panel)
panel.show()
return panel
def test_desktop_has_three_resizable_columns_and_initial_context(qtbot):
panel = make_panel(qtbot)
assert panel.workspace.orientation() == Qt.Orientation.Horizontal
assert panel.workspace.count() == 3
assert all(size > 0 for size in panel.workspace.sizes())
assert panel.windowTitle() == 'DSH助手'
assert 'board-a' in panel.context_text.toPlainText()
assert '暂停' in panel.issue.text()
def test_new_conversation_preserves_previous_local_history(qtbot):
panel = make_panel(qtbot)
panel.input.setPlainText('分析当前异常')
qtbot.mouseClick(panel.send, Qt.MouseButton.LeftButton)
qtbot.waitUntil(lambda: panel.send.isEnabled())
assert '未配置' in panel.chat.toPlainText()
qtbot.mouseClick(panel.new_chat_button, Qt.MouseButton.LeftButton)
assert panel.conversations.count() == 2
assert '分析当前异常' not in panel.chat.toPlainText()
panel.conversations.setCurrentRow(1)
assert '分析当前异常' in panel.chat.toPlainText()
def test_model_call_does_not_block_ui_or_mix_conversations(qtbot):
entered, release = Event(), Event()
class Client:
def chat(self, message, snapshot):
entered.set()
assert release.wait(5)
return {'reply': '<b>模型原文</b>', 'actions': []}
panel = make_panel(qtbot, client=Client())
panel.input.setPlainText('检查失败原因')
try:
qtbot.mouseClick(panel.send, Qt.MouseButton.LeftButton)
qtbot.waitUntil(entered.is_set)
assert not panel.send.isEnabled()
assert panel.stop_button.isEnabled()
qtbot.mouseClick(panel.new_chat_button, Qt.MouseButton.LeftButton)
release.set()
qtbot.waitUntil(lambda: not panel._workers)
assert '模型原文' not in panel.chat.toPlainText()
panel.conversations.setCurrentRow(1)
assert '<b>模型原文</b>' in panel.chat.toPlainText()
finally:
release.set()
def test_stopped_reply_is_not_displayed(qtbot):
release = Event()
class Client:
def chat(self, *_):
release.wait(5)
return {'reply': '迟到的回复', 'actions': []}
panel = make_panel(qtbot, client=Client())
panel.input.setPlainText('提问')
try:
qtbot.mouseClick(panel.send, Qt.MouseButton.LeftButton)
qtbot.mouseClick(panel.stop_button, Qt.MouseButton.LeftButton)
release.set()
qtbot.waitUntil(lambda: not panel._workers)
assert '迟到的回复' not in panel.chat.toPlainText()
assert '停止' in panel.chat.toPlainText()
finally:
release.set()
def test_escape_closes_panel_and_discards_late_reply(qtbot):
release = Event()
class Client:
def chat(self, *_):
release.wait(5)
return {'reply': '窗口关闭后的回复', 'actions': []}
panel = make_panel(qtbot, client=Client())
panel.input.setPlainText('分析')
try:
qtbot.mouseClick(panel.send, Qt.MouseButton.LeftButton)
qtbot.keyClick(panel, Qt.Key.Key_Escape)
release.set()
qtbot.waitUntil(lambda: not panel._workers)
assert not panel.isVisible()
assert '窗口关闭后的回复' not in panel.chat.toPlainText()
finally:
release.set()