"""Verify completion evidence, never infer completed software from contract readiness.""" from pathlib import Path import json,hashlib,sys from build_validate import load,need,schema_check def pointer(doc,p): cur=doc for x in p.split('/')[1:]: key=x.replace('~1','/').replace('~0','~') cur=cur[int(key)] if isinstance(cur,list) else cur[key] return cur def oracle(captured,expect): try:actual=pointer(captured[expect['source']],expect['pointer']) except (KeyError,IndexError,ValueError,TypeError):return expect['operator']=='absent' op=expect['operator'];v=expect['expected'] if op=='eq':return actual==v and type(actual)==type(v) if op=='contains':return v in actual if op=='gte':return actual>=v if op=='lte':return actual<=v return False def check(contract_path,receipt_path,evidence_root,standard_root=None): root=Path(standard_root) if standard_root else Path(__file__).resolve().parents[1] cp=Path(contract_path);c=load(cp);r=load(receipt_path);schema_check(load(root/'schemas/build_result.schema.json'),r) need(r['contract_sha256']==hashlib.sha256(cp.read_bytes()).hexdigest(),'Contract digest mismatch') need(r['status']=='software_complete' and not r['unresolved_build_blockers'],'Incomplete build') scope=set(c['scope']['requirement_ids']);res={x['requirement_id']:x for x in r['requirement_results']} need(set(res)==scope and len(res)==len(r['requirement_results']),'Incomplete or duplicate requirement results') scenarios={s['id']:s for u in c['use_cases'] for s in u['scenarios']} captures={s['scenario_id']:s for s in r['scenario_results']} need(set(captures)==set(scenarios) and len(captures)==len(r['scenario_results']),'Missing or duplicate scenario evidence') er=Path(evidence_root).resolve() def evidence(p): q=er/p;need('\\' not in p and ':' not in p and not any(ord(x)<32 for x in p),'Unsafe evidence name');need(not Path(p).is_absolute() and '..' not in Path(p).parts and q.resolve().is_relative_to(er),'Unsafe evidence path') need(not q.is_symlink() and q.is_file(),'Evidence missing '+p);return q for sid,record in captures.items(): p=evidence(record['capture_path']);need(hashlib.sha256(p.read_bytes()).hexdigest()==record['capture_sha256'],'Capture digest mismatch') need(record['provenance']['source_commit']==r['source_commit'],'Mixed source revisions') cap=load(p) need(cap.get('scenario_id')==sid and cap.get('run_id')==record['provenance']['run_id'],'Capture identity mismatch') need(all(oracle(cap,e) for e in scenarios[sid]['expected']),'Scenario failed '+sid) for rid,rr in res.items(): required={s['id'] for u in c['use_cases'] if rid in u['requirement_ids'] for s in u['scenarios']} need(rr['status']=='passed' and set(rr['scenario_ids'])==required,'Requirement evidence incomplete '+rid) cmds={x['command_id']:x for x in r['command_results']} need(set(cmds)=={x['id'] for x in c['commands']} and len(cmds)==len(r['command_results']),'Command coverage mismatch') for cmd in c['commands']: v=cmds[cmd['id']];need(v['exit_code']==0 and not v['skipped'],'Failed or skipped command') need(set(v['artifact_paths'])>=set(cmd['required_artifacts']),'Required artifact omitted') hashes={x['path']:x['sha256'] for x in v['artifact_hashes']} need(set(hashes)==set(v['artifact_paths']) and len(hashes)==len(v['artifact_hashes']),'Artifact hash inventory mismatch') for path in v['artifact_paths']: need(hashlib.sha256(evidence(path).read_bytes()).hexdigest()==hashes[path],'Command artifact digest mismatch') return {'software_complete':True,'source_commit':r['source_commit'],'scenario_count':len(scenarios),'live_enabled':False,'external_live_blockers':r['external_live_blockers'],'trust_boundary':'Evidence bytes and declared provenance verified; trusted CI must attest execution and revision. This verifier does not establish evidence authenticity.'} if __name__=='__main__': try:print(json.dumps(check(*sys.argv[1:4])));sys.exit(0) except Exception as e:print(json.dumps({'software_complete':False,'error':str(e)}));sys.exit(2)