<?php
declare(strict_types=1);
// Only this batch's verified worker and private cache/log files are removed.
if(__DIR__!=='/tmp/cdx-store-finance-reads-20261010'||is_link(__DIR__))exit(78);
$lane=$argv[1]??'';$pid=(int)($argv[2]??0);$port=$lane==='B'?18090:18091;
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('Root cache cleanup');}
usleep(100000);$socket=@fsockopen('127.0.0.1',$port,$n,$e,1);$closed=$socket===false;if($socket)fclose($socket);
echo json_encode(['lane'=>$lane,'worker_stopped'=>$closed,'private_cache_and_logs_removed'=>$removed,'database_writes'=>0,'actor_writes'=>0]),"\n";exit($closed?0:2);
