优化:未分类连续维护与柏拉图预汇总,同步已发布修复
This commit is contained in:
@@ -2,9 +2,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import hashlib
|
||||
import marshal
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
@@ -37,6 +42,37 @@ def _platform_visual_context(result):
|
||||
'missingEvidence': copy.deepcopy(result.get('missingEvidence', []))}
|
||||
|
||||
|
||||
def query_evidence_bindings(documents, proofs):
|
||||
"""Bind public evidence references to this execution's signed API receipts.
|
||||
|
||||
Normal evidence references are random and must not be reverse-engineered
|
||||
from query IDs. The backend independently checks these IDs and chart values.
|
||||
"""
|
||||
allowed = {proof.get('queryId') for proof in proofs if isinstance(proof, dict)}
|
||||
bindings = {}
|
||||
def query_ids(result):
|
||||
if not isinstance(result, dict):
|
||||
return set()
|
||||
raw = result.get('protocolEvidence')
|
||||
items = [raw] if isinstance(raw, dict) else raw if isinstance(raw, list) else []
|
||||
ids = {item['queryId'] for item in items
|
||||
if isinstance(item, dict) and isinstance(item.get('queryId'), str)}
|
||||
for side in ('current', 'comparison'):
|
||||
ids.update(query_ids(result.get(side)))
|
||||
return ids
|
||||
for document in documents:
|
||||
ref = document.get('evidenceRef')
|
||||
ids = query_ids(document.get('result'))
|
||||
if not isinstance(ref, str) or not re.fullmatch(r'Q[A-F0-9]{8}', ref) or not ids:
|
||||
continue
|
||||
if not ids <= allowed:
|
||||
raise ValueError('图表证据包含不属于本轮的查询证明')
|
||||
if ref in bindings and bindings[ref] != ids:
|
||||
raise ValueError('同一证据编号不能绑定不同正式查询')
|
||||
bindings[ref] = ids
|
||||
return [{'evidenceRef': ref, 'queryIds': sorted(ids)} for ref, ids in bindings.items()]
|
||||
|
||||
|
||||
def _confirmed_scalar_selection(request, task):
|
||||
"""Dispatch an already confirmed scalar choice without rewriting its task."""
|
||||
resolution = task.get('productResolution') or {}
|
||||
@@ -67,7 +103,8 @@ def _save_composition(final, actor_root, turn_id):
|
||||
from nao_quality_runtime import _atomic_json
|
||||
_atomic_json(actor_root / 'private' / 'answer-composition.json', {
|
||||
'turnId': turn_id, 'rawModelResponses': final['rawModelResponses'],
|
||||
'compositionDiagnostics': final.get('compositionDiagnostics', {})})
|
||||
'compositionDiagnostics': final.get('compositionDiagnostics', {}),
|
||||
'bindingInput': final.get('_privateBindingInput')})
|
||||
|
||||
|
||||
def _retryable_query_error(error):
|
||||
@@ -470,6 +507,131 @@ def _safe_private_error_summary(error, task):
|
||||
return message[:180] or '查询执行时发生内部错误。'
|
||||
|
||||
|
||||
def _private_module_provenance():
|
||||
"""Fingerprint the code objects actually loaded by this worker."""
|
||||
modules = {
|
||||
'nao_protocol_execution': ('_scoped_arguments', '_execute_query_plan'),
|
||||
'nao_quality_tools': ('QualityTools.quality_data',),
|
||||
'nao_weight_ranking': ('rank_response',),
|
||||
'nao_combined_ranking': ('_stage_has_explicit_zero_source', 'combined_response'),
|
||||
'nao_query_contract': ('bind_query',),
|
||||
'nao_agent_task': ('dimension_stages',),
|
||||
}
|
||||
result = {'python': sys.version.split()[0], 'executable': sys.executable, 'modules': {}}
|
||||
for name, symbols in modules.items():
|
||||
module = sys.modules.get(name)
|
||||
if module is None:
|
||||
result['modules'][name] = {'loaded': False}
|
||||
continue
|
||||
entry = {'loaded': True, 'file': getattr(module, '__file__', None), 'functions': {}}
|
||||
spec = getattr(module, '__spec__', None)
|
||||
if spec is not None:
|
||||
entry['origin'] = getattr(spec, 'origin', None)
|
||||
source_path = getattr(module, '__file__', None)
|
||||
if isinstance(source_path, str):
|
||||
try:
|
||||
source = Path(source_path)
|
||||
if source.is_file():
|
||||
entry['sourceSha256'] = hashlib.sha256(source.read_bytes()).hexdigest()
|
||||
entry['sourceMtimeNs'] = source.stat().st_mtime_ns
|
||||
except OSError:
|
||||
pass
|
||||
for dotted in symbols:
|
||||
value = module
|
||||
try:
|
||||
for part in dotted.split('.'):
|
||||
value = getattr(value, part)
|
||||
code = getattr(value, '__code__', None)
|
||||
if code is not None:
|
||||
entry['functions'][dotted] = hashlib.sha256(marshal.dumps(code)).hexdigest()
|
||||
except (AttributeError, TypeError, ValueError):
|
||||
continue
|
||||
result['modules'][name] = entry
|
||||
return result
|
||||
|
||||
|
||||
def _private_failure_diagnostic(error, diagnostic, task, arguments=None):
|
||||
"""Retain sanitized first-party validation detail only in private diagnostics."""
|
||||
from nao_quality_tools import QualityToolError
|
||||
|
||||
result = copy.deepcopy(diagnostic)
|
||||
if isinstance(error, QualityToolError):
|
||||
message = str(error)
|
||||
# Never persist SQL, transport addresses, or credential-bearing text,
|
||||
# even if a future caller wraps an upstream failure in QualityToolError.
|
||||
unsafe = re.compile(
|
||||
r'(?is)(?:\b(?:select|insert|update|delete|drop|alter|truncate|create)\b|\bsql\b|https?://|'
|
||||
r'\b(?:authorization|cookie|set-cookie)\s*[:=]|\bbearer\s+\S+)')
|
||||
result['errorSummary'] = (
|
||||
'受控查询校验失败;详细内容已隐藏。'
|
||||
if unsafe.search(message) else _safe_private_error_summary(error, task))
|
||||
result['moduleProvenance'] = _private_module_provenance()
|
||||
rank_trace = getattr(error, '_quality_private_rank_trace', None)
|
||||
if isinstance(rank_trace, dict):
|
||||
result['rankResponseTrace'] = copy.deepcopy(rank_trace)
|
||||
frames = []
|
||||
for frame in traceback.extract_tb(error.__traceback__)[-16:]:
|
||||
filename = Path(frame.filename).name
|
||||
function = frame.name
|
||||
if (re.fullmatch(r'[A-Za-z0-9_.-]{1,100}', filename)
|
||||
and re.fullmatch(r'[A-Za-z0-9_.<>-]{1,100}', function)
|
||||
and type(frame.lineno) is int and frame.lineno > 0):
|
||||
frames.append({'file': filename, 'function': function, 'line': frame.lineno})
|
||||
if frames:
|
||||
result['exceptionFrames'] = frames
|
||||
if isinstance(arguments, dict):
|
||||
allowed = {
|
||||
'mode': {'query', 'compare', 'rank', 'changes', 'investigate', 'defects'},
|
||||
'dimension': {'product', 'department', 'group', 'defect'},
|
||||
'metric': {'rate', 'scrapRate', 'totalWeight', 'scrapWeight'},
|
||||
'stage': {'black', 'bright'},
|
||||
}
|
||||
summary = {}
|
||||
for key, choices in allowed.items():
|
||||
value = arguments.get(key)
|
||||
if value in choices or key == 'stage' and value is None:
|
||||
summary[key] = value
|
||||
limit = arguments.get('limit')
|
||||
if type(limit) is int and 1 <= limit <= 100:
|
||||
summary['limit'] = limit
|
||||
if summary:
|
||||
result['queryInput'] = summary
|
||||
return result
|
||||
|
||||
|
||||
def _controlled_failure_diagnostic(error, *, node_id=None, purpose=None):
|
||||
"""Return bounded failure classification without copying upstream text or query values."""
|
||||
from nao_quality_tools import QualityToolError, SourceCoverageUnavailable
|
||||
from nao_quality_runtime import RuntimeErrorSafe
|
||||
|
||||
raw_status = getattr(error, 'http_status', getattr(error, 'code', None))
|
||||
status = raw_status if type(raw_status) is int and 100 <= raw_status <= 599 else None
|
||||
retryable = _retryable_query_error(error)
|
||||
if isinstance(error, SourceCoverageUnavailable):
|
||||
kind = 'source_coverage'
|
||||
elif status is not None:
|
||||
kind = 'transient_http' if retryable else 'http_error'
|
||||
elif isinstance(error, QualityToolError):
|
||||
kind = 'formal_query_rejected'
|
||||
elif isinstance(error, RuntimeErrorSafe) and retryable or isinstance(error, (TimeoutError, ConnectionError, OSError)):
|
||||
kind = 'transport_unavailable'
|
||||
elif isinstance(error, ValueError):
|
||||
kind = 'query_rejected'
|
||||
else:
|
||||
kind = 'internal_error'
|
||||
error_type = type(error).__name__
|
||||
if not re.fullmatch(r'[A-Za-z][A-Za-z0-9_]{0,63}', error_type):
|
||||
error_type = 'Exception'
|
||||
result = {'failureKind': kind, 'errorType': error_type, 'retryable': retryable}
|
||||
if status is not None:
|
||||
result['httpStatus'] = status
|
||||
if isinstance(node_id, str) and re.fullmatch(r'[A-Za-z0-9_-]{1,64}', node_id):
|
||||
result['nodeId'] = node_id
|
||||
if purpose in {'metrics', 'ranking', 'trend', 'changes', 'defects', 'source', 'records'}:
|
||||
result['purpose'] = purpose
|
||||
return result
|
||||
|
||||
|
||||
def _raw_period_label(value):
|
||||
from nao_evidence_report import period
|
||||
try:
|
||||
@@ -808,9 +970,10 @@ def _scoped_arguments(request, scope, node):
|
||||
elif node['purpose'] == 'ranking':
|
||||
dimension = node['dimension']
|
||||
for stage in dimension_stages(task, dimension, combined=False):
|
||||
if stage not in node['stages']:
|
||||
if stage not in node['stages'] and not (stage is None and not node['stages']):
|
||||
raise ValueError('排名scope请求阶段超出queryPlan节点')
|
||||
for field in node['fields']:
|
||||
if stage is None and field == 'rate':continue
|
||||
item = {'mode': 'rank', 'releaseId': request['releaseId'],
|
||||
'period': copy.deepcopy(task['period']), 'filters': copy.deepcopy(filters),
|
||||
'dimension': dimension, 'stage': stage, 'metric': field,
|
||||
@@ -884,6 +1047,8 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
server_gaps = []
|
||||
calls, proofs, accepted_refs = [], [], set()
|
||||
query_diagnostics = []
|
||||
failure_diagnostics = []
|
||||
private_failure_diagnostics = []
|
||||
raw_diagnostics = []
|
||||
query_recovery = []
|
||||
query_ids = set()
|
||||
@@ -929,6 +1094,7 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
if cancel_event.is_set():raise RuntimeError('cancelled')
|
||||
node = plan_nodes[scope['nodeId']]
|
||||
arguments_queue = []
|
||||
active_arguments = None
|
||||
scoped_api = None
|
||||
active_raw_diagnostic = None
|
||||
try:
|
||||
@@ -1073,6 +1239,7 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
'toolCalls':copy.deepcopy(calls),'toolCount':len(calls)})
|
||||
continue
|
||||
for index, arguments in enumerate(arguments_queue):
|
||||
active_arguments = arguments
|
||||
if cancel_event.is_set():raise RuntimeError('cancelled')
|
||||
started = datetime.now(timezone.utc).isoformat()
|
||||
reads_before = scoped_api.read_count
|
||||
@@ -1118,9 +1285,13 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
emit({'type': 'progress', 'message': f'正式查询 {read_count}/40 已完成。',
|
||||
'toolCalls': copy.deepcopy(calls), 'toolCount': len(calls)})
|
||||
except Exception as error:
|
||||
if isinstance(error, RuntimeError) and (str(error) == 'cancelled'
|
||||
or not _retryable_query_error(error)):
|
||||
if isinstance(error, RuntimeError) and str(error) == 'cancelled':
|
||||
raise
|
||||
diagnostic = _controlled_failure_diagnostic(
|
||||
error, node_id=scope.get('nodeId'), purpose=node.get('purpose'))
|
||||
failure_diagnostics.append(diagnostic)
|
||||
private_failure_diagnostics.append(
|
||||
_private_failure_diagnostic(error, diagnostic, scope.get('task'), active_arguments))
|
||||
recovered = None
|
||||
if (scoped_api is not None and node['purpose'] == 'metrics' and arguments_queue
|
||||
and len(arguments_queue) == 1 and arguments_queue[0].get('mode') == 'compare'
|
||||
@@ -1193,7 +1364,18 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
continue
|
||||
# A child failure remains visible and can never be hidden by a
|
||||
# successful sibling scope.
|
||||
server_gaps.append(f"节点{node['nodeId']}范围{scope.get('label') or scope['scopeId']}未完成({type(error).__name__})。")
|
||||
safe_failure = {
|
||||
'source_coverage': '所选期间和筛选没有可用的正式来源覆盖',
|
||||
'formal_query_rejected': '正式查询响应未通过范围或完整性核验',
|
||||
'transient_http': '正式查询服务暂时不可用,可在稍后重试',
|
||||
'http_error': '正式查询服务拒绝或无法处理本次请求',
|
||||
'transport_unavailable': '正式查询连接暂时不可用,可在稍后重试',
|
||||
'query_rejected': '正式查询未通过已确认的范围或参数校验',
|
||||
'internal_error': '正式查询执行未完成',
|
||||
}.get(diagnostic['failureKind'], '正式查询执行未完成')
|
||||
label = {'metrics':'正式指标','ranking':'正式排名','trend':'正式趋势','changes':'正式变化',
|
||||
'defects':'正式缺陷','source':'原表核验','records':'原始记录'}.get(node.get('purpose'),'正式查询')
|
||||
server_gaps.append(f"节点{node['nodeId']}:{label}{safe_failure}。")
|
||||
if cancel_event.is_set():raise RuntimeError('cancelled')
|
||||
scopes, pending, gaps, skipped = _protocol_query_snapshot(delegated, include_skipped=True)
|
||||
|
||||
@@ -1371,21 +1553,26 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
'period': copy.deepcopy(task['period'])}
|
||||
if task.get('comparisonPeriod') is not None:
|
||||
agent_context['comparisonPeriod'] = copy.deepcopy(task['comparisonPeriod'])
|
||||
if any(node.get('purpose')=='changes' for node in task.get('queryPlan',[])):
|
||||
if not final.get('readingSummary') and any(node.get('purpose')=='changes' for node in task.get('queryPlan',[])):
|
||||
# The model sees a concise fact set; queried statistical tables must
|
||||
# still survive in the persisted answer. The existing UI folds these
|
||||
# sections into its full appendix without further business reads.
|
||||
# still survive as normal key tables, without exposing audit appendices
|
||||
# or performing further business reads.
|
||||
sections=[section for section in scoped_report.get('sections',[])
|
||||
if isinstance(section,str) and section.strip()]
|
||||
if isinstance(section,str) and section.strip() and section.strip() not in final['answer']]
|
||||
if sections:
|
||||
final['answer'] += '\n\n## 统计数据附录\n\n'+'\n\n'.join(sections)
|
||||
final['answer'] += '\n\n## 重点数据\n\n'+'\n\n'.join(sections)
|
||||
result = {'type': 'result', 'engine': 'NAO', 'status': status, 'answer': final['answer'],
|
||||
'readingSummary': final.get('readingSummary'),
|
||||
'answerQuality': {'version': 1, 'dataComplete': bool(covered),
|
||||
'narrativeComplete': bool(final.get('evidenceSufficient')),
|
||||
'chartsComplete': not missing_visuals,
|
||||
'missingEvidence': missing_evidence},
|
||||
'stopReason': 'evidence_finalized' if complete else 'evidence_incomplete',
|
||||
'finishReason': 'stop', 'completionMethod': 'bounded_scoped_query_plan',
|
||||
'agentTask': task, 'agentContext': agent_context, 'protocol': identity,
|
||||
'actualModel': model, 'phaseTimingsMs': timings,
|
||||
'protocolEvidence': proofs, 'queryEvidenceRefs': [d['evidenceRef'] for d in evidence],
|
||||
'queryEvidenceBindings': query_evidence_bindings(evidence, proofs),
|
||||
'toolCalls': calls, 'toolCount': len(calls), 'reportTitle': final.get('reportTitle'),
|
||||
'evidenceCompletion': {'serverCoverageSufficient': covered,
|
||||
'modelDeclaredEvidenceSufficient': final.get('modelDeclaredEvidenceSufficient', final.get('evidenceSufficient')),
|
||||
@@ -1394,6 +1581,7 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
'visualSchemaVersion': 1, 'visualState': visuals.get('visualState', 'unavailable'),
|
||||
'visuals': visuals.get('visuals', []),
|
||||
'scopeDiagnostics': copy.deepcopy(raw_diagnostics),
|
||||
'failureDiagnostics': copy.deepcopy(failure_diagnostics),
|
||||
'queryRecovery': copy.deepcopy(query_recovery),
|
||||
'scopedReport': {'facts': scoped_report.get('facts', []),
|
||||
'sections': scoped_report.get('sections', []),
|
||||
@@ -1404,6 +1592,7 @@ def _execute_query_plan(*, request, task, authority, actor_root, delegated, conn
|
||||
'phaseTimingsMs': timings, 'queryIds': [proof['queryId'] for proof in proofs],
|
||||
'protocolEvidence': proofs,
|
||||
'queryDiagnostics': query_diagnostics,
|
||||
'failureDiagnostics': private_failure_diagnostics,
|
||||
'queryRecovery': query_recovery,
|
||||
'rawScopeDiagnostics': raw_diagnostics,
|
||||
'completion': {'serverCoverageSufficient': covered,
|
||||
@@ -1720,6 +1909,7 @@ def execute(*, request, task, authority, actor_root, delegated, connection, canc
|
||||
'agentTask': task, 'agentContext': agent_context, 'protocol': identity,
|
||||
'actualModel': model, 'phaseTimingsMs': timings,
|
||||
'protocolEvidence': proofs, 'queryEvidenceRefs': [d['evidenceRef'] for d in evidence],
|
||||
'queryEvidenceBindings': query_evidence_bindings(evidence, proofs),
|
||||
'toolCalls': calls, 'toolCount': len(calls), 'reportTitle': final.get('reportTitle'),
|
||||
'evidenceCompletion': completion, 'factsComplete': facts_complete, **visuals}
|
||||
from nao_quality_runtime import _atomic_json
|
||||
|
||||
Reference in New Issue
Block a user