test: enforce reproducible verification closed-loop standards

This commit is contained in:
阿白叔 committed 2026-11-18 10:23:14 +08:00
1 parent a74b810786
commit e65bed0408
7 files changed
+556 -4

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
import copy
import json
from pathlib import Path
import pytest
from test_independent_verification_intake import service
from test_independent_verification_runner import imported
from test_native_cli_netlist import graph
from test_verification_control import wait_idle
def complete(service, tmp_path, monkeypatch):
from tools.verification_closed_loop import freeze_inputs
from dx_productivity.trial_task_center import netlist_verification
task, item = imported(service, tmp_path)
scope = freeze_inputs(service, task, expected_count=1)
def native(capture, output, *, stop_event):
output.mkdir(parents=True, exist_ok=True)
return {'status': 'success', 'complete': True, 'capture': capture, 'graph': graph(output),
'outputs': {'effective': str(output/'effective.txt'), 'keyin': str(output/'design.kyn')}}
monkeypatch.setattr(netlist_verification, '_export_saved_capture', native)
run = service.start(task, request_id='loop-start'); wait_idle(service)
report = service.export_task(task, tmp_path/'reports', request_id='loop-report')
return task, item, scope, run, report
def test_complete_scope_report_and_inputs_pass_same_attempt_checks(service, tmp_path, monkeypatch):
from tools.verification_closed_loop import verify_closed_loop
task, item, scope, run, report = complete(service, tmp_path, monkeypatch)
result = verify_closed_loop(service, task, scope, run, report, expected_verdicts={item: 'CONSISTENT'})
assert result['passed'] and all(c['state']=='passed' for c in result['checks'])
def test_empty_task_cannot_be_frozen_as_a_closed_loop(service):
from tools.verification_closed_loop import freeze_inputs
task = service.create_task('空', request_id='empty')['task_id']
with pytest.raises(ValueError): freeze_inputs(service, task, expected_count=0)
@pytest.mark.parametrize('fault', ['run_member', 'report_attempt', 'html_binding', 'source', 'expected_verdict', 'resource_proof', 'evidence_binding'])
def test_closed_loop_rejects_wrong_members_stale_reports_and_changed_sources(service, tmp_path, monkeypatch, fault):
from tools.verification_closed_loop import verify_closed_loop
task, item, scope, run, report = complete(service, tmp_path, monkeypatch)
expected = {item: 'CONSISTENT'}
if fault=='run_member':
run = copy.deepcopy(run); run['members'] = []
elif fault=='report_attempt':
path = report/'task.json'; value = json.loads(path.read_text(encoding='utf-8'))
value['items'][0]['attempt_id'] = 'old-attempt'
path.write_text(json.dumps(value), encoding='utf-8')
elif fault=='html_binding':
path = report/f'{item}-v1.html'; path.write_text('<html>CONSISTENT</html>', encoding='utf-8')
elif fault=='source':
capture = service.artifacts.read(scope['items'][0]['before_ref'], 'snapshot')
Path(capture['project']).write_text('changed', encoding='utf-8')
elif fault=='expected_verdict':
expected[item] = 'DIFFERENT'
elif fault=='resource_proof':
attempt = service.repo.latest_attempt(item, 1)
(service.data_root/'verification/attempts'/attempt['attempt_id']/'resource-released.json').write_text(
json.dumps({'owner_id': 'another-owner', 'resource_state': 'released'}), encoding='utf-8')
else:
path = report/'task.json'; value = json.loads(path.read_text(encoding='utf-8'))
value['items'][0]['attempt']['result']['evidence']['binding']['input_digest'] = 'f'*64
path.write_text(json.dumps(value), encoding='utf-8')
result = verify_closed_loop(service, task, scope, run, report, expected_verdicts=expected)
assert not result['passed'] and any(c['state']=='failed' for c in result['checks'])
def test_stopped_attempt_is_not_a_completed_closed_loop(service, tmp_path):
from tools.verification_closed_loop import freeze_inputs, verify_closed_loop
task, item = imported(service, tmp_path)
scope = freeze_inputs(service, task, expected_count=1)
attempt = service.repo.create_attempt(item, 1, request_id='cancelled')
service.repo.finish(attempt['attempt_id'], generation=attempt['generation'], result={
'attempt_state': 'cancelled', 'resource_state': 'released', 'verdict': 'INCOMPLETE', 'reason': 'stop'})
run = {'run_id': None, 'members': [{'item_id': item, 'input_version': 1, 'attempt_id': attempt['attempt_id']}]}
report = service.export_task(task, tmp_path/'reports', request_id='cancelled-report')
result = verify_closed_loop(service, task, scope, run, report, expected_verdicts={item: 'INCOMPLETE'})
assert not result['passed']
def test_selected_rerun_is_checked_against_its_scope_inside_a_whole_task_report(service, tmp_path, monkeypatch):
from tools.verification_closed_loop import freeze_inputs, verify_closed_loop
task, item, _, _, _ = complete(service, tmp_path, monkeypatch)
payload = {key: value for key, value in service.repo.revision(item, 1)['payload'].items()
if key not in ('payload_digest', 'input_state')}
payload.update(item_key='second', display_name='second')
service.publish_inputs(task, (payload,), submission_id='second')
scope = freeze_inputs(service, task, expected_count=2)
whole = service.start(task, selection=tuple((row['item_id'], 1) for row in scope['items']), request_id='whole-task'); wait_idle(service)
subset = dict(scope, items=[row for row in scope['items'] if row['item_id']==item])
selected_run = dict(whole, members=[row for row in whole['members'] if row['item_id']==item])
whole_report = service.export_task(task, tmp_path/'whole-report', request_id='whole-report')
assert verify_closed_loop(service, task, subset, selected_run, whole_report, expected_verdicts={item: 'CONSISTENT'}, selected_only=True)['passed']
run = service.rerun(item, 1, request_id='selected'); wait_idle(service)
scope['items'] = [row for row in scope['items'] if row['item_id']==item]
report = service.export_task(task, tmp_path/'selected-report', request_id='selected-report')
result = verify_closed_loop(service, task, scope, run, report, expected_verdicts={item: 'CONSISTENT'}, selected_only=True)
assert result['passed']
assert not verify_closed_loop(service, task, scope, run, report, expected_verdicts={item: 'CONSISTENT'})['passed']
def test_existing_replacement_history_is_allowed_but_new_replacement_is_rejected(service, tmp_path, monkeypatch):
from tools.verification_closed_loop import verify_closed_loop
def operation(key):
with service.repo.store.transaction():
service.repo.connection.execute('''INSERT INTO execution_operations
(task_id,round_id,operation_key,command_json,state) VALUES ('prior','round',?,'{}','verified')''', (key,))
operation('before-verification')
task, item, scope, run, report = complete(service, tmp_path, monkeypatch)
assert verify_closed_loop(service, task, scope, run, report, expected_verdicts={item: 'CONSISTENT'})['passed']
operation('during-verification')
assert not verify_closed_loop(service, task, scope, run, report, expected_verdicts={item: 'CONSISTENT'})['passed']