<?php
declare(strict_types=1);
require __DIR__.'/hardware_contract.php';require __DIR__.'/contract.php';$runtime=getenv('SM7_RUNTIME');$lane=getenv('SM7_LANE');$port=$lane==='B'?18401:18402;if(is_file(__DIR__.'/worker_state.json')){throw new RuntimeException('Stop own worker first');}$socket=@fsockopen('127.0.0.1',$port,$n,$e,1);if($socket){fclose($socket);throw new RuntimeException('Private port still listening');}$db=smPdo();n6Actor($db);$f=smLoad('h7-fixture-private.json');$h=smLoad('h7-preflight-private.json');$a=smLoad('actor-private.json');$own=hcOwnRows($db,$f['seed'],$f['ids']);if(smDigest($own)!==smDigest($f['own_rows'])){throw new RuntimeException('Retained seven rows changed');}hcConsumedSource($db,$f['seed'],$f['ids'],$h,$a,4);
$tokenFiles=['h7-native-collection-private.json','native-report/h7-native.json','native-report/h7-native-raw.json'];$removed=[];foreach($tokenFiles as $file){$path=$runtime.'/'.$file;if(is_link($path)||!is_file($path)){throw new RuntimeException('Expected private token artifact missing');}$removed[]=['file'=>$file,'bytes'=>filesize($path),'sha256'=>hash_file('sha256',$path)];unlink($path);}
$cache=$runtime.'/cache';$cacheRemoved=0;if(is_dir($cache)){if(is_link($cache)||realpath($cache)!==realpath($runtime).'/cache'){throw new RuntimeException('Cache boundary');}$it=new RecursiveIteratorIterator(new RecursiveDirectoryIterator($cache,FilesystemIterator::SKIP_DOTS),RecursiveIteratorIterator::CHILD_FIRST);foreach($it as $file){if($file->isLink()){throw new RuntimeException('Cache symlink');}if($file->isFile()){unlink($file->getPathname());$cacheRemoved++;}elseif($file->isDir()){rmdir($file->getPathname());}}}
$remaining=0;if(is_dir($cache)){foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($cache,FilesystemIterator::SKIP_DOTS)) as $file){if($file->isFile()){$remaining++;}}}$out=['lane'=>$lane,'port'=>$port,'port_listening'=>false,'worker_state_exists'=>false,'private_cache_files_removed'=>$cacheRemoved,'private_cache_files_remaining'=>$remaining,'token_artifacts_removed'=>$removed,'token_artifacts_remaining'=>0,'new_pdo_owned_actor_and_consumed_chain_verified'=>true,'retained_rows'=>7,'fixture_meal_type'=>4,'new_fixture_dml'=>0,'ddl'=>0,'business_rows_deleted'=>0,'shared_cli_cache_preserved'=>'/tmp/cdx-apifox-cli-2.2.8-cache/package'];smSave('cleanup-private.json',$out);echo json_encode(['lane'=>$lane,'port_listening'=>false,'cache_remaining'=>$remaining,'token_artifacts_remaining'=>0,'retained_rows'=>7,'database_dml_ddl'=>0]),PHP_EOL;
