Files
quality_analyse/scripts/nao_plan_recovery.py
T
lyun ff7bbe3e46 同步:完善质量分析工作台并更新主题字体
同步已上线的智能分析执行协议、来源查询、图表报告与管理页面配套代码。
补齐数据库迁移、运行依赖和回归测试,保留原有指标计算口径。
统一午夜黑及其他主题的控件细节、中文和业务数字字体。
排除本地业务数据、凭据、预览、发布与验收产物。

验证:生产构建通过,320项前端回归通过;本轮仅同步Git,不再次部署。
2026-10-30 22:40:58 +08:00

157 lines
8.1 KiB
Python

"""Bounded plan repair. Never invent nodes, periods, filters, or business values."""
import copy
import hashlib
import json
import time
from contextvars import ContextVar
from functools import wraps
MAX_PLAN_ATTEMPTS = 6
PLANNING_SECONDS = 180
_planning_deadline = ContextVar('analysis_planning_deadline', default=None)
def planning_window(function):
@wraps(function)
def bounded(*args, **kwargs):
token = _planning_deadline.set(time.monotonic() + PLANNING_SECONDS)
try: return function(*args, **kwargs)
finally: _planning_deadline.reset(token)
return bounded
def planning_timeout(default):
deadline = _planning_deadline.get()
if deadline is None: return default
remaining = deadline - time.monotonic()
if remaining <= 0: raise TimeoutError('分析计划自动修复达到本轮时间预算。')
return min(default, 90, remaining)
def fingerprint(value, field, message):
raw = json.dumps([value, field, message], ensure_ascii=False, sort_keys=True, default=str)
return hashlib.sha256(raw.encode()).hexdigest()
def repair_optional_defaults(value):
"""Missing read flags default to no read; semantic review still catches omitted user demands."""
if not isinstance(value, dict): return value, []
candidate, repairs = copy.deepcopy(value), []
kind = value.get('kind')
defaults = {'verifySource': kind == 'source_verify', 'fullDetails': kind == 'details'}
if kind in ('analysis', 'metrics', 'members', 'source_verify', 'details'):
defaults['freshQuery'] = True
for field, default in defaults.items():
if candidate.get(field) is None:
candidate[field] = default
repairs.append({'field': field, 'before': None, 'after': default})
return candidate, repairs
def repair_summary(value, context):
"""Reconcile redundant root metadata only when all declared work remains in nodes.
The caller MUST run the strict normalizer and semantic reviewer afterwards.
A group requested at root but missing from nodes cannot be repaired by deleting it.
"""
if not isinstance(value, dict) or not isinstance(value.get('queryPlan'), list):
return None
nodes = value['queryPlan']
if not 1 <= len(nodes) <= 8 or any(not isinstance(node, dict) for node in nodes):
return None
fields, stages, dimensions, dimension_stages = set(), set(), set(), {}
protocol = (context or {}).get('_protocol')
rankings = protocol.get('availableRankings', []) if isinstance(protocol, dict) else []
if not isinstance(rankings, list): rankings = []
for node in nodes:
for key, target in (('fields', fields), ('stages', stages)):
items = node.get(key)
if not isinstance(items, list) or any(not isinstance(item, str) for item in items):
return None
target.update(items)
selector = node.get('selector')
if not isinstance(selector, dict): return None
if node.get('purpose') in ('ranking', 'changes'):
if not isinstance(node.get('dimension'), str): return None
dimensions.add(node.get('dimension'))
dimension_stages.setdefault(node.get('dimension'), set()).update(node['stages'])
if node.get('purpose') == 'defects':
affected = ['defect'] + (['product'] if node.get('dimension') == 'product' else [])
dimensions.update(affected)
for dimension in affected: dimension_stages.setdefault(dimension, set()).update(node['stages'])
if selector.get('kind') == 'catalog_members':
if not isinstance(selector.get('dimension'), str): return None
dimensions.add(selector['dimension'])
if selector.get('kind') == 'ranking_top': dimensions.add('product')
if selector.get('kind') == 'changes_top':
source = next((item for item in nodes if item.get('nodeId') == selector.get('sourceNodeId')), {})
if source.get('purpose') != 'changes': return None
dimensions.add(source.get('dimension'))
if selector.get('kind') == 'previous_ranking':
matches = [item for item in rankings if isinstance(item, dict) and item.get('queryId') == selector.get('queryId')]
if len(matches) != 1: return None
if not isinstance(matches[0].get('dimension'), str): return None
dimensions.add(matches[0]['dimension'])
allowed = {'fields': ['rate', 'scrapRate', 'totalWeight', 'scrapWeight'],
'stages': ['black', 'bright'], 'dimensions': ['product', 'department', 'group', 'defect']}
unions = {'fields': fields, 'stages': stages, 'dimensions': dimensions}
candidate, repairs = copy.deepcopy(value), []
for key, union in unions.items():
old = value.get(key, [])
if (not isinstance(old, list) or any(not isinstance(item, str) for item in old)
or not set(old) <= union or not union <= set(allowed[key])):
return None
desired = [item for item in allowed[key] if item in union]
if set(old) != union:
candidate[key] = desired
repairs.append({'field': key, 'before': old, 'after': desired})
for key, purpose in (('trendRequired', 'trend'), ('verifySource', 'source'), ('fullDetails', 'records')):
required = any(node.get('purpose') == purpose for node in nodes)
if bool(value.get(key)) != required:
# Read authorization and semantic demands are never auto-added or removed.
return None
if bool(value.get('comparisonRequired')) != any(node.get('comparisonRequired') is True for node in nodes):
return None
expected = {dim: [stage for stage in allowed['stages'] if stage in dimension_stages.get(dim, set())]
for dim in allowed['dimensions'] if dim in dimensions}
old = value.get('dimensionStages', {})
if old != expected:
candidate['dimensionStages'] = expected
repairs.append({'field': 'dimensionStages', 'before': old, 'after': expected})
return (candidate, repairs) if repairs else None
def repair_context(value, context):
catalog = (context or {}).get('_businessCatalog') or {}
if not isinstance(catalog, dict): catalog = {}
directory = {key: copy.deepcopy(catalog.get(key, [])) for key in ('departments', 'groups')}
result = {'authorizedCatalog': directory,
'rules': '保留用户目标、期间、范围及所有指标。root要求的维度若未在节点体现,应修复selector或节点,禁止通过删除root需求过关。'}
if isinstance(value, dict) and isinstance(value.get('dimensions'), list) and 'group' in value['dimensions']:
names = directory['groups']
filters = value.get('filterUpdates') if isinstance(value.get('filterUpdates'), dict) else {}
parent = filters.get('department') or ((context or {}).get('filters') or {}).get('department')
if parent:
relation = catalog.get('groupDepartments') or {}
names = [name for name in names if relation.get(name) == parent]
if names:
result['groupComparisonExample'] = {
'explanation': '使用一个changes节点批量比较完整授权分组,返回必要Top;不逐对象读取,不改成单期重量排名。',
'selector': {'kind': 'base'}, 'dimension':'group','sort':'rate_drop','limit':1,
'purpose': 'changes', 'comparisonRequired': True,
'dimensionStages': {'group':[]}, 'fields': ['rate'], 'stages': []}
return result
def clarification_request(task):
if not task.get('needsClarification'): return None
resolution = task.get('productResolution') or {}
match = resolution.get('productMatch') or {}
candidates = match.get('candidates') or []
options = [{'label': item['name'], 'value': item['name']} for item in candidates[:20]
if isinstance(item, dict) and isinstance(item.get('name'), str) and item['name'].strip()]
existing = task.get('clarificationRequest')
if not options and isinstance(existing, dict): options = existing.get('options', [])
return {'question': task['clarification'], 'options': options, 'allowCustom': True}