<?php
declare(strict_types=1);
$tools=__DIR__;$lane=$argv[1]??'';if(!in_array($lane,['B','C'],true))exit(78);$state=json_decode(file_get_contents($tools.'/state-'.$lane.'.json'),true);$runtime=$state['runtime'];$prefix=$lane==='B'?'/private/var/folders/21/wq30qp_s6m5f1fnbzq3vq5b80000gn/T/cdx-client-settings-B-':'/tmp/cdx-client-settings-C-';if(strpos($runtime,$prefix)!==0||!is_dir($runtime))throw new RuntimeException('Exact own private runtime');$report=json_decode(file_get_contents($tools.'/apifox-cli-evidence-'.$lane.'.json'),true);if($report['independent_original_database']['tokens_cache_reclaimed']!==true)throw new RuntimeException('First revoke cache tokens');$socket=@fsockopen('127.0.0.1',$state['port'],$no,$error,1);if($socket!==false)throw new RuntimeException('Own worker must already stop');$removed=0;foreach(glob($runtime.'/private-cli-report/*.json') as $file){$sha=hash_file('sha256',$file);if(!in_array($sha,array_column($report['actual_runs'],'raw_report_sha256'),true))throw new RuntimeException('Only separately archived CLI raw report');if(!unlink($file))throw new RuntimeException('Raw report cleanup failed');$removed++;}foreach(['private-cli-before.json','private-cli-collection.json'] as $name){$f=$runtime.'/'.$name;if(is_file($f)){if(!unlink($f))throw new RuntimeException('Ephemeral token file cleanup failed');$removed++;}}$count=0;if(is_dir($runtime.'/cache'))foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($runtime.'/cache',FilesystemIterator::SKIP_DOTS)) as $f)if($f->isFile())$count++;if($count!==0)throw new RuntimeException('Cache residual');$out=['lane'=>$lane,'own_worker_listening'=>false,'private_cache_files'=>0,'ephemeral_token_files_absent'=>!is_file($runtime.'/private-cli-before.json')&&!is_file($runtime.'/private-cli-collection.json'),'raw_token_bearing_CLI_reports_removed_after_sanitized_SHA_archival'=>true,'removed_files'=>$removed,'retained_business_rows_untouched'=>true];file_put_contents($tools.'/resource-cleanup-'.$lane.'.json',json_encode($out,JSON_PRETTY_PRINT));echo json_encode($out),"\n";
