Files
quality_analyse/scripts/nao_native_run.py
T
lyun f1a2fbd494 统一:DSH 智能分析、Top100 与图表静默恢复
接入 DSH 专用运行时和对话界面,保留模型原始回答,支持会话分页、连续追问与显式报告。
补齐组合筛选、各维度 Top100、分组查询到排名图的转换,图表故障静默省略并基于保存数据自动恢复。
优化报告风格、图表顺序和表格换行,更新接入说明及构建输入。

验证:独立目录前后端构建通过;118 项 Java、102 项前端、102 项图表、14 项 DSH 应用和 13 项组合筛选检查通过。
仅提交相关源码、锁定依赖与测试;不包含凭据、会话、业务数据或本地产物。
2026-11-12 11:57:41 +08:00

304 lines
20 KiB
Python

"""Run the upstream NAO loop; platform checks identity, reads and final claims."""
from __future__ import annotations
import copy
import re
import time
def exploration_notices(scopes, coverage):
by_pair = {(s.get('nodeId'),s.get('scopeId')):s for s in scopes}
grouped = {}
purposes = {'defects':'细分缺陷','records':'原始记录','source':'原件核验',
'changes':'批量比较','metrics':'正式指标','trend':'趋势'}
for row in coverage if isinstance(coverage,list) else []:
if row.get('complete') is True: continue
scope = by_pair.get((row.get('nodeId'),row.get('scopeId')), {})
child = scope.get('task') or {}
stage = '/'.join({'black':'黑片','bright':'亮片'}.get(s,s) for s in child.get('stages',[]))
kind = stage + purposes.get(scope.get('purpose'),'补充调查')
label = str(scope.get('label') or '当前范围')
grouped.setdefault(kind,[]).append('当前范围' if label == '基础范围' else label)
return [kind+'尚未完整核实:'+'、'.join(dict.fromkeys(labels))+';未用于已核实结论。'
for kind,labels in grouped.items()]
def _answer_evidence_refs(answer):
"""Return only explicit query references present in the upstream answer."""
return list(dict.fromkeys(re.findall(r'\bQ[A-F0-9]{8}\b', answer)))
def run_native_exploration(*, request, task, authority, actor_root, delegated, connection,
manager, state, cancel_event, emit, prior_evidence_files, phase_timings=None):
from nao_quality_runtime import _atomic_json, _authenticate, _sse_request, _read_json
from nao_agent_task import bind_task
from nao_protocol_execution import _identity, query_evidence_bindings
from nao_evidence_finalizer import evidence_summaries
from analysis_visual_evidence import protocol_visual_result
from nao_runtime_policy import native_timeout
from nao_native_exploration import _proofs
timings = dict(phase_timings or {})
identity = _identity(authority)
# _serverHistory is already reconstructed and authorized by the server.
# A fresh NAO chat each turn prevents hidden native memory restoring filters.
context = bind_task({key: copy.deepcopy(value) for key, value in request['context'].items()
if key in ('_serverHistory', '_serverQuestions', '_protocol')}, task)
context['releaseId'] = request['releaseId']
context['_nativeExecutionId'] = identity['executionId']
request['context'] = context
_atomic_json(actor_root / 'private' / 'last-agent-task.json', {
'sessionId': request['sessionId'], 'releaseId': request['releaseId'],
'task': task, 'protocol': identity})
started = time.monotonic()
engine = request.get('runtimeEngine', 'NAO')
model = connection[2]
if engine == 'NAO':
_, _, model, base_url = manager.ensure_instance(actor_root, request['sessionId'],
expected_actor_id=request['actorId'], model_connection=connection)
client = _authenticate(actor_root, base_url)
timings['nativeStartup'] = round((time.monotonic() - started) * 1000)
elif engine != 'DSH':
raise ValueError('分析引擎无效。')
emit({'type': 'progress', 'message': '正在根据实际结果自主调查…', 'executionMode': 'native_agent'})
started = time.monotonic()
investigation_started = started
def track_progress(event):
if ('firstUsefulMs' not in timings and event.get('type') == 'progress'
and any(call.get('status') == 'completed' and (call.get('input') or {}).get('mode') not in (None, 'catalog')
and '证据:' in str(call.get('outputSummary', '')) for call in event.get('toolCalls', []))):
timings['firstUsefulMs'] = (sum(timings.get(key, 0) for key in
('planningAndReview', 'taskConfirmation', 'nativeStartup'))
+ round((time.monotonic() - investigation_started) * 1000))
emit(event)
if engine == 'DSH':
from dsh_agent_runtime import run_dsh_agent
from nao_native_exploration import NativeExplorationTools, native_model_view
from nao_quality_tools import tool_definition
from nao_runtime_policy import turn_deadline
tools = NativeExplorationTools(delegated=delegated, evidence_dir=actor_root/'evidence',
task=task, identity={**identity, 'releaseId':request['releaseId']},
state_path=actor_root/'private'/'native-exploration-state.json')
def authorize_dsh():
if cancel_event.is_set():
raise RuntimeError('cancelled')
delegated.call('/api/analysis/model-authorization', method='GET')
accumulator, native_status, native_reason, stop_succeeded = run_dsh_agent(
actor_root=actor_root, connection=connection, question=request['question'],
context=context, task=task, identity=identity, turn_id=request['turnId'],
cancel_event=cancel_event, emit=track_progress,
tool_handler=lambda args:native_model_view(tools.quality_data(args)),
authorize=authorize_dsh, tool_definition=tool_definition(native_protocol=True), deadline=turn_deadline())
else:
accumulator, native_status, native_reason, stop_succeeded = _sse_request(
client=client, base_url=base_url, model=model, question=request['question'],
release_id=request['releaseId'], context=context, chat_id=None,
session_state=state, session_id=request['sessionId'], actor_root=actor_root,
emit=track_progress, cancel_event=cancel_event, timeout=native_timeout(),
prior_evidence_files=prior_evidence_files, turn_id=request['turnId'])
timings['investigation'] = round((time.monotonic() - started) * 1000)
draft_path = actor_root / 'private' / 'native-draft.json'
_atomic_json(draft_path, {'turnId': request['turnId'], 'executionId': identity['executionId'],
'answer': accumulator.answer, 'status': native_status,
'reason': native_reason})
if cancel_event.is_set() or native_status == 'CANCELLED':
result = {'type': 'result', 'engine': engine, 'status': 'CANCELLED', 'answer': '',
'stopReason': '用户停止', 'finishReason': 'cancelled', 'agentTask': task,
'protocol': identity, 'executionMode': 'native_agent', 'toolCalls': accumulator.tool_calls,
'toolCount': len(accumulator.tool_calls), 'timeline': accumulator.timeline,
'queryEvidenceRefs': [], 'protocolEvidence': [], 'visuals': [], 'visualState': 'not_requested'}
emit(result)
return result
snapshot = delegated.call('/api/analysis/protocol/queries', payload={}, method='POST')
scopes = snapshot.get('scopes', [])
receipts = [q.get('proof', {}) for q in snapshot.get('queries', []) if isinstance(q, dict)]
accepted_ids = {p.get('queryId') for p in receipts if p.get('executionId') == identity['executionId']}
coverage_rows = snapshot.get('scopeCoverage')
complete_scopes = ({(row.get('nodeId'), row.get('scopeId')) for row in coverage_rows
if isinstance(row, dict) and row.get('complete') is True}
if isinstance(coverage_rows, list) else None)
documents = evidence_summaries(actor_root / 'evidence', request['releaseId'],
exclude_files=prior_evidence_files, allow_empty=True)
documents = [doc for doc in documents if _proofs(doc.get('result', {}))
and all(p.get('executionId') == identity['executionId'] and p.get('queryId') in accepted_ids
and (complete_scopes is None or (p.get('nodeId'), p.get('scopeId')) in complete_scopes)
for p in _proofs(doc.get('result', {})))]
warnings = list(snapshot.get('explorationWarnings', []))
summary_warnings = exploration_notices(scopes,coverage_rows) or list(warnings)
missing = list(snapshot.get('rootGoalGaps', []))
if complete_scopes is None:
missing.extend(snapshot.get('gaps', []))
progress_path = actor_root / 'private' / 'native-exploration-state.json'
progress = _read_json(progress_path) if progress_path.is_file() else {}
timings['formalQuery'] = progress.get('queryElapsedMs', 0)
controlled_no_progress = (progress.get('executionId') == identity['executionId']
and progress.get('halted')
and type(progress.get('noProgress')) is int and progress['noProgress'] >= 3)
if controlled_no_progress:
missing.append('连续三次没有取得新证据,调查已停止;其余线索尚未验证。')
natural_finish = bool(accumulator.finish_reasons and accumulator.finish_reasons[-1] == 'stop'
and stop_succeeded is not False
and getattr(accumulator, 'final_answer_ready', True))
corrected_tool_errors = (native_reason == 'mcp_error' and natural_finish
and complete_scopes is not None and bool(documents)
and not missing and not progress.get('halted'))
if native_status != 'COMPLETED' and not corrected_tool_errors and not controlled_no_progress:
reason_label = {
'authorization_unavailable': '授权检查暂未完成',
'authorization_rejected': '本轮授权检查被拒绝',
'authorization_revoked': '本轮授权已失效',
'timeout': '已达到本轮时间上限',
'transport_error': '运行连接中断',
}.get(native_reason, str(native_reason or native_status))
missing.append('自主调查未正常结束(' + reason_label + '),已保留核实结果。')
elif native_status == 'COMPLETED' and not natural_finish:
missing.append('自主调查未收到有效的正常结束信号。')
if not documents:
missing.append('本轮未取得当前执行的可核验查询证据。')
context['_protocolScopes'] = {'scopes': scopes, 'pending': snapshot.get('pending', []), 'gaps': missing}
started = time.monotonic()
visuals = protocol_visual_result(documents, scopes, request['question'], request['turnId'],
release_id=request['releaseId'], base_context=context)
timings['visuals'] = round((time.monotonic() - started) * 1000)
emit({'type': 'progress', 'message': '正在生成正式图表并整理证据状态…', 'executionMode': 'native_agent'})
visual_warnings = list(visuals.get('missingEvidence', []))
if task.get('visualPreference') == 'required':
missing.extend(visual_warnings)
else:
if visual_warnings and not summary_warnings:
summary_warnings = ['部分重点图表未能完整展示;已保留核实文字和表格。']
missing = list(dict.fromkeys(str(item) for item in missing if item))
answer = accumulator.answer if isinstance(accumulator.answer, str) else ''
checked_answer = None
narrative_binding = 'not_assessed'
if engine == 'DSH' and not documents:
narrative_binding = 'rejected'
answer = '本轮尚未取得可核验的查询证据,不能形成正式分析结论。请检查数据范围或重新分析。'
missing.append('未取得可核验查询证据,调查草稿未作为分析结论发布。')
if engine == 'DSH' and documents and answer.strip():
# A successful agent loop is not a business-content acceptance. Keep its
# draft privately and bind the published narration to signed evidence.
from nao_evidence_finalizer import synthesize_native, FinalizationUnavailable
from nao_protocol_execution import _platform_visual_context, _save_composition
checked_context = {**context, '_autonomousAnswer': True,
'_platformVisuals': _platform_visual_context(visuals)}
emit({'type': 'progress', 'message': '正在核对分析结论与正式证据…',
'executionMode': 'native_agent'})
synthesis_started = time.monotonic()
try:
checked_answer = synthesize_native(base_url=connection[0], api_key=connection[1],
model=model, question=request['question'], evidence=documents,
cancel_event=cancel_event, context=checked_context, native_answer=answer)
_save_composition(checked_answer, actor_root, request['turnId'])
answer = checked_answer.get('answer', '')
checked_complete = (checked_answer.get('evidenceSufficient') is True
and checked_answer.get('finishReason') == 'stop'
and not (checked_answer.get('titleRequired') and not checked_answer.get('titleValid')))
narrative_binding = 'verified_composition' if checked_complete else 'incomplete'
missing.extend(checked_answer.get('missingEvidence', []))
missing.extend(checked_answer.get('titleMissingEvidence', []))
if narrative_binding != 'verified_composition':
missing.append('报告结论尚未全部通过证据核验。')
except (FinalizationUnavailable, ValueError, OSError):
narrative_binding = 'rejected'
missing.append('报告结论未通过证据核验,已保留查询证据。')
answer = '本轮已取得查询证据,但报告结论未通过核验。请查看已核实的图表与数据,或重新分析。'
timings['synthesis'] = round((time.monotonic()-synthesis_started)*1000)
if cancel_event.is_set():
result = {'type':'result', 'engine':engine, 'status':'CANCELLED', 'answer':'',
'stopReason':'用户停止', 'finishReason':'cancelled', 'agentTask':task,
'protocol':identity, 'executionMode':'native_agent',
'toolCalls':accumulator.tool_calls, 'timeline':accumulator.timeline,
'queryEvidenceRefs':[], 'protocolEvidence':[], 'visuals':[],
'visualState':'not_requested'}
emit(result)
return result
original_refs = _answer_evidence_refs(answer)
complete_receipt_ids = {p.get('queryId') for p in receipts if isinstance(p, dict)
and p.get('executionId') == identity['executionId'] and isinstance(p.get('queryId'), str)
and p.get('queryId') in accepted_ids
and complete_scopes is not None and (p.get('nodeId'), p.get('scopeId')) in complete_scopes}
verified_document_ids = {}
for doc in documents:
proofs = _proofs(doc.get('result', {})) if isinstance(doc, dict) else []
proof_ids = list(dict.fromkeys(p.get('queryId') for p in proofs
if isinstance(p, dict) and isinstance(p.get('queryId'), str)))
if (doc.get('evidenceRef') and proof_ids
and all(query_id in complete_receipt_ids for query_id in proof_ids)):
verified_document_ids[doc['evidenceRef']] = proof_ids
document_refs = set(verified_document_ids)
cited_refs = set(original_refs) & document_refs
cited_ids = list(dict.fromkeys(query_id for doc_ref in original_refs if doc_ref in cited_refs
for query_id in verified_document_ids[doc_ref]))
unresolved_refs = sorted(set(original_refs) - cited_refs)
if unresolved_refs:
missing.extend('原文引用未关联本轮完整scope正式查询证据:' + ref + '。' for ref in unresolved_refs)
background_ids = list(dict.fromkeys(query_id for proof_ids in verified_document_ids.values()
for query_id in proof_ids)) if not original_refs else []
if not original_refs and not background_ids and documents:
missing.append('本轮没有可关联的完整scope正式查询证明。')
conclusion_ids = cited_ids if original_refs else background_ids
if len(conclusion_ids) > 40:
missing.append('完整查询证据引用超过平台允许的40项上限。')
conclusion_ids = conclusion_ids[:40]
if not answer.strip():
missing.append('自主分析未生成正文。')
missing = list(dict.fromkeys(str(item) for item in missing if item))
data_complete = bool(documents and not snapshot.get('rootGoalGaps')
and (complete_scopes is not None or not snapshot.get('gaps'))
and bool(conclusion_ids) and not unresolved_refs
and len(cited_ids if original_refs else background_ids) <= 40)
narrative_complete = bool(answer.strip() and natural_finish
and (native_status == 'COMPLETED' or corrected_tool_errors)
and (engine != 'DSH' or narrative_binding == 'verified_composition'))
charts_complete = not visual_warnings
complete = bool(data_complete and narrative_complete and not missing)
if warnings or visual_warnings:
_atomic_json(actor_root / 'private' / 'native-warning-diagnostics.json', {
'turnId': request['turnId'], 'explorationWarnings': warnings,
'visualWarnings': visual_warnings})
_atomic_json(actor_root / 'private' / 'native-original-body-diagnostics.json', {
'turnId': request['turnId'], 'executionId': identity['executionId'],
'nativeStatus': native_status, 'nativeReason': native_reason,
'finishReasons': list(accumulator.finish_reasons),
'originalBodyCharacters': len(answer), 'originalBodyNonempty': bool(answer.strip()),
'narrativeComplete': narrative_complete, 'dataComplete': data_complete,
'narrativeFactBinding': narrative_binding, 'citedEvidenceRefs': sorted(cited_refs),
'unresolvedEvidenceRefs': unresolved_refs, 'missingEvidence': missing,
'visualWarnings': visual_warnings, 'notices': list(dict.fromkeys(summary_warnings))})
result = {'type': 'result', 'engine': engine, 'status': 'COMPLETED' if complete else 'PARTIAL',
'answer': answer, 'readingSummary': (checked_answer or {}).get('readingSummary'),
'reportTitle': (checked_answer or {}).get('reportTitle'), 'agentTask': task, 'protocol': identity,
'executionMode': 'native_agent', 'completionMethod': 'native_autonomous_analysis',
'stopReason': 'evidence_finalized' if complete else 'evidence_incomplete',
'finishReason': ('stop' if narrative_complete else
str(accumulator.finish_reasons[-1]) if natural_finish and accumulator.finish_reasons else
'incomplete' if native_status == 'COMPLETED' else str(native_reason or native_status)),
'nativeStopReason': native_reason, 'nativeStopSucceeded': stop_succeeded,
'nativeFinishReason': 'stop' if natural_finish else native_reason,
'conclusionProtocolQueryIds': conclusion_ids,
'answerQuality': {'version': 1, 'dataComplete': data_complete,
'narrativeComplete': narrative_complete,
'chartsComplete': charts_complete,
'narrativeFactBinding': narrative_binding,
'citedEvidenceRefs': sorted(cited_refs),
'backgroundEvidenceQueryIds': background_ids,
'unresolvedEvidenceRefs': unresolved_refs,
'missingEvidence': missing,
'notices': list(dict.fromkeys(summary_warnings))},
'actualModel': model, 'phaseTimingsMs': timings,
'protocolEvidence': receipts, 'queryEvidenceRefs': [doc['evidenceRef'] for doc in documents],
'queryEvidenceBindings': query_evidence_bindings(documents, receipts),
'toolCalls': accumulator.tool_calls, 'toolCount': len(accumulator.tool_calls),
'timeline': accumulator.timeline, 'factsComplete': None,
'evidenceCompletion': {'serverCoverageSufficient': not snapshot.get('rootGoalGaps'),
'missingEvidence': missing},
'agentContext': {key: copy.deepcopy(task[key]) for key in
('filters', 'period', 'comparisonPeriod') if task.get(key) is not None},
'visualSchemaVersion': 1, 'visualState': visuals.get('visualState', 'not_requested'),
'visuals': visuals.get('visuals', [])}
result['agentContext']['releaseId'] = request['releaseId']
_atomic_json(actor_root / 'private' / 'native-completion.json', result)
emit(result)
return result