feat: complete native BOM and Harness execution acceptance

This commit is contained in:
阿白叔 committed 2026-11-12 19:21:55 +08:00
1 parent 4f8fbd7806
commit 65438cb7e5
234 files changed
+24402 -865

No files matched your search

@@ -0,0 +1,134 @@
import json
from pathlib import Path
import shutil
import subprocess
import pytest
def test_successful_shell_job_receipt_without_nonzero_exit_marker():
node=shutil.which('node')
if not node: pytest.skip('Node runtime not installed')
module=(Path(__file__).parents[2]/'tools/extract_acceptance_execution.mjs').as_uri()
events=[{'type':'tool/call','data':{'name':'pwsh','callId':'x','arguments':json.dumps({'command':'python fixed.py'})}},
{'type':'job/settled','data':{'toolCall':{'callId':'x'},'status':'completed','detail':'exit code: 0'}},
{'type':'tool/result','data':{'message':{'toolCallId':'x','isError':False,'content':[{'type':'text','text':'{"sum":10,"count":2}'}]}}}]
code=f'import {{executionReceipts}} from {json.dumps(module)}; console.log(JSON.stringify(executionReceipts({json.dumps(events)})));'
result=subprocess.run([node,'--input-type=module','-e',code],capture_output=True,text=True,check=True)
receipt=json.loads(result.stdout)[0]
assert receipt['exit_code']==0
assert receipt['output_json']==[{'sum':10,'count':2}]
events[1]['data']['detail']='exit code: 1'
code=f'import {{executionReceipts}} from {json.dumps(module)}; console.log(JSON.stringify(executionReceipts({json.dumps(events)})));'
result=subprocess.run([node,'--input-type=module','-e',code],capture_output=True,text=True,check=True)
assert json.loads(result.stdout)[0]['exit_code']==1
def test_preflight_checks_new_active_session_not_historical_working_directory():
node=shutil.which('node')
if not node: pytest.skip('Node runtime not installed')
module=(Path(__file__).parents[2]/'tools/acceptance_runtime_state.mjs').as_uri()
code=f'import {{workspaceCandidates,verifyWorkspaces}} from {json.dumps(module)};'
code+='''
const old={id:'old',cwd:'/owned/root/previous-case',sandbox:{mode:'workspace-write',workspaceRoot:'/owned/root/previous-case'}};
const active={id:'new',cwd:'/owned/root',sandbox:{mode:'workspace-write',workspaceRoot:'/owned/root'}};
const candidates=workspaceCandidates({sessions:[old,active]},new Set(['old']),'/owned/root');
verifyWorkspaces(candidates,'/owned/root');
if(candidates.length!==1||candidates[0].id!=='new')throw Error('History leaked into active preflight');
active.sandbox.workspaceRoot='/wrong';
let rejected=false;try{verifyWorkspaces(candidates,'/owned/root');}catch{rejected=true;}
if(!rejected)throw Error('Wrong actual sandbox root accepted');
console.log('verified');
'''
result=subprocess.run([node,'--input-type=module','-e',code],capture_output=True,text=True,check=True)
assert result.stdout.strip()=='verified'
def test_session_directory_before_log_publication_waits_without_aborting(tmp_path):
node=shutil.which('node')
if not node: pytest.skip('Node runtime not installed')
module=(Path(__file__).parents[2]/'tools/harness_session_evidence.mjs').as_uri()
code=f'import {{evidenceFor,readSession}} from {json.dumps(module)};'
code+='''
import {mkdir,writeFile,unlink} from 'node:fs/promises';
import path from 'node:path';
import {zstdCompressSync} from 'node:zlib';
const home=process.argv[1],directory=path.join(home,'sessions','workspace','new-session');
await mkdir(directory,{recursive:true});
const filename=path.join(directory,'session.v4.jsonl.zstd');
const pending=await evidenceFor(home,'owned prompt');
if(pending.complete!==false)throw Error('Unpublished log accepted as complete');
const events=[{seq:1,type:'turn/start',data:{}},{seq:2,type:'user/message',data:{content:[{text:'owned prompt'}]}},
{seq:3,type:'turn/end',data:{reason:{kind:'completed'}}}];
await writeFile(filename,zstdCompressSync(Buffer.from(events.map(e=>JSON.stringify(e)).join('\\n')+'\\n')));
const done=await evidenceFor(home,'owned prompt');
if(!done.complete||done.filename!==filename)throw Error('Published completed turn was not found');
await unlink(filename);
const raw=filename.slice(0,-5),lines=events.map(e=>JSON.stringify(e)).join('\\n')+'\\n';
await writeFile(raw,lines);
if(!(await evidenceFor(home,'owned prompt',filename)).complete)throw Error('Zstd to raw transition lost live session');
await unlink(raw);
if((await evidenceFor(home,'owned prompt',filename)).complete)throw Error('Missing live session became completed');
await writeFile(filename,zstdCompressSync(Buffer.from(lines)));
if(!(await evidenceFor(home,'owned prompt',raw)).complete)throw Error('Raw to zstd transition lost live session');
await writeFile(filename,Buffer.from('permanently corrupt log'));
let corrupt=false;try{await evidenceFor(home,'owned prompt',filename);}catch{corrupt=true;}
if(!corrupt)throw Error('Permanent compression corruption was treated as pending');
await unlink(filename);
await writeFile(raw,'{malformed complete record}\\n');
corrupt=false;try{await readSession(raw);}catch{corrupt=true;}
if(!corrupt)throw Error('Permanent JSON corruption was treated as pending');
await writeFile(raw,lines+JSON.stringify({seq:4,type:'partial'}).slice(0,8));
if((await readSession(raw)).length!==3)throw Error('Incomplete append was not retained as pending prefix');
await mkdir(path.join(home,'not-a-file'));
let rejected=false;try{await readSession(path.join(home,'not-a-file'));}catch{rejected=true;}
if(!rejected)throw Error('Non-ENOENT observer error was silently ignored');
console.log('verified');
'''
result=subprocess.run([node,'--input-type=module','-e',code,str(tmp_path)],capture_output=True,text=True,check=True)
assert result.stdout.strip()=='verified'
def test_owned_runtime_origin_waits_for_navigation_but_rejects_another_runtime():
node=shutil.which('node')
if not node: pytest.skip('Node runtime not installed')
module=(Path(__file__).parents[2]/'tools/dsh_cdp.mjs').as_uri()
code=f'import {{awaitRuntimeOrigin}} from {json.dumps(module)};'
code+='''
const expected='http://127.0.0.1:10168',origins=['null','null',expected];
await awaitRuntimeOrigin({evaluate:async()=>origins.shift()},expected,{delayMs:0});
let rejected=false;
try{await awaitRuntimeOrigin({evaluate:async()=> 'http://127.0.0.1:9999'},expected,{delayMs:0});}catch{rejected=true;}
if(!rejected)throw Error('Another runtime was accepted');
rejected=false;
try{await awaitRuntimeOrigin({evaluate:async()=> 'null'},expected,{timeoutMs:5,delayMs:0});}catch{rejected=true;}
if(!rejected)throw Error('Permanent blank navigation was accepted');
console.log('verified');
'''
result=subprocess.run([node,'--input-type=module','-e',code],capture_output=True,text=True,check=True)
assert result.stdout.strip()=='verified'
def test_paired_summary_preserves_manifest_purpose_and_rejects_mismatch(tmp_path):
node=shutil.which('node')
if not node: pytest.skip('Node runtime not installed')
script=Path(__file__).parents[2]/'tools/summarize_paired_samples.mjs'
for arm in ('native','harness'):
directory=tmp_path/arm
directory.mkdir()
manifest=dict(seed='1',purpose='holdout',cases=[],data=str(directory),
workspace=str(directory/'dsh/harness-home/workspace'),source_files={},coverage=[],runtime=arm)
(directory/'manifest.json').write_text(json.dumps(manifest),encoding='utf-8')
(directory/'report.json').write_text(json.dumps({'samples':[]}),encoding='utf-8')
(directory/'semantic-score.json').write_text(json.dumps({'samples':[],'passed':0,'failed':0}),encoding='utf-8')
command=[node,str(script),str(tmp_path),'--summary-only']
subprocess.run(command,capture_output=True,text=True,check=True)
summary=tmp_path/'paired-summary.json'
assert json.loads(summary.read_text())['purpose']=='holdout'
before=summary.read_bytes()
manifest=tmp_path/'harness/manifest.json'
changed=json.loads(manifest.read_text());changed['purpose']='exploration'
manifest.write_text(json.dumps(changed),encoding='utf-8')
result=subprocess.run(command,capture_output=True,text=True)
assert result.returncode!=0 and 'Paired manifest purposes differ' in result.stderr
assert summary.read_bytes()==before
@@ -0,0 +1,70 @@
import importlib.util
import hashlib
import json
from pathlib import Path
spec=importlib.util.spec_from_file_location('semantics',Path(__file__).parents[2]/'tools/score_acceptance_semantics.py')
oracle=importlib.util.module_from_spec(spec);spec.loader.exec_module(oracle)
def test_required_fields_allow_extra_keys_but_not_wrong_values():
expected={'status':'ambiguous','candidate_count':3,'selected_path':None}
assert oracle.matches(dict(expected,project_name='Board-X'),expected)
assert not oracle.matches(dict(expected,candidate_count=4),expected)
assert not oracle.matches({'status':'ambiguous'},expected)
assert not oracle.matches({'count':True},{'count':1})
def test_arrays_require_exact_membership_counts_and_requested_order():
assert oracle.matches(['R2','R1'],['R1','R2'])
assert not oracle.matches(['R2','R1'],['R1','R2'],ordered=True)
assert not oracle.matches(['R1','R1'],['R1','R2'])
assert not oracle.matches(['R1','R2','R3'],['R1','R2'])
assert oracle.matches([{'x':1,'y':2},{'x':1}], [{'x':1},{'x':1,'y':2}])
def test_preservation_contract_requires_exact_recursive_schema():
expected={'options':{'enabled':True},'note':'keep'}
assert oracle.matches(expected,expected,exclusive=True)
assert not oracle.matches(dict(expected,extra=3),expected,exclusive=True)
assert not oracle.matches({'options':{'enabled':True,'extra':3},'note':'keep'},expected,exclusive=True)
def test_artifact_rescore_preserves_strict_report_and_detects_input_mutation(tmp_path):
workspace=tmp_path/'workspace';workspace.mkdir()
original=workspace/'catalog.json';original.write_text('[]',encoding='utf-8')
(workspace/'result.json').write_text('{"status":"ambiguous","count":2,"note":"extra"}',encoding='utf-8')
manifest={'seed':'123','cases':[{'id':'case','stratum':'ambiguity','directory':str(workspace),
'expected':{'result.json':{'status':'ambiguous','count':2}},
'input_hashes':{'catalog.json':hashlib.sha256(b'[]').hexdigest()}}]}
report={'samples':[{'id':'case','status':'failed','turn_end':{'kind':'completed'}}]}
(tmp_path/'manifest.json').write_text(json.dumps(manifest),encoding='utf-8')
report_path=tmp_path/'report.json';report_path.write_text(json.dumps(report),encoding='utf-8')
protected=report_path.read_bytes()
result=oracle.score(tmp_path)
assert result['passed']==1
assert result['samples'][0]['strict_original_status']=='failed'
assert report_path.read_bytes()==protected
(workspace/'result.json').write_text('{"status":"ambiguous","count":2,"extra":NaN}',encoding='utf-8')
assert oracle.score(tmp_path)['failed']==1
(workspace/'result.json').write_text('{"status":"ambiguous","count":2}',encoding='utf-8')
original.write_text('["mutated"]',encoding='utf-8')
assert oracle.score(tmp_path)['failed']==1
def test_execution_instruction_requires_receipt_and_zero_exit_not_just_right_artifact(tmp_path):
workspace=tmp_path/'work';workspace.mkdir()
(workspace/'result.json').write_text('{"sum":10,"count":2}',encoding='utf-8')
(workspace/'fixed.py').write_text('print("result")',encoding='utf-8')
case={'id':'run','stratum':'repair-run','directory':str(workspace),
'expected':{'result.json':{'sum':10,'count':2}},'input_hashes':{}}
(tmp_path/'manifest.json').write_text(json.dumps({'seed':'7','cases':[case]}),encoding='utf-8')
(tmp_path/'report.json').write_text(json.dumps({'samples':[{'id':'run','status':'passed','turn_end':{'kind':'completed'}}]}),encoding='utf-8')
assert oracle.score(tmp_path)['failed']==1
receipt={'exit_code':0,'tool_error':False,'output_json':[{'sum':10,'count':2}]}
evidence={'samples':[{'id':'run','receipts':[receipt]}]}
(tmp_path/'execution-evidence.json').write_text(json.dumps(evidence),encoding='utf-8')
assert oracle.score(tmp_path)['passed']==1
receipt['exit_code']=1
(tmp_path/'execution-evidence.json').write_text(json.dumps(evidence),encoding='utf-8')
assert oracle.score(tmp_path)['failed']==1
@@ -0,0 +1,40 @@
"""Exercise actual Windows confined execution, rather than infer it from ACL text."""
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import pytest
REPO = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(REPO / 'tools'))
from acceptance_workspace import create_workspace_root
@pytest.mark.skipif(sys.platform != 'win32', reason='Official Windows ACL runner regression')
def test_official_shell_private_temp_negative_inherited_root_positive():
parent = Path(os.environ['LOCALAPPDATA']) / 'DxProductivity/acceptance-shell'
parent.mkdir(parents=True, exist_ok=True)
roots = {'private_temp': Path(tempfile.mkdtemp(prefix='regression-private-', dir=parent)),
'inherited': create_workspace_root(parent, 'regression-inherited-')}
evidence = {}
for name, root in roots.items():
result = subprocess.run(['node', 'tools/check_acceptance_workspace.mjs', str(root), sys.executable],
cwd=REPO, capture_output=True, text=True, encoding='utf-8', timeout=120, check=True)
evidence[name] = json.loads(result.stdout)
output = REPO / 'outputs/shell-rootcause-probe'
output.mkdir(parents=True, exist_ok=True)
(output / 'behavior-regression.json').write_text(json.dumps(evidence, ensure_ascii=False, indent=2), encoding='utf-8')
for row in evidence.values():
assert row['inline']['exit_code'] == 0
assert '314159' in row['inline']['stdout']
negative, positive = evidence['private_temp'], evidence['inherited']
assert negative['script']['exit_code'] != 0
assert 'Permission denied' in negative['script']['stderr']
assert negative['listing']['exit_code'] != 0
assert positive['script']['exit_code'] == 0
assert json.loads(positive['script']['stdout']) == {'probe': 42}
assert positive['listing']['exit_code'] == 0
assert 'probe.py' in positive['listing']['stdout']
@@ -5,6 +5,18 @@ import os
from pathlib import Path
import subprocess
import sys
import ast
def test_dsh_webengine_python_dependencies_are_not_excluded() -> None:
root = Path(__file__).resolve().parents[2]
tree = ast.parse((root / 'packaging' / 'DxPartReplace.spec').read_text(encoding='utf-8'))
analysis = next(node for node in ast.walk(tree) if isinstance(node, ast.Call)
and isinstance(node.func, ast.Name) and node.func.id == 'Analysis')
keywords = {item.arg: ast.literal_eval(item.value) for item in analysis.keywords
if item.arg in ('hiddenimports', 'excludes')}
assert 'PySide6.QtWebChannel' in keywords['hiddenimports']
assert 'PySide6.QtWebChannel' not in keywords['excludes']
def test_build_files_target_only_repository_local_dx_part_replace_outputs() -> None:
+17
View File
@@ -0,0 +1,17 @@
"""The external oracle distinguishes slash-delimited NC from ordinary text."""
import importlib.util
from pathlib import Path
import pytest
@pytest.mark.parametrize('value,expected', [
('NC/402K/1%', True), ('402K/NC', True), ('nc/402K', True), (' nc /402K', True),
('/NC', True), ('402K/NC/1%', True), ('NC', False), (' NC ', False),
('NCM/402K', False), ('402K/NC_OPTION', False), ('', False)])
def test_external_oracle_recognizes_only_independent_nc_segments(value, expected):
path = Path(__file__).resolve().parents[2] / 'tools/accept_random_cad.py'
spec = importlib.util.spec_from_file_location('nc_marker_oracle_test', path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
assert module.has_nc_marker(value) is expected
@@ -0,0 +1,55 @@
import hashlib
import importlib.util
import json
from pathlib import Path
import pytest
spec = importlib.util.spec_from_file_location('random_cases', Path(__file__).parents[2] / 'tools/random_acceptance_cases.py')
generator = importlib.util.module_from_spec(spec)
spec.loader.exec_module(generator)
def normalized(cases):
return [{k: v for k, v in case.items() if k != 'directory'} for case in cases]
def test_seed_replay_and_semantic_variation(tmp_path):
seed, first = generator.generate(tmp_path / 'a', 1234)
_, replay = generator.generate(tmp_path / 'b', seed)
_, fresh = generator.generate(tmp_path / 'c', 5678)
assert normalized(first) == normalized(replay)
assert all(a['input_hashes'] != b['input_hashes'] for a,b in zip(first, fresh))
assert {case['stratum'] for case in first} == set(generator.STRATA)
def test_independent_literals_and_private_oracles(tmp_path):
_, cases = generator.generate(tmp_path / 'inputs', 1234)
by_kind = {c['stratum']: c for c in cases}
totals = by_kind['totals']
# Independent CSV read, not generator-derived expectation.
import csv
with (Path(totals['directory']) / 'bom.csv').open(encoding='utf-8') as stream:
rows = list(csv.DictReader(stream))
actual = {}
for row in rows:
actual[row['part']] = actual.get(row['part'], 0) + int(row['quantity'])
actual['total'] = sum(int(row['quantity']) for row in rows)
assert totals['expected']['result.json'] == actual
assert by_kind['missing-file']['expected'] == {'result.json': {'status': 'missing', 'content': None}}
assert by_kind['repair-json']['expected']['result.json'] == {'refdes': 'R204', 'part': 'NEW-819394'}
for case in cases:
directory = Path(case['directory'])
assert not (directory / 'result.json').exists()
assert not (directory / 'manifest.json').exists()
assert {str(p.relative_to(directory)).replace('\\', '/') for p in directory.rglob('*') if p.is_file()} == set(case['input_hashes'])
for filename, digest in case['input_hashes'].items():
assert hashlib.sha256((directory / filename).read_bytes()).hexdigest() == digest
def test_minimum_and_fresh_system_seeds(tmp_path):
with pytest.raises(ValueError):
generator.generate(tmp_path / 'small', count=19)
a, _ = generator.generate(tmp_path / 'a')
b, _ = generator.generate(tmp_path / 'b')
assert a != b
@@ -0,0 +1,407 @@
import importlib.util
import hashlib
import json
import sqlite3
from pathlib import Path
import pytest
def module():
spec = importlib.util.spec_from_file_location('random_cad', Path(__file__).parents[2] / 'tools/accept_random_cad.py')
result = importlib.util.module_from_spec(spec)
spec.loader.exec_module(result)
return result
def fixture(tmp_path):
source = tmp_path / 'original'
source.mkdir()
(source / 'board.prj').write_text('original project')
(source / 'database').mkdir()
(source / 'database/icdb.dat').write_bytes(b'authentic database')
graph = {'components': {f'U{i}': {'refdes': f'R{i}', 'attributes': {'Part Number': f'4100-{i}', 'Value': f'{i}K'}, 'pins': {}} for i in range(1, 8)}}
return source / 'board.prj', graph
def test_replay_varies_semantic_inputs_and_preserves_sources(tmp_path):
m = module()
source, graph = fixture(tmp_path)
before = m.fingerprint(source.parent)
a = m.prepare(tmp_path / 'a', source, graph, ['TARGET'], seed=123)
b = m.prepare(tmp_path / 'b', source, graph, ['TARGET'], seed=123)
c = m.prepare(tmp_path / 'c', source, graph, ['TARGET'], seed=456)
semantic = lambda manifest: [{k: case[k] for k in ('kind', 'requests', 'target_cell')} for case in manifest['cases']]
assert semantic(a) == semantic(b)
assert sum(x != y for x, y in zip(semantic(a), semantic(c))) >= 15
assert len(a['cases']) >= 20
assert len({json.dumps(case['requests'], sort_keys=True) for case in a['cases']}) == len(a['cases'])
assert m.fingerprint(source.parent) == before
assert (tmp_path / 'a/seed.json').is_file()
for case in a['cases']:
assert Path(case['project']).resolve().is_relative_to((tmp_path / 'a/projects').resolve())
def test_refuses_source_ancestry_or_existing_output(tmp_path):
m = module()
source, graph = fixture(tmp_path)
with pytest.raises(ValueError):
m.prepare(source.parent / 'copy', source, graph, ['TARGET'])
with pytest.raises(ValueError):
m.prepare(tmp_path, source, graph, ['TARGET'])
def test_code_fingerprint_prunes_installed_dependencies_but_keeps_assets(tmp_path):
m = module()
(tmp_path / 'node_modules').mkdir()
(tmp_path / 'node_modules/dependency.js').write_text('installed package')
(tmp_path / '__pycache__').mkdir()
(tmp_path / '__pycache__/module.pyc').write_bytes(b'cache')
(tmp_path / 'source.py').write_text('source')
(tmp_path / 'icon.ico').write_bytes(b'asset')
assert set(m.code_fingerprint(tmp_path)) == {'source.py', 'icon.ico'}
assert set(m.fingerprint(tmp_path)) == {'source.py', 'icon.ico', 'node_modules/dependency.js', '__pycache__/module.pyc'}
def test_rule_mode_selects_one_globally_unique_root_resistor_with_real_defaults(tmp_path):
m = module()
source, graph = fixture(tmp_path)
for comp in graph['components'].values():
comp['pins'] = {'1': {}, '2': {}}
comp['instance_path'] = 'child'
graph['components']['U1'].pop('instance_path')
graph['components']['U2'].pop('instance_path')
graph['components']['child_duplicate'] = dict(graph['components']['U1'], instance_path='child2')
before = m.fingerprint(source.parent)
result = m.prepare(tmp_path / 'rule', source, graph, ['4100-CA0000-T0'], count=1, supplemental='rule_mode')
case, = result['cases']
request, = case['requests']
assert request == {'refdes': 'R2', 'old_part_number': '4100-2', 'new_part_number': '4100-CA0000-T0',
'value_mode': 'preserve', 'custom_value': '', 'sync_nc': True, 'enabled': True}
assert case['kind'] == 'rule_mode' and not case['target_cell']
assert json.loads((tmp_path / 'rule/seed.json').read_text())['system_seed'] is True
assert m.fingerprint(source.parent) == before
@pytest.mark.parametrize('count,targets', [(2, ['4100-CA0000-T0']), (1, ['OTHER'])])
def test_rule_mode_is_one_real_t0_supplement(tmp_path, count, targets):
m = module()
source, graph = fixture(tmp_path)
with pytest.raises(ValueError, match='rule_mode'):
m.prepare(tmp_path / 'rule', source, graph, targets, count=count, supplemental='rule_mode')
def test_rule_mode_refuses_ambiguous_all_match_scope(tmp_path):
m = module()
source, graph = fixture(tmp_path)
for comp in graph['components'].values():
comp['attributes']['Part Number'] = '4100-SHARED'
comp['pins'] = {'1': {}, '2': {}}
with pytest.raises(ValueError, match='unique root resistor'):
m.prepare(tmp_path / 'rule', source, graph, ['4100-CA0000-T0'], count=1, supplemental='rule_mode')
@pytest.mark.parametrize('fault', [None, 'missing_rule_snapshot', 'wrong_uid', 'cancelled', 'missing_after'])
def test_rule_mode_requires_application_snapshot_bound_uid_and_wholegraph_report(tmp_path, fault):
m = module()
project = tmp_path / 'copy.prj'
project.touch()
request = {'refdes': 'RV15', 'old_part_number': '4100-CA1020-T7', 'new_part_number': '4100-CA0000-T0',
'value_mode': 'preserve', 'custom_value': '', 'sync_nc': True, 'enabled': True}
case = {'project': str(project), 'requests': [request]}
before = {'components': {'u1': {'refdes': 'RV15', 'attributes': {'Part Number': request['old_part_number']}}}}
directory = tmp_path / 'data/tasks/task/netlist-checks/queue'
directory.mkdir(parents=True)
plan = {'rows': [], 'copy_value': False, 'project_path': str(project), 'rules': [{
'old_part': request['old_part_number'].casefold(), 'new_part': request['new_part_number'].casefold(),
'value_mode': 'preserve', 'custom_value': '', 'sync_nc': '1', 'target_cell': ''}]}
report = {'status': 'verified', 'verified': True, 'before': {'status': 'success', 'complete': True},
'after': {'status': 'success', 'complete': True}, 'expected_changes': {'u1': {}},
'rule_scopes': [{'project_path': str(project), 'schematic_name': 'MAIN', 'sheet_number': '1',
'old_part': request['old_part_number'], 'new_part': request['new_part_number'], 'source_uids': ['u1']}]}
if fault == 'missing_rule_snapshot':
plan.pop('rules')
elif fault == 'wrong_uid':
report['rule_scopes'][0]['source_uids'] = ['other']
elif fault == 'cancelled':
report.update(status='cancelled', verified=False)
elif fault == 'missing_after':
report['after'] = None
(directory / 'plan.json').write_text(json.dumps(plan))
path = directory / 'verification.json'
path.write_text(json.dumps(report))
task = {'task_id': 'task', 'target_plan_mode': 0, 'copy_part_number_to_value': 0}
entry = {'netlist_verification': {'report_path': str(path)}}
if fault:
with pytest.raises((AssertionError, KeyError, TypeError)):
m.rule_netlist_evidence(tmp_path, task, entry, case, before)
else:
assert m.rule_netlist_evidence(tmp_path, task, entry, case, before)['bound_uid'] == 'u1'
def test_verifier_reads_each_queues_own_per_task_netlist_mapping(tmp_path):
m = module()
queues = tmp_path / 'data/task-queues'
queues.mkdir(parents=True)
(queues / 'first.json').write_text(json.dumps({'entries': {'A': {'state': 'completed'}},
'netlist_verification': {'A': {'report_path': 'own-A', 'status': 'verified'}}}))
(queues / 'second.json').write_text(json.dumps({'entries': {'B': {'state': 'completed'}},
'netlist_verification': {'A': {'report_path': 'foreign-A', 'status': 'verified'}}}))
entries = m.queue_entries(tmp_path)
assert entries['A']['netlist_verification']['report_path'] == 'own-A'
assert entries['B'] == {'state': 'completed'}
def test_verifier_can_pin_the_actual_new_execution_and_keep_old_interruption(tmp_path):
m = module()
queues = tmp_path / 'data/task-queues'
queues.mkdir(parents=True)
old = queues / ('a' * 32 + '.json')
old.write_text(json.dumps({'entries': {'A': {'state': 'blocked'}}}))
current = queues / ('b' * 32 + '.json')
current.write_text(json.dumps({'entries': {'A': {'state': 'completed'}},
'netlist_verification': {'A': {'report_path': 'current', 'status': 'verified'}}}))
before = old.read_bytes()
assert m.queue_entries(tmp_path, queue_id='b' * 32)['A']['netlist_verification']['report_path'] == 'current'
assert old.read_bytes() == before
with pytest.raises((ValueError, FileNotFoundError)):
m.queue_entries(tmp_path, queue_id='../outside')
with pytest.raises(FileNotFoundError):
m.queue_entries(tmp_path, queue_id='c' * 32)
@pytest.mark.parametrize('state,cleanup,passed', [
('released', None, True), ('released', {}, True),
('released', {'errors': ['actual cleanup failure']}, False),
('released', [], False), ('blocked', None, False)])
def test_verifier_handles_optional_cleanup_without_ignoring_resource_errors(tmp_path, state, cleanup, passed):
m = module()
source, _ = fixture(tmp_path)
baseline = tmp_path / 'baseline.json'
baseline.write_text('{"components":{}}')
baseline.with_name('effective.txt').write_text('')
(tmp_path / 'manifest.json').write_text(json.dumps({'baseline': str(baseline), 'source': str(source),
'source_hashes': m.fingerprint(source.parent), 'cases': []}))
data = tmp_path / 'data'
data.mkdir()
with sqlite3.connect(data / 'task-center.db') as db:
db.execute('CREATE TABLE tasks (task_id TEXT)')
db.execute('CREATE TABLE projects (project_id TEXT)')
session = data / 'batch-resources' / ('a' * 32) / 'session.json'
session.parent.mkdir(parents=True)
session.write_text(json.dumps({'state': state, 'cleanup': cleanup}))
report = m.verify(tmp_path)
assert report['passed'] is passed
assert bool(report['resource_errors']) is not passed
def saved_fixture(root, jobs_and_uids):
events = root / 'data/tasks/task/events.jsonl'
events.parent.mkdir(parents=True)
with sqlite3.connect(root / 'data/task-center.db') as db:
db.execute('CREATE TABLE execution_rounds (task_id TEXT, round_id TEXT)')
db.execute('CREATE TABLE execution_operations (task_id TEXT, round_id TEXT, command_json TEXT, receipt_json TEXT)')
db.execute("INSERT INTO execution_rounds VALUES ('task','current')")
for job, uid in jobs_and_uids:
command = {'target_scope': 'targets', 'target_selectors': uid+'\x1fR1\x1f10K'}
receipt = {'job_id': job, 'native_state': 'saved_ok'}
db.execute('INSERT INTO execution_operations VALUES (?,?,?,?)', ('task', 'current', json.dumps(command), json.dumps(receipt)))
events.write_text('\n'.join(json.dumps({'operation': 'quick_run_executed', 'message': 'identical display message',
'details': {'state': 'saved_ok', 'timing': {'job_id': job}}}) for job, uid in jobs_and_uids), encoding='utf-8')
return events
def test_saved_uid_counts_bind_identical_messages_to_distinct_job_selectors(tmp_path):
m = module()
saved_fixture(tmp_path, [('job1', '$2I1'), ('job2', '$2I2')])
assert m.saved_uids(tmp_path) == {'task:$2I1': 1, 'task:$2I2': 1}
def test_saved_uid_replay_with_new_job_increments_original_uid(tmp_path):
m = module()
saved_fixture(tmp_path, [('job1', '$2I1'), ('job2', '$2I1')])
assert m.saved_uids(tmp_path) == {'task:$2I1': 2}
@pytest.mark.parametrize('fault', ['missing_job', 'wrong_receipt_job', 'past_round', 'missing_uid'])
def test_saved_uid_missing_current_round_binding_is_insufficient_not_message_guess(tmp_path, fault):
m = module()
path = saved_fixture(tmp_path, [('job1', '$2I1')])
if fault == 'missing_job':
event = json.loads(path.read_text())
event['details']['timing'] = {}
path.write_text(json.dumps(event))
else:
with sqlite3.connect(tmp_path / 'data/task-center.db') as db:
if fault == 'past_round':
db.execute("UPDATE execution_operations SET round_id='old'")
elif fault == 'wrong_receipt_job':
db.execute('UPDATE execution_operations SET receipt_json=?', (json.dumps({'job_id': 'other', 'native_state': 'saved_ok'}),))
else:
db.execute('UPDATE execution_operations SET command_json=?', (json.dumps({'target_scope': 'targets', 'target_selectors': ''}),))
with pytest.raises(ValueError, match='Acceptance evidence insufficient'):
m.saved_uids(tmp_path)
@pytest.mark.parametrize('tampered', [False, True])
def test_saved_rule_uid_comes_from_hash_bound_native_snapshot(tmp_path, tampered):
m = module()
saved_fixture(tmp_path, [('job1', '$2I1')])
project = tmp_path / 'board.prj'
snapshot = tmp_path / 'data/tasks/task/execution-proof/snapshot-job1.tsv'
snapshot.parent.mkdir()
snapshot.write_text('\t'.join(['M', str(project), 'OLD', 'NEW', 'MAIN:1', 'preserve', '', '1'])+'\n'
+'\t'.join(['C', '$2I1', 'R1', '0', '0', '0', '0', ''])+'\n', encoding='utf-8')
proof = Path(str(snapshot)+'.native.json')
native = {'ok': True, 'stage': 'native_replace_verified', 'batch_attempt': {'task_id': 'task', 'job_id': 'job1'},
'transaction': {'snapshot_sha256': hashlib.sha256(snapshot.read_bytes()).hexdigest(),
'project_path': m.os.path.normcase(m.os.path.abspath(project)), 'sheet_identity': 'MAIN:1',
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'source_uids': ['$2I1'],
'target_selectors': None, 'library_row': ['R', 'NEW', 'lib:r', '', 'NAME', 'LABEL']}}
proof.write_text(json.dumps(native), encoding='utf-8')
receipt = {'job_id': 'job1', 'native_state': 'saved_ok', 'proof': {'files': [
{'path': str(p), 'sha256': hashlib.sha256(p.read_bytes()).hexdigest()} for p in (snapshot, proof)]}}
command = {'target_scope': 'all', 'target_selectors': '', 'project_path': str(project),
'old_part': 'OLD', 'new_part': 'NEW', 'schematic_name': 'MAIN', 'sheet_number': '1'}
with sqlite3.connect(tmp_path / 'data/task-center.db') as db:
db.execute('UPDATE execution_operations SET command_json=?,receipt_json=?', (json.dumps(command), json.dumps(receipt)))
if tampered:
snapshot.write_text(snapshot.read_text().replace('$2I1', '$2I2'))
with pytest.raises(ValueError, match='Acceptance evidence insufficient'):
m.saved_uids(tmp_path)
else:
assert m.saved_uids(tmp_path) == {'task:$2I1': 1}
@pytest.mark.parametrize('missing', [None, 'force', 'recovery','session_pid','frame'])
@pytest.mark.parametrize('report_name', ['packaged-ui-report.json', 'terminal-ui-recheck.json'])
def test_force_oracle_reads_actual_packaged_driver_steps(tmp_path, missing, report_name):
m = module()
executable=tmp_path/'candidate.exe'
executable.write_bytes(b'frozen test candidate')
sha=hashlib.sha256(executable.read_bytes()).hexdigest()
steps = [{'step':'real-ui-force-confirmed','frames':[{'stage':'native_replace','token':'a'*32,'job_id':'actual_job','sequence':4}]}, {'step':'real-ui-recovery-finished'}]
if missing in {'force','recovery'}:
steps = [s for s in steps if missing not in s['step']]
if missing=='frame':
steps[0]['frames'][0]['sequence']=0
(tmp_path/'ui-session.json').write_text(json.dumps({'pid':99 if missing=='session_pid' else 42,'executable':str(executable),'exe_sha256':sha}))
(tmp_path/report_name).write_text(json.dumps({'stage':'execute','passed':True,'steps':steps,
'mode':'frozen EXE external Windows UIA','root':str(tmp_path),'pid':42,'exe':str(executable),'exe_sha256':sha}))
options = {'ui_report': report_name} if report_name != 'packaged-ui-report.json' else {}
if missing:
with pytest.raises(AssertionError):
m.force_recovery_evidence(tmp_path, **options)
else:
assert m.force_recovery_evidence(tmp_path, **options)['recovery_observed'] is True
def test_pon_root_preparation_never_selects_child_or_reused_uid_or_part(tmp_path):
m = module()
source, graph = fixture(tmp_path)
for comp in graph['components'].values():
comp['instance_path']='child'
comp['pins']={'1':{},'2':{}}
graph['components']['U1'].pop('instance_path')
graph['components']['U2'].pop('instance_path')
graph['components']['child-reused'] = dict(graph['components']['U1'],source_uid='U1',refdes='CHILD',
attributes={'Part Number':'OTHER'},instance_path='child')
result=m.prepare(tmp_path/'pon',source,graph,['4100-CA0000-T0'],count=1,supplemental='pon_root')
case,=result['cases']
request,=case['requests']
assert request['refdes']=='R2' and case['kind']=='pon_root'
def test_identity_raw_oracle_preserves_duplicate_gates_and_child_attributes(tmp_path):
m = module()
text = (' Symbol $1I1\tref des R1\tpage \n\tPath root\n\tSymbol name r.1 type Primitive\n'
'\tProp Ref Designator R1\n\tProp Part Number OLD\n\tProp Value 10K\n'
' Symbol $2I2\tref des U1\tpage \n\tPath child\n\tSymbol name ic.1 type Primitive\n'
'\tProp Ref Designator U1\n\tProp Part Number IC\n\tProp Part Name \n\tProp Part Name ICNAME\n'
' Symbol $2I3\tref des U1\tpage \n\tPath child\n\tSymbol name ic.2 type Primitive\n'
'\tProp Ref Designator U1\n\tProp Part Number IC\n')
before=tmp_path/'before.txt'; after=tmp_path/'after.txt'
before.write_text(text); after.write_text(text.replace('Part Number OLD','Part Number NEW').replace('Value 10K','Value NATIVE'))
a=m.raw_identity_attributes(before); b=m.raw_identity_attributes(after)
assert m.unselected_raw_identities(a,{'$1I1'}) == m.unselected_raw_identities(b,{'$1I1'})
assert m.selected_root_attributes(b,{'$1I1'}) == {'R1':{'Part Number':'NEW','Value':'NATIVE'}}
after.write_text(after.read_text().replace('Part Name ICNAME','Part Name CHANGED'))
assert m.unselected_raw_identities(a,{'$1I1'}) != m.unselected_raw_identities(m.raw_identity_attributes(after),{'$1I1'})
def test_identity_raw_multiset_does_not_collapse_repeated_ground_symbols(tmp_path):
m=module()
symbol=' Symbol GND\tref des \tpage \n\tPath root\n\tSymbol name gnd.1 type \n\tProp Value GND\n'
path=tmp_path/'raw.txt'
path.write_text(symbol*2)
before=m.unselected_raw_identities(m.raw_identity_attributes(path),set())
path.write_text(symbol)
after=m.unselected_raw_identities(m.raw_identity_attributes(path),set())
assert sum(before.values())==2 and sum(after.values())==1 and before!=after
def observation(sequence, stage, pn, value='NATIVE', *, count=None, window=None):
fields = [('Part Number', pn), ('Part Label', 'LABEL')]
if value is not None:
fields.append(('Value', value))
if window is None:
window = ('before_native' if pn == 'OLD' else 'saved_readback' if stage == 'open_sheet'
else 'restoring' if stage == 'snapshot_attributes' else 'pre_restore')
lines = [f'O\tjob\tu1\t{stage}\t{sequence}.5\t{sequence}\t{window}']
lines += [f'E\tu1\t{i}\t{name}\t{text}\t\tunknown' for i, (name, text) in enumerate(fields)]
lines.append(f'Z_OBS\tjob\tu1\t{stage}\t{sequence}\t{len(fields) if count is None else count}\t{window}')
return '\n'.join(lines)+'\n'
def test_native_value_observation_is_count_bound_and_precedes_restoration():
m = module()
text = observation(1, 'query_components', 'OLD', 'OLD_VALUE')
text += observation(2, 'query_components', 'NEW', None)
text += observation(3, 'snapshot_attributes', 'NEW', None)
text += observation(4, 'open_sheet', 'NEW', None)
actual = m.native_value_observation(text, job='job', uid='u1', old='OLD', new='NEW')
assert actual['value'] == '' and actual['present'] is False
assert actual['sequence'] == 2 and actual['count'] == 2
assert actual['reopened_present'] is False
def test_native_window_remains_before_restoration_when_another_uid_changed_stage_label():
m = module()
text = observation(1, 'query_components', 'OLD', 'OLD_VALUE')
text += observation(2, 'snapshot_attributes', 'NEW', window='pre_restore')
text += observation(3, 'query_components', 'NEW', window='restoring')
text += observation(4, 'open_sheet', 'NEW')
assert m.native_value_observation(text, job='job', uid='u1', old='OLD', new='NEW')['sequence'] == 2
@pytest.mark.parametrize('fault', ['truncated', 'wrong_job', 'wrong_uid', 'wrong_part', 'wrong_count',
'missing_ordinal', 'postrestore', 'wrong_sequence', 'legacy', 'wrong_window', 'stale_stage'])
def test_native_value_observation_rejects_incomplete_foreign_or_late_baseline(fault):
m = module()
before = observation(1, 'query_components', 'OLD', 'OLD_VALUE')
native = observation(2, 'query_components', 'NEW')
if fault == 'truncated':
native = native[:native.index('Z_OBS')]
elif fault == 'wrong_job':
native = native.replace('Z_OBS\tjob', 'Z_OBS\tother')
elif fault == 'wrong_uid':
native = native.replace('E\tu1\t0', 'E\tu2\t0')
elif fault == 'wrong_part':
native = native.replace('Part Number\tNEW', 'Part Number\tOTHER')
elif fault == 'wrong_count':
native = observation(2, 'query_components', 'NEW', count=4)
elif fault == 'missing_ordinal':
native = native.replace('E\tu1\t1', 'E\tu1\t9')
elif fault == 'postrestore':
native = observation(2, 'snapshot_attributes', 'NEW')
elif fault == 'wrong_sequence':
native = native.replace('Z_OBS\tjob\tu1\tquery_components\t2', 'Z_OBS\tjob\tu1\tquery_components\t1')
elif fault == 'legacy':
native = native[:native.index('Z_OBS')].replace('\t2.5\t2\tpre_restore\n', '\t2.5\n')
elif fault == 'wrong_window':
native = native.replace('\t3\tpre_restore\n', '\t3\tbefore_native\n')
elif fault == 'stale_stage':
native = observation(2, 'query_components', 'NEW', window='restoring')
with pytest.raises(ValueError):
m.native_value_observation(before+native+observation(3, 'open_sheet', 'NEW'),
job='job', uid='u1', old='OLD', new='NEW')