<?php
declare(strict_types=1);require __DIR__.'/bootstrap.php';$f=json_decode(file_get_contents($runtime.'/fixture-journal.json'),true);$current=$own();$check($current===$f['after'],'Independent final36 scope matches seven-insert journal');$count=0;foreach($f['owned'] as $o){$rows=$read('SELECT * FROM yoshop_'.$o['table'].' WHERE '.$o['pk'].'=? AND store_id=?',[$o['id'],$o['module']==='store'?2147001111:2147001112]);$check(count($rows)===1&&$rows[0]===$o['original'],'Each retained7 full original exact');$count++;}$shadow=$dm?(int)$read("SELECT COUNT(*) n FROM USER_OBJECTS WHERE OBJECT_NAME='SP_TABLEDEF'")[0]['n']:null;$check(!$dm||$shadow===0,'No schema shadow of metadata routine');
$sources=json_decode(file_get_contents(__DIR__.'/source-final-sha.json'),true)['files'];foreach($sources as $file=>$sha)$check(hash_file('sha256',$root.'/'.$file)===$sha,'Final isolated all-source pin '.$file);$states=[];foreach(['state.json','state-FINAL.json'] as $name){$s=json_decode(file_get_contents($runtime.'/'.$name),true);$states[$name]=['pid'=>$s['pid'],'absent'=>!posix_kill($s['pid'],0)];$check($states[$name]['absent'],'Own worker must be stopped before deletion');}
$files=static function($dir){if(!is_dir($dir))return 0;$n=0;$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir,FilesystemIterator::SKIP_DOTS));foreach($it as $f)if($f->isFile())$n++;return $n;};$cacheFiles=$files($runtime.'/cache');$check($cacheFiles===0,'No remaining Auth cache files');
$remove=static function($dir)use(&$remove){if(is_link($dir)||is_file($dir)){if(!unlink($dir))throw new RuntimeException('Owned temporary file cleanup failed');return;}if(!is_dir($dir))return;foreach(new DirectoryIterator($dir) as $f)if(!$f->isDot())$remove($f->getPathname());if(!rmdir($dir))throw new RuntimeException('Owned temporary directory cleanup failed');};$removed=[];foreach([__DIR__.'/source',$runtime.'/cache',$runtime.'/runtime',$runtime.'/compile-runtime',$runtime.'/money-contract-runtime'] as $dir){$check(strpos($dir,__DIR__.'/')===0&&$dir!==__DIR__&&$dir!==$runtime,'Exact own cleanup target');$remove($dir);$check(!file_exists($dir),'Own temporary target absent');$removed[]=$dir;}
$out=['lane'=>$lane,'independent_final36_exact'=>true,'retained7_full_rows_exact'=>$count,'no_metadata_routine_shadow'=>$shadow,'final_source_pins_verified'=>count($sources),'original_and_final_workers_absent'=>$states,'Auth_cache_files_before_cleanup'=>$cacheFiles,'own_source_cache_runtime_removed'=>true,'removed_owned_paths'=>$removed,'DML'=>0,'DDL'=>0,'HTTP'=>0];$persist('resource-cleanup.json',$out);file_put_contents(__DIR__.'/resource-cleanup-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));echo json_encode($out),"\n";
