feat: execute independent verification on immutable inputs

This commit is contained in:
阿白叔 committed 2026-11-17 15:40:23 +08:00
1 parent b3c8c9c126
commit 4da211d5d2
2 files changed
+114

No files matched your search

@@ -0,0 +1,68 @@
"""Independent native read-only execution. No replacement or CAD mutation entrypoints."""
from pathlib import Path
from threading import Lock
from uuid import uuid4
from .. import native_netlist, netlist_verification
from ..batch_resources import pending_resources
from ..operation_control import check_cancelled
from .contracts import rollup_results
_native_lock=Lock()
def can_start_native_export(service):
with service.repo.store._lock:
held=service.repo.connection.execute("SELECT 1 FROM verification_attempts WHERE state='stopping' OR resource_state='unknown' LIMIT 1").fetchone()
if held or pending_resources(service.data_root):return {'allowed':False,'reason':'资源退出尚未确认'}
return {'allowed':not _native_lock.locked(),'reason':'原生导出忙' if _native_lock.locked() else ''}
def export_inputs(service,before_ref,after_ref,*,output,stop_event):
before=service.artifacts.read(before_ref,'snapshot')
after=service.artifacts.read(after_ref,'snapshot')
left=netlist_verification._export_saved_capture(before,Path(output)/'before',stop_event=stop_event)
if left.get('cleanup_uncertain'):return left,{}
check_cancelled(stop_event)
right=netlist_verification._export_saved_capture(after,Path(output)/'after',stop_event=stop_event)
return left,right
def run_attempt(service,attempt_id,*,stop_event):
if not can_start_native_export(service)['allowed'] or not _native_lock.acquire(blocking=False):
return {'published':False,'waiting':True}
attempt=None
try:
check_cancelled(stop_event)
attempt=service.repo.claim(attempt_id,owner_id=uuid4().hex)
if attempt is None:return {'published':False}
payload=service.repo.revision(attempt['item_id'],attempt['input_version'])['payload']
output=service.data_root/'verification'/'attempts'/attempt_id
left,right=export_inputs(service,payload['before_ref'],payload['after_ref'],output=output,stop_event=stop_event)
if left.get('cleanup_uncertain') or right.get('cleanup_uncertain'):
with service.repo.store.transaction():
service.repo.connection.execute("UPDATE verification_attempts SET state='stopping',resource_state='unknown',reason=? WHERE attempt_id=?",('原生资源退出未知',attempt_id))
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')
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)
result['result_ref']=ref
check_cancelled(stop_event)
published=service.repo.finish(attempt_id,generation=attempt['generation'],result=result)
return dict(result,published=published)
except Exception as error:
if attempt:
cancelled=getattr(error,'code','')=='operation_cancelled'
result={'attempt_state':'cancelled' if cancelled else 'failed','resource_state':'released',
'verdict':'INCOMPLETE','reason':str(error),'checks':[],'issues':[]}
service.repo.finish(attempt_id,generation=attempt['generation'],result=result)
return {'published':False,'reason':str(error)}
finally:
_native_lock.release()