<?php
declare(strict_types=1);
if(__DIR__!=='/tmp/cdx-store-internal-wallet-cent-20261010'||is_link(__DIR__))exit(78);$lane=$argv[1]??'';$pid=(int)($argv[2]??0);if(!in_array($lane,['B','C'],true)||$pid<=1)exit(78);$port=$lane==='B'?18301:18302;
$audit=json_decode((string)file_get_contents(__DIR__.'/audit-private.json'),true);if(empty($audit['all_financial_source_passed'])||empty($audit['financial_source_checks']['zero_active_temp_credentials']))throw new RuntimeException('Financial audit and temporary auth revoke required before runtime cleanup');
$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);usleep(300000);$socket=@fsockopen('127.0.0.1',$port,$n,$e,1);if($socket){fclose($socket);exit(2);}
$removed=0;foreach(['cache','runtime','runtime-cli','cli-cache'] as $name){$path=__DIR__.'/'.$name;if(!is_dir($path))continue;if(is_link($path))throw new RuntimeException('Runtime link');$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path,FilesystemIterator::SKIP_DOTS),RecursiveIteratorIterator::CHILD_FIRST);foreach($it as $item){if($item->isLink())throw new RuntimeException('Nested runtime link');if($item->isDir()){if(!rmdir($item->getPathname()))throw new RuntimeException('Runtime dir removal');}else{if(!unlink($item->getPathname()))throw new RuntimeException('Runtime file removal');++$removed;}}if(!rmdir($path))exit(2);}
foreach(['private-lease.json','private-state.json','private-auth.json','private-before.json','private-error.json','private-request-order-no.json','private-readonly-replay-response.json','result-private.json','audit-private.json','preflight-private.json'] as $file){$path=__DIR__.'/'.$file;if(is_link($path))throw new RuntimeException('Private link');if(is_file($path)){if(!unlink($path))exit(2);++$removed;}}
foreach(['cash_recharge','cash_repeat_cache','cash_repeat_same_forced_number','internal_pay','pay_repeat_cache','pay_repeat_same_msgid_database','refund_apply','internal_refund_agree','refund_repeat_cache','refund_repeat_approved_state'] as $name){$path=__DIR__.'/private-response-'.$name.'.json';if(is_link($path))throw new RuntimeException('Response link');if(is_file($path)){if(!unlink($path))exit(2);++$removed;}}
$report=['lane'=>$lane,'pid'=>$pid,'worker_stopped'=>!is_dir('/proc/'.$pid),'port'=>$port,'port_not_listening'=>true,'private_cache_files_remaining'=>0,'private_runtime_files_remaining'=>0,'private_lease_released'=>!file_exists(__DIR__.'/private-lease.json'),'private_credentials_removed'=>!file_exists(__DIR__.'/private-auth.json'),'private_response_and_journal_removed'=>!file_exists(__DIR__.'/private-state.json'),'private_files_removed'=>$removed,'financial_records_deleted'=>0,'financial_records_intentionally_retained'=>true,'active_temporary_api_auth'=>0];echo json_encode($report),"\n";exit($report['worker_stopped']?0:2);
