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

验证:独立副本通过 162 项 Python、73 项前端及 21 项后端测试,前端构建通过。
线上已用原 8 题复测,并追加两轮双期对比;TOP100 与组合筛选分别核对 100 行和 20 行。
2026-11-12 16:14:20 +08:00

381 lines
24 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Rank formal stage weights; all shares retain the full authorized denominator."""
from decimal import Decimal
import math
FIELDS = {'totalWeight':'投入重量', 'scrapWeight':'报废重量'}
DIMENSIONS = {'product':'产品规格', 'department':'产业部', 'group':'业务分组'}
def ranking_task(task):
return bool(_ranking_shape(task) and not task.get('comparisonPeriod')
and not task.get('comparisonRequired') and not task.get('trendRequired'))
def _ranking_shape(task):
"""Shared structural gate for weight-ranking tasks and compound requests."""
if not (task and task.get('period') and task.get('kind') in ('analysis','metrics','report')
and isinstance(task.get('dimensions'), list) and task['dimensions']
and set(task['dimensions']) <= set(DIMENSIONS)
and isinstance(task.get('fields'), list) and task['fields']
and set(task['fields']) <= (set(FIELDS) | {'rate'})
and task.get('visualType') in ('pareto','bar','column',None)):
return False
# Empty dimensionStages denotes one combined scrap ranking. Never turn it
# into two independent stage Top lists or combine stage input denominators.
from nao_agent_task import dimension_stages
return all((stages := dimension_stages(task, dimension, combined=False))
and (stages == [None] and set(task['fields']) <= {'rate', 'scrapWeight'} and 'scrapWeight' in task['fields']
or all(stage in ('black', 'bright') for stage in stages) and set(task['fields']) <= set(FIELDS))
for dimension in task['dimensions'])
def _single_filter_value(value):
"""Match the server SelectionValues wire contract, including encoded sets."""
if not isinstance(value, str) or not value.strip():
return False
if not value.startswith('~qv1~'):
return len(value) <= 256
import base64
import json
import re
encoded = value[5:]
if len(value) > 16384 or not re.fullmatch(r'[A-Za-z0-9_-]+', encoded):
return False
try:
values = json.loads(base64.b64decode(encoded + '=' * ((-len(encoded)) % 4),
altchars=b'-_', validate=True))
except (ValueError, TypeError, UnicodeError):
return False
return (isinstance(values, list) and 1 <= len(values) <= 50
and all(isinstance(item, str) and item.strip() and len(item) <= 256 for item in values)
and len(set(values)) == 1)
def protocol_scalar_metrics_scope(root_task, context, document):
"""Confirm that a combined ranking result is serving one exact scalar metrics goal.
The protocol scope itself may be an `analysis` task because its operator is
a combined ranking. Presentation can use the root `metrics` intent only
when the single declared node, child frame, evidence proof, and result all
match the same exact scope.
"""
if not isinstance(root_task, dict) or root_task.get('kind') != 'metrics':
return False
dimension = root_task.get('dimensions')
if not isinstance(dimension, list) or len(dimension) != 1 or dimension[0] not in DIMENSIONS:
return False
dimension = dimension[0]
filters = root_task.get('filters')
if not isinstance(filters, dict) or not _single_filter_value(filters.get(dimension)):
return False
if (set(root_task.get('fields') or []) != {'rate', 'scrapWeight'}
or root_task.get('stages') != []
or root_task.get('dimensionStages') != {dimension: []}
or root_task.get('comparisonRequired') is True
or root_task.get('comparisonPeriod') is not None
or root_task.get('trendRequired') is True):
return False
nodes = root_task.get('queryPlan')
if not isinstance(nodes, list) or len(nodes) != 1:
return False
node = nodes[0]
if (not isinstance(node, dict) or node.get('purpose') != 'ranking'
or node.get('dimension') != dimension or node.get('limit') != 1
or set(node.get('fields') or []) != {'rate', 'scrapWeight'}
or node.get('stages') != [] or node.get('comparisonRequired') is True
or node.get('selector') != {'kind': 'base'}):
return False
if not isinstance(context, dict) or not isinstance(document, dict):
return False
result = document.get('result')
if (document.get('mode') != 'rank' or not isinstance(result, dict)
or result.get('kind') != 'quality_weight_ranking'
or result.get('dimension') != dimension or result.get('stage') is not None
or result.get('metric') != 'scrapWeight'):
return False
state = context.get('_protocolScopes')
scopes = state.get('scopes') if isinstance(state, dict) else None
if (not isinstance(scopes, list) or len(scopes) != 1
or state.get('pending') or state.get('gaps')):
return False
scope = scopes[0]
if (not isinstance(scope, dict) or scope.get('purpose') != 'ranking'
or scope.get('nodeId') != node.get('nodeId')):
return False
child = scope.get('task')
if (not isinstance(child, dict) or child.get('kind') not in ('analysis', 'metrics')
or child.get('fields') is None or set(child.get('fields') or []) != {'rate', 'scrapWeight'}
or child.get('dimensions') != [dimension] or child.get('stages') != []
or child.get('dimensionStages') != {dimension: []}
or child.get('comparisonRequired') is True or child.get('comparisonPeriod') is not None
or child.get('trendRequired') is True):
return False
from nao_evidence_report import _filters
from nao_period import normalize_period
try:
if (_filters(child.get('filters')) != _filters(filters)
or normalize_period(child.get('period')) != normalize_period(root_task.get('period'))):
return False
except (TypeError, ValueError):
return False
from nao_query_plan import scope_documents
try:
selected = scope_documents([document], scope, release_id=context.get('releaseId'))
except (KeyError, TypeError, ValueError):
return False
return len(selected) == 1 and selected[0] is document
def ranking_intent(task):
"""A structurally valid ranking request, including incompatible trend/compare asks."""
return _ranking_shape(task)
def source_period(coverage, period, stage):
from nao_period import expected_months
expected = expected_months(period)
months = coverage.get('sourceMonths') if isinstance(coverage,dict) else None
available = sorted({row['month'] for row in months or [] if isinstance(row,dict)
and row.get('year')==period['year'] and row.get('month') in expected
and isinstance(row.get(stage+'Rows'),int) and row[stage+'Rows']>0})
missing = [m for m in expected if m not in available]
internal = [m for m in missing if available and m <= available[-1]]
suffix = (str(available[0])+'月' if len(available)==1 else
str(available[0])+'—'+str(available[-1])+'月' if not internal else
'、'.join(map(str,available))+'月') if available else '无已发布月份'
return {'availableMonths':available,'missingMonths':missing,'internalMissingMonths':internal,
'complete':not missing,'label':str(period['year'])+'年'+suffix+'已发布数据'}
def empty_scrap_ranking(result):
return (result.get('metric') == 'scrapWeight' and result.get('totalWeight') == 0
and result.get('rows') == [] and result.get('totalRows') == 0
and result.get('emptyReason') == 'no_positive_scrap'
and (result.get('rankingCoverage') or {}).get('completeRanking') is True
and bool((result.get('periodCoverage') or {}).get('availableMonths')))
def rank_response(response, release, period, filters, dimension, stage, metric, limit, *, _allow_zero=False, include_metrics=False):
if stage is None and metric == 'scrapWeight':
from nao_combined_ranking import combined_response
return combined_response(response, release, period, filters, dimension, limit)
from nao_quality_tools import _number, QualityToolError
rows = response.get('rows')
if not isinstance(rows,list) or len(rows)>100000:
raise QualityToolError('分组重量未取得完整有界结果,不能作为全量排名。')
if (response.get('context') or {}).get('rowCount',len(rows)) != len(rows):
raise QualityToolError('分组数据已被截断,不能以其生成全量排名。')
denominator = _number((response.get('summary',{}).get(stage) or {}).get(metric),minimum=Decimal(0))
if denominator is None or denominator<0 or denominator==0 and not (_allow_zero or metric == 'scrapWeight'):
raise QualityToolError('所选阶段的正式重量总量不可用,不能计算占比。')
allow_signed_black_total = stage == 'black' and metric == 'totalWeight'
all_rows=[]
seen=set()
unavailable_groups=0
for row in rows:
if not isinstance(row, dict):
raise QualityToolError('分组行结构无效,不能生成全量排名。')
key=str(row.get('key') or '')
if not key or key in seen:raise QualityToolError('分组标识缺失或重复,不能生成全量排名。')
seen.add(key)
raw_value=_number((row.get(stage) or {}).get(metric))
if raw_value is not None and raw_value<0 and not allow_signed_black_total:
raise QualityToolError('除黑片投入重量负向修正外,分组重量不能为负。')
value=raw_value if allow_signed_black_total else _number((row.get(stage) or {}).get(metric),minimum=Decimal(0))
if value is None:
if (row.get('coverage') or {}).get(stage+'Rows')==0:
unavailable_groups+=1
continue
raise QualityToolError('分组重量有缺失或无效值,未将缺失补零。')
all_rows.append({'key':key,'label':str(row.get('label') or key),'weight':value})
grouped=sum((row['weight'] for row in all_rows),Decimal(0))
if abs(grouped-denominator)>max(Decimal('.00001'),abs(denominator)*Decimal('1e-10')):
raise QualityToolError('分组重量合计与同范围正式总量不一致,不能计算全量占比。')
all_rows.sort(key=lambda row:(-row['weight'],row['key']))
positive=[row for row in all_rows if row['weight']>0]
negative=[row for row in all_rows if row['weight']<0]
negative_weight=sum((row['weight'] for row in negative),Decimal(0))
selected=[]; cumulative=Decimal(0)
source_rows = {str(row.get('key')): row for row in rows} if include_metrics else {}
for index,row in enumerate(positive[:limit]):
cumulative+=row['weight']
selected.append({**row,'weight':float(row['weight']),'rank':index+1,
'share':float(row['weight']/denominator),'cumulativeShare':float(cumulative/denominator)})
if include_metrics:
source = source_rows[row['key']].get(stage) or {}
selected[-1]['metrics'] = {field: float(number) if (number := _number(source[field])) is not None else None
for field in ('rate', 'totalWeight', 'scrapWeight') if field in source}
coverage=response.get('coverage') or {}
result = {'kind':'quality_weight_ranking','release':release,'releaseId':release['id'],
'period':period,'filters':filters,'dimension':dimension,'stage':stage,'metric':metric,
'totalWeight':float(denominator),'rows':selected,'totalRows':len(positive),
'returnedRows':len(selected),'truncated':len(selected)<len(positive),
'rankingCoverage':{'sourceGroups':len(rows),'groups':len(all_rows),
'unavailableGroups':unavailable_groups,'positiveGroups':len(positive),
'negativeGroups':len(negative),'negativeWeight':float(negative_weight),
'negativeWeightMagnitude':float(abs(negative_weight)),'completeRanking':True},
'coverage':coverage,'periodCoverage':source_period(coverage,period,stage),
'shareBasis':'all_scope_weight',
'basis':'先读取同范围全部正式分组值,再按所选阶段重量降序选Top;占比和累计占比的分母均为同范围全量正式重量,Top末项不强制为100%。'}
if isinstance(response.get('protocolEvidence'), dict):
result['protocolEvidence'] = response['protocolEvidence']
if metric == 'scrapWeight' and denominator == 0 and result['periodCoverage']['availableMonths']:
result['emptyReason'] = 'no_positive_scrap'
return result
def matching_rankings(evidence,task,context):
from nao_evidence_report import _filters
return [d for d in evidence if d.get('result',{}).get('kind')=='quality_weight_ranking'
and d['result'].get('releaseId')==context.get('releaseId',d['result'].get('releaseId'))
and d['result'].get('period')==task.get('period')
and _filters(d['result'].get('filters'))==_filters(task.get('filters'))]
def ranking_gaps(evidence,task,context):
from nao_agent_task import dimension_stages
docs=matching_rankings(evidence,task,context);gaps=[]
for dimension in task['dimensions']:
for stage in dimension_stages(task, dimension, combined=False):
for metric in task['fields']:
if stage is None and metric == 'rate':
continue # Companion formal rate, not an independently sorted ranking.
candidates=[d['result'] for d in docs if d['result'].get('dimension')==dimension
and d['result'].get('stage')==stage and d['result'].get('metric')==metric]
def valid(result):
proof=result.get('rankingCoverage',{});period=(result.get('periodCoverage') or {}) if stage is None else source_period(result.get('coverage'),result['period'],stage)
rows=result.get('rows');denominator=result.get('totalWeight')
return (proof.get('completeRanking') is True and isinstance(rows,list)
and isinstance(denominator,(int,float)) and not isinstance(denominator,bool)
and math.isfinite(denominator) and (bool(rows) and denominator>0 or empty_scrap_ranking(result))
and period['availableMonths'] and not period['internalMissingMonths']
and (task.get('periodCoverage','available')!='complete' or period['complete']))
if not any(valid(r) for r in candidates):
gaps.append('未取得'+DIMENSIONS[dimension]+'级'+('黑片' if stage=='black' else '亮片' if stage=='bright' else '黑片+亮片')+FIELDS[metric]+'完整排名及同范围全量分母。')
return gaps
def _negative_correction_chart_limit(result):
if (result.get('stage') != 'black' or result.get('metric') != 'totalWeight'
or (result.get('rankingCoverage') or {}).get('negativeGroups', 0) <= 0):
return False
denominator=result.get('totalWeight');rows=result.get('rows')
if (not isinstance(denominator,(int,float)) or isinstance(denominator,bool)
or not math.isfinite(denominator) or not isinstance(rows,list)):
return False
weights=[row.get('weight') for row in rows[:10] if isinstance(row,dict)]
if any(not isinstance(value,(int,float)) or isinstance(value,bool) or not math.isfinite(value) for value in weights):
return False
return sum(weights)>denominator+max(.00001,abs(denominator)*1e-10)
def ranking_chart(document,turn_id,task):
if empty_scrap_ranking(document.get('result', {})):
return None
from nao_agent_task import dimension_stages
from analysis_visuals import _chart,_chart_scope,_scope,_period
r=document.get('result',{})
if r.get('kind')!='quality_weight_ranking' or r.get('dimension') not in DIMENSIONS:return None
if r.get('stage') is None:
from nao_combined_ranking import combined_chart
return combined_chart(document, turn_id, task)
if r.get('stage') not in dimension_stages(task, r['dimension'], combined=False) or r.get('metric') not in task['fields']:return None
check={**task,'dimensions':[r['dimension']],'stages':[r['stage']],'fields':[r['metric']],
'dimensionStages':{r['dimension']:[r['stage']]}}
if ranking_gaps([document],check,{'releaseId':r.get('releaseId')}):return None
scope,_=_scope(r.get('filters'));period,_,_=_period(r.get('period'))
if scope is None or period is None:return None
rows=r['rows'][:100];weights=[row.get('weight') for row in rows];total=r['totalWeight']
if any(not isinstance(v,(int,float)) or isinstance(v,bool) or not math.isfinite(v) or v<0 for v in weights):return None
if sum(weights)>total+max(.00001,total*1e-10) or any(weights[i]>weights[i-1] for i in range(1,len(weights))):return None
metric=r['stage']+('_total_weight' if r['metric']=='totalWeight' else '_scrap_weight')
label=('黑片' if r['stage']=='black' else '亮片')+FIELDS[r['metric']]
source=source_period(r.get('coverage'),r['period'],r['stage'])
count=r['totalRows'];shown_count=len(rows);chart_type=task.get('visualType') or 'bar'
negative_groups=(r.get('rankingCoverage') or {}).get('negativeGroups',0)
negative_weight=(r.get('rankingCoverage') or {}).get('negativeWeight',0)
note='占比与累计占比=所列重量及累计重量÷同范围全部重量;Top末项可以低于100%。未发布月份不补零,不冒充完整全年。'
if r.get('stage')=='black' and r.get('metric')=='totalWeight' and negative_groups:
from nao_evidence_report import shown as format_shown
note+=f'黑片含{negative_groups}项负向投入重量修正,净额{format_shown(negative_weight)} kg仍计入正式净总重分母;图中仅展示正重量Top。'
return _chart(turn_id=turn_id,chart_type=chart_type,
title=DIMENSIONS[r['dimension']]+'级'+label+('柏拉图' if chart_type=='pareto' else '排名'),
subtitle=source['label']+';展示前'+str(shown_count)+'项,占比以同范围全部重量为分母。',
unit='kg',categories=[{'key':row['key'],'label':row['label']} for row in rows],
series=[{'key':metric,'label':label,'values':weights}],evidence_refs=[document['evidenceRef']],
required_fields=[metric],scope=_chart_scope(scope,r['releaseId'],r['period'],view=r['dimension'],stage=r['stage']),
period=period,stage=r['stage'],metric=metric,
coverage={'totalRows':count,'returnedRows':shown_count,'truncated':shown_count<count,
'shareBasis':'all_scope_weight','denominator':total},
section_key=r['dimension'],dimension=r['dimension'],note=note)
def missing_ranking_visuals(task,visuals,evidence=None):
if task.get('visualPreference') == 'none':return []
from nao_agent_task import dimension_stages
missing=[]
for dimension in task['dimensions']:
for stage in dimension_stages(task, dimension, combined=False):
for field in task['fields']:
if stage is None and field == 'rate':continue
if any(empty_scrap_ranking(doc['result']) and doc['result'].get('dimension') == dimension
and doc['result'].get('stage') == stage and doc['result'].get('metric') == field
for doc in matching_rankings(evidence or [], task, {})):
continue
metric='current_scrap_weight' if stage is None else stage+('_total_weight' if field=='totalWeight' else '_scrap_weight')
if not any(v.get('dimension')==dimension and v.get('stage')==stage
and v.get('metric')==metric and v.get('type')==(task.get('visualType') or 'bar') for v in visuals):
label=DIMENSIONS[dimension]+'级'+('黑片' if stage=='black' else '亮片' if stage=='bright' else '黑片+亮片')+FIELDS[field]+'图'
if stage=='black' and field=='totalWeight':
limited=any(_negative_correction_chart_limit(d.get('result',{}))
and d.get('result',{}).get('dimension')==dimension
for d in (evidence or []) if isinstance(d,dict))
if limited:
label+='(负向修正使正重量Top合计超过黑片净总重,当前图表无法表达。)'
missing.append(label)
return missing
def ranking_report(evidence,task,context):
from nao_agent_task import dimension_stages
if task.get('dimensions') and all(dimension_stages(task, dimension, combined=False) == [None] for dimension in task['dimensions']):
from nao_combined_ranking import combined_report
return combined_report(evidence, task, context)
from nao_evidence_report import shown,cell,_scope_label
facts=[];sections=[];seen=set()
for document in matching_rankings(evidence,task,context):
r=document['result'];key=(r['dimension'],r['stage'],r['metric'])
if key in seen or r['dimension'] not in task['dimensions'] or r['stage'] not in dimension_stages(task, r['dimension'], combined=False) or r['metric'] not in task['fields']:continue
seen.add(key);ref=document['evidenceRef'];name=('黑片' if r['stage']=='black' else '亮片')+FIELDS[r['metric']]
p=source_period(r.get('coverage'),r['period'],r['stage']);scope=_scope_label(r['filters']);top=r['rows']
if empty_scrap_ranking(r):
text=p['label']+';'+scope+';'+name+'为0.00 kg,没有正报废重量的'+DIMENSIONS[r['dimension']]+',无需生成排名或占比图。'
facts.append({'id':'F'+str(len(facts)+1),'kind':'overall','label':name,'text':text,'evidenceRef':ref})
sections.append('### '+name+'\n\n'+text+'\n\n['+ref+']')
continue
title=DIMENSIONS[r['dimension']]+'级'+name+' Top '+str(len(top))
table=['| 排名 | '+DIMENSIONS[r['dimension']]+' | '+name+'(kg) | 占全部重量 | 累计占全部重量 |','| --- | --- | --- | --- | --- |']
for row in top:
table.append('| '+' | '.join(cell(str(v)) for v in (row['rank'],row['label'],shown(row['weight']),shown(row['share'],percent=True),shown(row['cumulativeShare'],percent=True)))+' |')
negative_groups=(r.get('rankingCoverage') or {}).get('negativeGroups',0)
negative_weight=(r.get('rankingCoverage') or {}).get('negativeWeight',0)
correction_note=''
if r['stage']=='black' and r['metric']=='totalWeight' and negative_groups:
correction_note=(f'黑片存在{negative_groups}项负向投入重量修正,净额{shown(negative_weight)} kg仍计入正式净总重分母;'
'排名表只展示正重量Top。')
facts.append({'id':'F'+str(len(facts)+1),'kind':'overall','label':title,
'text':p['label']+';'+scope+';'+DIMENSIONS[r['dimension']]+'级'+name+'全量分母为'+shown(r['totalWeight'])+' kg,展示前'+str(len(top))+'项,累计占全部重量'+shown(top[-1]['cumulativeShare'],percent=True)+'。'+correction_note,'evidenceRef':ref})
for row in top:
facts.append({'id':'F'+str(len(facts)+1),'kind':r['dimension'],'label':row['label'],
'text':DIMENSIONS[r['dimension']]+':'+row['label']+';'+name+shown(row['weight'])+' kg,占全部重量'+shown(row['share'],percent=True)+'。','evidenceRef':ref,
'dimension':r['dimension'],'stage':r['stage'],'rank':row['rank']})
sections.append('### '+title+'\n\n'+p['label']+';'+scope+'。分母:全部'+name+shown(r['totalWeight'])+' kg。\n\n'+'\n'.join(table)
+('\n\n'+correction_note if correction_note else '')+'\n\n['+ref+']')
if not facts:raise ValueError('未取得与本轮阶段、期间和分组一致的重量排名')
missing=ranking_gaps(evidence,task,context)
return {'headline':'按已发布数据展示分组重量排名;Top之外的重量保留在占比分母中。',
'sections':sections,'facts':facts,'policy':'占比与累计占比均按同范围所有分组的正式重量计算;Top未覆盖全量时累计占比可低于100%。未发布月份不补零,图表不代表完整全年。',
'focusEnding':'按已发布月份展示。各项占比和累计占比均以同范围全部产品/组织的所选阶段重量为分母,Top之外的产品仍计入分母;不把未发布月份补零,末项累计占比不强制为100%。',
'missingEvidence':missing,'coverageSufficient':not missing,
'rawMetrics':[d['result'] for d in matching_rankings(evidence,task,context)]}