优化:未分类连续维护与柏拉图预汇总,同步已发布修复
This commit is contained in:
+871
-38
@@ -6,6 +6,7 @@ import json
|
||||
import math
|
||||
import re
|
||||
import time
|
||||
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
|
||||
|
||||
|
||||
class HistorySnapshotError(ValueError):
|
||||
@@ -23,6 +24,10 @@ _MAX_SECTIONS = 5000
|
||||
_MAX_ROWS_PER_SECTION = 10000
|
||||
_MAX_FACTS = 10000
|
||||
_MAX_SNAPSHOT_BYTES = 16 * 1024 * 1024
|
||||
# Server metrics summary/group/trend/aggregate rows type their generic
|
||||
# `value` cell with this hidden field; it is not a displayed table column.
|
||||
_HISTORY_ROW_METRICS = frozenset(('rate', 'scrapRate', 'totalWeight', 'scrapWeight'))
|
||||
_HISTORY_ROW_METADATA = frozenset(('metricCode',))
|
||||
|
||||
|
||||
def _nonempty_text(value, *, maximum=500):
|
||||
@@ -93,14 +98,77 @@ def _validate_section(section, available_turn_ids):
|
||||
if len(set(keys)) != len(keys):
|
||||
raise HistorySnapshotError('历史报告分节包含重复列')
|
||||
for row in rows:
|
||||
if (not isinstance(row, dict) or set(row) - set(keys)
|
||||
if (not isinstance(row, dict) or set(row) - set(keys) - _HISTORY_ROW_METADATA
|
||||
or not _json_value(row)):
|
||||
raise HistorySnapshotError('历史报告分节行与列定义不一致')
|
||||
if 'metricCode' in row:
|
||||
metric = row['metricCode']
|
||||
value = row.get('value')
|
||||
if (not isinstance(metric, str) or metric not in _HISTORY_ROW_METRICS
|
||||
or 'value' not in keys or 'value' not in row
|
||||
or value is not None and (type(value) not in (int, float)
|
||||
or isinstance(value, float) and not math.isfinite(value))):
|
||||
raise HistorySnapshotError('历史报告行指标类型或正式值无效')
|
||||
note = section.get('note')
|
||||
if note is not None and not isinstance(note, str):
|
||||
raise HistorySnapshotError('历史报告分节说明无效')
|
||||
|
||||
|
||||
def _validate_source_task_context(source):
|
||||
"""Validate the small server projection used after sourceResults are stripped."""
|
||||
task = source.get('taskContext')
|
||||
if task is None:
|
||||
return
|
||||
allowed = {'period', 'filters', 'dimensions', 'fields', 'stages', 'comparisonPeriod',
|
||||
'goal', 'rootGoal', 'question', 'sourceFieldLabels', 'queryPlan'}
|
||||
if not isinstance(task, dict) or set(task) - allowed or not _json_value(task):
|
||||
raise HistorySnapshotError('历史来源任务摘要结构无效')
|
||||
for key in ('goal', 'rootGoal', 'question'):
|
||||
if key in task and (not isinstance(task[key], str) or len(task[key]) > 4000):
|
||||
raise HistorySnapshotError('历史来源任务说明无效')
|
||||
for key in ('period', 'comparisonPeriod'):
|
||||
period = task.get(key)
|
||||
if period is not None:
|
||||
try:
|
||||
from nao_period import normalize_period
|
||||
if normalize_period(period) is None:
|
||||
raise ValueError
|
||||
except (TypeError, ValueError):
|
||||
raise HistorySnapshotError('历史来源任务期间无效') from None
|
||||
if 'filters' in task and not isinstance(task['filters'], dict):
|
||||
raise HistorySnapshotError('历史来源任务筛选结构无效')
|
||||
for key in ('dimensions', 'fields', 'stages'):
|
||||
value = task.get(key)
|
||||
if value is not None and (not isinstance(value, list) or len(value) > 100
|
||||
or any(not isinstance(item, str) or len(item) > 256 for item in value)):
|
||||
raise HistorySnapshotError('历史来源任务列表无效')
|
||||
labels = task.get('sourceFieldLabels')
|
||||
if labels is not None and (not isinstance(labels, dict) or len(labels) > 500
|
||||
or any(not isinstance(key, str) or not isinstance(value, str)
|
||||
or len(key) > 256 or len(value) > 200
|
||||
for key, value in labels.items())):
|
||||
raise HistorySnapshotError('历史来源字段标签无效')
|
||||
nodes = task.get('queryPlan')
|
||||
if nodes is not None:
|
||||
if not isinstance(nodes, list) or len(nodes) > 100:
|
||||
raise HistorySnapshotError('历史来源排名计划无效')
|
||||
node_keys = {'purpose', 'dimension', 'fields', 'stages', 'limit'}
|
||||
for node in nodes:
|
||||
if not isinstance(node, dict) or set(node) - node_keys:
|
||||
raise HistorySnapshotError('历史来源排名计划节点无效')
|
||||
if 'purpose' in node and not isinstance(node['purpose'], str):
|
||||
raise HistorySnapshotError('历史来源排名计划用途无效')
|
||||
if 'dimension' in node and not isinstance(node['dimension'], str):
|
||||
raise HistorySnapshotError('历史来源排名计划维度无效')
|
||||
for key in ('fields', 'stages'):
|
||||
value = node.get(key)
|
||||
if value is not None and (not isinstance(value, list) or len(value) > 100
|
||||
or any(not isinstance(item, str) or len(item) > 256 for item in value)):
|
||||
raise HistorySnapshotError('历史来源排名计划字段无效')
|
||||
if 'limit' in node and (type(node['limit']) is not int or node['limit'] < 1):
|
||||
raise HistorySnapshotError('历史来源排名计划条数无效')
|
||||
|
||||
|
||||
def validate_history_snapshot(snapshot, *, task, authority, release_id, request_turn_id):
|
||||
"""Validate the exact server-issued snapshot before using its facts."""
|
||||
if not isinstance(authority, dict) or not isinstance(task, dict):
|
||||
@@ -171,6 +239,7 @@ def validate_history_snapshot(snapshot, *, task, authority, release_id, request_
|
||||
raise HistorySnapshotError('可用历史来源没有已完成查询依据')
|
||||
if source['available']:
|
||||
available_ids.add(expected_id)
|
||||
_validate_source_task_context(source)
|
||||
|
||||
sections = snapshot.get('sections')
|
||||
if not isinstance(sections, list) or len(sections) > _MAX_SECTIONS:
|
||||
@@ -178,6 +247,68 @@ def validate_history_snapshot(snapshot, *, task, authority, release_id, request_
|
||||
for section in sections:
|
||||
_validate_section(section, available_ids)
|
||||
|
||||
visual_schema = snapshot.get('visualSchemaVersion', 1)
|
||||
visual_state = snapshot.get('visualState', 'not_requested')
|
||||
visuals = snapshot.get('visuals', [])
|
||||
lineages = snapshot.get('historyVisualSources', [])
|
||||
if (type(visual_schema) is not int or visual_schema != 1
|
||||
or visual_state not in ('ready', 'not_requested', 'unavailable', 'failed')
|
||||
or not isinstance(visuals, list) or len(visuals) > 12
|
||||
or not isinstance(lineages, list) or len(lineages) > 12):
|
||||
raise HistorySnapshotError('历史报告图表快照结构无效')
|
||||
if visual_state == 'ready':
|
||||
if not visuals:
|
||||
raise HistorySnapshotError('历史报告标记为可用但没有图表')
|
||||
from nao_period import normalize_period
|
||||
if len(lineages) != len(visuals):
|
||||
raise HistorySnapshotError('历史报告图表缺少来源绑定信息')
|
||||
lineage_by_id = {}
|
||||
for lineage in lineages:
|
||||
lineage_keys = {'visualId', 'sourceTurnId', 'scope', 'period', 'comparisonPeriod',
|
||||
'dimension', 'stage', 'metric', 'evidenceRefs'}
|
||||
if (not isinstance(lineage, dict) or not _nonempty_text(lineage.get('visualId'), maximum=120)
|
||||
or not lineage_keys.issubset(lineage) or set(lineage) - lineage_keys
|
||||
or lineage.get('sourceTurnId') not in available_ids or not _json_value(lineage)
|
||||
or lineage['visualId'] in lineage_by_id):
|
||||
raise HistorySnapshotError('历史报告图表来源绑定无效')
|
||||
lineage_by_id[lineage['visualId']] = lineage
|
||||
visual_ids = set()
|
||||
for chart in visuals:
|
||||
scope = chart.get('scope') if isinstance(chart, dict) else None
|
||||
if (not isinstance(chart, dict) or not _nonempty_text(chart.get('id'), maximum=120)
|
||||
or chart.get('originTurnId') not in available_ids
|
||||
or not isinstance(scope, dict) or scope.get('releaseId') != release_id
|
||||
or not _json_value(chart)):
|
||||
raise HistorySnapshotError('历史图表没有绑定可用来源及原始范围')
|
||||
chart_period = chart.get('period')
|
||||
try:
|
||||
if not isinstance(chart_period, dict) or normalize_period(chart_period) is None:
|
||||
raise ValueError
|
||||
comparison_period = chart.get('comparisonPeriod')
|
||||
if comparison_period is not None and normalize_period(comparison_period) is None:
|
||||
raise ValueError
|
||||
except (TypeError, ValueError):
|
||||
raise HistorySnapshotError('历史图表期间无效') from None
|
||||
evidence_refs = chart.get('evidenceRefs')
|
||||
if (not isinstance(evidence_refs, list) or not evidence_refs
|
||||
or any(not isinstance(ref, str) or not re.fullmatch(r'Q[A-F0-9]{8}', ref)
|
||||
for ref in evidence_refs)
|
||||
or len(set(evidence_refs)) != len(evidence_refs)
|
||||
or chart['id'] in visual_ids):
|
||||
raise HistorySnapshotError('历史图表没有绑定有效的原查询证据')
|
||||
lineage = lineage_by_id.get(chart['id'])
|
||||
if (lineage is None or lineage.get('sourceTurnId') != chart.get('originTurnId')
|
||||
or lineage.get('scope') != scope or lineage.get('period') != chart_period
|
||||
or lineage.get('evidenceRefs') != evidence_refs
|
||||
or any(lineage.get(key) != chart.get(key) for key in
|
||||
('comparisonPeriod', 'dimension', 'stage', 'metric'))):
|
||||
raise HistorySnapshotError('历史图表来源范围与保存图表不一致')
|
||||
visual_ids.add(chart['id'])
|
||||
elif visuals:
|
||||
raise HistorySnapshotError('历史报告图表状态与保存图表不一致')
|
||||
elif snapshot.get('historyVisualSources', []):
|
||||
raise HistorySnapshotError('历史报告没有图表但保留了图表来源绑定')
|
||||
|
||||
facts = snapshot.get('facts')
|
||||
if not isinstance(facts, list) or len(facts) > _MAX_FACTS:
|
||||
raise HistorySnapshotError('历史报告事实数量无效')
|
||||
@@ -344,6 +475,659 @@ def _bounded_history_answer_facts(snapshot):
|
||||
return _bounded_model_facts(distributed, [source['turnId'] for source in snapshot['historySources']])
|
||||
|
||||
|
||||
def _history_section_signature(section):
|
||||
"""Identify a repeated saved table without collapsing different scopes or rankings."""
|
||||
keys = ('title', 'period', 'comparisonPeriod', 'filters', 'stage', 'dimension', 'metric',
|
||||
'columns', 'rows')
|
||||
return json.dumps({key: section.get(key) for key in keys if key in section},
|
||||
ensure_ascii=False, sort_keys=True, separators=(',', ':'))
|
||||
|
||||
|
||||
def _unique_history_sections(snapshot):
|
||||
sections, seen = [], set()
|
||||
for section in snapshot.get('sections', []):
|
||||
signature = _history_section_signature(section)
|
||||
if signature in seen:
|
||||
continue
|
||||
seen.add(signature)
|
||||
sections.append(section)
|
||||
return sections
|
||||
|
||||
|
||||
def _deduplicated_history_facts(snapshot):
|
||||
"""Remove exact same-scope fact copies from repeated saved tables for model input only."""
|
||||
sections = snapshot.get('sections', [])
|
||||
seen, facts = set(), []
|
||||
for fact in snapshot.get('facts', []):
|
||||
scope = _snapshot_fact_scope(snapshot, fact)
|
||||
section_index = fact.get('sectionIndex')
|
||||
section_signature = (_history_section_signature(sections[section_index])
|
||||
if type(section_index) is int and 0 <= section_index < len(sections) else None)
|
||||
identifiers = sorted({(item.get('field'), item.get('value'))
|
||||
for item in _snapshot_fact_identifiers(snapshot, fact)
|
||||
if isinstance(item, dict) and isinstance(item.get('value'), str)})
|
||||
scope.pop('sourceTurnId', None)
|
||||
scope.pop('sectionIndex', None)
|
||||
signature = json.dumps({'text': fact.get('displayText') or fact.get('text'),
|
||||
'values': fact.get('values'), 'factKind': fact.get('factKind'),
|
||||
'metricCode': fact.get('metricCode'), 'scope': scope,
|
||||
'section': section_signature, 'identifiers': identifiers},
|
||||
ensure_ascii=False, sort_keys=True, separators=(',', ':'))
|
||||
if signature in seen:
|
||||
continue
|
||||
seen.add(signature)
|
||||
facts.append(fact)
|
||||
return facts
|
||||
|
||||
|
||||
def _title_scope_components(label):
|
||||
return [part.strip() for part in re.split(r'[--—–//·]+',label)
|
||||
if len(part.strip())>=2 and part.strip()!=label and not part.strip().isdigit()]
|
||||
|
||||
|
||||
def _title_scope_label_pattern(label, organization=False):
|
||||
chars=re.sub(r'[\s\--—–//·]','',label) if organization else label
|
||||
pattern=r'[\s\--—–//·]*'.join(map(re.escape,chars)) if organization else re.escape(chars)
|
||||
if chars and re.match(r'[A-Za-z0-9_]',chars[0]):pattern=r'(?<![A-Za-z0-9_])'+pattern
|
||||
if chars and re.match(r'[A-Za-z0-9_]',chars[-1]):pattern+=r'(?![A-Za-z0-9_])'
|
||||
return pattern
|
||||
|
||||
|
||||
def _title_scope_frames(sections, source_goals=()):
|
||||
"""Business labels only; no source IDs, raw values or transport filters."""
|
||||
frames=[];origins=[]
|
||||
for section in sections:
|
||||
filters=section.get('filters') or {};positive,negative=_frame_scope_literals(section)
|
||||
organizations={filters[key] for key in ('department','group','projectGroup')
|
||||
if isinstance(filters.get(key),str) and filters[key]}
|
||||
required=set(positive)
|
||||
if filters.get('group') and filters.get('department'):required.discard(filters['department'])
|
||||
frame={'period':section.get('period'),'labels':sorted(positive),'requiredLabels':sorted(required),
|
||||
'organizationLabels':sorted(organizations),'negativeLabels':sorted(negative),'unfiltered':not filters}
|
||||
if frame not in frames:frames.append(frame);origins.append([])
|
||||
origins[frames.index(frame)].append(section)
|
||||
from nao_evidence_report import _filters
|
||||
candidates={}
|
||||
for index,frame in enumerate(frames):
|
||||
for section in origins[index]:
|
||||
for goal in source_goals:
|
||||
if (not isinstance(goal,dict) or not section.get('sourceTurnId')
|
||||
or goal.get('_sourceTurnId')!=section['sourceTurnId']
|
||||
or goal.get('period')!=section.get('period')
|
||||
or _filters(goal.get('filters'))!=_filters(section.get('filters'))):continue
|
||||
prose='\n'.join(goal.get(key,'') for key in ('question','goal','rootGoal') if isinstance(goal.get(key,''),str))
|
||||
# A component inside the canonical full name is not evidence
|
||||
# that the original source task used it as a short name.
|
||||
for label in sorted(frame['organizationLabels'],key=len,reverse=True):
|
||||
prose=re.sub(_title_scope_label_pattern(label,True),' ',prose)
|
||||
for label in frame['organizationLabels']:
|
||||
for component in _title_scope_components(label):
|
||||
pattern=r'(?<![A-Za-z0-9_])'+re.escape(component)+r'(?![A-Za-z0-9_])'
|
||||
if re.search(pattern,prose):
|
||||
key=(json.dumps(frame['period'],sort_keys=True),component)
|
||||
candidates.setdefault(key,set()).add((index,label))
|
||||
for (_,alias),owners in candidates.items():
|
||||
if len(owners)==1:
|
||||
index,label=next(iter(owners));frames[index].setdefault('aliases',{})[alias]=label
|
||||
return frames
|
||||
|
||||
|
||||
def _history_title_source_goals(snapshot):
|
||||
"""Only the validated Java worker projection, never sourceResults."""
|
||||
return [{**source['taskContext'],'_sourceTurnId':source['turnId']}
|
||||
for source in snapshot.get('historySources',[]) if isinstance(source.get('taskContext'),dict)
|
||||
and source.get('available') is True and source.get('status') in ('COMPLETED','PARTIAL')]
|
||||
|
||||
|
||||
def _title_scope_match(title, frames, excluded=()):
|
||||
"""Bind named scopes to their own calendar clauses, never to equal numbers."""
|
||||
labels={label for frame in frames for label in frame['labels']+frame['negativeLabels']} | set(excluded)
|
||||
organizations={label for frame in frames for label in frame['organizationLabels']}
|
||||
mentions=[]
|
||||
for label in labels:
|
||||
# Only complete organization labels permit presentation separators.
|
||||
# Product/customer identifiers retain their literal character contract.
|
||||
pattern=_title_scope_label_pattern(label,label in organizations)
|
||||
mentions.extend({'start':m.start(),'end':m.end(),'label':label} for m in re.finditer(pattern,title))
|
||||
canonical=list(mentions)
|
||||
component_spans=set()
|
||||
for index,frame in enumerate(frames):
|
||||
for label in frame['organizationLabels']:
|
||||
for component in _title_scope_components(label):
|
||||
pattern=r'(?<![A-Za-z0-9_])'+re.escape(component)+r'(?![A-Za-z0-9_])'
|
||||
component_spans.update(m.span() for m in re.finditer(pattern,title)
|
||||
if not any(n['start']<=m.start() and m.end()<=n['end'] for n in canonical))
|
||||
for alias,label in frame.get('aliases',{}).items():
|
||||
pattern=r'(?<![A-Za-z0-9_])'+re.escape(alias)+r'(?![A-Za-z0-9_])'
|
||||
mentions.extend({'start':m.start(),'end':m.end(),'label':label,'frameIndex':index}
|
||||
for m in re.finditer(pattern,title) if not any(n['start']<=m.start() and m.end()<=n['end'] for n in canonical))
|
||||
if component_spans-{(m['start'],m['end']) for m in mentions if 'frameIndex' in m}:return False,[]
|
||||
forbidden={label for frame in frames for label in frame['negativeLabels']}
|
||||
if any(m['label'] in forbidden for m in mentions):return False,[]
|
||||
positives=[set(frame['labels']) for frame in frames]
|
||||
partial=(set.union(*positives)-set.intersection(*positives)) if positives else set()
|
||||
partial.update(excluded)
|
||||
aliased=any('frameIndex' in m for m in mentions)
|
||||
if not aliased and not any(m['label'] in partial for m in mentions):return True,mentions
|
||||
dates=list(re.finditer(r'(?:(?P<year>(?:19|20)[0-9]{2})年)?(?P<month>[0-9]{1,2})月',title))
|
||||
years={frame['period'].get('year') for frame in frames if isinstance(frame.get('period'),dict)}
|
||||
year=next(iter(years)) if len(years)==1 else None;covered=set();assigned=[]
|
||||
if not dates and len(frames)==1 and aliased and set(frames[0]['requiredLabels'])<={m['label'] for m in mentions}:return True,mentions
|
||||
for i,date in enumerate(dates):
|
||||
if date['year']:year=int(date['year'])
|
||||
end=dates[i+1].start() if i+1<len(dates) else len(title)
|
||||
period={'year':year,'month':int(date['month'])}
|
||||
local=[m for m in mentions if date.end()<=m['start'] and m['end']<=end
|
||||
and ('frameIndex' not in m or frames[m['frameIndex']]['period']==period)]
|
||||
local.extend({'start':date.end()+m.start(),'end':date.end()+m.end(),'label':None}
|
||||
for m in re.finditer(r'全公司|全厂|总体',title[date.end():end]))
|
||||
clusters=[]
|
||||
for mention in sorted(local,key=lambda m:(m['start'],m['end'])):
|
||||
if not clusters or re.search(r'与|及|和|、',title[clusters[-1][-1]['end']:mention['start']]):clusters.append([])
|
||||
clusters[-1].append(mention)
|
||||
for cluster in clusters:
|
||||
named={m['label'] for m in cluster if m['label'] is not None};global_scope=any(m['label'] is None for m in cluster)
|
||||
matches=[j for j,frame in enumerate(frames) if frame.get('period')=={'year':year,'month':int(date['month'])}
|
||||
and all(m.get('frameIndex',j)==j for m in cluster)
|
||||
and (frame['unfiltered'] and not named if global_scope else named<=set(frame['labels'])
|
||||
and set(frame['requiredLabels'])<=named)]
|
||||
if len(matches)!=1:return False,[]
|
||||
covered.add(matches[0]);assigned.extend(cluster)
|
||||
assigned_spans={(m['start'],m['end']) for m in assigned}
|
||||
if any((m['start'],m['end']) not in assigned_spans for m in mentions) or covered!=set(range(len(frames))):return False,[]
|
||||
return True,mentions
|
||||
|
||||
|
||||
def _history_title_is_scope_safe(title, snapshot, question):
|
||||
if (not isinstance(title, str) or not title.strip() or len(title.strip()) > 40
|
||||
or any(ord(char) < 32 for char in title)
|
||||
or any(char in title for char in '<>\r\n')
|
||||
or '?' in title or '?' in title
|
||||
or title.strip() == str(question or '').strip()
|
||||
or re.search(r'(?:Q[A-F0-9]{8}|history-fact:|[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12})', title, re.I)):
|
||||
return None
|
||||
normalized = title.strip()
|
||||
frames=_title_scope_frames(snapshot.get('sections',[]),_history_title_source_goals(snapshot))
|
||||
if any(not any(section.get('sourceTurnId')==source['turnId'] for section in snapshot.get('sections',[]))
|
||||
for source in snapshot.get('historySources',[])):
|
||||
frames.append({'period':None,'labels':[],'requiredLabels':[],'organizationLabels':[],'negativeLabels':[],'unfiltered':True})
|
||||
if not _title_scope_match(normalized,frames,_noncommon_scope_literals(snapshot))[0]:
|
||||
return None
|
||||
return normalized
|
||||
|
||||
|
||||
def _native_history_title_info(native_final, snapshot, question):
|
||||
"""Reuse a native title only when its source and scope have both been checked."""
|
||||
if not isinstance(native_final, dict):
|
||||
return None
|
||||
title = _history_title_is_scope_safe(native_final.get('reportTitle'), snapshot, question)
|
||||
if (title and native_final.get('titleValid') is True
|
||||
and native_final.get('titleGeneratedByModel') is True
|
||||
and native_final.get('titleSource') == 'model_verified_content'):
|
||||
return title, True
|
||||
if title and native_final.get('titleSource') == 'safe_partial_fallback':
|
||||
return title, False
|
||||
return None
|
||||
|
||||
|
||||
def _history_fallback_title(snapshot):
|
||||
from nao_evidence_report import period as shown_period
|
||||
sections = _unique_history_sections(snapshot)
|
||||
periods = []
|
||||
for section in sections:
|
||||
period_value = section.get('period')
|
||||
if isinstance(period_value, dict) and period_value != {'allPublished': True}:
|
||||
try:
|
||||
label = shown_period(period_value)
|
||||
except (KeyError, TypeError, ValueError):
|
||||
continue
|
||||
if label not in periods:
|
||||
periods.append(label)
|
||||
period_labels = sorted(periods, key=lambda value: tuple(map(int, re.findall(r'\d+', value))))
|
||||
if len(period_labels) == 1:
|
||||
prefix = period_labels[0]
|
||||
elif (len(period_labels) == 2 and
|
||||
(first := re.fullmatch(r'(\d{4})年(\d{1,2})月', period_labels[0])) and
|
||||
(last := re.fullmatch(r'(\d{4})年(\d{1,2})月', period_labels[1])) and
|
||||
first[1] == last[1] and int(last[2]) == int(first[2]) + 1):
|
||||
prefix = f'{first[1]}年{first[2]}月至{last[2]}月'
|
||||
elif period_labels:
|
||||
prefix = '、'.join(period_labels[:3])
|
||||
else:
|
||||
prefix = '历史'
|
||||
scopes = {json.dumps({key: section.get(key) for key in
|
||||
('period', 'comparisonPeriod', 'filters', 'stage', 'dimension', 'metric')
|
||||
if key in section}, ensure_ascii=False, sort_keys=True)
|
||||
for section in sections}
|
||||
suffix = '多范围质量报告' if len(scopes) > 1 else '历史质量报告'
|
||||
title = prefix + suffix
|
||||
return title[:40]
|
||||
|
||||
|
||||
_HISTORY_WEIGHT_KEYS = {
|
||||
'weight', 'totalWeight', 'scrapWeight', 'blackScrapWeight', 'brightScrapWeight',
|
||||
'blackTotalWeight', 'brightTotalWeight', 'blackWeight', 'brightWeight',
|
||||
}
|
||||
_HISTORY_RATE_KEYS = {'rate', 'compositeRate', 'scrapRate', 'share', 'cumulativeShare'}
|
||||
_HISTORY_HIDDEN_COLUMNS = {'id', 'key', 'queryId', 'evidenceRef', 'factId', 'sourceTurnId', 'metricCode'}
|
||||
_HISTORY_COLUMN_LABELS = {
|
||||
'rank': '排名', 'label': '规格', 'weight': '综合报废重量',
|
||||
'totalWeight': '投入重量', 'scrapWeight': '报废重量', 'compositeRate': '综合合格率',
|
||||
'rate': '合格率', 'scrapRate': '报废率', 'blackScrapWeight': '黑片报废重量',
|
||||
'brightScrapWeight': '亮片报废重量', 'blackTotalWeight': '黑片投入重量',
|
||||
'brightTotalWeight': '亮片投入重量', 'share': '占同范围全部报废',
|
||||
'cumulativeShare': '累计占比',
|
||||
}
|
||||
|
||||
|
||||
def _history_decimal(value):
|
||||
if type(value) not in (int, float, str) or isinstance(value, bool):
|
||||
return None
|
||||
try:
|
||||
number = Decimal(str(value).strip())
|
||||
except (InvalidOperation, ValueError):
|
||||
return None
|
||||
return number if number.is_finite() else None
|
||||
|
||||
|
||||
def _history_unit(snapshot, section, *, stage=None):
|
||||
"""Read physical weight units from the same saved source and business scope."""
|
||||
physical = {'kg':'kg', '公斤':'kg', '千克':'kg', 'g':'g', '克':'g',
|
||||
'mg':'mg', '毫克':'mg', '吨':'吨', 't':'吨', 'ton':'吨', 'tonne':'吨'}
|
||||
weight_metrics = {'scrap_delta', 'current_scrap_weight', 'source_defect_weight',
|
||||
'source_defect_delta_weight', 'black_total_weight', 'bright_total_weight',
|
||||
'black_scrap_weight', 'bright_scrap_weight', *_HISTORY_WEIGHT_KEYS}
|
||||
def business_scope(value):
|
||||
return {key: value[key] for key in _SCOPE_FILTERS if key in value
|
||||
and not (key == 'includeUnassigned' and value[key] is False)}
|
||||
expected_scope = business_scope(section.get('filters') or {})
|
||||
expected_stage = stage or section.get('stage')
|
||||
units = set()
|
||||
for chart in snapshot.get('visuals', []):
|
||||
if not isinstance(chart, dict) or not isinstance(chart.get('unit'), str):
|
||||
continue
|
||||
if (chart.get('period') != section.get('period')
|
||||
or chart.get('originTurnId') != section.get('sourceTurnId')
|
||||
or chart.get('metric') not in weight_metrics):
|
||||
continue
|
||||
scope = chart.get('scope') if isinstance(chart.get('scope'), dict) else {}
|
||||
chart_stage = chart.get('stage') or scope.get('stage')
|
||||
if (business_scope(scope) != expected_scope
|
||||
or chart_stage is not None and chart_stage != expected_stage):
|
||||
continue
|
||||
unit = physical.get(chart['unit'].strip().lower())
|
||||
if unit: units.add(unit)
|
||||
for column in section.get('columns', []):
|
||||
label = str(column.get('label') or '')
|
||||
if '重量' not in label:
|
||||
continue
|
||||
match = re.search(r'[((]([^()()]+)[))]', label)
|
||||
unit = physical.get(match[1].strip().lower()) if match else None
|
||||
if unit: units.add(unit)
|
||||
return next(iter(units)) if len(units) == 1 else None
|
||||
|
||||
|
||||
def _history_cell(value, key, label, unit, *, metric_code=None):
|
||||
if metric_code is not None and (not isinstance(metric_code, str) or metric_code not in _HISTORY_ROW_METRICS):
|
||||
raise HistorySnapshotError('历史报告行指标类型无效')
|
||||
if value is None:
|
||||
return '—'
|
||||
if isinstance(value, bool):
|
||||
return '是' if value else '否'
|
||||
number = _history_decimal(value)
|
||||
normalized_key = metric_code if key == 'value' and metric_code is not None else str(key)
|
||||
normalized_label = str(label or '')
|
||||
is_rate = (normalized_key in _HISTORY_RATE_KEYS or 'rate' in normalized_key.lower()
|
||||
or any(word in normalized_label for word in ('合格率', '报废率', '占比', '比例')))
|
||||
is_weight = (normalized_key in _HISTORY_WEIGHT_KEYS or 'weight' in normalized_key.lower()
|
||||
or '重量' in normalized_label)
|
||||
if number is not None:
|
||||
if normalized_key == 'rank' and number == number.to_integral_value():
|
||||
return str(int(number))
|
||||
if is_rate:
|
||||
percent = (number * Decimal(100)).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
|
||||
return f'{percent:,.2f}%'
|
||||
shown = format(number.normalize(), ',f')
|
||||
if is_weight and unit:
|
||||
return f'{shown} {unit}'
|
||||
if is_weight and metric_code in ('totalWeight', 'scrapWeight'):
|
||||
return f'{shown}(原单位)'
|
||||
return shown
|
||||
if isinstance(value, (dict, list)):
|
||||
return '—'
|
||||
return str(value).replace('|', r'\|').replace('\n', ' ').strip()
|
||||
|
||||
|
||||
def _history_source_field_labels(snapshot, source_turn_id=None):
|
||||
labels = {}
|
||||
direct = snapshot.get('sourceFieldLabels') if isinstance(snapshot.get('sourceFieldLabels'), dict) else {}
|
||||
labels.update(direct)
|
||||
for source in snapshot.get('historySources', []):
|
||||
if (not isinstance(source, dict) or source_turn_id and source.get('turnId') != source_turn_id
|
||||
or source.get('available') is not True
|
||||
or source.get('status') not in ('COMPLETED', 'PARTIAL')):
|
||||
continue
|
||||
task = source.get('taskContext') if isinstance(source.get('taskContext'), dict) else {}
|
||||
saved = task.get('sourceFieldLabels') if isinstance(task.get('sourceFieldLabels'), dict) else {}
|
||||
labels.update(saved)
|
||||
return {key: value.strip() for key, value in labels.items()
|
||||
if isinstance(key, str) and isinstance(value, str) and value.strip()}
|
||||
|
||||
|
||||
def _history_scope_caption(snapshot, section):
|
||||
from nao_evidence_report import period as shown_period
|
||||
parts = []
|
||||
period_value = section.get('period')
|
||||
if isinstance(period_value, dict):
|
||||
try:
|
||||
parts.append(shown_period(period_value))
|
||||
except (KeyError, TypeError, ValueError):
|
||||
pass
|
||||
filters = section.get('filters') if isinstance(section.get('filters'), dict) else {}
|
||||
dimensions = (('department', '产业部'), ('group', '业务分组'),
|
||||
('product', '规格'), ('projectGroup', '项目组'))
|
||||
for key, label in dimensions:
|
||||
values = _filter_literals(filters.get(key))
|
||||
if values:
|
||||
parts.append(label + ' ' + '、'.join(sorted(values)))
|
||||
field_labels = _history_source_field_labels(snapshot, section.get('sourceTurnId'))
|
||||
column_labels = filters.get('columnLabels') if isinstance(filters.get('columnLabels'), dict) else {}
|
||||
field_labels.update({key: value.strip() for key, value in column_labels.items()
|
||||
if isinstance(key, str) and isinstance(value, str) and value.strip()})
|
||||
extra = filters.get('extraFilters') if isinstance(filters.get('extraFilters'), dict) else {}
|
||||
for key, value in sorted(extra.items()):
|
||||
values = _filter_literals(value)
|
||||
if values:
|
||||
label = field_labels.get(key) or {'customerCode': '客户代码'}.get(key) or '附加条件'
|
||||
parts.append(str(label) + ' ' + '、'.join(sorted(values)))
|
||||
exclusions = filters.get('exclusions') if isinstance(filters.get('exclusions'), dict) else {}
|
||||
dimension_names = dict(dimensions)
|
||||
for key, value in sorted(exclusions.items()):
|
||||
values = _filter_literals(value)
|
||||
if values:
|
||||
parts.append('排除' + dimension_names.get(key, '对象') + ' ' + '、'.join(sorted(values)))
|
||||
if filters.get('includeUnassigned') is True:
|
||||
parts.append('包含未分配项')
|
||||
column_filters = filters.get('columnFilters')
|
||||
if isinstance(column_filters, list):
|
||||
operator_labels = {'in': '包含', 'not_in': '不含', 'is_empty': '为空', 'not_empty': '非空'}
|
||||
for rule in column_filters:
|
||||
if not isinstance(rule, dict):
|
||||
continue
|
||||
key = rule.get('field')
|
||||
label = field_labels.get(key) if isinstance(key, str) else None
|
||||
if not label:
|
||||
label = '授权列'
|
||||
stages = [_stage_label(stage) for stage in rule.get('stages', []) if stage in ('black', 'bright')]
|
||||
prefix = '、'.join(stages)
|
||||
operator = operator_labels.get(rule.get('operator'), '筛选')
|
||||
values = _filter_literals(rule.get('values'))
|
||||
suffix = '、'.join(sorted(values))
|
||||
parts.append(' '.join(part for part in (prefix, str(label), operator, suffix) if part))
|
||||
return ' / '.join(parts) if parts else '全范围(无额外筛选)'
|
||||
|
||||
|
||||
def _history_model_fact_projection(snapshot, facts):
|
||||
"""Add a human-facing model view while retaining signed text and scope intact."""
|
||||
result = []
|
||||
dimension_labels = {'department': '产业部', 'group': '业务分组', 'product': '规格',
|
||||
'defect': '缺陷', 'projectGroup': '项目组'}
|
||||
stage_labels = {'overall': '综合', 'black': '黑片', 'bright': '亮片'}
|
||||
sections = snapshot.get('sections', []) if isinstance(snapshot.get('sections'), list) else []
|
||||
for fact in facts:
|
||||
if not isinstance(fact, dict):
|
||||
continue
|
||||
section_index = fact.get('sectionIndex')
|
||||
section = (sections[section_index] if type(section_index) is int
|
||||
and 0 <= section_index < len(sections)
|
||||
and sections[section_index].get('sourceTurnId') == fact.get('sourceTurnId') else None)
|
||||
scope = fact.get('scope') if isinstance(fact.get('scope'), dict) else {}
|
||||
if section is None:
|
||||
frames = scope.get('frames') if isinstance(scope.get('frames'), list) else []
|
||||
frames = frames or [scope]
|
||||
captions = []
|
||||
for frame in frames:
|
||||
if not isinstance(frame, dict):
|
||||
continue
|
||||
captions.append(_history_scope_caption(snapshot, {
|
||||
'sourceTurnId': fact.get('sourceTurnId'),
|
||||
'period': frame.get('period'), 'filters': frame.get('filters', {}),
|
||||
'stage': frame.get('stage'),
|
||||
}))
|
||||
display_scope = ';'.join(dict.fromkeys(captions)) if captions else '历史已确认范围'
|
||||
stage = scope.get('stage')
|
||||
dimension = scope.get('dimension')
|
||||
else:
|
||||
display_scope = _history_scope_caption(snapshot, section)
|
||||
stage = scope.get('stage', section.get('stage'))
|
||||
dimension = scope.get('dimension', section.get('dimension'))
|
||||
stage_label = (stage_labels.get(stage, stage) if isinstance(stage, str)
|
||||
else '未注明阶段' if stage is None else '')
|
||||
dimension_label = (dimension_labels.get(dimension, dimension) if isinstance(dimension, str)
|
||||
else '')
|
||||
raw_text = fact.get('displayText') or fact.get('text') or ''
|
||||
if not isinstance(raw_text, str):
|
||||
raw_text = ''
|
||||
separator = next(((offset, len(token)) for token in ('):', '):', ':', ':')
|
||||
if (offset := raw_text.find(token)) >= 0), None)
|
||||
if separator:
|
||||
offset, width = separator
|
||||
body = raw_text[offset + width:].strip()
|
||||
else:
|
||||
body = raw_text.strip()
|
||||
body = re.sub(r'^筛选\s*\{[^{}]*\}\s*[::]?\s*', '', body).strip()
|
||||
model_text = f'{display_scope}:{body}' if body else display_scope
|
||||
projected = copy.deepcopy(fact)
|
||||
projected['modelText'] = model_text
|
||||
projected['modelScope'] = {
|
||||
'displayScope': display_scope,
|
||||
'stageLabel': stage_label,
|
||||
'dimensionLabel': dimension_label,
|
||||
}
|
||||
result.append(projected)
|
||||
return result
|
||||
|
||||
|
||||
def _history_model_source_goals(snapshot, source_goals):
|
||||
"""Remove machine filter maps from the model-only source-goal view."""
|
||||
result = []
|
||||
for goal in source_goals:
|
||||
if not isinstance(goal, dict):
|
||||
continue
|
||||
section = {'sourceTurnId': goal.get('_sourceTurnId'),
|
||||
'period': goal.get('period'), 'filters': goal.get('filters', {})}
|
||||
display_scope = _history_scope_caption(snapshot, section)
|
||||
view = {key: copy.deepcopy(value) for key, value in goal.items()
|
||||
if key not in ('filters', 'sourceFieldLabels', '_sourceTurnId')}
|
||||
view['displayScopeLabel'] = display_scope
|
||||
result.append(view)
|
||||
return result
|
||||
|
||||
|
||||
def _stage_label(stage):
|
||||
return {'black': '黑片', 'bright': '亮片'}.get(stage, str(stage))
|
||||
|
||||
|
||||
def _history_section_markdown(snapshot, section):
|
||||
rows = section.get('rows') if isinstance(section.get('rows'), list) else []
|
||||
columns = section.get('columns') if isinstance(section.get('columns'), list) else []
|
||||
if not rows or not columns:
|
||||
return None
|
||||
keys = []
|
||||
for column in columns:
|
||||
key = column.get('key')
|
||||
if (not isinstance(key, str) or key in _HISTORY_HIDDEN_COLUMNS
|
||||
or key.lower().endswith('id') or not any(key in row for row in rows)):
|
||||
continue
|
||||
keys.append(key)
|
||||
if not keys:
|
||||
return None
|
||||
title = str(section.get('title') or '')
|
||||
dimension = section.get('dimension')
|
||||
product_table = dimension == 'product' or 'product' in title.lower()
|
||||
unit = _history_unit(snapshot, section)
|
||||
count = len(rows)
|
||||
top = re.search(r'\btop\s*(\d+)\b', title, re.I)
|
||||
is_ranking = bool(top or 'ranking' in title.lower()
|
||||
or any(isinstance(row.get('rank'), int) for row in rows))
|
||||
top_count = min(int(top[1]), count) if top else count
|
||||
sort_label = ('(按合计报废重量排序)' if 'weight' in title.lower() or
|
||||
any(key in ('weight', 'scrapWeight') for key in keys)
|
||||
and {'blackScrapWeight', 'brightScrapWeight'}.issubset(keys) else '')
|
||||
section_title = (f"Top {top_count}{'规格' if product_table else '项'}排名{sort_label}"
|
||||
if is_ranking else title.replace('正式', '').replace('product', '产品'))
|
||||
heading = f"### {_history_scope_caption(snapshot, section)} · {section_title or '已确认数据表'}"
|
||||
labels = {column['key']: str(column.get('label') or column['key']) for column in columns}
|
||||
mapped_labels = []
|
||||
for key in keys:
|
||||
if key == 'label' and product_table:
|
||||
label = '规格'
|
||||
else:
|
||||
label = _HISTORY_COLUMN_LABELS.get(key, labels.get(key, key))
|
||||
if key in _HISTORY_WEIGHT_KEYS or 'weight' in key.lower() or '重量' in label:
|
||||
label += f'({unit})' if unit and '(' not in label and '(' not in label else ''
|
||||
mapped_labels.append(label)
|
||||
header = '| ' + ' | '.join(mapped_labels) + ' |'
|
||||
separator = '| ' + ' | '.join('---' for _ in keys) + ' |'
|
||||
body = []
|
||||
for row in rows:
|
||||
row_stage = {'黑片':'black', '亮片':'bright', '综合':None}.get(row.get('stage'), row.get('stage'))
|
||||
row_unit = (_history_unit(snapshot, section, stage=row_stage)
|
||||
if row.get('metricCode') in ('totalWeight', 'scrapWeight') else unit)
|
||||
body.append('| ' + ' | '.join(_history_cell(row.get(key), key, labels.get(key), row_unit,
|
||||
metric_code=row.get('metricCode'))
|
||||
for key in keys) + ' |')
|
||||
rendered = heading + '\n\n' + '\n'.join((header, separator, *body))
|
||||
note = section.get('note')
|
||||
if isinstance(note, str) and note.strip() and not re.search(
|
||||
r'(?:history-fact:|Q[A-F0-9]{8}|[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}|'
|
||||
r'(?:queryId|sourceTurnId|attemptId|executionId|snapshotHash|scopeHash)\s*[:=]|'
|
||||
r'filters\s*=|\d+\.\d{7,}|\{\s*["]|<[^>]+>)', note, re.I):
|
||||
visible_note = note.strip()
|
||||
if unit:
|
||||
visible_note = visible_note.replace('重量沿用来源单位', f'重量单位为 {unit}')
|
||||
rendered += '\n\n' + visible_note
|
||||
return rendered
|
||||
|
||||
|
||||
def _history_fallback_conclusions(snapshot, sections):
|
||||
conclusions, saw_duplicate = [], len(sections) < len(snapshot.get('sections', []))
|
||||
for section in sections:
|
||||
rows = section.get('rows') if isinstance(section.get('rows'), list) else []
|
||||
columns = section.get('columns') if isinstance(section.get('columns'), list) else []
|
||||
keys = {column.get('key') for column in columns if isinstance(column, dict)}
|
||||
if not rows or not ({'rank', 'weight'} <= keys or {'rank', 'scrapWeight'} <= keys):
|
||||
continue
|
||||
first = next((row for row in rows if row.get('rank') == 1), rows[0])
|
||||
label = str(first.get('label') or first.get('product') or 'Top排名首位')
|
||||
unit = _history_unit(snapshot, section)
|
||||
parts = [f"{_history_scope_caption(snapshot, section)}的Top首位为{label}"]
|
||||
weight_key = next((key for key in ('weight', 'scrapWeight') if key in first), None)
|
||||
if weight_key:
|
||||
parts.append('综合报废重量 ' + _history_cell(first.get(weight_key), weight_key, '报废重量', unit))
|
||||
rate_key = next((key for key in ('compositeRate', 'rate') if key in first), None)
|
||||
if rate_key:
|
||||
parts.append('综合合格率 ' + _history_cell(first.get(rate_key), rate_key, '综合合格率', unit))
|
||||
share_key = next((key for key in ('share', 'cumulativeShare') if key in first), None)
|
||||
if share_key:
|
||||
parts.append(('占同范围全部报废 ' if share_key == 'share' else '累计占比 ')
|
||||
+ _history_cell(first.get(share_key), share_key, share_key, unit))
|
||||
conclusion = ','.join(parts) + '。'
|
||||
conclusions.append(conclusion)
|
||||
if not conclusions:
|
||||
conclusions.append('已确认历史排名表按各自期间和筛选保留,未对不同范围的数值进行合并或重算。')
|
||||
if saw_duplicate:
|
||||
conclusions.append('完全相同范围与内容的排名表只呈现一次;同范围结果由多个已确认来源复核,图表仍保留各自签发来源,占比沿用保存时的完整范围分母。')
|
||||
conclusions.append('正文解释未通过事实绑定校验,报告保持部分完成。')
|
||||
return '\n\n'.join(conclusions)
|
||||
|
||||
|
||||
def _history_fallback_fact_ids(snapshot, *, limit=5):
|
||||
"""Anchor a partial fallback to the first saved fact in each unique scope."""
|
||||
raw_sections = snapshot.get('sections', [])
|
||||
seen_sections, seen_facts, selected = set(), set(), []
|
||||
for section_index, section in enumerate(raw_sections):
|
||||
signature = _history_section_signature(section)
|
||||
if signature in seen_sections:
|
||||
continue
|
||||
seen_sections.add(signature)
|
||||
candidates = [fact for fact in snapshot.get('facts', [])
|
||||
if fact.get('sectionIndex') == section_index
|
||||
and isinstance(fact.get('id'), str)
|
||||
and (fact.get('displayText') or fact.get('text'))]
|
||||
candidates.sort(key=lambda fact: (fact.get('rowIndex') if isinstance(fact.get('rowIndex'), int) else -1,
|
||||
str(fact.get('id'))))
|
||||
if candidates:
|
||||
identifier = candidates[0]['id']
|
||||
if identifier not in seen_facts:
|
||||
selected.append(identifier)
|
||||
seen_facts.add(identifier)
|
||||
if len(selected) >= limit:
|
||||
break
|
||||
return selected
|
||||
|
||||
|
||||
def _history_public_missing(snapshot, *, explanation_ok, title_ok, native_missing=()):
|
||||
missing = []
|
||||
coverage = snapshot.get('historyCoverage') if isinstance(snapshot.get('historyCoverage'), dict) else {}
|
||||
unavailable = any(isinstance(source, dict) and not source.get('available')
|
||||
for source in snapshot.get('historySources', []))
|
||||
if coverage.get('complete') is not True or unavailable:
|
||||
missing.append('部分已选历史来源未能完整核验。')
|
||||
if snapshot.get('facts') and not explanation_ok:
|
||||
missing.append('正文解释未通过事实绑定校验。')
|
||||
elif native_missing:
|
||||
missing.append('正文解释尚未覆盖全部要求。')
|
||||
if snapshot.get('facts') and not title_ok:
|
||||
missing.append(('报告正文已通过核验,但报告标题尚未生成或未通过内容核验。'
|
||||
if explanation_ok else '报告标题尚未生成或未通过内容核验。'))
|
||||
if not snapshot.get('facts'):
|
||||
missing.append('已确认历史快照没有可用于解释的正式指标事实。')
|
||||
if not missing and not explanation_ok:
|
||||
missing.append('报告内容尚未完成核验。')
|
||||
reasons = list(dict.fromkeys(missing))
|
||||
if not reasons:
|
||||
return []
|
||||
return [';'.join(reason.rstrip('。') for reason in reasons) + ',报告保持部分完成。']
|
||||
|
||||
|
||||
def _history_public_source_gaps(snapshot):
|
||||
coverage = snapshot.get('historyCoverage') if isinstance(snapshot.get('historyCoverage'), dict) else {}
|
||||
unavailable = any(isinstance(source, dict) and not source.get('available')
|
||||
for source in snapshot.get('historySources', []))
|
||||
if coverage.get('complete') is not True or unavailable:
|
||||
return ['部分已选历史来源未能完整核验。']
|
||||
return []
|
||||
|
||||
|
||||
def _history_partial_fallback(snapshot, *, missing, headline_ids=()):
|
||||
sections = _unique_history_sections(snapshot)
|
||||
tables = [table for section in sections
|
||||
if (table := _history_section_markdown(snapshot, section))]
|
||||
if snapshot.get('facts'):
|
||||
lead = _history_fallback_conclusions(snapshot, sections)
|
||||
elif tables:
|
||||
lead = ('当前历史快照没有可供模型解释的指标事实,以下仅呈现已确认历史表格;'
|
||||
'正文解释尚未完成,报告保持部分完成。')
|
||||
else:
|
||||
lead = '确认的历史快照没有可用于解释的指标事实,报告解释尚未完成。'
|
||||
if snapshot.get('facts') and not tables:
|
||||
lead = ('确认的历史快照没有可直接呈现的重点排名表。'
|
||||
'正文解释未通过事实绑定校验,报告保持部分完成。')
|
||||
answer = '\n\n'.join([lead, *tables])
|
||||
visual_ids = list(dict.fromkeys(chart['id'] for chart in snapshot.get('visuals', [])
|
||||
if isinstance(chart, dict) and isinstance(chart.get('id'), str)))[:3]
|
||||
evidence_refs = []
|
||||
selected_fact_ids = set(headline_ids if isinstance(headline_ids, (list, tuple)) else [])
|
||||
evidence_refs.extend(fact['id'] for fact in snapshot.get('facts', [])
|
||||
if fact.get('id') in selected_fact_ids and isinstance(fact.get('id'), str))
|
||||
evidence_refs.extend(ref for chart in snapshot.get('visuals', []) if isinstance(chart, dict)
|
||||
for ref in chart.get('evidenceRefs', []) if isinstance(ref, str))
|
||||
summary = {'version': 1, 'lead': lead, 'evidence': tables, 'notices': list(missing),
|
||||
'evidenceRefs': list(dict.fromkeys(evidence_refs)), 'visualIds': visual_ids}
|
||||
return answer, summary, sections
|
||||
|
||||
|
||||
def _model_source_frames(snapshot):
|
||||
by_turn = {source['turnId']: [] for source in snapshot['historySources']}
|
||||
seen_by_turn = {turn_id: set() for turn_id in by_turn}
|
||||
@@ -561,49 +1345,70 @@ def execute_history_report(*, request, task, authority, actor_root, delegated,
|
||||
if autonomous_answer:
|
||||
from nao_native_answer import synthesize_history_snapshot
|
||||
from nao_quality_runtime import _atomic_json
|
||||
model_snapshot = copy.deepcopy(snapshot)
|
||||
model_snapshot['facts'] = _deduplicated_history_facts(snapshot)
|
||||
native_final = synthesize_history_snapshot(base_url=connection[0], api_key=connection[1],
|
||||
model=connection[2], question=request.get('question'), snapshot=snapshot,
|
||||
model=connection[2], question=request.get('question'), snapshot=model_snapshot,
|
||||
task=task, authority=authority, release_id=request.get('releaseId'),
|
||||
request_turn_id=request.get('turnId'), cancel_event=cancel_event)
|
||||
private = actor_root / 'private'
|
||||
private.mkdir(parents=True, exist_ok=True)
|
||||
_atomic_json(private / 'native-composition.json', {'turnId': request.get('turnId'),
|
||||
'rawModelResponses': native_final.get('rawModelResponses', []),
|
||||
'bindingInput': native_final.get('_privateBindingInput'),
|
||||
'compositionDiagnostics': native_final.get('compositionDiagnostics', {}),
|
||||
'readingSummary': native_final.get('readingSummary')})
|
||||
if native_final.get('evidenceSufficient'):
|
||||
selection = (native_final['reportTitle'], native_final.get('historyHeadlineFactIds', []))
|
||||
'readingSummary': native_final.get('readingSummary'),
|
||||
'reportTitle': native_final.get('reportTitle'),
|
||||
'titleValid': native_final.get('titleValid'),
|
||||
'titleGeneratedByModel': native_final.get('titleGeneratedByModel'),
|
||||
'titleSource': native_final.get('titleSource')})
|
||||
else:
|
||||
selection = select_history_headline(snapshot=snapshot, question=request.get('question'),
|
||||
connection=connection, cancel_event=cancel_event)
|
||||
if selection is None:
|
||||
if selection is None and not autonomous_answer:
|
||||
error_type = 'CancelledOrInvalidHeadlineSelection' if cancel_event.is_set() else 'InvalidHeadlineSelection'
|
||||
except Exception as error:
|
||||
error_type = type(error).__name__
|
||||
native_body_sufficient = (native_final.get('bodyEvidenceSufficient', native_final.get('evidenceSufficient'))
|
||||
if isinstance(native_final,dict) else False)
|
||||
native_explanation_ok = autonomous_answer and native_body_sufficient is True
|
||||
native_title_info = _native_history_title_info(native_final, snapshot, request.get('question'))
|
||||
native_model_title_ok = bool(native_title_info and native_title_info[1])
|
||||
native_safe_title = native_title_info[0] if native_title_info and not native_title_info[1] else None
|
||||
if native_model_title_ok:
|
||||
known_fact_ids = {fact['id'] for fact in snapshot['facts'] if isinstance(fact.get('id'), str)}
|
||||
raw_headline_ids = native_final.get('historyHeadlineFactIds')
|
||||
headline_ids = list(dict.fromkeys(value for value in raw_headline_ids
|
||||
if isinstance(raw_headline_ids, list) and isinstance(value, str)
|
||||
and value in known_fact_ids))[:5]
|
||||
selection = (native_title_info[0], headline_ids)
|
||||
if autonomous_answer and snapshot['facts'] and selection is None and not cancel_event.is_set():
|
||||
if error_type is None:
|
||||
error_type = ('IncompleteNativeComposition' if native_final is not None
|
||||
and not native_explanation_ok else 'InvalidNativeTitle')
|
||||
timings['historyHeadline'] = round((time.monotonic() - model_started) * 1000)
|
||||
title_ok = selection is not None
|
||||
report_title, headline_ids = selection if title_ok else ('历史质量分析报告', [])
|
||||
if native_final:
|
||||
report_title = native_final['reportTitle']
|
||||
headline_ids = native_final.get('historyHeadlineFactIds', [])
|
||||
report_title, headline_ids = selection if title_ok else (
|
||||
native_safe_title or _history_fallback_title(snapshot), [])
|
||||
coverage = snapshot['historyCoverage']
|
||||
complete = bool(coverage['complete'] and title_ok and snapshot['facts'])
|
||||
missing = list(coverage['missingEvidence'])
|
||||
if native_final:
|
||||
missing.extend(native_final.get('missingEvidence') or [])
|
||||
for source in snapshot['historySources']:
|
||||
if source['available']:
|
||||
continue
|
||||
reason = source.get('reason')
|
||||
if reason:
|
||||
missing.append(reason)
|
||||
if not coverage['complete'] and not missing:
|
||||
missing.append('部分已确认的历史来源暂不可用,报告尚未完整整合。')
|
||||
if not snapshot['facts']:
|
||||
missing.append('确认的历史快照没有可引用事实,未生成报告标题。')
|
||||
elif not title_ok:
|
||||
missing.append('历史报告可信依据已载入,但解释答复尚未完成。' if autonomous_answer else '历史报告可信依据已载入,但标题或重点事实选择未完成。')
|
||||
missing = list(dict.fromkeys(missing))
|
||||
content_ok = (native_explanation_ok if autonomous_answer else True)
|
||||
complete = bool(coverage['complete'] and title_ok and snapshot['facts'] and content_ok)
|
||||
if autonomous_answer:
|
||||
missing = _history_public_missing(snapshot, explanation_ok=native_explanation_ok,
|
||||
title_ok=native_model_title_ok,
|
||||
native_missing=(native_final.get('missingEvidence') or []) if native_explanation_ok else ())
|
||||
else:
|
||||
missing = list(coverage['missingEvidence'])
|
||||
for source in snapshot['historySources']:
|
||||
if not source['available'] and source.get('reason'):
|
||||
missing.append(source['reason'])
|
||||
if not coverage['complete'] and not missing:
|
||||
missing.append('部分已确认的历史来源暂不可用,报告尚未完整整合。')
|
||||
if not snapshot['facts']:
|
||||
missing.append('确认的历史快照没有可引用事实,未生成报告标题。')
|
||||
elif not title_ok:
|
||||
missing.append('历史报告可信依据已载入,但标题或重点事实选择未完成。')
|
||||
missing = list(dict.fromkeys(missing))
|
||||
_write_private_diagnostic(actor_root, identity=identity, snapshot=snapshot,
|
||||
coverage_ok=coverage['complete'], model_ok=title_ok,
|
||||
error_type=error_type)
|
||||
@@ -612,28 +1417,56 @@ def execute_history_report(*, request, task, authority, actor_root, delegated,
|
||||
'label': '读取已确认历史报告快照', 'input': {}, 'status': 'completed',
|
||||
'startedAt': datetime_now(), 'finishedAt': datetime_now(),
|
||||
'outputSummary': f"已导入{coverage['importedSources']}/{coverage['expectedSources']}个确认历史来源。"}
|
||||
if autonomous_answer and not native_explanation_ok:
|
||||
fallback_ids = headline_ids or _history_fallback_fact_ids(snapshot)
|
||||
headline_ids = fallback_ids
|
||||
answer, safe_summary, _ = _history_partial_fallback(snapshot, missing=missing, headline_ids=fallback_ids)
|
||||
else:
|
||||
answer = native_final.get('answer','') if native_explanation_ok else ''
|
||||
safe_summary = native_final.get('readingSummary') if native_explanation_ok else None
|
||||
saved_chart_refs = list(dict.fromkeys(ref for chart in snapshot.get('visuals', [])
|
||||
if isinstance(chart, dict) for ref in chart.get('evidenceRefs', []) if isinstance(ref, str)))
|
||||
public_history_coverage = copy.deepcopy(snapshot['historyCoverage'])
|
||||
if autonomous_answer:
|
||||
public_history_coverage['missingEvidence'] = _history_public_source_gaps(snapshot)
|
||||
result = {'type': 'result', 'engine': 'NAO', 'status': 'COMPLETED' if complete else 'PARTIAL',
|
||||
'answer': native_final.get('answer', '') if native_final else '', 'stopReason': 'history_report_complete' if complete else 'history_report_partial',
|
||||
'answer': answer, 'stopReason': 'history_report_complete' if complete else 'history_report_partial',
|
||||
'finishReason': 'stop', 'completionMethod': 'confirmed_history_report',
|
||||
'agentTask': copy.deepcopy(task),
|
||||
'protocol': identity, 'actualModel': connection[2], 'phaseTimingsMs': timings,
|
||||
'protocolEvidence': [], 'queryEvidenceRefs': [], 'toolCalls': [source_call], 'toolCount': 1,
|
||||
'visualSchemaVersion': 1, 'visualState': 'not_requested', 'visuals': [],
|
||||
'protocolEvidence': [], 'queryEvidenceRefs': saved_chart_refs,
|
||||
'toolCalls': [source_call], 'toolCount': 1,
|
||||
'visualSchemaVersion': snapshot.get('visualSchemaVersion', 1),
|
||||
'visualState': snapshot.get('visualState', 'not_requested'),
|
||||
'visuals': copy.deepcopy(snapshot.get('visuals', [])),
|
||||
'historyVisualSources': copy.deepcopy(snapshot.get('historyVisualSources', [])),
|
||||
'historyEvidence': copy.deepcopy(snapshot['historyEvidence']),
|
||||
'historyCoverage': copy.deepcopy(snapshot['historyCoverage']),
|
||||
'historyCoverage': public_history_coverage,
|
||||
'historySourceTurnIds': list(task['historySourceTurnIds']),
|
||||
'historyHeadlineFactIds': headline_ids, 'reportTitle': report_title,
|
||||
'titleGeneratedByModel': title_ok and not autonomous_answer,
|
||||
'titleGeneratedByModel': bool(title_ok and (not autonomous_answer or native_model_title_ok)),
|
||||
# Confirmed History reports always require a content title. Do not
|
||||
# let a missing/malformed composer flag silently downgrade that contract.
|
||||
'titleRequired': True,
|
||||
'titleValid': bool(title_ok and (not autonomous_answer or native_model_title_ok)),
|
||||
'titleMissingEvidence': list(native_final.get('titleMissingEvidence') or [])
|
||||
if autonomous_answer and isinstance(native_final,dict) else ([] if title_ok else ['报告标题尚未生成或未通过内容核验。']),
|
||||
'evidenceCompletion': {'serverCoverageSufficient': coverage['complete'],
|
||||
'modelDeclaredEvidenceSufficient': None,
|
||||
'modelDeclaredEvidenceSufficient': native_explanation_ok if autonomous_answer else None,
|
||||
'bodyEvidenceSufficient': native_explanation_ok if autonomous_answer else None,
|
||||
'titleRequired': True if autonomous_answer else None,
|
||||
'titleValid': native_model_title_ok if autonomous_answer else None,
|
||||
'titleMissingEvidence': list(native_final.get('titleMissingEvidence') or [])
|
||||
if autonomous_answer and isinstance(native_final,dict) else [],
|
||||
'missingEvidence': missing}}
|
||||
if native_final:
|
||||
result['readingSummary'] = native_final.get('readingSummary')
|
||||
if native_explanation_ok:
|
||||
result['readingSummary'] = safe_summary
|
||||
result['titleSource'] = native_final.get('titleSource')
|
||||
elif autonomous_answer:
|
||||
result['answer'] = '本轮尚无可解释的历史指标事实;已确认的表格、清单或原始记录由平台附录展示。'
|
||||
result['readingSummary'] = {'version': 1, 'lead': result['answer'], 'evidence': [],
|
||||
'notices': missing, 'evidenceRefs': []}
|
||||
result['readingSummary'] = safe_summary or {'version':1,'lead':answer,'evidence':[],
|
||||
'notices':missing,'evidenceRefs':[],'visualIds':[]}
|
||||
result['titleSource'] = ('model_verified_content' if native_model_title_ok
|
||||
else 'safe_partial_fallback')
|
||||
emit(result)
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user