"""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() _, _, 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) 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) 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': 'NAO', '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: missing.append('自主调查未正常结束(' + str(native_reason or native_status) + '),已保留核实结果。') 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 '' 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)) 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': 'not_assessed', 'citedEvidenceRefs': sorted(cited_refs), 'unresolvedEvidenceRefs': unresolved_refs, 'missingEvidence': missing, 'visualWarnings': visual_warnings, 'notices': list(dict.fromkeys(summary_warnings))}) result = {'type': 'result', 'engine': 'NAO', 'status': 'COMPLETED' if complete else 'PARTIAL', 'answer': answer, 'readingSummary': None, 'reportTitle': None, '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': 'not_assessed', '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