<?php
declare(strict_types=1);
$lane=$argv[1]??'';if(!in_array($lane,['B','C'],true))exit(78);$worker=$lane==='B'?6987:2282385;$parent=$lane==='B'?10001:112175;$port=$lane==='B'?18921:18922;$root=$lane==='B'?'/tmp/zhct-http-store-mysql.dQTTBP':'/tmp/zhct-dm-poc-20260928.fcpiiN/store';
$r=['lane'=>$lane,'own_worker_absent'=>!is_file('/proc/'.$worker.'/cmdline'),'own_auth_absent'=>!file_exists(__DIR__.'/private/auth.json'),'own_case_absent'=>!file_exists(__DIR__.'/private/current-case.json'),'parent_original_cmdline_match'=>strpos((string)file_get_contents('/proc/'.$parent.'/cmdline'),$root.'/public')!==false];$s=@fsockopen('127.0.0.1',$port,$n,$e,0.5);$r['own_port_closed']=$s===false;if($s)fclose($s);
$r['cache_files']=0;if(is_dir(__DIR__.'/cache'))foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__.'/cache',FilesystemIterator::SKIP_DOTS))as $f){if($f->isLink())throw new RuntimeException('Cache symlink');if($f->isFile())$r['cache_files']++;}
$lock=json_decode(file_get_contents(__DIR__.'/source-lock.json'),true);$r['source_count']=count($lock);$r['source_lock_sha256']=hash_file('sha256',__DIR__.'/source-lock.json');foreach($lock as $f=>$h)if(hash_file('sha256',__DIR__.'/source/application/'.$f)!==$h)throw new RuntimeException('Candidate source drift');
foreach(['own_worker_absent','own_auth_absent','own_case_absent','parent_original_cmdline_match','own_port_closed']as $k)if(!$r[$k])throw new RuntimeException('Resource check '.$k);if($r['source_count']!==958||$r['cache_files']!==0)throw new RuntimeException('Resource cardinality');$r['SQL']=0;$r['HTTP']=0;$f=__DIR__.'/private/resource-check.json';file_put_contents($f,json_encode($r,JSON_PRETTY_PRINT));chmod($f,0600);echo json_encode($r),"\n";
