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()
@@ -0,0 +1,46 @@
from threading import Event
from test_independent_verification_intake import service
from test_native_cli_netlist import project_fixture, graph
def imported(service,tmp_path):
a=tmp_path/'a';b=tmp_path/'b';a.mkdir();b.mkdir()
task=service.create_task('独立',request_id='c')['task_id']
item=service.import_pair(task,project_fixture(a),project_fixture(b),request_id='i',stop_event=Event())['accepted'][0]['item_id']
return task,item
def test_runner_uses_frozen_captures_and_publishes_attempt(service,tmp_path,monkeypatch):
from dx_productivity.trial_task_center.verification.runner import run_attempt
from dx_productivity.trial_task_center import netlist_verification
task,item=imported(service,tmp_path)
before=service.repo.revision(item,1)['payload']
captured=service.artifacts.read(before['before_ref'],'snapshot')
original=open(captured['database_path'],'rb').read()
calls=[]
def export(capture,directory,*,stop_event):
calls.append(capture['root'])
directory.mkdir(parents=True,exist_ok=True)
return {'status':'success','complete':True,'graph':graph(directory),'outputs':{}}
monkeypatch.setattr(netlist_verification,'_export_saved_capture',export)
attempt=service.repo.create_attempt(item,1,request_id='a')
result=run_attempt(service,attempt['attempt_id'],stop_event=Event())
assert result['published'] and len(calls)==2
assert service.repo.attempt(attempt['attempt_id'])['state']=='succeeded'
assert open(captured['database_path'],'rb').read()==original
assert service.repo.store._connection.execute('SELECT COUNT(*) FROM execution_operations').fetchone()[0]==0
assert not run_attempt(service,attempt['attempt_id'],stop_event=Event())['published']
def test_corrupt_input_does_not_export_and_failure_is_not_green(service,tmp_path,monkeypatch):
from dx_productivity.trial_task_center.verification.runner import run_attempt
from dx_productivity.trial_task_center import netlist_verification
_,item=imported(service,tmp_path)
capture=service.artifacts.read(service.repo.revision(item,1)['payload']['before_ref'],'snapshot')
open(capture['database_path'],'wb').write(b'corrupt')
def forbidden(*a,**k):raise AssertionError('must not export corrupt capture')
monkeypatch.setattr(netlist_verification,'_export_saved_capture',forbidden)
attempt=service.repo.create_attempt(item,1,request_id='a')
run_attempt(service,attempt['attempt_id'],stop_event=Event())
assert service.repo.attempt(attempt['attempt_id'])['state']=='failed'
assert service.read_item(item)['status']=='unavailable'