修复:DSH 自主查询、图片直显与报告稳定性

允许 Agent 在循环中查目录、修正查询和提问,保留实时权限检查及模型原始回答。
统一相同查询证据与图表资源;排名补齐阶段指标,修正 OR 重量筛选,并限制模型明细输入。
增加运行恢复、图表自适应坐标轴与两位小数,修复报告主图选择、重复图及边缘标签。

验证:独立副本通过 162 项 Python、73 项前端及 21 项后端测试,前端构建通过。
线上已用原 8 题复测,并追加两轮双期对比;TOP100 与组合筛选分别核对 100 行和 20 行。
This commit is contained in:
2026-11-12 16:14:20 +08:00
parent f1a2fbd494
commit bc19475b29
35 changed files with 983 additions and 114 deletions
+57 -4
View File
@@ -9,7 +9,7 @@ import re
def initial_read_arguments(task, question):
"""Read the confirmed business frame before asking a model for prose."""
if task.get('kind') == 'report' or task.get('freshQuery') is not True or not task.get('fields'):
if task.get('agentDriven') or task.get('kind') == 'report' or task.get('freshQuery') is not True or not task.get('fields'):
return None
dimensions = task.get('dimensions') or []
if task.get('aggregateSelection') and not task.get('comparisonPeriod') and not task.get('comparisonRequired'):
@@ -40,6 +40,16 @@ def _documents(directory, excluded):
return documents
def image_resources(visuals):
"""工具只提供由正式数据实际构造的本轮图表资源。"""
return [{'src': 'chart:' + visual['id'], 'alt': visual['title'],
'scope': copy.deepcopy(visual.get('scope', {})),
'period': copy.deepcopy(visual.get('period', {}))}
for visual in visuals.get('visuals', [])
if isinstance(visual, dict) and isinstance(visual.get('id'), str)
and isinstance(visual.get('title'), str)]
def run_native_exploration(*, request, task, authority, actor_root, delegated, connection,
cancel_event, emit, prior_evidence_files, phase_timings=None):
from dsh_agent_runtime import run_dsh_agent
@@ -76,7 +86,16 @@ def run_native_exploration(*, request, task, authority, actor_root, delegated, c
definition = tool_definition(native_protocol=True, task=task, release_id=request['releaseId'])
allowed_modes = definition['inputSchema']['properties']['mode']['enum']
context['toolPolicy'] = {'allowedModes': allowed_modes,
'guidance': '总体query一次已含全部阶段率、逐月趋势、投入、报废和正式大类。query不传stage,period及comparisonPeriod可省略,由运行时带入;若传必须用对象,不用JSON字符串。需要产业部、业务分组时分别query(view=department/group)一次批量汇总。排名用rank且metric必填:投入重量totalWeight,报废重量scrapWeight。产业部排名用dimension=department,不要先选某个产业部。rank返回行含投入、报废、合格率等辅助字段,不为同一对象再查另一个指标排名或扩大Top数量。按原问题取得足够数据即回答,不为未要求的组织或产品扩展调查。analysisDirectory.periods是全部已发布月份,不等于本轮窗口。相邻月份比较使用query的trend逐月行,窗口合计不是焦点月份值,不另行调用compare。重复查询返回reusedEvidence时直接使用已有结果,不重复工具循环。'}
'guidance': '总体query一次已含全部阶段率、逐月趋势、投入、报废和正式大类。query不传stage,period及comparisonPeriod可省略,由运行时带入;若传必须用对象,不用JSON字符串。需要产业部、业务分组时分别query(view=department/group)一次批量汇总。排名用rank且metric必填:投入重量totalWeight,报废重量scrapWeight。产业部排名用dimension=department,不要先选某个产业部。rank返回行含投入、报废、合格率等辅助字段,不为同一对象再查另一个指标排名或扩大Top数量。按原问题取得足够数据即回答,不为未要求的组织或产品扩展调查。analysisDirectory.periods是全部已发布月份,不等于本轮窗口。双期比较用compare,并明确period和comparisonPeriod;一次多月query已提供各月trend时也可直接对比,不需重复读取。窗口合计不是焦点月份值。重复查询返回reusedEvidence时直接使用已有结果,不重复工具循环。'}
if task.get('agentDriven'):
context['agentIntent'] = copy.deepcopy(request.get('_agentIntent', {}))
context['toolPolicy']['guidance'] += (' 本轮发布版本固定,相同查询复用已返回数据和图片,freshQuery不能产生更新数据。'
'图表src是已经就绪的展示资源,写入正文即可,不需要再次查询来获取或确认图片。'
'queryCompletion描述本次调用的完成状态;数据已覆盖问题时直接回答,只有需要新的范围、期间、指标或分页时再查询。')
context['toolPolicy']['guidance'] += (' 本轮由你自主确定和修正查询。agentIntent仅为未确认的意图提示,不是取数前提。'
'规格缩写先catalog(dimension=product,search=原规格)查询授权候选;只有一个候选时使用完整name,有多个候选时直接向用户提问,保留原目标与期间,不猜选。'
'需要不同期间、筛选条件或指标时可在下一次工具调用中指定;参数错误和未匹配名称是可恢复反馈,按返回说明调整后继续。'
'query可传fields=[rate/scrapRate/totalWeight/scrapWeight]表达需要呈现的指标;所有正式值仍来自服务器。只有forbidden才停止查询。')
if task.get('aggregateSelection'):
context['aggregateSelection'] = copy.deepcopy(task['aggregateSelection'])
context['toolPolicy']['guidance'] += ' 本轮按aggregateSelection查询完整分组,initialObservation.rows已先按汇总重量筛选、再按正式合格率排序并取所需Top N(最多100)。保持返回名次和条件,直接撰写回答;不再用重量rank代替合格率排名,不再扩大读取或把summary当作Top N合计。原始需求见aggregateSelection.requestQuestion,当前消息可能只是补充口径。'
@@ -100,7 +119,40 @@ def run_native_exploration(*, request, task, authority, actor_root, delegated, c
raise QualityToolError('本轮仅整理已有历史快照,不允许重新查询业务数据。')
if not isinstance(arguments, dict) or arguments.get('mode') not in allowed_modes:
raise QualityToolError('本轮可用mode为 '+', '.join(allowed_modes)+';请选择可用模式。排名用rank并传metric=totalWeight或scrapWeight。')
return native_model_view(tools.quality_data(arguments), compact=True, task=task)
arguments = copy.deepcopy(arguments)
page = None
if arguments['mode'] == 'query':
offset = arguments.pop('offset', 0)
if type(offset) is not int or offset < 0:
raise QualityToolError('query.offset必须为非负整数。', code='invalid_arguments')
requested_limit = arguments.get('limit', 100)
if type(requested_limit) is not int or requested_limit < 1:
raise QualityToolError('query.limit必须为正整数。', code='invalid_arguments')
page = {'offset': offset, 'limit': min(100, requested_limit)}
projection_task = {**task, **({'modelPage': page} if page else {})}
observation = native_model_view(tools.quality_data(arguments), compact=True, task=projection_task)
if isinstance(observation, dict) and not observation.get('error') and arguments['mode'] != 'catalog':
reused = observation.get('reusedEvidence') is True and not (page and page['offset'])
observation['queryCompletion'] = {
'state': 'no_data' if observation.get('status') == 'no_data' else 'data_ready',
'reused': reused,
'newEvidence': not reused,
'guidance': ('相同查询已完成,本次复用原数据与图表,没有新增证据。'
if reused else '本次查询已完成,数据可直接用于回答。') +
'若已覆盖用户问题,请现在作答;缺失月份按来源覆盖说明,不为同一条件重查。需要新增范围、期间、指标或分页时仍可继续调用。'}
if observation.get('modelPage', {}).get('hasMore'):
observation['queryCompletion']['guidance'] += ' 本次模型展示有分页;确需其余分组时沿用相同条件并传offset=modelPage.nextOffset。不能用当前页声称全量排名;rank和selectionDisplay的Top结果已在全量范围排序,无需补查。'
try:
from analysis_visual_evidence import protocol_visual_result
documents = _documents(actor_root / 'evidence', prior_evidence_files)
snapshot = delegated.call('/api/analysis/protocol/queries', payload={}, method='POST')
images = image_resources(protocol_visual_result(documents, snapshot.get('scopes', []),
request['question'], request['turnId'], release_id=request['releaseId'], base_context=context))
observation = {**observation, 'presentation': {'images': images,
'guidance': '图片已就绪,无需再查询生成或确认。可用![标题](src)将最贴题的一张图直接放入正文;只引用此列表中的src,不自行编造ID或地址,不重复插入相同图表。没有资源时直接写文字和必要的表格。'}}
except Exception as error:
_atomic_json(actor_root / 'private/chart-resource-diagnostics.json', {'errorType': type(error).__name__})
return observation
started = time.monotonic()
initial = None
@@ -150,7 +202,8 @@ def run_native_exploration(*, request, task, authority, actor_root, delegated, c
status, reason = 'FAILED', reason or 'runtime_error'
business_calls = [call for call in accumulator.tool_calls
if (call.get('input') or {}).get('mode') not in (None, 'catalog')]
if status == 'COMPLETED' and business_calls and not any(call.get('status') == 'completed' for call in business_calls):
if (not task.get('agentDriven') and status == 'COMPLETED' and business_calls
and not any(call.get('status') == 'completed' for call in business_calls)):
status, reason = 'FAILED', 'tool_queries_failed'
# Charts are optional presentation. A chart/provenance failure cannot