<?php
declare(strict_types=1);
require __DIR__.'/bootstrap.php';require __DIR__.'/natural.php';
$fixture=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$final=$own();$naturalFinal=$natural();$protection=$protect();
$check($fixture['committed']&&$final===$fixture['after'],'All29 final original sets exact');$check($naturalFinal===$fixture['natural_after'],'Natural dictionaries and identity digests exact');$check($protection===$fixture['before_protect'],'Prior natural/default unchanged');
$persist('final-original.json',['original'=>$final,'natural'=>$naturalFinal,'protect'=>$protection]);
$dir=$runtime.'/evidence';if(!is_dir($dir))mkdir($dir,0700);foreach(glob($runtime.'/*') as $file){$name=basename($file);if(preg_match('/^(fixture-journal|final-original|run-[A-Z]+|cases-[A-Z]+)\.json$|^(private-wire|private-sql|queries)\.jsonl$/D',$name)){copy($file,$dir.'/'.$name);chmod($dir.'/'.$name,0600);}}
$tokens=0;foreach(glob($runtime.'/run-*.json') as $file){$run=json_decode(file_get_contents($file),true);$check($run['all_own_tokens_absent'],'Own phase tokens absent');$tokens+=$run['own_tokens'];}
foreach(glob($runtime.'/native-*/*.json') as $file){$r=json_decode(file_get_contents($file),true);if(isset($r['run'])){$phase=substr(basename(dirname($file)),7);$native=['stats'=>$r['run']['stats'],'failures'=>count($r['run']['failures']),'raw_result_sha256'=>hash_file('sha256',$file)];file_put_contents(__DIR__.'/native-'.$phase.'-'.$lane.'.json',json_encode($native,JSON_PRETTY_PRINT));}}
$out=['lane'=>$lane,'final_all29_original_exact'=>true,'natural_dictionary_and_identity_digests_exact'=>naturalDigest($naturalFinal),'natural_default_unchanged'=>true,'own_phase_tokens_absent'=>$tokens,'HTTP_DML'=>0,'preparation_INSERT'=>count($fixture['dml']),'preparation_UPDATE'=>0,'preparation_DELETE'=>0,'DDL'=>0,'marked_operator_rows_retained'=>2,'evidence'=>$dir,'files'=>array_map('basename',glob($dir.'/*'))];file_put_contents(__DIR__.'/audit-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT));echo json_encode($out),"\n";
