"""Run the upstream NAO loop; platform checks identity, reads and final claims.""" from __future__ import annotations import copy 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 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, _platform_visual_context, query_evidence_bindings from nao_evidence_finalizer import evidence_summaries, synthesize_native 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 = 'stop' in accumulator.finish_reasons and stop_succeeded is not False 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) + '),已保留核实结果。') 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) context['_platformVisuals'] = _platform_visual_context(visuals) emit({'type': 'progress', 'message': '正在核对结论与图表…', 'executionMode': 'native_agent'}) started = time.monotonic() final = synthesize_native(base_url=connection[0], api_key=connection[1], model=connection[2], question=request['question'], evidence=documents, cancel_event=cancel_event, context=context, native_answer=accumulator.answer) timings['synthesis'] = round((time.monotonic() - started) * 1000) _atomic_json(actor_root / 'private' / 'native-composition.json', { 'turnId': request['turnId'], 'rawModelResponses': final.get('rawModelResponses', []), 'compositionDiagnostics': final.get('compositionDiagnostics', {}), 'bindingInput': final.get('_privateBindingInput'), 'titleMetadata': {key: final.get(key) for key in ('reportTitle', 'titleGeneratedByModel', 'titleValid', 'titleSource')}, 'readingSummary': final.get('readingSummary')}) full_facts = final.get('fullEvidenceFacts', []) if full_facts: _atomic_json(actor_root / 'private' / 'native-full-evidence-facts.json', full_facts) missing.extend(final.get('missingEvidence', [])) 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)) complete = bool(documents and final.get('evidenceSufficient') and not missing) used_refs = set(final.get('usedEvidenceRefs', [])) cited_ids = list(dict.fromkeys(p['queryId'] for doc in documents if doc.get('evidenceRef') in used_refs for p in _proofs(doc.get('result', {})))) if final.get('evidenceSufficient') and not cited_ids: missing.append('解释未关联当前执行的实际查询证明。') complete = False if warnings or visual_warnings: _atomic_json(actor_root / 'private' / 'native-warning-diagnostics.json', { 'turnId': request['turnId'], 'explorationWarnings': warnings, 'visualWarnings': visual_warnings}) if summary_warnings: if isinstance(final.get('readingSummary'), dict): final['readingSummary']['notices'] = list(dict.fromkeys([ *final['readingSummary'].get('notices', []), *summary_warnings])) else: final['answer'] += '\n\n' + '\n'.join(dict.fromkeys(summary_warnings)) if missing: final['answer'] += '\n\n尚未解决:' + ';'.join(missing) if isinstance(final.get('readingSummary'), dict): final['readingSummary']['notices'] = list(dict.fromkeys([ *final['readingSummary'].get('notices', []), *missing])) result = {'type': 'result', 'engine': 'NAO', 'status': 'COMPLETED' if complete else 'PARTIAL', 'answer': final['answer'], 'readingSummary': final.get('readingSummary'), 'reportTitle': final.get('reportTitle'), 'agentTask': task, 'protocol': identity, 'executionMode': 'native_agent', 'completionMethod': 'native_autonomous_analysis', 'stopReason': 'evidence_finalized' if complete else 'evidence_incomplete', 'finishReason': 'stop', 'nativeStopReason': native_reason, 'nativeStopSucceeded': stop_succeeded, 'nativeFinishReason': 'stop' if natural_finish else native_reason, 'conclusionProtocolQueryIds': cited_ids, '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': bool(documents and not missing), '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