"""Render verified single-period source-defect weights without inventing a comparison.""" from nao_evidence_report import _filters, _scope_label, number, shown, period, cell from nao_agent_task import dimension_stages import copy def single_defect_task(task): return bool(task and 'defect' in task.get('dimensions', []) and task.get('period') and not task.get('comparisonPeriod') and not task.get('comparisonRequired')) def matching_single_defects(evidence, task, context): if not single_defect_task(task): return [] expected_release = (context or {}).get('releaseId') matches = {} for doc in evidence: r = doc.get('result', {}) coverage = r.get('coverage') or {} if (r.get('kind') != 'quality_fine_defects' or r.get('dimension') != 'defect' or r.get('period') != task['period'] or r.get('comparisonPeriod') is not None or expected_release is not None and r.get('releaseId') != expected_release or _filters(r.get('filters')) != _filters(task.get('filters')) or r.get('stage') not in dimension_stages(task, 'defect') or r.get('category') is not None or r.get('defect') is not None or coverage.get('available') is not True or coverage.get('hasValidWeights') is False or not isinstance(r.get('sourceRows'), int) or r['sourceRows'] <= 0 or r.get('rankingComplete') is not True or not isinstance(r.get('rows'), list)): continue rows = r['rows'] if any(not isinstance(row, dict) or not isinstance(row.get('key'), str) or number(row.get('weight')) is None or number(row['weight']) < 0 for row in rows): continue matches[r['stage']] = doc return list(matches.values()) def build_single_defect_report(evidence, task, context): docs = matching_single_defects(evidence, task, context) if not docs: return None facts, sections = [], [] covered = set() for doc in docs: r = doc['result']; stage = r['stage']; covered.add(stage) label = '黑片' if stage == 'black' else '亮片'; ref = doc['evidenceRef'] table = ['| 序号 | 具体缺陷 | 大类 | 来源重量(kg) |', '| --- | --- | --- | --- |'] for index, row in enumerate(r['rows'], 1): text = f'{label}具体缺陷{row.get("label") or row["key"]}:来源重量{shown(row["weight"])} kg;大类{row.get("category") or "未提供"}。' facts.append({'id': 'F'+str(len(facts)+1), 'kind':'defect', 'label':row.get('label') or row['key'], 'text':text, 'evidenceRef':ref, 'stage':stage}) if index <= 5: table.append('| ' + ' | '.join([str(index), cell(row.get('label') or row['key']), cell(row.get('category') or '未提供'), shown(row['weight'])]) + ' |') if not r['rows']: facts.append({'id':'F'+str(len(facts)+1),'kind':'defect','label':label, 'text':label+'当前范围没有可列出的有效具体缺陷排名。','evidenceRef':ref,'stage':stage}) sections.append('### '+period(task['period'])+' · '+label+'具体缺陷排名\n\n'+'\n'.join(table)+'\n\n['+ref+']') missing = [('黑片' if stage == 'black' else '亮片')+'单期具体缺陷排名尚未取得同期间、范围的有效来源依据。' for stage in dimension_stages(task, 'defect') if stage not in covered] return {'headline':period(task['period'])+';'+_scope_label(task.get('filters') or {})+'。', 'facts':facts,'sections':sections,'missingEvidence':missing,'coverageSufficient':not missing, 'policy':'具体缺陷按来源字段分别统计,重量单位为kg;来源细分不等同于正式报废贡献,不据此判定生产根因,未提供的细分不补零。', 'rawMetrics':[]} def _valid_defect_rows(rows, dimension, defect, source_fields, stage): if not isinstance(rows, list) or not rows or not isinstance(source_fields, list): return False allowed_categories = ({'责废', '料废', '外废'} if stage == 'black' else {'镀废', '材料废', '加工废', '扩散废'} if stage == 'bright' else set()) if not allowed_categories or len(source_fields) > 100: return False field_keys = set() field_categories = {} for item in source_fields: key = item.get('key') if isinstance(item, dict) else None if (not isinstance(item, dict) or not isinstance(item.get('key'), str) or not item['key'].strip() or not item['key'].endswith('重量') or item.get('sourceField', key) != key or item.get('category') not in allowed_categories or not isinstance(item.get('label', key), str) or not item.get('label', key).strip() or item['key'] in field_keys): return False field_keys.add(item['key']) field_categories[item['key']] = item['category'] if defect is not None and field_keys != {defect}: return False seen = set() for row in rows: if not isinstance(row, dict): return False key = row.get('key') weight = number(row.get('weight')) if not isinstance(key, str) or not key or key in seen or weight is None or weight < 0: return False seen.add(key) if dimension == 'defect' and key not in field_keys: return False if dimension == 'defect' and row.get('category') != field_categories.get(key): return False if dimension == 'product': if row.get('defect') != defect or row.get('category') != field_categories.get(defect): return False return True def _defect_categories_reconciled(source_fields, reconciliation, requested_category=None, source_rows=None): if not isinstance(source_fields, list) or not isinstance(reconciliation, list): return False selected_by_category = {} for field in source_fields: if (not isinstance(field, dict) or not isinstance(field.get('key'), str) or not isinstance(field.get('category'), str)): return False selected_by_category.setdefault(field['category'], set()).add(field['key']) expected = set(selected_by_category) if requested_category is not None: expected.add(requested_category) if not expected: return False actual = {} for item in reconciliation: if isinstance(item, dict) and isinstance(item.get('category'), str): if item['category'] in actual: return False actual[item['category']] = item for category in expected: item = actual.get(category) selected = selected_by_category.get(category, set()) if not isinstance(item, dict) or not selected or type(item.get('isReconciled')) is not bool: return False item_source_rows = item.get('sourceRows') if (type(item_source_rows) is not int or item_source_rows <= 0 or source_rows is not None and item_source_rows != source_rows): return False expected_fields = item.get('subitemFieldCount') coverage = item.get('coverage') components = item.get('components') if (type(expected_fields) is not int or expected_fields < len(selected) or not isinstance(coverage, dict) or type(coverage.get('total')) is not int or coverage['total'] != expected_fields or type(coverage.get('valid')) is not int or coverage['valid'] != len(selected) or not isinstance(components, list)): return False component_by_key = {} for component in components: if not isinstance(component, dict) or not isinstance(component.get('key'), str): return False if component['key'] in component_by_key: return False component_by_key[component['key']] = component # Java returns per-source-field row counts. Validate the requested # field independently of the broader category add-back result. if set(component_by_key) != selected: return False for component in component_by_key.values(): counts = [component.get(key) for key in ('sourceRows', 'presentRows', 'missingRows', 'invalidRows', 'negativeRows')] if (any(type(count) is not int or count < 0 for count in counts) or component['sourceRows'] != item_source_rows or component['presentRows'] != item_source_rows or component['missingRows'] != 0 or component['invalidRows'] != 0 or component['negativeRows'] != 0 or number(component.get('weight')) is None or number(component.get('weight')) < 0): return False selected_complete_category = len(selected) == expected_fields if not selected_complete_category: # A product/defect request may select one authorized raw field # from a larger formal category. Java explicitly marks this as # non-reconcilable; do not require a category add-back here. reasons = item.get('reconciliationReasons') if not isinstance(reasons, list) or not any( reason in ('当前请求仅选了部分源缺陷字段,不能与大类完整回加', '部分原始分项字段受授权限制,不能完整回加') for reason in reasons): return False if item.get('isReconciled') is True: return False continue # Compare the full category only when both sides have complete, # comparable coverage. A valid numerical DIFF is reportable source # evidence; it is not a missing-source condition or a formal KPI # contribution. Reject only contradictory reconciliation flags. formal_coverage = item.get('formalComponentCoverage') formal_component = item.get('formalComponent') formal_sum = number(item.get('formalComponentSum')) diff = number(item.get('diff')) formal_counts = ([formal_coverage.get(key) for key in ('sourceRows', 'presentRows', 'missingRows', 'invalidRows', 'negativeRows')] if isinstance(formal_coverage, dict) else []) formal_complete = (isinstance(formal_component, str) and bool(formal_component) and formal_sum is not None and diff is not None and len(formal_counts) == 5 and all(type(count) is int and count >= 0 for count in formal_counts) and formal_counts[0] == item_source_rows and formal_counts[1] == item_source_rows and formal_counts[2:] == [0, 0, 0]) if formal_complete: reconciled_by_value = abs(diff) <= 0.000001 if item.get('isReconciled') is not reconciled_by_value: return False if item.get('isReconciled') is False and item.get('reconciliationStatus') != 'DIFF': return False elif item.get('isReconciled') is True: return False return True def _defect_category_differences(source_fields, reconciliation): """Return full-category add-back differences for explicit source reporting.""" if not isinstance(source_fields, list) or not isinstance(reconciliation, list): return [] selected_by_category = {} for field in source_fields: if isinstance(field, dict) and isinstance(field.get('category'), str) and isinstance(field.get('key'), str): selected_by_category.setdefault(field['category'], set()).add(field['key']) differences = [] for item in reconciliation: if not isinstance(item, dict): continue category = item.get('category') expected = item.get('subitemFieldCount') selected = selected_by_category.get(category, set()) diff = number(item.get('diff')) source_rows = item.get('sourceRows') formal_coverage = item.get('formalComponentCoverage') formal_counts = ([formal_coverage.get(key) for key in ('sourceRows', 'presentRows', 'missingRows', 'invalidRows', 'negativeRows')] if isinstance(formal_coverage, dict) else []) formal_complete = (isinstance(item.get('formalComponent'), str) and bool(item.get('formalComponent')) and number(item.get('formalComponentSum')) is not None and type(source_rows) is int and source_rows > 0 and len(formal_counts) == 5 and all(type(value) is int for value in formal_counts) and formal_counts[0] == source_rows and formal_counts[1] == source_rows and formal_counts[2:] == [0, 0, 0]) if (type(expected) is int and expected > 0 and len(selected) == expected and formal_complete and item.get('reconciliationStatus') == 'DIFF' and diff is not None): differences.append({'category': category, 'diff': diff}) return differences def _defect_partial_categories(source_fields, reconciliation): if not isinstance(source_fields, list) or not isinstance(reconciliation, list): return [] selected_by_category = {} for field in source_fields: if isinstance(field, dict) and isinstance(field.get('category'), str) and isinstance(field.get('key'), str): selected_by_category.setdefault(field['category'], set()).add(field['key']) partial = [] for item in reconciliation: if not isinstance(item, dict): continue category = item.get('category') expected = item.get('subitemFieldCount') reasons = item.get('reconciliationReasons') if (type(expected) is int and expected > len(selected_by_category.get(category, set())) and isinstance(reasons, list) and any(reason in ('当前请求仅选了部分源缺陷字段,不能与大类完整回加', '部分原始分项字段受授权限制,不能完整回加') for reason in reasons)): partial.append(category) return partial def expand_defect_model_view(result, selected_defect=None): """Expand the lossless columnar form used in bounded evidence summaries.""" if not isinstance(result, dict): return None expanded = copy.deepcopy(result) for reconciliation_key in ('categoryReconciliation', 'comparisonCategoryReconciliation'): categories = expanded.get(reconciliation_key) if categories is None: continue if not isinstance(categories, list): return None for category in categories: if not isinstance(category, dict): return None has_columns = 'componentColumns' in category has_values = 'componentValues' in category if not has_columns and not has_values: continue if has_columns != has_values or 'components' in category: return None columns, values = category.get('componentColumns'), category.get('componentValues') if (not isinstance(columns, list) or not columns or any(not isinstance(column, str) or not column for column in columns) or len(set(columns)) != len(columns) or 'key' not in columns or not isinstance(values, list)): return None components = [] for row_values in values: if not isinstance(row_values, list) or len(row_values) != len(columns): return None component = dict(zip(columns, row_values)) if not isinstance(component.get('key'), str) or not component['key']: return None components.append(component) category.pop('componentColumns') category.pop('componentValues') category['components'] = components if isinstance(expanded.get('pairedRows'), list): return expanded columns, values = expanded.get('defectColumns'), expanded.get('defectValues') if not isinstance(columns, list) or not isinstance(values, list): return expanded if (len(set(columns)) != len(columns) or not {'field', 'category', 'currentWeight', 'comparisonWeight', 'deltaWeight', 'currentPresent', 'comparisonPresent', 'deltaAvailable', 'deltaUnavailableReason'} <= set(columns)): return None positions = {name: index for index, name in enumerate(columns)} coverage_keys = ('sourceRows', 'presentRows', 'missingRows', 'invalidRows', 'negativeRows') paired, current_rows, seen = [], [], set() for row_values in values: if not isinstance(row_values, list) or len(row_values) != len(columns): return None row = dict(zip(columns, row_values)) key = row.get('field') if not isinstance(key, str) or not key or key in seen: return None seen.add(key) current = None comparison = None for side_name, present_key, weight_key in ( ('current', 'currentPresent', 'currentWeight'), ('comparison', 'comparisonPresent', 'comparisonWeight')): present = row.get(present_key) if type(present) is not bool: return None if not present: continue side = {'key': key, 'label': key, 'category': row.get('category'), 'weight': row.get(weight_key)} if isinstance(selected_defect, str): side['defect'] = selected_defect for field in coverage_keys: stored = side_name + '_' + field if stored in row: side[field] = row[stored] if side_name == 'current': current = side current_rows.append(side) else: comparison = side delta_available = row.get('deltaAvailable') if type(delta_available) is not bool: return None paired.append({'key': key, 'label': key, 'category': row.get('category'), 'present': {'current': row['currentPresent'], 'comparison': row['comparisonPresent']}, 'current': current, 'comparison': comparison, 'deltaWeight': row.get('deltaWeight') if delta_available else None, 'deltaAvailable': delta_available, 'deltaUnavailableReason': row.get('deltaUnavailableReason')}) expanded['pairedRows'] = paired expanded.setdefault('rows', current_rows) if not isinstance(expanded.get('rows'), list): expanded['rows'] = current_rows # This row list exists only for structural evidence checks. Its count is # tied to the restored current-side rows in the paired bounded result. expanded['rowsReturned'] = len(expanded['rows']) return expanded def protocol_defect_report(evidence, task, release_id): """Build checked facts and a bounded table for one protocol defect scope.""" from nao_period import normalize_period query = task.get('defectQuery') if isinstance(task, dict) else None dimension = query.get('dimension') if isinstance(query, dict) else None defect = query.get('defect') if isinstance(query, dict) else None category = query.get('category') if isinstance(query, dict) else None limit = query.get('limit') if isinstance(query, dict) else None stages = task.get('stages') if isinstance(task, dict) else None if (dimension not in ('defect', 'product') or type(limit) is not int or not 1 <= limit <= 100 or dimension == 'product' and not isinstance(defect, str) or not isinstance(stages, list) or not stages): return {'facts': [], 'sections': [], 'missingEvidence': ['服务器签发的缺陷查询范围无效。'], 'coverageSufficient': False, 'policy': '', 'rawMetrics': []} try: expected_period = normalize_period(task.get('period')) expected_comparison = normalize_period(task.get('comparisonPeriod')) if task.get('comparisonRequired') else None except (TypeError, ValueError): expected_period = expected_comparison = None if expected_period is None or task.get('comparisonRequired') and expected_comparison is None: return {'facts': [], 'sections': [], 'missingEvidence': ['缺陷查询期间或比较期间无效。'], 'coverageSufficient': False, 'policy': '', 'rawMetrics': []} expected_filters = _filters(task.get('filters')) by_stage, missing = {}, [] for document in evidence if isinstance(evidence, list) else []: raw_result = document.get('result') if isinstance(document, dict) else None result = expand_defect_model_view(raw_result, defect) if isinstance(raw_result, dict) else None stage = result.get('stage') if isinstance(result, dict) else None if stage not in stages or stage in by_stage: continue if (document.get('mode') != 'defects' or result.get('kind') != 'quality_fine_defects' or result.get('releaseId') != release_id or result.get('dimension') != dimension or result.get('defect') != defect or result.get('category') != category or _filters(result.get('filters')) != expected_filters): continue try: if normalize_period(result.get('period')) != expected_period: continue if task.get('comparisonRequired'): if normalize_period(result.get('comparisonPeriod')) != expected_comparison: continue elif result.get('comparisonPeriod') is not None: continue except (TypeError, ValueError): continue coverage = result.get('coverage') rows = result.get('rows') total = result.get('totalRows') if (result.get('grain') != 'source_defect_weight' or not isinstance(coverage, dict) or coverage.get('available') is not True or coverage.get('hasValidWeights') is not True or type(result.get('sourceRows')) is not int or result['sourceRows'] <= 0 or type(total) is not int or total < 0 or not isinstance(rows, list) or result.get('rowsReturned') != len(rows) or not isinstance(result.get('truncated'), bool) or result.get('rankingComplete') is not True or not _valid_defect_rows(rows, dimension, defect, result.get('sourceFields'), stage) or not _defect_categories_reconciled(result.get('sourceFields'), result.get('categoryReconciliation'), category, result.get('sourceRows')) or any(not isinstance(row, dict) or not isinstance(row.get('category'), str) or row.get('category') not in {'责废', '料废', '外废', '镀废', '材料废', '加工废', '扩散废'} for row in rows)): continue if category is not None and not any( isinstance(item, dict) and item.get('category') == category for item in result.get('categoryReconciliation', [])): continue if task.get('comparisonRequired'): paired = result.get('pairedRows') compare_coverage = result.get('comparisonCoverage') if (not isinstance(compare_coverage, dict) or compare_coverage.get('available') is not True or compare_coverage.get('hasValidWeights') is not True or type(compare_coverage.get('sourceRows')) is not int or compare_coverage.get('sourceRows') <= 0 or result.get('pairingComplete') is not True or result.get('pairedRankingComplete') is not True or not isinstance(result.get('pairedTruncated'), bool) or not isinstance(paired, list) or not _defect_categories_reconciled(result.get('comparisonSourceFields'), result.get('comparisonCategoryReconciliation'), category, compare_coverage.get('sourceRows'))): continue seen, valid = set(), True comparison_fields = result.get('comparisonSourceFields') if not isinstance(comparison_fields, list): valid = False elif defect is not None and {item.get('key') for item in comparison_fields if isinstance(item, dict)} != {defect}: valid = False for row in paired: if not isinstance(row, dict) or not isinstance(row.get('key'), str) or row['key'] in seen: valid = False break seen.add(row['key']) present = row.get('present') if not isinstance(present, dict) or any(type(present.get(key)) is not bool for key in ('current', 'comparison')): valid = False break for side_name in ('current', 'comparison'): side = row.get(side_name) if present[side_name]: weight = number(side.get('weight')) if isinstance(side, dict) else None if (not isinstance(side, dict) or side.get('key') != row['key'] or weight is None or weight < 0 or not _valid_defect_rows([side], dimension, defect, comparison_fields if side_name == 'comparison' else result.get('sourceFields'), stage)): valid = False break elif side is not None: valid = False break if not valid: break delta = row.get('deltaWeight') if row.get('deltaAvailable') is True: if not present['current'] or not present['comparison'] or number(delta) is None: valid = False break elif delta is not None: valid = False break if not valid: continue selected_document = copy.copy(document) selected_document['result'] = result by_stage[stage] = selected_document facts, sections, required_fact_ids = [], [], [] for stage in stages: document = by_stage.get(stage) label = '黑片' if stage == 'black' else '亮片' if document is None: missing.append(label+'来源具体缺陷范围没有取得完整、同期间的正式细项证据。') continue result, ref = document['result'], document.get('evidenceRef') paired = task.get('comparisonRequired') is True shown_rows = (result.get('pairedRows') if paired else result.get('rows'))[:limit] total = result.get('pairedTotalRows') if paired else result.get('totalRows') top_text = f'返回Top {len(shown_rows)} 项 / 全范围 {total} 个分组' if result.get('truncated') or paired and result.get('pairedTruncated'): top_text += ';仅展示Top,不声称全名单或推断未返回分组的新进、退出' if paired: table = ['| 序号 | '+('具体缺陷' if dimension == 'defect' else '规格')+' | 对照期重量(kg) | 本期重量(kg) | 增减(kg) |', '| --- | --- | ---: | ---: | ---: |'] caption = period(expected_comparison)+'→'+period(expected_period) for index, row in enumerate(shown_rows, 1): now, before = row.get('current'), row.get('comparison') source = now if isinstance(now, dict) else before if isinstance(before, dict) else {} row_label = source.get('label') or row.get('key') now_value = shown(now.get('weight')) if isinstance(now, dict) else '未返回' before_value = shown(before.get('weight')) if isinstance(before, dict) else '未返回' delta = shown(row.get('deltaWeight'), signed=True) if row.get('deltaAvailable') is True else '不可比' table.append('| '+' | '.join((str(index), cell(row_label), before_value, now_value, delta))+' |') facts.append({'id':'F'+str(len(facts)+1),'kind':'defect','label':str(row_label), 'text':f'{label}来源细项{row_label}:{period(expected_comparison)}重量{before_value} kg,对照本期{period(expected_period)}重量{now_value} kg,增减{delta} kg。', 'evidenceRef':ref,'stage':stage}) if index <= 5: required_fact_ids.append(facts[-1]['id']) else: table = ['| 序号 | '+('具体缺陷' if dimension == 'defect' else '规格')+' | 来源重量(kg) | 大类 |', '| --- | --- | ---: | --- |'] caption = period(expected_period) for index, row in enumerate(shown_rows, 1): row_label, weight = row.get('label') or row.get('key'), shown(row.get('weight')) table.append('| '+' | '.join((str(index), cell(row_label), weight, cell(row.get('category') or '未提供')))+' |') facts.append({'id':'F'+str(len(facts)+1),'kind':'defect','label':str(row_label), 'text':f'{label}来源细项{row_label}:来源重量{weight} kg;大类{row.get("category") or "未提供"}。', 'evidenceRef':ref,'stage':stage}) if index <= 5: required_fact_ids.append(facts[-1]['id']) summary = {'id':'F'+str(len(facts)+1),'kind':'defect','label':label, 'text':f'{label}{"规格" if dimension == "product" else "具体缺陷"}来源重量Top:{top_text};期间{caption}。来源细项重量不等同于正式总体报废贡献。', 'evidenceRef':ref,'stage':stage} facts.append(summary) required_fact_ids.append(summary['id']) table.extend(['',top_text+'。来源细项重量按原始字段分别统计,不作为正式scrapWeight贡献。']) partial_categories = [] if paired: partial_categories.extend( ('对照期', name) for name in _defect_partial_categories( result.get('comparisonSourceFields'), result.get('comparisonCategoryReconciliation'))) partial_categories.extend( ('本期', name) for name in _defect_partial_categories( result.get('sourceFields'), result.get('categoryReconciliation'))) else: partial_categories.extend( ('本期', name) for name in _defect_partial_categories( result.get('sourceFields'), result.get('categoryReconciliation'))) for period_label, partial_category in partial_categories: partial_text = (f'{period_label}{partial_category}只查询了部分来源细项字段;' '不与正式大类整体回加,来源细项重量与正式KPI分开解释。') facts.append({'id':'F'+str(len(facts)+1),'kind':'defect', 'label':str(partial_category)+'部分来源字段口径', 'text':partial_text,'evidenceRef':ref,'stage':stage}) required_fact_ids.append(facts[-1]['id']) table.append(partial_text) category_differences = [] if paired: category_differences.extend( ('对照期', item) for item in _defect_category_differences( result.get('comparisonSourceFields'), result.get('comparisonCategoryReconciliation'))) category_differences.extend( ('本期', item) for item in _defect_category_differences( result.get('sourceFields'), result.get('categoryReconciliation'))) else: category_differences.extend( ('本期', item) for item in _defect_category_differences( result.get('sourceFields'), result.get('categoryReconciliation'))) for period_label, difference_info in category_differences: difference_text = ( f'{period_label}{difference_info["category"]}完整来源细项合计与正式大类口径相差 ' f'{shown(difference_info["diff"], signed=True)} kg(来源细项减正式大类);' '这是两种口径的差异说明,不代表正式KPI贡献或已回加一致。') facts.append({'id':'F'+str(len(facts)+1),'kind':'defect', 'label':str(difference_info['category'])+'来源与正式口径差异', 'text':difference_text,'evidenceRef':ref,'stage':stage}) required_fact_ids.append(facts[-1]['id']) table.append(difference_text) if ref: table.append('['+str(ref)+']') sections.append('### '+caption+' · '+label+'来源具体缺陷重量\n\n'+'\n'.join(table)) missing = list(dict.fromkeys(missing)) return {'headline':period(expected_period)+';'+_scope_label(expected_filters)+'。', 'facts':facts,'sections':sections,'missingEvidence':missing, 'coverageSufficient':bool(by_stage) and len(by_stage) == len(stages) and not missing, 'requiredFactIds':required_fact_ids, 'policy':'来源具体缺陷重量按原始字段分别聚合,不等同于正式报废重量或总体贡献;Top截断时不声称全名单,也不从未返回分组推断新增、退出。', 'rawMetrics':[]}