"""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, []) # A combined ranking explicitly declares an empty output stage on both # the node and dimension. The two source stages sometimes leak into the # redundant root list; retain the actual declared combined work. combined_sources = (key == 'stages' and not union and set(old or []) == {'black','bright'} and all(node.get('purpose') == 'ranking' and node.get('stages') == [] and 'scrapWeight' in node.get('fields',[]) and set(node['fields']) <= {'rate','scrapWeight'} for node in nodes) and all(value.get('dimensionStages',{}).get(node.get('dimension')) == [] for node in nodes)) if (not isinstance(old, list) or any(not isinstance(item, str) for item in old) or not set(old) <= union and not combined_sources 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}