<?php
// Root-scoped final missing-uuid case: evidence-only, no DB/bootstrap.
$dir=__DIR__.'/private';foreach(['sent','response','client','wire','execution','oracle']as $p)if(file_exists($dir.'/'.$p.'-R12.json'))throw new RuntimeException('R12 already exists');
$before=json_decode(file_get_contents($dir.'/read8-preflight.json'),true);$end=json_decode(file_get_contents($dir.'/read8-tool-failure-1791691027-397814.json'),true);$d=[];$same=[];foreach($end['observer']as $r){preg_match('/FROM ([a-z_]+) ORDER/',$r['sql'],$m);$t=$m[1];if($before['snapshot'][$t]!==$r['result'])$d[$t]=['before'=>$before['snapshot'][$t],'after'=>$r['result']];else $same[]=$t;}
if(array_keys($d)!==['ydy_staff']||count($same)!==16||!in_array('ydy_user',$same,true)||!in_array('ydy_config',$same,true))throw new RuntimeException('Scoped root authorized protection only');
$base=json_decode(file_get_contents($dir.'/baseline.json'),true);$oracle=json_decode(file_get_contents($dir.'/oracle-R04.json'),true);if($oracle['inputs']['user']!==$base['actor'])throw new RuntimeException('Original actor full row mismatch');
$proof=json_decode(file_get_contents($dir.'/resource-read8-proof.json'),true);$gate=json_decode(file_get_contents($dir.'/read8-pureguard.json'),true);if($proof['source_files']!==959||$gate['count']!==14||$gate['gate_sha256']!==hash_file('sha256',__DIR__.'/Gate.php'))throw new RuntimeException('Source/hardRO proof mismatch');
$r=['absent_R12_artifacts'=>true,'same16'=>$same,'unknown_staff_delta'=>$d,'original_actor_full_row_equal'=>true,'source959'=>true,'hardRO_pure14'=>true,'readonly_total_before'=>(int)file_get_contents($dir.'/budget-total-read.json'),'reserved_before'=>(int)file_get_contents($dir.'/budget-read8-HTTP.json'),'actual_sent_before'=>count(glob($dir.'/sent-R*.json')),'new_SQL'=>0,'new_HTTP'=>0];$f=$dir.'/R12-file-preflight.json';$h=fopen($f,'xb');if(!$h)throw new RuntimeException('No overwrite');fwrite($h,json_encode($r,JSON_PRETTY_PRINT));fclose($h);chmod($f,0600);echo json_encode($r),PHP_EOL;
