From de486a9c6be2b12016f02a1da93fedd3f5ec3c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Tue, 17 Nov 2026 15:42:19 +0800 Subject: [PATCH] feat: classify verification checks with explicit evidence and coverage --- .../trial_task_center/verification/rules.py | 152 ++++++++++++++++++ .../trial_task_center/verification/runner.py | 8 +- .../test_independent_verification_rules.py | 86 ++++++++++ 3 files changed, 241 insertions(+), 5 deletions(-) create mode 100644 src/dx_productivity/trial_task_center/verification/rules.py create mode 100644 tests/v1_1/test_independent_verification_rules.py diff --git a/src/dx_productivity/trial_task_center/verification/rules.py b/src/dx_productivity/trial_task_center/verification/rules.py new file mode 100644 index 0000000..24a5744 --- /dev/null +++ b/src/dx_productivity/trial_task_center/verification/rules.py @@ -0,0 +1,152 @@ +"""Electrical comparison at fixed instance/refdes/physical-pin identities.""" +from collections import Counter, defaultdict +from .contracts import CHECKS, digest, rollup_results, validate_plan +from ..native_netlist import _is_pcb, _proven_symbol + + +def component_key(uid,row): + scope=row.get('instance_path',uid.rpartition('/')[0]) or 'root' + ref=row.get('refdes') or row.get('attributes',{}).get('Ref Designator') + return (scope,ref) if ref else None + + +def component_index(graph): + index={};ambiguous=[] + for uid,row in graph.get('components',{}).items(): + if not _is_pcb(row):continue + key=component_key(uid,row) + if key is None or key in index:ambiguous.append(key) + else:index[key]=(uid,row) + return index,ambiguous + + +def match_components(before_graph,after_graph): + left,la=component_index(before_graph);right,ra=component_index(after_graph) + return {'before':left,'after':right,'ambiguous':la+ra, + 'matches':{key:(left[key][0],right[key][0]) for key in left.keys()&right.keys()}} + + +def physical_view(graph): + """Keep unknowns explicit; networks include self to distinguish singles from open pins.""" + indexed,ambiguous=component_index(graph) + if ambiguous:raise ValueError('位号或实例范围对应不唯一') + endpoints={};pins={} + for uid,row in graph.get('components',{}).items(): + pcb=_is_pcb(row);key=component_key(uid,row) + for name,pin in row.get('pins',{}).items(): + number=pin.get('number') if pcb else name + if pcb and (key is None or not number):raise ValueError('缺少物理脚号') + endpoint=(*key,str(number)) if key else ('native',uid,name) + if endpoint in pins:raise ValueError('物理脚号重复,无法唯一绑定') + endpoints[(uid,name)]=endpoint + pins[endpoint]={'logical_name':name,'number':str(number),'nets':[], + 'uid':uid,'raw_key':['components',uid,'pins',name], + 'declared_nets':pin.get('nets')} + seen=defaultdict(list) + for net in graph.get('nets',()): + members=[] + for uid,name in net['endpoints']: + if (uid,name) not in endpoints:raise ValueError('网表端点未覆盖') + members.append(endpoints[(uid,name)]) + if len(set(members))!=len(members):raise ValueError('网络含重复物理端点') + for endpoint in members: + record={'name':net['name'],'endpoints':[list(e) for e in sorted(members)]} + pins[endpoint]['nets'].append(record);seen[endpoint].append(net['name']) + for endpoint,pin in pins.items(): + if pin['declared_nets'] is None or Counter(pin['declared_nets'])!=Counter(seen[endpoint]): + raise ValueError('引脚与网络互反覆盖不完整') + pin['nets'].sort(key=lambda n:(n['name'],n['endpoints'])) + return pins + + +def evaluate(before,after,*,expectations,check_plan,pin_metadata=None): + plan=validate_plan(check_plan) + requested=set(plan['required_checks'])|set(plan['optional_checks']) + states={key:'unknown' if key in requested else 'not_requested' for key in CHECKS} + issues=[] + rank={'not_requested':0,'equal':1,'expected_change':2,'unknown':3,'warning':4,'error':5} + def issue(check,state,reason,*,key=None,pin=None,left=None,right=None,field=None): + # Hard facts stay visible even if a user did not request optional enrichment. + if rank[state]>rank.get(states[check],0) or states[check]=='unknown':states[check]=state + row={'check_id':check,'state':state,'reason':reason,'before':left,'after':right} + if key:row.update(instance_path=key[0],refdes=key[1]) + if pin is not None:row['pin_number']=pin + if field:row['field']=field + row['issue_id']=digest(row)[:24];issues.append(row) + states['inputs']='equal' + complete=all(e.get('status')=='success' and e.get('complete') is True and e.get('graph',{}).get('components') for e in (before,after)) + left=before.get('graph',{});right=after.get('graph',{}) + states['coverage']='equal' if complete else 'unknown' + if not complete: + issue('coverage','unknown','缺少完整原生导出',left=before.get('reason'),right=after.get('reason')) + matched=match_components(left,right) + if complete and not matched['ambiguous']: + states['components']='equal' + for key in sorted(matched['before'].keys()^matched['after'].keys()): + issue('components','error','器件位号或实例范围增减',key=key, + left=key in matched['before'],right=key in matched['after']) + else:issue('components','unknown','器件身份或覆盖无法唯一证明') + views=[] + try: + if not complete:raise ValueError('导出不完整') + views=[physical_view(left),physical_view(right)] + except (ValueError,KeyError,TypeError) as error: + issue('coverage','unknown',str(error)) + if views: + states['physical_pins']=states['connections']=states['net_names']='equal' + states['pin_text']='equal' if 'pin_text' in requested else 'not_requested' + states['pin_semantics']='equal' if 'pin_semantics' in requested else 'not_requested' + lp,rp=views + for key in sorted(matched['matches']): + lset={e[2] for e in lp if e[:2]==key};rset={e[2] for e in rp if e[:2]==key} + if lset!=rset:issue('physical_pins','error','物理脚号集合变化',key=key,left=sorted(lset),right=sorted(rset)) + for endpoint in sorted(lp.keys()&rp.keys()): + l,r=lp[endpoint],rp[endpoint];key=endpoint[:2];pin=endpoint[2] + lt=sorted(n['endpoints'] for n in l['nets']);rt=sorted(n['endpoints'] for n in r['nets']) + if lt!=rt:issue('connections','error','固定物理脚号的连接变化',key=key,pin=pin,left=l,right=r) + elif [n['name'] for n in l['nets']]!=[n['name'] for n in r['nets']]: + issue('net_names','error','网络名称变化,需确认',key=key,pin=pin,left=l,right=r) + if l['logical_name']!=r['logical_name'] and 'pin_text' in requested: + issue('pin_text','warning','引脚名称变化,需人工确认功能',key=key,pin=pin,left=l,right=r) + if {l['logical_name'].upper(),r['logical_name'].upper()} & {'GND','VCC','VDD','VSS'}: + issue('pin_semantics','warning','疑似电源或地功能变化,高关注',key=key,pin=pin,left=l,right=r) + declared=expectations is not None + states['declared_attributes']='equal' if declared else 'not_requested' + states['attribute_changes']='equal' if 'attribute_changes' in requested else 'not_requested' + changes=(expectations or {}).get('changes',{}) + symbols=(expectations or {}).get('symbols',{}) + used=set() + for key,(lid,rid) in matched['matches'].items(): + l,r=left['components'][lid],right['components'][rid] + expected=dict(l.get('attributes',{}));delta=changes.get(lid,changes.get(key[1],{}));used.add(lid);used.add(key[1]) + for name,value in delta.items(): + if value is None:expected.pop(name,None) + else:expected[name]=value + actual=r.get('attributes',{}) + if declared: + if actual!=expected:issue('declared_attributes','error','器件属性与固定预期不符',key=key,left=expected,right=actual,field='attributes') + elif delta:states['declared_attributes']='expected_change' if states['declared_attributes']!='error' else 'error' + elif actual!=expected:issue('attribute_changes','warning','器件属性变化;未核对换料规则',key=key,left=expected,right=actual,field='attributes') + proof=symbols.get(lid) + proven=proof is not None and _proven_symbol(l,r,proof) + if proof is not None and not proven:issue('declared_attributes','error','目标符号证明无法绑定',key=key,left=l.get('symbol'),right=r.get('symbol')) + for field in ('symbol','other_attributes','ambiguous_attributes'): + if field=='symbol' and proven:continue + if l.get(field)!=r.get(field):issue('declared_attributes' if declared else 'attribute_changes', + 'error' if declared else 'warning','符号或其他属性变化',key=key,left=l.get(field),right=r.get(field),field=field) + for missing in set(changes)-used:issue('declared_attributes','error','预期清单目标未找到',left=missing) + # Proven metadata is enrichment; never substitute component Value for pin Value. + if pin_metadata: + for key in ('pin_value','electrical_type'): + for endpoint,fields in pin_metadata.get('comparisons',{}).items(): + l,r=fields.get('before',{}).get(key),fields.get('after',{}).get(key) + if l is not None and r is not None and l!=r: + issue('pin_text' if key=='pin_value' else 'pin_semantics','warning','引脚字段变化,需人工确认',left=l,right=r,field=key) + if 'export_identity' in requested: + diffs=[g.get('identity_tables',{}).get('keyin',{}).get('part_number_differences',[]) for g in (left,right)] + states['export_identity']='warning' if any(diffs) else 'equal' + if any(diffs):issue('export_identity','warning','原生身份表与有效料号存在差异',left=diffs[0],right=diffs[1]) + checks=[{'check_id':key,'state':states[key],'issues':[i['issue_id'] for i in issues if i['check_id']==key]} for key in sorted(CHECKS)] + return dict(rollup_results(checks,required_checks=plan['required_checks'],optional_checks=plan['optional_checks']), + checks=checks,issues=issues,scope='已核对换料规则' if declared else '未核对换料规则', + uncovered=['封装库内部引脚到焊盘映射','引脚级 Value(原生输出未证明)']) diff --git a/src/dx_productivity/trial_task_center/verification/runner.py b/src/dx_productivity/trial_task_center/verification/runner.py index caefcde..ae6324d 100644 --- a/src/dx_productivity/trial_task_center/verification/runner.py +++ b/src/dx_productivity/trial_task_center/verification/runner.py @@ -45,11 +45,9 @@ def run_attempt(service,attempt_id,*,stop_event): return {'published':False,'blocked':True} check_cancelled(stop_event) expected=service.artifacts.read(payload['expectation_ref'],'expectations') if payload['expectation_ref'] else None - comparison=native_netlist.compare_netlists(left,right,expected_changes=(expected or {}).get('changes')) - checks=[{'check_id':'inputs','state':'equal'}, - {'check_id':'coverage','state':'equal' if comparison['status']!='unavailable' else 'unknown'}] - result=dict(rollup_results(checks,required_checks=payload['check_plan']['required_checks']), - checks=checks,issues=[],before=left,after=right,attempt_state='succeeded',resource_state='released') + from .rules import evaluate + result=dict(evaluate(left,right,expectations=expected,check_plan=payload['check_plan']), + before=left,after=right,attempt_state='succeeded',resource_state='released') result.update(item_id=attempt['item_id'],input_version=attempt['input_version'],attempt_id=attempt_id, generation=attempt['generation'],input_digest=payload['payload_digest'],policy=payload['check_plan']) ref=service.artifacts.put('result',result) diff --git a/tests/v1_1/test_independent_verification_rules.py b/tests/v1_1/test_independent_verification_rules.py new file mode 100644 index 0000000..1a1c990 --- /dev/null +++ b/tests/v1_1/test_independent_verification_rules.py @@ -0,0 +1,86 @@ +import copy +import pytest +from dx_productivity.trial_task_center.verification.contracts import default_plan + + +def export(): + components={} + for uid,ref,pins in [('a','R12',{'loop':('1',['LOOP']),'GND':('2',['GND'])}), + ('b','U1',{'out':('3',['LOOP'])}),('c','C5',{'gnd':('2',['GND'])})]: + components[uid]={'refdes':ref,'attributes':{'Ref Designator':ref,'Part Number':'OLD','Value':'10K'}, + 'pins':{k:{'number':num,'nets':nets} for k,(num,nets) in pins.items()}} + return {'status':'success','complete':True,'graph':{'components':components,'nets':[ + {'name':'LOOP','endpoints':[['a','loop'],['b','out']]},{'name':'GND','endpoints':[['a','GND'],['c','gnd']]}]}} + + +def evaluate(left,right,**kwargs): + from dx_productivity.trial_task_center.verification.rules import evaluate + return evaluate(left,right,expectations=kwargs.pop('expectations',None),check_plan=kwargs.pop('check_plan',default_plan()),**kwargs) + + +def test_equal_graph_and_uid_change_preserve_refdes_identity(): + a=export();b=copy.deepcopy(a) + b['graph']['components']['new']=b['graph']['components'].pop('a') + for net in b['graph']['nets']: + for endpoint in net['endpoints']: + if endpoint[0]=='a':endpoint[0]='new' + assert evaluate(a,b)['verdict']=='CONSISTENT' + + +def test_swap_physical_pin_connections_is_hard_error(): + a=export();b=copy.deepcopy(a) + b['graph']['components']['a']['pins']['loop']['number']='2' + b['graph']['components']['a']['pins']['GND']['number']='1' + result=evaluate(a,b) + assert result['verdict']=='DIFFERENT' + assert any(i['check_id']=='connections' and i.get('pin_number')=='1' for i in result['issues']) + + +def test_pin_rename_is_warning_without_connection_error(): + a=export();b=copy.deepcopy(a) + b['graph']['components']['a']['pins']['outloop']=b['graph']['components']['a']['pins'].pop('loop') + b['graph']['nets'][0]['endpoints'][0][1]='outloop' + result=evaluate(a,b) + assert result['verdict']=='WARNING' + assert any(i['check_id']=='pin_text' for i in result['issues']) + assert not any(i['check_id']=='connections' for i in result['issues']) + + +@pytest.mark.parametrize('change',['disconnect','short','refdes','number','netname']) +def test_structural_changes_are_errors(change): + a=export();b=copy.deepcopy(a) + if change=='disconnect': + b['graph']['nets'][0]['endpoints'].pop(0);b['graph']['components']['a']['pins']['loop']['nets']=[] + if change=='short': + b['graph']['nets'][0]['endpoints']+=b['graph']['nets'][1]['endpoints'];b['graph']['nets'].pop() + for component in b['graph']['components'].values(): + for pin in component['pins'].values():pin['nets']=['LOOP'] + if change=='refdes':b['graph']['components']['a']['refdes']='R13';b['graph']['components']['a']['attributes']['Ref Designator']='R13' + if change=='number':b['graph']['components']['a']['pins']['loop']['number']='9' + if change=='netname': + b['graph']['nets'][0]['name']='RENAMED' + for uid,pin in [('a','loop'),('b','out')]:b['graph']['components'][uid]['pins'][pin]['nets']=['RENAMED'] + assert evaluate(a,b)['verdict']=='DIFFERENT' + + +def test_declared_values_exact_and_undeclared_changes_warn(): + a=export();b=copy.deepcopy(a);b['graph']['components']['a']['attributes']['Value']='NEW' + assert evaluate(a,b)['verdict']=='WARNING' + assert evaluate(a,b,expectations={'changes':{'a':{'Value':'NEW'}}})['verdict']=='CONSISTENT' + assert evaluate(a,b,expectations={'changes':{'a':{'Value':'WRONG'}}})['verdict']=='DIFFERENT' + assert evaluate(a,b,expectations={'changes':{}})['verdict']=='DIFFERENT' + + +def test_missing_required_mapping_never_passes_and_duplicate_ref_is_unknown(): + a=export();plan=default_plan();plan['excluded_checks']=[];plan['required_checks'].append('footprint_mapping') + assert evaluate(a,a,check_plan=plan)['verdict']=='INCOMPLETE' + b=copy.deepcopy(a);b['graph']['components']['c']['refdes']='R12' + assert evaluate(a,b)['verdict']=='INCOMPLETE' + + +def test_missing_pin_number_cannot_prove_equal_and_error_beats_unknown(): + a=export();b=copy.deepcopy(a);b['graph']['components']['a']['pins']['loop']['number']='' + assert evaluate(a,b)['verdict']=='INCOMPLETE' + b['graph']['components']['b']['attributes']['Value']='WRONG' + result=evaluate(a,b,expectations={'changes':{}}) + assert result['verdict']=='DIFFERENT' and result['unknown_checks']