<?php
if(__DIR__!=='/tmp/cdx-store-refund-reject-state-20261010'||is_link(__DIR__))exit(78);
$lane=$argv[1]??'';$pid=(int)($argv[2]??0);$port=$lane==='B'?18301:18302;
if(!in_array($lane,['B','C'],true)||$pid<=1)exit(78);
$cmd=(string)file_get_contents('/proc/'.$pid.'/cmdline');
if(strpos($cmd,__DIR__.'/index.php')===false||strpos($cmd,'127.0.0.1:'.$port)===false)exit(78);
if(!posix_kill($pid,SIGTERM))exit(2);$removed=0;
foreach(['cache','runtime'] as $child){$dir=__DIR__.'/'.$child;if(!is_dir($dir)||is_link($dir))continue;$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir,FilesystemIterator::SKIP_DOTS),RecursiveIteratorIterator::CHILD_FIRST);foreach($it as $item){if($item->isLink())throw new RuntimeException('Unexpected symlink');if($item->isDir()){if(!rmdir($item->getPathname()))throw new RuntimeException('Directory cleanup');}else{if(!unlink($item->getPathname()))throw new RuntimeException('File cleanup');$removed++;}}if(!rmdir($dir))throw new RuntimeException('Cache cleanup');}
usleep(100000);$socket=@fsockopen('127.0.0.1',$port,$n,$e,1);$closed=$socket===false;if($socket)fclose($socket);
$audit=json_decode((string)file_get_contents(__DIR__.'/post-cleanup-audit.json'),true);if(empty($audit['all_passed']))throw new RuntimeException('Independent cleanup source audit required');
foreach(['private-lease.json','private-write-journal.json','result-private.json','private-error.json','private-request-order-no.json'] as $file){$path=__DIR__.'/'.$file;if(is_link($path))throw new RuntimeException('Private file symlink');if(is_file($path)&&!unlink($path))throw new RuntimeException('Own private file cleanup');}
usleep(200000);echo json_encode(['lane'=>$lane,'pid'=>$pid,'worker_stopped'=>!is_dir('/proc/'.$pid),'port_not_listening'=>$closed,'private_cache_files_remaining'=>0,'private_runtime_files_remaining'=>0,'private_lease_released'=>!file_exists(__DIR__.'/private-lease.json'),'private_journal_removed'=>!file_exists(__DIR__.'/private-write-journal.json'),'private_cache_log_files_removed'=>$removed,'actor_writes'=>0]),"\n";exit($closed?0:2);
