<?php
// No common/bootstrap/PDO: independent resource and exact source-byte check only.
$plan=json_decode(file_get_contents(__DIR__.'/execution-plan.json'),true);$lane=getenv('NF_LANE');$worker=json_decode(file_get_contents(__DIR__.'/private/worker-pid.json'),true);$cmd=(string)@file_get_contents('/proc/'.$worker['pid'].'/cmdline');if($cmd!==''&&strpos($cmd,__DIR__.'/index.php')!==false)throw new RuntimeException('Own worker remains');$s=@fsockopen('127.0.0.1',$plan['ports'][$lane],$n,$e,0.5);if($s){fclose($s);throw new RuntimeException('Own port occupied');}foreach(['auth.json','current-case.json','current-lease.json']as $f)if(file_exists(__DIR__.'/private/'.$f))throw new RuntimeException('Own temporary credential remains');if(is_dir(__DIR__.'/cache')){$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__.'/cache',FilesystemIterator::SKIP_DOTS));foreach($it as $f)if($f->isFile())throw new RuntimeException('Own cache remains');}
$parent=$lane==='B'?10001:112175;$parentRoot=$lane==='B'?'/tmp/zhct-http-store-mysql.dQTTBP':'/tmp/zhct-dm-poc-20260928.fcpiiN/store';if(strpos((string)file_get_contents('/proc/'.$parent.'/cmdline'),$parentRoot.'/public')===false)throw new RuntimeException('Original parent identity');
$source=[];$all=[];$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__,FilesystemIterator::SKIP_DOTS));foreach($it as $f){if($f->isLink())throw new RuntimeException('Runtime symlink');if(!$f->isFile())continue;$rel=substr($f->getPathname(),strlen(__DIR__)+1);if(strpos(basename($rel),'._')===0)throw new RuntimeException('AppleDouble extra');$all[$rel]=['sha256'=>hash_file('sha256',$f->getPathname()),'bytes'=>$f->getSize(),'mode'=>sprintf('%04o',$f->getPerms()&0777)];if(strpos($rel,'source/application/')===0)$source[substr($rel,strlen('source/application/'))]=$all[$rel]['sha256'];}
$lock=json_decode(file_get_contents(__DIR__.'/source-lock.json'),true);ksort($lock);ksort($source);ksort($all);if($source!==$lock||count($source)!==959)throw new RuntimeException('Final full APP959 source fileset');
$r=['lane'=>$lane,'source959_exact'=>true,'runtime_actual_regular_count_excluding_this_receipt'=>count($all),'runtime_actual_fileset_excluding_this_receipt'=>$all,'no_AppleDouble'=>true,'own_worker_absent'=>true,'own_port_closed'=>true,'own_cache_auth_absent'=>true,'original_parent_alive'=>true,'SQL'=>0,'HTTP'=>0,'DML'=>0,'DDL'=>0];$f=__DIR__.'/private/resource-check.json';$h=fopen($f,'xb');if(!$h)throw new RuntimeException('Resource receipt no overwrite');fwrite($h,json_encode($r,JSON_PRETTY_PRINT));fclose($h);chmod($f,0600);echo json_encode(['lane'=>$lane,'source959_exact'=>true,'own_resources_clear'=>true,'original_parent_alive'=>true,'regular_files'=>count($all),'SQL'=>0,'HTTP'=>0,'DML'=>0]),PHP_EOL;
