接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。 补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。 优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。 验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。 仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
348 lines
22 KiB
Python
348 lines
22 KiB
Python
"""Exercise the staged DSH worker contract with only authored local fixtures."""
|
|
import importlib
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
import uuid
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'scripts'))
|
|
from build_dsh_application import build
|
|
|
|
|
|
class DshApplicationTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
temporary = tempfile.TemporaryDirectory(prefix='quality-dsh-application-')
|
|
cls.addClassCleanup(temporary.cleanup)
|
|
cls.application = Path(temporary.name) / 'worker'
|
|
build(cls.application)
|
|
candidate = str(cls.application / 'scripts')
|
|
sys.path.insert(0, candidate)
|
|
cls.addClassCleanup(sys.path.remove, candidate)
|
|
names = [path.stem for path in (cls.application / 'scripts').glob('*.py')]
|
|
original_modules = {name: sys.modules.pop(name) for name in names if name in sys.modules}
|
|
|
|
def restore_modules():
|
|
for name in names:
|
|
sys.modules.pop(name, None)
|
|
sys.modules.update(original_modules)
|
|
|
|
cls.addClassCleanup(restore_modules)
|
|
cls.worker = importlib.import_module('quality_analysis_runtime')
|
|
cls.context = importlib.import_module('quality_runtime_context')
|
|
cls.drafts = importlib.import_module('quality_draft')
|
|
cls.reports = importlib.import_module('quality_report_deliverable')
|
|
cls.original = importlib.import_module('quality_original_answer')
|
|
|
|
@staticmethod
|
|
def request(question, **changes):
|
|
request = {key: str(uuid.uuid4()) for key in ('turnId', 'sessionId', 'actorId', 'releaseId')}
|
|
request.update(question=question, runtimeEngine='DSH', accessToken='fixture-only-delegation',
|
|
context={'_protocol': {'version': 1, 'attemptId': request['sessionId'],
|
|
'autonomousEnabled': True}})
|
|
return {**request, **changes}
|
|
|
|
def test_independent_worker_preserves_delegation_and_publishes_original_report(self):
|
|
request = self.request('请输出黑片10月质量对比报告')
|
|
task = {'kind': 'analysis', 'executionMode': 'native_agent'}
|
|
calls = []
|
|
access_files = []
|
|
answer = '## 本期质量概况\n\n| 项目 | 本期 |\n| --- | --- |\n| 例示指标 | 42 |'
|
|
|
|
def delegate(instance, path, **kwargs):
|
|
calls.append(path)
|
|
self.assertEqual(instance.access_path.stat().st_mode & 0o777, 0o600)
|
|
access_files.append(instance.access_path)
|
|
if path == '/api/analysis/protocol/task':
|
|
self.assertEqual(kwargs['payload'], task)
|
|
return {'task': task, 'attemptId': request['sessionId']}
|
|
return {}
|
|
|
|
def execute(**options):
|
|
self.assertEqual(options['authority']['task'], task)
|
|
self.assertNotIn('accessToken', options['request'])
|
|
self.assertEqual(options['connection'][0].split(':')[1], '//127.0.0.1')
|
|
self.original.current().capture_native(answer)
|
|
options['emit']({'type': 'result', 'status': 'COMPLETED', 'answer': answer,
|
|
'agentTask': task, 'reportTitle': '10月黑片质量对比', 'protocolEvidence': [{'queryId': 'fixture'}],
|
|
'completionMethod': 'native_autonomous_analysis',
|
|
'answerQuality': {'narrativeFactBinding': 'verified_composition'},
|
|
'timeline': [{'kind': 'assistant', 'text': '临时草稿'},
|
|
{'kind': 'tool_result', 'toolCallId': 'dsh-tool-fixture'}]})
|
|
|
|
output = io.StringIO()
|
|
with (patch('setup_dsh_runtime.inspect_runtime', return_value={'ready': True}),
|
|
patch('quality_model_settings.model_configuration', return_value=('https://fixture.invalid/v1', 'fixture-key', 'fixture-model', {})),
|
|
patch.object(self.context.DelegatedApi, 'call', delegate),
|
|
patch.object(self.worker, '_plan', return_value=task),
|
|
patch('quality_native_run.run_native_exploration', side_effect=execute)):
|
|
self.worker.run_turn(request, stdin=io.StringIO(), stdout=output)
|
|
self.assertEqual(calls, ['/api/analysis/model-authorization', '/api/analysis/model-authorization',
|
|
'/api/analysis/protocol/task'])
|
|
self.assertTrue(all(not path.exists() for path in access_files))
|
|
result = json.loads(output.getvalue().splitlines()[-1])
|
|
self.assertEqual(result['engine'], 'DSH')
|
|
self.assertEqual(result['reportMarkdown'], result['answer'])
|
|
self.assertEqual(result['reportTitle'], '本期质量概况')
|
|
self.assertEqual(result['timeline'], [{'kind': 'tool_result', 'toolCallId': 'dsh-tool-fixture'}])
|
|
self.assertNotIn('fixture-only-delegation', output.getvalue())
|
|
|
|
def test_engine_and_attempt_are_pinned_before_access(self):
|
|
requests = (self.request('检查指标', runtimeEngine='legacy'),
|
|
self.request('检查指标', context={'_protocol': {'version': 1, 'attemptId': str(uuid.uuid4())}}))
|
|
with patch.object(self.context.DelegatedApi, 'call') as delegated:
|
|
for request in requests:
|
|
output = io.StringIO()
|
|
self.worker.run_turn(request, stdin=io.StringIO(), stdout=output)
|
|
self.assertEqual(json.loads(output.getvalue())['status'], 'FAILED')
|
|
delegated.assert_not_called()
|
|
|
|
def test_worker_retains_original_text_when_execution_raises(self):
|
|
request = self.request('输出质量报告')
|
|
task = {'kind': 'analysis', 'executionMode': 'native_agent'}
|
|
body = ' 模型原始内容,后续检查异常也要保留。\n'
|
|
|
|
def execute(**_options):
|
|
self.original.current().capture_native(body)
|
|
raise ValueError('fixture validation rejection')
|
|
|
|
output = io.StringIO()
|
|
with (patch('setup_dsh_runtime.inspect_runtime', return_value={'ready': True}),
|
|
patch('quality_model_settings.model_configuration', return_value=('https://fixture.invalid/v1', 'fixture-key', 'fixture-model', {})),
|
|
patch.object(self.context.DelegatedApi, 'call', return_value={'task': task, 'attemptId': request['sessionId']}),
|
|
patch.object(self.worker, '_plan', return_value=task),
|
|
patch('quality_native_run.run_native_exploration', side_effect=execute)):
|
|
self.worker.run_turn(request, stdin=io.StringIO(), stdout=output)
|
|
result = json.loads(output.getvalue().splitlines()[-1])
|
|
self.assertEqual(result['status'], 'FAILED')
|
|
self.assertEqual(result['answer'], body)
|
|
self.assertEqual(result['modelAnswer'], body)
|
|
self.assertEqual(result['reportMarkdown'], body)
|
|
|
|
def test_native_draft_replaces_and_clears_without_emitting_private_text(self):
|
|
frames = []
|
|
now = [1.0]
|
|
publisher = self.drafts.DraftPublisher(frames.append, clock=lambda: now[0])
|
|
text = '10月黑片质量指标已进入核验,正在检查异常变化和对象范围。'
|
|
publisher.update('fixture-attempt-id', text)
|
|
publisher.update('fixture-attempt-id', text + '第二个片段。')
|
|
self.assertEqual(len(frames), 1)
|
|
self.assertNotEqual(frames[0]['dshAttemptId'], 'fixture-attempt-id')
|
|
now[0] += 0.26
|
|
publisher.update('fixture-attempt-id', text + '补充结论。')
|
|
self.assertEqual(frames[1]['dshDraft'], text + '补充结论。')
|
|
publisher.update('fixture-attempt-id', text + '内部请求归属标记')
|
|
self.assertEqual(frames[-1], {'type': 'progress', 'dshDraft': ''})
|
|
|
|
def test_terminal_status_preserves_native_body_after_failed_attempt(self):
|
|
import dsh_agent_runtime
|
|
for status in ('PARTIAL', 'FAILED', 'CANCELLED'):
|
|
frames = []
|
|
body = ' ## 模型的原始判断\n\n98.04%显著低于组合乘积。\n'
|
|
with self.original.OriginalAnswer(frames.append) as original:
|
|
projection = dsh_agent_runtime._Projection(self.context.ConversationAccumulator(), frames.append)
|
|
projection._text('failed-provider-attempt', body)
|
|
projection._discard('failed-provider-attempt')
|
|
result = {'type': 'result', 'status': status, 'answer': '程序替换文本',
|
|
'readingSummary': {'overview': '固定摘要'}, 'stopReason': 'validation_failed'}
|
|
original.settle(result)
|
|
self.reports.attach(result, '输出质量分析报告')
|
|
self.assertEqual(result['answer'], body)
|
|
self.assertEqual(result['modelAnswer'], body)
|
|
self.assertEqual(result['reportMarkdown'], body)
|
|
self.assertNotIn('readingSummary', result)
|
|
self.assertEqual(result['answerValidation']['stopReason'], 'validation_failed')
|
|
self.assertTrue(any(frame.get('modelAnswer') == body for frame in frames))
|
|
|
|
def test_bounded_business_blocks_preserve_order_whitespace_and_original_title(self):
|
|
frames = []
|
|
texts = [' ## 先看这项\n原始数字999.9%。 ', '| 项目 | 结果 |\n| --- | --- |\n| X | 5 |', '最后一段\n']
|
|
raw = json.dumps({'reportTitle': '模型原始标题', 'blocks': [
|
|
{'role': 'detail', 'text': texts[0]}, {'role': 'core', 'text': texts[1]},
|
|
{'role': 'key_evidence', 'text': texts[2]}]}, ensure_ascii=False)
|
|
with self.original.OriginalAnswer(frames.append) as original:
|
|
self.original.capture(raw)
|
|
self.original.capture(json.dumps({'blocks': [{'text': '第二次修复后的替代内容'}]}))
|
|
result = {'type': 'result', 'status': 'PARTIAL', 'answer': '正式统计固定回退',
|
|
'reportTitle': '程序改写标题', 'answerQuality': {'narrativeComplete': False}}
|
|
original.settle(result)
|
|
self.assertEqual(result['answer'], '\n\n'.join(texts))
|
|
self.assertEqual(result['reportTitle'], '模型原始标题')
|
|
self.assertEqual(result['answerOrigin'], 'model_business_text')
|
|
|
|
def test_business_response_survives_invalid_composition_and_interrupted_finish(self):
|
|
import quality_native_answer
|
|
import threading
|
|
raw_body = '{"blocks":[{"text":"模型未完成的原始回复,格式未闭合'
|
|
envelope = json.dumps({'choices': [{'message': {'content': raw_body}, 'finish_reason': 'length'}]})
|
|
with self.original.OriginalAnswer(lambda _value: None) as original:
|
|
with io.BytesIO(envelope.encode()) as response:
|
|
content, _, finish = quality_native_answer._request_model(
|
|
connection=('https://fixture.invalid/v1', 'fixture-only-key', 'fixture-model'),
|
|
messages=[{'role': 'user', 'content': '查询质量'}], cancel_event=threading.Event(),
|
|
opener=lambda *_args, **_kwargs: response, phase='native_synthesis')
|
|
result = {'type': 'result', 'status': 'FAILED', 'answer': '固定回退文本'}
|
|
original.settle(result)
|
|
self.assertEqual(content, raw_body)
|
|
self.assertEqual(finish, 'length')
|
|
self.assertEqual(result['answer'], raw_body)
|
|
|
|
def test_review_metadata_does_not_replace_native_answer_and_empty_model_stays_empty(self):
|
|
frames = []
|
|
with self.original.OriginalAnswer(frames.append) as original:
|
|
original.capture_native('原始模型正文')
|
|
self.original.capture('{"blocks":[{"text":"复核模型改写"}]}')
|
|
result = {'type': 'result', 'status': 'PARTIAL', 'answer': '固定统计摘要'}
|
|
original.settle(result)
|
|
self.assertEqual(result['answer'], '原始模型正文')
|
|
with self.original.OriginalAnswer(frames.append) as original:
|
|
self.original.capture('{"reviews":[]}', phase='native_draft_review')
|
|
result = {'type': 'result', 'status': 'FAILED', 'answer': '程序生成的失败正文', 'message': '模型连接失败'}
|
|
original.settle(result)
|
|
self.assertEqual(result['answer'], '')
|
|
self.assertEqual(result['modelAnswer'], '')
|
|
self.assertEqual(result['message'], '模型连接失败')
|
|
|
|
def test_report_request_reuses_original_body_for_any_validation_status(self):
|
|
self.assertFalse(self.reports.requested('只列产品排名,不生成报告'))
|
|
self.assertTrue(self.reports.requested('不分析原因,生成报告'))
|
|
self.assertTrue(self.reports.requested('生成一份不推断原因的报告'))
|
|
for status in ('COMPLETED', 'PARTIAL', 'FAILED', 'CANCELLED'):
|
|
for question in ('生成一份对比报告', '请给我一份排查简报,优先列风险'):
|
|
result = {'type': 'result', 'status': status, 'modelAnswer': '## 模型报告\n\n原始正文。',
|
|
'answerQuality': {'narrativeComplete': False}, 'protocolEvidence': []}
|
|
self.reports.attach(result, question)
|
|
self.assertEqual(result['reportMarkdown'], result['modelAnswer'])
|
|
result = {'type': 'result', 'status': 'COMPLETED', 'modelAnswer': '原文', 'reportMarkdown': '过期的报告'}
|
|
self.reports.attach(result, '不要写报告,先核对质量数据')
|
|
self.assertNotIn('reportMarkdown', result)
|
|
|
|
def test_direct_delivery_never_recomposes_or_requires_query_evidence(self):
|
|
runner = importlib.import_module('quality_native_run')
|
|
task = {'kind': 'analysis', 'executionMode': 'native_agent', 'filters': {},
|
|
'period': {'year': 2026}, 'fields': ['rate'], 'stages': []}
|
|
request = self.request('生成质量报告')
|
|
authority = {**{key: request[key] for key in ('sessionId', 'turnId')},
|
|
'attemptId': request['sessionId'], 'revision': 1, 'executionId': str(uuid.uuid4())}
|
|
accumulator = SimpleNamespace(answer='## 模型原文\n\n未经额外核验的原始回答。',
|
|
finish_reasons=['stop'], tool_calls=[], timeline=[])
|
|
delegate = SimpleNamespace(call=lambda *args, **kwargs: {})
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
(root / 'private').mkdir()
|
|
(root / 'evidence').mkdir()
|
|
with (patch('dsh_agent_runtime.run_dsh_agent', return_value=(accumulator, 'COMPLETED', 'stop', True)),
|
|
patch('quality_evidence_finalizer.synthesize_native', side_effect=AssertionError('extra model review')),
|
|
patch('analysis_visual_evidence.protocol_visual_result', side_effect=ValueError('chart unavailable'))):
|
|
result = runner.run_native_exploration(request=request, task=task, authority=authority,
|
|
actor_root=root, delegated=delegate, connection=('http://127.0.0.1:1/v1', 'fixture', 'fixture'),
|
|
cancel_event=threading.Event(), emit=lambda event: None, prior_evidence_files=frozenset())
|
|
self.assertEqual(result['status'], 'COMPLETED')
|
|
self.assertEqual(result['answer'], accumulator.answer)
|
|
self.assertEqual(result['phaseTimingsMs']['synthesis'], 0)
|
|
self.assertNotIn('answerQuality', result)
|
|
|
|
def test_business_data_is_read_before_model_and_seeded_in_the_native_stream(self):
|
|
runner = importlib.import_module('quality_native_run')
|
|
task = {'kind': 'analysis', 'freshQuery': True, 'executionMode': 'native_agent', 'filters': {},
|
|
'period': {'year': 2026, 'month': 8}, 'fields': ['rate'], 'stages': []}
|
|
request = self.request('2026年8月综合合格率,一句话')
|
|
authority = {**{key: request[key] for key in ('sessionId', 'turnId')},
|
|
'attemptId': request['sessionId'], 'revision': 1, 'executionId': str(uuid.uuid4())}
|
|
phases = []
|
|
def data(_args):
|
|
phases.append('read')
|
|
return {'kind': 'quality_metrics', 'summary': {'compositeRate': .9557}, 'evidenceRef': 'Q12345678'}
|
|
def model(**kwargs):
|
|
phases.append('model')
|
|
self.assertEqual(kwargs['context']['initialObservation']['summary']['compositeRate'], .9557)
|
|
seeded = kwargs['initial_accumulator']
|
|
self.assertEqual(seeded.tool_calls[0]['status'], 'completed')
|
|
seeded.feed({'type':'text-delta','id':'original','delta':'模型原文:95.57%。'})
|
|
return seeded, 'COMPLETED', 'stop', True
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root=Path(directory); (root/'private').mkdir(); (root/'evidence').mkdir()
|
|
with (patch('quality_native_exploration.NativeExplorationTools.quality_data', side_effect=data),
|
|
patch('dsh_agent_runtime.run_dsh_agent', side_effect=model)):
|
|
result = runner.run_native_exploration(request=request, task=task, authority=authority,
|
|
actor_root=root, delegated=SimpleNamespace(call=lambda *args, **kwargs: {}),
|
|
connection=('http://127.0.0.1:1/v1','fixture','fixture'),cancel_event=threading.Event(),
|
|
emit=lambda event:None,prior_evidence_files=frozenset())
|
|
self.assertEqual(phases,['read','model'])
|
|
self.assertEqual(result['answer'],'模型原文:95.57%。')
|
|
self.assertEqual(result['toolCount'],1)
|
|
|
|
def test_all_failed_business_calls_keep_raw_answer_but_do_not_claim_completion(self):
|
|
runner = importlib.import_module('quality_native_run')
|
|
task = {'kind': 'analysis', 'executionMode': 'native_agent', 'filters': {},
|
|
'period': {'year': 2026}, 'fields': ['rate'], 'stages': []}
|
|
request = self.request('比较9月和10月')
|
|
authority = {**{key: request[key] for key in ('sessionId', 'turnId')},
|
|
'attemptId': request['sessionId'], 'revision': 1, 'executionId': str(uuid.uuid4())}
|
|
accumulator = SimpleNamespace(answer='原始模型说明:本轮查询没有成功。', finish_reasons=['stop'],
|
|
tool_calls=[{'input': {'mode': 'compare'}, 'status': 'failed'} for _ in range(3)], timeline=[])
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory); (root/'private').mkdir(); (root/'evidence').mkdir()
|
|
with patch('dsh_agent_runtime.run_dsh_agent', return_value=(accumulator, 'COMPLETED', 'stop', True)):
|
|
result = runner.run_native_exploration(request=request, task=task, authority=authority,
|
|
actor_root=root, delegated=SimpleNamespace(call=lambda *args, **kwargs: {}),
|
|
connection=('http://127.0.0.1:1/v1', 'fixture', 'fixture'),
|
|
cancel_event=threading.Event(), emit=lambda event: None, prior_evidence_files=frozenset())
|
|
self.assertEqual(result['status'], 'FAILED')
|
|
self.assertEqual(result['answer'], accumulator.answer)
|
|
self.assertEqual(result['stopReason'], 'tool_queries_failed')
|
|
|
|
def test_unchecked_planning_calls_model_once_without_semantic_review_or_repair(self):
|
|
planner = importlib.import_module('quality_agent_task')
|
|
context = {'_protocol': {'version': 1, 'autonomousEnabled': True},
|
|
'_businessCatalog': {'years': [2026], 'latestDataPeriod': '2026-10'}}
|
|
value = {'goal': '2026年综合合格率', 'kind': 'metrics', 'freshQuery': True,
|
|
'dimensions': [], 'stages': [], 'fields': ['rate'], 'verifySource': False,
|
|
'fullDetails': False, 'needsClarification': False, 'filterUpdates': {}, 'period': {'year': 2026}}
|
|
with patch.object(planner, '_model_json', return_value=value) as call:
|
|
result = planner.plan_task(base_url='http://fixture', api_key='fixture', model='fixture',
|
|
question='2026年综合合格率', context=context, cancel_event=threading.Event(),
|
|
checks_enabled=False, semantic_review=True)
|
|
self.assertEqual(call.call_count, 1)
|
|
self.assertEqual(result['executionMode'], 'native_agent')
|
|
with patch.object(planner, '_model_json', return_value={'kind': 'invalid'}) as call:
|
|
with self.assertRaises(planner.TaskUnavailable):
|
|
planner.plan_task(base_url='http://fixture', api_key='fixture', model='fixture',
|
|
question='q', context=context, cancel_event=threading.Event(), checks_enabled=False)
|
|
self.assertEqual(call.call_count, 1)
|
|
|
|
def test_direct_agent_planning_does_not_block_presentation_or_grouping_choices(self):
|
|
planner = importlib.import_module('quality_agent_task')
|
|
context = {'_protocol': {'version': 1, 'autonomousEnabled': True},
|
|
'_businessCatalog': {'years': [2026], 'latestDataPeriod': {'year': 2026, 'month': 10}}}
|
|
# The exact period/trend combination which stopped the real report.
|
|
proposal = {'goal': '生成最近月份质量报告', 'kind': 'analysis', 'freshQuery': True,
|
|
'dimensions': [], 'stages': ['black', 'bright'],
|
|
'fields': ['rate', 'totalWeight', 'scrapWeight', 'scrapRate'],
|
|
'verifySource': False, 'fullDetails': False, 'needsClarification': False,
|
|
'filterUpdates': {}, 'period': {'year': 2026, 'month': 10},
|
|
'trendRequired': True, 'comparisonRequired': False}
|
|
for dimensions in ([], ['group']):
|
|
with self.subTest(dimensions=dimensions), patch.object(
|
|
planner, '_model_json', return_value={**proposal, 'dimensions': dimensions}) as call:
|
|
result = planner.plan_task(base_url='http://fixture', api_key='fixture', model='fixture',
|
|
question='生成一份最近月份的质量分析报告', context=context,
|
|
cancel_event=threading.Event(), checks_enabled=False, semantic_review=True)
|
|
self.assertEqual(call.call_count, 1)
|
|
self.assertEqual(result['period'], proposal['period'])
|
|
self.assertTrue(result['trendRequired'])
|
|
self.assertEqual(result['dimensions'], dimensions)
|
|
self.assertEqual(result['executionMode'], 'native_agent')
|
|
self.assertNotIn('queryPlan', result)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|